michael@0: /* michael@0: * Copyright 2001-2004 Unicode, Inc. michael@0: * michael@0: * Disclaimer michael@0: * michael@0: * This source code is provided as is by Unicode, Inc. No claims are michael@0: * made as to fitness for any particular purpose. No warranties of any michael@0: * kind are expressed or implied. The recipient agrees to determine michael@0: * applicability of information provided. If this file has been michael@0: * purchased on magnetic or optical media from Unicode, Inc., the michael@0: * sole remedy for any claim will be exchange of defective media michael@0: * within 90 days of receipt. michael@0: * michael@0: * Limitations on Rights to Redistribute This Code michael@0: * michael@0: * Unicode, Inc. hereby grants the right to freely use the information michael@0: * supplied in this file in the creation of products supporting the michael@0: * Unicode Standard, and to make copies of this file in any form michael@0: * for internal or external distribution as long as this notice michael@0: * remains attached. michael@0: */ michael@0: michael@0: /* --------------------------------------------------------------------- michael@0: michael@0: Conversions between UTF32, UTF-16, and UTF-8. Source code file. michael@0: Author: Mark E. Davis, 1994. michael@0: Rev History: Rick McGowan, fixes & updates May 2001. michael@0: Sept 2001: fixed const & error conditions per michael@0: mods suggested by S. Parent & A. Lillich. michael@0: June 2002: Tim Dodd added detection and handling of incomplete michael@0: source sequences, enhanced error detection, added casts michael@0: to eliminate compiler warnings. michael@0: July 2003: slight mods to back out aggressive FFFE detection. michael@0: Jan 2004: updated switches in from-UTF8 conversions. michael@0: Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions. michael@0: michael@0: See the header file "ConvertUTF.h" for complete documentation. michael@0: michael@0: ------------------------------------------------------------------------ */ michael@0: michael@0: michael@0: #include "convert_UTF.h" michael@0: #ifdef CVTUTF_DEBUG michael@0: #include michael@0: #endif michael@0: michael@0: static const int halfShift = 10; /* used for shifting by 10 bits */ michael@0: michael@0: static const UTF32 halfBase = 0x0010000UL; michael@0: static const UTF32 halfMask = 0x3FFUL; michael@0: michael@0: #define UNI_SUR_HIGH_START (UTF32)0xD800 michael@0: #define UNI_SUR_HIGH_END (UTF32)0xDBFF michael@0: #define UNI_SUR_LOW_START (UTF32)0xDC00 michael@0: #define UNI_SUR_LOW_END (UTF32)0xDFFF michael@0: #define false 0 michael@0: #define true 1 michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: ConversionResult ConvertUTF32toUTF16 (const UTF32** sourceStart, const UTF32* sourceEnd, michael@0: UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { michael@0: ConversionResult result = conversionOK; michael@0: const UTF32* source = *sourceStart; michael@0: UTF16* target = *targetStart; michael@0: while (source < sourceEnd) { michael@0: UTF32 ch; michael@0: if (target >= targetEnd) { michael@0: result = targetExhausted; break; michael@0: } michael@0: ch = *source++; michael@0: if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ michael@0: /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */ michael@0: if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { michael@0: if (flags == strictConversion) { michael@0: --source; /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } else { michael@0: *target++ = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: } else { michael@0: *target++ = (UTF16)ch; /* normal case */ michael@0: } michael@0: } else if (ch > UNI_MAX_LEGAL_UTF32) { michael@0: if (flags == strictConversion) { michael@0: result = sourceIllegal; michael@0: } else { michael@0: *target++ = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: } else { michael@0: /* target is a character in range 0xFFFF - 0x10FFFF. */ michael@0: if (target + 1 >= targetEnd) { michael@0: --source; /* Back up source pointer! */ michael@0: result = targetExhausted; break; michael@0: } michael@0: ch -= halfBase; michael@0: *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); michael@0: *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); michael@0: } michael@0: } michael@0: *sourceStart = source; michael@0: *targetStart = target; michael@0: return result; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: ConversionResult ConvertUTF16toUTF32 (const UTF16** sourceStart, const UTF16* sourceEnd, michael@0: UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { michael@0: ConversionResult result = conversionOK; michael@0: const UTF16* source = *sourceStart; michael@0: UTF32* target = *targetStart; michael@0: UTF32 ch, ch2; michael@0: while (source < sourceEnd) { michael@0: const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ michael@0: ch = *source++; michael@0: /* If we have a surrogate pair, convert to UTF32 first. */ michael@0: if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { michael@0: /* If the 16 bits following the high surrogate are in the source buffer... */ michael@0: if (source < sourceEnd) { michael@0: ch2 = *source; michael@0: /* If it's a low surrogate, convert to UTF32. */ michael@0: if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { michael@0: ch = ((ch - UNI_SUR_HIGH_START) << halfShift) michael@0: + (ch2 - UNI_SUR_LOW_START) + halfBase; michael@0: ++source; michael@0: } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ michael@0: --source; /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: } else { /* We don't have the 16 bits following the high surrogate. */ michael@0: --source; /* return to the high surrogate */ michael@0: result = sourceExhausted; michael@0: break; michael@0: } michael@0: } else if (flags == strictConversion) { michael@0: /* UTF-16 surrogate values are illegal in UTF-32 */ michael@0: if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { michael@0: --source; /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: } michael@0: if (target >= targetEnd) { michael@0: source = oldSource; /* Back up source pointer! */ michael@0: result = targetExhausted; break; michael@0: } michael@0: *target++ = ch; michael@0: } michael@0: *sourceStart = source; michael@0: *targetStart = target; michael@0: #ifdef CVTUTF_DEBUG michael@0: if (result == sourceIllegal) { michael@0: fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2); michael@0: fflush(stderr); michael@0: } michael@0: #endif michael@0: return result; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: /* michael@0: * Index into the table below with the first byte of a UTF-8 sequence to michael@0: * get the number of trailing bytes that are supposed to follow it. michael@0: * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is michael@0: * left as-is for anyone who may want to do such conversion, which was michael@0: * allowed in earlier algorithms. michael@0: */ michael@0: static const char trailingBytesForUTF8[256] = { michael@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,0, michael@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,0, michael@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,0, michael@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,0, michael@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,0, michael@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,0, michael@0: 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, michael@0: 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 michael@0: }; michael@0: michael@0: /* michael@0: * Magic values subtracted from a buffer value during UTF8 conversion. michael@0: * This table contains as many values as there might be trailing bytes michael@0: * in a UTF-8 sequence. michael@0: */ michael@0: static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL, michael@0: 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; michael@0: michael@0: /* michael@0: * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed michael@0: * into the first byte, depending on how many bytes follow. There are michael@0: * as many entries in this table as there are UTF-8 sequence types. michael@0: * (I.e., one byte sequence, two byte... etc.). Remember that sequencs michael@0: * for *legal* UTF-8 will be 4 or fewer bytes total. michael@0: */ michael@0: static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: /* The interface converts a whole buffer to avoid function-call overhead. michael@0: * Constants have been gathered. Loops & conditionals have been removed as michael@0: * much as possible for efficiency, in favor of drop-through switches. michael@0: * (See "Note A" at the bottom of the file for equivalent code.) michael@0: * If your compiler supports it, the "isLegalUTF8" call can be turned michael@0: * into an inline function. michael@0: */ michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: ConversionResult ConvertUTF16toUTF8 (const UTF16** sourceStart, const UTF16* sourceEnd, michael@0: UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { michael@0: ConversionResult result = conversionOK; michael@0: const UTF16* source = *sourceStart; michael@0: UTF8* target = *targetStart; michael@0: while (source < sourceEnd) { michael@0: UTF32 ch; michael@0: unsigned short bytesToWrite = 0; michael@0: const UTF32 byteMask = 0xBF; michael@0: const UTF32 byteMark = 0x80; michael@0: const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ michael@0: ch = *source++; michael@0: /* If we have a surrogate pair, convert to UTF32 first. */ michael@0: if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { michael@0: /* If the 16 bits following the high surrogate are in the source buffer... */ michael@0: if (source < sourceEnd) { michael@0: UTF32 ch2 = *source; michael@0: /* If it's a low surrogate, convert to UTF32. */ michael@0: if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { michael@0: ch = ((ch - UNI_SUR_HIGH_START) << halfShift) michael@0: + (ch2 - UNI_SUR_LOW_START) + halfBase; michael@0: ++source; michael@0: } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ michael@0: --source; /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: } else { /* We don't have the 16 bits following the high surrogate. */ michael@0: --source; /* return to the high surrogate */ michael@0: result = sourceExhausted; michael@0: break; michael@0: } michael@0: } else if (flags == strictConversion) { michael@0: /* UTF-16 surrogate values are illegal in UTF-32 */ michael@0: if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { michael@0: --source; /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: } michael@0: /* Figure out how many bytes the result will require */ michael@0: if (ch < (UTF32)0x80) { bytesToWrite = 1; michael@0: } else if (ch < (UTF32)0x800) { bytesToWrite = 2; michael@0: } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; michael@0: } else if (ch < (UTF32)0x110000) { bytesToWrite = 4; michael@0: } else { bytesToWrite = 3; michael@0: ch = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: michael@0: target += bytesToWrite; michael@0: if (target > targetEnd) { michael@0: source = oldSource; /* Back up source pointer! */ michael@0: target -= bytesToWrite; result = targetExhausted; break; michael@0: } michael@0: switch (bytesToWrite) { /* note: everything falls through. */ michael@0: case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; michael@0: case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; michael@0: case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; michael@0: case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]); michael@0: } michael@0: target += bytesToWrite; michael@0: } michael@0: *sourceStart = source; michael@0: *targetStart = target; michael@0: return result; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: /* michael@0: * Utility routine to tell whether a sequence of bytes is legal UTF-8. michael@0: * This must be called with the length pre-determined by the first byte. michael@0: * If not calling this from ConvertUTF8to*, then the length can be set by: michael@0: * length = trailingBytesForUTF8[*source]+1; michael@0: * and the sequence is illegal right away if there aren't that many bytes michael@0: * available. michael@0: * If presented with a length > 4, this returns false. The Unicode michael@0: * definition of UTF-8 goes up to 4-byte sequences. michael@0: */ michael@0: michael@0: static Boolean isLegalUTF8(const UTF8 *source, int length) { michael@0: UTF8 a; michael@0: const UTF8 *srcptr = source+length; michael@0: switch (length) { michael@0: default: return false; michael@0: /* Everything else falls through when "true"... */ michael@0: case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; michael@0: case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; michael@0: case 2: if ((a = (*--srcptr)) > 0xBF) return false; michael@0: michael@0: switch (*source) { michael@0: /* no fall-through in this inner switch */ michael@0: case 0xE0: if (a < 0xA0) return false; break; michael@0: case 0xED: if (a > 0x9F) return false; break; michael@0: case 0xF0: if (a < 0x90) return false; break; michael@0: case 0xF4: if (a > 0x8F) return false; break; michael@0: default: if (a < 0x80) return false; michael@0: } michael@0: michael@0: case 1: if (*source >= 0x80 && *source < 0xC2) return false; michael@0: } michael@0: if (*source > 0xF4) return false; michael@0: return true; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: /* michael@0: * Exported function to return whether a UTF-8 sequence is legal or not. michael@0: * This is not used here; it's just exported. michael@0: */ michael@0: Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) { michael@0: int length = trailingBytesForUTF8[*source]+1; michael@0: if (source+length > sourceEnd) { michael@0: return false; michael@0: } michael@0: return isLegalUTF8(source, length); michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: ConversionResult ConvertUTF8toUTF16 (const UTF8** sourceStart, const UTF8* sourceEnd, michael@0: UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { michael@0: ConversionResult result = conversionOK; michael@0: const UTF8* source = *sourceStart; michael@0: UTF16* target = *targetStart; michael@0: while (source < sourceEnd) { michael@0: UTF32 ch = 0; michael@0: unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; michael@0: if (source + extraBytesToRead >= sourceEnd) { michael@0: result = sourceExhausted; break; michael@0: } michael@0: /* Do this check whether lenient or strict */ michael@0: if (! isLegalUTF8(source, extraBytesToRead+1)) { michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: /* michael@0: * The cases all fall through. See "Note A" below. michael@0: */ michael@0: switch (extraBytesToRead) { michael@0: case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ michael@0: case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ michael@0: case 3: ch += *source++; ch <<= 6; michael@0: case 2: ch += *source++; ch <<= 6; michael@0: case 1: ch += *source++; ch <<= 6; michael@0: case 0: ch += *source++; michael@0: } michael@0: ch -= offsetsFromUTF8[extraBytesToRead]; michael@0: michael@0: if (target >= targetEnd) { michael@0: source -= (extraBytesToRead+1); /* Back up source pointer! */ michael@0: result = targetExhausted; break; michael@0: } michael@0: if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ michael@0: /* UTF-16 surrogate values are illegal in UTF-32 */ michael@0: if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { michael@0: if (flags == strictConversion) { michael@0: source -= (extraBytesToRead+1); /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } else { michael@0: *target++ = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: } else { michael@0: *target++ = (UTF16)ch; /* normal case */ michael@0: } michael@0: } else if (ch > UNI_MAX_UTF16) { michael@0: if (flags == strictConversion) { michael@0: result = sourceIllegal; michael@0: source -= (extraBytesToRead+1); /* return to the start */ michael@0: break; /* Bail out; shouldn't continue */ michael@0: } else { michael@0: *target++ = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: } else { michael@0: /* target is a character in range 0xFFFF - 0x10FFFF. */ michael@0: if (target + 1 >= targetEnd) { michael@0: source -= (extraBytesToRead+1); /* Back up source pointer! */ michael@0: result = targetExhausted; break; michael@0: } michael@0: ch -= halfBase; michael@0: *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); michael@0: *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); michael@0: } michael@0: } michael@0: *sourceStart = source; michael@0: *targetStart = target; michael@0: return result; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: ConversionResult ConvertUTF32toUTF8 (const UTF32** sourceStart, const UTF32* sourceEnd, michael@0: UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { michael@0: ConversionResult result = conversionOK; michael@0: const UTF32* source = *sourceStart; michael@0: UTF8* target = *targetStart; michael@0: while (source < sourceEnd) { michael@0: UTF32 ch; michael@0: unsigned short bytesToWrite = 0; michael@0: const UTF32 byteMask = 0xBF; michael@0: const UTF32 byteMark = 0x80; michael@0: ch = *source++; michael@0: if (flags == strictConversion ) { michael@0: /* UTF-16 surrogate values are illegal in UTF-32 */ michael@0: if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { michael@0: --source; /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: } michael@0: /* michael@0: * Figure out how many bytes the result will require. Turn any michael@0: * illegally large UTF32 things (> Plane 17) into replacement chars. michael@0: */ michael@0: if (ch < (UTF32)0x80) { bytesToWrite = 1; michael@0: } else if (ch < (UTF32)0x800) { bytesToWrite = 2; michael@0: } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; michael@0: } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4; michael@0: } else { bytesToWrite = 3; michael@0: ch = UNI_REPLACEMENT_CHAR; michael@0: result = sourceIllegal; michael@0: } michael@0: michael@0: target += bytesToWrite; michael@0: if (target > targetEnd) { michael@0: --source; /* Back up source pointer! */ michael@0: target -= bytesToWrite; result = targetExhausted; break; michael@0: } michael@0: switch (bytesToWrite) { /* note: everything falls through. */ michael@0: case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; michael@0: case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; michael@0: case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; michael@0: case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]); michael@0: } michael@0: target += bytesToWrite; michael@0: } michael@0: *sourceStart = source; michael@0: *targetStart = target; michael@0: return result; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- */ michael@0: michael@0: ConversionResult ConvertUTF8toUTF32 (const UTF8** sourceStart, const UTF8* sourceEnd, michael@0: UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { michael@0: ConversionResult result = conversionOK; michael@0: const UTF8* source = *sourceStart; michael@0: UTF32* target = *targetStart; michael@0: while (source < sourceEnd) { michael@0: UTF32 ch = 0; michael@0: unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; michael@0: if (source + extraBytesToRead >= sourceEnd) { michael@0: result = sourceExhausted; break; michael@0: } michael@0: /* Do this check whether lenient or strict */ michael@0: if (! isLegalUTF8(source, extraBytesToRead+1)) { michael@0: result = sourceIllegal; michael@0: break; michael@0: } michael@0: /* michael@0: * The cases all fall through. See "Note A" below. michael@0: */ michael@0: switch (extraBytesToRead) { michael@0: case 5: ch += *source++; ch <<= 6; michael@0: case 4: ch += *source++; ch <<= 6; michael@0: case 3: ch += *source++; ch <<= 6; michael@0: case 2: ch += *source++; ch <<= 6; michael@0: case 1: ch += *source++; ch <<= 6; michael@0: case 0: ch += *source++; michael@0: } michael@0: ch -= offsetsFromUTF8[extraBytesToRead]; michael@0: michael@0: if (target >= targetEnd) { michael@0: source -= (extraBytesToRead+1); /* Back up the source pointer! */ michael@0: result = targetExhausted; break; michael@0: } michael@0: if (ch <= UNI_MAX_LEGAL_UTF32) { michael@0: /* michael@0: * UTF-16 surrogate values are illegal in UTF-32, and anything michael@0: * over Plane 17 (> 0x10FFFF) is illegal. michael@0: */ michael@0: if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { michael@0: if (flags == strictConversion) { michael@0: source -= (extraBytesToRead+1); /* return to the illegal value itself */ michael@0: result = sourceIllegal; michael@0: break; michael@0: } else { michael@0: *target++ = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: } else { michael@0: *target++ = ch; michael@0: } michael@0: } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */ michael@0: result = sourceIllegal; michael@0: *target++ = UNI_REPLACEMENT_CHAR; michael@0: } michael@0: } michael@0: *sourceStart = source; michael@0: *targetStart = target; michael@0: return result; michael@0: } michael@0: michael@0: /* --------------------------------------------------------------------- michael@0: michael@0: Note A. michael@0: The fall-through switches in UTF-8 reading code save a michael@0: temp variable, some decrements & conditionals. The switches michael@0: are equivalent to the following loop: michael@0: { michael@0: int tmpBytesToRead = extraBytesToRead+1; michael@0: do { michael@0: ch += *source++; michael@0: --tmpBytesToRead; michael@0: if (tmpBytesToRead) ch <<= 6; michael@0: } while (tmpBytesToRead > 0); michael@0: } michael@0: In UTF-8 writing code, the switches on "bytesToWrite" are michael@0: similarly unrolled loops. michael@0: michael@0: --------------------------------------------------------------------- */