aboutsummaryrefslogtreecommitdiff
path: root/src/utf8.c
blob: ffb050de36cf40fda4882a2ca9d252b9a2917557 (plain)
  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,
  8. 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,
  10. 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,
  12. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  13. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  14. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  20. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  21. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  22. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
  23. static void encode_unknown(cmark_strbuf *buf)
  24. {
  25. static const uint8_t repl[] = {239, 191, 189};
  26. cmark_strbuf_put(buf, repl, 3);
  27. }
  28. static int utf8proc_charlen(const uint8_t *str, int str_len)
  29. {
  30. int length, i;
  31. if (!str_len)
  32. return 0;
  33. length = utf8proc_utf8class[str[0]];
  34. if (!length)
  35. return -1;
  36. if (str_len >= 0 && length > str_len)
  37. return -str_len;
  38. for (i = 1; i < length; i++) {
  39. if ((str[i] & 0xC0) != 0x80)
  40. return -i;
  41. }
  42. return length;
  43. }
  44. // Validate a single UTF-8 character according to RFC 3629.
  45. static int utf8proc_valid(const uint8_t *str, int str_len)
  46. {
  47. int length = utf8proc_charlen(str, str_len);
  48. if (length <= 0)
  49. return length;
  50. switch (length) {
  51. case 1:
  52. if (str[0] == 0x00) {
  53. // ASCII NUL is technically valid but rejected
  54. // for security reasons.
  55. return -length;
  56. }
  57. break;
  58. case 2:
  59. if (str[0] < 0xC2) {
  60. // Overlong
  61. return -length;
  62. }
  63. break;
  64. case 3:
  65. if (str[0] == 0xE0) {
  66. if (str[1] < 0xA0) {
  67. // Overlong
  68. return -length;
  69. }
  70. }
  71. else if (str[0] == 0xED) {
  72. if (str[1] >= 0xA0) {
  73. // Surrogate
  74. return -length;
  75. }
  76. }
  77. break;
  78. case 4:
  79. if (str[0] == 0xF0) {
  80. if (str[1] < 0x90) {
  81. // Overlong
  82. return -length;
  83. }
  84. }
  85. else if (str[0] >= 0xF4) {
  86. if (str[0] > 0xF4 || str[1] >= 0x90) {
  87. // Above 0x10FFFF
  88. return -length;
  89. }
  90. }
  91. break;
  92. }
  93. return length;
  94. }
  95. void utf8proc_detab(cmark_strbuf *ob, const uint8_t *line, size_t size)
  96. {
  97. static const uint8_t whitespace[] = " ";
  98. size_t i = 0, tab = 0;
  99. while (i < size) {
  100. size_t org = i;
  101. while (i < size && line[i] != '\t' && line[i] != '\0'
  102. && line[i] < 0x80) {
  103. i++; tab++;
  104. }
  105. if (i > org)
  106. cmark_strbuf_put(ob, line + org, i - org);
  107. if (i >= size)
  108. break;
  109. if (line[i] == '\t') {
  110. int numspaces = 4 - (tab % 4);
  111. cmark_strbuf_put(ob, whitespace, numspaces);
  112. i += 1;
  113. tab += numspaces;
  114. } else {
  115. int charlen = utf8proc_valid(line + i, size - i);
  116. if (charlen >= 0) {
  117. cmark_strbuf_put(ob, line + i, charlen);
  118. } else {
  119. encode_unknown(ob);
  120. charlen = -charlen;
  121. }
  122. i += charlen;
  123. tab += 1;
  124. }
  125. }
  126. }
  127. int utf8proc_iterate(const uint8_t *str, int str_len, int32_t *dst)
  128. {
  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) uc = -1;
  142. break;
  143. case 3:
  144. uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
  145. + (str[2] & 0x3F);
  146. if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
  147. (uc >= 0xFDD0 && uc < 0xFDF0)) 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) uc = -1;
  153. break;
  154. }
  155. if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
  156. return -1;
  157. *dst = uc;
  158. return length;
  159. }
  160. void utf8proc_encode_char(int32_t uc, cmark_strbuf *buf)
  161. {
  162. uint8_t dst[4];
  163. int len = 0;
  164. assert(uc >= 0);
  165. if (uc < 0x80) {
  166. dst[0] = uc;
  167. len = 1;
  168. } else if (uc < 0x800) {
  169. dst[0] = 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] = 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] = 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 utf8proc_case_fold(cmark_strbuf *dest, const uint8_t *str, int len)
  196. {
  197. int32_t c;
  198. #define bufpush(x) \
  199. utf8proc_encode_char(x, dest)
  200. while (len > 0) {
  201. int char_len = utf8proc_iterate(str, len, &c);
  202. if (char_len >= 0) {
  203. #include "case_fold_switch.inc"
  204. } else {
  205. encode_unknown(dest);
  206. char_len = -char_len;
  207. }
  208. str += char_len;
  209. len -= char_len;
  210. }
  211. }
  212. // matches anything in the Zs class, plus LF, CR, TAB, FF.
  213. int utf8proc_is_space(int32_t uc)
  214. {
  215. return (uc == 9 ||
  216. uc == 10 ||
  217. uc == 12 ||
  218. uc == 13 ||
  219. uc == 32 ||
  220. uc == 160 ||
  221. uc == 5760 ||
  222. (uc >= 8192 && uc <= 8202) ||
  223. uc == 8239 ||
  224. uc == 8287 ||
  225. uc == 12288);
  226. }
  227. // matches anything in the P[cdefios] classes.
  228. int utf8proc_is_punctuation(int32_t uc)
  229. {
  230. return ((uc < 128 && cmark_ispunct((char)uc)) ||
  231. uc == 161 ||
  232. uc == 167 ||
  233. uc == 171 ||
  234. uc == 182 ||
  235. uc == 183 ||
  236. uc == 187 ||
  237. uc == 191 ||
  238. uc == 894 ||
  239. uc == 903 ||
  240. (uc >= 1370 && uc <= 1375) ||
  241. uc == 1417 ||
  242. uc == 1418 ||
  243. uc == 1470 ||
  244. uc == 1472 ||
  245. uc == 1475 ||
  246. uc == 1478 ||
  247. uc == 1523 ||
  248. uc == 1524 ||
  249. uc == 1545 ||
  250. uc == 1546 ||
  251. uc == 1548 ||
  252. uc == 1549 ||
  253. uc == 1563 ||
  254. uc == 1566 ||
  255. uc == 1567 ||
  256. (uc >= 1642 && uc <= 1645) ||
  257. uc == 1748 ||
  258. (uc >= 1792 && uc <= 1805) ||
  259. (uc >= 2039 && uc <= 2041) ||
  260. (uc >= 2096 && uc <= 2110) ||
  261. uc == 2142 ||
  262. uc == 2404 ||
  263. uc == 2405 ||
  264. uc == 2416 ||
  265. uc == 2800 ||
  266. uc == 3572 ||
  267. uc == 3663 ||
  268. uc == 3674 ||
  269. uc == 3675 ||
  270. (uc >= 3844 && uc <= 3858) ||
  271. uc == 3860 ||
  272. (uc >= 3898 && uc <= 3901) ||
  273. uc == 3973 ||
  274. (uc >= 4048 && uc <= 4052) ||
  275. uc == 4057 ||
  276. uc == 4058 ||
  277. (uc >= 4170 && uc <= 4175) ||
  278. uc == 4347 ||
  279. (uc >= 4960 && uc <= 4968) ||
  280. uc == 5120 ||
  281. uc == 5741 ||
  282. uc == 5742 ||
  283. uc == 5787 ||
  284. uc == 5788 ||
  285. (uc >= 5867 && uc <= 5869) ||
  286. uc == 5941 ||
  287. uc == 5942 ||
  288. (uc >= 6100 && uc <= 6102) ||
  289. (uc >= 6104 && uc <= 6106) ||
  290. (uc >= 6144 && uc <= 6154) ||
  291. uc == 6468 ||
  292. uc == 6469 ||
  293. uc == 6686 ||
  294. uc == 6687 ||
  295. (uc >= 6816 && uc <= 6822) ||
  296. (uc >= 6824 && uc <= 6829) ||
  297. (uc >= 7002 && uc <= 7008) ||
  298. (uc >= 7164 && uc <= 7167) ||
  299. (uc >= 7227 && uc <= 7231) ||
  300. uc == 7294 ||
  301. uc == 7295 ||
  302. (uc >= 7360 && uc <= 7367) ||
  303. uc == 7379 ||
  304. (uc >= 8208 && uc <= 8231) ||
  305. (uc >= 8240 && uc <= 8259) ||
  306. (uc >= 8261 && uc <= 8273) ||
  307. (uc >= 8275 && uc <= 8286) ||
  308. uc == 8317 ||
  309. uc == 8318 ||
  310. uc == 8333 ||
  311. uc == 8334 ||
  312. (uc >= 8968 && uc <= 8971) ||
  313. uc == 9001 ||
  314. uc == 9002 ||
  315. (uc >= 10088 && uc <= 10101) ||
  316. uc == 10181 ||
  317. uc == 10182 ||
  318. (uc >= 10214 && uc <= 10223) ||
  319. (uc >= 10627 && uc <= 10648) ||
  320. (uc >= 10712 && uc <= 10715) ||
  321. uc == 10748 ||
  322. uc == 10749 ||
  323. (uc >= 11513 && uc <= 11516) ||
  324. uc == 11518 ||
  325. uc == 11519 ||
  326. uc == 11632 ||
  327. (uc >= 11776 && uc <= 11822) ||
  328. (uc >= 11824 && uc <= 11842) ||
  329. (uc >= 12289 && uc <= 12291) ||
  330. (uc >= 12296 && uc <= 12305) ||
  331. (uc >= 12308 && uc <= 12319) ||
  332. uc == 12336 ||
  333. uc == 12349 ||
  334. uc == 12448 ||
  335. uc == 12539 ||
  336. uc == 42238 ||
  337. uc == 42239 ||
  338. (uc >= 42509 && uc <= 42511) ||
  339. uc == 42611 ||
  340. uc == 42622 ||
  341. (uc >= 42738 && uc <= 42743) ||
  342. (uc >= 43124 && uc <= 43127) ||
  343. uc == 43214 ||
  344. uc == 43215 ||
  345. (uc >= 43256 && uc <= 43258) ||
  346. uc == 43310 ||
  347. uc == 43311 ||
  348. uc == 43359 ||
  349. (uc >= 43457 && uc <= 43469) ||
  350. uc == 43486 ||
  351. uc == 43487 ||
  352. (uc >= 43612 && uc <= 43615) ||
  353. uc == 43742 ||
  354. uc == 43743 ||
  355. uc == 43760 ||
  356. uc == 43761 ||
  357. uc == 44011 ||
  358. uc == 64830 ||
  359. uc == 64831 ||
  360. (uc >= 65040 && uc <= 65049) ||
  361. (uc >= 65072 && uc <= 65106) ||
  362. (uc >= 65108 && uc <= 65121) ||
  363. uc == 65123 ||
  364. uc == 65128 ||
  365. uc == 65130 ||
  366. uc == 65131 ||
  367. (uc >= 65281 && uc <= 65283) ||
  368. (uc >= 65285 && uc <= 65290) ||
  369. (uc >= 65292 && uc <= 65295) ||
  370. uc == 65306 ||
  371. uc == 65307 ||
  372. uc == 65311 ||
  373. uc == 65312 ||
  374. (uc >= 65339 && uc <= 65341) ||
  375. uc == 65343 ||
  376. uc == 65371 ||
  377. uc == 65373 ||
  378. (uc >= 65375 && uc <= 65381) ||
  379. (uc >= 65792 && uc <= 65794) ||
  380. uc == 66463 ||
  381. uc == 66512 ||
  382. uc == 66927 ||
  383. uc == 67671 ||
  384. uc == 67871 ||
  385. uc == 67903 ||
  386. (uc >= 68176 && uc <= 68184) ||
  387. uc == 68223 ||
  388. (uc >= 68336 && uc <= 68342) ||
  389. (uc >= 68409 && uc <= 68415) ||
  390. (uc >= 68505 && uc <= 68508) ||
  391. (uc >= 69703 && uc <= 69709) ||
  392. uc == 69819 ||
  393. uc == 69820 ||
  394. (uc >= 69822 && uc <= 69825) ||
  395. (uc >= 69952 && uc <= 69955) ||
  396. uc == 70004 ||
  397. uc == 70005 ||
  398. (uc >= 70085 && uc <= 70088) ||
  399. uc == 70093 ||
  400. (uc >= 70200 && uc <= 70205) ||
  401. uc == 70854 ||
  402. (uc >= 71105 && uc <= 71113) ||
  403. (uc >= 71233 && uc <= 71235) ||
  404. (uc >= 74864 && uc <= 74868) ||
  405. uc == 92782 ||
  406. uc == 92783 ||
  407. uc == 92917 ||
  408. (uc >= 92983 && uc <= 92987) ||
  409. uc == 92996 ||
  410. uc == 113823);
  411. }