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