dutf8proc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Copyright (c) 2006-2007 Jan Behrens, FlexiGuided GmbH, Berlin
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. * This library contains derived data from a modified version of the
  24. * Unicode data files.
  25. *
  26. * The original data files are available at
  27. * http://www.unicode.org/Public/UNIDATA/
  28. *
  29. * Please notice the copyright statement in the file "utf8proc_data.c".
  30. */
  31. /*
  32. * File name: utf8proc.c
  33. * Version: 1.1.1
  34. * Last changed: 2007-07-22
  35. *
  36. * Description:
  37. * Implementation of libutf8proc.
  38. */
  39. #include "utf8proc.h"
  40. #include "utf8proc_data.h"
  41. const int8_t utf8proc_utf8class[256] = {
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  43. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  45. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  46. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  47. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  48. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  49. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  57. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
  58. #define UTF8PROC_HANGUL_SBASE 0xAC00
  59. #define UTF8PROC_HANGUL_LBASE 0x1100
  60. #define UTF8PROC_HANGUL_VBASE 0x1161
  61. #define UTF8PROC_HANGUL_TBASE 0x11A7
  62. #define UTF8PROC_HANGUL_LCOUNT 19
  63. #define UTF8PROC_HANGUL_VCOUNT 21
  64. #define UTF8PROC_HANGUL_TCOUNT 28
  65. #define UTF8PROC_HANGUL_NCOUNT 588
  66. #define UTF8PROC_HANGUL_SCOUNT 11172
  67. /*// END is exclusive*/
  68. #define UTF8PROC_HANGUL_L_START 0x1100
  69. #define UTF8PROC_HANGUL_L_END 0x115A
  70. #define UTF8PROC_HANGUL_L_FILLER 0x115F
  71. #define UTF8PROC_HANGUL_V_START 0x1160
  72. #define UTF8PROC_HANGUL_V_END 0x11A3
  73. #define UTF8PROC_HANGUL_T_START 0x11A8
  74. #define UTF8PROC_HANGUL_T_END 0x11FA
  75. #define UTF8PROC_HANGUL_S_START 0xAC00
  76. #define UTF8PROC_HANGUL_S_END 0xD7A4
  77. #define UTF8PROC_BOUNDCLASS_START 0
  78. #define UTF8PROC_BOUNDCLASS_OTHER 1
  79. #define UTF8PROC_BOUNDCLASS_CR 2
  80. #define UTF8PROC_BOUNDCLASS_LF 3
  81. #define UTF8PROC_BOUNDCLASS_CONTROL 4
  82. #define UTF8PROC_BOUNDCLASS_EXTEND 5
  83. #define UTF8PROC_BOUNDCLASS_L 6
  84. #define UTF8PROC_BOUNDCLASS_V 7
  85. #define UTF8PROC_BOUNDCLASS_T 8
  86. #define UTF8PROC_BOUNDCLASS_LV 9
  87. #define UTF8PROC_BOUNDCLASS_LVT 10
  88. const char *utf8proc_errmsg(ssize_t errcode) {
  89. switch (errcode) {
  90. case UTF8PROC_ERROR_NOMEM:
  91. return "Memory for processing UTF-8 data could not be allocated.";
  92. case UTF8PROC_ERROR_OVERFLOW:
  93. return "UTF-8 string is too long to be processed.";
  94. case UTF8PROC_ERROR_INVALIDUTF8:
  95. return "Invalid UTF-8 string";
  96. case UTF8PROC_ERROR_NOTASSIGNED:
  97. return "Unassigned Unicode code point found in UTF-8 string.";
  98. case UTF8PROC_ERROR_INVALIDOPTS:
  99. return "Invalid options for UTF-8 processing chosen.";
  100. default:
  101. return "An unknown error occured while processing UTF-8 data.";
  102. }
  103. }
  104. ssize_t utf8proc_iterate(
  105. const uint8_t *str, ssize_t slen, int32_t *dst
  106. ) {
  107. int length;
  108. int i;
  109. int32_t uc = -1;
  110. *dst = -1;
  111. if (!slen) return 0;
  112. length = utf8proc_utf8class[str[0]];
  113. if (!length) return UTF8PROC_ERROR_INVALIDUTF8;
  114. if (slen >= 0 && length > slen) return UTF8PROC_ERROR_INVALIDUTF8;
  115. for (i=1; i<length; i++) {
  116. if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8;
  117. }
  118. switch (length) {
  119. case 1:
  120. uc = str[0];
  121. break;
  122. case 2:
  123. uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
  124. if (uc < 0x80) uc = -1;
  125. break;
  126. case 3:
  127. uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
  128. + (str[2] & 0x3F);
  129. if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
  130. (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
  131. break;
  132. case 4:
  133. uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
  134. + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
  135. if (uc < 0x10000 || uc >= 0x110000) uc = -1;
  136. break;
  137. }
  138. if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
  139. return UTF8PROC_ERROR_INVALIDUTF8;
  140. *dst = uc;
  141. return length;
  142. }
  143. bool utf8proc_codepoint_valid(int32_t uc) {
  144. if (uc < 0 || uc >= 0x110000 ||
  145. ((uc & 0xFFFF) >= 0xFFFE) || (uc >= 0xD800 && uc < 0xE000) ||
  146. (uc >= 0xFDD0 && uc < 0xFDF0)) return false;
  147. else return true;
  148. }
  149. ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
  150. if (uc < 0x00) {
  151. return 0;
  152. } else if (uc < 0x80) {
  153. dst[0] = uc;
  154. return 1;
  155. } else if (uc < 0x800) {
  156. dst[0] = 0xC0 + (uc >> 6);
  157. dst[1] = 0x80 + (uc & 0x3F);
  158. return 2;
  159. } else if (uc == 0xFFFF) {
  160. dst[0] = 0xFF;
  161. return 1;
  162. } else if (uc == 0xFFFE) {
  163. dst[0] = 0xFE;
  164. return 1;
  165. } else if (uc < 0x10000) {
  166. dst[0] = 0xE0 + (uc >> 12);
  167. dst[1] = 0x80 + ((uc >> 6) & 0x3F);
  168. dst[2] = 0x80 + (uc & 0x3F);
  169. return 3;
  170. } else if (uc < 0x110000) {
  171. dst[0] = 0xF0 + (uc >> 18);
  172. dst[1] = 0x80 + ((uc >> 12) & 0x3F);
  173. dst[2] = 0x80 + ((uc >> 6) & 0x3F);
  174. dst[3] = 0x80 + (uc & 0x3F);
  175. return 4;
  176. } else return 0;
  177. }
  178. const utf8proc_property_t *utf8proc_get_property(int32_t uc) {
  179. /* // ASSERT: uc >= 0 && uc < 0x110000*/
  180. return utf8proc_properties + (
  181. utf8proc_stage2table[
  182. utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
  183. ]
  184. );
  185. }
  186. #define utf8proc_decompose_lump(replacement_uc) \
  187. return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
  188. options & ~UTF8PROC_LUMP, last_boundclass)
  189. ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
  190. int options, int *last_boundclass) {
  191. /*// ASSERT: uc >= 0 && uc < 0x110000*/
  192. const utf8proc_property_t *property;
  193. utf8proc_propval_t category;
  194. int32_t hangul_sindex;
  195. property = utf8proc_get_property(uc);
  196. category = property->category;
  197. hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
  198. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  199. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
  200. int32_t hangul_tindex;
  201. if (bufsize >= 1) {
  202. dst[0] = UTF8PROC_HANGUL_LBASE +
  203. hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
  204. if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
  205. (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
  206. }
  207. hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
  208. if (!hangul_tindex) return 2;
  209. if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
  210. return 3;
  211. }
  212. }
  213. if (options & UTF8PROC_REJECTNA) {
  214. if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
  215. }
  216. if (options & UTF8PROC_IGNORE) {
  217. if (property->ignorable) return 0;
  218. }
  219. if (options & UTF8PROC_LUMP) {
  220. if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
  221. if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
  222. utf8proc_decompose_lump(0x0027);
  223. if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
  224. utf8proc_decompose_lump(0x002D);
  225. if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
  226. if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
  227. if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
  228. utf8proc_decompose_lump(0x003C);
  229. if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
  230. utf8proc_decompose_lump(0x003E);
  231. if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
  232. if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
  233. utf8proc_decompose_lump(0x005E);
  234. if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
  235. utf8proc_decompose_lump(0x005F);
  236. if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
  237. if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
  238. if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
  239. if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
  240. if (category == UTF8PROC_CATEGORY_ZL ||
  241. category == UTF8PROC_CATEGORY_ZP)
  242. utf8proc_decompose_lump(0x000A);
  243. }
  244. }
  245. if (options & UTF8PROC_STRIPMARK) {
  246. if (category == UTF8PROC_CATEGORY_MN ||
  247. category == UTF8PROC_CATEGORY_MC ||
  248. category == UTF8PROC_CATEGORY_ME) return 0;
  249. }
  250. if (options & UTF8PROC_CASEFOLD) {
  251. if (property->casefold_mapping) {
  252. const int32_t *casefold_entry;
  253. ssize_t written = 0;
  254. for (casefold_entry = property->casefold_mapping;
  255. *casefold_entry >= 0; casefold_entry++) {
  256. written += utf8proc_decompose_char(*casefold_entry, dst+written,
  257. (bufsize > written) ? (bufsize - written) : 0, options,
  258. last_boundclass);
  259. if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
  260. }
  261. return written;
  262. }
  263. }
  264. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  265. if (property->decomp_mapping &&
  266. (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
  267. const int32_t *decomp_entry;
  268. ssize_t written = 0;
  269. for (decomp_entry = property->decomp_mapping;
  270. *decomp_entry >= 0; decomp_entry++) {
  271. written += utf8proc_decompose_char(*decomp_entry, dst+written,
  272. (bufsize > written) ? (bufsize - written) : 0, options,
  273. last_boundclass);
  274. if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
  275. }
  276. return written;
  277. }
  278. }
  279. if (options & UTF8PROC_CHARBOUND) {
  280. bool boundary;
  281. int tbc, lbc;
  282. tbc =
  283. (uc == 0x000D) ? UTF8PROC_BOUNDCLASS_CR :
  284. (uc == 0x000A) ? UTF8PROC_BOUNDCLASS_LF :
  285. ((category == UTF8PROC_CATEGORY_ZL ||
  286. category == UTF8PROC_CATEGORY_ZP ||
  287. category == UTF8PROC_CATEGORY_CC ||
  288. category == UTF8PROC_CATEGORY_CF) &&
  289. !(uc == 0x200C || uc == 0x200D)) ? UTF8PROC_BOUNDCLASS_CONTROL :
  290. property->extend ? UTF8PROC_BOUNDCLASS_EXTEND :
  291. ((uc >= UTF8PROC_HANGUL_L_START && uc < UTF8PROC_HANGUL_L_END) ||
  292. uc == UTF8PROC_HANGUL_L_FILLER) ? UTF8PROC_BOUNDCLASS_L :
  293. (uc >= UTF8PROC_HANGUL_V_START && uc < UTF8PROC_HANGUL_V_END) ?
  294. UTF8PROC_BOUNDCLASS_V :
  295. (uc >= UTF8PROC_HANGUL_T_START && uc < UTF8PROC_HANGUL_T_END) ?
  296. UTF8PROC_BOUNDCLASS_T :
  297. (uc >= UTF8PROC_HANGUL_S_START && uc < UTF8PROC_HANGUL_S_END) ? (
  298. ((uc-UTF8PROC_HANGUL_SBASE) % UTF8PROC_HANGUL_TCOUNT == 0) ?
  299. UTF8PROC_BOUNDCLASS_LV : UTF8PROC_BOUNDCLASS_LVT
  300. ) :
  301. UTF8PROC_BOUNDCLASS_OTHER;
  302. lbc = *last_boundclass;
  303. boundary =
  304. (tbc == UTF8PROC_BOUNDCLASS_EXTEND) ? false :
  305. (lbc == UTF8PROC_BOUNDCLASS_START) ? true :
  306. (lbc == UTF8PROC_BOUNDCLASS_CR &&
  307. tbc == UTF8PROC_BOUNDCLASS_LF) ? false :
  308. (lbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
  309. (tbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
  310. (lbc == UTF8PROC_BOUNDCLASS_L &&
  311. (tbc == UTF8PROC_BOUNDCLASS_L ||
  312. tbc == UTF8PROC_BOUNDCLASS_V ||
  313. tbc == UTF8PROC_BOUNDCLASS_LV ||
  314. tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false :
  315. ((lbc == UTF8PROC_BOUNDCLASS_LV ||
  316. lbc == UTF8PROC_BOUNDCLASS_V) &&
  317. (tbc == UTF8PROC_BOUNDCLASS_V ||
  318. tbc == UTF8PROC_BOUNDCLASS_T)) ? false :
  319. ((lbc == UTF8PROC_BOUNDCLASS_LVT ||
  320. lbc == UTF8PROC_BOUNDCLASS_T) &&
  321. tbc == UTF8PROC_BOUNDCLASS_T) ? false :
  322. true;
  323. *last_boundclass = tbc;
  324. if (boundary) {
  325. if (bufsize >= 1) dst[0] = 0xFFFF;
  326. if (bufsize >= 2) dst[1] = uc;
  327. return 2;
  328. }
  329. }
  330. if (bufsize >= 1) *dst = uc;
  331. return 1;
  332. }
  333. ssize_t utf8proc_decompose(
  334. const uint8_t *str, ssize_t slen,
  335. int32_t *buffer, ssize_t bufsize, int options
  336. ) {
  337. /*// slen will be ignored, if UTF8PROC_NULLTERM is set in options*/
  338. ssize_t wpos = 0;
  339. if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
  340. return UTF8PROC_ERROR_INVALIDOPTS;
  341. if ((options & UTF8PROC_STRIPMARK) &&
  342. !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
  343. return UTF8PROC_ERROR_INVALIDOPTS;
  344. {
  345. int32_t uc;
  346. ssize_t rpos = 0;
  347. ssize_t decomp_result;
  348. int boundclass = UTF8PROC_BOUNDCLASS_START;
  349. while (1) {
  350. if (options & UTF8PROC_NULLTERM) {
  351. rpos += utf8proc_iterate(str + rpos, -1, &uc);
  352. /* checking of return value is not neccessary,
  353. as 'uc' is < 0 in case of error. */
  354. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  355. if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
  356. if (uc == 0) break;
  357. } else {
  358. if (rpos >= slen) break;
  359. rpos += utf8proc_iterate(str + rpos, slen - rpos, &uc);
  360. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  361. }
  362. decomp_result = utf8proc_decompose_char(
  363. uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
  364. &boundclass
  365. );
  366. if (decomp_result < 0) return decomp_result;
  367. wpos += decomp_result;
  368. /* // prohibiting integer overflows due to too long strings:*/
  369. if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2)
  370. return UTF8PROC_ERROR_OVERFLOW;
  371. }
  372. }
  373. if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
  374. ssize_t pos = 0;
  375. while (pos < wpos-1) {
  376. int32_t uc1, uc2;
  377. const utf8proc_property_t *property1, *property2;
  378. uc1 = buffer[pos];
  379. uc2 = buffer[pos+1];
  380. property1 = utf8proc_get_property(uc1);
  381. property2 = utf8proc_get_property(uc2);
  382. if (property1->combining_class > property2->combining_class &&
  383. property2->combining_class > 0) {
  384. buffer[pos] = uc2;
  385. buffer[pos+1] = uc1;
  386. if (pos > 0) pos--; else pos++;
  387. } else {
  388. pos++;
  389. }
  390. }
  391. }
  392. return wpos;
  393. }
  394. ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) {
  395. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
  396. ASSERT: 'buffer' has one spare byte of free space at the end! */
  397. if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
  398. ssize_t rpos;
  399. ssize_t wpos = 0;
  400. int32_t uc;
  401. for (rpos = 0; rpos < length; rpos++) {
  402. uc = buffer[rpos];
  403. if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
  404. if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
  405. ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
  406. if (options & UTF8PROC_NLF2LS) {
  407. if (options & UTF8PROC_NLF2PS) {
  408. buffer[wpos++] = 0x000A;
  409. } else {
  410. buffer[wpos++] = 0x2028;
  411. }
  412. } else {
  413. if (options & UTF8PROC_NLF2PS) {
  414. buffer[wpos++] = 0x2029;
  415. } else {
  416. buffer[wpos++] = 0x0020;
  417. }
  418. }
  419. } else if ((options & UTF8PROC_STRIPCC) &&
  420. (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
  421. if (uc == 0x0009) buffer[wpos++] = 0x0020;
  422. } else {
  423. buffer[wpos++] = uc;
  424. }
  425. }
  426. length = wpos;
  427. }
  428. if (options & UTF8PROC_COMPOSE) {
  429. int32_t *starter = NULL;
  430. int32_t current_char;
  431. const utf8proc_property_t *starter_property = NULL, *current_property;
  432. utf8proc_propval_t max_combining_class = -1;
  433. ssize_t rpos;
  434. ssize_t wpos = 0;
  435. int32_t composition;
  436. for (rpos = 0; rpos < length; rpos++) {
  437. current_char = buffer[rpos];
  438. current_property = utf8proc_get_property(current_char);
  439. if (starter && current_property->combining_class > max_combining_class) {
  440. /* // combination perhaps possible*/
  441. int32_t hangul_lindex;
  442. int32_t hangul_sindex;
  443. hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
  444. if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
  445. int32_t hangul_vindex;
  446. hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
  447. if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
  448. *starter = UTF8PROC_HANGUL_SBASE +
  449. (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
  450. UTF8PROC_HANGUL_TCOUNT;
  451. starter_property = NULL;
  452. continue;
  453. }
  454. }
  455. hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
  456. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
  457. (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
  458. int32_t hangul_tindex;
  459. hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
  460. if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
  461. *starter += hangul_tindex;
  462. starter_property = NULL;
  463. continue;
  464. }
  465. }
  466. if (!starter_property) {
  467. starter_property = utf8proc_get_property(*starter);
  468. }
  469. if (starter_property->comb1st_index >= 0 &&
  470. current_property->comb2nd_index >= 0) {
  471. composition = utf8proc_combinations[
  472. starter_property->comb1st_index +
  473. current_property->comb2nd_index
  474. ];
  475. if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
  476. !(utf8proc_get_property(composition)->comp_exclusion))) {
  477. *starter = composition;
  478. starter_property = NULL;
  479. continue;
  480. }
  481. }
  482. }
  483. buffer[wpos] = current_char;
  484. if (current_property->combining_class) {
  485. if (current_property->combining_class > max_combining_class) {
  486. max_combining_class = current_property->combining_class;
  487. }
  488. } else {
  489. starter = buffer + wpos;
  490. starter_property = NULL;
  491. max_combining_class = -1;
  492. }
  493. wpos++;
  494. }
  495. length = wpos;
  496. }
  497. {
  498. ssize_t rpos, wpos = 0;
  499. int32_t uc;
  500. for (rpos = 0; rpos < length; rpos++) {
  501. uc = buffer[rpos];
  502. wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos);
  503. }
  504. ((uint8_t *)buffer)[wpos] = 0;
  505. return wpos;
  506. }
  507. }
  508. ssize_t utf8proc_map(
  509. const uint8_t *str, ssize_t slen, uint8_t **dstptr, int options
  510. ) {
  511. int32_t *buffer;
  512. ssize_t result;
  513. *dstptr = NULL;
  514. result = utf8proc_decompose(str, slen, NULL, 0, options);
  515. if (result < 0) return result;
  516. buffer = malloc(result * sizeof(int32_t) + 1);
  517. if (!buffer) return UTF8PROC_ERROR_NOMEM;
  518. result = utf8proc_decompose(str, slen, buffer, result, options);
  519. if (result < 0) {
  520. free(buffer);
  521. return result;
  522. }
  523. result = utf8proc_reencode(buffer, result, options);
  524. if (result < 0) {
  525. free(buffer);
  526. return result;
  527. }
  528. {
  529. int32_t *newptr;
  530. newptr = realloc(buffer, result+1);
  531. if (newptr) buffer = newptr;
  532. }
  533. *dstptr = (uint8_t *)buffer;
  534. return result;
  535. }
  536. uint8_t *utf8proc_NFD(const uint8_t *str) {
  537. uint8_t *retval;
  538. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  539. UTF8PROC_DECOMPOSE);
  540. return retval;
  541. }
  542. uint8_t *utf8proc_NFC(const uint8_t *str) {
  543. uint8_t *retval;
  544. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  545. UTF8PROC_COMPOSE);
  546. return retval;
  547. }
  548. uint8_t *utf8proc_NFKD(const uint8_t *str) {
  549. uint8_t *retval;
  550. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  551. UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
  552. return retval;
  553. }
  554. uint8_t *utf8proc_NFKC(const uint8_t *str) {
  555. uint8_t *retval;
  556. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  557. UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
  558. return retval;
  559. }
  560. ssize_t utf8proc_check(const uint8_t *str) {
  561. ssize_t result;
  562. result = utf8proc_decompose(str, 0, NULL, 0,
  563. UTF8PROC_NULLTERM | UTF8PROC_STABLE);
  564. return result;
  565. }