physfs_unicode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #define __PHYSICSFS_INTERNAL__
  2. #include "physfs_internal.h"
  3. #include "physfs_casefolding.h"
  4. /*
  5. * From rfc3629, the UTF-8 spec:
  6. * https://www.ietf.org/rfc/rfc3629.txt
  7. *
  8. * Char. number range | UTF-8 octet sequence
  9. * (hexadecimal) | (binary)
  10. * --------------------+---------------------------------------------
  11. * 0000 0000-0000 007F | 0xxxxxxx
  12. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  13. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  14. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  15. */
  16. /*
  17. * This may not be the best value, but it's one that isn't represented
  18. * in Unicode (0x10FFFF is the largest codepoint value). We return this
  19. * value from __PHYSFS_utf8codepoint() if there's bogus bits in the
  20. * stream. __PHYSFS_utf8codepoint() will turn this value into something
  21. * reasonable (like a question mark), for text that wants to try to recover,
  22. * whereas utf8valid() will use the value to determine if a string has bad
  23. * bits.
  24. */
  25. #define UNICODE_BOGUS_CHAR_VALUE 0xFFFFFFFF
  26. /*
  27. * This is the codepoint we currently return when there was bogus bits in a
  28. * UTF-8 string. May not fly in Asian locales?
  29. */
  30. #define UNICODE_BOGUS_CHAR_CODEPOINT '?'
  31. PHYSFS_uint32 __PHYSFS_utf8codepoint(const char **_str)
  32. {
  33. const char *str = *_str;
  34. PHYSFS_uint32 retval = 0;
  35. PHYSFS_uint32 octet = (PHYSFS_uint32) ((PHYSFS_uint8) *str);
  36. PHYSFS_uint32 octet2, octet3, octet4;
  37. if (octet == 0) /* null terminator, end of string. */
  38. return 0;
  39. else if (octet < 128) /* one octet char: 0 to 127 */
  40. {
  41. (*_str)++; /* skip to next possible start of codepoint. */
  42. return octet;
  43. } /* else if */
  44. else if ((octet > 127) && (octet < 192)) /* bad (starts with 10xxxxxx). */
  45. {
  46. /*
  47. * Apparently each of these is supposed to be flagged as a bogus
  48. * char, instead of just resyncing to the next valid codepoint.
  49. */
  50. (*_str)++; /* skip to next possible start of codepoint. */
  51. return UNICODE_BOGUS_CHAR_VALUE;
  52. } /* else if */
  53. else if (octet < 224) /* two octets */
  54. {
  55. (*_str)++; /* advance at least one byte in case of an error */
  56. octet -= (128+64);
  57. octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  58. if ((octet2 & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  59. return UNICODE_BOGUS_CHAR_VALUE;
  60. *_str += 1; /* skip to next possible start of codepoint. */
  61. retval = ((octet << 6) | (octet2 - 128));
  62. if ((retval >= 0x80) && (retval <= 0x7FF))
  63. return retval;
  64. } /* else if */
  65. else if (octet < 240) /* three octets */
  66. {
  67. (*_str)++; /* advance at least one byte in case of an error */
  68. octet -= (128+64+32);
  69. octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  70. if ((octet2 & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  71. return UNICODE_BOGUS_CHAR_VALUE;
  72. octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  73. if ((octet3 & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  74. return UNICODE_BOGUS_CHAR_VALUE;
  75. *_str += 2; /* skip to next possible start of codepoint. */
  76. retval = ( ((octet << 12)) | ((octet2-128) << 6) | ((octet3-128)) );
  77. /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
  78. switch (retval)
  79. {
  80. case 0xD800:
  81. case 0xDB7F:
  82. case 0xDB80:
  83. case 0xDBFF:
  84. case 0xDC00:
  85. case 0xDF80:
  86. case 0xDFFF:
  87. return UNICODE_BOGUS_CHAR_VALUE;
  88. } /* switch */
  89. /* 0xFFFE and 0xFFFF are illegal, too, so we check them at the edge. */
  90. if ((retval >= 0x800) && (retval <= 0xFFFD))
  91. return retval;
  92. } /* else if */
  93. else if (octet < 248) /* four octets */
  94. {
  95. (*_str)++; /* advance at least one byte in case of an error */
  96. octet -= (128+64+32+16);
  97. octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  98. if ((octet2 & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  99. return UNICODE_BOGUS_CHAR_VALUE;
  100. octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  101. if ((octet3 & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  102. return UNICODE_BOGUS_CHAR_VALUE;
  103. octet4 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  104. if ((octet4 & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  105. return UNICODE_BOGUS_CHAR_VALUE;
  106. *_str += 3; /* skip to next possible start of codepoint. */
  107. retval = ( ((octet << 18)) | ((octet2 - 128) << 12) |
  108. ((octet3 - 128) << 6) | ((octet4 - 128)) );
  109. if ((retval >= 0x10000) && (retval <= 0x10FFFF))
  110. return retval;
  111. } /* else if */
  112. /*
  113. * Five and six octet sequences became illegal in rfc3629.
  114. * We throw the codepoint away, but parse them to make sure we move
  115. * ahead the right number of bytes and don't overflow the buffer.
  116. */
  117. else if (octet < 252) /* five octets */
  118. {
  119. (*_str)++; /* advance at least one byte in case of an error */
  120. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  121. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  122. return UNICODE_BOGUS_CHAR_VALUE;
  123. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  124. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  125. return UNICODE_BOGUS_CHAR_VALUE;
  126. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  127. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  128. return UNICODE_BOGUS_CHAR_VALUE;
  129. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  130. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  131. return UNICODE_BOGUS_CHAR_VALUE;
  132. *_str += 4; /* skip to next possible start of codepoint. */
  133. return UNICODE_BOGUS_CHAR_VALUE;
  134. } /* else if */
  135. else /* six octets */
  136. {
  137. (*_str)++; /* advance at least one byte in case of an error */
  138. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  139. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  140. return UNICODE_BOGUS_CHAR_VALUE;
  141. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  142. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  143. return UNICODE_BOGUS_CHAR_VALUE;
  144. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  145. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  146. return UNICODE_BOGUS_CHAR_VALUE;
  147. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  148. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  149. return UNICODE_BOGUS_CHAR_VALUE;
  150. octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
  151. if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
  152. return UNICODE_BOGUS_CHAR_VALUE;
  153. *_str += 5; /* skip to next possible start of codepoint. */
  154. return UNICODE_BOGUS_CHAR_VALUE;
  155. } /* else if */
  156. return UNICODE_BOGUS_CHAR_VALUE;
  157. } /* __PHYSFS_utf8codepoint */
  158. static inline PHYSFS_uint32 utf8codepoint(const char **_str)
  159. {
  160. return __PHYSFS_utf8codepoint(_str);
  161. } /* utf8codepoint */
  162. static PHYSFS_uint32 utf16codepoint(const PHYSFS_uint16 **_str)
  163. {
  164. const PHYSFS_uint16 *src = *_str;
  165. PHYSFS_uint32 cp = (PHYSFS_uint32) *(src++);
  166. if (cp == 0) /* null terminator, end of string. */
  167. return 0;
  168. /* Orphaned second half of surrogate pair? */
  169. else if ((cp >= 0xDC00) && (cp <= 0xDFFF))
  170. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  171. else if ((cp >= 0xD800) && (cp <= 0xDBFF)) /* start surrogate pair! */
  172. {
  173. const PHYSFS_uint32 pair = (PHYSFS_uint32) *src;
  174. if (pair == 0)
  175. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  176. else if ((pair < 0xDC00) || (pair > 0xDFFF))
  177. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  178. else
  179. {
  180. src++; /* eat the other surrogate. */
  181. cp = 0x10000 + (((cp - 0xD800) << 10) | (pair - 0xDC00));
  182. } /* else */
  183. } /* else if */
  184. *_str = src;
  185. return cp;
  186. } /* utf16codepoint */
  187. static PHYSFS_uint32 utf32codepoint(const PHYSFS_uint32 **_str)
  188. {
  189. const PHYSFS_uint32 *src = *_str;
  190. PHYSFS_uint32 cp = *(src++);
  191. if (cp == 0) /* null terminator, end of string. */
  192. return 0;
  193. else if (cp > 0x10FFF)
  194. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  195. *_str = src;
  196. return cp;
  197. } /* utf32codepoint */
  198. void PHYSFS_utf8ToUcs4(const char *src, PHYSFS_uint32 *dst, PHYSFS_uint64 len)
  199. {
  200. len -= sizeof (PHYSFS_uint32); /* save room for null char. */
  201. while (len >= sizeof (PHYSFS_uint32))
  202. {
  203. PHYSFS_uint32 cp = __PHYSFS_utf8codepoint(&src);
  204. if (cp == 0)
  205. break;
  206. else if (cp == UNICODE_BOGUS_CHAR_VALUE)
  207. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  208. *(dst++) = cp;
  209. len -= sizeof (PHYSFS_uint32);
  210. } /* while */
  211. *dst = 0;
  212. } /* PHYSFS_utf8ToUcs4 */
  213. void PHYSFS_utf8ToUcs2(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len)
  214. {
  215. len -= sizeof (PHYSFS_uint16); /* save room for null char. */
  216. while (len >= sizeof (PHYSFS_uint16))
  217. {
  218. PHYSFS_uint32 cp = __PHYSFS_utf8codepoint(&src);
  219. if (cp == 0)
  220. break;
  221. else if (cp == UNICODE_BOGUS_CHAR_VALUE)
  222. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  223. if (cp > 0xFFFF) /* UTF-16 surrogates (bogus chars in UCS-2) */
  224. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  225. *(dst++) = cp;
  226. len -= sizeof (PHYSFS_uint16);
  227. } /* while */
  228. *dst = 0;
  229. } /* PHYSFS_utf8ToUcs2 */
  230. void PHYSFS_utf8ToUtf16(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len)
  231. {
  232. len -= sizeof (PHYSFS_uint16); /* save room for null char. */
  233. while (len >= sizeof (PHYSFS_uint16))
  234. {
  235. PHYSFS_uint32 cp = __PHYSFS_utf8codepoint(&src);
  236. if (cp == 0)
  237. break;
  238. else if (cp == UNICODE_BOGUS_CHAR_VALUE)
  239. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  240. if (cp > 0xFFFF) /* encode as surrogate pair */
  241. {
  242. if (len < (sizeof (PHYSFS_uint16) * 2))
  243. break; /* not enough room for the pair, stop now. */
  244. cp -= 0x10000; /* Make this a 20-bit value */
  245. *(dst++) = 0xD800 + ((cp >> 10) & 0x3FF);
  246. len -= sizeof (PHYSFS_uint16);
  247. cp = 0xDC00 + (cp & 0x3FF);
  248. } /* if */
  249. *(dst++) = cp;
  250. len -= sizeof (PHYSFS_uint16);
  251. } /* while */
  252. *dst = 0;
  253. } /* PHYSFS_utf8ToUtf16 */
  254. static void utf8fromcodepoint(PHYSFS_uint32 cp, char **_dst, PHYSFS_uint64 *_len)
  255. {
  256. char *dst = *_dst;
  257. PHYSFS_uint64 len = *_len;
  258. if (len == 0)
  259. return;
  260. if (cp > 0x10FFFF)
  261. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  262. else if ((cp == 0xFFFE) || (cp == 0xFFFF)) /* illegal values. */
  263. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  264. else
  265. {
  266. /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
  267. switch (cp)
  268. {
  269. case 0xD800:
  270. case 0xDB7F:
  271. case 0xDB80:
  272. case 0xDBFF:
  273. case 0xDC00:
  274. case 0xDF80:
  275. case 0xDFFF:
  276. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  277. } /* switch */
  278. } /* else */
  279. /* Do the encoding... */
  280. if (cp < 0x80)
  281. {
  282. *(dst++) = (char) cp;
  283. len--;
  284. } /* if */
  285. else if (cp < 0x800)
  286. {
  287. if (len < 2)
  288. len = 0;
  289. else
  290. {
  291. *(dst++) = (char) ((cp >> 6) | 128 | 64);
  292. *(dst++) = (char) (cp & 0x3F) | 128;
  293. len -= 2;
  294. } /* else */
  295. } /* else if */
  296. else if (cp < 0x10000)
  297. {
  298. if (len < 3)
  299. len = 0;
  300. else
  301. {
  302. *(dst++) = (char) ((cp >> 12) | 128 | 64 | 32);
  303. *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
  304. *(dst++) = (char) (cp & 0x3F) | 128;
  305. len -= 3;
  306. } /* else */
  307. } /* else if */
  308. else
  309. {
  310. if (len < 4)
  311. len = 0;
  312. else
  313. {
  314. *(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16);
  315. *(dst++) = (char) ((cp >> 12) & 0x3F) | 128;
  316. *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
  317. *(dst++) = (char) (cp & 0x3F) | 128;
  318. len -= 4;
  319. } /* else if */
  320. } /* else */
  321. *_dst = dst;
  322. *_len = len;
  323. } /* utf8fromcodepoint */
  324. #define UTF8FROMTYPE(typ, src, dst, len) \
  325. if (len == 0) return; \
  326. len--; \
  327. while (len) \
  328. { \
  329. const PHYSFS_uint32 cp = (PHYSFS_uint32) ((typ) (*(src++))); \
  330. if (cp == 0) break; \
  331. utf8fromcodepoint(cp, &dst, &len); \
  332. } \
  333. *dst = '\0'; \
  334. void PHYSFS_utf8FromUcs4(const PHYSFS_uint32 *src, char *dst, PHYSFS_uint64 len)
  335. {
  336. UTF8FROMTYPE(PHYSFS_uint32, src, dst, len);
  337. } /* PHYSFS_utf8FromUcs4 */
  338. void PHYSFS_utf8FromUcs2(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len)
  339. {
  340. UTF8FROMTYPE(PHYSFS_uint64, src, dst, len);
  341. } /* PHYSFS_utf8FromUcs2 */
  342. /* latin1 maps to unicode codepoints directly, we just utf-8 encode it. */
  343. void PHYSFS_utf8FromLatin1(const char *src, char *dst, PHYSFS_uint64 len)
  344. {
  345. UTF8FROMTYPE(PHYSFS_uint8, src, dst, len);
  346. } /* PHYSFS_utf8FromLatin1 */
  347. #undef UTF8FROMTYPE
  348. void PHYSFS_utf8FromUtf16(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len)
  349. {
  350. if (len == 0)
  351. return;
  352. len--;
  353. while (len)
  354. {
  355. const PHYSFS_uint32 cp = utf16codepoint(&src);
  356. if (!cp)
  357. break;
  358. utf8fromcodepoint(cp, &dst, &len);
  359. } /* while */
  360. *dst = '\0';
  361. } /* PHYSFS_utf8FromUtf16 */
  362. int PHYSFS_caseFold(const PHYSFS_uint32 from, PHYSFS_uint32 *to)
  363. {
  364. int i;
  365. if (from < 128) /* low-ASCII, easy! */
  366. {
  367. if ((from >= 'A') && (from <= 'Z'))
  368. {
  369. *to = from - ('A' - 'a');
  370. return 1;
  371. } /* if */
  372. } /* if */
  373. else if (from <= 0xFFFF)
  374. {
  375. const PHYSFS_uint8 hash = ((from ^ (from >> 8)) & 0xFF);
  376. const PHYSFS_uint16 from16 = (PHYSFS_uint16) from;
  377. {
  378. const CaseFoldHashBucket1_16 *bucket = &case_fold_hash1_16[hash];
  379. const int count = (int) bucket->count;
  380. for (i = 0; i < count; i++)
  381. {
  382. const CaseFoldMapping1_16 *mapping = &bucket->list[i];
  383. if (mapping->from == from16)
  384. {
  385. *to = mapping->to0;
  386. return 1;
  387. } /* if */
  388. } /* for */
  389. }
  390. {
  391. const CaseFoldHashBucket2_16 *bucket = &case_fold_hash2_16[hash & 15];
  392. const int count = (int) bucket->count;
  393. for (i = 0; i < count; i++)
  394. {
  395. const CaseFoldMapping2_16 *mapping = &bucket->list[i];
  396. if (mapping->from == from16)
  397. {
  398. to[0] = mapping->to0;
  399. to[1] = mapping->to1;
  400. return 2;
  401. } /* if */
  402. } /* for */
  403. }
  404. {
  405. const CaseFoldHashBucket3_16 *bucket = &case_fold_hash3_16[hash & 3];
  406. const int count = (int) bucket->count;
  407. for (i = 0; i < count; i++)
  408. {
  409. const CaseFoldMapping3_16 *mapping = &bucket->list[i];
  410. if (mapping->from == from16)
  411. {
  412. to[0] = mapping->to0;
  413. to[1] = mapping->to1;
  414. to[2] = mapping->to2;
  415. return 3;
  416. } /* if */
  417. } /* for */
  418. }
  419. } /* else if */
  420. else /* codepoint that doesn't fit in 16 bits. */
  421. {
  422. const PHYSFS_uint8 hash = ((from ^ (from >> 8)) & 0xFF);
  423. const CaseFoldHashBucket1_32 *bucket = &case_fold_hash1_32[hash & 15];
  424. const int count = (int) bucket->count;
  425. for (i = 0; i < count; i++)
  426. {
  427. const CaseFoldMapping1_32 *mapping = &bucket->list[i];
  428. if (mapping->from == from)
  429. {
  430. *to = mapping->to0;
  431. return 1;
  432. } /* if */
  433. } /* for */
  434. } /* else */
  435. /* Not found...there's no remapping for this codepoint. */
  436. *to = from;
  437. return 1;
  438. } /* PHYSFS_caseFold */
  439. #define UTFSTRICMP(bits) \
  440. PHYSFS_uint32 folded1[3], folded2[3]; \
  441. int head1 = 0, tail1 = 0, head2 = 0, tail2 = 0; \
  442. while (1) { \
  443. PHYSFS_uint32 cp1, cp2; \
  444. if (head1 != tail1) { \
  445. cp1 = folded1[tail1++]; \
  446. } else { \
  447. head1 = PHYSFS_caseFold(utf##bits##codepoint(&str1), folded1); \
  448. cp1 = folded1[0]; \
  449. tail1 = 1; \
  450. } \
  451. if (head2 != tail2) { \
  452. cp2 = folded2[tail2++]; \
  453. } else { \
  454. head2 = PHYSFS_caseFold(utf##bits##codepoint(&str2), folded2); \
  455. cp2 = folded2[0]; \
  456. tail2 = 1; \
  457. } \
  458. if (cp1 < cp2) { \
  459. return -1; \
  460. } else if (cp1 > cp2) { \
  461. return 1; \
  462. } else if (cp1 == 0) { \
  463. break; /* complete match. */ \
  464. } \
  465. } \
  466. return 0
  467. int PHYSFS_utf8stricmp(const char *str1, const char *str2)
  468. {
  469. UTFSTRICMP(8);
  470. } /* PHYSFS_utf8stricmp */
  471. int PHYSFS_utf16stricmp(const PHYSFS_uint16 *str1, const PHYSFS_uint16 *str2)
  472. {
  473. UTFSTRICMP(16);
  474. } /* PHYSFS_utf16stricmp */
  475. int PHYSFS_ucs4stricmp(const PHYSFS_uint32 *str1, const PHYSFS_uint32 *str2)
  476. {
  477. UTFSTRICMP(32);
  478. } /* PHYSFS_ucs4stricmp */
  479. #undef UTFSTRICMP
  480. /* end of physfs_unicode.c ... */