Coverage Report

Created: 2025-06-09 08:44

/src/gdal/netcdf-c-4.7.4/libdispatch/utf8proc.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */
2
/*
3
 *  Copyright (c) 2015 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
4
 *  Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
5
 *
6
 *  Permission is hereby granted, free of charge, to any person obtaining a
7
 *  copy of this software and associated documentation files (the "Software"),
8
 *  to deal in the Software without restriction, including without limitation
9
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 *  and/or sell copies of the Software, and to permit persons to whom the
11
 *  Software is furnished to do so, subject to the following conditions:
12
 *
13
 *  The above copyright notice and this permission notice shall be included in
14
 *  all copies or substantial portions of the Software.
15
 *
16
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 *  DEALINGS IN THE SOFTWARE.
23
 */
24
25
/*
26
 *  This library contains derived data from a modified version of the
27
 *  Unicode data files.
28
 *
29
 *  The original data files are available at
30
 *  http://www.unicode.org/Public/UNIDATA/
31
 *
32
 *  Please notice the copyright statement in the file "utf8proc_data.c".
33
 */
34
35
36
/*
37
 *  File name:    utf8proc.c
38
 *
39
 *  Description:
40
 *  Implementation of libutf8proc.
41
 */
42
43
44
#include "utf8proc.h"
45
#include "utf8proc_data.c"
46
47
48
/**
49
 * Array containing the byte lengths of a UTF-8 encoded codepoint based
50
 * on the first byte.
51
 */
52
const nc_utf8proc_int8_t nc_utf8proc_utf8class[256] = {
53
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
58
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
59
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
61
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
66
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
67
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
68
  4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
69
70
125M
#define UTF8PROC_HANGUL_SBASE 0xAC00
71
37.3M
#define UTF8PROC_HANGUL_LBASE 0x1100
72
3.02k
#define UTF8PROC_HANGUL_VBASE 0x1161
73
3.02k
#define UTF8PROC_HANGUL_TBASE 0x11A7
74
132k
#define UTF8PROC_HANGUL_LCOUNT 19
75
3.02k
#define UTF8PROC_HANGUL_VCOUNT 21
76
10.7k
#define UTF8PROC_HANGUL_TCOUNT 28
77
3.02k
#define UTF8PROC_HANGUL_NCOUNT 588
78
38.4M
#define UTF8PROC_HANGUL_SCOUNT 11172
79
/* END is exclusive */
80
#define UTF8PROC_HANGUL_L_START  0x1100
81
#define UTF8PROC_HANGUL_L_END    0x115A
82
#define UTF8PROC_HANGUL_L_FILLER 0x115F
83
#define UTF8PROC_HANGUL_V_START  0x1160
84
#define UTF8PROC_HANGUL_V_END    0x11A3
85
#define UTF8PROC_HANGUL_T_START  0x11A8
86
#define UTF8PROC_HANGUL_T_END    0x11FA
87
#define UTF8PROC_HANGUL_S_START  0xAC00
88
#define UTF8PROC_HANGUL_S_END    0xD7A4
89
90
/* Should follow semantic-versioning rules (semver.org) based on API
91
   compatibility.  (Note that the shared-library version number will
92
   be different, being based on ABI compatibility.): */
93
0
#define STRINGIZEx(x) #x
94
0
#define STRINGIZE(x) STRINGIZEx(x)
95
0
const char *nc_utf8proc_version(void) {
96
0
  return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) "";
97
0
}
98
99
0
 const char *nc_utf8proc_errmsg(nc_utf8proc_ssize_t errcode) {
100
0
  switch (errcode) {
101
0
    case UTF8PROC_ERROR_NOMEM:
102
0
    return "Memory for processing UTF-8 data could not be allocated.";
103
0
    case UTF8PROC_ERROR_OVERFLOW:
104
0
    return "UTF-8 string is too long to be processed.";
105
0
    case UTF8PROC_ERROR_INVALIDUTF8:
106
0
    return "Invalid UTF-8 string";
107
0
    case UTF8PROC_ERROR_NOTASSIGNED:
108
0
    return "Unassigned Unicode code point found in UTF-8 string.";
109
0
    case UTF8PROC_ERROR_INVALIDOPTS:
110
0
    return "Invalid options for UTF-8 processing chosen.";
111
0
    default:
112
0
    return "An unknown error occurred while processing UTF-8 data.";
113
0
  }
114
0
}
115
116
2.05M
#define utf_cont(ch)  (((ch) & 0xc0) == 0x80)
117
nc_utf8proc_ssize_t nc_utf8proc_iterate(
118
  const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen, nc_utf8proc_int32_t *dst
119
103M
) {
120
103M
  nc_utf8proc_uint32_t uc;
121
103M
  const nc_utf8proc_uint8_t *end;
122
123
103M
  *dst = -1;
124
103M
  if (!strlen) return 0;
125
103M
  end = str + ((strlen < 0) ? 4 : strlen);
126
103M
  uc = *str++;
127
103M
  if (uc < 0x80) {
128
102M
    *dst = uc;
129
102M
    return 1;
130
102M
  }
131
  /* Must be between 0xc2 and 0xf4 inclusive to be valid */
132
885k
  if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
133
656k
  if (uc < 0xe0) {         /* 2-byte sequence */
134
     /* Must have valid continuation character */
135
255k
     if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
136
126k
     *dst = ((uc & 0x1f)<<6) | (*str & 0x3f);
137
126k
     return 2;
138
255k
  }
139
400k
  if (uc < 0xf0) {        /* 3-byte sequence */
140
72.5k
     if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
141
37.1k
        return UTF8PROC_ERROR_INVALIDUTF8;
142
     /* Check for surrogate chars */
143
35.3k
     if (uc == 0xed && *str > 0x9f)
144
13
         return UTF8PROC_ERROR_INVALIDUTF8;
145
35.3k
     uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f);
146
35.3k
     if (uc < 0x800)
147
74
         return UTF8PROC_ERROR_INVALIDUTF8;
148
35.3k
     *dst = uc;
149
35.3k
     return 3;
150
35.3k
  }
151
  /* 4-byte sequence */
152
  /* Must have 3 valid continuation characters */
153
327k
  if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2]))
154
29.3k
     return UTF8PROC_ERROR_INVALIDUTF8;
155
  /* Make sure in correct range (0x10000 - 0x10ffff) */
156
298k
  if (uc == 0xf0) {
157
155k
    if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8;
158
155k
  } else if (uc == 0xf4) {
159
7
    if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8;
160
7
  }
161
298k
  *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f);
162
298k
  return 4;
163
298k
}
164
165
0
 nc_utf8proc_bool nc_utf8proc_codepoint_valid(nc_utf8proc_int32_t uc) {
166
0
    return (((nc_utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((nc_utf8proc_uint32_t)uc < 0x110000);
167
0
}
168
169
41.8M
 nc_utf8proc_ssize_t nc_utf8proc_encode_char(nc_utf8proc_int32_t uc, nc_utf8proc_uint8_t *dst) {
170
41.8M
  if (uc < 0x00) {
171
0
    return 0;
172
41.8M
  } else if (uc < 0x80) {
173
41.5M
    dst[0] = (nc_utf8proc_uint8_t) uc;
174
41.5M
    return 1;
175
41.5M
  } else if (uc < 0x800) {
176
13.9k
    dst[0] = (nc_utf8proc_uint8_t)(0xC0 + (uc >> 6));
177
13.9k
    dst[1] = (nc_utf8proc_uint8_t)(0x80 + (uc & 0x3F));
178
13.9k
    return 2;
179
  /* Note: we allow encoding 0xd800-0xdfff here, so as not to change */
180
  /* the API, however, these are actually invalid in UTF-8 */
181
320k
  } else if (uc < 0x10000) {
182
17.5k
    dst[0] = (nc_utf8proc_uint8_t)(0xE0 + (uc >> 12));
183
17.5k
    dst[1] = (nc_utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
184
17.5k
    dst[2] = (nc_utf8proc_uint8_t)(0x80 + (uc & 0x3F));
185
17.5k
    return 3;
186
302k
  } else if (uc < 0x110000) {
187
302k
    dst[0] = (nc_utf8proc_uint8_t)(0xF0 + (uc >> 18));
188
302k
    dst[1] = (nc_utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
189
302k
    dst[2] = (nc_utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
190
302k
    dst[3] = (nc_utf8proc_uint8_t)(0x80 + (uc & 0x3F));
191
302k
    return 4;
192
302k
  } else return 0;
193
41.8M
}
194
195
/* internal "unsafe" version that does not check whether uc is in range */
196
0
static nc_utf8proc_ssize_t nc_unsafe_encode_char(nc_utf8proc_int32_t uc, nc_utf8proc_uint8_t *dst) {
197
0
   if (uc < 0x00) {
198
0
      return 0;
199
0
   } else if (uc < 0x80) {
200
0
      dst[0] = (nc_utf8proc_uint8_t)uc;
201
0
      return 1;
202
0
   } else if (uc < 0x800) {
203
0
      dst[0] = (nc_utf8proc_uint8_t)(0xC0 + (uc >> 6));
204
0
      dst[1] = (nc_utf8proc_uint8_t)(0x80 + (uc & 0x3F));
205
0
      return 2;
206
0
   } else if (uc == 0xFFFF) {
207
0
       dst[0] = (nc_utf8proc_uint8_t)0xFF;
208
0
       return 1;
209
0
   } else if (uc == 0xFFFE) {
210
0
       dst[0] = (nc_utf8proc_uint8_t)0xFE;
211
0
       return 1;
212
0
   } else if (uc < 0x10000) {
213
0
      dst[0] = (nc_utf8proc_uint8_t)(0xE0 + (uc >> 12));
214
0
      dst[1] = (nc_utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
215
0
      dst[2] = (nc_utf8proc_uint8_t)(0x80 + (uc & 0x3F));
216
0
      return 3;
217
0
   } else if (uc < 0x110000) {
218
0
      dst[0] = (nc_utf8proc_uint8_t)(0xF0 + (uc >> 18));
219
0
      dst[1] = (nc_utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
220
0
      dst[2] = (nc_utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
221
0
      dst[3] = (nc_utf8proc_uint8_t)(0x80 + (uc & 0x3F));
222
0
      return 4;
223
0
   } else return 0;
224
0
}
225
226
/* internal "unsafe" version that does not check whether uc is in range */
227
241M
static const nc_utf8proc_property_t *nc_unsafe_get_property(nc_utf8proc_int32_t uc) {
228
  /* ASSERT: uc >= 0 && uc < 0x110000 */
229
241M
  return nc_utf8proc_properties + (
230
241M
    nc_utf8proc_stage2table[
231
241M
      nc_utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
232
241M
    ]
233
241M
  );
234
241M
}
235
236
0
 const nc_utf8proc_property_t *nc_utf8proc_get_property(nc_utf8proc_int32_t uc) {
237
0
  return uc < 0 || uc >= 0x110000 ? nc_utf8proc_properties : nc_unsafe_get_property(uc);
238
0
}
239
240
/* return whether there is a grapheme break between boundclasses lbc and tbc
241
   (according to the definition of extended grapheme clusters)
242
243
  Rule numbering refers to TR29 Version 29 (Unicode 9.0.0):
244
  http://www.unicode.org/reports/tr29/tr29-29.html
245
246
  CAVEATS:
247
   Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences)
248
   and GB 12/13 (regional indicator code points) require knowledge of previous characters
249
   and are thus not handled by this function. This may result in an incorrect break before
250
   an E_Modifier class codepoint and an incorrectly missing break between two
251
   REGIONAL_INDICATOR class code points if such support does not exist in the caller.
252
253
   See the special support in grapheme_break_extended, for required bookkeeping by the caller.
254
*/
255
0
static nc_utf8proc_bool nc_grapheme_break_simple(int lbc, int tbc) {
256
0
  return
257
0
    (lbc == UTF8PROC_BOUNDCLASS_START) ? true :       /* GB1 */
258
0
    (lbc == UTF8PROC_BOUNDCLASS_CR &&                 /* GB3 */
259
0
     tbc == UTF8PROC_BOUNDCLASS_LF) ? false :         /* --- */
260
0
    (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true :  /* GB4 */
261
0
    (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true :  /* GB5 */
262
0
    (lbc == UTF8PROC_BOUNDCLASS_L &&                  /* GB6 */
263
0
     (tbc == UTF8PROC_BOUNDCLASS_L ||                 /* --- */
264
0
      tbc == UTF8PROC_BOUNDCLASS_V ||                 /* --- */
265
0
      tbc == UTF8PROC_BOUNDCLASS_LV ||                /* --- */
266
0
      tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false :      /* --- */
267
0
    ((lbc == UTF8PROC_BOUNDCLASS_LV ||                /* GB7 */
268
0
      lbc == UTF8PROC_BOUNDCLASS_V) &&                /* --- */
269
0
     (tbc == UTF8PROC_BOUNDCLASS_V ||                 /* --- */
270
0
      tbc == UTF8PROC_BOUNDCLASS_T)) ? false :        /* --- */
271
0
    ((lbc == UTF8PROC_BOUNDCLASS_LVT ||               /* GB8 */
272
0
      lbc == UTF8PROC_BOUNDCLASS_T) &&                /* --- */
273
0
     tbc == UTF8PROC_BOUNDCLASS_T) ? false :          /* --- */
274
0
    (tbc == UTF8PROC_BOUNDCLASS_EXTEND ||             /* GB9 */
275
0
     tbc == UTF8PROC_BOUNDCLASS_ZWJ ||                /* --- */
276
0
     tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK ||        /* GB9a */
277
0
     lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false :    /* GB9b */
278
0
    ((lbc == UTF8PROC_BOUNDCLASS_E_BASE ||            /* GB10 (requires additional handling below) */
279
0
      lbc == UTF8PROC_BOUNDCLASS_E_BASE_GAZ) &&       /* ---- */
280
0
     tbc == UTF8PROC_BOUNDCLASS_E_MODIFIER) ? false : /* ---- */
281
0
    (lbc == UTF8PROC_BOUNDCLASS_ZWJ &&                         /* GB11 */
282
0
     (tbc == UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ ||             /* ---- */
283
0
      tbc == UTF8PROC_BOUNDCLASS_E_BASE_GAZ)) ? false :        /* ---- */
284
0
    (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR &&          /* GB12/13 (requires additional handling below) */
285
0
     tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false :  /* ---- */
286
0
    true; /* GB999 */
287
0
}
288
289
static nc_utf8proc_bool nc_grapheme_break_extended(int lbc, int tbc, nc_utf8proc_int32_t *state)
290
0
{
291
0
  int lbc_override = lbc;
292
0
  nc_utf8proc_bool break_permitted;
293
0
  if (state && *state != UTF8PROC_BOUNDCLASS_START)
294
0
    lbc_override = *state;
295
0
  break_permitted = nc_grapheme_break_simple(lbc_override, tbc);
296
0
  if (state) {
297
    /* Special support for GB 12/13 made possible by GB999. After two RI */
298
    /* class codepoints we want to force a break. Do this by resetting the */
299
    /* second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break */
300
    /* after that character according to GB999 (unless of course such a break is */
301
    /* forbidden by a different rule such as GB9). */
302
0
    if (*state == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR)
303
0
      *state = UTF8PROC_BOUNDCLASS_OTHER;
304
    /* Special support for GB10. Fold any EXTEND codepoints into the previous */
305
    /* boundclass if we're dealing with an emoji base boundclass. */
306
0
    else if ((*state == UTF8PROC_BOUNDCLASS_E_BASE      ||
307
0
              *state == UTF8PROC_BOUNDCLASS_E_BASE_GAZ) &&
308
0
             tbc == UTF8PROC_BOUNDCLASS_EXTEND)
309
0
      *state = UTF8PROC_BOUNDCLASS_E_BASE;
310
0
    else
311
0
      *state = tbc;
312
0
  }
313
0
  return break_permitted;
314
0
}
315
316
 nc_utf8proc_bool nc_utf8proc_grapheme_break_stateful(
317
0
    nc_utf8proc_int32_t c1, nc_utf8proc_int32_t c2, nc_utf8proc_int32_t *state) {
318
319
0
  return nc_grapheme_break_extended(nc_utf8proc_get_property(c1)->boundclass,
320
0
                                 nc_utf8proc_get_property(c2)->boundclass,
321
0
                                 state);
322
0
}
323
324
325
 nc_utf8proc_bool nc_utf8proc_grapheme_break(
326
0
    nc_utf8proc_int32_t c1, nc_utf8proc_int32_t c2) {
327
0
  return nc_utf8proc_grapheme_break_stateful(c1, c2, NULL);
328
0
}
329
330
static nc_utf8proc_int32_t nc_seqindex_decode_entry(const nc_utf8proc_uint16_t **entry)
331
629k
{
332
629k
  nc_utf8proc_int32_t entry_cp = **entry;
333
629k
  if ((entry_cp & 0xF800) == 0xD800) {
334
617k
    *entry = *entry + 1;
335
617k
    entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF);
336
617k
    entry_cp += 0x10000;
337
617k
  }
338
629k
  return entry_cp;
339
629k
}
340
341
static nc_utf8proc_int32_t nc_seqindex_decode_index(const nc_utf8proc_uint32_t seqindex)
342
0
{
343
0
  const nc_utf8proc_uint16_t *entry = &nc_utf8proc_sequences[seqindex];
344
0
  return nc_seqindex_decode_entry(&entry);
345
0
}
346
347
314k
static nc_utf8proc_ssize_t nc_seqindex_write_char_decomposed(nc_utf8proc_uint16_t seqindex, nc_utf8proc_int32_t *dst, nc_utf8proc_ssize_t bufsize, nc_utf8proc_option_t options, int *last_boundclass) {
348
314k
  nc_utf8proc_ssize_t written = 0;
349
314k
  const nc_utf8proc_uint16_t *entry = &nc_utf8proc_sequences[seqindex & 0x1FFF];
350
314k
  int len = seqindex >> 13;
351
314k
  if (len >= 7) {
352
0
    len = *entry;
353
0
    entry++;
354
0
  }
355
944k
  for (; len >= 0; entry++, len--) {
356
629k
    nc_utf8proc_int32_t entry_cp = nc_seqindex_decode_entry(&entry);
357
358
629k
    written += nc_utf8proc_decompose_char(entry_cp, dst+written,
359
629k
      (bufsize > written) ? (bufsize - written) : 0, options,
360
629k
    last_boundclass);
361
629k
    if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
362
629k
  }
363
314k
  return written;
364
314k
}
365
366
 nc_utf8proc_int32_t nc_utf8proc_tolower(nc_utf8proc_int32_t c)
367
0
{
368
0
  nc_utf8proc_int32_t cl = nc_utf8proc_get_property(c)->lowercase_seqindex;
369
0
  return cl != UINT16_MAX ? nc_seqindex_decode_index(cl) : c;
370
0
}
371
372
 nc_utf8proc_int32_t nc_utf8proc_toupper(nc_utf8proc_int32_t c)
373
0
{
374
0
  nc_utf8proc_int32_t cu = nc_utf8proc_get_property(c)->uppercase_seqindex;
375
0
  return cu != UINT16_MAX ? nc_seqindex_decode_index(cu) : c;
376
0
}
377
378
 nc_utf8proc_int32_t nc_utf8proc_totitle(nc_utf8proc_int32_t c)
379
0
{
380
0
  nc_utf8proc_int32_t cu = nc_utf8proc_get_property(c)->titlecase_seqindex;
381
0
  return cu != UINT16_MAX ? nc_seqindex_decode_index(cu) : c;
382
0
}
383
384
/* return a character width analogous to wcwidth (except portable and
385
   hopefully less buggy than most system wcwidth functions). */
386
0
 int nc_utf8proc_charwidth(nc_utf8proc_int32_t c) {
387
0
  return nc_utf8proc_get_property(c)->charwidth;
388
0
}
389
390
0
 nc_utf8proc_category_t nc_utf8proc_category(nc_utf8proc_int32_t c) {
391
0
  return nc_utf8proc_get_property(c)->category;
392
0
}
393
394
0
 const char *nc_utf8proc_category_string(nc_utf8proc_int32_t c) {
395
0
  static const char s[][3] = {"Cn","Lu","Ll","Lt","Lm","Lo","Mn","Mc","Me","Nd","Nl","No","Pc","Pd","Ps","Pe","Pi","Pf","Po","Sm","Sc","Sk","So","Zs","Zl","Zp","Cc","Cf","Cs","Co"};
396
0
  return s[nc_utf8proc_category(c)];
397
0
}
398
399
#define nc_utf8proc_decompose_lump(replacement_uc) \
400
0
  return nc_utf8proc_decompose_char((replacement_uc), dst, bufsize, \
401
0
  options & ~UTF8PROC_LUMP, last_boundclass)
402
403
87.7M
 nc_utf8proc_ssize_t nc_utf8proc_decompose_char(nc_utf8proc_int32_t uc, nc_utf8proc_int32_t *dst, nc_utf8proc_ssize_t bufsize, nc_utf8proc_option_t options, int *last_boundclass) {
404
87.7M
  const nc_utf8proc_property_t *property;
405
87.7M
  nc_utf8proc_propval_t category;
406
87.7M
  nc_utf8proc_int32_t hangul_sindex;
407
87.7M
  if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
408
87.7M
  property = nc_unsafe_get_property(uc);
409
87.7M
  category = property->category;
410
87.7M
  hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
411
87.7M
  if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
412
87.7M
    if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
413
3.15k
      nc_utf8proc_int32_t hangul_tindex;
414
3.15k
      if (bufsize >= 1) {
415
1.51k
        dst[0] = UTF8PROC_HANGUL_LBASE +
416
1.51k
          hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
417
1.51k
        if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
418
1.51k
          (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
419
1.51k
      }
420
3.15k
      hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
421
3.15k
      if (!hangul_tindex) return 2;
422
3.15k
      if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
423
3.15k
      return 3;
424
3.15k
    }
425
87.7M
  }
426
87.7M
  if (options & UTF8PROC_REJECTNA) {
427
0
    if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
428
0
  }
429
87.7M
  if (options & UTF8PROC_IGNORE) {
430
0
    if (property->ignorable) return 0;
431
0
  }
432
87.7M
  if (options & UTF8PROC_LUMP) {
433
0
    if (category == UTF8PROC_CATEGORY_ZS) nc_utf8proc_decompose_lump(0x0020);
434
0
    if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
435
0
      nc_utf8proc_decompose_lump(0x0027);
436
0
    if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
437
0
      nc_utf8proc_decompose_lump(0x002D);
438
0
    if (uc == 0x2044 || uc == 0x2215) nc_utf8proc_decompose_lump(0x002F);
439
0
    if (uc == 0x2236) nc_utf8proc_decompose_lump(0x003A);
440
0
    if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
441
0
      nc_utf8proc_decompose_lump(0x003C);
442
0
    if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
443
0
      nc_utf8proc_decompose_lump(0x003E);
444
0
    if (uc == 0x2216) nc_utf8proc_decompose_lump(0x005C);
445
0
    if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
446
0
      nc_utf8proc_decompose_lump(0x005E);
447
0
    if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
448
0
      nc_utf8proc_decompose_lump(0x005F);
449
0
    if (uc == 0x02CB) nc_utf8proc_decompose_lump(0x0060);
450
0
    if (uc == 0x2223) nc_utf8proc_decompose_lump(0x007C);
451
0
    if (uc == 0x223C) nc_utf8proc_decompose_lump(0x007E);
452
0
    if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
453
0
      if (category == UTF8PROC_CATEGORY_ZL ||
454
0
          category == UTF8PROC_CATEGORY_ZP)
455
0
        nc_utf8proc_decompose_lump(0x000A);
456
0
    }
457
0
  }
458
87.7M
  if (options & UTF8PROC_STRIPMARK) {
459
0
    if (category == UTF8PROC_CATEGORY_MN ||
460
0
      category == UTF8PROC_CATEGORY_MC ||
461
0
      category == UTF8PROC_CATEGORY_ME) return 0;
462
0
  }
463
87.7M
  if (options & UTF8PROC_CASEFOLD) {
464
0
    if (property->casefold_seqindex != UINT16_MAX) {
465
0
      return nc_seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass);
466
0
    }
467
0
  }
468
87.7M
  if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
469
87.7M
    if (property->decomp_seqindex != UINT16_MAX &&
470
87.7M
        (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
471
314k
      return nc_seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass);
472
314k
    }
473
87.7M
  }
474
87.4M
  if (options & UTF8PROC_CHARBOUND) {
475
0
    nc_utf8proc_bool boundary;
476
0
    int tbc = property->boundclass;
477
0
    boundary = nc_grapheme_break_extended(*last_boundclass, tbc, last_boundclass);
478
0
    if (boundary) {
479
0
      if (bufsize >= 1) dst[0] = 0xFFFF;
480
0
      if (bufsize >= 2) dst[1] = uc;
481
0
      return 2;
482
0
    }
483
0
  }
484
87.4M
  if (bufsize >= 1) *dst = uc;
485
87.4M
  return 1;
486
87.4M
}
487
488
 nc_utf8proc_ssize_t nc_utf8proc_decompose(
489
  const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen,
490
  nc_utf8proc_int32_t *buffer, nc_utf8proc_ssize_t bufsize, nc_utf8proc_option_t options
491
0
) {
492
0
    return nc_utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL);
493
0
}
494
495
 nc_utf8proc_ssize_t nc_utf8proc_decompose_custom(
496
  const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen,
497
  nc_utf8proc_int32_t *buffer, nc_utf8proc_ssize_t bufsize, nc_utf8proc_option_t options,
498
  nc_utf8proc_custom_func custom_func, void *custom_data
499
9.44M
) {
500
  /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
501
9.44M
  nc_utf8proc_ssize_t wpos = 0;
502
9.44M
  if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
503
0
    return UTF8PROC_ERROR_INVALIDOPTS;
504
9.44M
  if ((options & UTF8PROC_STRIPMARK) &&
505
9.44M
      !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
506
0
    return UTF8PROC_ERROR_INVALIDOPTS;
507
9.44M
  {
508
9.44M
    nc_utf8proc_int32_t uc;
509
9.44M
    nc_utf8proc_ssize_t rpos = 0;
510
9.44M
    nc_utf8proc_ssize_t decomp_result;
511
9.44M
    int boundclass = UTF8PROC_BOUNDCLASS_START;
512
96.5M
    while (1) {
513
96.5M
      if (options & UTF8PROC_NULLTERM) {
514
96.5M
        rpos += nc_utf8proc_iterate(str + rpos, -1, &uc);
515
        /* checking of return value is not necessary,
516
           as 'uc' is < 0 in case of error */
517
96.5M
        if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
518
96.1M
        if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
519
96.1M
        if (uc == 0) break;
520
96.1M
      } else {
521
0
        if (rpos >= strlen) break;
522
0
        rpos += nc_utf8proc_iterate(str + rpos, strlen - rpos, &uc);
523
0
        if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
524
0
      }
525
87.1M
      if (custom_func != NULL) {
526
0
        uc = custom_func(uc, custom_data);   /* user-specified custom mapping */
527
0
      }
528
87.1M
      decomp_result = nc_utf8proc_decompose_char(
529
87.1M
        uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
530
87.1M
        &boundclass
531
87.1M
      );
532
87.1M
      if (decomp_result < 0) return decomp_result;
533
87.1M
      wpos += decomp_result;
534
      /* prohibiting integer overflows due to too long strings: */
535
87.1M
      if (wpos < 0 ||
536
87.1M
          wpos > (nc_utf8proc_ssize_t)(SSIZE_MAX/sizeof(nc_utf8proc_int32_t)/2))
537
0
        return UTF8PROC_ERROR_OVERFLOW;
538
87.1M
    }
539
9.44M
  }
540
9.01M
  if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
541
4.63M
    nc_utf8proc_ssize_t pos = 0;
542
42.1M
    while (pos < wpos-1) {
543
37.4M
      nc_utf8proc_int32_t uc1, uc2;
544
37.4M
      const nc_utf8proc_property_t *property1, *property2;
545
37.4M
      uc1 = buffer[pos];
546
37.4M
      uc2 = buffer[pos+1];
547
37.4M
      property1 = nc_unsafe_get_property(uc1);
548
37.4M
      property2 = nc_unsafe_get_property(uc2);
549
37.4M
      if (property1->combining_class > property2->combining_class &&
550
37.4M
          property2->combining_class > 0) {
551
4.36k
        buffer[pos] = uc2;
552
4.36k
        buffer[pos+1] = uc1;
553
4.36k
        if (pos > 0) pos--; else pos++;
554
37.4M
      } else {
555
37.4M
        pos++;
556
37.4M
      }
557
37.4M
    }
558
4.63M
  }
559
9.01M
  return wpos;
560
9.44M
}
561
562
4.50M
 nc_utf8proc_ssize_t nc_utf8proc_normalize_utf32(nc_utf8proc_int32_t *buffer, nc_utf8proc_ssize_t length, nc_utf8proc_option_t options) {
563
  /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */
564
4.50M
  if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
565
0
    nc_utf8proc_ssize_t rpos;
566
0
    nc_utf8proc_ssize_t wpos = 0;
567
0
    nc_utf8proc_int32_t uc;
568
0
    for (rpos = 0; rpos < length; rpos++) {
569
0
      uc = buffer[rpos];
570
0
      if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
571
0
      if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
572
0
          ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
573
0
        if (options & UTF8PROC_NLF2LS) {
574
0
          if (options & UTF8PROC_NLF2PS) {
575
0
            buffer[wpos++] = 0x000A;
576
0
          } else {
577
0
            buffer[wpos++] = 0x2028;
578
0
          }
579
0
        } else {
580
0
          if (options & UTF8PROC_NLF2PS) {
581
0
            buffer[wpos++] = 0x2029;
582
0
          } else {
583
0
            buffer[wpos++] = 0x0020;
584
0
          }
585
0
        }
586
0
      } else if ((options & UTF8PROC_STRIPCC) &&
587
0
          (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
588
0
        if (uc == 0x0009) buffer[wpos++] = 0x0020;
589
0
      } else {
590
0
        buffer[wpos++] = uc;
591
0
      }
592
0
    }
593
0
    length = wpos;
594
0
  }
595
4.50M
  if (options & UTF8PROC_COMPOSE) {
596
4.50M
    nc_utf8proc_int32_t *starter = NULL;
597
4.50M
    nc_utf8proc_int32_t current_char;
598
4.50M
    const nc_utf8proc_property_t *starter_property = NULL, *current_property;
599
4.50M
    nc_utf8proc_propval_t max_combining_class = -1;
600
4.50M
    nc_utf8proc_ssize_t rpos;
601
4.50M
    nc_utf8proc_ssize_t wpos = 0;
602
4.50M
    nc_utf8proc_int32_t composition;
603
46.3M
    for (rpos = 0; rpos < length; rpos++) {
604
41.8M
      current_char = buffer[rpos];
605
41.8M
      current_property = nc_unsafe_get_property(current_char);
606
41.8M
      if (starter && current_property->combining_class > max_combining_class) {
607
        /* combination perhaps possible */
608
37.3M
        nc_utf8proc_int32_t hangul_lindex;
609
37.3M
        nc_utf8proc_int32_t hangul_sindex;
610
37.3M
        hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
611
37.3M
        if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
612
1.51k
          nc_utf8proc_int32_t hangul_vindex;
613
1.51k
          hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
614
1.51k
          if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
615
1.51k
            *starter = UTF8PROC_HANGUL_SBASE +
616
1.51k
              (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
617
1.51k
              UTF8PROC_HANGUL_TCOUNT;
618
1.51k
            starter_property = NULL;
619
1.51k
            continue;
620
1.51k
          }
621
1.51k
        }
622
37.3M
        hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
623
37.3M
        if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
624
37.3M
            (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
625
1.51k
          nc_utf8proc_int32_t hangul_tindex;
626
1.51k
          hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
627
1.51k
          if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
628
1.51k
            *starter += hangul_tindex;
629
1.51k
            starter_property = NULL;
630
1.51k
            continue;
631
1.51k
          }
632
1.51k
        }
633
37.3M
        if (!starter_property) {
634
37.3M
          starter_property = nc_unsafe_get_property(*starter);
635
37.3M
        }
636
37.3M
        if (starter_property->comb_index < 0x8000 &&
637
37.3M
            current_property->comb_index != UINT16_MAX &&
638
37.3M
            current_property->comb_index >= 0x8000) {
639
80.1k
          int sidx = starter_property->comb_index;
640
80.1k
          int idx = (current_property->comb_index & 0x3FFF) - nc_utf8proc_combinations[sidx];
641
80.1k
          if (idx >= 0 && idx <= nc_utf8proc_combinations[sidx + 1] ) {
642
80.0k
            idx += sidx + 2;
643
80.0k
            if (current_property->comb_index & 0x4000) {
644
76.9k
              composition = (nc_utf8proc_combinations[idx] << 16) | nc_utf8proc_combinations[idx+1];
645
76.9k
            } else
646
3.07k
              composition = nc_utf8proc_combinations[idx];
647
648
80.0k
            if (composition > 0 && (!(options & UTF8PROC_STABLE) ||
649
79.9k
                !(nc_unsafe_get_property(composition)->comp_exclusion))) {
650
3.05k
              *starter = composition;
651
3.05k
              starter_property = NULL;
652
3.05k
              continue;
653
3.05k
            }
654
80.0k
          }
655
80.1k
        }
656
37.3M
      }
657
41.8M
      buffer[wpos] = current_char;
658
41.8M
      if (current_property->combining_class) {
659
162k
        if (current_property->combining_class > max_combining_class) {
660
85.8k
          max_combining_class = current_property->combining_class;
661
85.8k
        }
662
41.6M
      } else {
663
41.6M
        starter = buffer + wpos;
664
41.6M
        starter_property = NULL;
665
41.6M
        max_combining_class = -1;
666
41.6M
      }
667
41.8M
      wpos++;
668
41.8M
    }
669
4.50M
    length = wpos;
670
4.50M
  }
671
4.50M
  return length;
672
4.50M
}
673
674
4.50M
 nc_utf8proc_ssize_t nc_utf8proc_reencode(nc_utf8proc_int32_t *buffer, nc_utf8proc_ssize_t length, nc_utf8proc_option_t options) {
675
  /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
676
     ASSERT: 'buffer' has one spare byte of free space at the end! */
677
4.50M
  length = nc_utf8proc_normalize_utf32(buffer, length, options);
678
4.50M
  if (length < 0) return length;
679
4.50M
  {
680
4.50M
    nc_utf8proc_ssize_t rpos, wpos = 0;
681
4.50M
    nc_utf8proc_int32_t uc;
682
4.50M
    if (options & UTF8PROC_CHARBOUND) {
683
0
        for (rpos = 0; rpos < length; rpos++) {
684
0
            uc = buffer[rpos];
685
0
            wpos += nc_unsafe_encode_char(uc, ((nc_utf8proc_uint8_t *)buffer) + wpos);
686
0
        }
687
4.50M
    } else {
688
46.3M
        for (rpos = 0; rpos < length; rpos++) {
689
41.8M
            uc = buffer[rpos];
690
41.8M
            wpos += nc_utf8proc_encode_char(uc, ((nc_utf8proc_uint8_t *)buffer) + wpos);
691
41.8M
        }
692
4.50M
    }
693
4.50M
    ((nc_utf8proc_uint8_t *)buffer)[wpos] = 0;
694
4.50M
    return wpos;
695
4.50M
  }
696
4.50M
}
697
698
 nc_utf8proc_ssize_t nc_utf8proc_map(
699
  const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen, nc_utf8proc_uint8_t **dstptr, nc_utf8proc_option_t options
700
4.93M
) {
701
4.93M
    return nc_utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL);
702
4.93M
}
703
704
 nc_utf8proc_ssize_t nc_utf8proc_map_custom(
705
  const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen, nc_utf8proc_uint8_t **dstptr, nc_utf8proc_option_t options,
706
  nc_utf8proc_custom_func custom_func, void *custom_data
707
4.93M
) {
708
4.93M
  nc_utf8proc_int32_t *buffer;
709
4.93M
  nc_utf8proc_ssize_t result;
710
4.93M
  *dstptr = NULL;
711
4.93M
  result = nc_utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
712
4.93M
  if (result < 0) return result;
713
4.50M
  buffer = (nc_utf8proc_int32_t *) malloc(result * sizeof(nc_utf8proc_int32_t) + 1);
714
4.50M
  if (!buffer) return UTF8PROC_ERROR_NOMEM;
715
4.50M
  result = nc_utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
716
4.50M
  if (result < 0) {
717
0
    free(buffer);
718
0
    return result;
719
0
  }
720
4.50M
  result = nc_utf8proc_reencode(buffer, result, options);
721
4.50M
  if (result < 0) {
722
0
    free(buffer);
723
0
    return result;
724
0
  }
725
4.50M
  {
726
4.50M
    nc_utf8proc_int32_t *newptr;
727
4.50M
    newptr = (nc_utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
728
4.50M
    if (newptr) buffer = newptr;
729
4.50M
  }
730
4.50M
  *dstptr = (nc_utf8proc_uint8_t *)buffer;
731
4.50M
  return result;
732
4.50M
}
733
734
0
 nc_utf8proc_uint8_t *nc_utf8proc_NFD(const nc_utf8proc_uint8_t *str) {
735
0
  nc_utf8proc_uint8_t *retval;
736
0
  nc_utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
737
0
    UTF8PROC_DECOMPOSE);
738
0
  return retval;
739
0
}
740
741
0
 nc_utf8proc_uint8_t *nc_utf8proc_NFC(const nc_utf8proc_uint8_t *str) {
742
0
  nc_utf8proc_uint8_t *retval;
743
0
  nc_utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
744
0
    UTF8PROC_COMPOSE);
745
0
  return retval;
746
0
}
747
748
0
 nc_utf8proc_uint8_t *nc_utf8proc_NFKD(const nc_utf8proc_uint8_t *str) {
749
0
  nc_utf8proc_uint8_t *retval;
750
0
  nc_utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
751
0
    UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
752
0
  return retval;
753
0
}
754
755
0
 nc_utf8proc_uint8_t *nc_utf8proc_NFKC(const nc_utf8proc_uint8_t *str) {
756
0
  nc_utf8proc_uint8_t *retval;
757
0
  nc_utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
758
0
    UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
759
0
  return retval;
760
0
}