Coverage Report

Created: 2026-12-01 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/uninorm/u-normalize-internal.h
Line
Count
Source
1
/* Decomposition and composition of Unicode strings.
2
   Copyright (C) 2009-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5
   This file is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as
7
   published by the Free Software Foundation; either version 2.1 of the
8
   License, or (at your option) any later version.
9
10
   This file is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17
18
UNIT *
19
FUNC (uninorm_t nf, const UNIT *s, size_t n,
20
      UNIT *resultbuf, size_t *lengthp)
21
4.31M
{
22
4.31M
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
4.31M
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
4.31M
  UNIT *result;
27
4.31M
  size_t allocated;
28
4.31M
  if (resultbuf == NULL)
29
4.31M
    {
30
4.31M
      result = NULL;
31
4.31M
      allocated = 0;
32
4.31M
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
4.31M
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
4.31M
  #define SORTBUF_PREALLOCATED 64
42
4.31M
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
4.31M
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
4.31M
    sortbuf_preallocated;
45
4.31M
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
4.31M
  size_t sortbuf_count = 0;
47
48
4.31M
  {
49
4.31M
    const UNIT *s_end = s + n;
50
51
4.31M
    for (;;)
52
28.1M
      {
53
28.1M
        int count;
54
28.1M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
28.1M
        int decomposed_count;
56
57
28.1M
        if (s < s_end)
58
23.8M
          {
59
            /* Fetch the next character.  */
60
23.8M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
23.8M
            decomposed_count = 1;
62
63
            /* Decompose it, recursively.
64
               It would be possible to precompute the recursive decomposition
65
               and store it in a table.  But this would significantly increase
66
               the size of the decomposition tables, because for example for
67
               U+1FC1 the recursive canonical decomposition and the recursive
68
               compatibility decomposition are different.  */
69
115M
            for (int curr = 0; curr < decomposed_count; )
70
91.6M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
91.6M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
91.6M
                int curr_decomposed_count;
75
76
91.6M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
91.6M
                if (curr_decomposed_count >= 0)
78
8.35M
                  {
79
                    /* Move curr_decomposed[0..curr_decomposed_count-1] over
80
                       decomposed[curr], making room.  It's not worth using
81
                       memcpy() here, since the counts are so small.  */
82
8.35M
                    int shift = curr_decomposed_count - 1;
83
84
8.35M
                    if (shift < 0)
85
0
                      abort ();
86
8.35M
                    if (shift > 0)
87
7.64M
                      {
88
7.64M
                        decomposed_count += shift;
89
7.64M
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
7.69M
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
51.7k
                          decomposed[j + shift] = decomposed[j];
93
7.64M
                      }
94
76.1M
                    for (; shift >= 0; shift--)
95
67.7M
                      decomposed[curr + shift] = curr_decomposed[shift];
96
8.35M
                  }
97
83.2M
                else
98
83.2M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
83.2M
                    curr++;
101
83.2M
                  }
102
91.6M
              }
103
23.8M
          }
104
4.31M
        else
105
4.31M
          {
106
4.31M
            count = 0;
107
4.31M
            decomposed_count = 0;
108
4.31M
          }
109
110
28.1M
        int i = 0;
111
28.1M
        for (;;)
112
111M
          {
113
111M
            ucs4_t uc;
114
111M
            int ccc;
115
116
111M
            if (s < s_end)
117
107M
              {
118
                /* Fetch the next character from the decomposition.  */
119
107M
                if (i == decomposed_count)
120
23.8M
                  break;
121
83.2M
                uc = decomposed[i];
122
83.2M
                ccc = uc_combining_class (uc);
123
83.2M
              }
124
4.31M
            else
125
4.31M
              {
126
                /* End of string reached.  */
127
4.31M
                uc = 0;
128
4.31M
                ccc = 0;
129
4.31M
              }
130
131
87.5M
            if (ccc == 0)
132
80.5M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
80.5M
                if (sortbuf_count > 1)
136
1.81M
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
1.81M
                                                           sortbuf + sortbuf_count);
138
139
80.5M
                if (composer != NULL)
140
80.5M
                  {
141
                    /* Attempt to combine decomposed characters, as specified
142
                       in the Unicode Standard Annex #15 "Unicode Normalization
143
                       Forms".  We need to check
144
                         1. whether the first accumulated character is a
145
                            "starter" (i.e. has ccc = 0).  This is usually the
146
                            case.  But when the string starts with a
147
                            non-starter, the sortbuf also starts with a
148
                            non-starter.  Btw, this check could also be
149
                            omitted, because the composition table has only
150
                            entries (code1, code2) for which code1 is a
151
                            starter; if the first accumulated character is not
152
                            a starter, no lookup will succeed.
153
                         2. If the sortbuf has more than one character, check
154
                            for each of these characters that are not "blocked"
155
                            from the starter (i.e. have a ccc that is higher
156
                            than the ccc of the previous character) whether it
157
                            can be combined with the first character.
158
                         3. If only one character is left in sortbuf, check
159
                            whether it can be combined with the next character
160
                            (also a starter).  */
161
80.5M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
76.2M
                      {
163
83.2M
                        for (size_t j = 1; j < sortbuf_count; )
164
6.97M
                          {
165
6.97M
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
1.90M
                              {
167
1.90M
                                ucs4_t combined =
168
1.90M
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
1.90M
                                if (combined)
170
1.77M
                                  {
171
1.77M
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
4.51M
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
2.73M
                                      sortbuf[k - 1] = sortbuf[k];
175
1.77M
                                    sortbuf_count--;
176
1.77M
                                    continue;
177
1.77M
                                  }
178
1.90M
                              }
179
5.19M
                            j++;
180
5.19M
                          }
181
76.2M
                        if (s < s_end && sortbuf_count == 1)
182
71.8M
                          {
183
71.8M
                            ucs4_t combined =
184
71.8M
                              composer (sortbuf[0].code, uc);
185
71.8M
                            if (combined)
186
26.4k
                              {
187
26.4k
                                uc = combined;
188
26.4k
                                ccc = 0;
189
                                /* uc could be further combined with subsequent
190
                                   characters.  So don't put it into sortbuf[0] in
191
                                   this round, only in the next round.  */
192
26.4k
                                sortbuf_count = 0;
193
26.4k
                              }
194
71.8M
                          }
195
76.2M
                      }
196
80.5M
                  }
197
198
162M
                for (size_t j = 0; j < sortbuf_count; j++)
199
81.4M
                  {
200
81.4M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
81.4M
                    if (length < allocated)
204
77.1M
                      {
205
77.1M
                        int ret =
206
77.1M
                          U_UCTOMB (result + length, muc, allocated - length);
207
77.1M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
77.1M
                        if (ret >= 0)
213
77.1M
                          {
214
77.1M
                            length += ret;
215
77.1M
                            goto done_appending;
216
77.1M
                          }
217
77.1M
                      }
218
4.33M
                    {
219
4.33M
                      size_t old_allocated = allocated;
220
4.33M
                      size_t new_allocated = 2 * old_allocated;
221
4.33M
                      if (new_allocated < 64)
222
4.29M
                        new_allocated = 64;
223
4.33M
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
4.33M
                      {
226
4.33M
                        UNIT *larger_result;
227
4.33M
                        if (result == NULL)
228
4.29M
                          {
229
4.29M
                            larger_result =
230
4.29M
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
4.29M
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
4.29M
                          }
237
33.9k
                        else if (result == resultbuf)
238
0
                          {
239
0
                            larger_result =
240
0
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
241
0
                            if (larger_result == NULL)
242
0
                              {
243
0
                                errno = ENOMEM;
244
0
                                goto fail;
245
0
                              }
246
0
                            U_CPY (larger_result, resultbuf, length);
247
0
                          }
248
33.9k
                        else
249
33.9k
                          {
250
33.9k
                            larger_result =
251
33.9k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
33.9k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
33.9k
                          }
258
4.33M
                        result = larger_result;
259
4.33M
                        allocated = new_allocated;
260
4.33M
                        {
261
4.33M
                          int ret =
262
4.33M
                            U_UCTOMB (result + length, muc, allocated - length);
263
4.33M
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
4.33M
                          if (ret < 0)
269
0
                            abort ();
270
4.33M
                          length += ret;
271
4.33M
                          goto done_appending;
272
4.33M
                        }
273
4.33M
                      }
274
4.33M
                    }
275
81.4M
                   done_appending: ;
276
81.4M
                  }
277
278
                /* sortbuf is now empty.  */
279
80.5M
                sortbuf_count = 0;
280
80.5M
              }
281
282
87.5M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
4.31M
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
83.2M
            if (sortbuf_count == sortbuf_allocated)
288
4.24k
              {
289
4.24k
                sortbuf_allocated = 2 * sortbuf_allocated;
290
4.24k
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
291
0
                  abort ();
292
4.24k
                struct ucs4_with_ccc *new_sortbuf =
293
4.24k
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
294
4.24k
                if (new_sortbuf == NULL)
295
0
                  {
296
0
                    errno = ENOMEM;
297
0
                    goto fail;
298
0
                  }
299
4.24k
                memcpy (new_sortbuf, sortbuf,
300
4.24k
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
301
4.24k
                if (sortbuf != sortbuf_preallocated)
302
2.14k
                  free (sortbuf);
303
4.24k
                sortbuf = new_sortbuf;
304
4.24k
              }
305
83.2M
            sortbuf[sortbuf_count].code = uc;
306
83.2M
            sortbuf[sortbuf_count].ccc = ccc;
307
83.2M
            sortbuf_count++;
308
309
83.2M
            i++;
310
83.2M
          }
311
312
28.1M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
4.31M
          break;
315
316
23.8M
        s += count;
317
23.8M
      }
318
4.31M
  }
319
320
4.31M
  if (length == 0)
321
21.9k
    {
322
21.9k
      if (result == NULL)
323
21.9k
        {
324
          /* Return a non-NULL value.  NULL means error.  */
325
21.9k
          result = (UNIT *) malloc (1);
326
21.9k
          if (result == NULL)
327
0
            {
328
0
              errno = ENOMEM;
329
0
              goto fail;
330
0
            }
331
21.9k
        }
332
21.9k
    }
333
4.29M
  else if (result != resultbuf && length < allocated)
334
4.29M
    {
335
      /* Shrink the allocated memory if possible.  */
336
4.29M
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
4.29M
      if (memory != NULL)
338
4.29M
        result = memory;
339
4.29M
    }
340
341
4.31M
  if (sortbuf_count > 0)
342
0
    abort ();
343
4.31M
  if (sortbuf != sortbuf_preallocated)
344
2.09k
    free (sortbuf);
345
346
4.31M
  *lengthp = length;
347
4.31M
  return result;
348
349
0
 fail:
350
0
  {
351
0
    int saved_errno = errno;
352
0
    if (sortbuf != sortbuf_preallocated)
353
0
      free (sortbuf);
354
0
    if (result != resultbuf)
355
0
      free (result);
356
0
    errno = saved_errno;
357
0
  }
358
  return NULL;
359
4.31M
}
u8_normalize
Line
Count
Source
21
2.10M
{
22
2.10M
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
2.10M
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
2.10M
  UNIT *result;
27
2.10M
  size_t allocated;
28
2.10M
  if (resultbuf == NULL)
29
2.10M
    {
30
2.10M
      result = NULL;
31
2.10M
      allocated = 0;
32
2.10M
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
2.10M
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
2.10M
  #define SORTBUF_PREALLOCATED 64
42
2.10M
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
2.10M
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
2.10M
    sortbuf_preallocated;
45
2.10M
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
2.10M
  size_t sortbuf_count = 0;
47
48
2.10M
  {
49
2.10M
    const UNIT *s_end = s + n;
50
51
2.10M
    for (;;)
52
22.7M
      {
53
22.7M
        int count;
54
22.7M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
22.7M
        int decomposed_count;
56
57
22.7M
        if (s < s_end)
58
20.5M
          {
59
            /* Fetch the next character.  */
60
20.5M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
20.5M
            decomposed_count = 1;
62
63
            /* Decompose it, recursively.
64
               It would be possible to precompute the recursive decomposition
65
               and store it in a table.  But this would significantly increase
66
               the size of the decomposition tables, because for example for
67
               U+1FC1 the recursive canonical decomposition and the recursive
68
               compatibility decomposition are different.  */
69
108M
            for (int curr = 0; curr < decomposed_count; )
70
88.1M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
88.1M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
88.1M
                int curr_decomposed_count;
75
76
88.1M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
88.1M
                if (curr_decomposed_count >= 0)
78
8.26M
                  {
79
                    /* Move curr_decomposed[0..curr_decomposed_count-1] over
80
                       decomposed[curr], making room.  It's not worth using
81
                       memcpy() here, since the counts are so small.  */
82
8.26M
                    int shift = curr_decomposed_count - 1;
83
84
8.26M
                    if (shift < 0)
85
0
                      abort ();
86
8.26M
                    if (shift > 0)
87
7.56M
                      {
88
7.56M
                        decomposed_count += shift;
89
7.56M
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
7.60M
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
36.0k
                          decomposed[j + shift] = decomposed[j];
93
7.56M
                      }
94
75.8M
                    for (; shift >= 0; shift--)
95
67.6M
                      decomposed[curr + shift] = curr_decomposed[shift];
96
8.26M
                  }
97
79.9M
                else
98
79.9M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
79.9M
                    curr++;
101
79.9M
                  }
102
88.1M
              }
103
20.5M
          }
104
2.10M
        else
105
2.10M
          {
106
2.10M
            count = 0;
107
2.10M
            decomposed_count = 0;
108
2.10M
          }
109
110
22.7M
        int i = 0;
111
22.7M
        for (;;)
112
102M
          {
113
102M
            ucs4_t uc;
114
102M
            int ccc;
115
116
102M
            if (s < s_end)
117
100M
              {
118
                /* Fetch the next character from the decomposition.  */
119
100M
                if (i == decomposed_count)
120
20.5M
                  break;
121
79.9M
                uc = decomposed[i];
122
79.9M
                ccc = uc_combining_class (uc);
123
79.9M
              }
124
2.10M
            else
125
2.10M
              {
126
                /* End of string reached.  */
127
2.10M
                uc = 0;
128
2.10M
                ccc = 0;
129
2.10M
              }
130
131
82.0M
            if (ccc == 0)
132
75.2M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
75.2M
                if (sortbuf_count > 1)
136
1.74M
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
1.74M
                                                           sortbuf + sortbuf_count);
138
139
75.2M
                if (composer != NULL)
140
75.2M
                  {
141
                    /* Attempt to combine decomposed characters, as specified
142
                       in the Unicode Standard Annex #15 "Unicode Normalization
143
                       Forms".  We need to check
144
                         1. whether the first accumulated character is a
145
                            "starter" (i.e. has ccc = 0).  This is usually the
146
                            case.  But when the string starts with a
147
                            non-starter, the sortbuf also starts with a
148
                            non-starter.  Btw, this check could also be
149
                            omitted, because the composition table has only
150
                            entries (code1, code2) for which code1 is a
151
                            starter; if the first accumulated character is not
152
                            a starter, no lookup will succeed.
153
                         2. If the sortbuf has more than one character, check
154
                            for each of these characters that are not "blocked"
155
                            from the starter (i.e. have a ccc that is higher
156
                            than the ccc of the previous character) whether it
157
                            can be combined with the first character.
158
                         3. If only one character is left in sortbuf, check
159
                            whether it can be combined with the next character
160
                            (also a starter).  */
161
75.2M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
73.1M
                      {
163
79.8M
                        for (size_t j = 1; j < sortbuf_count; )
164
6.77M
                          {
165
6.77M
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
1.81M
                              {
167
1.81M
                                ucs4_t combined =
168
1.81M
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
1.81M
                                if (combined)
170
1.71M
                                  {
171
1.71M
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
4.40M
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
2.68M
                                      sortbuf[k - 1] = sortbuf[k];
175
1.71M
                                    sortbuf_count--;
176
1.71M
                                    continue;
177
1.71M
                                  }
178
1.81M
                              }
179
5.05M
                            j++;
180
5.05M
                          }
181
73.1M
                        if (s < s_end && sortbuf_count == 1)
182
70.9M
                          {
183
70.9M
                            ucs4_t combined =
184
70.9M
                              composer (sortbuf[0].code, uc);
185
70.9M
                            if (combined)
186
9.99k
                              {
187
9.99k
                                uc = combined;
188
9.99k
                                ccc = 0;
189
                                /* uc could be further combined with subsequent
190
                                   characters.  So don't put it into sortbuf[0] in
191
                                   this round, only in the next round.  */
192
9.99k
                                sortbuf_count = 0;
193
9.99k
                              }
194
70.9M
                          }
195
73.1M
                      }
196
75.2M
                  }
197
198
153M
                for (size_t j = 0; j < sortbuf_count; j++)
199
78.2M
                  {
200
78.2M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
78.2M
                    if (length < allocated)
204
76.0M
                      {
205
76.0M
                        int ret =
206
76.0M
                          U_UCTOMB (result + length, muc, allocated - length);
207
76.0M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
76.0M
                        if (ret >= 0)
213
76.0M
                          {
214
76.0M
                            length += ret;
215
76.0M
                            goto done_appending;
216
76.0M
                          }
217
76.0M
                      }
218
2.13M
                    {
219
2.13M
                      size_t old_allocated = allocated;
220
2.13M
                      size_t new_allocated = 2 * old_allocated;
221
2.13M
                      if (new_allocated < 64)
222
2.10M
                        new_allocated = 64;
223
2.13M
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
2.13M
                      {
226
2.13M
                        UNIT *larger_result;
227
2.13M
                        if (result == NULL)
228
2.10M
                          {
229
2.10M
                            larger_result =
230
2.10M
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
2.10M
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
2.10M
                          }
237
29.5k
                        else if (result == resultbuf)
238
0
                          {
239
0
                            larger_result =
240
0
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
241
0
                            if (larger_result == NULL)
242
0
                              {
243
0
                                errno = ENOMEM;
244
0
                                goto fail;
245
0
                              }
246
0
                            U_CPY (larger_result, resultbuf, length);
247
0
                          }
248
29.5k
                        else
249
29.5k
                          {
250
29.5k
                            larger_result =
251
29.5k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
29.5k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
29.5k
                          }
258
2.13M
                        result = larger_result;
259
2.13M
                        allocated = new_allocated;
260
2.13M
                        {
261
2.13M
                          int ret =
262
2.13M
                            U_UCTOMB (result + length, muc, allocated - length);
263
2.13M
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
2.13M
                          if (ret < 0)
269
0
                            abort ();
270
2.13M
                          length += ret;
271
2.13M
                          goto done_appending;
272
2.13M
                        }
273
2.13M
                      }
274
2.13M
                    }
275
78.2M
                   done_appending: ;
276
78.2M
                  }
277
278
                /* sortbuf is now empty.  */
279
75.2M
                sortbuf_count = 0;
280
75.2M
              }
281
282
82.0M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
2.10M
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
79.9M
            if (sortbuf_count == sortbuf_allocated)
288
2.84k
              {
289
2.84k
                sortbuf_allocated = 2 * sortbuf_allocated;
290
2.84k
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
291
0
                  abort ();
292
2.84k
                struct ucs4_with_ccc *new_sortbuf =
293
2.84k
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
294
2.84k
                if (new_sortbuf == NULL)
295
0
                  {
296
0
                    errno = ENOMEM;
297
0
                    goto fail;
298
0
                  }
299
2.84k
                memcpy (new_sortbuf, sortbuf,
300
2.84k
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
301
2.84k
                if (sortbuf != sortbuf_preallocated)
302
1.96k
                  free (sortbuf);
303
2.84k
                sortbuf = new_sortbuf;
304
2.84k
              }
305
79.9M
            sortbuf[sortbuf_count].code = uc;
306
79.9M
            sortbuf[sortbuf_count].ccc = ccc;
307
79.9M
            sortbuf_count++;
308
309
79.9M
            i++;
310
79.9M
          }
311
312
22.7M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
2.10M
          break;
315
316
20.5M
        s += count;
317
20.5M
      }
318
2.10M
  }
319
320
2.10M
  if (length == 0)
321
0
    {
322
0
      if (result == NULL)
323
0
        {
324
          /* Return a non-NULL value.  NULL means error.  */
325
0
          result = (UNIT *) malloc (1);
326
0
          if (result == NULL)
327
0
            {
328
0
              errno = ENOMEM;
329
0
              goto fail;
330
0
            }
331
0
        }
332
0
    }
333
2.10M
  else if (result != resultbuf && length < allocated)
334
2.10M
    {
335
      /* Shrink the allocated memory if possible.  */
336
2.10M
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
2.10M
      if (memory != NULL)
338
2.10M
        result = memory;
339
2.10M
    }
340
341
2.10M
  if (sortbuf_count > 0)
342
0
    abort ();
343
2.10M
  if (sortbuf != sortbuf_preallocated)
344
880
    free (sortbuf);
345
346
2.10M
  *lengthp = length;
347
2.10M
  return result;
348
349
0
 fail:
350
0
  {
351
0
    int saved_errno = errno;
352
0
    if (sortbuf != sortbuf_preallocated)
353
0
      free (sortbuf);
354
0
    if (result != resultbuf)
355
0
      free (result);
356
0
    errno = saved_errno;
357
0
  }
358
  return NULL;
359
2.10M
}
u32_normalize
Line
Count
Source
21
2.20M
{
22
2.20M
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
2.20M
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
2.20M
  UNIT *result;
27
2.20M
  size_t allocated;
28
2.20M
  if (resultbuf == NULL)
29
2.20M
    {
30
2.20M
      result = NULL;
31
2.20M
      allocated = 0;
32
2.20M
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
2.20M
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
2.20M
  #define SORTBUF_PREALLOCATED 64
42
2.20M
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
2.20M
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
2.20M
    sortbuf_preallocated;
45
2.20M
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
2.20M
  size_t sortbuf_count = 0;
47
48
2.20M
  {
49
2.20M
    const UNIT *s_end = s + n;
50
51
2.20M
    for (;;)
52
5.47M
      {
53
5.47M
        int count;
54
5.47M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
5.47M
        int decomposed_count;
56
57
5.47M
        if (s < s_end)
58
3.26M
          {
59
            /* Fetch the next character.  */
60
3.26M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
3.26M
            decomposed_count = 1;
62
63
            /* Decompose it, recursively.
64
               It would be possible to precompute the recursive decomposition
65
               and store it in a table.  But this would significantly increase
66
               the size of the decomposition tables, because for example for
67
               U+1FC1 the recursive canonical decomposition and the recursive
68
               compatibility decomposition are different.  */
69
6.69M
            for (int curr = 0; curr < decomposed_count; )
70
3.43M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
3.43M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
3.43M
                int curr_decomposed_count;
75
76
3.43M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
3.43M
                if (curr_decomposed_count >= 0)
78
81.1k
                  {
79
                    /* Move curr_decomposed[0..curr_decomposed_count-1] over
80
                       decomposed[curr], making room.  It's not worth using
81
                       memcpy() here, since the counts are so small.  */
82
81.1k
                    int shift = curr_decomposed_count - 1;
83
84
81.1k
                    if (shift < 0)
85
0
                      abort ();
86
81.1k
                    if (shift > 0)
87
80.6k
                      {
88
80.6k
                        decomposed_count += shift;
89
80.6k
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
96.3k
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
15.7k
                          decomposed[j + shift] = decomposed[j];
93
80.6k
                      }
94
242k
                    for (; shift >= 0; shift--)
95
161k
                      decomposed[curr + shift] = curr_decomposed[shift];
96
81.1k
                  }
97
3.34M
                else
98
3.34M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
3.34M
                    curr++;
101
3.34M
                  }
102
3.43M
              }
103
3.26M
          }
104
2.20M
        else
105
2.20M
          {
106
2.20M
            count = 0;
107
2.20M
            decomposed_count = 0;
108
2.20M
          }
109
110
5.47M
        int i = 0;
111
5.47M
        for (;;)
112
8.82M
          {
113
8.82M
            ucs4_t uc;
114
8.82M
            int ccc;
115
116
8.82M
            if (s < s_end)
117
6.61M
              {
118
                /* Fetch the next character from the decomposition.  */
119
6.61M
                if (i == decomposed_count)
120
3.26M
                  break;
121
3.34M
                uc = decomposed[i];
122
3.34M
                ccc = uc_combining_class (uc);
123
3.34M
              }
124
2.20M
            else
125
2.20M
              {
126
                /* End of string reached.  */
127
2.20M
                uc = 0;
128
2.20M
                ccc = 0;
129
2.20M
              }
130
131
5.55M
            if (ccc == 0)
132
5.33M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
5.33M
                if (sortbuf_count > 1)
136
68.1k
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
68.1k
                                                           sortbuf + sortbuf_count);
138
139
5.33M
                if (composer != NULL)
140
5.33M
                  {
141
                    /* Attempt to combine decomposed characters, as specified
142
                       in the Unicode Standard Annex #15 "Unicode Normalization
143
                       Forms".  We need to check
144
                         1. whether the first accumulated character is a
145
                            "starter" (i.e. has ccc = 0).  This is usually the
146
                            case.  But when the string starts with a
147
                            non-starter, the sortbuf also starts with a
148
                            non-starter.  Btw, this check could also be
149
                            omitted, because the composition table has only
150
                            entries (code1, code2) for which code1 is a
151
                            starter; if the first accumulated character is not
152
                            a starter, no lookup will succeed.
153
                         2. If the sortbuf has more than one character, check
154
                            for each of these characters that are not "blocked"
155
                            from the starter (i.e. have a ccc that is higher
156
                            than the ccc of the previous character) whether it
157
                            can be combined with the first character.
158
                         3. If only one character is left in sortbuf, check
159
                            whether it can be combined with the next character
160
                            (also a starter).  */
161
5.33M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
3.12M
                      {
163
3.32M
                        for (size_t j = 1; j < sortbuf_count; )
164
202k
                          {
165
202k
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
86.2k
                              {
167
86.2k
                                ucs4_t combined =
168
86.2k
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
86.2k
                                if (combined)
170
60.4k
                                  {
171
60.4k
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
114k
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
53.6k
                                      sortbuf[k - 1] = sortbuf[k];
175
60.4k
                                    sortbuf_count--;
176
60.4k
                                    continue;
177
60.4k
                                  }
178
86.2k
                              }
179
142k
                            j++;
180
142k
                          }
181
3.12M
                        if (s < s_end && sortbuf_count == 1)
182
926k
                          {
183
926k
                            ucs4_t combined =
184
926k
                              composer (sortbuf[0].code, uc);
185
926k
                            if (combined)
186
16.4k
                              {
187
16.4k
                                uc = combined;
188
16.4k
                                ccc = 0;
189
                                /* uc could be further combined with subsequent
190
                                   characters.  So don't put it into sortbuf[0] in
191
                                   this round, only in the next round.  */
192
16.4k
                                sortbuf_count = 0;
193
16.4k
                              }
194
926k
                          }
195
3.12M
                      }
196
5.33M
                  }
197
198
8.60M
                for (size_t j = 0; j < sortbuf_count; j++)
199
3.27M
                  {
200
3.27M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
3.27M
                    if (length < allocated)
204
1.08M
                      {
205
1.08M
                        int ret =
206
1.08M
                          U_UCTOMB (result + length, muc, allocated - length);
207
1.08M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
1.08M
                        if (ret >= 0)
213
1.08M
                          {
214
1.08M
                            length += ret;
215
1.08M
                            goto done_appending;
216
1.08M
                          }
217
1.08M
                      }
218
2.19M
                    {
219
2.19M
                      size_t old_allocated = allocated;
220
2.19M
                      size_t new_allocated = 2 * old_allocated;
221
2.19M
                      if (new_allocated < 64)
222
2.18M
                        new_allocated = 64;
223
2.19M
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
2.19M
                      {
226
2.19M
                        UNIT *larger_result;
227
2.19M
                        if (result == NULL)
228
2.18M
                          {
229
2.18M
                            larger_result =
230
2.18M
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
2.18M
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
2.18M
                          }
237
4.34k
                        else if (result == resultbuf)
238
0
                          {
239
0
                            larger_result =
240
0
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
241
0
                            if (larger_result == NULL)
242
0
                              {
243
0
                                errno = ENOMEM;
244
0
                                goto fail;
245
0
                              }
246
0
                            U_CPY (larger_result, resultbuf, length);
247
0
                          }
248
4.34k
                        else
249
4.34k
                          {
250
4.34k
                            larger_result =
251
4.34k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
4.34k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
4.34k
                          }
258
2.19M
                        result = larger_result;
259
2.19M
                        allocated = new_allocated;
260
2.19M
                        {
261
2.19M
                          int ret =
262
2.19M
                            U_UCTOMB (result + length, muc, allocated - length);
263
2.19M
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
2.19M
                          if (ret < 0)
269
0
                            abort ();
270
2.19M
                          length += ret;
271
2.19M
                          goto done_appending;
272
2.19M
                        }
273
2.19M
                      }
274
2.19M
                    }
275
3.27M
                   done_appending: ;
276
3.27M
                  }
277
278
                /* sortbuf is now empty.  */
279
5.33M
                sortbuf_count = 0;
280
5.33M
              }
281
282
5.55M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
2.20M
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
3.34M
            if (sortbuf_count == sortbuf_allocated)
288
1.39k
              {
289
1.39k
                sortbuf_allocated = 2 * sortbuf_allocated;
290
1.39k
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
291
0
                  abort ();
292
1.39k
                struct ucs4_with_ccc *new_sortbuf =
293
1.39k
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
294
1.39k
                if (new_sortbuf == NULL)
295
0
                  {
296
0
                    errno = ENOMEM;
297
0
                    goto fail;
298
0
                  }
299
1.39k
                memcpy (new_sortbuf, sortbuf,
300
1.39k
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
301
1.39k
                if (sortbuf != sortbuf_preallocated)
302
180
                  free (sortbuf);
303
1.39k
                sortbuf = new_sortbuf;
304
1.39k
              }
305
3.34M
            sortbuf[sortbuf_count].code = uc;
306
3.34M
            sortbuf[sortbuf_count].ccc = ccc;
307
3.34M
            sortbuf_count++;
308
309
3.34M
            i++;
310
3.34M
          }
311
312
5.47M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
2.20M
          break;
315
316
3.26M
        s += count;
317
3.26M
      }
318
2.20M
  }
319
320
2.20M
  if (length == 0)
321
21.9k
    {
322
21.9k
      if (result == NULL)
323
21.9k
        {
324
          /* Return a non-NULL value.  NULL means error.  */
325
21.9k
          result = (UNIT *) malloc (1);
326
21.9k
          if (result == NULL)
327
0
            {
328
0
              errno = ENOMEM;
329
0
              goto fail;
330
0
            }
331
21.9k
        }
332
21.9k
    }
333
2.18M
  else if (result != resultbuf && length < allocated)
334
2.18M
    {
335
      /* Shrink the allocated memory if possible.  */
336
2.18M
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
2.18M
      if (memory != NULL)
338
2.18M
        result = memory;
339
2.18M
    }
340
341
2.20M
  if (sortbuf_count > 0)
342
0
    abort ();
343
2.20M
  if (sortbuf != sortbuf_preallocated)
344
1.21k
    free (sortbuf);
345
346
2.20M
  *lengthp = length;
347
2.20M
  return result;
348
349
0
 fail:
350
0
  {
351
0
    int saved_errno = errno;
352
0
    if (sortbuf != sortbuf_preallocated)
353
0
      free (sortbuf);
354
0
    if (result != resultbuf)
355
0
      free (result);
356
0
    errno = saved_errno;
357
0
  }
358
  return NULL;
359
2.20M
}