2
* Copyright 2001-2004 Unicode, Inc.
6
* This source code is provided as is by Unicode, Inc. No claims are
7
* made as to fitness for any particular purpose. No warranties of any
8
* kind are expressed or implied. The recipient agrees to determine
9
* applicability of information provided. If this file has been
10
* purchased on magnetic or optical media from Unicode, Inc., the
11
* sole remedy for any claim will be exchange of defective media
12
* within 90 days of receipt.
14
* Limitations on Rights to Redistribute This Code
16
* Unicode, Inc. hereby grants the right to freely use the information
17
* supplied in this file in the creation of products supporting the
18
* Unicode Standard, and to make copies of this file in any form
19
* for internal or external distribution as long as this notice
23
/* ---------------------------------------------------------------------
25
Conversions between UTF32, UTF-16, and UTF-8. Source code file.
26
Author: Mark E. Davis, 1994.
27
Rev History: Rick McGowan, fixes & updates May 2001.
28
Sept 2001: fixed const & error conditions per
29
mods suggested by S. Parent & A. Lillich.
30
June 2002: Tim Dodd added detection and handling of incomplete
31
source sequences, enhanced error detection, added casts
32
to eliminate compiler warnings.
33
July 2003: slight mods to back out aggressive FFFE detection.
34
Jan 2004: updated switches in from-UTF8 conversions.
35
Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
37
See the header file "ConvertUTF.h" for complete documentation.
39
------------------------------------------------------------------------ */
42
#include "convert_UTF.h"
47
static const int halfShift = 10; /* used for shifting by 10 bits */
49
static const UTF32 halfBase = 0x0010000UL;
50
static const UTF32 halfMask = 0x3FFUL;
52
#define UNI_SUR_HIGH_START (UTF32)0xD800
53
#define UNI_SUR_HIGH_END (UTF32)0xDBFF
54
#define UNI_SUR_LOW_START (UTF32)0xDC00
55
#define UNI_SUR_LOW_END (UTF32)0xDFFF
59
/* --------------------------------------------------------------------- */
61
ConversionResult ConvertUTF32toUTF16 (const UTF32** sourceStart, const UTF32* sourceEnd,
62
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
63
ConversionResult result = conversionOK;
64
const UTF32* source = *sourceStart;
65
UTF16* target = *targetStart;
66
while (source < sourceEnd) {
68
if (target >= targetEnd) {
69
result = targetExhausted; break;
72
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
73
/* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
74
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
75
if (flags == strictConversion) {
76
--source; /* return to the illegal value itself */
77
result = sourceIllegal;
80
*target++ = UNI_REPLACEMENT_CHAR;
83
*target++ = (UTF16)ch; /* normal case */
85
} else if (ch > UNI_MAX_LEGAL_UTF32) {
86
if (flags == strictConversion) {
87
result = sourceIllegal;
89
*target++ = UNI_REPLACEMENT_CHAR;
92
/* target is a character in range 0xFFFF - 0x10FFFF. */
93
if (target + 1 >= targetEnd) {
94
--source; /* Back up source pointer! */
95
result = targetExhausted; break;
98
*target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
99
*target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
102
*sourceStart = source;
103
*targetStart = target;
107
/* --------------------------------------------------------------------- */
109
ConversionResult ConvertUTF16toUTF32 (const UTF16** sourceStart, const UTF16* sourceEnd,
110
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
111
ConversionResult result = conversionOK;
112
const UTF16* source = *sourceStart;
113
UTF32* target = *targetStart;
115
while (source < sourceEnd) {
116
const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
118
/* If we have a surrogate pair, convert to UTF32 first. */
119
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
120
/* If the 16 bits following the high surrogate are in the source buffer... */
121
if (source < sourceEnd) {
123
/* If it's a low surrogate, convert to UTF32. */
124
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
125
ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
126
+ (ch2 - UNI_SUR_LOW_START) + halfBase;
128
} else if (flags == strictConversion) { /* it's an unpaired high surrogate */
129
--source; /* return to the illegal value itself */
130
result = sourceIllegal;
133
} else { /* We don't have the 16 bits following the high surrogate. */
134
--source; /* return to the high surrogate */
135
result = sourceExhausted;
138
} else if (flags == strictConversion) {
139
/* UTF-16 surrogate values are illegal in UTF-32 */
140
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
141
--source; /* return to the illegal value itself */
142
result = sourceIllegal;
146
if (target >= targetEnd) {
147
source = oldSource; /* Back up source pointer! */
148
result = targetExhausted; break;
152
*sourceStart = source;
153
*targetStart = target;
155
if (result == sourceIllegal) {
156
fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
163
/* --------------------------------------------------------------------- */
166
* Index into the table below with the first byte of a UTF-8 sequence to
167
* get the number of trailing bytes that are supposed to follow it.
168
* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
169
* left as-is for anyone who may want to do such conversion, which was
170
* allowed in earlier algorithms.
172
static const char trailingBytesForUTF8[256] = {
173
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
174
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
175
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
176
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
177
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
178
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
179
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
180
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
184
* Magic values subtracted from a buffer value during UTF8 conversion.
185
* This table contains as many values as there might be trailing bytes
186
* in a UTF-8 sequence.
188
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
189
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
192
* Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
193
* into the first byte, depending on how many bytes follow. There are
194
* as many entries in this table as there are UTF-8 sequence types.
195
* (I.e., one byte sequence, two byte... etc.). Remember that sequencs
196
* for *legal* UTF-8 will be 4 or fewer bytes total.
198
static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
200
/* --------------------------------------------------------------------- */
202
/* The interface converts a whole buffer to avoid function-call overhead.
203
* Constants have been gathered. Loops & conditionals have been removed as
204
* much as possible for efficiency, in favor of drop-through switches.
205
* (See "Note A" at the bottom of the file for equivalent code.)
206
* If your compiler supports it, the "isLegalUTF8" call can be turned
207
* into an inline function.
210
/* --------------------------------------------------------------------- */
212
ConversionResult ConvertUTF16toUTF8 (const UTF16** sourceStart, const UTF16* sourceEnd,
213
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
214
ConversionResult result = conversionOK;
215
const UTF16* source = *sourceStart;
216
UTF8* target = *targetStart;
217
while (source < sourceEnd) {
219
unsigned short bytesToWrite = 0;
220
const UTF32 byteMask = 0xBF;
221
const UTF32 byteMark = 0x80;
222
const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
224
/* If we have a surrogate pair, convert to UTF32 first. */
225
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
226
/* If the 16 bits following the high surrogate are in the source buffer... */
227
if (source < sourceEnd) {
229
/* If it's a low surrogate, convert to UTF32. */
230
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
231
ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
232
+ (ch2 - UNI_SUR_LOW_START) + halfBase;
234
} else if (flags == strictConversion) { /* it's an unpaired high surrogate */
235
--source; /* return to the illegal value itself */
236
result = sourceIllegal;
239
} else { /* We don't have the 16 bits following the high surrogate. */
240
--source; /* return to the high surrogate */
241
result = sourceExhausted;
244
} else if (flags == strictConversion) {
245
/* UTF-16 surrogate values are illegal in UTF-32 */
246
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
247
--source; /* return to the illegal value itself */
248
result = sourceIllegal;
252
/* Figure out how many bytes the result will require */
253
if (ch < (UTF32)0x80) { bytesToWrite = 1;
254
} else if (ch < (UTF32)0x800) { bytesToWrite = 2;
255
} else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
256
} else if (ch < (UTF32)0x110000) { bytesToWrite = 4;
257
} else { bytesToWrite = 3;
258
ch = UNI_REPLACEMENT_CHAR;
261
target += bytesToWrite;
262
if (target > targetEnd) {
263
source = oldSource; /* Back up source pointer! */
264
target -= bytesToWrite; result = targetExhausted; break;
266
switch (bytesToWrite) { /* note: everything falls through. */
267
case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
268
case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
269
case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
270
case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]);
272
target += bytesToWrite;
274
*sourceStart = source;
275
*targetStart = target;
279
/* --------------------------------------------------------------------- */
282
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
283
* This must be called with the length pre-determined by the first byte.
284
* If not calling this from ConvertUTF8to*, then the length can be set by:
285
* length = trailingBytesForUTF8[*source]+1;
286
* and the sequence is illegal right away if there aren't that many bytes
288
* If presented with a length > 4, this returns false. The Unicode
289
* definition of UTF-8 goes up to 4-byte sequences.
292
static Boolean isLegalUTF8(const UTF8 *source, int length) {
294
const UTF8 *srcptr = source+length;
296
default: return false;
297
/* Everything else falls through when "true"... */
298
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
299
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
300
case 2: if ((a = (*--srcptr)) > 0xBF) return false;
303
/* no fall-through in this inner switch */
304
case 0xE0: if (a < 0xA0) return false; break;
305
case 0xED: if (a > 0x9F) return false; break;
306
case 0xF0: if (a < 0x90) return false; break;
307
case 0xF4: if (a > 0x8F) return false; break;
308
default: if (a < 0x80) return false;
311
case 1: if (*source >= 0x80 && *source < 0xC2) return false;
313
if (*source > 0xF4) return false;
317
/* --------------------------------------------------------------------- */
320
* Exported function to return whether a UTF-8 sequence is legal or not.
321
* This is not used here; it's just exported.
323
Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
324
int length = trailingBytesForUTF8[*source]+1;
325
if (source+length > sourceEnd) {
328
return isLegalUTF8(source, length);
331
/* --------------------------------------------------------------------- */
333
ConversionResult ConvertUTF8toUTF16 (const UTF8** sourceStart, const UTF8* sourceEnd,
334
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
335
ConversionResult result = conversionOK;
336
const UTF8* source = *sourceStart;
337
UTF16* target = *targetStart;
338
while (source < sourceEnd) {
340
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
341
if (source + extraBytesToRead >= sourceEnd) {
342
result = sourceExhausted; break;
344
/* Do this check whether lenient or strict */
345
if (! isLegalUTF8(source, extraBytesToRead+1)) {
346
result = sourceIllegal;
350
* The cases all fall through. See "Note A" below.
352
switch (extraBytesToRead) {
353
case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
354
case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
355
case 3: ch += *source++; ch <<= 6;
356
case 2: ch += *source++; ch <<= 6;
357
case 1: ch += *source++; ch <<= 6;
358
case 0: ch += *source++;
360
ch -= offsetsFromUTF8[extraBytesToRead];
362
if (target >= targetEnd) {
363
source -= (extraBytesToRead+1); /* Back up source pointer! */
364
result = targetExhausted; break;
366
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
367
/* UTF-16 surrogate values are illegal in UTF-32 */
368
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
369
if (flags == strictConversion) {
370
source -= (extraBytesToRead+1); /* return to the illegal value itself */
371
result = sourceIllegal;
374
*target++ = UNI_REPLACEMENT_CHAR;
377
*target++ = (UTF16)ch; /* normal case */
379
} else if (ch > UNI_MAX_UTF16) {
380
if (flags == strictConversion) {
381
result = sourceIllegal;
382
source -= (extraBytesToRead+1); /* return to the start */
383
break; /* Bail out; shouldn't continue */
385
*target++ = UNI_REPLACEMENT_CHAR;
388
/* target is a character in range 0xFFFF - 0x10FFFF. */
389
if (target + 1 >= targetEnd) {
390
source -= (extraBytesToRead+1); /* Back up source pointer! */
391
result = targetExhausted; break;
394
*target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
395
*target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
398
*sourceStart = source;
399
*targetStart = target;
403
/* --------------------------------------------------------------------- */
405
ConversionResult ConvertUTF32toUTF8 (const UTF32** sourceStart, const UTF32* sourceEnd,
406
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
407
ConversionResult result = conversionOK;
408
const UTF32* source = *sourceStart;
409
UTF8* target = *targetStart;
410
while (source < sourceEnd) {
412
unsigned short bytesToWrite = 0;
413
const UTF32 byteMask = 0xBF;
414
const UTF32 byteMark = 0x80;
416
if (flags == strictConversion ) {
417
/* UTF-16 surrogate values are illegal in UTF-32 */
418
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
419
--source; /* return to the illegal value itself */
420
result = sourceIllegal;
425
* Figure out how many bytes the result will require. Turn any
426
* illegally large UTF32 things (> Plane 17) into replacement chars.
428
if (ch < (UTF32)0x80) { bytesToWrite = 1;
429
} else if (ch < (UTF32)0x800) { bytesToWrite = 2;
430
} else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
431
} else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4;
432
} else { bytesToWrite = 3;
433
ch = UNI_REPLACEMENT_CHAR;
434
result = sourceIllegal;
437
target += bytesToWrite;
438
if (target > targetEnd) {
439
--source; /* Back up source pointer! */
440
target -= bytesToWrite; result = targetExhausted; break;
442
switch (bytesToWrite) { /* note: everything falls through. */
443
case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
444
case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
445
case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
446
case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
448
target += bytesToWrite;
450
*sourceStart = source;
451
*targetStart = target;
455
/* --------------------------------------------------------------------- */
457
ConversionResult ConvertUTF8toUTF32 (const UTF8** sourceStart, const UTF8* sourceEnd,
458
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
459
ConversionResult result = conversionOK;
460
const UTF8* source = *sourceStart;
461
UTF32* target = *targetStart;
462
while (source < sourceEnd) {
464
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
465
if (source + extraBytesToRead >= sourceEnd) {
466
result = sourceExhausted; break;
468
/* Do this check whether lenient or strict */
469
if (! isLegalUTF8(source, extraBytesToRead+1)) {
470
result = sourceIllegal;
474
* The cases all fall through. See "Note A" below.
476
switch (extraBytesToRead) {
477
case 5: ch += *source++; ch <<= 6;
478
case 4: ch += *source++; ch <<= 6;
479
case 3: ch += *source++; ch <<= 6;
480
case 2: ch += *source++; ch <<= 6;
481
case 1: ch += *source++; ch <<= 6;
482
case 0: ch += *source++;
484
ch -= offsetsFromUTF8[extraBytesToRead];
486
if (target >= targetEnd) {
487
source -= (extraBytesToRead+1); /* Back up the source pointer! */
488
result = targetExhausted; break;
490
if (ch <= UNI_MAX_LEGAL_UTF32) {
492
* UTF-16 surrogate values are illegal in UTF-32, and anything
493
* over Plane 17 (> 0x10FFFF) is illegal.
495
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
496
if (flags == strictConversion) {
497
source -= (extraBytesToRead+1); /* return to the illegal value itself */
498
result = sourceIllegal;
501
*target++ = UNI_REPLACEMENT_CHAR;
506
} else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
507
result = sourceIllegal;
508
*target++ = UNI_REPLACEMENT_CHAR;
511
*sourceStart = source;
512
*targetStart = target;
516
/* ---------------------------------------------------------------------
519
The fall-through switches in UTF-8 reading code save a
520
temp variable, some decrements & conditionals. The switches
521
are equivalent to the following loop:
523
int tmpBytesToRead = extraBytesToRead+1;
527
if (tmpBytesToRead) ch <<= 6;
528
} while (tmpBytesToRead > 0);
530
In UTF-8 writing code, the switches on "bytesToWrite" are
531
similarly unrolled loops.
533
--------------------------------------------------------------------- */