Coverage Report

Created: 2026-07-05 07:04

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
7.17k
{
22
7.17k
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
7.17k
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
7.17k
  UNIT *result;
27
7.17k
  size_t allocated;
28
7.17k
  if (resultbuf == NULL)
29
7.17k
    {
30
7.17k
      result = NULL;
31
7.17k
      allocated = 0;
32
7.17k
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
7.17k
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
7.17k
  #define SORTBUF_PREALLOCATED 64
42
7.17k
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
7.17k
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
7.17k
    sortbuf_preallocated;
45
7.17k
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
7.17k
  size_t sortbuf_count = 0;
47
48
7.17k
  {
49
7.17k
    const UNIT *s_end = s + n;
50
51
7.17k
    for (;;)
52
6.33M
      {
53
6.33M
        int count;
54
6.33M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
6.33M
        int decomposed_count;
56
57
6.33M
        if (s < s_end)
58
6.32M
          {
59
            /* Fetch the next character.  */
60
6.32M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
6.32M
            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
21.9M
            for (int curr = 0; curr < decomposed_count; )
70
15.6M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
15.6M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
15.6M
                int curr_decomposed_count;
75
76
15.6M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
15.6M
                if (curr_decomposed_count >= 0)
78
2.93M
                  {
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
2.93M
                    int shift = curr_decomposed_count - 1;
83
84
2.93M
                    if (shift < 0)
85
0
                      abort ();
86
2.93M
                    if (shift > 0)
87
2.67M
                      {
88
2.67M
                        decomposed_count += shift;
89
2.67M
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
2.68M
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
10.7k
                          decomposed[j + shift] = decomposed[j];
93
2.67M
                      }
94
12.2M
                    for (; shift >= 0; shift--)
95
9.32M
                      decomposed[curr + shift] = curr_decomposed[shift];
96
2.93M
                  }
97
12.7M
                else
98
12.7M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
12.7M
                    curr++;
101
12.7M
                  }
102
15.6M
              }
103
6.32M
          }
104
7.17k
        else
105
7.17k
          {
106
7.17k
            count = 0;
107
7.17k
            decomposed_count = 0;
108
7.17k
          }
109
110
6.33M
        int i = 0;
111
6.33M
        for (;;)
112
19.0M
          {
113
19.0M
            ucs4_t uc;
114
19.0M
            int ccc;
115
116
19.0M
            if (s < s_end)
117
19.0M
              {
118
                /* Fetch the next character from the decomposition.  */
119
19.0M
                if (i == decomposed_count)
120
6.32M
                  break;
121
12.7M
                uc = decomposed[i];
122
12.7M
                ccc = uc_combining_class (uc);
123
12.7M
              }
124
7.17k
            else
125
7.17k
              {
126
                /* End of string reached.  */
127
7.17k
                uc = 0;
128
7.17k
                ccc = 0;
129
7.17k
              }
130
131
12.7M
            if (ccc == 0)
132
9.64M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
9.64M
                if (sortbuf_count > 1)
136
1.68M
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
1.68M
                                                           sortbuf + sortbuf_count);
138
139
9.64M
                if (composer != NULL)
140
9.64M
                  {
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
9.64M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
9.64M
                      {
163
12.7M
                        for (size_t j = 1; j < sortbuf_count; )
164
3.06M
                          {
165
3.06M
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
1.70M
                              {
167
1.70M
                                ucs4_t combined =
168
1.70M
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
1.70M
                                if (combined)
170
1.66M
                                  {
171
1.66M
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
3.61M
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
1.95M
                                      sortbuf[k - 1] = sortbuf[k];
175
1.66M
                                    sortbuf_count--;
176
1.66M
                                    continue;
177
1.66M
                                  }
178
1.70M
                              }
179
1.40M
                            j++;
180
1.40M
                          }
181
9.64M
                        if (s < s_end && sortbuf_count == 1)
182
9.60M
                          {
183
9.60M
                            ucs4_t combined =
184
9.60M
                              composer (sortbuf[0].code, uc);
185
9.60M
                            if (combined)
186
4.96k
                              {
187
4.96k
                                uc = combined;
188
4.96k
                                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
4.96k
                                sortbuf_count = 0;
193
4.96k
                              }
194
9.60M
                          }
195
9.64M
                      }
196
9.64M
                  }
197
198
20.6M
                for (size_t j = 0; j < sortbuf_count; j++)
199
11.0M
                  {
200
11.0M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
11.0M
                    if (length < allocated)
204
11.0M
                      {
205
11.0M
                        int ret =
206
11.0M
                          U_UCTOMB (result + length, muc, allocated - length);
207
11.0M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
11.0M
                        if (ret >= 0)
213
11.0M
                          {
214
11.0M
                            length += ret;
215
11.0M
                            goto done_appending;
216
11.0M
                          }
217
11.0M
                      }
218
14.6k
                    {
219
14.6k
                      size_t old_allocated = allocated;
220
14.6k
                      size_t new_allocated = 2 * old_allocated;
221
14.6k
                      if (new_allocated < 64)
222
7.17k
                        new_allocated = 64;
223
14.6k
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
14.6k
                      {
226
14.6k
                        UNIT *larger_result;
227
14.6k
                        if (result == NULL)
228
7.17k
                          {
229
7.17k
                            larger_result =
230
7.17k
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
7.17k
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
7.17k
                          }
237
7.50k
                        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
7.50k
                        else
249
7.50k
                          {
250
7.50k
                            larger_result =
251
7.50k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
7.50k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
7.50k
                          }
258
14.6k
                        result = larger_result;
259
14.6k
                        allocated = new_allocated;
260
14.6k
                        {
261
14.6k
                          int ret =
262
14.6k
                            U_UCTOMB (result + length, muc, allocated - length);
263
14.6k
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
14.6k
                          if (ret < 0)
269
0
                            abort ();
270
14.6k
                          length += ret;
271
14.6k
                          goto done_appending;
272
14.6k
                        }
273
14.6k
                      }
274
14.6k
                    }
275
11.0M
                   done_appending: ;
276
11.0M
                  }
277
278
                /* sortbuf is now empty.  */
279
9.64M
                sortbuf_count = 0;
280
9.64M
              }
281
282
12.7M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
7.17k
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
12.7M
            if (sortbuf_count == sortbuf_allocated)
288
864
              {
289
864
                sortbuf_allocated = 2 * sortbuf_allocated;
290
864
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
291
0
                  abort ();
292
864
                struct ucs4_with_ccc *new_sortbuf =
293
864
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
294
864
                if (new_sortbuf == NULL)
295
0
                  {
296
0
                    errno = ENOMEM;
297
0
                    goto fail;
298
0
                  }
299
864
                memcpy (new_sortbuf, sortbuf,
300
864
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
301
864
                if (sortbuf != sortbuf_preallocated)
302
864
                  free (sortbuf);
303
864
                sortbuf = new_sortbuf;
304
864
              }
305
12.7M
            sortbuf[sortbuf_count].code = uc;
306
12.7M
            sortbuf[sortbuf_count].ccc = ccc;
307
12.7M
            sortbuf_count++;
308
309
12.7M
            i++;
310
12.7M
          }
311
312
6.33M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
7.17k
          break;
315
316
6.32M
        s += count;
317
6.32M
      }
318
7.17k
  }
319
320
7.17k
  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
7.17k
  else if (result != resultbuf && length < allocated)
334
7.11k
    {
335
      /* Shrink the allocated memory if possible.  */
336
7.11k
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
7.11k
      if (memory != NULL)
338
7.11k
        result = memory;
339
7.11k
    }
340
341
7.17k
  if (sortbuf_count > 0)
342
0
    abort ();
343
7.17k
  if (sortbuf != sortbuf_preallocated)
344
7.17k
    free (sortbuf);
345
346
7.17k
  *lengthp = length;
347
7.17k
  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
7.17k
}
u8_normalize
Line
Count
Source
21
6.77k
{
22
6.77k
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
6.77k
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
6.77k
  UNIT *result;
27
6.77k
  size_t allocated;
28
6.77k
  if (resultbuf == NULL)
29
6.77k
    {
30
6.77k
      result = NULL;
31
6.77k
      allocated = 0;
32
6.77k
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
6.77k
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
6.77k
  #define SORTBUF_PREALLOCATED 64
42
6.77k
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
6.77k
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
6.77k
    sortbuf_preallocated;
45
6.77k
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
6.77k
  size_t sortbuf_count = 0;
47
48
6.77k
  {
49
6.77k
    const UNIT *s_end = s + n;
50
51
6.77k
    for (;;)
52
6.33M
      {
53
6.33M
        int count;
54
6.33M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
6.33M
        int decomposed_count;
56
57
6.33M
        if (s < s_end)
58
6.32M
          {
59
            /* Fetch the next character.  */
60
6.32M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
6.32M
            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
21.9M
            for (int curr = 0; curr < decomposed_count; )
70
15.6M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
15.6M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
15.6M
                int curr_decomposed_count;
75
76
15.6M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
15.6M
                if (curr_decomposed_count >= 0)
78
2.93M
                  {
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
2.93M
                    int shift = curr_decomposed_count - 1;
83
84
2.93M
                    if (shift < 0)
85
0
                      abort ();
86
2.93M
                    if (shift > 0)
87
2.66M
                      {
88
2.66M
                        decomposed_count += shift;
89
2.66M
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
2.68M
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
10.7k
                          decomposed[j + shift] = decomposed[j];
93
2.66M
                      }
94
12.2M
                    for (; shift >= 0; shift--)
95
9.32M
                      decomposed[curr + shift] = curr_decomposed[shift];
96
2.93M
                  }
97
12.7M
                else
98
12.7M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
12.7M
                    curr++;
101
12.7M
                  }
102
15.6M
              }
103
6.32M
          }
104
6.77k
        else
105
6.77k
          {
106
6.77k
            count = 0;
107
6.77k
            decomposed_count = 0;
108
6.77k
          }
109
110
6.33M
        int i = 0;
111
6.33M
        for (;;)
112
19.0M
          {
113
19.0M
            ucs4_t uc;
114
19.0M
            int ccc;
115
116
19.0M
            if (s < s_end)
117
19.0M
              {
118
                /* Fetch the next character from the decomposition.  */
119
19.0M
                if (i == decomposed_count)
120
6.32M
                  break;
121
12.7M
                uc = decomposed[i];
122
12.7M
                ccc = uc_combining_class (uc);
123
12.7M
              }
124
6.77k
            else
125
6.77k
              {
126
                /* End of string reached.  */
127
6.77k
                uc = 0;
128
6.77k
                ccc = 0;
129
6.77k
              }
130
131
12.7M
            if (ccc == 0)
132
9.64M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
9.64M
                if (sortbuf_count > 1)
136
1.68M
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
1.68M
                                                           sortbuf + sortbuf_count);
138
139
9.64M
                if (composer != NULL)
140
9.64M
                  {
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
9.64M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
9.63M
                      {
163
12.7M
                        for (size_t j = 1; j < sortbuf_count; )
164
3.06M
                          {
165
3.06M
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
1.70M
                              {
167
1.70M
                                ucs4_t combined =
168
1.70M
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
1.70M
                                if (combined)
170
1.66M
                                  {
171
1.66M
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
3.61M
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
1.95M
                                      sortbuf[k - 1] = sortbuf[k];
175
1.66M
                                    sortbuf_count--;
176
1.66M
                                    continue;
177
1.66M
                                  }
178
1.70M
                              }
179
1.40M
                            j++;
180
1.40M
                          }
181
9.63M
                        if (s < s_end && sortbuf_count == 1)
182
9.60M
                          {
183
9.60M
                            ucs4_t combined =
184
9.60M
                              composer (sortbuf[0].code, uc);
185
9.60M
                            if (combined)
186
4.96k
                              {
187
4.96k
                                uc = combined;
188
4.96k
                                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
4.96k
                                sortbuf_count = 0;
193
4.96k
                              }
194
9.60M
                          }
195
9.63M
                      }
196
9.64M
                  }
197
198
20.6M
                for (size_t j = 0; j < sortbuf_count; j++)
199
11.0M
                  {
200
11.0M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
11.0M
                    if (length < allocated)
204
11.0M
                      {
205
11.0M
                        int ret =
206
11.0M
                          U_UCTOMB (result + length, muc, allocated - length);
207
11.0M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
11.0M
                        if (ret >= 0)
213
11.0M
                          {
214
11.0M
                            length += ret;
215
11.0M
                            goto done_appending;
216
11.0M
                          }
217
11.0M
                      }
218
14.2k
                    {
219
14.2k
                      size_t old_allocated = allocated;
220
14.2k
                      size_t new_allocated = 2 * old_allocated;
221
14.2k
                      if (new_allocated < 64)
222
6.77k
                        new_allocated = 64;
223
14.2k
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
14.2k
                      {
226
14.2k
                        UNIT *larger_result;
227
14.2k
                        if (result == NULL)
228
6.77k
                          {
229
6.77k
                            larger_result =
230
6.77k
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
6.77k
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
6.77k
                          }
237
7.50k
                        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
7.50k
                        else
249
7.50k
                          {
250
7.50k
                            larger_result =
251
7.50k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
7.50k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
7.50k
                          }
258
14.2k
                        result = larger_result;
259
14.2k
                        allocated = new_allocated;
260
14.2k
                        {
261
14.2k
                          int ret =
262
14.2k
                            U_UCTOMB (result + length, muc, allocated - length);
263
14.2k
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
14.2k
                          if (ret < 0)
269
0
                            abort ();
270
14.2k
                          length += ret;
271
14.2k
                          goto done_appending;
272
14.2k
                        }
273
14.2k
                      }
274
14.2k
                    }
275
11.0M
                   done_appending: ;
276
11.0M
                  }
277
278
                /* sortbuf is now empty.  */
279
9.64M
                sortbuf_count = 0;
280
9.64M
              }
281
282
12.7M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
6.77k
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
12.7M
            if (sortbuf_count == sortbuf_allocated)
288
864
              {
289
864
                sortbuf_allocated = 2 * sortbuf_allocated;
290
864
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
291
0
                  abort ();
292
864
                struct ucs4_with_ccc *new_sortbuf =
293
864
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
294
864
                if (new_sortbuf == NULL)
295
0
                  {
296
0
                    errno = ENOMEM;
297
0
                    goto fail;
298
0
                  }
299
864
                memcpy (new_sortbuf, sortbuf,
300
864
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
301
864
                if (sortbuf != sortbuf_preallocated)
302
864
                  free (sortbuf);
303
864
                sortbuf = new_sortbuf;
304
864
              }
305
12.7M
            sortbuf[sortbuf_count].code = uc;
306
12.7M
            sortbuf[sortbuf_count].ccc = ccc;
307
12.7M
            sortbuf_count++;
308
309
12.7M
            i++;
310
12.7M
          }
311
312
6.33M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
6.77k
          break;
315
316
6.32M
        s += count;
317
6.32M
      }
318
6.77k
  }
319
320
6.77k
  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
6.77k
  else if (result != resultbuf && length < allocated)
334
6.72k
    {
335
      /* Shrink the allocated memory if possible.  */
336
6.72k
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
6.72k
      if (memory != NULL)
338
6.72k
        result = memory;
339
6.72k
    }
340
341
6.77k
  if (sortbuf_count > 0)
342
0
    abort ();
343
6.77k
  if (sortbuf != sortbuf_preallocated)
344
6.77k
    free (sortbuf);
345
346
6.77k
  *lengthp = length;
347
6.77k
  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
6.77k
}
u32_normalize
Line
Count
Source
21
396
{
22
396
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
396
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
396
  UNIT *result;
27
396
  size_t allocated;
28
396
  if (resultbuf == NULL)
29
396
    {
30
396
      result = NULL;
31
396
      allocated = 0;
32
396
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
396
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
396
  #define SORTBUF_PREALLOCATED 64
42
396
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
396
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
396
    sortbuf_preallocated;
45
396
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
396
  size_t sortbuf_count = 0;
47
48
396
  {
49
396
    const UNIT *s_end = s + n;
50
51
396
    for (;;)
52
1.48k
      {
53
1.48k
        int count;
54
1.48k
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
1.48k
        int decomposed_count;
56
57
1.48k
        if (s < s_end)
58
1.08k
          {
59
            /* Fetch the next character.  */
60
1.08k
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
1.08k
            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
2.77k
            for (int curr = 0; curr < decomposed_count; )
70
1.68k
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
1.68k
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
1.68k
                int curr_decomposed_count;
75
76
1.68k
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
1.68k
                if (curr_decomposed_count >= 0)
78
297
                  {
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
297
                    int shift = curr_decomposed_count - 1;
83
84
297
                    if (shift < 0)
85
0
                      abort ();
86
297
                    if (shift > 0)
87
297
                      {
88
297
                        decomposed_count += shift;
89
297
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
297
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
0
                          decomposed[j + shift] = decomposed[j];
93
297
                      }
94
891
                    for (; shift >= 0; shift--)
95
594
                      decomposed[curr + shift] = curr_decomposed[shift];
96
297
                  }
97
1.38k
                else
98
1.38k
                  {
99
                    /* decomposed[curr] is atomic.  */
100
1.38k
                    curr++;
101
1.38k
                  }
102
1.68k
              }
103
1.08k
          }
104
396
        else
105
396
          {
106
396
            count = 0;
107
396
            decomposed_count = 0;
108
396
          }
109
110
1.48k
        int i = 0;
111
1.48k
        for (;;)
112
2.87k
          {
113
2.87k
            ucs4_t uc;
114
2.87k
            int ccc;
115
116
2.87k
            if (s < s_end)
117
2.47k
              {
118
                /* Fetch the next character from the decomposition.  */
119
2.47k
                if (i == decomposed_count)
120
1.08k
                  break;
121
1.38k
                uc = decomposed[i];
122
1.38k
                ccc = uc_combining_class (uc);
123
1.38k
              }
124
396
            else
125
396
              {
126
                /* End of string reached.  */
127
396
                uc = 0;
128
396
                ccc = 0;
129
396
              }
130
131
1.78k
            if (ccc == 0)
132
1.48k
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
1.48k
                if (sortbuf_count > 1)
136
297
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
297
                                                           sortbuf + sortbuf_count);
138
139
1.48k
                if (composer != NULL)
140
1.48k
                  {
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
1.48k
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
1.08k
                      {
163
1.38k
                        for (size_t j = 1; j < sortbuf_count; )
164
297
                          {
165
297
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
297
                              {
167
297
                                ucs4_t combined =
168
297
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
297
                                if (combined)
170
297
                                  {
171
297
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
297
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
0
                                      sortbuf[k - 1] = sortbuf[k];
175
297
                                    sortbuf_count--;
176
297
                                    continue;
177
297
                                  }
178
297
                              }
179
0
                            j++;
180
0
                          }
181
1.08k
                        if (s < s_end && sortbuf_count == 1)
182
693
                          {
183
693
                            ucs4_t combined =
184
693
                              composer (sortbuf[0].code, uc);
185
693
                            if (combined)
186
0
                              {
187
0
                                uc = combined;
188
0
                                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
0
                                sortbuf_count = 0;
193
0
                              }
194
693
                          }
195
1.08k
                      }
196
1.48k
                  }
197
198
2.57k
                for (size_t j = 0; j < sortbuf_count; j++)
199
1.08k
                  {
200
1.08k
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
1.08k
                    if (length < allocated)
204
693
                      {
205
693
                        int ret =
206
693
                          U_UCTOMB (result + length, muc, allocated - length);
207
693
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
693
                        if (ret >= 0)
213
693
                          {
214
693
                            length += ret;
215
693
                            goto done_appending;
216
693
                          }
217
693
                      }
218
396
                    {
219
396
                      size_t old_allocated = allocated;
220
396
                      size_t new_allocated = 2 * old_allocated;
221
396
                      if (new_allocated < 64)
222
396
                        new_allocated = 64;
223
396
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
396
                      {
226
396
                        UNIT *larger_result;
227
396
                        if (result == NULL)
228
396
                          {
229
396
                            larger_result =
230
396
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
396
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
396
                          }
237
0
                        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
0
                        else
249
0
                          {
250
0
                            larger_result =
251
0
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
0
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
0
                          }
258
396
                        result = larger_result;
259
396
                        allocated = new_allocated;
260
396
                        {
261
396
                          int ret =
262
396
                            U_UCTOMB (result + length, muc, allocated - length);
263
396
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
396
                          if (ret < 0)
269
0
                            abort ();
270
396
                          length += ret;
271
396
                          goto done_appending;
272
396
                        }
273
396
                      }
274
396
                    }
275
1.08k
                   done_appending: ;
276
1.08k
                  }
277
278
                /* sortbuf is now empty.  */
279
1.48k
                sortbuf_count = 0;
280
1.48k
              }
281
282
1.78k
            if (!(s < s_end))
283
              /* End of string reached.  */
284
396
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
1.38k
            if (sortbuf_count == sortbuf_allocated)
288
0
              {
289
0
                sortbuf_allocated = 2 * sortbuf_allocated;
290
0
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
291
0
                  abort ();
292
0
                struct ucs4_with_ccc *new_sortbuf =
293
0
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
294
0
                if (new_sortbuf == NULL)
295
0
                  {
296
0
                    errno = ENOMEM;
297
0
                    goto fail;
298
0
                  }
299
0
                memcpy (new_sortbuf, sortbuf,
300
0
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
301
0
                if (sortbuf != sortbuf_preallocated)
302
0
                  free (sortbuf);
303
0
                sortbuf = new_sortbuf;
304
0
              }
305
1.38k
            sortbuf[sortbuf_count].code = uc;
306
1.38k
            sortbuf[sortbuf_count].ccc = ccc;
307
1.38k
            sortbuf_count++;
308
309
1.38k
            i++;
310
1.38k
          }
311
312
1.48k
        if (!(s < s_end))
313
          /* End of string reached.  */
314
396
          break;
315
316
1.08k
        s += count;
317
1.08k
      }
318
396
  }
319
320
396
  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
396
  else if (result != resultbuf && length < allocated)
334
396
    {
335
      /* Shrink the allocated memory if possible.  */
336
396
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
396
      if (memory != NULL)
338
396
        result = memory;
339
396
    }
340
341
396
  if (sortbuf_count > 0)
342
0
    abort ();
343
396
  if (sortbuf != sortbuf_preallocated)
344
396
    free (sortbuf);
345
346
396
  *lengthp = length;
347
396
  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
396
}