OVR_UTF8Util.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**************************************************************************
  2. Filename : OVR_UTF8Util.cpp
  3. Content : UTF8 Unicode character encoding/decoding support
  4. Created : September 19, 2012
  5. Notes :
  6. Notes : Much useful info at "UTF-8 and Unicode FAQ"
  7. http://www.cl.cam.ac.uk/~mgk25/unicode.html
  8. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  9. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  10. you may not use the Oculus VR Rift SDK except in compliance with the License,
  11. which is provided at the time of installation or download, or which
  12. otherwise accompanies this software in either electronic or hard copy form.
  13. You may obtain a copy of the License at
  14. http://www.oculusvr.com/licenses/LICENSE-3.2
  15. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. ************************************************************************************/
  21. #include "OVR_UTF8Util.h"
  22. #include <wchar.h>
  23. #include <string.h>
  24. // sizeof(wchar_t) in preprocessor-accessible form.
  25. #ifndef OVR_WCHAR_SIZE
  26. #if defined(__WCHAR_MAX__)
  27. #if (__WCHAR_MAX__ == 127) || (__WCHAR_MAX__ == 255)
  28. #define OVR_WCHAR_SIZE 1
  29. #elif (__WCHAR_MAX__ == 32767) || (__WCHAR_MAX__ == 65535)
  30. #define OVR_WCHAR_SIZE 2
  31. #else
  32. #define OVR_WCHAR_SIZE 4
  33. #endif
  34. #elif defined(OVR_OS_UNIX)
  35. #define OVR_WCHAR_SIZE 4
  36. #else
  37. #define OVR_WCHAR_SIZE 2
  38. #endif
  39. #endif
  40. namespace OVR { namespace UTF8Util {
  41. size_t Strlcpy(char* pDestUTF8, size_t destCharCount, const wchar_t* pSrcUCS, size_t sourceLength)
  42. {
  43. if (sourceLength == (size_t)-1)
  44. sourceLength = wcslen(pSrcUCS);
  45. size_t destLength = 0, requiredLength = 0;
  46. for (size_t i = 0; (i < sourceLength); ++i)
  47. {
  48. char buff[6]; // longest utf8 encoding just to be safe
  49. intptr_t count = 0;
  50. EncodeChar(buff, &count, pSrcUCS[i]);
  51. // We check requiredLength instead of destLength because we want to make sure that the first time
  52. // that we fail the 'if' below, we don't succeed at it the next time we execute it (which could
  53. // otherwise happen if count were a lower number on the subsequent pass -- low enough that the if
  54. // would succeed).
  55. if ((requiredLength + count) < destCharCount) // If there is enough space to append count bytes (leaving room for a trailing '\0')...
  56. {
  57. memcpy(pDestUTF8 + destLength, buff, count);
  58. destLength += (size_t)count;
  59. }
  60. requiredLength += (size_t)count;
  61. }
  62. if (destLength < destCharCount) // Should be true for all cases other than destCharCount == 0.
  63. pDestUTF8[destLength] = '\0';
  64. return requiredLength; // Return the intended strlen of pDestUTF8.
  65. }
  66. size_t Strlcpy(wchar_t* pDestUCS, size_t destCharCount, const char* pSrcUTF8, size_t sourceLength)
  67. {
  68. if (sourceLength == (size_t)-1)
  69. sourceLength = strlen(pSrcUTF8);
  70. size_t destLength = 0, requiredLength = 0;
  71. for (const char* pSrcUTF8End = (pSrcUTF8 + sourceLength); pSrcUTF8 < pSrcUTF8End; )
  72. {
  73. uint32_t c = DecodeNextChar_Advance0(&pSrcUTF8);
  74. OVR_ASSERT_M(pSrcUTF8 <= (pSrcUTF8 + sourceLength), "Strlcpy sourceLength was not on a UTF8 boundary.");
  75. #if (OVR_WCHAR_SIZE == 2)
  76. if (c >= 0x0000FFFF)
  77. c = 0x0000FFFD;
  78. #endif
  79. if((destLength + 1) < destCharCount) // If there is enough space to append a wchar_t (leaving room for a trailing '\0')...
  80. {
  81. pDestUCS[destLength] = wchar_t(c);
  82. destLength++;
  83. }
  84. requiredLength++;
  85. }
  86. if (destLength < destCharCount)
  87. pDestUCS[destLength] = L'\0';
  88. return requiredLength; // Return the intended wcslen of pDestUCS.
  89. }
  90. intptr_t GetLength(const char* buf, intptr_t buflen)
  91. {
  92. const char* p = buf;
  93. intptr_t length = 0;
  94. if (buflen != -1)
  95. {
  96. while (p - buf < buflen)
  97. {
  98. // We should be able to have ASStrings with 0 in the middle.
  99. UTF8Util::DecodeNextChar_Advance0(&p);
  100. length++;
  101. }
  102. }
  103. else
  104. {
  105. while (UTF8Util::DecodeNextChar_Advance0(&p))
  106. length++;
  107. }
  108. return length;
  109. }
  110. uint32_t GetCharAt(intptr_t index, const char* putf8str, intptr_t length)
  111. {
  112. const char* buf = putf8str;
  113. uint32_t c = 0;
  114. if (length != -1)
  115. {
  116. while (buf - putf8str < length)
  117. {
  118. c = UTF8Util::DecodeNextChar_Advance0(&buf);
  119. if (index == 0)
  120. return c;
  121. index--;
  122. }
  123. return c;
  124. }
  125. do
  126. {
  127. c = UTF8Util::DecodeNextChar_Advance0(&buf);
  128. index--;
  129. if (c == 0)
  130. {
  131. // We've hit the end of the string; don't go further.
  132. OVR_ASSERT(index == 0);
  133. return c;
  134. }
  135. } while (index >= 0);
  136. return c;
  137. }
  138. intptr_t GetByteIndex(intptr_t index, const char *putf8str, intptr_t length)
  139. {
  140. const char* buf = putf8str;
  141. if (length != -1)
  142. {
  143. while ((buf - putf8str) < length && index > 0)
  144. {
  145. UTF8Util::DecodeNextChar_Advance0(&buf);
  146. index--;
  147. }
  148. return buf-putf8str;
  149. }
  150. while (index > 0)
  151. {
  152. uint32_t c = UTF8Util::DecodeNextChar_Advance0(&buf);
  153. index--;
  154. if (c == 0)
  155. return buf-putf8str;
  156. };
  157. return buf-putf8str;
  158. }
  159. int GetEncodeCharSize(uint32_t ucs_character)
  160. {
  161. if (ucs_character <= 0x7F)
  162. return 1;
  163. else if (ucs_character <= 0x7FF)
  164. return 2;
  165. else if (ucs_character <= 0xFFFF)
  166. return 3;
  167. else if (ucs_character <= 0x1FFFFF)
  168. return 4;
  169. else if (ucs_character <= 0x3FFFFFF)
  170. return 5;
  171. else if (ucs_character <= 0x7FFFFFFF)
  172. return 6;
  173. else
  174. return 0;
  175. }
  176. uint32_t DecodeNextChar_Advance0(const char** putf8Buffer)
  177. {
  178. uint32_t uc;
  179. char c;
  180. // Security considerations:
  181. //
  182. // Changed, this is now only the case for DecodeNextChar:
  183. // - If we hit a zero byte, we want to return 0 without stepping
  184. // the buffer pointer past the 0. th
  185. //
  186. // If we hit an "overlong sequence"; i.e. a character encoded
  187. // in a longer multibyte string than is necessary, then we
  188. // need to discard the character. This is so attackers can't
  189. // disguise dangerous characters or character sequences --
  190. // there is only one valid encoding for each character.
  191. //
  192. // If we decode characters { 0xD800 .. 0xDFFF } or { 0xFFFE,
  193. // 0xFFFF } then we ignore them; they are not valid in UTF-8.
  194. // This isn't actually an invalid character; it's a valid char that
  195. // looks like an inverted question mark.
  196. #define INVALID_CHAR 0x0FFFD
  197. #define FIRST_BYTE(mask, shift) \
  198. uc = (c & (mask)) << (shift);
  199. #define NEXT_BYTE(shift) \
  200. c = **putf8Buffer; \
  201. if (c == 0) return 0; /* end of buffer, do not advance */ \
  202. if ((c & 0xC0) != 0x80) return INVALID_CHAR; /* standard check */ \
  203. (*putf8Buffer)++; \
  204. uc |= (c & 0x3F) << shift;
  205. c = **putf8Buffer;
  206. (*putf8Buffer)++;
  207. if (c == 0)
  208. return 0; // End of buffer.
  209. if ((c & 0x80) == 0) return (uint32_t) c; // Conventional 7-bit ASCII.
  210. // Multi-byte sequences.
  211. if ((c & 0xE0) == 0xC0)
  212. {
  213. // Two-byte sequence.
  214. FIRST_BYTE(0x1F, 6);
  215. NEXT_BYTE(0);
  216. if (uc < 0x80) return INVALID_CHAR; // overlong
  217. return uc;
  218. }
  219. else if ((c & 0xF0) == 0xE0)
  220. {
  221. // Three-byte sequence.
  222. FIRST_BYTE(0x0F, 12);
  223. NEXT_BYTE(6);
  224. NEXT_BYTE(0);
  225. if (uc < 0x800) return INVALID_CHAR; // overlong
  226. // Not valid ISO 10646, but Flash requires these to work
  227. // see AS3 test e15_5_3_2_3 for String.fromCharCode().charCodeAt(0)
  228. // if (uc >= 0x0D800 && uc <= 0x0DFFF) return INVALID_CHAR;
  229. // if (uc == 0x0FFFE || uc == 0x0FFFF) return INVALID_CHAR; // not valid ISO 10646
  230. return uc;
  231. }
  232. else if ((c & 0xF8) == 0xF0)
  233. {
  234. // Four-byte sequence.
  235. FIRST_BYTE(0x07, 18);
  236. NEXT_BYTE(12);
  237. NEXT_BYTE(6);
  238. NEXT_BYTE(0);
  239. if (uc < 0x010000) return INVALID_CHAR; // overlong
  240. return uc;
  241. }
  242. else if ((c & 0xFC) == 0xF8)
  243. {
  244. // Five-byte sequence.
  245. FIRST_BYTE(0x03, 24);
  246. NEXT_BYTE(18);
  247. NEXT_BYTE(12);
  248. NEXT_BYTE(6);
  249. NEXT_BYTE(0);
  250. if (uc < 0x0200000) return INVALID_CHAR; // overlong
  251. return uc;
  252. }
  253. else if ((c & 0xFE) == 0xFC)
  254. {
  255. // Six-byte sequence.
  256. FIRST_BYTE(0x01, 30);
  257. NEXT_BYTE(24);
  258. NEXT_BYTE(18);
  259. NEXT_BYTE(12);
  260. NEXT_BYTE(6);
  261. NEXT_BYTE(0);
  262. if (uc < 0x04000000) return INVALID_CHAR; // overlong
  263. return uc;
  264. }
  265. else
  266. {
  267. // Invalid.
  268. return INVALID_CHAR;
  269. }
  270. }
  271. void EncodeChar(char* pbuffer, intptr_t* pindex, uint32_t ucs_character)
  272. {
  273. if (ucs_character <= 0x7F)
  274. {
  275. // Plain single-byte ASCII.
  276. pbuffer[(*pindex)++] = (char) ucs_character;
  277. }
  278. else if (ucs_character <= 0x7FF)
  279. {
  280. // Two bytes.
  281. pbuffer[(*pindex)++] = 0xC0 | (char)(ucs_character >> 6);
  282. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 0) & 0x3F);
  283. }
  284. else if (ucs_character <= 0xFFFF)
  285. {
  286. // Three bytes.
  287. pbuffer[(*pindex)++] = 0xE0 | (char)(ucs_character >> 12);
  288. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 6) & 0x3F);
  289. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 0) & 0x3F);
  290. }
  291. else if (ucs_character <= 0x1FFFFF)
  292. {
  293. // Four bytes.
  294. pbuffer[(*pindex)++] = 0xF0 | (char)(ucs_character >> 18);
  295. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 12) & 0x3F);
  296. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 6) & 0x3F);
  297. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 0) & 0x3F);
  298. }
  299. else if (ucs_character <= 0x3FFFFFF)
  300. {
  301. // Five bytes.
  302. pbuffer[(*pindex)++] = 0xF8 | (char)(ucs_character >> 24);
  303. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 18) & 0x3F);
  304. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 12) & 0x3F);
  305. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 6) & 0x3F);
  306. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 0) & 0x3F);
  307. }
  308. else if (ucs_character <= 0x7FFFFFFF)
  309. {
  310. // Six bytes.
  311. pbuffer[(*pindex)++] = 0xFC | (char)(ucs_character >> 30);
  312. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 24) & 0x3F);
  313. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 18) & 0x3F);
  314. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 12) & 0x3F);
  315. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 6) & 0x3F);
  316. pbuffer[(*pindex)++] = 0x80 | (char)((ucs_character >> 0) & 0x3F);
  317. }
  318. else
  319. {
  320. // Invalid char; don't encode anything.
  321. }
  322. }
  323. intptr_t GetEncodeStringSize(const wchar_t* pchar, intptr_t length)
  324. {
  325. intptr_t len = 0;
  326. if (length != -1)
  327. for (int i = 0; i < length; i++)
  328. {
  329. len += GetEncodeCharSize(pchar[i]);
  330. }
  331. else
  332. for (int i = 0;; i++)
  333. {
  334. if (pchar[i] == 0)
  335. return len;
  336. len += GetEncodeCharSize(pchar[i]);
  337. }
  338. return len;
  339. }
  340. void EncodeString(char *pbuff, const wchar_t* pchar, intptr_t length)
  341. {
  342. intptr_t ofs = 0;
  343. if (length != -1)
  344. {
  345. for (int i = 0; i < length; i++)
  346. {
  347. EncodeChar(pbuff, &ofs, pchar[i]);
  348. }
  349. }
  350. else
  351. {
  352. for (int i = 0;; i++)
  353. {
  354. if (pchar[i] == 0)
  355. break;
  356. EncodeChar(pbuff, &ofs, pchar[i]);
  357. }
  358. }
  359. pbuff[ofs] = 0;
  360. }
  361. size_t DecodeString(wchar_t *pbuff, const char* putf8str, intptr_t bytesLen)
  362. {
  363. wchar_t *pbegin = pbuff;
  364. if (bytesLen == -1)
  365. {
  366. while (1)
  367. {
  368. uint32_t ch = DecodeNextChar_Advance0(&putf8str);
  369. if (ch == 0)
  370. break;
  371. else if (ch >= 0xFFFF)
  372. ch = 0xFFFD;
  373. *pbuff++ = wchar_t(ch);
  374. }
  375. }
  376. else
  377. {
  378. const char* p = putf8str;
  379. while ((p - putf8str) < bytesLen)
  380. {
  381. uint32_t ch = DecodeNextChar_Advance0(&p);
  382. if (ch >= 0xFFFF)
  383. ch = 0xFFFD;
  384. *pbuff++ = wchar_t(ch);
  385. }
  386. }
  387. *pbuff = 0;
  388. return pbuff - pbegin;
  389. }
  390. }} // namespace UTF8Util::OVR