+static inline LONG APIENTRY
+Imm32CompStrAnsiToWide(LPCSTR psz, DWORD cb, LPWSTR lpBuf, DWORD dwBufLen, UINT uCodePage)
+{
+ DWORD ret = MultiByteToWideChar(uCodePage, MB_PRECOMPOSED, psz, cb / sizeof(CHAR),
+ lpBuf, dwBufLen / sizeof(WCHAR));
+ if ((ret + 1) * sizeof(WCHAR) <= dwBufLen)
+ lpBuf[ret] = 0;
+ return ret * sizeof(WCHAR);
+}
+
+static inline LONG APIENTRY
+Imm32CompStrWideToAnsi(LPCWSTR psz, DWORD cb, LPSTR lpBuf, DWORD dwBufLen, UINT uCodePage)
+{
+ DWORD ret = WideCharToMultiByte(uCodePage, 0, psz, cb / sizeof(WCHAR),
+ lpBuf, dwBufLen / sizeof(CHAR), NULL, NULL);
+ if ((ret + 1) * sizeof(CHAR) <= dwBufLen)
+ lpBuf[ret] = 0;
+ return ret * sizeof(CHAR);
+}
+
+static INT APIENTRY
+Imm32CompAttrWideToAnsi(const BYTE *src, INT src_len, LPCWSTR text,
+ INT str_len, LPBYTE dst, INT dst_len, UINT uCodePage)
+{
+ INT rc;
+ INT i, j = 0, k = 0, len;
+
+ if (!src_len)
+ return 0;
+
+ rc = WideCharToMultiByte(uCodePage, 0, text, str_len, NULL, 0, NULL, NULL);
+
+ if (dst_len)
+ {
+ if (dst_len > rc)
+ dst_len = rc;
+
+ for (i = 0; i < str_len; ++i, ++k)
+ {
+ len = WideCharToMultiByte(uCodePage, 0, &text[i], 1, NULL, 0, NULL, NULL);
+ for (; len > 0; --len)
+ {
+ dst[j++] = src[k];
+
+ if (dst_len <= j)
+ goto end;
+ }
+ }
+end:
+ rc = j;
+ }
+
+ return rc;
+}
+
+static INT APIENTRY
+Imm32CompAttrAnsiToWide(const BYTE *src, INT src_len, LPCSTR text,
+ INT str_len, LPBYTE dst, INT dst_len, UINT uCodePage)
+{
+ INT rc;
+ INT i, j = 0;
+
+ if (!src_len)
+ return 0;
+
+ rc = MultiByteToWideChar(uCodePage, MB_PRECOMPOSED, text, str_len, NULL, 0);
+
+ if (dst_len)
+ {
+ if (dst_len > rc)
+ dst_len = rc;
+
+ for (i = 0; i < str_len; ++i)
+ {
+ if (IsDBCSLeadByteEx(uCodePage, text[i]) && text[i + 1])
+ continue;
+
+ dst[j++] = src[i];
+
+ if (dst_len <= j)
+ break;
+ }
+
+ rc = j;
+ }
+
+ return rc;
+}
+
+static INT APIENTRY
+Imm32CompClauseAnsiToWide(const DWORD *source, INT slen, LPCSTR text,
+ LPDWORD target, INT tlen, UINT uCodePage)
+{
+ INT rc, i;
+
+ if (!slen)
+ return 0;
+
+ if (tlen)
+ {
+ if (tlen > slen)
+ tlen = slen;
+
+ tlen /= sizeof(DWORD);
+
+ for (i = 0; i < tlen; ++i)
+ {
+ target[i] = MultiByteToWideChar(uCodePage, MB_PRECOMPOSED, text, source[i], NULL, 0);
+ }
+
+ rc = sizeof(DWORD) * i;
+ }
+ else
+ {
+ rc = slen;
+ }
+
+ return rc;
+}
+
+static INT APIENTRY
+Imm32CompClauseWideToAnsi(const DWORD *source, INT slen, LPCWSTR text,
+ LPDWORD target, INT tlen, UINT uCodePage)
+{
+ INT rc, i;
+
+ if (!slen)
+ return 0;
+
+ if (tlen)
+ {
+ if (tlen > slen)
+ tlen = slen;
+
+ tlen /= sizeof(DWORD);
+
+ for (i = 0; i < tlen; ++i)
+ {
+ target[i] = WideCharToMultiByte(uCodePage, 0, text, source[i], NULL, 0, NULL, NULL);
+ }
+
+ rc = sizeof(DWORD) * i;
+ }
+ else
+ {
+ rc = slen;
+ }
+
+ return rc;
+}
+
+#define CS_StrA(pCS, Name) ((LPCSTR)(pCS) + (pCS)->dw##Name##Offset)
+#define CS_StrW(pCS, Name) ((LPCWSTR)CS_StrA(pCS, Name))
+#define CS_Attr(pCS, Name) ((const BYTE *)CS_StrA(pCS, Name))
+#define CS_Clause(pCS, Name) ((const DWORD *)CS_StrA(pCS, Name))
+#define CS_Size(pCS, Name) ((pCS)->dw##Name##Len)
+#define CS_SizeA(pCS, Name) (CS_Size(pCS, Name) * sizeof(CHAR))
+#define CS_SizeW(pCS, Name) (CS_Size(pCS, Name) * sizeof(WCHAR))
+
+#define CS_DoStr(pCS, Name, AorW) do { \
+ if (dwBufLen == 0) { \
+ dwBufLen = CS_Size##AorW((pCS), Name); \
+ } else { \
+ if (dwBufLen > CS_Size##AorW((pCS), Name)) \
+ dwBufLen = CS_Size##AorW((pCS), Name); \
+ RtlCopyMemory(lpBuf, CS_Str##AorW((pCS), Name), dwBufLen); \
+ } \
+} while (0)
+
+#define CS_DoStrA(pCS, Name) CS_DoStr(pCS, Name, A)
+#define CS_DoStrW(pCS, Name) CS_DoStr(pCS, Name, W)
+#define CS_DoAttr CS_DoStrA
+#define CS_DoClause CS_DoStrA
+