This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcutf.h
414 lines (377 loc) · 12.4 KB
/
cutf.h
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
/*
Copyright 2014, Jason Kozak
Based on UTF8-CPP: Copyright 2006, Nemanja Trifunovicm
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef CUTF_H
#define CUTF_H
#include <stdint.h>
#include <stddef.h>
int cutf_is_valid(uint8_t* start, uint8_t* end);
int cutf_starts_with_bom(uint8_t* start, uint8_t* end);
uint8_t* cutf_append(uint32_t cp, uint8_t* result);
uint32_t cutf_next(uint8_t** it);
uint32_t cutf_peek_next(uint8_t* it);
uint32_t cutf_prior(uint8_t** it);
void cutf_advance(uint8_t** it, size_t n);
size_t cutf_distance(uint8_t* first, uint8_t* last);
size_t cutf_16to8(uint16_t* start, uint16_t* end, uint8_t* out);
size_t cutf_8to16(uint8_t* start, uint8_t* end, uint16_t* out);
size_t cutf_32to8(uint32_t* start, uint32_t* end, uint8_t* out);
size_t cutf_8to32(uint8_t* start, uint8_t* end, uint32_t* out);
size_t cutf_replace_invalid(uint8_t* start, uint8_t* end, uint8_t* out, size_t limit, uint32_t replacement);
size_t cutf_default_replace_invalid(uint8_t* start, uint8_t* end, uint8_t* out, size_t limit);
#if CUTF_IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
// internal
////////////////////////////////////////////////////////////////////////////////
#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);
}
////////////////////////////////////////////////////////////////////////////////
/// API
////////////////////////////////////////////////////////////////////////////////
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)
{
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)
{
uint32_t cp = CUTF_MASK8(**it);
size_t length = cutf_sequence_length(*it);
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);
}
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);
}
void cutf_advance(uint8_t** it, size_t n)
{
size_t i;
for(i = 0; i < n; ++i) {
cutf_next(it);
}
}
size_t cutf_distance(uint8_t* first, uint8_t* last)
{
size_t dist;
for(dist = 0; first < last; ++dist) {
cutf_next(&first);
}
return dist;
}
size_t cutf_16to8(uint16_t* start, uint16_t* end, uint8_t* out)
{
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);
}
return it - out;
}
size_t cutf_8to16(uint8_t* start, uint8_t* end, uint16_t* out)
{
uint16_t* it = out;
while(start < end) {
uint32_t cp = cutf_next(&start);
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);
}
}
return it - out;
}
size_t cutf_32to8(uint32_t* start, uint32_t* end, uint8_t* out)
{
uint8_t* it = out;
for(; start != end; ++start) {
it = cutf_append(*start, it);
}
return it - out;
}
size_t cutf_8to32(uint8_t* start, uint8_t* end, uint32_t* out)
{
uint32_t* it = out;
for(; start < end; ++it) {
*it = cutf_next(&start);
}
return 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;
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);
++start;
break;
case CUTF_TRUNCATED_SEQUENCE:
case CUTF_INCOMPLETE_SEQUENCE:
case CUTF_OVERLONG_SEQUENCE:
case CUTF_INVALID_CODE_POINT:
it = cutf_append(replacement, it);
++start;
// just one replacement mark for the sequence
while(start != end && CUTF_IS_TRAIL(*start))
++start;
break;
}
}
return it - out;
}
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);
}
#endif // CUTF_IMPLEMENTATION
#endif // CUTF_H