1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "bsearch-insert-pos.h"
#include "unichar.h"
#include "unicodemap.c"
#define HANGUL_FIRST 0xac00
#define HANGUL_LAST 0xd7a3
const unsigned char utf8_replacement_char[UTF8_REPLACEMENT_CHAR_LEN] =
{ 0xef, 0xbf, 0xbd }; /* 0xfffd */
static const uint8_t utf8_non1_bytes[256 - 192 - 2] = {
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
};
const uint8_t *const uni_utf8_non1_bytes = utf8_non1_bytes;
unsigned int uni_strlen(const unichar_t *str)
{
unsigned int len = 0;
for (len = 0; str[len] != 0; len++) ;
return len;
}
int uni_utf8_get_char(const char *input, unichar_t *chr_r)
{
return uni_utf8_get_char_n((const unsigned char *)input, SIZE_MAX,
chr_r);
}
int uni_utf8_get_char_n(const void *_input, size_t max_len, unichar_t *chr_r)
{
static unichar_t lowest_valid_chr_table[] =
{ 0, 0, 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
const unsigned char *input = _input;
unichar_t chr, lowest_valid_chr;
unsigned int i, len;
int ret;
i_assert(max_len > 0);
if (*input < 0x80) {
*chr_r = *input;
return 1;
}
/* first byte has len highest bits set, followed by zero bit.
the rest of the bits are used as the highest bits of the value. */
chr = *input;
len = uni_utf8_char_bytes(*input);
switch (len) {
case 2:
chr &= 0x1f;
break;
case 3:
chr &= 0x0f;
break;
case 4:
chr &= 0x07;
break;
case 5:
chr &= 0x03;
break;
case 6:
chr &= 0x01;
break;
default:
/* only 7bit chars should have len==1 */
i_assert(len == 1);
return -1;
}
if (len <= max_len) {
lowest_valid_chr = lowest_valid_chr_table[len];
ret = len;
} else {
/* check first if the input is invalid before returning 0 */
lowest_valid_chr = 0;
ret = 0;
len = max_len;
}
/* the following bytes must all be 10xxxxxx */
for (i = 1; i < len; i++) {
if ((input[i] & 0xc0) != 0x80)
return input[i] == '\0' ? 0 : -1;
chr <<= 6;
chr |= input[i] & 0x3f;
}
/* these are specified as invalid encodings by standards
see RFC3629 */
if (!uni_is_valid_ucs4(chr))
return -1;
if (chr < lowest_valid_chr) {
/* overlong encoding */
return -1;
}
*chr_r = chr;
return ret;
}
int uni_utf8_to_ucs4(const char *input, ARRAY_TYPE(unichars) *output)
{
unichar_t chr;
while (*input != '\0') {
int len = uni_utf8_get_char(input, &chr);
if (len <= 0) {
/* invalid input */
return -1;
}
input += len;
array_push_back(output, &chr);
}
return 0;
}
int uni_utf8_to_ucs4_n(const unsigned char *input, size_t size,
ARRAY_TYPE(unichars) *output)
{
unichar_t chr;
while (size > 0) {
int len = uni_utf8_get_char_n(input, size, &chr);
if (len <= 0)
return -1; /* invalid input */
input += len; size -= len;
array_push_back(output, &chr);
}
return 0;
}
void uni_ucs4_to_utf8(const unichar_t *input, size_t len, buffer_t *output)
{
for (; len > 0 && *input != '\0'; input++, len--)
uni_ucs4_to_utf8_c(*input, output);
}
void uni_ucs4_to_utf8_c(unichar_t chr, buffer_t *output)
{
unsigned char first;
int bitpos;
if (chr < 0x80) {
buffer_append_c(output, chr);
return;
}
i_assert(uni_is_valid_ucs4(chr));
if (chr < (1 << (6 + 5))) {
/* 110xxxxx */
bitpos = 6;
first = 0x80 | 0x40;
} else if (chr < (1 << ((2*6) + 4))) {
/* 1110xxxx */
bitpos = 2*6;
first = 0x80 | 0x40 | 0x20;
} else if (chr < (1 << ((3*6) + 3))) {
/* 11110xxx */
bitpos = 3*6;
first = 0x80 | 0x40 | 0x20 | 0x10;
} else if (chr < (1 << ((4*6) + 2))) {
/* 111110xx */
bitpos = 4*6;
first = 0x80 | 0x40 | 0x20 | 0x10 | 0x08;
} else {
/* 1111110x */
bitpos = 5*6;
first = 0x80 | 0x40 | 0x20 | 0x10 | 0x08 | 0x04;
}
buffer_append_c(output, first | (chr >> bitpos));
do {
bitpos -= 6;
buffer_append_c(output, 0x80 | ((chr >> bitpos) & 0x3f));
} while (bitpos > 0);
}
unsigned int uni_utf8_strlen(const char *input)
{
return uni_utf8_strlen_n(input, strlen(input));
}
unsigned int uni_utf8_strlen_n(const void *input, size_t size)
{
size_t partial_pos;
return uni_utf8_partial_strlen_n(input, size, &partial_pos);
}
unsigned int uni_utf8_partial_strlen_n(const void *_input, size_t size,
size_t *partial_pos_r)
{
const unsigned char *input = _input;
unsigned int count, len = 0;
size_t i;
for (i = 0; i < size; ) {
count = uni_utf8_char_bytes(input[i]);
if (i + count > size)
break;
i += count;
len++;
}
*partial_pos_r = i;
return len;
}
static bool uint16_find(const uint16_t *data, unsigned int count,
uint16_t value, unsigned int *idx_r)
{
BINARY_NUMBER_SEARCH(data, count, value, idx_r);
}
static bool uint32_find(const uint32_t *data, unsigned int count,
uint32_t value, unsigned int *idx_r)
{
BINARY_NUMBER_SEARCH(data, count, value, idx_r);
}
unichar_t uni_ucs4_to_titlecase(unichar_t chr)
{
unsigned int idx;
if (chr <= 0xff)
return titlecase8_map[chr];
else if (chr <= 0xffff) {
if (!uint16_find(titlecase16_keys, N_ELEMENTS(titlecase16_keys),
chr, &idx))
return chr;
else
return titlecase16_values[idx];
} else {
if (!uint32_find(titlecase32_keys, N_ELEMENTS(titlecase32_keys),
chr, &idx))
return chr;
else
return titlecase32_values[idx];
}
}
static bool uni_ucs4_decompose_uni(unichar_t *chr)
{
unsigned int idx;
if (*chr <= 0xff) {
if (uni8_decomp_map[*chr] == *chr)
return FALSE;
*chr = uni8_decomp_map[*chr];
} else if (*chr <= 0xffff) {
if (*chr < uni16_decomp_keys[0])
return FALSE;
if (!uint16_find(uni16_decomp_keys,
N_ELEMENTS(uni16_decomp_keys), *chr, &idx))
return FALSE;
*chr = uni16_decomp_values[idx];
} else {
if (!uint32_find(uni32_decomp_keys,
N_ELEMENTS(uni32_decomp_keys), *chr, &idx))
return FALSE;
*chr = uni32_decomp_values[idx];
}
return TRUE;
}
static void uni_ucs4_decompose_hangul_utf8(unichar_t chr, buffer_t *output)
{
#define SBase HANGUL_FIRST
#define LBase 0x1100
#define VBase 0x1161
#define TBase 0x11A7
#define LCount 19
#define VCount 21
#define TCount 28
#define NCount (VCount * TCount)
unsigned int SIndex = chr - SBase;
unichar_t L = LBase + SIndex / NCount;
unichar_t V = VBase + (SIndex % NCount) / TCount;
unichar_t T = TBase + SIndex % TCount;
uni_ucs4_to_utf8_c(L, output);
uni_ucs4_to_utf8_c(V, output);
if (T != TBase) uni_ucs4_to_utf8_c(T, output);
}
static bool uni_ucs4_decompose_multi_utf8(unichar_t chr, buffer_t *output)
{
const uint32_t *value;
unsigned int idx;
if (chr < multidecomp_keys[0] || chr > 0xffff)
return FALSE;
if (!uint32_find(multidecomp_keys, N_ELEMENTS(multidecomp_keys),
chr, &idx))
return FALSE;
value = &multidecomp_values[multidecomp_offsets[idx]];
for (; *value != 0; value++)
uni_ucs4_to_utf8_c(*value, output);
return TRUE;
}
static void output_add_replacement_char(buffer_t *output)
{
if (output->used >= UTF8_REPLACEMENT_CHAR_LEN &&
memcmp(CONST_PTR_OFFSET(output->data,
output->used - UTF8_REPLACEMENT_CHAR_LEN),
utf8_replacement_char, UTF8_REPLACEMENT_CHAR_LEN) == 0) {
/* don't add the replacement char multiple times */
return;
}
buffer_append(output, utf8_replacement_char, UTF8_REPLACEMENT_CHAR_LEN);
}
int uni_utf8_to_decomposed_titlecase(const void *_input, size_t size,
buffer_t *output)
{
const unsigned char *input = _input;
unichar_t chr;
int ret = 0;
while (size > 0) {
int bytes = uni_utf8_get_char_n(input, size, &chr);
if (bytes <= 0) {
/* invalid input. try the next byte. */
ret = -1;
input++; size--;
output_add_replacement_char(output);
continue;
}
input += bytes;
size -= bytes;
chr = uni_ucs4_to_titlecase(chr);
if (chr >= HANGUL_FIRST && chr <= HANGUL_LAST)
uni_ucs4_decompose_hangul_utf8(chr, output);
else if (uni_ucs4_decompose_uni(&chr) ||
!uni_ucs4_decompose_multi_utf8(chr, output))
uni_ucs4_to_utf8_c(chr, output);
}
return ret;
}
static inline unsigned int
is_valid_utf8_seq(const unsigned char *input, unsigned int size)
{
unichar_t chr;
int len = uni_utf8_get_char_n(input, size, &chr);
return len <= 0 ? 0 : len;
}
static int uni_utf8_find_invalid_pos(const unsigned char *input, size_t size,
size_t *pos_r)
{
size_t i, len;
/* find the first invalid utf8 sequence */
for (i = 0; i < size;) {
if (input[i] < 0x80)
i++;
else {
len = is_valid_utf8_seq(input + i, size-i);
if (unlikely(len == 0)) {
*pos_r = i;
return -1;
}
i += len;
}
}
return 0;
}
bool uni_utf8_get_valid_data(const unsigned char *input, size_t size,
buffer_t *buf)
{
size_t i, len;
if (uni_utf8_find_invalid_pos(input, size, &i) == 0)
return TRUE;
/* broken utf-8 input - skip the broken characters */
buffer_append(buf, input, i++);
output_add_replacement_char(buf);
while (i < size) {
if (input[i] < 0x80) {
buffer_append_c(buf, input[i++]);
continue;
}
len = is_valid_utf8_seq(input + i, size-i);
if (len == 0) {
i++;
output_add_replacement_char(buf);
continue;
}
buffer_append(buf, input + i, len);
i += len;
}
return FALSE;
}
bool uni_utf8_str_is_valid(const char *str)
{
size_t i;
return uni_utf8_find_invalid_pos((const unsigned char *)str,
strlen(str), &i) == 0;
}
bool uni_utf8_data_is_valid(const unsigned char *data, size_t size)
{
size_t i;
return uni_utf8_find_invalid_pos(data, size, &i) == 0;
}
size_t uni_utf8_data_truncate(const unsigned char *data, size_t old_size,
size_t max_new_size)
{
if (max_new_size >= old_size)
return old_size;
if (max_new_size == 0)
return 0;
if ((data[max_new_size] & 0x80) == 0)
return max_new_size;
while (max_new_size > 0 && (data[max_new_size-1] & 0xc0) == 0x80)
max_new_size--;
if (max_new_size > 0 && (data[max_new_size-1] & 0xc0) == 0xc0)
max_new_size--;
return max_new_size;
}
|