-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcutf.cpp
498 lines (430 loc) · 13.5 KB
/
cutf.cpp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
Copyright 2014, Jason Kozak
Copyright 2019, Tarmo Pikaro
Based on UTF8-CPP: Copyright 2006, Nemanja Trifunovicm
*/
#include "cutf.h"
#define CUTF_LEAD_SURROGATE_MIN 0xd800u
#define CUTF_LEAD_SURROGATE_MAX 0xdbffu
#define CUTF_TRAIL_SURROGATE_MIN 0xdc00u
#define CUTF_TRAIL_SURROGATE_MAX 0xdfffu
#define CUTF_LEAD_OFFSET (CUTF_LEAD_SURROGATE_MIN - (0x10000 >> 10))
#define CUTF_SURROGATE_OFFSET (0x10000u - (CUTF_LEAD_SURROGATE_MIN << 10) - CUTF_TRAIL_SURROGATE_MIN)
#define CUTF_CODE_POINT_MAX 0x0010ffffu // Maximum valid value for a Unicode code point
#define CUTF_MASK8(oc) ((uint8_t)(0xff & (oc)))
#define CUTF_MASK16(oc) ((uint16_t)(0xffff & (oc)))
#define CUTF_IS_TRAIL(oc) ((CUTF_MASK8(oc) >> 6) == 0x2)
#define CUTF_IS_LEAD_SURROGATE(cp) ((cp) >= CUTF_LEAD_SURROGATE_MIN && (cp) <= CUTF_LEAD_SURROGATE_MAX)
#define CUTF_IS_TRAIL_SURROGATE(cp) ((cp) >= CUTF_TRAIL_SURROGATE_MIN && (cp) <= CUTF_TRAIL_SURROGATE_MAX)
#define CUTF_IS_SURROGATE(cp) ((cp) >= CUTF_LEAD_SURROGATE_MIN && (cp) <= CUTF_TRAIL_SURROGATE_MAX)
#define CUTF_IS_CODE_POINT_VALID(cp) ((cp) <= CUTF_CODE_POINT_MAX && !CUTF_IS_SURROGATE(cp))
#define CUTF_INC_OR_RETURN(IT, END) {enum cutf_error ret = cutf_safe_inc((IT),(END)); if(ret != CUTF_OK) return ret;}
#define CUTF_INC_IT_PTR(it) (++(*it))
#define CUTF_DEC_IT_PTR(it) (--(*it))
enum cutf_error
{
CUTF_OK,
CUTF_INVALID_LEAD,
CUTF_TRUNCATED_SEQUENCE,
CUTF_INCOMPLETE_SEQUENCE,
CUTF_OVERLONG_SEQUENCE,
CUTF_INVALID_CODE_POINT
};
static size_t cutf_codepoint_length(uint32_t cp)
{
if(cp < 0x80)
return 1;
else if(cp < 0x800)
return 2;
else if(cp < 0x10000)
return 3;
else
return 4;
}
static size_t cutf_sequence_length(uint8_t* lead_it)
{
uint8_t lead = CUTF_MASK8(*lead_it);
if(lead < 0x80) {
return 1;
} else if((lead >> 5) == 0x6) {
return 2;
} else if((lead >> 4) == 0xe) {
return 3;
} else if((lead >> 3) == 0x1e) {
return 4;
}
return 0;
}
static int cutf_is_overlong_sequence(uint32_t cp, size_t length)
{
if(cp < 0x80) {
return length != 1;
} else if(cp < 0x800) {
return length != 2;
} else if(cp < 0x10000) {
return length != 3;
}
return 0;
}
static enum cutf_error cutf_safe_inc(uint8_t** it, uint8_t* end)
{
if(CUTF_INC_IT_PTR(it) == end) {
return CUTF_TRUNCATED_SEQUENCE;
} else if(!CUTF_IS_TRAIL(**it)) {
return CUTF_INCOMPLETE_SEQUENCE;
}
return CUTF_OK;
}
/// get_sequence_x functions decode utf-8 sequences of the length x
static enum cutf_error cutf_get_sequence_1(uint8_t** it, uint8_t* end, uint32_t* code_point)
{
if(*it == end) {
return CUTF_TRUNCATED_SEQUENCE;
}
*code_point = CUTF_MASK8(**it);
return CUTF_OK;
}
static enum cutf_error cutf_get_sequence_2(uint8_t** it, uint8_t* end, uint32_t* code_point)
{
if(*it == end) {
return CUTF_TRUNCATED_SEQUENCE;
}
*code_point = CUTF_MASK8(**it);
CUTF_INC_OR_RETURN(it, end)
*code_point = ((*code_point << 6) & 0x7ff) + ((**it) & 0x3f);
return CUTF_OK;
}
static enum cutf_error cutf_get_sequence_3(uint8_t** it, uint8_t* end, uint32_t* code_point)
{
if(*it == end) {
return CUTF_TRUNCATED_SEQUENCE;
}
*code_point = CUTF_MASK8(**it);
CUTF_INC_OR_RETURN(it, end)
*code_point = ((*code_point << 12) & 0xffff) + ((CUTF_MASK8(**it) << 6) & 0xfff);
CUTF_INC_OR_RETURN(it, end)
*code_point += (**it) & 0x3f;
return CUTF_OK;
}
static enum cutf_error cutf_get_sequence_4(uint8_t** it, uint8_t* end, uint32_t* code_point)
{
if(*it == end) {
return CUTF_TRUNCATED_SEQUENCE;
}
*code_point = CUTF_MASK8(**it);
CUTF_INC_OR_RETURN(it, end)
*code_point = ((*code_point << 18) & 0x1fffff) + ((CUTF_MASK8(**it) << 12) & 0x3ffff);
CUTF_INC_OR_RETURN(it, end)
*code_point += (CUTF_MASK8(**it) << 6) & 0xfff;
CUTF_INC_OR_RETURN(it, end)
*code_point += (**it) & 0x3f;
return CUTF_OK;
}
static enum cutf_error cutf_get_next(uint8_t** it, uint8_t* end, uint32_t* code_point)
{
// Determine the sequence length based on the lead octet
const size_t length = cutf_sequence_length(*it);
uint8_t* original_it = *it;
uint32_t cp = 0;
enum cutf_error err = CUTF_OK;
// Get trail octets and calculate the code point
switch(length) {
case 0: return CUTF_INVALID_LEAD;
case 1: err = cutf_get_sequence_1(it, end, &cp); break;
case 2: err = cutf_get_sequence_2(it, end, &cp); break;
case 3: err = cutf_get_sequence_3(it, end, &cp); break;
case 4: err = cutf_get_sequence_4(it, end, &cp); break;
}
if(err == CUTF_OK) {
if(!CUTF_IS_CODE_POINT_VALID(cp)) {
err = CUTF_INVALID_CODE_POINT;
} else if(cutf_is_overlong_sequence(cp, length)) {
err = CUTF_OVERLONG_SEQUENCE;
} else {
// Passed! Return here.
if(code_point) {
*code_point = cp;
}
CUTF_INC_IT_PTR(it);
return CUTF_OK;
}
}
*it = original_it;
return err;
}
static enum cutf_error cutf_validate_next(uint8_t** it, uint8_t* end)
{
return cutf_get_next(it, end, 0);
}
#ifdef __cplusplus
extern "C"
{
#endif
uint8_t* cutf_find_invalid(uint8_t* start, uint8_t* end)
{
uint8_t* result = start;
while(result != end) {
enum cutf_error err_code = cutf_validate_next(&result, end);
if(err_code != CUTF_OK)
return result;
}
return result;
}
int cutf_is_valid(uint8_t* start, uint8_t* end)
{
return (cutf_find_invalid(start, end) == end);
}
int cutf_starts_with_bom(uint8_t* start, uint8_t* end)
{
const uint8_t bom[] = {0xef, 0xbb, 0xbf};
return end - start >= 3 && start[0] == bom[0] && start[1] == bom[1] && start[2] == bom[2];
}
uint8_t* cutf_append(uint32_t cp, uint8_t* result, size_t* remain)
{
size_t charlen = cutf_codepoint_length(cp);
// If we ran out of buffer size, then we don't fill it anymore, but continue iterating to get correct length
if (*remain < charlen)
{
*remain = 0;
return result + charlen;
}
if (cp < 0x80) // one octet
{
*(result++) = (uint8_t)(cp);
}
else if(cp < 0x800) { // two octets
*(result++) = (uint8_t)((cp >> 6) | 0xc0);
*(result++) = (uint8_t)((cp & 0x3f) | 0x80);
}
else if(cp < 0x10000) { // three octets
*(result++) = (uint8_t)((cp >> 12) | 0xe0);
*(result++) = (uint8_t)(((cp >> 6) & 0x3f) | 0x80);
*(result++) = (uint8_t)((cp & 0x3f) | 0x80);
}
else { // four octets
*(result++) = (uint8_t)((cp >> 18) | 0xf0);
*(result++) = (uint8_t)(((cp >> 12) & 0x3f)| 0x80);
*(result++) = (uint8_t)(((cp >> 6) & 0x3f) | 0x80);
*(result++) = (uint8_t)((cp & 0x3f) | 0x80);
}
return result;
}
uint32_t cutf_next(uint8_t** it, size_t* remain)
{
uint32_t cp = CUTF_MASK8(**it);
size_t length = cutf_sequence_length(*it);
if(remain)
*remain -= length;
switch (length) {
case 1:
break;
case 2:
CUTF_INC_IT_PTR(it);
cp = ((cp << 6) & 0x7ff) + ((**it) & 0x3f);
break;
case 3:
CUTF_INC_IT_PTR(it);
cp = ((cp << 12) & 0xffff) + ((CUTF_MASK8(**it) << 6) & 0xfff);
CUTF_INC_IT_PTR(it);
cp += (**it) & 0x3f;
break;
case 4:
CUTF_INC_IT_PTR(it);
cp = ((cp << 18) & 0x1fffff) + ((CUTF_MASK8(**it) << 12) & 0x3ffff);
CUTF_INC_IT_PTR(it);
cp += (CUTF_MASK8(**it) << 6) & 0xfff;
CUTF_INC_IT_PTR(it);
cp += (**it) & 0x3f;
break;
}
CUTF_INC_IT_PTR(it);
return cp;
}
uint32_t cutf_peek_next(uint8_t* it)
{
return cutf_next(&it, nullptr);
}
uint32_t cutf_prior(uint8_t** it)
{
for(CUTF_DEC_IT_PTR(it); CUTF_IS_TRAIL(**it); CUTF_DEC_IT_PTR(it)) {}
return cutf_peek_next(*it);
}
size_t cutf_distance(uint8_t* first, uint8_t* last)
{
size_t dist;
for(dist = 0; first < last; ++dist) {
cutf_next(&first, nullptr);
}
return dist;
}
#define PTR_DIFF_(it, out) (size_t)(it - out)
size_t cutf_16to8(uint16_t* start, uint16_t* end, uint8_t* out, size_t outsize)
{
uint8_t* it = out;
while(start != end) {
uint32_t cp = CUTF_MASK16(*start);
++start;
// Take care of surrogate pairs first
if(CUTF_IS_LEAD_SURROGATE(cp)) {
uint32_t trail_surrogate = CUTF_MASK16(*start);
++start;
cp = (cp << 10) + trail_surrogate + CUTF_SURROGATE_OFFSET;
}
it = cutf_append(cp, it, &outsize);
}
if(outsize != 0)
*it = 0; //Zero terminate
it++;
return PTR_DIFF_(it,out);
}
size_t cutf_8to16(uint8_t* start, uint8_t* end, uint16_t* out, size_t outsize)
{
uint16_t* it = out;
while(start < end) {
uint32_t cp = cutf_next(&start, &outsize);
if(cp > 0xffff) { //make a surrogate pair
*(it++) = (uint16_t)((cp >> 10) + CUTF_LEAD_OFFSET);
*(it++) = (uint16_t)((cp & 0x3ff) + CUTF_TRAIL_SURROGATE_MIN);
} else {
*(it++) = (uint16_t)(cp);
}
}
if (outsize != 0)
*it = 0; //Zero terminate
it++;
return PTR_DIFF_(it, out);
}
size_t cutf_32to8(uint32_t* start, uint32_t* end, uint8_t* out, size_t outsize)
{
uint8_t* it = out;
for(; start != end; ++start)
{
it = cutf_append(*start, it, &outsize);
}
if( outsize != 0)
*it = 0; //Zero terminate
it++;
return PTR_DIFF_(it, out);
}
size_t cutf_8to32(uint8_t* start, uint8_t* end, uint32_t* out, size_t outsize)
{
uint32_t* it = out;
for(; start < end; ++it)
*it = cutf_next(&start, &outsize);
*it = 0; // Zero termination
return PTR_DIFF_(it, out);
}
size_t cutf_replace_invalid(uint8_t* start, uint8_t* end, uint8_t* out, size_t limit, uint32_t replacement)
{
uint8_t* it = out;
size_t outsize = limit;
limit -= cutf_codepoint_length(replacement);
while(start != end && (size_t)(it - out) <= limit ) {
uint8_t* sequence = start;
enum cutf_error err_code = cutf_validate_next(&start, end);
switch (err_code) {
case CUTF_OK :
for(; sequence != start; ++sequence)
*it++ = *sequence;
break;
case CUTF_INVALID_LEAD:
it = cutf_append(replacement, it, &outsize);
++start;
break;
case CUTF_TRUNCATED_SEQUENCE:
case CUTF_INCOMPLETE_SEQUENCE:
case CUTF_OVERLONG_SEQUENCE:
case CUTF_INVALID_CODE_POINT:
it = cutf_append(replacement, it, &outsize);
++start;
// just one replacement mark for the sequence
while(start != end && CUTF_IS_TRAIL(*start))
++start;
break;
}
}
return PTR_DIFF_(it, out);
}
#undef PTR_DIFF_
size_t cutf_default_replace_invalid(uint8_t* start, uint8_t* end, uint8_t* out, size_t limit)
{
const uint32_t replacement_marker = CUTF_MASK16(0xfffd);
return cutf_replace_invalid(start, end, out, limit, replacement_marker);
}
//
// Converts utf-8 string to wide version.
//
// returns target string length.
//
size_t utf8towchar(const char* s, size_t inSize, wchar_t* out, size_t outsize)
{
uint8_t* start = (uint8_t*) s;
if (inSize == SIZE_MAX)
inSize = strlen(s);
uint8_t* end = start + inSize;
size_t destLen = cutf_distance(start, end);
// Insufficient buffer size
if (destLen > outsize)
{
if (outsize != 0)
*out = 0;
return destLen + 1 /* zero termination */;
}
if (sizeof(wchar_t) == 2)
cutf_8to16(start, end, (uint16_t*)out, outsize);
else
cutf_8to32(start, end, (uint32_t*)out, outsize);
return destLen + 1 /* zero termination */;
}
size_t utf8ztowchar(const char* s, wchar_t* out, size_t outsize)
{
return utf8towchar(s, SIZE_MAX, out, outsize);
}
size_t utf8zestimate(const char* s)
{
return utf8towchar(s, SIZE_MAX, NULL, 0);
}
size_t wchartoutf8(const wchar_t* s, size_t inSize, char* out, size_t outsize)
{
const wchar_t* start = s;
if (inSize == SIZE_MAX)
inSize = wcslen(s);
const wchar_t* end = start + inSize;
if (sizeof(wchar_t) == 2)
return cutf_16to8((uint16_t*)start, (uint16_t*)end, (uint8_t*)out, outsize);
else
return cutf_32to8((uint32_t*)start, (uint32_t*)end, (uint8_t*)out, outsize);
}
size_t wcharztoutf8(const wchar_t* s, char* out, size_t outsize)
{
return wchartoutf8(s, SIZE_MAX, out, outsize);
}
size_t wcharzestimate(const wchar_t* ws)
{
return wchartoutf8(ws, SIZE_MAX, NULL, 0);
}
#ifdef __cplusplus
};
std::wstring utf8towide(const char* s)
{
std::wstring ws;
ws.resize(utf8towchar(s, SIZE_MAX, nullptr, 0) - 1);
utf8towchar(s, SIZE_MAX, &ws[0], ws.length());
return ws;
}
std::wstring utf8towide(const std::string& s)
{
return utf8towide(s.c_str());
}
std::string widetoutf8(const wchar_t* ws)
{
std::string s;
s.resize(wchartoutf8(ws, SIZE_MAX, nullptr, 0) - 1);
wchartoutf8(ws, SIZE_MAX, &s[0], s.length());
return s;
}
std::string widetoutf8(const std::wstring& ws)
{
return widetoutf8(ws.c_str());
}
#endif