physfs_unicode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 utf8codepoint() if there's bogus bits in the
  20. * stream. 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. static PHYSFS_uint32 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 += 6; /* skip to next possible start of codepoint. */
  154. return UNICODE_BOGUS_CHAR_VALUE;
  155. } /* else if */
  156. return UNICODE_BOGUS_CHAR_VALUE;
  157. } /* utf8codepoint */
  158. static PHYSFS_uint32 utf16codepoint(const PHYSFS_uint16 **_str)
  159. {
  160. const PHYSFS_uint16 *src = *_str;
  161. PHYSFS_uint32 cp = (PHYSFS_uint32) *(src++);
  162. if (cp == 0) /* null terminator, end of string. */
  163. return 0;
  164. /* Orphaned second half of surrogate pair? */
  165. else if ((cp >= 0xDC00) && (cp <= 0xDFFF))
  166. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  167. else if ((cp >= 0xD800) && (cp <= 0xDBFF)) /* start surrogate pair! */
  168. {
  169. const PHYSFS_uint32 pair = (PHYSFS_uint32) *src;
  170. if (pair == 0)
  171. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  172. else if ((pair < 0xDC00) || (pair > 0xDFFF))
  173. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  174. else
  175. {
  176. src++; /* eat the other surrogate. */
  177. cp = (((cp - 0xD800) << 10) | (pair - 0xDC00));
  178. } /* else */
  179. } /* else if */
  180. *_str = src;
  181. return cp;
  182. } /* utf16codepoint */
  183. static PHYSFS_uint32 utf32codepoint(const PHYSFS_uint32 **_str)
  184. {
  185. const PHYSFS_uint32 *src = *_str;
  186. PHYSFS_uint32 cp = *(src++);
  187. if (cp == 0) /* null terminator, end of string. */
  188. return 0;
  189. else if (cp > 0x10FFF)
  190. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  191. *_str = src;
  192. return cp;
  193. } /* utf32codepoint */
  194. void PHYSFS_utf8ToUcs4(const char *src, PHYSFS_uint32 *dst, PHYSFS_uint64 len)
  195. {
  196. len -= sizeof (PHYSFS_uint32); /* save room for null char. */
  197. while (len >= sizeof (PHYSFS_uint32))
  198. {
  199. PHYSFS_uint32 cp = utf8codepoint(&src);
  200. if (cp == 0)
  201. break;
  202. else if (cp == UNICODE_BOGUS_CHAR_VALUE)
  203. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  204. *(dst++) = cp;
  205. len -= sizeof (PHYSFS_uint32);
  206. } /* while */
  207. *dst = 0;
  208. } /* PHYSFS_utf8ToUcs4 */
  209. void PHYSFS_utf8ToUcs2(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len)
  210. {
  211. len -= sizeof (PHYSFS_uint16); /* save room for null char. */
  212. while (len >= sizeof (PHYSFS_uint16))
  213. {
  214. PHYSFS_uint32 cp = utf8codepoint(&src);
  215. if (cp == 0)
  216. break;
  217. else if (cp == UNICODE_BOGUS_CHAR_VALUE)
  218. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  219. if (cp > 0xFFFF) /* UTF-16 surrogates (bogus chars in UCS-2) */
  220. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  221. *(dst++) = cp;
  222. len -= sizeof (PHYSFS_uint16);
  223. } /* while */
  224. *dst = 0;
  225. } /* PHYSFS_utf8ToUcs2 */
  226. void PHYSFS_utf8ToUtf16(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len)
  227. {
  228. len -= sizeof (PHYSFS_uint16); /* save room for null char. */
  229. while (len >= sizeof (PHYSFS_uint16))
  230. {
  231. PHYSFS_uint32 cp = utf8codepoint(&src);
  232. if (cp == 0)
  233. break;
  234. else if (cp == UNICODE_BOGUS_CHAR_VALUE)
  235. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  236. if (cp > 0xFFFF) /* encode as surrogate pair */
  237. {
  238. if (len < (sizeof (PHYSFS_uint16) * 2))
  239. break; /* not enough room for the pair, stop now. */
  240. cp -= 0x10000; /* Make this a 20-bit value */
  241. *(dst++) = 0xD800 + ((cp >> 10) & 0x3FF);
  242. len -= sizeof (PHYSFS_uint16);
  243. cp = 0xDC00 + (cp & 0x3FF);
  244. } /* if */
  245. *(dst++) = cp;
  246. len -= sizeof (PHYSFS_uint16);
  247. } /* while */
  248. *dst = 0;
  249. } /* PHYSFS_utf8ToUtf16 */
  250. static void utf8fromcodepoint(PHYSFS_uint32 cp, char **_dst, PHYSFS_uint64 *_len)
  251. {
  252. char *dst = *_dst;
  253. PHYSFS_uint64 len = *_len;
  254. if (len == 0)
  255. return;
  256. if (cp > 0x10FFFF)
  257. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  258. else if ((cp == 0xFFFE) || (cp == 0xFFFF)) /* illegal values. */
  259. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  260. else
  261. {
  262. /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
  263. switch (cp)
  264. {
  265. case 0xD800:
  266. case 0xDB7F:
  267. case 0xDB80:
  268. case 0xDBFF:
  269. case 0xDC00:
  270. case 0xDF80:
  271. case 0xDFFF:
  272. cp = UNICODE_BOGUS_CHAR_CODEPOINT;
  273. } /* switch */
  274. } /* else */
  275. /* Do the encoding... */
  276. if (cp < 0x80)
  277. {
  278. *(dst++) = (char) cp;
  279. len--;
  280. } /* if */
  281. else if (cp < 0x800)
  282. {
  283. if (len < 2)
  284. len = 0;
  285. else
  286. {
  287. *(dst++) = (char) ((cp >> 6) | 128 | 64);
  288. *(dst++) = (char) (cp & 0x3F) | 128;
  289. len -= 2;
  290. } /* else */
  291. } /* else if */
  292. else if (cp < 0x10000)
  293. {
  294. if (len < 3)
  295. len = 0;
  296. else
  297. {
  298. *(dst++) = (char) ((cp >> 12) | 128 | 64 | 32);
  299. *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
  300. *(dst++) = (char) (cp & 0x3F) | 128;
  301. len -= 3;
  302. } /* else */
  303. } /* else if */
  304. else
  305. {
  306. if (len < 4)
  307. len = 0;
  308. else
  309. {
  310. *(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16);
  311. *(dst++) = (char) ((cp >> 12) & 0x3F) | 128;
  312. *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
  313. *(dst++) = (char) (cp & 0x3F) | 128;
  314. len -= 4;
  315. } /* else if */
  316. } /* else */
  317. *_dst = dst;
  318. *_len = len;
  319. } /* utf8fromcodepoint */
  320. #define UTF8FROMTYPE(typ, src, dst, len) \
  321. if (len == 0) return; \
  322. len--; \
  323. while (len) \
  324. { \
  325. const PHYSFS_uint32 cp = (PHYSFS_uint32) ((typ) (*(src++))); \
  326. if (cp == 0) break; \
  327. utf8fromcodepoint(cp, &dst, &len); \
  328. } \
  329. *dst = '\0'; \
  330. void PHYSFS_utf8FromUcs4(const PHYSFS_uint32 *src, char *dst, PHYSFS_uint64 len)
  331. {
  332. UTF8FROMTYPE(PHYSFS_uint32, src, dst, len);
  333. } /* PHYSFS_utf8FromUcs4 */
  334. void PHYSFS_utf8FromUcs2(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len)
  335. {
  336. UTF8FROMTYPE(PHYSFS_uint64, src, dst, len);
  337. } /* PHYSFS_utf8FromUcs2 */
  338. /* latin1 maps to unicode codepoints directly, we just utf-8 encode it. */
  339. void PHYSFS_utf8FromLatin1(const char *src, char *dst, PHYSFS_uint64 len)
  340. {
  341. UTF8FROMTYPE(PHYSFS_uint8, src, dst, len);
  342. } /* PHYSFS_utf8FromLatin1 */
  343. #undef UTF8FROMTYPE
  344. void PHYSFS_utf8FromUtf16(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len)
  345. {
  346. if (len == 0)
  347. return;
  348. len--;
  349. while (len)
  350. {
  351. const PHYSFS_uint32 cp = utf16codepoint(&src);
  352. if (!cp)
  353. break;
  354. utf8fromcodepoint(cp, &dst, &len);
  355. } /* while */
  356. *dst = '\0';
  357. } /* PHYSFS_utf8FromUtf16 */
  358. int PHYSFS_caseFold(const PHYSFS_uint32 from, PHYSFS_uint32 *to)
  359. {
  360. int i;
  361. if (from < 128) /* low-ASCII, easy! */
  362. {
  363. if ((from >= 'A') && (from <= 'Z'))
  364. *to = from - ('A' - 'a');
  365. else
  366. *to = from;
  367. return 1;
  368. } /* if */
  369. else if (from <= 0xFFFF)
  370. {
  371. const PHYSFS_uint8 hash = ((from ^ (from >> 8)) & 0xFF);
  372. const PHYSFS_uint16 from16 = (PHYSFS_uint16) from;
  373. {
  374. const CaseFoldHashBucket1_16 *bucket = &case_fold_hash1_16[hash];
  375. const int count = (int) bucket->count;
  376. for (i = 0; i < count; i++)
  377. {
  378. const CaseFoldMapping1_16 *mapping = &bucket->list[i];
  379. if (mapping->from == from16)
  380. {
  381. *to = mapping->to0;
  382. return 1;
  383. } /* if */
  384. } /* for */
  385. }
  386. {
  387. const CaseFoldHashBucket2_16 *bucket = &case_fold_hash2_16[hash & 15];
  388. const int count = (int) bucket->count;
  389. for (i = 0; i < count; i++)
  390. {
  391. const CaseFoldMapping2_16 *mapping = &bucket->list[i];
  392. if (mapping->from == from16)
  393. {
  394. to[0] = mapping->to0;
  395. to[1] = mapping->to1;
  396. return 2;
  397. } /* if */
  398. } /* for */
  399. }
  400. {
  401. const CaseFoldHashBucket3_16 *bucket = &case_fold_hash3_16[hash & 3];
  402. const int count = (int) bucket->count;
  403. for (i = 0; i < count; i++)
  404. {
  405. const CaseFoldMapping3_16 *mapping = &bucket->list[i];
  406. if (mapping->from == from16)
  407. {
  408. to[0] = mapping->to0;
  409. to[1] = mapping->to1;
  410. to[2] = mapping->to2;
  411. return 3;
  412. } /* if */
  413. } /* for */
  414. }
  415. } /* else if */
  416. else /* codepoint that doesn't fit in 16 bits. */
  417. {
  418. const PHYSFS_uint8 hash = ((from ^ (from >> 8)) & 0xFF);
  419. const CaseFoldHashBucket1_32 *bucket = &case_fold_hash1_32[hash & 15];
  420. const int count = (int) bucket->count;
  421. for (i = 0; i < count; i++)
  422. {
  423. const CaseFoldMapping1_32 *mapping = &bucket->list[i];
  424. if (mapping->from == from)
  425. {
  426. *to = mapping->to0;
  427. return 1;
  428. } /* if */
  429. } /* for */
  430. } /* else */
  431. /* Not found...there's no remapping for this codepoint. */
  432. *to = from;
  433. return 1;
  434. } /* PHYSFS_caseFold */
  435. #define UTFSTRICMP(bits) \
  436. PHYSFS_uint32 folded1[3], folded2[3]; \
  437. int head1 = 0, tail1 = 0, head2 = 0, tail2 = 0; \
  438. while (1) { \
  439. PHYSFS_uint32 cp1, cp2; \
  440. if (head1 != tail1) { \
  441. cp1 = folded1[tail1++]; \
  442. } else { \
  443. head1 = PHYSFS_caseFold(utf##bits##codepoint(&str1), folded1); \
  444. cp1 = folded1[0]; \
  445. tail1 = 1; \
  446. } \
  447. if (head2 != tail2) { \
  448. cp2 = folded2[tail2++]; \
  449. } else { \
  450. head2 = PHYSFS_caseFold(utf##bits##codepoint(&str2), folded2); \
  451. cp2 = folded2[0]; \
  452. tail2 = 1; \
  453. } \
  454. if (cp1 < cp2) { \
  455. return -1; \
  456. } else if (cp1 > cp2) { \
  457. return 1; \
  458. } else if (cp1 == 0) { \
  459. break; /* complete match. */ \
  460. } \
  461. } \
  462. return 0
  463. int PHYSFS_utf8stricmp(const char *str1, const char *str2)
  464. {
  465. UTFSTRICMP(8);
  466. } /* PHYSFS_utf8stricmp */
  467. int PHYSFS_utf16stricmp(const PHYSFS_uint16 *str1, const PHYSFS_uint16 *str2)
  468. {
  469. UTFSTRICMP(16);
  470. } /* PHYSFS_utf16stricmp */
  471. int PHYSFS_ucs4stricmp(const PHYSFS_uint32 *str1, const PHYSFS_uint32 *str2)
  472. {
  473. UTFSTRICMP(32);
  474. } /* PHYSFS_ucs4stricmp */
  475. #undef UTFSTRICMP
  476. /* end of physfs_unicode.c ... */