nfkc.c 26 KB

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