forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8.cpp
More file actions
576 lines (521 loc) · 18.8 KB
/
utf8.cpp
File metadata and controls
576 lines (521 loc) · 18.8 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
unicode/utf8.c
Abstract:
Functions to encode and decode UTF-8 strings
Revision History:
--*/
#include "pal/utf8.h"
#include "pal/dbgmsg.h"
#include "pal/unicode_data.h"
//
// Constant Declarations.
//
#define ASCII 0x007f
#define UTF8_2_MAX 0x07ff // max UTF8 2-byte sequence (32 * 64 = 2048)
#define UTF8_1ST_OF_2 0xc0 // 110x xxxx
#define UTF8_1ST_OF_3 0xe0 // 1110 xxxx
#define UTF8_1ST_OF_4 0xf0 // 1111 xxxx
#define UTF8_TRAIL 0x80 // 10xx xxxx
#define HIGHER_6_BIT(u) ((u) >> 12)
#define MIDDLE_6_BIT(u) (((u) & 0x0fc0) >> 6)
#define LOWER_6_BIT(u) ((u) & 0x003f)
#define BIT7(a) ((a) & 0x80)
#define BIT6(a) ((a) & 0x40)
#define HIGH_SURROGATE_START 0xd800
#define HIGH_SURROGATE_END 0xdbff
#define LOW_SURROGATE_START 0xdc00
#define LOW_SURROGATE_END 0xdfff
////////////////////////////////////////////////////////////////////////////
//
// UTF8ToUnicode
//
// Maps a UTF-8 character string to its wide character string counterpart.
//
////////////////////////////////////////////////////////////////////////////
int UTF8ToUnicode(
LPCSTR lpSrcStr,
int cchSrc,
LPWSTR lpDestStr,
int cchDest,
DWORD dwFlags
)
{
int nTB = 0; // # trail bytes to follow
int cchWC = 0; // # of Unicode code points generated
CONST BYTE* pUTF8 = (CONST BYTE*)lpSrcStr;
DWORD dwUnicodeChar = 0; // Our character with room for full surrogate char
BOOL bSurrogatePair = FALSE; // Indicate we're collecting a surrogate pair
BOOL bCheckInvalidBytes = (dwFlags & MB_ERR_INVALID_CHARS);
BYTE UTF8;
// Note that we can't test destination buffer length here because we may have to
// iterate through thousands of broken characters which won't be output, even though
// the buffer has no more room.
while (cchSrc--)
{
//
// See if there are any trail bytes.
//
if (BIT7(*pUTF8) == 0)
{
//
// Found ASCII.
//
if (cchDest)
{
// In this function always test buffer size before using it
if (cchWC >= cchDest)
{
// Error: Buffer too small, we didn't process this character
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (0);
}
lpDestStr[cchWC] = (WCHAR)*pUTF8;
}
nTB = bSurrogatePair = 0;
cchWC++;
}
else if (BIT6(*pUTF8) == 0)
{
//
// Found a trail byte.
// Note : Ignore the trail byte if there was no lead byte.
//
if (nTB != 0)
{
//
// Decrement the trail byte counter.
//
nTB--;
// Add room for trail byte and add the trail byte falue
dwUnicodeChar <<= 6;
dwUnicodeChar |= LOWER_6_BIT(*pUTF8);
// If we're done then we may need to store the data
if (nTB == 0)
{
if (bSurrogatePair)
{
if (cchDest)
{
if ((cchWC + 1) >= cchDest)
{
// Error: Buffer too small, we didn't process this character
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (0);
}
lpDestStr[cchWC] = (WCHAR)
(((dwUnicodeChar - 0x10000) >> 10) + HIGH_SURROGATE_START);
lpDestStr[cchWC+1] = (WCHAR)
((dwUnicodeChar - 0x10000)%0x400 + LOW_SURROGATE_START);
}
//
// End of sequence. Advance the output counter, turn off surrogateness
//
cchWC += 2;
bSurrogatePair = FALSE;
}
else
{
if (cchDest)
{
if (cchWC >= cchDest)
{
// Error: Buffer too small, we didn't process this character
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (0);
}
lpDestStr[cchWC] = (WCHAR)dwUnicodeChar;
}
//
// End of sequence. Advance the output counter.
//
cchWC++;
}
}
}
else
{
if (bCheckInvalidBytes)
{
SetLastError(ERROR_NO_UNICODE_TRANSLATION);
return (0);
}
// error - not expecting a trail byte. That is, there is a trailing byte without leading byte.
bSurrogatePair = FALSE;
}
}
else
{
//
// Found a lead byte.
//
if (nTB > 0)
{
// error - A leading byte before the previous sequence is completed.
if (bCheckInvalidBytes)
{
SetLastError(ERROR_NO_UNICODE_TRANSLATION);
return (0);
}
//
// Error - previous sequence not finished.
//
nTB = 0;
bSurrogatePair = FALSE;
// Put this character back so that we can start over another sequence.
cchSrc++;
pUTF8--;
}
else
{
//
// Calculate the number of bytes to follow.
// Look for the first 0 from left to right.
//
UTF8 = *pUTF8;
while (BIT7(UTF8) != 0)
{
UTF8 <<= 1;
nTB++;
}
// Recover the data from the byte
UTF8 >>= nTB;
//
// Check for non-shortest form.
//
switch (nTB)
{
case 1:
nTB = 0;
break;
case 2:
// Make sure that bit 8 ~ bit 11 is not all zero.
// 110XXXXx 10xxxxxx
if ((*pUTF8 & 0x1e) == 0)
{
nTB = 0;
}
break;
case 3:
// Look ahead to check for non-shortest form.
// 1110XXXX 10Xxxxxx 10xxxxxx
if (cchSrc >= 2)
{
if (((*pUTF8 & 0x0f) == 0) && (*(pUTF8 + 1) & 0x20) == 0)
{
nTB = 0;
}
}
break;
case 4:
//
// This is a surrogate unicode pair
//
if (cchSrc >= 3)
{
WORD word = (((WORD)*pUTF8) << 8) | *(pUTF8 + 1);
// Look ahead to check for non-shortest form.
// 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx
// Check if the 5 X bits are all zero.
// 0x0730 == 00000111 00110000
if ( (word & 0x0730) == 0 ||
// If the 21st bit is 1, we have extra work
( (word & 0x0400) == 0x0400 &&
// The 21st bit is 1.
// Make sure that the resulting Unicode is within the valid surrogate range.
// The 4 byte code sequence can hold up to 21 bits, and the maximum valid code point range
// that Unicode (with surrogate) could represent are from U+000000 ~ U+10FFFF.
// Therefore, if the 21 bit (the most significant bit) is 1, we should verify that the 17 ~ 20
// bit are all zero.
// I.e., in 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx,
// XXXXX can only be 10000.
// 0x0330 = 0000 0011 0011 0000
(word & 0x0330) != 0 ) )
{
// Not shortest form
nTB = 0;
}
else
{
// A real surrogate pair
bSurrogatePair = TRUE;
}
}
break;
default:
//
// If the bits is greater than 4, this is an invalid
// UTF8 lead byte.
//
nTB = 0;
break;
}
if (nTB != 0)
{
//
// Store the value from the first byte and decrement
// the number of bytes to follow.
//
dwUnicodeChar = UTF8;
nTB--;
} else
{
if (bCheckInvalidBytes)
{
SetLastError(ERROR_NO_UNICODE_TRANSLATION);
return (0);
}
}
}
}
pUTF8++;
}
if ((bCheckInvalidBytes && nTB != 0) || (cchWC == 0))
{
// About (cchWC == 0):
// Because we now throw away non-shortest form, it is possible that we generate 0 chars.
// In this case, we have to set error to ERROR_NO_UNICODE_TRANSLATION so that we conform
// to the spec of MultiByteToWideChar.
SetLastError(ERROR_NO_UNICODE_TRANSLATION);
return (0);
}
//
// Return the number of Unicode characters written.
//
return (cchWC);
}
////////////////////////////////////////////////////////////////////////////
//
// UnicodeToUTF8
//
// Maps a Unicode character string to its UTF-8 string counterpart.
//
////////////////////////////////////////////////////////////////////////////
int UnicodeToUTF8(
LPCWSTR lpSrcStr,
int cchSrc,
LPSTR lpDestStr,
int cchDest)
{
LPCWSTR lpWC = lpSrcStr;
int cchU8 = 0; // # of UTF8 chars generated
DWORD dwSurrogateChar;
WCHAR wchHighSurrogate = 0;
BOOL bHandled;
while ((cchSrc--) && ((cchDest == 0) || (cchU8 < cchDest)))
{
bHandled = FALSE;
//
// Check if high surrogate is available
//
if ((*lpWC >= HIGH_SURROGATE_START) && (*lpWC <= HIGH_SURROGATE_END))
{
if (cchDest)
{
// Another high surrogate, then treat the 1st as normal
// Unicode character.
if (wchHighSurrogate)
{
if ((cchU8 + 2) < cchDest)
{
lpDestStr[cchU8++] = UTF8_1ST_OF_3 | HIGHER_6_BIT(wchHighSurrogate);
lpDestStr[cchU8++] = UTF8_TRAIL | MIDDLE_6_BIT(wchHighSurrogate);
lpDestStr[cchU8++] = UTF8_TRAIL | LOWER_6_BIT(wchHighSurrogate);
}
else
{
// not enough buffer
cchSrc++;
break;
}
}
}
else
{
cchU8 += 3;
}
wchHighSurrogate = *lpWC;
bHandled = TRUE;
}
if (!bHandled && wchHighSurrogate)
{
if ((*lpWC >= LOW_SURROGATE_START) && (*lpWC <= LOW_SURROGATE_END))
{
// wheee, valid surrogate pairs
if (cchDest)
{
if ((cchU8 + 3) < cchDest)
{
dwSurrogateChar = (((wchHighSurrogate-0xD800) << 10) + (*lpWC - 0xDC00) + 0x10000);
lpDestStr[cchU8++] = (UTF8_1ST_OF_4 |
(unsigned char)(dwSurrogateChar >> 18)); // 3 bits from 1st byte
lpDestStr[cchU8++] = (UTF8_TRAIL |
(unsigned char)((dwSurrogateChar >> 12) & 0x3f)); // 6 bits from 2nd byte
lpDestStr[cchU8++] = (UTF8_TRAIL |
(unsigned char)((dwSurrogateChar >> 6) & 0x3f)); // 6 bits from 3rd byte
lpDestStr[cchU8++] = (UTF8_TRAIL |
(unsigned char)(0x3f & dwSurrogateChar)); // 6 bits from 4th byte
}
else
{
// not enough buffer
cchSrc++;
break;
}
}
else
{
// we already counted 3 previously (in high surrogate)
cchU8 ++;
}
bHandled = TRUE;
}
else
{
// Bad Surrogate pair : ERROR
// Just process wchHighSurrogate , and the code below will
// process the current code point
if (cchDest)
{
if ((cchU8 + 2) < cchDest)
{
lpDestStr[cchU8++] = UTF8_1ST_OF_3 | HIGHER_6_BIT(wchHighSurrogate);
lpDestStr[cchU8++] = UTF8_TRAIL | MIDDLE_6_BIT(wchHighSurrogate);
lpDestStr[cchU8++] = UTF8_TRAIL | LOWER_6_BIT(wchHighSurrogate);
}
else
{
// not enough buffer
cchSrc++;
break;
}
}
}
wchHighSurrogate = 0;
}
if (!bHandled)
{
if (*lpWC <= ASCII)
{
//
// Found ASCII.
//
if (cchDest)
{
if (cchU8 < cchDest)
{
lpDestStr[cchU8] = (char)*lpWC;
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
cchU8++;
}
else if (*lpWC <= UTF8_2_MAX)
{
//
// Found 2 byte sequence if < 0x07ff (11 bits).
//
if (cchDest)
{
if ((cchU8 + 1) < cchDest)
{
//
// Use upper 5 bits in first byte.
// Use lower 6 bits in second byte.
//
lpDestStr[cchU8++] = UTF8_1ST_OF_2 | (*lpWC >> 6);
lpDestStr[cchU8++] = UTF8_TRAIL | LOWER_6_BIT(*lpWC);
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
else
{
cchU8 += 2;
}
}
else
{
//
// Found 3 byte sequence.
//
if (cchDest)
{
if ((cchU8 + 2) < cchDest)
{
//
// Use upper 4 bits in first byte.
// Use middle 6 bits in second byte.
// Use lower 6 bits in third byte.
//
lpDestStr[cchU8++] = UTF8_1ST_OF_3 | HIGHER_6_BIT(*lpWC);
lpDestStr[cchU8++] = UTF8_TRAIL | MIDDLE_6_BIT(*lpWC);
lpDestStr[cchU8++] = UTF8_TRAIL | LOWER_6_BIT(*lpWC);
}
else
{
//
// Error - buffer too small.
//
cchSrc++;
break;
}
}
else
{
cchU8 += 3;
}
}
}
lpWC++;
}
//
// If the last character was a high surrogate, then handle it as a normal
// unicode character.
//
if ((cchSrc < 0) && (wchHighSurrogate != 0))
{
if (cchDest)
{
if ((cchU8 + 2) < cchDest)
{
lpDestStr[cchU8++] = UTF8_1ST_OF_3 | HIGHER_6_BIT(wchHighSurrogate);
lpDestStr[cchU8++] = UTF8_TRAIL | MIDDLE_6_BIT(wchHighSurrogate);
lpDestStr[cchU8++] = UTF8_TRAIL | LOWER_6_BIT(wchHighSurrogate);
}
else
{
cchSrc++;
}
}
}
//
// Make sure the destination buffer was large enough.
//
if (cchDest && (cchSrc >= 0))
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return (0);
}
//
// Return the number of UTF-8 characters written.
//
return (cchU8);
}