Unicode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Unicode.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides utitlity functions to work with Unicode and other encodings. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifdef _WIN32
  12. #include <specstrings.h>
  13. #else
  14. #include <clocale>
  15. #endif
  16. #include <string>
  17. #include "dxc/Support/Global.h"
  18. #include "dxc/Support/Unicode.h"
  19. #include "dxc/Support/WinIncludes.h"
  20. #ifndef _WIN32
  21. // MultiByteToWideChar which is a Windows-specific method.
  22. // This is a very simplistic implementation for non-Windows platforms. This
  23. // implementation completely ignores CodePage and dwFlags.
  24. int MultiByteToWideChar(uint32_t CodePage, uint32_t /*dwFlags*/,
  25. const char *lpMultiByteStr, int cbMultiByte,
  26. wchar_t *lpWideCharStr, int cchWideChar) {
  27. if (cbMultiByte == 0) {
  28. SetLastError(ERROR_INVALID_PARAMETER);
  29. return 0;
  30. }
  31. // if cbMultiByte is -1, it indicates that lpMultiByteStr is null-terminated
  32. // and the entire string should be processed.
  33. if (cbMultiByte == -1) {
  34. for (cbMultiByte = 0; lpMultiByteStr[cbMultiByte] != '\0'; ++cbMultiByte)
  35. ;
  36. // Add 1 for the null-terminating character.
  37. ++cbMultiByte;
  38. }
  39. // If zero is given as the destination size, this function should
  40. // return the required size (including the null-terminating character).
  41. // This is the behavior of mbstowcs when the target is null.
  42. if (cchWideChar == 0) {
  43. lpWideCharStr = nullptr;
  44. } else if (cchWideChar < cbMultiByte) {
  45. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  46. return 0;
  47. }
  48. size_t rv;
  49. const char *locale = CPToLocale(CodePage);
  50. locale = setlocale(LC_ALL, locale);
  51. if (lpMultiByteStr[cbMultiByte] != '\0') {
  52. char *srcStr = (char *)malloc((cbMultiByte +1) * sizeof(char));
  53. strncpy(srcStr, lpMultiByteStr, cbMultiByte);
  54. srcStr[cbMultiByte]='\0';
  55. rv = mbstowcs(lpWideCharStr, srcStr, cchWideChar);
  56. free(srcStr);
  57. } else {
  58. rv = mbstowcs(lpWideCharStr, lpMultiByteStr, cchWideChar);
  59. }
  60. setlocale(LC_ALL, locale);
  61. if (rv == (size_t)cbMultiByte) return rv;
  62. return rv + 1; // mbstowcs excludes the terminating character
  63. }
  64. // WideCharToMultiByte is a Windows-specific method.
  65. // This is a very simplistic implementation for non-Windows platforms. This
  66. // implementation completely ignores CodePage and dwFlags.
  67. int WideCharToMultiByte(uint32_t CodePage, uint32_t /*dwFlags*/,
  68. const wchar_t *lpWideCharStr, int cchWideChar,
  69. char *lpMultiByteStr, int cbMultiByte,
  70. const char * /*lpDefaultChar*/,
  71. bool * /*lpUsedDefaultChar*/) {
  72. if (cchWideChar == 0) {
  73. SetLastError(ERROR_INVALID_PARAMETER);
  74. return 0;
  75. }
  76. // if cchWideChar is -1, it indicates that lpWideCharStr is null-terminated
  77. // and the entire string should be processed.
  78. if (cchWideChar == -1) {
  79. for (cchWideChar = 0; lpWideCharStr[cchWideChar] != '\0'; ++cchWideChar)
  80. ;
  81. // Add 1 for the null-terminating character.
  82. ++cchWideChar;
  83. }
  84. // If zero is given as the destination size, this function should
  85. // return the required size (including the null-terminating character).
  86. // This is the behavior of wcstombs when the target is null.
  87. if (cbMultiByte == 0) {
  88. lpMultiByteStr = nullptr;
  89. } else if (cbMultiByte < cchWideChar) {
  90. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  91. return 0;
  92. }
  93. size_t rv;
  94. const char *locale = CPToLocale(CodePage);
  95. locale = setlocale(LC_ALL, locale);
  96. if (lpWideCharStr[cchWideChar] != L'\0') {
  97. wchar_t *srcStr = (wchar_t *)malloc((cchWideChar+1) * sizeof(wchar_t));
  98. wcsncpy(srcStr, lpWideCharStr, cchWideChar);
  99. srcStr[cchWideChar] = L'\0';
  100. rv = wcstombs(lpMultiByteStr, srcStr, cbMultiByte);
  101. free(srcStr);
  102. } else {
  103. rv = wcstombs(lpMultiByteStr, lpWideCharStr, cbMultiByte);
  104. }
  105. setlocale(LC_ALL, locale);
  106. if (rv == (size_t)cchWideChar) return rv;
  107. return rv + 1; // mbstowcs excludes the terminating character
  108. }
  109. #endif // _WIN32
  110. namespace Unicode {
  111. _Success_(return != false)
  112. bool UTF16ToEncodedString(_In_z_ const wchar_t* text, DWORD cp, DWORD flags, _Inout_ std::string* pValue, _Out_opt_ bool* lossy) {
  113. BOOL usedDefaultChar;
  114. LPBOOL pUsedDefaultChar = (lossy == nullptr) ? nullptr : &usedDefaultChar;
  115. size_t cUTF16 = wcslen(text);
  116. if (lossy != nullptr) *lossy = false;
  117. // Handle zero-length as a special case; it's a special value to indicate errors in WideCharToMultiByte.
  118. if (cUTF16 == 0) {
  119. pValue->resize(0);
  120. DXASSERT(lossy == nullptr || *lossy == false, "otherwise earlier initialization in this function was updated");
  121. return true;
  122. }
  123. int cbUTF8 = ::WideCharToMultiByte(cp, flags, text, cUTF16, nullptr, 0, nullptr, pUsedDefaultChar);
  124. if (cbUTF8 == 0)
  125. return false;
  126. pValue->resize(cbUTF8);
  127. cbUTF8 = ::WideCharToMultiByte(cp, flags, text, cUTF16, &(*pValue)[0], pValue->size(), nullptr, pUsedDefaultChar);
  128. DXASSERT(cbUTF8 > 0, "otherwise contents have changed");
  129. DXASSERT((*pValue)[pValue->size()] == '\0', "otherwise string didn't null-terminate after resize() call");
  130. if (lossy != nullptr) *lossy = usedDefaultChar;
  131. return true;
  132. }
  133. _Use_decl_annotations_
  134. bool UTF8ToUTF16String(const char *pUTF8, std::wstring *pUTF16) {
  135. size_t cbUTF8 = (pUTF8 == nullptr) ? 0 : strlen(pUTF8);
  136. return UTF8ToUTF16String(pUTF8, cbUTF8, pUTF16);
  137. }
  138. _Use_decl_annotations_
  139. bool UTF8ToUTF16String(const char *pUTF8, size_t cbUTF8, std::wstring *pUTF16) {
  140. DXASSERT_NOMSG(pUTF16 != nullptr);
  141. // Handle zero-length as a special case; it's a special value to indicate
  142. // errors in MultiByteToWideChar.
  143. if (cbUTF8 == 0) {
  144. pUTF16->resize(0);
  145. return true;
  146. }
  147. int cUTF16 = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8,
  148. cbUTF8, nullptr, 0);
  149. if (cUTF16 == 0)
  150. return false;
  151. pUTF16->resize(cUTF16);
  152. cUTF16 = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, cbUTF8,
  153. &(*pUTF16)[0], pUTF16->size());
  154. DXASSERT(cUTF16 > 0, "otherwise contents changed");
  155. DXASSERT((*pUTF16)[pUTF16->size()] == L'\0',
  156. "otherwise wstring didn't null-terminate after resize() call");
  157. return true;
  158. }
  159. std::wstring UTF8ToUTF16StringOrThrow(_In_z_ const char *pUTF8) {
  160. std::wstring result;
  161. if (!UTF8ToUTF16String(pUTF8, &result)) {
  162. throw hlsl::Exception(DXC_E_STRING_ENCODING_FAILED);
  163. }
  164. return result;
  165. }
  166. _Use_decl_annotations_
  167. bool UTF8ToConsoleString(_In_z_ const char* text, _Inout_ std::string* pValue, _Out_opt_ bool* lossy) {
  168. DXASSERT_NOMSG(text != nullptr);
  169. DXASSERT_NOMSG(pValue != nullptr);
  170. std::wstring text16;
  171. if (lossy != nullptr) *lossy = false;
  172. if (!UTF8ToUTF16String(text, &text16)) {
  173. return false;
  174. }
  175. return UTF16ToConsoleString(text16.c_str(), pValue, lossy);
  176. }
  177. _Use_decl_annotations_
  178. bool UTF16ToConsoleString(const wchar_t* text, std::string* pValue, bool* lossy) {
  179. DXASSERT_NOMSG(text != nullptr);
  180. DXASSERT_NOMSG(pValue != nullptr);
  181. UINT cp = GetConsoleOutputCP();
  182. return UTF16ToEncodedString(text, cp, 0, pValue, lossy);
  183. }
  184. _Use_decl_annotations_
  185. bool UTF16ToUTF8String(const wchar_t *pUTF16, std::string *pUTF8) {
  186. DXASSERT_NOMSG(pUTF16 != nullptr);
  187. DXASSERT_NOMSG(pUTF8 != nullptr);
  188. return UTF16ToEncodedString(pUTF16, CP_UTF8, 0, pUTF8, nullptr);
  189. }
  190. std::string UTF16ToUTF8StringOrThrow(_In_z_ const wchar_t *pUTF16) {
  191. std::string result;
  192. if (!UTF16ToUTF8String(pUTF16, &result)) {
  193. throw hlsl::Exception(DXC_E_STRING_ENCODING_FAILED);
  194. }
  195. return result;
  196. }
  197. _Use_decl_annotations_
  198. bool UTF8BufferToUTF16ComHeap(const char *pUTF8, wchar_t **ppUTF16) throw() {
  199. *ppUTF16 = nullptr;
  200. int c = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, -1,
  201. nullptr, 0);
  202. if (c == 0)
  203. return false;
  204. CComHeapPtr<wchar_t> p;
  205. if (!p.Allocate(c))
  206. return false;
  207. DXVERIFY_NOMSG(0 < ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8,
  208. -1, p.m_pData, c));
  209. *ppUTF16 = p.Detach();
  210. return true;
  211. }
  212. _Use_decl_annotations_
  213. bool UTF8BufferToUTF16Buffer(const char *pUTF8, int cbUTF8, wchar_t **ppUTF16, size_t *pcUTF16) throw() {
  214. *ppUTF16 = nullptr;
  215. *pcUTF16 = 0;
  216. if (cbUTF8 == 0 || (cbUTF8 == -1 && *pUTF8 == '\0')) {
  217. *ppUTF16 = new (std::nothrow) wchar_t[1];
  218. if (*ppUTF16 == nullptr)
  219. return false;
  220. (*ppUTF16)[0] = L'\0';
  221. *pcUTF16 = 1;
  222. return true;
  223. }
  224. int c = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pUTF8, cbUTF8, nullptr, 0);
  225. if (c == 0)
  226. return false;
  227. // add space for null-terminator if we're not accounting for it
  228. if (cbUTF8 != -1)
  229. c += 1;
  230. wchar_t *p = new (std::nothrow) wchar_t[c];
  231. if (p == nullptr)
  232. return false;
  233. int converted = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  234. pUTF8, cbUTF8,
  235. p, c);
  236. (void)converted;
  237. DXASSERT(converted > 0, "otherwise contents have changed");
  238. p[c - 1] = L'\0';
  239. *ppUTF16 = p;
  240. *pcUTF16 = c;
  241. return true;
  242. }
  243. _Use_decl_annotations_
  244. bool UTF16BufferToUTF8Buffer(const wchar_t *pUTF16, int cUTF16, char **ppUTF8, size_t *pcUTF8) throw() {
  245. *ppUTF8 = nullptr;
  246. *pcUTF8 = 0;
  247. if (cUTF16 == 0 || (cUTF16 == -1 && *pUTF16 == '\0')) {
  248. *ppUTF8 = new (std::nothrow) char[1];
  249. if (*ppUTF8 == nullptr)
  250. return false;
  251. (*ppUTF8)[0] = '\0';
  252. *pcUTF8 = 1;
  253. return true;
  254. }
  255. int c1 = ::WideCharToMultiByte(CP_UTF8, // code page
  256. 0, // flags
  257. pUTF16, // string to convert
  258. cUTF16, // size, in chars, of string to convert
  259. nullptr, // output buffer
  260. 0, // size of output buffer
  261. nullptr, nullptr);
  262. if (c1 == 0)
  263. return false;
  264. // add space for null-terminator if we're not accounting for it
  265. if (cUTF16 != -1)
  266. c1 += 1;
  267. char *p = new (std::nothrow) char[c1];
  268. if (p == nullptr)
  269. return false;
  270. int converted = ::WideCharToMultiByte(CP_UTF8, 0,
  271. pUTF16, cUTF16,
  272. p, c1,
  273. nullptr, nullptr);
  274. (void)converted;
  275. DXASSERT(converted > 0, "otherwise contents have changed");
  276. p[c1 - 1] = '\0';
  277. *ppUTF8 = p;
  278. *pcUTF8 = c1;
  279. return true;
  280. }
  281. template<typename TChar>
  282. static
  283. bool IsStarMatchT(const TChar *pMask, size_t maskLen, const TChar *pName, size_t nameLen, TChar star) {
  284. if (maskLen == 0 && nameLen == 0) {
  285. return true;
  286. }
  287. if (maskLen == 0 || nameLen == 0) {
  288. return false;
  289. }
  290. if (pMask[maskLen - 1] == star) {
  291. // Prefix match.
  292. if (maskLen == 1) { // For just '*', everything is a match.
  293. return true;
  294. }
  295. --maskLen;
  296. if (maskLen > nameLen) { // Mask is longer than name, can't be a match.
  297. return false;
  298. }
  299. return 0 == memcmp(pMask, pName, sizeof(TChar) * maskLen);
  300. }
  301. else {
  302. // Exact match.
  303. if (nameLen != maskLen) {
  304. return false;
  305. }
  306. return 0 == memcmp(pMask, pName, sizeof(TChar) * nameLen);
  307. }
  308. }
  309. _Use_decl_annotations_
  310. bool IsStarMatchUTF8(const char *pMask, size_t maskLen, const char *pName, size_t nameLen) {
  311. return IsStarMatchT<char>(pMask, maskLen, pName, nameLen, '*');
  312. }
  313. _Use_decl_annotations_
  314. bool IsStarMatchUTF16(const wchar_t *pMask, size_t maskLen, const wchar_t *pName, size_t nameLen) {
  315. return IsStarMatchT<wchar_t>(pMask, maskLen, pName, nameLen, L'*');
  316. }
  317. } // namespace Unicode