utf8.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <assert.h>
  4. #include "cmark_ctype.h"
  5. #include "utf8.h"
  6. static const int8_t utf8proc_utf8class[256] = {
  7. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  8. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  9. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  10. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  11. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  12. 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  14. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  15. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  16. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  17. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0};
  18. static void encode_unknown(cmark_strbuf *buf) {
  19. static const uint8_t repl[] = {239, 191, 189};
  20. cmark_strbuf_put(buf, repl, 3);
  21. }
  22. static int utf8proc_charlen(const uint8_t *str, bufsize_t str_len) {
  23. int length, i;
  24. if (!str_len)
  25. return 0;
  26. length = utf8proc_utf8class[str[0]];
  27. if (!length)
  28. return -1;
  29. if (str_len >= 0 && (bufsize_t)length > str_len)
  30. return -str_len;
  31. for (i = 1; i < length; i++) {
  32. if ((str[i] & 0xC0) != 0x80)
  33. return -i;
  34. }
  35. return length;
  36. }
  37. // Validate a single UTF-8 character according to RFC 3629.
  38. static int utf8proc_valid(const uint8_t *str, bufsize_t str_len) {
  39. int length = utf8proc_utf8class[str[0]];
  40. if (!length)
  41. return -1;
  42. if ((bufsize_t)length > str_len)
  43. return -str_len;
  44. switch (length) {
  45. case 2:
  46. if ((str[1] & 0xC0) != 0x80)
  47. return -1;
  48. if (str[0] < 0xC2) {
  49. // Overlong
  50. return -length;
  51. }
  52. break;
  53. case 3:
  54. if ((str[1] & 0xC0) != 0x80)
  55. return -1;
  56. if ((str[2] & 0xC0) != 0x80)
  57. return -2;
  58. if (str[0] == 0xE0) {
  59. if (str[1] < 0xA0) {
  60. // Overlong
  61. return -length;
  62. }
  63. } else if (str[0] == 0xED) {
  64. if (str[1] >= 0xA0) {
  65. // Surrogate
  66. return -length;
  67. }
  68. }
  69. break;
  70. case 4:
  71. if ((str[1] & 0xC0) != 0x80)
  72. return -1;
  73. if ((str[2] & 0xC0) != 0x80)
  74. return -2;
  75. if ((str[3] & 0xC0) != 0x80)
  76. return -3;
  77. if (str[0] == 0xF0) {
  78. if (str[1] < 0x90) {
  79. // Overlong
  80. return -length;
  81. }
  82. } else if (str[0] >= 0xF4) {
  83. if (str[0] > 0xF4 || str[1] >= 0x90) {
  84. // Above 0x10FFFF
  85. return -length;
  86. }
  87. }
  88. break;
  89. }
  90. return length;
  91. }
  92. void cmark_utf8proc_check(cmark_strbuf *ob, const uint8_t *line,
  93. bufsize_t size) {
  94. bufsize_t i = 0;
  95. while (i < size) {
  96. bufsize_t org = i;
  97. int charlen = 0;
  98. while (i < size) {
  99. if (line[i] < 0x80 && line[i] != 0) {
  100. i++;
  101. } else if (line[i] >= 0x80) {
  102. charlen = utf8proc_valid(line + i, size - i);
  103. if (charlen < 0) {
  104. charlen = -charlen;
  105. break;
  106. }
  107. i += charlen;
  108. } else if (line[i] == 0) {
  109. // ASCII NUL is technically valid but rejected
  110. // for security reasons.
  111. charlen = 1;
  112. break;
  113. }
  114. }
  115. if (i > org) {
  116. cmark_strbuf_put(ob, line + org, i - org);
  117. }
  118. if (i >= size) {
  119. break;
  120. } else {
  121. // Invalid UTF-8
  122. encode_unknown(ob);
  123. i += charlen;
  124. }
  125. }
  126. }
  127. int cmark_utf8proc_iterate(const uint8_t *str, bufsize_t str_len,
  128. int32_t *dst) {
  129. int length;
  130. int32_t uc = -1;
  131. *dst = -1;
  132. length = utf8proc_charlen(str, str_len);
  133. if (length < 0)
  134. return -1;
  135. switch (length) {
  136. case 1:
  137. uc = str[0];
  138. break;
  139. case 2:
  140. uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
  141. if (uc < 0x80)
  142. uc = -1;
  143. break;
  144. case 3:
  145. uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6) + (str[2] & 0x3F);
  146. if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000))
  147. uc = -1;
  148. break;
  149. case 4:
  150. uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12) +
  151. ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
  152. if (uc < 0x10000 || uc >= 0x110000)
  153. uc = -1;
  154. break;
  155. }
  156. if (uc < 0)
  157. return -1;
  158. *dst = uc;
  159. return length;
  160. }
  161. void cmark_utf8proc_encode_char(int32_t uc, cmark_strbuf *buf) {
  162. uint8_t dst[4];
  163. bufsize_t len = 0;
  164. assert(uc >= 0);
  165. if (uc < 0x80) {
  166. dst[0] = (uint8_t)(uc);
  167. len = 1;
  168. } else if (uc < 0x800) {
  169. dst[0] = (uint8_t)(0xC0 + (uc >> 6));
  170. dst[1] = 0x80 + (uc & 0x3F);
  171. len = 2;
  172. } else if (uc == 0xFFFF) {
  173. dst[0] = 0xFF;
  174. len = 1;
  175. } else if (uc == 0xFFFE) {
  176. dst[0] = 0xFE;
  177. len = 1;
  178. } else if (uc < 0x10000) {
  179. dst[0] = (uint8_t)(0xE0 + (uc >> 12));
  180. dst[1] = 0x80 + ((uc >> 6) & 0x3F);
  181. dst[2] = 0x80 + (uc & 0x3F);
  182. len = 3;
  183. } else if (uc < 0x110000) {
  184. dst[0] = (uint8_t)(0xF0 + (uc >> 18));
  185. dst[1] = 0x80 + ((uc >> 12) & 0x3F);
  186. dst[2] = 0x80 + ((uc >> 6) & 0x3F);
  187. dst[3] = 0x80 + (uc & 0x3F);
  188. len = 4;
  189. } else {
  190. encode_unknown(buf);
  191. return;
  192. }
  193. cmark_strbuf_put(buf, dst, len);
  194. }
  195. void cmark_utf8proc_case_fold(cmark_strbuf *dest, const uint8_t *str,
  196. bufsize_t len) {
  197. int32_t c;
  198. #define bufpush(x) cmark_utf8proc_encode_char(x, dest)
  199. while (len > 0) {
  200. bufsize_t char_len = cmark_utf8proc_iterate(str, len, &c);
  201. if (char_len >= 0) {
  202. #include "case_fold_switch.inc"
  203. } else {
  204. encode_unknown(dest);
  205. char_len = -char_len;
  206. }
  207. str += char_len;
  208. len -= char_len;
  209. }
  210. }
  211. // matches anything in the Zs class, plus LF, CR, TAB, FF.
  212. int cmark_utf8proc_is_space(int32_t uc) {
  213. return (uc == 9 || uc == 10 || uc == 12 || uc == 13 || uc == 32 ||
  214. uc == 160 || uc == 5760 || (uc >= 8192 && uc <= 8202) || uc == 8239 ||
  215. uc == 8287 || uc == 12288);
  216. }
  217. // matches anything in the P[cdefios] classes.
  218. int cmark_utf8proc_is_punctuation(int32_t uc) {
  219. return (
  220. (uc < 128 && cmark_ispunct((char)uc)) || uc == 161 || uc == 167 ||
  221. uc == 171 || uc == 182 || uc == 183 || uc == 187 || uc == 191 ||
  222. uc == 894 || uc == 903 || (uc >= 1370 && uc <= 1375) || uc == 1417 ||
  223. uc == 1418 || uc == 1470 || uc == 1472 || uc == 1475 || uc == 1478 ||
  224. uc == 1523 || uc == 1524 || uc == 1545 || uc == 1546 || uc == 1548 ||
  225. uc == 1549 || uc == 1563 || uc == 1566 || uc == 1567 ||
  226. (uc >= 1642 && uc <= 1645) || uc == 1748 || (uc >= 1792 && uc <= 1805) ||
  227. (uc >= 2039 && uc <= 2041) || (uc >= 2096 && uc <= 2110) || uc == 2142 ||
  228. uc == 2404 || uc == 2405 || uc == 2416 || uc == 2800 || uc == 3572 ||
  229. uc == 3663 || uc == 3674 || uc == 3675 || (uc >= 3844 && uc <= 3858) ||
  230. uc == 3860 || (uc >= 3898 && uc <= 3901) || uc == 3973 ||
  231. (uc >= 4048 && uc <= 4052) || uc == 4057 || uc == 4058 ||
  232. (uc >= 4170 && uc <= 4175) || uc == 4347 || (uc >= 4960 && uc <= 4968) ||
  233. uc == 5120 || uc == 5741 || uc == 5742 || uc == 5787 || uc == 5788 ||
  234. (uc >= 5867 && uc <= 5869) || uc == 5941 || uc == 5942 ||
  235. (uc >= 6100 && uc <= 6102) || (uc >= 6104 && uc <= 6106) ||
  236. (uc >= 6144 && uc <= 6154) || uc == 6468 || uc == 6469 || uc == 6686 ||
  237. uc == 6687 || (uc >= 6816 && uc <= 6822) || (uc >= 6824 && uc <= 6829) ||
  238. (uc >= 7002 && uc <= 7008) || (uc >= 7164 && uc <= 7167) ||
  239. (uc >= 7227 && uc <= 7231) || uc == 7294 || uc == 7295 ||
  240. (uc >= 7360 && uc <= 7367) || uc == 7379 || (uc >= 8208 && uc <= 8231) ||
  241. (uc >= 8240 && uc <= 8259) || (uc >= 8261 && uc <= 8273) ||
  242. (uc >= 8275 && uc <= 8286) || uc == 8317 || uc == 8318 || uc == 8333 ||
  243. uc == 8334 || (uc >= 8968 && uc <= 8971) || uc == 9001 || uc == 9002 ||
  244. (uc >= 10088 && uc <= 10101) || uc == 10181 || uc == 10182 ||
  245. (uc >= 10214 && uc <= 10223) || (uc >= 10627 && uc <= 10648) ||
  246. (uc >= 10712 && uc <= 10715) || uc == 10748 || uc == 10749 ||
  247. (uc >= 11513 && uc <= 11516) || uc == 11518 || uc == 11519 ||
  248. uc == 11632 || (uc >= 11776 && uc <= 11822) ||
  249. (uc >= 11824 && uc <= 11842) || (uc >= 12289 && uc <= 12291) ||
  250. (uc >= 12296 && uc <= 12305) || (uc >= 12308 && uc <= 12319) ||
  251. uc == 12336 || uc == 12349 || uc == 12448 || uc == 12539 || uc == 42238 ||
  252. uc == 42239 || (uc >= 42509 && uc <= 42511) || uc == 42611 ||
  253. uc == 42622 || (uc >= 42738 && uc <= 42743) ||
  254. (uc >= 43124 && uc <= 43127) || uc == 43214 || uc == 43215 ||
  255. (uc >= 43256 && uc <= 43258) || uc == 43310 || uc == 43311 ||
  256. uc == 43359 || (uc >= 43457 && uc <= 43469) || uc == 43486 ||
  257. uc == 43487 || (uc >= 43612 && uc <= 43615) || uc == 43742 ||
  258. uc == 43743 || uc == 43760 || uc == 43761 || uc == 44011 || uc == 64830 ||
  259. uc == 64831 || (uc >= 65040 && uc <= 65049) ||
  260. (uc >= 65072 && uc <= 65106) || (uc >= 65108 && uc <= 65121) ||
  261. uc == 65123 || uc == 65128 || uc == 65130 || uc == 65131 ||
  262. (uc >= 65281 && uc <= 65283) || (uc >= 65285 && uc <= 65290) ||
  263. (uc >= 65292 && uc <= 65295) || uc == 65306 || uc == 65307 ||
  264. uc == 65311 || uc == 65312 || (uc >= 65339 && uc <= 65341) ||
  265. uc == 65343 || uc == 65371 || uc == 65373 ||
  266. (uc >= 65375 && uc <= 65381) || (uc >= 65792 && uc <= 65794) ||
  267. uc == 66463 || uc == 66512 || uc == 66927 || uc == 67671 || uc == 67871 ||
  268. uc == 67903 || (uc >= 68176 && uc <= 68184) || uc == 68223 ||
  269. (uc >= 68336 && uc <= 68342) || (uc >= 68409 && uc <= 68415) ||
  270. (uc >= 68505 && uc <= 68508) || (uc >= 69703 && uc <= 69709) ||
  271. uc == 69819 || uc == 69820 || (uc >= 69822 && uc <= 69825) ||
  272. (uc >= 69952 && uc <= 69955) || uc == 70004 || uc == 70005 ||
  273. (uc >= 70085 && uc <= 70088) || uc == 70093 ||
  274. (uc >= 70200 && uc <= 70205) || uc == 70854 ||
  275. (uc >= 71105 && uc <= 71113) || (uc >= 71233 && uc <= 71235) ||
  276. (uc >= 74864 && uc <= 74868) || uc == 92782 || uc == 92783 ||
  277. uc == 92917 || (uc >= 92983 && uc <= 92987) || uc == 92996 ||
  278. uc == 113823);
  279. }