SystemCall run(block) can now exit the run if it returns false
[io/quag.git] / libs / basekit / source / ConvertUTF.c
blob4568b9e89f467b02793859c4c0a65173d521c7e0
1 /*
2 * Copyright 2001-2004 Unicode, Inc.
3 *
4 * Disclaimer
5 *
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 io_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
20 * remains attached.
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 "ConvertUTF.h"
43 #ifdef CVTUTF_DEBUG
44 #include <stdio.h>
45 #endif
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
56 #define false 0
57 #define true 1
59 /* --------------------------------------------------------------------- */
61 ConversionResult ConvertUTF32toUTF16 (
62 const UTF32** sourceStart,
63 const UTF32* sourceEnd,
64 UTF16** targetStart,
65 UTF16* targetEnd,
66 ConversionFlags flags)
68 ConversionResult result = conversionOK;
69 const UTF32* source = *sourceStart;
70 UTF16* target = *targetStart;
72 while (source < sourceEnd)
74 UTF32 ch;
76 if (target >= targetEnd)
78 result = targetExhausted; break;
81 ch = *source++;
83 if (ch <= UNI_MAX_BMP)
84 { /* Target is a character <= 0xFFFF */
85 /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
86 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)
88 if (flags == strictConversion)
90 --source; /* return to the illegal value itself */
91 result = sourceIllegal;
92 break;
94 else
96 *target++ = UNI_REPLACEMENT_CHAR;
99 else
101 *target++ = (UTF16)ch; /* normal case */
104 else if (ch > UNI_MAX_LEGAL_UTF32)
106 if (flags == strictConversion)
108 result = sourceIllegal;
110 else
112 *target++ = UNI_REPLACEMENT_CHAR;
115 else
117 /* target is a character in range 0xFFFF - 0x10FFFF. */
118 if (target + 1 >= targetEnd)
120 --source; /* Back up source pointer! */
121 result = targetExhausted; break;
123 ch -= halfBase;
124 *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
125 *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
128 *sourceStart = source;
129 *targetStart = target;
130 return result;
133 /* --------------------------------------------------------------------- */
135 ConversionResult ConvertUTF16toUTF32 (
136 const UTF16** sourceStart, const UTF16* sourceEnd,
137 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
138 ConversionResult result = conversionOK;
139 const UTF16* source = *sourceStart;
140 UTF32* target = *targetStart;
141 UTF32 ch, ch2;
142 while (source < sourceEnd) {
143 const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
144 ch = *source++;
145 /* If we have a surrogate pair, convert to UTF32 first. */
146 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
147 /* If the 16 bits following the high surrogate are in the source buffer... */
148 if (source < sourceEnd) {
149 ch2 = *source;
150 /* If it's a low surrogate, convert to UTF32. */
151 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
152 ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
153 + (ch2 - UNI_SUR_LOW_START) + halfBase;
154 ++source;
155 } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
156 --source; /* return to the illegal value itself */
157 result = sourceIllegal;
158 break;
160 } else { /* We don't have the 16 bits following the high surrogate. */
161 --source; /* return to the high surrogate */
162 result = sourceExhausted;
163 break;
165 } else if (flags == strictConversion) {
166 /* UTF-16 surrogate values are illegal in UTF-32 */
167 if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
168 --source; /* return to the illegal value itself */
169 result = sourceIllegal;
170 break;
173 if (target >= targetEnd) {
174 source = oldSource; /* Back up source pointer! */
175 result = targetExhausted; break;
177 *target++ = ch;
179 *sourceStart = source;
180 *targetStart = target;
181 #ifdef CVTUTF_DEBUG
182 if (result == sourceIllegal) {
183 fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
184 fflush(stderr);
186 #endif
187 return result;
190 /* --------------------------------------------------------------------- */
193 * Index into the table below with the first byte of a UTF-8 sequence to
194 * get the number of trailing bytes that are supposed to follow it.
195 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
196 * left as-is for anyone who may want to do such conversion, which was
197 * allowed in earlier algorithms.
199 static const char trailingBytesForUTF8[256] = {
200 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,
201 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,
202 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,
203 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,
204 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,
205 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,
206 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,
207 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
211 * Magic values subtracted from a buffer value during UTF8 conversion.
212 * This table contains as many values as there might be trailing bytes
213 * in a UTF-8 sequence.
215 static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
216 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
219 * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
220 * into the first byte, depending on how many bytes follow. There are
221 * as many entries in this table as there are UTF-8 sequence types.
222 * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
223 * for *legal* UTF-8 will be 4 or fewer bytes total.
225 static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
227 /* --------------------------------------------------------------------- */
229 /* The interface converts a whole buffer to avoid function-call overhead.
230 * Constants have been gathered. Loops & conditionals have been removed as
231 * much as possible for efficiency, in favor of drop-through switches.
232 * (See "Note A" at the bottom of the file for equivalent code.)
233 * If your compiler supports it, the "isLegalUTF8" call can be turned
234 * into an inline function.
237 /* --------------------------------------------------------------------- */
239 ConversionResult ConvertUTF16toUTF8 (
240 const UTF16** sourceStart, const UTF16* sourceEnd,
241 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
242 ConversionResult result = conversionOK;
243 const UTF16* source = *sourceStart;
244 UTF8* target = *targetStart;
245 while (source < sourceEnd) {
246 UTF32 ch;
247 unsigned short bytesToWrite = 0;
248 const UTF32 byteMask = 0xBF;
249 const UTF32 byteMark = 0x80;
250 const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
251 ch = *source++;
252 /* If we have a surrogate pair, convert to UTF32 first. */
253 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
254 /* If the 16 bits following the high surrogate are in the source buffer... */
255 if (source < sourceEnd) {
256 UTF32 ch2 = *source;
257 /* If it's a low surrogate, convert to UTF32. */
258 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
259 ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
260 + (ch2 - UNI_SUR_LOW_START) + halfBase;
261 ++source;
262 } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
263 --source; /* return to the illegal value itself */
264 result = sourceIllegal;
265 break;
267 } else { /* We don't have the 16 bits following the high surrogate. */
268 --source; /* return to the high surrogate */
269 result = sourceExhausted;
270 break;
272 } else if (flags == strictConversion) {
273 /* UTF-16 surrogate values are illegal in UTF-32 */
274 if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
275 --source; /* return to the illegal value itself */
276 result = sourceIllegal;
277 break;
280 /* Figure out how many bytes the result will require */
281 if (ch < (UTF32)0x80) { bytesToWrite = 1;
282 } else if (ch < (UTF32)0x800) { bytesToWrite = 2;
283 } else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
284 } else if (ch < (UTF32)0x110000) { bytesToWrite = 4;
285 } else { bytesToWrite = 3;
286 ch = UNI_REPLACEMENT_CHAR;
289 target += bytesToWrite;
290 if (target > targetEnd) {
291 source = oldSource; /* Back up source pointer! */
292 target -= bytesToWrite; result = targetExhausted; break;
294 switch (bytesToWrite) { /* note: everything falls through. */
295 case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
296 case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
297 case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
298 case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]);
300 target += bytesToWrite;
302 *sourceStart = source;
303 *targetStart = target;
304 return result;
307 /* --------------------------------------------------------------------- */
310 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
311 * This must be called with the length pre-determined by the first byte.
312 * If not calling this from ConvertUTF8to*, then the length can be set by:
313 * length = trailingBytesForUTF8[*source]+1;
314 * and the sequence is illegal right away if there aren't that many bytes
315 * available.
316 * If presented with a length > 4, this returns false. The Unicode
317 * definition of UTF-8 goes up to 4-byte sequences.
320 static Boolean isLegalUTF8(const UTF8 *source, int length) {
321 UTF8 a;
322 const UTF8 *srcptr = source+length;
323 switch (length) {
324 default: return false;
325 /* Everything else falls through when "true"... */
326 case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
327 case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
328 case 2: if ((a = (*--srcptr)) > 0xBF) return false;
330 switch (*source) {
331 /* no fall-through in this inner switch */
332 case 0xE0: if (a < 0xA0) return false; break;
333 case 0xED: if (a > 0x9F) return false; break;
334 case 0xF0: if (a < 0x90) return false; break;
335 case 0xF4: if (a > 0x8F) return false; break;
336 default: if (a < 0x80) return false;
339 case 1: if (*source >= 0x80 && *source < 0xC2) return false;
341 if (*source > 0xF4) return false;
342 return true;
345 /* --------------------------------------------------------------------- */
348 * Exported function to return whether a UTF-8 sequence is legal or not.
349 * This is not used here; it's just exported.
351 Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
352 int length = trailingBytesForUTF8[*source]+1;
353 if (source+length > sourceEnd) {
354 return false;
356 return isLegalUTF8(source, length);
359 /* --------------------------------------------------------------------- */
361 ConversionResult ConvertUTF8toUTF16 (
362 const UTF8** sourceStart, const UTF8* sourceEnd,
363 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
364 ConversionResult result = conversionOK;
365 const UTF8* source = *sourceStart;
366 UTF16* target = *targetStart;
367 while (source < sourceEnd) {
368 UTF32 ch = 0;
369 unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
370 if (source + extraBytesToRead >= sourceEnd) {
371 result = sourceExhausted; break;
373 /* Do this check whether lenient or strict */
374 if (! isLegalUTF8(source, extraBytesToRead+1)) {
375 result = sourceIllegal;
376 break;
379 * The cases all fall through. See "Note A" below.
381 switch (extraBytesToRead) {
382 case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
383 case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
384 case 3: ch += *source++; ch <<= 6;
385 case 2: ch += *source++; ch <<= 6;
386 case 1: ch += *source++; ch <<= 6;
387 case 0: ch += *source++;
389 ch -= offsetsFromUTF8[extraBytesToRead];
391 if (target >= targetEnd) {
392 source -= (extraBytesToRead+1); /* Back up source pointer! */
393 result = targetExhausted; break;
395 if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
396 /* UTF-16 surrogate values are illegal in UTF-32 */
397 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
398 if (flags == strictConversion) {
399 source -= (extraBytesToRead+1); /* return to the illegal value itself */
400 result = sourceIllegal;
401 break;
402 } else {
403 *target++ = UNI_REPLACEMENT_CHAR;
405 } else {
406 *target++ = (UTF16)ch; /* normal case */
408 } else if (ch > UNI_MAX_UTF16) {
409 if (flags == strictConversion) {
410 result = sourceIllegal;
411 source -= (extraBytesToRead+1); /* return to the start */
412 break; /* Bail out; shouldn't continue */
413 } else {
414 *target++ = UNI_REPLACEMENT_CHAR;
416 } else {
417 /* target is a character in range 0xFFFF - 0x10FFFF. */
418 if (target + 1 >= targetEnd) {
419 source -= (extraBytesToRead+1); /* Back up source pointer! */
420 result = targetExhausted; break;
422 ch -= halfBase;
423 *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
424 *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
427 *sourceStart = source;
428 *targetStart = target;
429 return result;
432 /* --------------------------------------------------------------------- */
434 ConversionResult ConvertUTF32toUTF8 (
435 const UTF32** sourceStart, const UTF32* sourceEnd,
436 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
437 ConversionResult result = conversionOK;
438 const UTF32* source = *sourceStart;
439 UTF8* target = *targetStart;
440 while (source < sourceEnd) {
441 UTF32 ch;
442 unsigned short bytesToWrite = 0;
443 const UTF32 byteMask = 0xBF;
444 const UTF32 byteMark = 0x80;
445 ch = *source++;
446 if (flags == strictConversion ) {
447 /* UTF-16 surrogate values are illegal in UTF-32 */
448 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
449 --source; /* return to the illegal value itself */
450 result = sourceIllegal;
451 break;
455 * Figure out how many bytes the result will require. Turn any
456 * illegally large UTF32 things (> Plane 17) into replacement chars.
458 if (ch < (UTF32)0x80) { bytesToWrite = 1;
459 } else if (ch < (UTF32)0x800) { bytesToWrite = 2;
460 } else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
461 } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4;
462 } else { bytesToWrite = 3;
463 ch = UNI_REPLACEMENT_CHAR;
464 result = sourceIllegal;
467 target += bytesToWrite;
468 if (target > targetEnd) {
469 --source; /* Back up source pointer! */
470 target -= bytesToWrite; result = targetExhausted; break;
472 switch (bytesToWrite) { /* note: everything falls through. */
473 case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
474 case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
475 case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
476 case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
478 target += bytesToWrite;
480 *sourceStart = source;
481 *targetStart = target;
482 return result;
485 /* --------------------------------------------------------------------- */
487 ConversionResult ConvertUTF8toUTF32 (
488 const UTF8** sourceStart, const UTF8* sourceEnd,
489 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
490 ConversionResult result = conversionOK;
491 const UTF8* source = *sourceStart;
492 UTF32* target = *targetStart;
493 while (source < sourceEnd) {
494 UTF32 ch = 0;
495 unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
496 if (source + extraBytesToRead >= sourceEnd) {
497 result = sourceExhausted; break;
499 /* Do this check whether lenient or strict */
500 if (! isLegalUTF8(source, extraBytesToRead+1)) {
501 result = sourceIllegal;
502 break;
505 * The cases all fall through. See "Note A" below.
507 switch (extraBytesToRead) {
508 case 5: ch += *source++; ch <<= 6;
509 case 4: ch += *source++; ch <<= 6;
510 case 3: ch += *source++; ch <<= 6;
511 case 2: ch += *source++; ch <<= 6;
512 case 1: ch += *source++; ch <<= 6;
513 case 0: ch += *source++;
515 ch -= offsetsFromUTF8[extraBytesToRead];
517 if (target >= targetEnd) {
518 source -= (extraBytesToRead+1); /* Back up the source pointer! */
519 result = targetExhausted; break;
521 if (ch <= UNI_MAX_LEGAL_UTF32) {
523 * UTF-16 surrogate values are illegal in UTF-32, and anything
524 * over Plane 17 (> 0x10FFFF) is illegal.
526 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
527 if (flags == strictConversion) {
528 source -= (extraBytesToRead+1); /* return to the illegal value itself */
529 result = sourceIllegal;
530 break;
531 } else {
532 *target++ = UNI_REPLACEMENT_CHAR;
534 } else {
535 *target++ = ch;
537 } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
538 result = sourceIllegal;
539 *target++ = UNI_REPLACEMENT_CHAR;
542 *sourceStart = source;
543 *targetStart = target;
544 return result;
547 /* ---------------------------------------------------------------------
549 Note A.
550 The fall-through switches in UTF-8 reading code save a
551 temp variable, some decrements & conditionals. The switches
552 are equivalent to the following loop:
554 int tmpBytesToRead = extraBytesToRead+1;
555 do {
556 ch += *source++;
557 --tmpBytesToRead;
558 if (tmpBytesToRead) ch <<= 6;
559 } while (tmpBytesToRead > 0);
561 In UTF-8 writing code, the switches on "bytesToWrite" are
562 similarly unrolled loops.
564 --------------------------------------------------------------------- */