physfs_unicode.c 16 KB

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