utf8proc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */
  2. /*
  3. * Copyright (c) 2014-2021 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
  4. * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * This library contains derived data from a modified version of the
  26. * Unicode data files.
  27. *
  28. * The original data files are available at
  29. * https://www.unicode.org/Public/UNIDATA/
  30. *
  31. * Please notice the copyright statement in the file "utf8proc_data.c".
  32. */
  33. /*
  34. * File name: utf8proc.c
  35. *
  36. * Description:
  37. * Implementation of libutf8proc.
  38. */
  39. #include "utf8proc.h"
  40. #ifndef SSIZE_MAX
  41. #define SSIZE_MAX ((size_t)SIZE_MAX/2)
  42. #endif
  43. #ifndef UINT16_MAX
  44. # define UINT16_MAX 65535U
  45. #endif
  46. #include "utf8proc_data.c"
  47. UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = {
  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. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  62. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  63. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
  64. #define UTF8PROC_HANGUL_SBASE 0xAC00
  65. #define UTF8PROC_HANGUL_LBASE 0x1100
  66. #define UTF8PROC_HANGUL_VBASE 0x1161
  67. #define UTF8PROC_HANGUL_TBASE 0x11A7
  68. #define UTF8PROC_HANGUL_LCOUNT 19
  69. #define UTF8PROC_HANGUL_VCOUNT 21
  70. #define UTF8PROC_HANGUL_TCOUNT 28
  71. #define UTF8PROC_HANGUL_NCOUNT 588
  72. #define UTF8PROC_HANGUL_SCOUNT 11172
  73. /* END is exclusive */
  74. #define UTF8PROC_HANGUL_L_START 0x1100
  75. #define UTF8PROC_HANGUL_L_END 0x115A
  76. #define UTF8PROC_HANGUL_L_FILLER 0x115F
  77. #define UTF8PROC_HANGUL_V_START 0x1160
  78. #define UTF8PROC_HANGUL_V_END 0x11A3
  79. #define UTF8PROC_HANGUL_T_START 0x11A8
  80. #define UTF8PROC_HANGUL_T_END 0x11FA
  81. #define UTF8PROC_HANGUL_S_START 0xAC00
  82. #define UTF8PROC_HANGUL_S_END 0xD7A4
  83. /* Should follow semantic-versioning rules (semver.org) based on API
  84. compatibility. (Note that the shared-library version number will
  85. be different, being based on ABI compatibility.): */
  86. #define STRINGIZEx(x) #x
  87. #define STRINGIZE(x) STRINGIZEx(x)
  88. UTF8PROC_DLLEXPORT const char *utf8proc_version(void) {
  89. return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) "";
  90. }
  91. UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void) {
  92. return "15.1.0";
  93. }
  94. UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) {
  95. switch (errcode) {
  96. case UTF8PROC_ERROR_NOMEM:
  97. return "Memory for processing UTF-8 data could not be allocated.";
  98. case UTF8PROC_ERROR_OVERFLOW:
  99. return "UTF-8 string is too long to be processed.";
  100. case UTF8PROC_ERROR_INVALIDUTF8:
  101. return "Invalid UTF-8 string";
  102. case UTF8PROC_ERROR_NOTASSIGNED:
  103. return "Unassigned Unicode code point found in UTF-8 string.";
  104. case UTF8PROC_ERROR_INVALIDOPTS:
  105. return "Invalid options for UTF-8 processing chosen.";
  106. default:
  107. return "An unknown error occurred while processing UTF-8 data.";
  108. }
  109. }
  110. #define utf_cont(ch) (((ch) & 0xc0) == 0x80)
  111. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
  112. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst
  113. ) {
  114. utf8proc_int32_t uc;
  115. const utf8proc_uint8_t *end;
  116. *dst = -1;
  117. if (!strlen) return 0;
  118. end = str + ((strlen < 0) ? 4 : strlen);
  119. uc = *str++;
  120. if (uc < 0x80) {
  121. *dst = uc;
  122. return 1;
  123. }
  124. // Must be between 0xc2 and 0xf4 inclusive to be valid
  125. if ((utf8proc_uint32_t)(uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
  126. if (uc < 0xe0) { // 2-byte sequence
  127. // Must have valid continuation character
  128. if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
  129. *dst = ((uc & 0x1f)<<6) | (*str & 0x3f);
  130. return 2;
  131. }
  132. if (uc < 0xf0) { // 3-byte sequence
  133. if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
  134. return UTF8PROC_ERROR_INVALIDUTF8;
  135. // Check for surrogate chars
  136. if (uc == 0xed && *str > 0x9f)
  137. return UTF8PROC_ERROR_INVALIDUTF8;
  138. uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f);
  139. if (uc < 0x800)
  140. return UTF8PROC_ERROR_INVALIDUTF8;
  141. *dst = uc;
  142. return 3;
  143. }
  144. // 4-byte sequence
  145. // Must have 3 valid continuation characters
  146. if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2]))
  147. return UTF8PROC_ERROR_INVALIDUTF8;
  148. // Make sure in correct range (0x10000 - 0x10ffff)
  149. if (uc == 0xf0) {
  150. if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8;
  151. } else if (uc == 0xf4) {
  152. if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8;
  153. }
  154. *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f);
  155. return 4;
  156. }
  157. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) {
  158. return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000);
  159. }
  160. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
  161. if (uc < 0x00) {
  162. return 0;
  163. } else if (uc < 0x80) {
  164. dst[0] = (utf8proc_uint8_t) uc;
  165. return 1;
  166. } else if (uc < 0x800) {
  167. dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
  168. dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  169. return 2;
  170. // Note: we allow encoding 0xd800-0xdfff here, so as not to change
  171. // the API, however, these are actually invalid in UTF-8
  172. } else if (uc < 0x10000) {
  173. dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
  174. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  175. dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  176. return 3;
  177. } else if (uc < 0x110000) {
  178. dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
  179. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
  180. dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  181. dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  182. return 4;
  183. } else return 0;
  184. }
  185. /* internal version used for inserting 0xff bytes between graphemes */
  186. static utf8proc_ssize_t charbound_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
  187. if (uc < 0x00) {
  188. if (uc == -1) { /* internal value used for grapheme breaks */
  189. dst[0] = (utf8proc_uint8_t)0xFF;
  190. return 1;
  191. }
  192. return 0;
  193. } else if (uc < 0x80) {
  194. dst[0] = (utf8proc_uint8_t)uc;
  195. return 1;
  196. } else if (uc < 0x800) {
  197. dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
  198. dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  199. return 2;
  200. } else if (uc < 0x10000) {
  201. dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
  202. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  203. dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  204. return 3;
  205. } else if (uc < 0x110000) {
  206. dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
  207. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
  208. dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  209. dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  210. return 4;
  211. } else return 0;
  212. }
  213. /* internal "unsafe" version that does not check whether uc is in range */
  214. static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) {
  215. /* ASSERT: uc >= 0 && uc < 0x110000 */
  216. return utf8proc_properties + (
  217. utf8proc_stage2table[
  218. utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
  219. ]
  220. );
  221. }
  222. UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) {
  223. return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc);
  224. }
  225. /* return whether there is a grapheme break between boundclasses lbc and tbc
  226. (according to the definition of extended grapheme clusters)
  227. Rule numbering refers to TR29 Version 29 (Unicode 9.0.0):
  228. http://www.unicode.org/reports/tr29/tr29-29.html
  229. CAVEATS:
  230. Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences)
  231. and GB 12/13 (regional indicator code points) require knowledge of previous characters
  232. and are thus not handled by this function. This may result in an incorrect break before
  233. an E_Modifier class codepoint and an incorrectly missing break between two
  234. REGIONAL_INDICATOR class code points if such support does not exist in the caller.
  235. See the special support in grapheme_break_extended, for required bookkeeping by the caller.
  236. */
  237. static utf8proc_bool grapheme_break_simple(int lbc, int tbc) {
  238. return
  239. (lbc == UTF8PROC_BOUNDCLASS_START) ? true : // GB1
  240. (lbc == UTF8PROC_BOUNDCLASS_CR && // GB3
  241. tbc == UTF8PROC_BOUNDCLASS_LF) ? false : // ---
  242. (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB4
  243. (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB5
  244. (lbc == UTF8PROC_BOUNDCLASS_L && // GB6
  245. (tbc == UTF8PROC_BOUNDCLASS_L || // ---
  246. tbc == UTF8PROC_BOUNDCLASS_V || // ---
  247. tbc == UTF8PROC_BOUNDCLASS_LV || // ---
  248. tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false : // ---
  249. ((lbc == UTF8PROC_BOUNDCLASS_LV || // GB7
  250. lbc == UTF8PROC_BOUNDCLASS_V) && // ---
  251. (tbc == UTF8PROC_BOUNDCLASS_V || // ---
  252. tbc == UTF8PROC_BOUNDCLASS_T)) ? false : // ---
  253. ((lbc == UTF8PROC_BOUNDCLASS_LVT || // GB8
  254. lbc == UTF8PROC_BOUNDCLASS_T) && // ---
  255. tbc == UTF8PROC_BOUNDCLASS_T) ? false : // ---
  256. (tbc == UTF8PROC_BOUNDCLASS_EXTEND || // GB9
  257. tbc == UTF8PROC_BOUNDCLASS_ZWJ || // ---
  258. tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK || // GB9a
  259. lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false : // GB9b
  260. (lbc == UTF8PROC_BOUNDCLASS_E_ZWG && // GB11 (requires additional handling below)
  261. tbc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) ? false : // ----
  262. (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR && // GB12/13 (requires additional handling below)
  263. tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false : // ----
  264. true; // GB999
  265. }
  266. static utf8proc_bool grapheme_break_extended(int lbc, int tbc, int licb, int ticb, utf8proc_int32_t *state)
  267. {
  268. if (state) {
  269. int state_bc, state_icb; /* boundclass and indic_conjunct_break state */
  270. if (*state == 0) { /* state initialization */
  271. state_bc = lbc;
  272. state_icb = licb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT ? licb : UTF8PROC_INDIC_CONJUNCT_BREAK_NONE;
  273. }
  274. else { /* lbc and licb are already encoded in *state */
  275. state_bc = *state & 0xff; // 1st byte of state is bound class
  276. state_icb = *state >> 8; // 2nd byte of state is indic conjunct break
  277. }
  278. utf8proc_bool break_permitted = grapheme_break_simple(state_bc, tbc) &&
  279. !(state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER
  280. && ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT); // GB9c
  281. // Special support for GB9c. Don't break between two consonants
  282. // separated 1+ linker characters and 0+ extend characters in any order.
  283. // After a consonant, we enter LINKER state after at least one linker.
  284. if (ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT
  285. || state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT
  286. || state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND)
  287. state_icb = ticb;
  288. else if (state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER)
  289. state_icb = ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND ?
  290. UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER : ticb;
  291. // Special support for GB 12/13 made possible by GB999. After two RI
  292. // class codepoints we want to force a break. Do this by resetting the
  293. // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break
  294. // after that character according to GB999 (unless of course such a break is
  295. // forbidden by a different rule such as GB9).
  296. if (state_bc == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR)
  297. state_bc = UTF8PROC_BOUNDCLASS_OTHER;
  298. // Special support for GB11 (emoji extend* zwj / emoji)
  299. else if (state_bc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) {
  300. if (tbc == UTF8PROC_BOUNDCLASS_EXTEND) // fold EXTEND codepoints into emoji
  301. state_bc = UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC;
  302. else if (tbc == UTF8PROC_BOUNDCLASS_ZWJ)
  303. state_bc = UTF8PROC_BOUNDCLASS_E_ZWG; // state to record emoji+zwg combo
  304. else
  305. state_bc = tbc;
  306. }
  307. else
  308. state_bc = tbc;
  309. *state = state_bc + (state_icb << 8);
  310. return break_permitted;
  311. }
  312. else
  313. return grapheme_break_simple(lbc, tbc);
  314. }
  315. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  316. utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) {
  317. const utf8proc_property_t *p1 = utf8proc_get_property(c1);
  318. const utf8proc_property_t *p2 = utf8proc_get_property(c2);
  319. return grapheme_break_extended(p1->boundclass,
  320. p2->boundclass,
  321. p1->indic_conjunct_break,
  322. p2->indic_conjunct_break,
  323. state);
  324. }
  325. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  326. utf8proc_int32_t c1, utf8proc_int32_t c2) {
  327. return utf8proc_grapheme_break_stateful(c1, c2, NULL);
  328. }
  329. static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry)
  330. {
  331. utf8proc_int32_t entry_cp = **entry;
  332. if ((entry_cp & 0xF800) == 0xD800) {
  333. *entry = *entry + 1;
  334. entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF);
  335. entry_cp += 0x10000;
  336. }
  337. return entry_cp;
  338. }
  339. static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex)
  340. {
  341. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex];
  342. return seqindex_decode_entry(&entry);
  343. }
  344. static utf8proc_ssize_t seqindex_write_char_decomposed(utf8proc_uint16_t seqindex, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
  345. utf8proc_ssize_t written = 0;
  346. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x3FFF];
  347. int len = seqindex >> 14;
  348. if (len >= 3) {
  349. len = *entry;
  350. entry++;
  351. }
  352. for (; len >= 0; entry++, len--) {
  353. utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry);
  354. written += utf8proc_decompose_char(entry_cp, dst+written,
  355. (bufsize > written) ? (bufsize - written) : 0, options,
  356. last_boundclass);
  357. if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
  358. }
  359. return written;
  360. }
  361. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
  362. {
  363. utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex;
  364. return cl != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cl) : c;
  365. }
  366. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
  367. {
  368. utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex;
  369. return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
  370. }
  371. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c)
  372. {
  373. utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex;
  374. return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
  375. }
  376. UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c)
  377. {
  378. const utf8proc_property_t *p = utf8proc_get_property(c);
  379. return p->lowercase_seqindex != p->uppercase_seqindex && p->lowercase_seqindex == UINT16_MAX;
  380. }
  381. UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c)
  382. {
  383. const utf8proc_property_t *p = utf8proc_get_property(c);
  384. return p->lowercase_seqindex != p->uppercase_seqindex && p->uppercase_seqindex == UINT16_MAX && p->category != UTF8PROC_CATEGORY_LT;
  385. }
  386. /* return a character width analogous to wcwidth (except portable and
  387. hopefully less buggy than most system wcwidth functions). */
  388. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) {
  389. return utf8proc_get_property(c)->charwidth;
  390. }
  391. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) {
  392. return (utf8proc_category_t) utf8proc_get_property(c)->category;
  393. }
  394. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
  395. static const char s[][3] = {"Cn","Lu","Ll","Lt","Lm","Lo","Mn","Mc","Me","Nd","Nl","No","Pc","Pd","Ps","Pe","Pi","Pf","Po","Sm","Sc","Sk","So","Zs","Zl","Zp","Cc","Cf","Cs","Co"};
  396. return s[utf8proc_category(c)];
  397. }
  398. #define utf8proc_decompose_lump(replacement_uc) \
  399. return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
  400. options & ~(unsigned int)UTF8PROC_LUMP, last_boundclass)
  401. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(utf8proc_int32_t uc, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
  402. const utf8proc_property_t *property;
  403. utf8proc_propval_t category;
  404. utf8proc_int32_t hangul_sindex;
  405. if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
  406. property = unsafe_get_property(uc);
  407. category = property->category;
  408. hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
  409. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  410. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
  411. utf8proc_int32_t hangul_tindex;
  412. if (bufsize >= 1) {
  413. dst[0] = UTF8PROC_HANGUL_LBASE +
  414. hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
  415. if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
  416. (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
  417. }
  418. hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
  419. if (!hangul_tindex) return 2;
  420. if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
  421. return 3;
  422. }
  423. }
  424. if (options & UTF8PROC_REJECTNA) {
  425. if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
  426. }
  427. if (options & UTF8PROC_IGNORE) {
  428. if (property->ignorable) return 0;
  429. }
  430. if (options & UTF8PROC_STRIPNA) {
  431. if (!category) return 0;
  432. }
  433. if (options & UTF8PROC_LUMP) {
  434. if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
  435. if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
  436. utf8proc_decompose_lump(0x0027);
  437. if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
  438. utf8proc_decompose_lump(0x002D);
  439. if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
  440. if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
  441. if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
  442. utf8proc_decompose_lump(0x003C);
  443. if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
  444. utf8proc_decompose_lump(0x003E);
  445. if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
  446. if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
  447. utf8proc_decompose_lump(0x005E);
  448. if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
  449. utf8proc_decompose_lump(0x005F);
  450. if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
  451. if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
  452. if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
  453. if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
  454. if (category == UTF8PROC_CATEGORY_ZL ||
  455. category == UTF8PROC_CATEGORY_ZP)
  456. utf8proc_decompose_lump(0x000A);
  457. }
  458. }
  459. if (options & UTF8PROC_STRIPMARK) {
  460. if (category == UTF8PROC_CATEGORY_MN ||
  461. category == UTF8PROC_CATEGORY_MC ||
  462. category == UTF8PROC_CATEGORY_ME) return 0;
  463. }
  464. if (options & UTF8PROC_CASEFOLD) {
  465. if (property->casefold_seqindex != UINT16_MAX) {
  466. return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass);
  467. }
  468. }
  469. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  470. if (property->decomp_seqindex != UINT16_MAX &&
  471. (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
  472. return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass);
  473. }
  474. }
  475. if (options & UTF8PROC_CHARBOUND) {
  476. utf8proc_bool boundary;
  477. boundary = grapheme_break_extended(0, property->boundclass, 0, property->indic_conjunct_break,
  478. last_boundclass);
  479. if (boundary) {
  480. if (bufsize >= 1) dst[0] = -1; /* sentinel value for grapheme break */
  481. if (bufsize >= 2) dst[1] = uc;
  482. return 2;
  483. }
  484. }
  485. if (bufsize >= 1) *dst = uc;
  486. return 1;
  487. }
  488. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  489. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  490. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  491. ) {
  492. return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL);
  493. }
  494. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  495. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  496. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  497. utf8proc_custom_func custom_func, void *custom_data
  498. ) {
  499. /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
  500. utf8proc_ssize_t wpos = 0;
  501. if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
  502. return UTF8PROC_ERROR_INVALIDOPTS;
  503. if ((options & UTF8PROC_STRIPMARK) &&
  504. !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
  505. return UTF8PROC_ERROR_INVALIDOPTS;
  506. {
  507. utf8proc_int32_t uc;
  508. utf8proc_ssize_t rpos = 0;
  509. utf8proc_ssize_t decomp_result;
  510. int boundclass = UTF8PROC_BOUNDCLASS_START;
  511. while (1) {
  512. if (options & UTF8PROC_NULLTERM) {
  513. rpos += utf8proc_iterate(str + rpos, -1, &uc);
  514. /* checking of return value is not necessary,
  515. as 'uc' is < 0 in case of error */
  516. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  517. if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
  518. if (uc == 0) break;
  519. } else {
  520. if (rpos >= strlen) break;
  521. rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
  522. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  523. }
  524. if (custom_func != NULL) {
  525. uc = custom_func(uc, custom_data); /* user-specified custom mapping */
  526. }
  527. decomp_result = utf8proc_decompose_char(
  528. uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
  529. &boundclass
  530. );
  531. if (decomp_result < 0) return decomp_result;
  532. wpos += decomp_result;
  533. /* prohibiting integer overflows due to too long strings: */
  534. if (wpos < 0 ||
  535. wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2))
  536. return UTF8PROC_ERROR_OVERFLOW;
  537. }
  538. }
  539. if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
  540. utf8proc_ssize_t pos = 0;
  541. while (pos < wpos-1) {
  542. utf8proc_int32_t uc1, uc2;
  543. const utf8proc_property_t *property1, *property2;
  544. uc1 = buffer[pos];
  545. uc2 = buffer[pos+1];
  546. property1 = unsafe_get_property(uc1);
  547. property2 = unsafe_get_property(uc2);
  548. if (property1->combining_class > property2->combining_class &&
  549. property2->combining_class > 0) {
  550. buffer[pos] = uc2;
  551. buffer[pos+1] = uc1;
  552. if (pos > 0) pos--; else pos++;
  553. } else {
  554. pos++;
  555. }
  556. }
  557. }
  558. return wpos;
  559. }
  560. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  561. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */
  562. if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
  563. utf8proc_ssize_t rpos;
  564. utf8proc_ssize_t wpos = 0;
  565. utf8proc_int32_t uc;
  566. for (rpos = 0; rpos < length; rpos++) {
  567. uc = buffer[rpos];
  568. if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
  569. if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
  570. ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
  571. if (options & UTF8PROC_NLF2LS) {
  572. if (options & UTF8PROC_NLF2PS) {
  573. buffer[wpos++] = 0x000A;
  574. } else {
  575. buffer[wpos++] = 0x2028;
  576. }
  577. } else {
  578. if (options & UTF8PROC_NLF2PS) {
  579. buffer[wpos++] = 0x2029;
  580. } else {
  581. buffer[wpos++] = 0x0020;
  582. }
  583. }
  584. } else if ((options & UTF8PROC_STRIPCC) &&
  585. (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
  586. if (uc == 0x0009) buffer[wpos++] = 0x0020;
  587. } else {
  588. buffer[wpos++] = uc;
  589. }
  590. }
  591. length = wpos;
  592. }
  593. if (options & UTF8PROC_COMPOSE) {
  594. utf8proc_int32_t *starter = NULL;
  595. utf8proc_int32_t current_char;
  596. const utf8proc_property_t *starter_property = NULL, *current_property;
  597. utf8proc_propval_t max_combining_class = -1;
  598. utf8proc_ssize_t rpos;
  599. utf8proc_ssize_t wpos = 0;
  600. utf8proc_int32_t composition;
  601. for (rpos = 0; rpos < length; rpos++) {
  602. current_char = buffer[rpos];
  603. current_property = unsafe_get_property(current_char);
  604. if (starter && current_property->combining_class > max_combining_class) {
  605. /* combination perhaps possible */
  606. utf8proc_int32_t hangul_lindex;
  607. utf8proc_int32_t hangul_sindex;
  608. hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
  609. if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
  610. utf8proc_int32_t hangul_vindex;
  611. hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
  612. if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
  613. *starter = UTF8PROC_HANGUL_SBASE +
  614. (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
  615. UTF8PROC_HANGUL_TCOUNT;
  616. starter_property = NULL;
  617. continue;
  618. }
  619. }
  620. hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
  621. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
  622. (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
  623. utf8proc_int32_t hangul_tindex;
  624. hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
  625. if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
  626. *starter += hangul_tindex;
  627. starter_property = NULL;
  628. continue;
  629. }
  630. }
  631. if (!starter_property) {
  632. starter_property = unsafe_get_property(*starter);
  633. }
  634. if (starter_property->comb_index < 0x8000 &&
  635. current_property->comb_index != UINT16_MAX &&
  636. current_property->comb_index >= 0x8000) {
  637. int sidx = starter_property->comb_index;
  638. int idx = current_property->comb_index & 0x3FFF;
  639. if (idx >= utf8proc_combinations[sidx] && idx <= utf8proc_combinations[sidx + 1] ) {
  640. idx += sidx + 2 - utf8proc_combinations[sidx];
  641. if (current_property->comb_index & 0x4000) {
  642. composition = (utf8proc_combinations[idx] << 16) | utf8proc_combinations[idx+1];
  643. } else
  644. composition = utf8proc_combinations[idx];
  645. if (composition > 0 && (!(options & UTF8PROC_STABLE) ||
  646. !(unsafe_get_property(composition)->comp_exclusion))) {
  647. *starter = composition;
  648. starter_property = NULL;
  649. continue;
  650. }
  651. }
  652. }
  653. }
  654. buffer[wpos] = current_char;
  655. if (current_property->combining_class) {
  656. if (current_property->combining_class > max_combining_class) {
  657. max_combining_class = current_property->combining_class;
  658. }
  659. } else {
  660. starter = buffer + wpos;
  661. starter_property = NULL;
  662. max_combining_class = -1;
  663. }
  664. wpos++;
  665. }
  666. length = wpos;
  667. }
  668. return length;
  669. }
  670. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  671. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
  672. ASSERT: 'buffer' has one spare byte of free space at the end! */
  673. length = utf8proc_normalize_utf32(buffer, length, options);
  674. if (length < 0) return length;
  675. {
  676. utf8proc_ssize_t rpos, wpos = 0;
  677. utf8proc_int32_t uc;
  678. if (options & UTF8PROC_CHARBOUND) {
  679. for (rpos = 0; rpos < length; rpos++) {
  680. uc = buffer[rpos];
  681. wpos += charbound_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  682. }
  683. } else {
  684. for (rpos = 0; rpos < length; rpos++) {
  685. uc = buffer[rpos];
  686. wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  687. }
  688. }
  689. ((utf8proc_uint8_t *)buffer)[wpos] = 0;
  690. return wpos;
  691. }
  692. }
  693. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  694. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  695. ) {
  696. return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL);
  697. }
  698. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  699. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  700. utf8proc_custom_func custom_func, void *custom_data
  701. ) {
  702. utf8proc_int32_t *buffer;
  703. utf8proc_ssize_t result;
  704. *dstptr = NULL;
  705. result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
  706. if (result < 0) return result;
  707. buffer = (utf8proc_int32_t *) malloc(((utf8proc_size_t)result) * sizeof(utf8proc_int32_t) + 1);
  708. if (!buffer) return UTF8PROC_ERROR_NOMEM;
  709. result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
  710. if (result < 0) {
  711. free(buffer);
  712. return result;
  713. }
  714. result = utf8proc_reencode(buffer, result, options);
  715. if (result < 0) {
  716. free(buffer);
  717. return result;
  718. }
  719. {
  720. utf8proc_int32_t *newptr;
  721. newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
  722. if (newptr) buffer = newptr;
  723. }
  724. *dstptr = (utf8proc_uint8_t *)buffer;
  725. return result;
  726. }
  727. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str) {
  728. utf8proc_uint8_t *retval;
  729. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  730. UTF8PROC_DECOMPOSE);
  731. return retval;
  732. }
  733. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str) {
  734. utf8proc_uint8_t *retval;
  735. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  736. UTF8PROC_COMPOSE);
  737. return retval;
  738. }
  739. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str) {
  740. utf8proc_uint8_t *retval;
  741. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  742. UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
  743. return retval;
  744. }
  745. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str) {
  746. utf8proc_uint8_t *retval;
  747. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  748. UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
  749. return retval;
  750. }
  751. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str) {
  752. utf8proc_uint8_t *retval;
  753. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  754. UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE);
  755. return retval;
  756. }