nfkc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /* nfkc.c --- Unicode normalization utilities.
  2. Copyright (C) 2002-2024 Simon Josefsson
  3. This file is part of GNU Libidn.
  4. GNU Libidn is free software: you can redistribute it and/or
  5. modify it under the terms of either:
  6. * the GNU Lesser General Public License as published by the Free
  7. Software Foundation; either version 3 of the License, or (at
  8. your option) any later version.
  9. or
  10. * the GNU General Public License as published by the Free
  11. Software Foundation; either version 2 of the License, or (at
  12. your option) any later version.
  13. or both in parallel, as here.
  14. GNU Libidn is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received copies of the GNU General Public License and
  19. the GNU Lesser General Public License along with this program. If
  20. not, see <https://www.gnu.org/licenses/>. */
  21. #ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. #endif
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "stringprep.h"
  27. /* Hacks to make syncing with GLIB code easier. */
  28. #define gboolean int
  29. #define gchar char
  30. #define guchar unsigned char
  31. #define gint int
  32. #define guint unsigned int
  33. #define gushort unsigned short
  34. #define gint16 int16_t
  35. #define guint16 uint16_t
  36. #define gunichar uint32_t
  37. #define gsize size_t
  38. #define gssize ssize_t
  39. #define g_malloc malloc
  40. #define g_free free
  41. #define g_return_val_if_fail(expr,val) { \
  42. if (!(expr)) \
  43. return (val); \
  44. }
  45. /* Code from GLIB gmacros.h starts here. */
  46. /* GLIB - Library of useful routines for C programming
  47. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  48. *
  49. * This library is free software; you can redistribute it and/or
  50. * modify it under the terms of the GNU Lesser General Public
  51. * License as published by the Free Software Foundation; either
  52. * version 2 of the License, or (at your option) any later version.
  53. *
  54. * This library is distributed in the hope that it will be useful,
  55. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  57. * Lesser General Public License for more details.
  58. *
  59. * You should have received a copy of the GNU Lesser General Public
  60. * License along with this library; if not, write to the
  61. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  62. * Boston, MA 02111-1307, USA.
  63. */
  64. #ifndef FALSE
  65. # define FALSE (0)
  66. #endif
  67. #ifndef TRUE
  68. # define TRUE (!FALSE)
  69. #endif
  70. #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
  71. #define G_UNLIKELY(expr) (expr)
  72. /* Code from GLIB gunicode.h starts here. */
  73. /* gunicode.h - Unicode manipulation functions
  74. *
  75. * Copyright (C) 1999, 2000 Tom Tromey
  76. * Copyright 2000, 2005 Red Hat, Inc.
  77. *
  78. * The Gnome Library is free software; you can redistribute it and/or
  79. * modify it under the terms of the GNU Lesser General Public License as
  80. * published by the Free Software Foundation; either version 2 of the
  81. * License, or (at your option) any later version.
  82. *
  83. * The Gnome Library is distributed in the hope that it will be useful,
  84. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  85. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  86. * Lesser General Public License for more details.
  87. *
  88. * You should have received a copy of the GNU Lesser General Public
  89. * License along with the Gnome Library; see the file COPYING.LIB. If not,
  90. * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  91. * Boston, MA 02111-1307, USA.
  92. */
  93. typedef enum
  94. {
  95. G_NORMALIZE_DEFAULT,
  96. G_NORMALIZE_NFD = G_NORMALIZE_DEFAULT,
  97. G_NORMALIZE_DEFAULT_COMPOSE,
  98. G_NORMALIZE_NFC = G_NORMALIZE_DEFAULT_COMPOSE,
  99. G_NORMALIZE_ALL,
  100. G_NORMALIZE_NFKD = G_NORMALIZE_ALL,
  101. G_NORMALIZE_ALL_COMPOSE,
  102. G_NORMALIZE_NFKC = G_NORMALIZE_ALL_COMPOSE
  103. }
  104. GNormalizeMode;
  105. #define g_utf8_next_char(p) ((p) + g_utf8_skip[*(const guchar *)(p)])
  106. /* Code from GLIB gutf8.c starts here. */
  107. /* gutf8.c - Operations on UTF-8 strings.
  108. *
  109. * Copyright (C) 1999 Tom Tromey
  110. * Copyright (C) 2000 Red Hat, Inc.
  111. *
  112. * This library is free software; you can redistribute it and/or
  113. * modify it under the terms of the GNU Lesser General Public
  114. * License as published by the Free Software Foundation; either
  115. * version 2 of the License, or (at your option) any later version.
  116. *
  117. * This library is distributed in the hope that it will be useful,
  118. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  119. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  120. * Lesser General Public License for more details.
  121. *
  122. * You should have received a copy of the GNU Lesser General Public
  123. * License along with this library; if not, write to the
  124. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  125. * Boston, MA 02111-1307, USA.
  126. */
  127. #define UTF8_COMPUTE(Char, Mask, Len) \
  128. if (Char < 128) \
  129. { \
  130. Len = 1; \
  131. Mask = 0x7f; \
  132. } \
  133. else if ((Char & 0xe0) == 0xc0) \
  134. { \
  135. Len = 2; \
  136. Mask = 0x1f; \
  137. } \
  138. else if ((Char & 0xf0) == 0xe0) \
  139. { \
  140. Len = 3; \
  141. Mask = 0x0f; \
  142. } \
  143. else if ((Char & 0xf8) == 0xf0) \
  144. { \
  145. Len = 4; \
  146. Mask = 0x07; \
  147. } \
  148. else if ((Char & 0xfc) == 0xf8) \
  149. { \
  150. Len = 5; \
  151. Mask = 0x03; \
  152. } \
  153. else if ((Char & 0xfe) == 0xfc) \
  154. { \
  155. Len = 6; \
  156. Mask = 0x01; \
  157. } \
  158. else \
  159. Len = -1;
  160. #define UTF8_LENGTH(Char) \
  161. ((Char) < 0x80 ? 1 : \
  162. ((Char) < 0x800 ? 2 : \
  163. ((Char) < 0x10000 ? 3 : \
  164. ((Char) < 0x200000 ? 4 : \
  165. ((Char) < 0x4000000 ? 5 : 6)))))
  166. #define UTF8_GET(Result, Chars, Count, Mask, Len) \
  167. (Result) = (Chars)[0] & (Mask); \
  168. for ((Count) = 1; (Count) < (Len); ++(Count)) \
  169. { \
  170. if (((Chars)[(Count)] & 0xc0) != 0x80) \
  171. { \
  172. (Result) = -1; \
  173. break; \
  174. } \
  175. (Result) <<= 6; \
  176. (Result) |= ((Chars)[(Count)] & 0x3f); \
  177. }
  178. static const gchar utf8_skip_data[256] = {
  179. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  180. 1, 1, 1, 1, 1, 1, 1,
  181. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  182. 1, 1, 1, 1, 1, 1, 1,
  183. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  184. 1, 1, 1, 1, 1, 1, 1,
  185. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  186. 1, 1, 1, 1, 1, 1, 1,
  187. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  188. 1, 1, 1, 1, 1, 1, 1,
  189. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  190. 1, 1, 1, 1, 1, 1, 1,
  191. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  192. 2, 2, 2, 2, 2, 2, 2,
  193. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5,
  194. 5, 5, 5, 6, 6, 1, 1
  195. };
  196. static const gchar *const g_utf8_skip = utf8_skip_data;
  197. /*
  198. * g_utf8_strlen:
  199. * @p: pointer to the start of a UTF-8 encoded string
  200. * @max: the maximum number of bytes to examine. If @max
  201. * is less than 0, then the string is assumed to be
  202. * nul-terminated. If @max is 0, @p will not be examined and
  203. * may be %NULL.
  204. *
  205. * Computes the length of the string in characters, not including
  206. * the terminating nul character.
  207. *
  208. * Return value: the length of the string in characters
  209. **/
  210. static gsize
  211. g_utf8_strlen (const gchar *p)
  212. {
  213. gsize len = 0;
  214. g_return_val_if_fail (p != NULL, 0);
  215. while (*p)
  216. {
  217. p = g_utf8_next_char (p);
  218. ++len;
  219. }
  220. return len;
  221. }
  222. /*
  223. * g_utf8_get_char:
  224. * @p: a pointer to Unicode character encoded as UTF-8
  225. *
  226. * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
  227. * If @p does not point to a valid UTF-8 encoded character, results are
  228. * undefined. If you are not sure that the bytes are complete
  229. * valid Unicode characters, you should use g_utf8_get_char_validated()
  230. * instead.
  231. *
  232. * Return value: the resulting character
  233. **/
  234. static gunichar
  235. g_utf8_get_char (const gchar *p)
  236. {
  237. int i, mask = 0, len;
  238. gunichar result;
  239. unsigned char c = (unsigned char) *p;
  240. UTF8_COMPUTE (c, mask, len);
  241. if (len == -1)
  242. return (gunichar) - 1;
  243. UTF8_GET (result, p, i, mask, len);
  244. return result;
  245. }
  246. /*
  247. * g_unichar_to_utf8:
  248. * @c: a Unicode character code
  249. * @outbuf: output buffer, must have at least 6 bytes of space.
  250. * If %NULL, the length will be computed and returned
  251. * and nothing will be written to @outbuf.
  252. *
  253. * Converts a single character to UTF-8.
  254. *
  255. * Return value: number of bytes written
  256. **/
  257. static int
  258. g_unichar_to_utf8 (gunichar c, gchar *outbuf)
  259. {
  260. /* If this gets modified, also update the copy in g_string_insert_unichar() */
  261. guint len = 0;
  262. int first;
  263. int i;
  264. if (c < 0x80)
  265. {
  266. first = 0;
  267. len = 1;
  268. }
  269. else if (c < 0x800)
  270. {
  271. first = 0xc0;
  272. len = 2;
  273. }
  274. else if (c < 0x10000)
  275. {
  276. first = 0xe0;
  277. len = 3;
  278. }
  279. else if (c < 0x200000)
  280. {
  281. first = 0xf0;
  282. len = 4;
  283. }
  284. else if (c < 0x4000000)
  285. {
  286. first = 0xf8;
  287. len = 5;
  288. }
  289. else
  290. {
  291. first = 0xfc;
  292. len = 6;
  293. }
  294. if (outbuf)
  295. {
  296. for (i = len - 1; i > 0; --i)
  297. {
  298. outbuf[i] = (c & 0x3f) | 0x80;
  299. c >>= 6;
  300. }
  301. outbuf[0] = c | first;
  302. }
  303. return len;
  304. }
  305. /*
  306. * g_utf8_to_ucs4_fast:
  307. * @str: a UTF-8 encoded string
  308. * @len: the maximum length of @str to use, in bytes. If @len < 0,
  309. * then the string is nul-terminated.
  310. * @items_written: location to store the number of characters in the
  311. * result, or %NULL.
  312. *
  313. * Convert a string from UTF-8 to a 32-bit fixed width
  314. * representation as UCS-4, assuming valid UTF-8 input.
  315. * This function is roughly twice as fast as g_utf8_to_ucs4()
  316. * but does no error checking on the input. A trailing 0 character
  317. * will be added to the string after the converted text.
  318. *
  319. * Return value: a pointer to a newly allocated UCS-4 string.
  320. * This value must be freed with g_free().
  321. **/
  322. static gunichar *
  323. g_utf8_to_ucs4_fast (const gchar *str, gssize len, gsize *items_written)
  324. {
  325. gunichar *result;
  326. gsize n_chars, i;
  327. const gchar *p;
  328. g_return_val_if_fail (str != NULL, NULL);
  329. p = str;
  330. n_chars = 0;
  331. if (len < 0)
  332. {
  333. while (*p)
  334. {
  335. p = g_utf8_next_char (p);
  336. ++n_chars;
  337. }
  338. }
  339. else
  340. {
  341. while (p < str + len && *p)
  342. {
  343. p = g_utf8_next_char (p);
  344. ++n_chars;
  345. }
  346. }
  347. result = g_malloc (sizeof (gunichar) * (n_chars + 1));
  348. if (!result)
  349. return NULL;
  350. p = str;
  351. for (i = 0; i < n_chars; i++)
  352. {
  353. gunichar wc = (guchar) * p++;
  354. if (wc < 0x80)
  355. {
  356. result[i] = wc;
  357. }
  358. else
  359. {
  360. gunichar mask = 0x40;
  361. if (G_UNLIKELY ((wc & mask) == 0))
  362. {
  363. /* It's an out-of-sequence 10xxxxxxx byte.
  364. * Rather than making an ugly hash of this and the next byte
  365. * and overrunning the buffer, it's more useful to treat it
  366. * with a replacement character */
  367. result[i] = 0xfffd;
  368. continue;
  369. }
  370. do
  371. {
  372. wc <<= 6;
  373. wc |= (guchar) (*p++) & 0x3f;
  374. mask <<= 5;
  375. }
  376. while ((wc & mask) != 0);
  377. wc &= mask - 1;
  378. result[i] = wc;
  379. }
  380. }
  381. result[i] = 0;
  382. if (items_written)
  383. *items_written = i;
  384. return result;
  385. }
  386. /*
  387. * g_ucs4_to_utf8:
  388. * @str: a UCS-4 encoded string
  389. * @len: the maximum length (number of characters) of @str to use.
  390. * If @len < 0, then the string is nul-terminated.
  391. * @items_read: location to store number of characters read, or %NULL.
  392. * @items_written: location to store number of bytes written or %NULL.
  393. * The value here stored does not include the trailing 0
  394. * byte.
  395. * @error: location to store the error occurring, or %NULL to ignore
  396. * errors. Any of the errors in #GConvertError other than
  397. * %G_CONVERT_ERROR_NO_CONVERSION may occur.
  398. *
  399. * Convert a string from a 32-bit fixed width representation as UCS-4.
  400. * to UTF-8. The result will be terminated with a 0 byte.
  401. *
  402. * Return value: a pointer to a newly allocated UTF-8 string.
  403. * This value must be freed with g_free(). If an
  404. * error occurs, %NULL will be returned and
  405. * @error set. In that case, @items_read will be
  406. * set to the position of the first invalid input
  407. * character.
  408. **/
  409. static gchar *
  410. g_ucs4_to_utf8 (const gunichar *str,
  411. gsize len, gsize *items_read, gsize *items_written)
  412. {
  413. gint result_length;
  414. gchar *result = NULL;
  415. gchar *p;
  416. gsize i;
  417. result_length = 0;
  418. for (i = 0; i < len; i++)
  419. {
  420. if (!str[i])
  421. break;
  422. if (str[i] >= 0x80000000)
  423. goto err_out;
  424. result_length += UTF8_LENGTH (str[i]);
  425. }
  426. result = g_malloc (result_length + 1);
  427. if (!result)
  428. return NULL;
  429. p = result;
  430. i = 0;
  431. while (p < result + result_length)
  432. p += g_unichar_to_utf8 (str[i++], p);
  433. *p = '\0';
  434. if (items_written)
  435. *items_written = p - result;
  436. err_out:
  437. if (items_read)
  438. *items_read = i;
  439. return result;
  440. }
  441. /* Code from GLIB gunidecomp.c starts here. */
  442. /* decomp.c - Character decomposition.
  443. *
  444. * Copyright (C) 1999, 2000 Tom Tromey
  445. * Copyright 2000 Red Hat, Inc.
  446. *
  447. * The Gnome Library is free software; you can redistribute it and/or
  448. * modify it under the terms of the GNU Lesser General Public License as
  449. * published by the Free Software Foundation; either version 2 of the
  450. * License, or (at your option) any later version.
  451. *
  452. * The Gnome Library is distributed in the hope that it will be useful,
  453. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  454. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  455. * Lesser General Public License for more details.
  456. *
  457. * You should have received a copy of the GNU Lesser General Public
  458. * License along with the Gnome Library; see the file COPYING.LIB. If not,
  459. * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  460. * Boston, MA 02111-1307, USA.
  461. */
  462. #include "gunidecomp.h"
  463. #include "gunicomp.h"
  464. #define CC_PART1(Page, Char) \
  465. ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
  466. ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
  467. : (cclass_data[combining_class_table_part1[Page]][Char]))
  468. #define CC_PART2(Page, Char) \
  469. ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
  470. ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
  471. : (cclass_data[combining_class_table_part2[Page]][Char]))
  472. #define COMBINING_CLASS(Char) \
  473. (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
  474. ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
  475. : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
  476. ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
  477. : 0))
  478. /* constants for hangul syllable [de]composition */
  479. #define SBase 0xAC00
  480. #define LBase 0x1100
  481. #define VBase 0x1161
  482. #define TBase 0x11A7
  483. #define LCount 19
  484. #define VCount 21
  485. #define TCount 28
  486. #define NCount (VCount * TCount)
  487. #define SCount (LCount * NCount)
  488. /*
  489. * g_unicode_canonical_ordering:
  490. * @string: a UCS-4 encoded string.
  491. * @len: the maximum length of @string to use.
  492. *
  493. * Computes the canonical ordering of a string in-place.
  494. * This rearranges decomposed characters in the string
  495. * according to their combining classes. See the Unicode
  496. * manual for more information.
  497. **/
  498. static void
  499. g_unicode_canonical_ordering (gunichar *string, gsize len)
  500. {
  501. gsize i;
  502. int swap = 1;
  503. while (swap)
  504. {
  505. int last;
  506. swap = 0;
  507. last = COMBINING_CLASS (string[0]);
  508. for (i = 0; i < len - 1; ++i)
  509. {
  510. int next = COMBINING_CLASS (string[i + 1]);
  511. if (next != 0 && last > next)
  512. {
  513. gsize j;
  514. /* Percolate item leftward through string. */
  515. for (j = i + 1; j > 0; --j)
  516. {
  517. gunichar t;
  518. if (COMBINING_CLASS (string[j - 1]) <= next)
  519. break;
  520. t = string[j];
  521. string[j] = string[j - 1];
  522. string[j - 1] = t;
  523. swap = 1;
  524. }
  525. /* We're re-entering the loop looking at the old
  526. character again. */
  527. next = last;
  528. }
  529. last = next;
  530. }
  531. }
  532. }
  533. /* http://www.unicode.org/unicode/reports/tr15/#Hangul
  534. * r should be null or have sufficient space. Calling with r == NULL will
  535. * only calculate the result_len; however, a buffer with space for three
  536. * characters will always be big enough. */
  537. static void
  538. decompose_hangul (gunichar s, gunichar *r, gsize *result_len)
  539. {
  540. gint SIndex = s - SBase;
  541. gint TIndex = SIndex % TCount;
  542. if (r)
  543. {
  544. r[0] = LBase + SIndex / NCount;
  545. r[1] = VBase + (SIndex % NCount) / TCount;
  546. }
  547. if (TIndex)
  548. {
  549. if (r)
  550. r[2] = TBase + TIndex;
  551. *result_len = 3;
  552. }
  553. else
  554. *result_len = 2;
  555. }
  556. /* returns a pointer to a null-terminated UTF-8 string */
  557. static const gchar *
  558. find_decomposition (gunichar ch, gboolean compat)
  559. {
  560. int start = 0;
  561. int end = G_N_ELEMENTS (decomp_table);
  562. if (ch >= decomp_table[start].ch && ch <= decomp_table[end - 1].ch)
  563. {
  564. while (TRUE)
  565. {
  566. int half = (start + end) / 2;
  567. if (ch == decomp_table[half].ch)
  568. {
  569. int offset;
  570. if (compat)
  571. {
  572. offset = decomp_table[half].compat_offset;
  573. if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
  574. offset = decomp_table[half].canon_offset;
  575. }
  576. else
  577. {
  578. offset = decomp_table[half].canon_offset;
  579. if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
  580. return NULL;
  581. }
  582. return &(decomp_expansion_string[offset]);
  583. }
  584. else if (half == start)
  585. break;
  586. else if (ch > decomp_table[half].ch)
  587. start = half;
  588. else
  589. end = half;
  590. }
  591. }
  592. return NULL;
  593. }
  594. /* L,V => LV and LV,T => LVT */
  595. static gboolean
  596. combine_hangul (gunichar a, gunichar b, gunichar *result)
  597. {
  598. if (a >= LBase && a < LCount + LBase && b >= VBase && b < VCount + VBase)
  599. {
  600. gint LIndex = a - LBase;
  601. gint VIndex = b - VBase;
  602. *result = SBase + (LIndex * VCount + VIndex) * TCount;
  603. return TRUE;
  604. }
  605. if (a >= SBase && a < SCount + SBase && b > TBase && b < TCount + TBase)
  606. {
  607. gint SIndex = a - SBase;
  608. if ((SIndex % TCount) == 0)
  609. {
  610. gint TIndex = b - TBase;
  611. *result = a + TIndex;
  612. return TRUE;
  613. }
  614. }
  615. return FALSE;
  616. }
  617. #define CI(Page, Char) \
  618. ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
  619. ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
  620. : (compose_data[compose_table[Page]][Char]))
  621. #define COMPOSE_INDEX(Char) \
  622. (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
  623. static gboolean
  624. combine (gunichar a, gunichar b, gunichar *result)
  625. {
  626. gushort index_a, index_b;
  627. if (combine_hangul (a, b, result))
  628. return TRUE;
  629. index_a = COMPOSE_INDEX (a);
  630. if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
  631. {
  632. if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
  633. {
  634. *result =
  635. compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
  636. return TRUE;
  637. }
  638. else
  639. return FALSE;
  640. }
  641. index_b = COMPOSE_INDEX (b);
  642. if (index_b >= COMPOSE_SECOND_SINGLE_START)
  643. {
  644. if (a ==
  645. compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
  646. {
  647. *result =
  648. compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
  649. return TRUE;
  650. }
  651. else
  652. return FALSE;
  653. }
  654. if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START
  655. && index_b >= COMPOSE_SECOND_START
  656. && index_b < COMPOSE_SECOND_SINGLE_START)
  657. {
  658. gunichar res =
  659. compose_array[index_a - COMPOSE_FIRST_START][index_b -
  660. COMPOSE_SECOND_START];
  661. if (res)
  662. {
  663. *result = res;
  664. return TRUE;
  665. }
  666. }
  667. return FALSE;
  668. }
  669. static gunichar *
  670. _g_utf8_normalize_wc (const gchar *str, gssize max_len, GNormalizeMode mode)
  671. {
  672. gsize n_wc;
  673. gunichar *wc_buffer;
  674. const char *p;
  675. gsize last_start;
  676. gboolean do_compat = (mode == G_NORMALIZE_NFKC || mode == G_NORMALIZE_NFKD);
  677. gboolean do_compose = (mode == G_NORMALIZE_NFC || mode == G_NORMALIZE_NFKC);
  678. n_wc = 0;
  679. p = str;
  680. while ((max_len < 0 || p < str + max_len) && *p)
  681. {
  682. const gchar *decomp;
  683. gunichar wc = g_utf8_get_char (p);
  684. if (wc >= SBase && wc < SBase + SCount)
  685. {
  686. gsize result_len;
  687. decompose_hangul (wc, NULL, &result_len);
  688. n_wc += result_len;
  689. }
  690. else
  691. {
  692. decomp = find_decomposition (wc, do_compat);
  693. if (decomp)
  694. n_wc += g_utf8_strlen (decomp);
  695. else
  696. n_wc++;
  697. }
  698. p = g_utf8_next_char (p);
  699. }
  700. wc_buffer = g_malloc (sizeof (gunichar) * (n_wc + 1));
  701. if (!wc_buffer)
  702. return NULL;
  703. last_start = 0;
  704. n_wc = 0;
  705. p = str;
  706. while ((max_len < 0 || p < str + max_len) && *p)
  707. {
  708. gunichar wc = g_utf8_get_char (p);
  709. const gchar *decomp;
  710. int cc;
  711. gsize old_n_wc = n_wc;
  712. if (wc >= SBase && wc < SBase + SCount)
  713. {
  714. gsize result_len;
  715. decompose_hangul (wc, wc_buffer + n_wc, &result_len);
  716. n_wc += result_len;
  717. }
  718. else
  719. {
  720. decomp = find_decomposition (wc, do_compat);
  721. if (decomp)
  722. {
  723. const char *pd;
  724. for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
  725. wc_buffer[n_wc++] = g_utf8_get_char (pd);
  726. }
  727. else
  728. wc_buffer[n_wc++] = wc;
  729. }
  730. if (n_wc > 0)
  731. {
  732. cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
  733. if (cc == 0)
  734. {
  735. g_unicode_canonical_ordering (wc_buffer + last_start,
  736. n_wc - last_start);
  737. last_start = old_n_wc;
  738. }
  739. }
  740. p = g_utf8_next_char (p);
  741. }
  742. if (n_wc > 0)
  743. {
  744. g_unicode_canonical_ordering (wc_buffer + last_start,
  745. n_wc - last_start);
  746. /* dead assignment: last_start = n_wc; */
  747. }
  748. wc_buffer[n_wc] = 0;
  749. /* All decomposed and reordered */
  750. if (do_compose && n_wc > 0)
  751. {
  752. gsize i, j;
  753. int last_cc = 0;
  754. last_start = 0;
  755. for (i = 0; i < n_wc; i++)
  756. {
  757. int cc = COMBINING_CLASS (wc_buffer[i]);
  758. if (i > 0 &&
  759. (last_cc == 0 || last_cc != cc) &&
  760. combine (wc_buffer[last_start], wc_buffer[i],
  761. &wc_buffer[last_start]))
  762. {
  763. for (j = i + 1; j < n_wc; j++)
  764. wc_buffer[j - 1] = wc_buffer[j];
  765. n_wc--;
  766. i--;
  767. if (i == last_start)
  768. last_cc = 0;
  769. else
  770. last_cc = COMBINING_CLASS (wc_buffer[i - 1]);
  771. continue;
  772. }
  773. if (cc == 0)
  774. last_start = i;
  775. last_cc = cc;
  776. }
  777. }
  778. wc_buffer[n_wc] = 0;
  779. return wc_buffer;
  780. }
  781. /*
  782. * g_utf8_normalize:
  783. * @str: a UTF-8 encoded string.
  784. * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
  785. * @mode: the type of normalization to perform.
  786. *
  787. * Converts a string into canonical form, standardizing
  788. * such issues as whether a character with an accent
  789. * is represented as a base character and combining
  790. * accent or as a single precomposed character. The
  791. * string has to be valid UTF-8, otherwise %NULL is
  792. * returned. You should generally call g_utf8_normalize()
  793. * before comparing two Unicode strings.
  794. *
  795. * The normalization mode %G_NORMALIZE_DEFAULT only
  796. * standardizes differences that do not affect the
  797. * text content, such as the above-mentioned accent
  798. * representation. %G_NORMALIZE_ALL also standardizes
  799. * the "compatibility" characters in Unicode, such
  800. * as SUPERSCRIPT THREE to the standard forms
  801. * (in this case DIGIT THREE). Formatting information
  802. * may be lost but for most text operations such
  803. * characters should be considered the same.
  804. *
  805. * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
  806. * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
  807. * but returned a result with composed forms rather
  808. * than a maximally decomposed form. This is often
  809. * useful if you intend to convert the string to
  810. * a legacy encoding or pass it to a system with
  811. * less capable Unicode handling.
  812. *
  813. * Return value: a newly allocated string, that is the
  814. * normalized form of @str, or %NULL if @str is not
  815. * valid UTF-8.
  816. **/
  817. static gchar *
  818. g_utf8_normalize (const gchar *str, gssize len, GNormalizeMode mode)
  819. {
  820. gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
  821. gchar *result = NULL;
  822. if (result_wc)
  823. result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL);
  824. g_free (result_wc);
  825. return result;
  826. }
  827. /* Public Libidn API starts here. */
  828. /**
  829. * stringprep_utf8_to_unichar:
  830. * @p: a pointer to Unicode character encoded as UTF-8
  831. *
  832. * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
  833. * If @p does not point to a valid UTF-8 encoded character, results are
  834. * undefined.
  835. *
  836. * Return value: the resulting character.
  837. **/
  838. uint32_t
  839. stringprep_utf8_to_unichar (const char *p)
  840. {
  841. return g_utf8_get_char (p);
  842. }
  843. /**
  844. * stringprep_unichar_to_utf8:
  845. * @c: a ISO10646 character code
  846. * @outbuf: output buffer, must have at least 6 bytes of space.
  847. * If %NULL, the length will be computed and returned
  848. * and nothing will be written to @outbuf.
  849. *
  850. * Converts a single character to UTF-8.
  851. *
  852. * Return value: number of bytes written.
  853. **/
  854. int
  855. stringprep_unichar_to_utf8 (uint32_t c, char *outbuf)
  856. {
  857. return g_unichar_to_utf8 (c, outbuf);
  858. }
  859. #include <unistr.h>
  860. /**
  861. * stringprep_utf8_to_ucs4:
  862. * @str: a UTF-8 encoded string
  863. * @len: the maximum length of @str to use. If @len < 0, then
  864. * the string is nul-terminated.
  865. * @items_written: location to store the number of characters in the
  866. * result, or %NULL.
  867. *
  868. * Convert a string from UTF-8 to a 32-bit fixed width representation
  869. * as UCS-4. The function now performs error checking to verify that
  870. * the input is valid UTF-8 (before it was documented to not do error
  871. * checking).
  872. *
  873. * Return value: a pointer to a newly allocated UCS-4 string.
  874. * This value must be deallocated by the caller.
  875. **/
  876. uint32_t *
  877. stringprep_utf8_to_ucs4 (const char *str, ssize_t len, size_t *items_written)
  878. {
  879. size_t n;
  880. if (len < 0)
  881. n = strlen (str);
  882. else
  883. n = len;
  884. if (u8_check ((const uint8_t *) str, n))
  885. return NULL;
  886. return g_utf8_to_ucs4_fast (str, len, items_written);
  887. }
  888. /**
  889. * stringprep_ucs4_to_utf8:
  890. * @str: a UCS-4 encoded string
  891. * @len: the maximum length of @str to use. If @len < 0, then
  892. * the string is terminated with a 0 character.
  893. * @items_read: location to store number of characters read read, or %NULL.
  894. * @items_written: location to store number of bytes written or %NULL.
  895. * The value here stored does not include the trailing 0
  896. * byte.
  897. *
  898. * Convert a string from a 32-bit fixed width representation as UCS-4.
  899. * to UTF-8. The result will be terminated with a 0 byte.
  900. *
  901. * Return value: a pointer to a newly allocated UTF-8 string.
  902. * This value must be deallocated by the caller.
  903. * If an error occurs, %NULL will be returned.
  904. **/
  905. char *
  906. stringprep_ucs4_to_utf8 (const uint32_t *str, ssize_t len,
  907. size_t *items_read, size_t *items_written)
  908. {
  909. return g_ucs4_to_utf8 (str, len, items_read, items_written);
  910. }
  911. /**
  912. * stringprep_utf8_nfkc_normalize:
  913. * @str: a UTF-8 encoded string.
  914. * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
  915. *
  916. * Converts a string into canonical form, standardizing
  917. * such issues as whether a character with an accent
  918. * is represented as a base character and combining
  919. * accent or as a single precomposed character.
  920. *
  921. * The normalization mode is NFKC (ALL COMPOSE). It standardizes
  922. * differences that do not affect the text content, such as the
  923. * above-mentioned accent representation. It standardizes the
  924. * "compatibility" characters in Unicode, such as SUPERSCRIPT THREE to
  925. * the standard forms (in this case DIGIT THREE). Formatting
  926. * information may be lost but for most text operations such
  927. * characters should be considered the same. It returns a result with
  928. * composed forms rather than a maximally decomposed form.
  929. *
  930. * Return value: a newly allocated string, that is the
  931. * NFKC normalized form of @str.
  932. **/
  933. char *
  934. stringprep_utf8_nfkc_normalize (const char *str, ssize_t len)
  935. {
  936. size_t n;
  937. if (len < 0)
  938. n = strlen (str);
  939. else
  940. n = len;
  941. if (u8_check ((const uint8_t *) str, n))
  942. return NULL;
  943. return g_utf8_normalize (str, len, G_NORMALIZE_NFKC);
  944. }
  945. #include <stdio.h>
  946. /**
  947. * stringprep_ucs4_nfkc_normalize:
  948. * @str: a Unicode string.
  949. * @len: length of @str array, or -1 if @str is nul-terminated.
  950. *
  951. * Converts a UCS4 string into canonical form, see
  952. * stringprep_utf8_nfkc_normalize() for more information.
  953. *
  954. * Return value: a newly allocated Unicode string, that is the NFKC
  955. * normalized form of @str.
  956. **/
  957. uint32_t *
  958. stringprep_ucs4_nfkc_normalize (const uint32_t *str, ssize_t len)
  959. {
  960. char *p;
  961. uint32_t *result_wc;
  962. p = stringprep_ucs4_to_utf8 (str, len, 0, 0);
  963. if (!p)
  964. return NULL;
  965. result_wc = _g_utf8_normalize_wc (p, -1, G_NORMALIZE_NFKC);
  966. free (p);
  967. return result_wc;
  968. }