Coverage Report

Created: 2026-10-14 06:53

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
192k
{
22
192k
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
192k
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
192k
  UNIT *result;
27
192k
  size_t allocated;
28
192k
  if (resultbuf == NULL)
29
192k
    {
30
192k
      result = NULL;
31
192k
      allocated = 0;
32
192k
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
192k
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
192k
  #define SORTBUF_PREALLOCATED 64
42
192k
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
192k
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
192k
    sortbuf_preallocated;
45
192k
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
192k
  size_t sortbuf_count = 0;
47
48
192k
  {
49
192k
    const UNIT *s_end = s + n;
50
51
192k
    for (;;)
52
2.18M
      {
53
2.18M
        int count;
54
2.18M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
2.18M
        int decomposed_count;
56
57
2.18M
        if (s < s_end)
58
1.99M
          {
59
            /* Fetch the next character.  */
60
1.99M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
1.99M
            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
4.30M
            for (int curr = 0; curr < decomposed_count; )
70
2.30M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
2.30M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
2.30M
                int curr_decomposed_count;
75
76
2.30M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
2.30M
                if (curr_decomposed_count >= 0)
78
156k
                  {
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
156k
                    int shift = curr_decomposed_count - 1;
83
84
156k
                    if (shift < 0)
85
0
                      abort ();
86
156k
                    if (shift > 0)
87
154k
                      {
88
154k
                        decomposed_count += shift;
89
154k
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
167k
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
12.8k
                          decomposed[j + shift] = decomposed[j];
93
154k
                      }
94
467k
                    for (; shift >= 0; shift--)
95
311k
                      decomposed[curr + shift] = curr_decomposed[shift];
96
156k
                  }
97
2.15M
                else
98
2.15M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
2.15M
                    curr++;
101
2.15M
                  }
102
2.30M
              }
103
1.99M
          }
104
192k
        else
105
192k
          {
106
192k
            count = 0;
107
192k
            decomposed_count = 0;
108
192k
          }
109
110
2.18M
        int i = 0;
111
2.18M
        for (;;)
112
4.33M
          {
113
4.33M
            ucs4_t uc;
114
4.33M
            int ccc;
115
116
4.33M
            if (s < s_end)
117
4.14M
              {
118
                /* Fetch the next character from the decomposition.  */
119
4.14M
                if (i == decomposed_count)
120
1.99M
                  break;
121
2.15M
                uc = decomposed[i];
122
2.15M
                ccc = uc_combining_class (uc);
123
2.15M
              }
124
192k
            else
125
192k
              {
126
                /* End of string reached.  */
127
192k
                uc = 0;
128
192k
                ccc = 0;
129
192k
              }
130
131
2.34M
            if (ccc == 0)
132
2.02M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
2.02M
                if (sortbuf_count > 1)
136
134k
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
134k
                                                           sortbuf + sortbuf_count);
138
139
2.02M
                if (composer != NULL)
140
2.02M
                  {
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
2.02M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
1.83M
                      {
163
2.09M
                        for (size_t j = 1; j < sortbuf_count; )
164
256k
                          {
165
256k
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
150k
                              {
167
150k
                                ucs4_t combined =
168
150k
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
150k
                                if (combined)
170
124k
                                  {
171
124k
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
225k
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
100k
                                      sortbuf[k - 1] = sortbuf[k];
175
124k
                                    sortbuf_count--;
176
124k
                                    continue;
177
124k
                                  }
178
150k
                              }
179
132k
                            j++;
180
132k
                          }
181
1.83M
                        if (s < s_end && sortbuf_count == 1)
182
1.63M
                          {
183
1.63M
                            ucs4_t combined =
184
1.63M
                              composer (sortbuf[0].code, uc);
185
1.63M
                            if (combined)
186
6.76k
                              {
187
6.76k
                                uc = combined;
188
6.76k
                                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
6.76k
                                sortbuf_count = 0;
193
6.76k
                              }
194
1.63M
                          }
195
1.83M
                      }
196
2.02M
                  }
197
198
4.04M
                for (size_t j = 0; j < sortbuf_count; j++)
199
2.01M
                  {
200
2.01M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
2.01M
                    if (length < allocated)
204
1.81M
                      {
205
1.81M
                        int ret =
206
1.81M
                          U_UCTOMB (result + length, muc, allocated - length);
207
1.81M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
1.81M
                        if (ret >= 0)
213
1.81M
                          {
214
1.81M
                            length += ret;
215
1.81M
                            goto done_appending;
216
1.81M
                          }
217
1.81M
                      }
218
198k
                    {
219
198k
                      size_t old_allocated = allocated;
220
198k
                      size_t new_allocated = 2 * old_allocated;
221
198k
                      if (new_allocated < 64)
222
190k
                        new_allocated = 64;
223
198k
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
198k
                      {
226
198k
                        UNIT *larger_result;
227
198k
                        if (result == NULL)
228
190k
                          {
229
190k
                            larger_result =
230
190k
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
190k
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
190k
                          }
237
7.91k
                        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.91k
                        else
249
7.91k
                          {
250
7.91k
                            larger_result =
251
7.91k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
7.91k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
7.91k
                          }
258
198k
                        result = larger_result;
259
198k
                        allocated = new_allocated;
260
198k
                        {
261
198k
                          int ret =
262
198k
                            U_UCTOMB (result + length, muc, allocated - length);
263
198k
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
198k
                          if (ret < 0)
269
0
                            abort ();
270
198k
                          length += ret;
271
198k
                          goto done_appending;
272
198k
                        }
273
198k
                      }
274
198k
                    }
275
2.01M
                   done_appending: ;
276
2.01M
                  }
277
278
                /* sortbuf is now empty.  */
279
2.02M
                sortbuf_count = 0;
280
2.02M
              }
281
282
2.34M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
192k
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
2.15M
            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
1.39k
                  free (sortbuf);
303
1.39k
                sortbuf = new_sortbuf;
304
1.39k
              }
305
2.15M
            sortbuf[sortbuf_count].code = uc;
306
2.15M
            sortbuf[sortbuf_count].ccc = ccc;
307
2.15M
            sortbuf_count++;
308
309
2.15M
            i++;
310
2.15M
          }
311
312
2.18M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
192k
          break;
315
316
1.99M
        s += count;
317
1.99M
      }
318
192k
  }
319
320
192k
  if (length == 0)
321
2.14k
    {
322
2.14k
      if (result == NULL)
323
2.14k
        {
324
          /* Return a non-NULL value.  NULL means error.  */
325
2.14k
          result = (UNIT *) malloc (1);
326
2.14k
          if (result == NULL)
327
0
            {
328
0
              errno = ENOMEM;
329
0
              goto fail;
330
0
            }
331
2.14k
        }
332
2.14k
    }
333
190k
  else if (result != resultbuf && length < allocated)
334
188k
    {
335
      /* Shrink the allocated memory if possible.  */
336
188k
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
188k
      if (memory != NULL)
338
188k
        result = memory;
339
188k
    }
340
341
192k
  if (sortbuf_count > 0)
342
0
    abort ();
343
192k
  if (sortbuf != sortbuf_preallocated)
344
192k
    free (sortbuf);
345
346
192k
  *lengthp = length;
347
192k
  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
192k
}
Unexecuted instantiation: u8_normalize
u32_normalize
Line
Count
Source
21
192k
{
22
192k
  int (*decomposer) (ucs4_t uc, ucs4_t *decomposition) = nf->decomposer;
23
192k
  ucs4_t (*composer) (ucs4_t uc1, ucs4_t uc2) = nf->composer;
24
25
  /* The result being accumulated.  */
26
192k
  UNIT *result;
27
192k
  size_t allocated;
28
192k
  if (resultbuf == NULL)
29
192k
    {
30
192k
      result = NULL;
31
192k
      allocated = 0;
32
192k
    }
33
0
  else
34
0
    {
35
0
      result = resultbuf;
36
0
      allocated = *lengthp;
37
0
    }
38
192k
  size_t length = 0;
39
40
  /* The buffer for sorting.  */
41
192k
  #define SORTBUF_PREALLOCATED 64
42
192k
  struct ucs4_with_ccc sortbuf_preallocated[2 * SORTBUF_PREALLOCATED];
43
192k
  struct ucs4_with_ccc *sortbuf = /* array of size 2 * sortbuf_allocated */
44
192k
    sortbuf_preallocated;
45
192k
  size_t sortbuf_allocated = SORTBUF_PREALLOCATED;
46
192k
  size_t sortbuf_count = 0;
47
48
192k
  {
49
192k
    const UNIT *s_end = s + n;
50
51
192k
    for (;;)
52
2.18M
      {
53
2.18M
        int count;
54
2.18M
        ucs4_t decomposed[UC_DECOMPOSITION_MAX_LENGTH];
55
2.18M
        int decomposed_count;
56
57
2.18M
        if (s < s_end)
58
1.99M
          {
59
            /* Fetch the next character.  */
60
1.99M
            count = U_MBTOUC_UNSAFE (&decomposed[0], s, s_end - s);
61
1.99M
            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
4.30M
            for (int curr = 0; curr < decomposed_count; )
70
2.30M
              {
71
                /* Invariant: decomposed[0..curr-1] is fully decomposed, i.e.
72
                   all elements are atomic.  */
73
2.30M
                ucs4_t curr_decomposed[UC_DECOMPOSITION_MAX_LENGTH];
74
2.30M
                int curr_decomposed_count;
75
76
2.30M
                curr_decomposed_count = decomposer (decomposed[curr], curr_decomposed);
77
2.30M
                if (curr_decomposed_count >= 0)
78
156k
                  {
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
156k
                    int shift = curr_decomposed_count - 1;
83
84
156k
                    if (shift < 0)
85
0
                      abort ();
86
156k
                    if (shift > 0)
87
154k
                      {
88
154k
                        decomposed_count += shift;
89
154k
                        if (decomposed_count > UC_DECOMPOSITION_MAX_LENGTH)
90
0
                          abort ();
91
167k
                        for (int j = decomposed_count - 1 - shift; j > curr; j--)
92
12.8k
                          decomposed[j + shift] = decomposed[j];
93
154k
                      }
94
467k
                    for (; shift >= 0; shift--)
95
311k
                      decomposed[curr + shift] = curr_decomposed[shift];
96
156k
                  }
97
2.15M
                else
98
2.15M
                  {
99
                    /* decomposed[curr] is atomic.  */
100
2.15M
                    curr++;
101
2.15M
                  }
102
2.30M
              }
103
1.99M
          }
104
192k
        else
105
192k
          {
106
192k
            count = 0;
107
192k
            decomposed_count = 0;
108
192k
          }
109
110
2.18M
        int i = 0;
111
2.18M
        for (;;)
112
4.33M
          {
113
4.33M
            ucs4_t uc;
114
4.33M
            int ccc;
115
116
4.33M
            if (s < s_end)
117
4.14M
              {
118
                /* Fetch the next character from the decomposition.  */
119
4.14M
                if (i == decomposed_count)
120
1.99M
                  break;
121
2.15M
                uc = decomposed[i];
122
2.15M
                ccc = uc_combining_class (uc);
123
2.15M
              }
124
192k
            else
125
192k
              {
126
                /* End of string reached.  */
127
192k
                uc = 0;
128
192k
                ccc = 0;
129
192k
              }
130
131
2.34M
            if (ccc == 0)
132
2.02M
              {
133
                /* Apply the canonical ordering algorithm to the accumulated
134
                   sequence of characters.  */
135
2.02M
                if (sortbuf_count > 1)
136
134k
                  gl_uninorm_decompose_merge_sort_inplace (sortbuf, sortbuf_count,
137
134k
                                                           sortbuf + sortbuf_count);
138
139
2.02M
                if (composer != NULL)
140
2.02M
                  {
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
2.02M
                    if (sortbuf_count > 0 && sortbuf[0].ccc == 0)
162
1.83M
                      {
163
2.09M
                        for (size_t j = 1; j < sortbuf_count; )
164
256k
                          {
165
256k
                            if (sortbuf[j].ccc > sortbuf[j - 1].ccc)
166
150k
                              {
167
150k
                                ucs4_t combined =
168
150k
                                  composer (sortbuf[0].code, sortbuf[j].code);
169
150k
                                if (combined)
170
124k
                                  {
171
124k
                                    sortbuf[0].code = combined;
172
                                    /* sortbuf[0].ccc = 0, still valid.  */
173
225k
                                    for (size_t k = j + 1; k < sortbuf_count; k++)
174
100k
                                      sortbuf[k - 1] = sortbuf[k];
175
124k
                                    sortbuf_count--;
176
124k
                                    continue;
177
124k
                                  }
178
150k
                              }
179
132k
                            j++;
180
132k
                          }
181
1.83M
                        if (s < s_end && sortbuf_count == 1)
182
1.63M
                          {
183
1.63M
                            ucs4_t combined =
184
1.63M
                              composer (sortbuf[0].code, uc);
185
1.63M
                            if (combined)
186
6.76k
                              {
187
6.76k
                                uc = combined;
188
6.76k
                                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
6.76k
                                sortbuf_count = 0;
193
6.76k
                              }
194
1.63M
                          }
195
1.83M
                      }
196
2.02M
                  }
197
198
4.04M
                for (size_t j = 0; j < sortbuf_count; j++)
199
2.01M
                  {
200
2.01M
                    ucs4_t muc = sortbuf[j].code;
201
202
                    /* Append muc to the result accumulator.  */
203
2.01M
                    if (length < allocated)
204
1.81M
                      {
205
1.81M
                        int ret =
206
1.81M
                          U_UCTOMB (result + length, muc, allocated - length);
207
1.81M
                        if (ret == -1)
208
0
                          {
209
0
                            errno = EINVAL;
210
0
                            goto fail;
211
0
                          }
212
1.81M
                        if (ret >= 0)
213
1.81M
                          {
214
1.81M
                            length += ret;
215
1.81M
                            goto done_appending;
216
1.81M
                          }
217
1.81M
                      }
218
198k
                    {
219
198k
                      size_t old_allocated = allocated;
220
198k
                      size_t new_allocated = 2 * old_allocated;
221
198k
                      if (new_allocated < 64)
222
190k
                        new_allocated = 64;
223
198k
                      if (new_allocated < old_allocated) /* integer overflow? */
224
0
                        abort ();
225
198k
                      {
226
198k
                        UNIT *larger_result;
227
198k
                        if (result == NULL)
228
190k
                          {
229
190k
                            larger_result =
230
190k
                              (UNIT *) malloc (new_allocated * sizeof (UNIT));
231
190k
                            if (larger_result == NULL)
232
0
                              {
233
0
                                errno = ENOMEM;
234
0
                                goto fail;
235
0
                              }
236
190k
                          }
237
7.91k
                        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.91k
                        else
249
7.91k
                          {
250
7.91k
                            larger_result =
251
7.91k
                              (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
252
7.91k
                            if (larger_result == NULL)
253
0
                              {
254
0
                                errno = ENOMEM;
255
0
                                goto fail;
256
0
                              }
257
7.91k
                          }
258
198k
                        result = larger_result;
259
198k
                        allocated = new_allocated;
260
198k
                        {
261
198k
                          int ret =
262
198k
                            U_UCTOMB (result + length, muc, allocated - length);
263
198k
                          if (ret == -1)
264
0
                            {
265
0
                              errno = EINVAL;
266
0
                              goto fail;
267
0
                            }
268
198k
                          if (ret < 0)
269
0
                            abort ();
270
198k
                          length += ret;
271
198k
                          goto done_appending;
272
198k
                        }
273
198k
                      }
274
198k
                    }
275
2.01M
                   done_appending: ;
276
2.01M
                  }
277
278
                /* sortbuf is now empty.  */
279
2.02M
                sortbuf_count = 0;
280
2.02M
              }
281
282
2.34M
            if (!(s < s_end))
283
              /* End of string reached.  */
284
192k
              break;
285
286
            /* Append (uc, ccc) to sortbuf.  */
287
2.15M
            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
1.39k
                  free (sortbuf);
303
1.39k
                sortbuf = new_sortbuf;
304
1.39k
              }
305
2.15M
            sortbuf[sortbuf_count].code = uc;
306
2.15M
            sortbuf[sortbuf_count].ccc = ccc;
307
2.15M
            sortbuf_count++;
308
309
2.15M
            i++;
310
2.15M
          }
311
312
2.18M
        if (!(s < s_end))
313
          /* End of string reached.  */
314
192k
          break;
315
316
1.99M
        s += count;
317
1.99M
      }
318
192k
  }
319
320
192k
  if (length == 0)
321
2.14k
    {
322
2.14k
      if (result == NULL)
323
2.14k
        {
324
          /* Return a non-NULL value.  NULL means error.  */
325
2.14k
          result = (UNIT *) malloc (1);
326
2.14k
          if (result == NULL)
327
0
            {
328
0
              errno = ENOMEM;
329
0
              goto fail;
330
0
            }
331
2.14k
        }
332
2.14k
    }
333
190k
  else if (result != resultbuf && length < allocated)
334
188k
    {
335
      /* Shrink the allocated memory if possible.  */
336
188k
      UNIT *memory = (UNIT *) realloc (result, length * sizeof (UNIT));
337
188k
      if (memory != NULL)
338
188k
        result = memory;
339
188k
    }
340
341
192k
  if (sortbuf_count > 0)
342
0
    abort ();
343
192k
  if (sortbuf != sortbuf_preallocated)
344
192k
    free (sortbuf);
345
346
192k
  *lengthp = length;
347
192k
  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
192k
}