Coverage Report

Created: 2026-01-31 06:23

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-2025 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
189k
{
22
189k
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
189k
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
189k
  UNIT *result;
27
189k
  size_t length;
28
189k
  size_t allocated;
29
  /* The buffer for sorting.  */
30
189k
  #define SORTBUF_PREALLOCATED 64
31
189k
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
32
189k
  struct ucs4_with_ccc *sortbuf; /* array of size 2 * sortbuf_allocated */
33
189k
  size_t sortbuf_allocated;
34
189k
  size_t sortbuf_count;
35
36
  /* Initialize the accumulator.  */
37
189k
  if (resultbuf == NULL)
38
189k
    {
39
189k
      result = NULL;
40
189k
      allocated = 0;
41
189k
    }
42
0
  else
43
0
    {
44
0
      result = resultbuf;
45
0
      allocated = *lengthp;
46
0
    }
47
189k
  length = 0;
48
49
  /* Initialize the buffer for sorting.  */
50
189k
  sortbuf = sortbuf_preallocated;
51
189k
  sortbuf_allocated = SORTBUF_PREALLOCATED;
52
189k
  sortbuf_count = 0;
53
54
189k
  {
55
189k
    const UNIT *s_end = s + n;
56
57
189k
    for (;;)
58
2.17M
      {
59
2.17M
        int count;
60
2.17M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
61
2.17M
        int decomposed_count;
62
2.17M
        int i;
63
64
2.17M
        if (s < s_end)
65
1.98M
          {
66
            /* Fetch the next character.  */
67
1.98M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
68
1.98M
            decomposed_count = 1;
69
70
            /* Decompose it, recursively.
71
               It would be possible to precompute the recursive decomposition
72
               and store it in a table.  But this would significantly increase
73
               the size of the decomposition tables, because for example for
74
               U+1FC1 the recursive canonical decomposition and the recursive
75
               compatibility decomposition are different.  */
76
1.98M
            {
77
1.98M
              int curr;
78
79
4.25M
              for (curr = 0; curr < decomposed_count; )
80
2.26M
                {
81
                  /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
82
                     all elements are atomic.  */
83
2.26M
                  ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
84
2.26M
                  int curr_decomposed_count;
85
86
2.26M
                  curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
87
2.26M
                  if (curr_decomposed_count >= 0)
88
140k
                    {
89
                      /* Move curr_decomposed[0..curr_decomposed_count-1] over
90
                         decomposed[curr], making room.  It's not worth using
91
                         memcpy() here, since the counts are so small.  */
92
140k
                      int shift = curr_decomposed_count - 1;
93
94
140k
                      if (shift < 0)
95
0
                        abort ();
96
140k
                      if (shift > 0)
97
138k
                        {
98
138k
                          int j;
99
100
138k
                          decomposed_count += shift;
101
138k
                          if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
102
0
                            abort ();
103
151k
                          for (j = decomposed_count - 1 - shift; j > curr; j--)
104
12.8k
                            decomposed[j + shift] = decomposed[j];
105
138k
                        }
106
419k
                      for (; shift >= 0; shift--)
107
279k
                        decomposed[curr + shift] = curr_decomposed[shift];
108
140k
                    }
109
2.12M
                  else
110
2.12M
                    {
111
                      /* decomposed[curr] is atomic.  */
112
2.12M
                      curr++;
113
2.12M
                    }
114
2.26M
                }
115
1.98M
            }
116
1.98M
          }
117
189k
        else
118
189k
          {
119
189k
            count = 0;
120
189k
            decomposed_count = 0;
121
189k
          }
122
123
2.17M
        i = 0;
124
2.17M
        for (;;)
125
4.30M
          {
126
4.30M
            ucs4_t uc;
127
4.30M
            int ccc;
128
129
4.30M
            if (s < s_end)
130
4.11M
              {
131
                /* Fetch the next character from the decomposition.  */
132
4.11M
                if (i == decomposed_count)
133
1.98M
                  break;
134
2.12M
                uc = decomposed[i];
135
2.12M
                ccc = uc_combining_class (uc);
136
2.12M
              }
137
189k
            else
138
189k
              {
139
                /* End of string reached.  */
140
189k
                uc = 0;
141
189k
                ccc = 0;
142
189k
              }
143
144
2.31M
            if (ccc == 0)
145
2.03M
              {
146
2.03M
                size_t j;
147
148
                /* Apply the canonical ordering algorithm to the accumulated
149
                   sequence of characters.  */
150
2.03M
                if (sortbuf_count > 1)
151
118k
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
152
118k
                                                           sortbuf + sortbuf_count);
153
154
2.03M
                if (composer != NULL)
155
2.03M
                  {
156
                    /* Attempt to combine decomposed characters, as specified
157
                       in the Unicode Standard Annex #15 "Unicode Normalization
158
                       Forms".  We need to check
159
                         1. whether the first accumulated character is a
160
                            "starter" (i.e. has ccc = 0).  This is usually the
161
                            case.  But when the string starts with a
162
                            non-starter, the sortbuf also starts with a
163
                            non-starter.  Btw, this check could also be
164
                            omitted, because the composition table has only
165
                            entries (code1, code2) for which code1 is a
166
                            starter; if the first accumulated character is not
167
                            a starter, no lookup will succeed.
168
                         2. If the sortbuf has more than one character, check
169
                            for each of these characters that are not "blocked"
170
                            from the starter (i.e. have a ccc that is higher
171
                            than the ccc of the previous character) whether it
172
                            can be combined with the first character.
173
                         3. If only one character is left in sortbuf, check
174
                            whether it can be combined with the next character
175
                            (also a starter).  */
176
2.03M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
177
1.84M
                      {
178
2.07M
                        for (j = 1; j < sortbuf_count; )
179
231k
                          {
180
231k
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
181
135k
                              {
182
135k
                                ucs4_t combined =
183
135k
                                  composer (sortbuf[0].code, sortbuf[j].code);
184
135k
                                if (combined)
185
109k
                                  {
186
109k
                                    size_t k;
187
188
109k
                                    sortbuf[0].code = combined;
189
                                    /* sortbuf[0].ccc = 0, still valid.  */
190
193k
                                    for (k = j + 1; k < sortbuf_count; k++)
191
84.7k
                                      sortbuf[k - 1] = sortbuf[k];
192
109k
                                    sortbuf_count--;
193
109k
                                    continue;
194
109k
                                  }
195
135k
                              }
196
122k
                            j++;
197
122k
                          }
198
1.84M
                        if (s < s_end && sortbuf_count == 1)
199
1.64M
                          {
200
1.64M
                            ucs4_t combined =
201
1.64M
                              composer (sortbuf[0].code, uc);
202
1.64M
                            if (combined)
203
6.91k
                              {
204
6.91k
                                uc = combined;
205
6.91k
                                ccc = 0;
206
                                /* uc could be further combined with subsequent
207
                                   characters.  So don't put it into sortbuf[0] in
208
                                   this round, only in the next round.  */
209
6.91k
                                sortbuf_count = 0;
210
6.91k
                              }
211
1.64M
                          }
212
1.84M
                      }
213
2.03M
                  }
214
215
4.04M
                for (j = 0; j < sortbuf_count; j++)
216
2.01M
                  {
217
2.01M
                    ucs4_t muc = sortbuf[j].code;
218
219
                    /* Append muc to the result accumulator.  */
220
2.01M
                    if (length < allocated)
221
1.81M
                      {
222
1.81M
                        int ret =
223
1.81M
                          U_UCTOMB (result + length, muc, allocated - length);
224
1.81M
                        if (ret == -1)
225
0
                          {
226
0
                            errno = EINVAL;
227
0
                            goto fail;
228
0
                          }
229
1.81M
                        if (ret >= 0)
230
1.81M
                          {
231
1.81M
                            length += ret;
232
1.81M
                            goto done_appending;
233
1.81M
                          }
234
1.81M
                      }
235
195k
                    {
236
195k
                      size_t old_allocated = allocated;
237
195k
                      size_t new_allocated = 2 * old_allocated;
238
195k
                      if (new_allocated < 64)
239
187k
                        new_allocated = 64;
240
195k
                      if (new_allocated < old_allocated) /* integer overflow? */
241
0
                        abort ();
242
195k
                      {
243
195k
                        UNIT *larger_result;
244
195k
                        if (result == NULL)
245
187k
                          {
246
187k
                            larger_result =
247
187k
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
248
187k
                            if (larger_result == NULL)
249
0
                              {
250
0
                                errno = ENOMEM;
251
0
                                goto fail;
252
0
                              }
253
187k
                          }
254
7.69k
                        else if (result == resultbuf)
255
0
                          {
256
0
                            larger_result =
257
0
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
258
0
                            if (larger_result == NULL)
259
0
                              {
260
0
                                errno = ENOMEM;
261
0
                                goto fail;
262
0
                              }
263
0
                            U_CPY (larger_result, resultbuf, length);
264
0
                          }
265
7.69k
                        else
266
7.69k
                          {
267
7.69k
                            larger_result =
268
7.69k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
269
7.69k
                            if (larger_result == NULL)
270
0
                              {
271
0
                                errno = ENOMEM;
272
0
                                goto fail;
273
0
                              }
274
7.69k
                          }
275
195k
                        result = larger_result;
276
195k
                        allocated = new_allocated;
277
195k
                        {
278
195k
                          int ret =
279
195k
                            U_UCTOMB (result + length, muc, allocated - length);
280
195k
                          if (ret == -1)
281
0
                            {
282
0
                              errno = EINVAL;
283
0
                              goto fail;
284
0
                            }
285
195k
                          if (ret < 0)
286
0
                            abort ();
287
195k
                          length += ret;
288
195k
                          goto done_appending;
289
195k
                        }
290
195k
                      }
291
195k
                    }
292
2.01M
                   done_appending: ;
293
2.01M
                  }
294
295
                /* sortbuf is now empty.  */
296
2.03M
                sortbuf_count = 0;
297
2.03M
              }
298
299
2.31M
            if (!(s < s_end))
300
              /* End of string reached.  */
301
189k
              break;
302
303
            /* Append (uc, ccc) to sortbuf.  */
304
2.12M
            if (sortbuf_count == sortbuf_allocated)
305
1.32k
              {
306
1.32k
                struct ucs4_with_ccc *new_sortbuf;
307
308
1.32k
                sortbuf_allocated = 2 * sortbuf_allocated;
309
1.32k
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
310
0
                  abort ();
311
1.32k
                new_sortbuf =
312
1.32k
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
313
1.32k
                if (new_sortbuf == NULL)
314
0
                  {
315
0
                    errno = ENOMEM;
316
0
                    goto fail;
317
0
                  }
318
1.32k
                memcpy (new_sortbuf, sortbuf,
319
1.32k
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
320
1.32k
                if (sortbuf != sortbuf_preallocated)
321
1.32k
                  free (sortbuf);
322
1.32k
                sortbuf = new_sortbuf;
323
1.32k
              }
324
2.12M
            sortbuf[sortbuf_count].code = uc;
325
2.12M
            sortbuf[sortbuf_count].ccc = ccc;
326
2.12M
            sortbuf_count++;
327
328
2.12M
            i++;
329
2.12M
          }
330
331
2.17M
        if (!(s < s_end))
332
          /* End of string reached.  */
333
189k
          break;
334
335
1.98M
        s += count;
336
1.98M
      }
337
189k
  }
338
339
189k
  if (length == 0)
340
2.39k
    {
341
2.39k
      if (result == NULL)
342
2.39k
        {
343
          /* Return a non-NULL value.  NULL means error.  */
344
2.39k
          result = (UNIT *) malloc (1);
345
2.39k
          if (result == NULL)
346
0
            {
347
0
              errno = ENOMEM;
348
0
              goto fail;
349
0
            }
350
2.39k
        }
351
2.39k
    }
352
187k
  else if (result != resultbuf && length < allocated)
353
185k
    {
354
      /* Shrink the allocated memory if possible.  */
355
185k
      UNIT *memory;
356
357
185k
      memory = (UNIT *) realloc (result, length * sizeof (UNIT));
358
185k
      if (memory != NULL)
359
185k
        result = memory;
360
185k
    }
361
362
189k
  if (sortbuf_count > 0)
363
0
    abort ();
364
189k
  if (sortbuf != sortbuf_preallocated)
365
189k
    free (sortbuf);
366
367
189k
  *lengthp = length;
368
189k
  return result;
369
370
0
 fail:
371
0
  {
372
0
    int saved_errno = errno;
373
0
    if (sortbuf != sortbuf_preallocated)
374
0
      free (sortbuf);
375
0
    if (result != resultbuf)
376
0
      free (result);
377
0
    errno = saved_errno;
378
0
  }
379
  return NULL;
380
189k
}
Unexecuted instantiation: u8_normalize
u32_normalize
Line
Count
Source
21
189k
{
22
189k
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
189k
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
189k
  UNIT *result;
27
189k
  size_t length;
28
189k
  size_t allocated;
29
  /* The buffer for sorting.  */
30
189k
  #define SORTBUF_PREALLOCATED 64
31
189k
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
32
189k
  struct ucs4_with_ccc *sortbuf; /* array of size 2 * sortbuf_allocated */
33
189k
  size_t sortbuf_allocated;
34
189k
  size_t sortbuf_count;
35
36
  /* Initialize the accumulator.  */
37
189k
  if (resultbuf == NULL)
38
189k
    {
39
189k
      result = NULL;
40
189k
      allocated = 0;
41
189k
    }
42
0
  else
43
0
    {
44
0
      result = resultbuf;
45
0
      allocated = *lengthp;
46
0
    }
47
189k
  length = 0;
48
49
  /* Initialize the buffer for sorting.  */
50
189k
  sortbuf = sortbuf_preallocated;
51
189k
  sortbuf_allocated = SORTBUF_PREALLOCATED;
52
189k
  sortbuf_count = 0;
53
54
189k
  {
55
189k
    const UNIT *s_end = s + n;
56
57
189k
    for (;;)
58
2.17M
      {
59
2.17M
        int count;
60
2.17M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
61
2.17M
        int decomposed_count;
62
2.17M
        int i;
63
64
2.17M
        if (s < s_end)
65
1.98M
          {
66
            /* Fetch the next character.  */
67
1.98M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
68
1.98M
            decomposed_count = 1;
69
70
            /* Decompose it, recursively.
71
               It would be possible to precompute the recursive decomposition
72
               and store it in a table.  But this would significantly increase
73
               the size of the decomposition tables, because for example for
74
               U+1FC1 the recursive canonical decomposition and the recursive
75
               compatibility decomposition are different.  */
76
1.98M
            {
77
1.98M
              int curr;
78
79
4.25M
              for (curr = 0; curr < decomposed_count; )
80
2.26M
                {
81
                  /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
82
                     all elements are atomic.  */
83
2.26M
                  ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
84
2.26M
                  int curr_decomposed_count;
85
86
2.26M
                  curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
87
2.26M
                  if (curr_decomposed_count >= 0)
88
140k
                    {
89
                      /* Move curr_decomposed[0..curr_decomposed_count-1] over
90
                         decomposed[curr], making room.  It's not worth using
91
                         memcpy() here, since the counts are so small.  */
92
140k
                      int shift = curr_decomposed_count - 1;
93
94
140k
                      if (shift < 0)
95
0
                        abort ();
96
140k
                      if (shift > 0)
97
138k
                        {
98
138k
                          int j;
99
100
138k
                          decomposed_count += shift;
101
138k
                          if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
102
0
                            abort ();
103
151k
                          for (j = decomposed_count - 1 - shift; j > curr; j--)
104
12.8k
                            decomposed[j + shift] = decomposed[j];
105
138k
                        }
106
419k
                      for (; shift >= 0; shift--)
107
279k
                        decomposed[curr + shift] = curr_decomposed[shift];
108
140k
                    }
109
2.12M
                  else
110
2.12M
                    {
111
                      /* decomposed[curr] is atomic.  */
112
2.12M
                      curr++;
113
2.12M
                    }
114
2.26M
                }
115
1.98M
            }
116
1.98M
          }
117
189k
        else
118
189k
          {
119
189k
            count = 0;
120
189k
            decomposed_count = 0;
121
189k
          }
122
123
2.17M
        i = 0;
124
2.17M
        for (;;)
125
4.30M
          {
126
4.30M
            ucs4_t uc;
127
4.30M
            int ccc;
128
129
4.30M
            if (s < s_end)
130
4.11M
              {
131
                /* Fetch the next character from the decomposition.  */
132
4.11M
                if (i == decomposed_count)
133
1.98M
                  break;
134
2.12M
                uc = decomposed[i];
135
2.12M
                ccc = uc_combining_class (uc);
136
2.12M
              }
137
189k
            else
138
189k
              {
139
                /* End of string reached.  */
140
189k
                uc = 0;
141
189k
                ccc = 0;
142
189k
              }
143
144
2.31M
            if (ccc == 0)
145
2.03M
              {
146
2.03M
                size_t j;
147
148
                /* Apply the canonical ordering algorithm to the accumulated
149
                   sequence of characters.  */
150
2.03M
                if (sortbuf_count > 1)
151
118k
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
152
118k
                                                           sortbuf + sortbuf_count);
153
154
2.03M
                if (composer != NULL)
155
2.03M
                  {
156
                    /* Attempt to combine decomposed characters, as specified
157
                       in the Unicode Standard Annex #15 "Unicode Normalization
158
                       Forms".  We need to check
159
                         1. whether the first accumulated character is a
160
                            "starter" (i.e. has ccc = 0).  This is usually the
161
                            case.  But when the string starts with a
162
                            non-starter, the sortbuf also starts with a
163
                            non-starter.  Btw, this check could also be
164
                            omitted, because the composition table has only
165
                            entries (code1, code2) for which code1 is a
166
                            starter; if the first accumulated character is not
167
                            a starter, no lookup will succeed.
168
                         2. If the sortbuf has more than one character, check
169
                            for each of these characters that are not "blocked"
170
                            from the starter (i.e. have a ccc that is higher
171
                            than the ccc of the previous character) whether it
172
                            can be combined with the first character.
173
                         3. If only one character is left in sortbuf, check
174
                            whether it can be combined with the next character
175
                            (also a starter).  */
176
2.03M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
177
1.84M
                      {
178
2.07M
                        for (j = 1; j < sortbuf_count; )
179
231k
                          {
180
231k
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
181
135k
                              {
182
135k
                                ucs4_t combined =
183
135k
                                  composer (sortbuf[0].code, sortbuf[j].code);
184
135k
                                if (combined)
185
109k
                                  {
186
109k
                                    size_t k;
187
188
109k
                                    sortbuf[0].code = combined;
189
                                    /* sortbuf[0].ccc = 0, still valid.  */
190
193k
                                    for (k = j + 1; k < sortbuf_count; k++)
191
84.7k
                                      sortbuf[k - 1] = sortbuf[k];
192
109k
                                    sortbuf_count--;
193
109k
                                    continue;
194
109k
                                  }
195
135k
                              }
196
122k
                            j++;
197
122k
                          }
198
1.84M
                        if (s < s_end && sortbuf_count == 1)
199
1.64M
                          {
200
1.64M
                            ucs4_t combined =
201
1.64M
                              composer (sortbuf[0].code, uc);
202
1.64M
                            if (combined)
203
6.91k
                              {
204
6.91k
                                uc = combined;
205
6.91k
                                ccc = 0;
206
                                /* uc could be further combined with subsequent
207
                                   characters.  So don't put it into sortbuf[0] in
208
                                   this round, only in the next round.  */
209
6.91k
                                sortbuf_count = 0;
210
6.91k
                              }
211
1.64M
                          }
212
1.84M
                      }
213
2.03M
                  }
214
215
4.04M
                for (j = 0; j < sortbuf_count; j++)
216
2.01M
                  {
217
2.01M
                    ucs4_t muc = sortbuf[j].code;
218
219
                    /* Append muc to the result accumulator.  */
220
2.01M
                    if (length < allocated)
221
1.81M
                      {
222
1.81M
                        int ret =
223
1.81M
                          U_UCTOMB (result + length, muc, allocated - length);
224
1.81M
                        if (ret == -1)
225
0
                          {
226
0
                            errno = EINVAL;
227
0
                            goto fail;
228
0
                          }
229
1.81M
                        if (ret >= 0)
230
1.81M
                          {
231
1.81M
                            length += ret;
232
1.81M
                            goto done_appending;
233
1.81M
                          }
234
1.81M
                      }
235
195k
                    {
236
195k
                      size_t old_allocated = allocated;
237
195k
                      size_t new_allocated = 2 * old_allocated;
238
195k
                      if (new_allocated < 64)
239
187k
                        new_allocated = 64;
240
195k
                      if (new_allocated < old_allocated) /* integer overflow? */
241
0
                        abort ();
242
195k
                      {
243
195k
                        UNIT *larger_result;
244
195k
                        if (result == NULL)
245
187k
                          {
246
187k
                            larger_result =
247
187k
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
248
187k
                            if (larger_result == NULL)
249
0
                              {
250
0
                                errno = ENOMEM;
251
0
                                goto fail;
252
0
                              }
253
187k
                          }
254
7.69k
                        else if (result == resultbuf)
255
0
                          {
256
0
                            larger_result =
257
0
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
258
0
                            if (larger_result == NULL)
259
0
                              {
260
0
                                errno = ENOMEM;
261
0
                                goto fail;
262
0
                              }
263
0
                            U_CPY (larger_result, resultbuf, length);
264
0
                          }
265
7.69k
                        else
266
7.69k
                          {
267
7.69k
                            larger_result =
268
7.69k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
269
7.69k
                            if (larger_result == NULL)
270
0
                              {
271
0
                                errno = ENOMEM;
272
0
                                goto fail;
273
0
                              }
274
7.69k
                          }
275
195k
                        result = larger_result;
276
195k
                        allocated = new_allocated;
277
195k
                        {
278
195k
                          int ret =
279
195k
                            U_UCTOMB (result + length, muc, allocated - length);
280
195k
                          if (ret == -1)
281
0
                            {
282
0
                              errno = EINVAL;
283
0
                              goto fail;
284
0
                            }
285
195k
                          if (ret < 0)
286
0
                            abort ();
287
195k
                          length += ret;
288
195k
                          goto done_appending;
289
195k
                        }
290
195k
                      }
291
195k
                    }
292
2.01M
                   done_appending: ;
293
2.01M
                  }
294
295
                /* sortbuf is now empty.  */
296
2.03M
                sortbuf_count = 0;
297
2.03M
              }
298
299
2.31M
            if (!(s < s_end))
300
              /* End of string reached.  */
301
189k
              break;
302
303
            /* Append (uc, ccc) to sortbuf.  */
304
2.12M
            if (sortbuf_count == sortbuf_allocated)
305
1.32k
              {
306
1.32k
                struct ucs4_with_ccc *new_sortbuf;
307
308
1.32k
                sortbuf_allocated = 2 * sortbuf_allocated;
309
1.32k
                if (sortbuf_allocated < sortbuf_count) /* integer overflow? */
310
0
                  abort ();
311
1.32k
                new_sortbuf =
312
1.32k
                  (struct ucs4_with_ccc *) malloc (2 * sortbuf_allocated * sizeof (struct ucs4_with_ccc));
313
1.32k
                if (new_sortbuf == NULL)
314
0
                  {
315
0
                    errno = ENOMEM;
316
0
                    goto fail;
317
0
                  }
318
1.32k
                memcpy (new_sortbuf, sortbuf,
319
1.32k
                        sortbuf_count * sizeof (struct ucs4_with_ccc));
320
1.32k
                if (sortbuf != sortbuf_preallocated)
321
1.32k
                  free (sortbuf);
322
1.32k
                sortbuf = new_sortbuf;
323
1.32k
              }
324
2.12M
            sortbuf[sortbuf_count].code = uc;
325
2.12M
            sortbuf[sortbuf_count].ccc = ccc;
326
2.12M
            sortbuf_count++;
327
328
2.12M
            i++;
329
2.12M
          }
330
331
2.17M
        if (!(s < s_end))
332
          /* End of string reached.  */
333
189k
          break;
334
335
1.98M
        s += count;
336
1.98M
      }
337
189k
  }
338
339
189k
  if (length == 0)
340
2.39k
    {
341
2.39k
      if (result == NULL)
342
2.39k
        {
343
          /* Return a non-NULL value.  NULL means error.  */
344
2.39k
          result = (UNIT *) malloc (1);
345
2.39k
          if (result == NULL)
346
0
            {
347
0
              errno = ENOMEM;
348
0
              goto fail;
349
0
            }
350
2.39k
        }
351
2.39k
    }
352
187k
  else if (result != resultbuf && length < allocated)
353
185k
    {
354
      /* Shrink the allocated memory if possible.  */
355
185k
      UNIT *memory;
356
357
185k
      memory = (UNIT *) realloc (result, length * sizeof (UNIT));
358
185k
      if (memory != NULL)
359
185k
        result = memory;
360
185k
    }
361
362
189k
  if (sortbuf_count > 0)
363
0
    abort ();
364
189k
  if (sortbuf != sortbuf_preallocated)
365
189k
    free (sortbuf);
366
367
189k
  *lengthp = length;
368
189k
  return result;
369
370
0
 fail:
371
0
  {
372
0
    int saved_errno = errno;
373
0
    if (sortbuf != sortbuf_preallocated)
374
0
      free (sortbuf);
375
0
    if (result != resultbuf)
376
0
      free (result);
377
0
    errno = saved_errno;
378
0
  }
379
  return NULL;
380
189k
}