Coverage Report

Created: 2026-06-08 06:33

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