annotate trunk/src/Support/ConvertUTF.c @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents e14c70d1b171
children
rev   line source
tomwalters@268 1 /*
tomwalters@268 2 * Copyright 2001-2004 Unicode, Inc.
tomwalters@268 3 *
tomwalters@268 4 * Disclaimer
tomwalters@268 5 *
tomwalters@268 6 * This source code is provided as is by Unicode, Inc. No claims are
tomwalters@268 7 * made as to fitness for any particular purpose. No warranties of any
tomwalters@268 8 * kind are expressed or implied. The recipient agrees to determine
tomwalters@268 9 * applicability of information provided. If this file has been
tomwalters@268 10 * purchased on magnetic or optical media from Unicode, Inc., the
tomwalters@268 11 * sole remedy for any claim will be exchange of defective media
tomwalters@268 12 * within 90 days of receipt.
tomwalters@268 13 *
tomwalters@268 14 * Limitations on Rights to Redistribute This Code
tomwalters@268 15 *
tomwalters@268 16 * Unicode, Inc. hereby grants the right to freely use the information
tomwalters@268 17 * supplied in this file in the creation of products supporting the
tomwalters@268 18 * Unicode Standard, and to make copies of this file in any form
tomwalters@268 19 * for internal or external distribution as long as this notice
tomwalters@268 20 * remains attached.
tomwalters@268 21 */
tomwalters@268 22
tomwalters@268 23 /* ---------------------------------------------------------------------
tomwalters@268 24
tomwalters@268 25 Conversions between UTF32, UTF-16, and UTF-8. Source code file.
tomwalters@268 26 Author: Mark E. Davis, 1994.
tomwalters@268 27 Rev History: Rick McGowan, fixes & updates May 2001.
tomwalters@268 28 Sept 2001: fixed const & error conditions per
tomwalters@268 29 mods suggested by S. Parent & A. Lillich.
tomwalters@268 30 June 2002: Tim Dodd added detection and handling of incomplete
tomwalters@268 31 source sequences, enhanced error detection, added casts
tomwalters@268 32 to eliminate compiler warnings.
tomwalters@268 33 July 2003: slight mods to back out aggressive FFFE detection.
tomwalters@268 34 Jan 2004: updated switches in from-UTF8 conversions.
tomwalters@268 35 Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
tomwalters@268 36
tomwalters@268 37 See the header file "ConvertUTF.h" for complete documentation.
tomwalters@268 38
tomwalters@268 39 ------------------------------------------------------------------------ */
tomwalters@268 40
tomwalters@268 41
tomwalters@268 42 #include "ConvertUTF.h"
tomwalters@268 43 #ifdef CVTUTF_DEBUG
tomwalters@268 44 #include <stdio.h>
tomwalters@268 45 #endif
tomwalters@268 46
tomwalters@268 47 static const int halfShift = 10; /* used for shifting by 10 bits */
tomwalters@268 48
tomwalters@268 49 static const UTF32 halfBase = 0x0010000UL;
tomwalters@268 50 static const UTF32 halfMask = 0x3FFUL;
tomwalters@268 51
tomwalters@268 52 #define UNI_SUR_HIGH_START (UTF32)0xD800
tomwalters@268 53 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
tomwalters@268 54 #define UNI_SUR_LOW_START (UTF32)0xDC00
tomwalters@268 55 #define UNI_SUR_LOW_END (UTF32)0xDFFF
tomwalters@268 56 #define false 0
tomwalters@268 57 #define true 1
tomwalters@268 58
tomwalters@268 59 /* --------------------------------------------------------------------- */
tomwalters@268 60
tomwalters@268 61 ConversionResult ConvertUTF32toUTF16 (
tomwalters@268 62 const UTF32** sourceStart, const UTF32* sourceEnd,
tomwalters@268 63 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
tomwalters@268 64 ConversionResult result = conversionOK;
tomwalters@268 65 const UTF32* source = *sourceStart;
tomwalters@268 66 UTF16* target = *targetStart;
tomwalters@268 67 while (source < sourceEnd) {
tomwalters@268 68 UTF32 ch;
tomwalters@268 69 if (target >= targetEnd) {
tomwalters@268 70 result = targetExhausted; break;
tomwalters@268 71 }
tomwalters@268 72 ch = *source++;
tomwalters@268 73 if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
tomwalters@268 74 /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
tomwalters@268 75 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
tomwalters@268 76 if (flags == strictConversion) {
tomwalters@268 77 --source; /* return to the illegal value itself */
tomwalters@268 78 result = sourceIllegal;
tomwalters@268 79 break;
tomwalters@268 80 } else {
tomwalters@268 81 *target++ = UNI_REPLACEMENT_CHAR;
tomwalters@268 82 }
tomwalters@268 83 } else {
tomwalters@268 84 *target++ = (UTF16)ch; /* normal case */
tomwalters@268 85 }
tomwalters@268 86 } else if (ch > UNI_MAX_LEGAL_UTF32) {
tomwalters@268 87 if (flags == strictConversion) {
tomwalters@268 88 result = sourceIllegal;
tomwalters@268 89 } else {
tomwalters@268 90 *target++ = UNI_REPLACEMENT_CHAR;
tomwalters@268 91 }
tomwalters@268 92 } else {
tomwalters@268 93 /* target is a character in range 0xFFFF - 0x10FFFF. */
tomwalters@268 94 if (target + 1 >= targetEnd) {
tomwalters@268 95 --source; /* Back up source pointer! */
tomwalters@268 96 result = targetExhausted; break;
tomwalters@268 97 }
tomwalters@268 98 ch -= halfBase;
tomwalters@268 99 *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
tomwalters@268 100 *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
tomwalters@268 101 }
tomwalters@268 102 }
tomwalters@268 103 *sourceStart = source;
tomwalters@268 104 *targetStart = target;
tomwalters@268 105 return result;
tomwalters@268 106 }
tomwalters@268 107
tomwalters@268 108 /* --------------------------------------------------------------------- */
tomwalters@268 109
tomwalters@268 110 ConversionResult ConvertUTF16toUTF32 (
tomwalters@268 111 const UTF16** sourceStart, const UTF16* sourceEnd,
tomwalters@268 112 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
tomwalters@268 113 ConversionResult result = conversionOK;
tomwalters@268 114 const UTF16* source = *sourceStart;
tomwalters@268 115 UTF32* target = *targetStart;
tomwalters@268 116 UTF32 ch, ch2;
tomwalters@268 117 while (source < sourceEnd) {
tomwalters@268 118 const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
tomwalters@268 119 ch = *source++;
tomwalters@268 120 /* If we have a surrogate pair, convert to UTF32 first. */
tomwalters@268 121 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
tomwalters@268 122 /* If the 16 bits following the high surrogate are in the source buffer... */
tomwalters@268 123 if (source < sourceEnd) {
tomwalters@268 124 ch2 = *source;
tomwalters@268 125 /* If it's a low surrogate, convert to UTF32. */
tomwalters@268 126 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
tomwalters@268 127 ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
tomwalters@268 128 + (ch2 - UNI_SUR_LOW_START) + halfBase;
tomwalters@268 129 ++source;
tomwalters@268 130 } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
tomwalters@268 131 --source; /* return to the illegal value itself */
tomwalters@268 132 result = sourceIllegal;
tomwalters@268 133 break;
tomwalters@268 134 }
tomwalters@268 135 } else { /* We don't have the 16 bits following the high surrogate. */
tomwalters@268 136 --source; /* return to the high surrogate */
tomwalters@268 137 result = sourceExhausted;
tomwalters@268 138 break;
tomwalters@268 139 }
tomwalters@268 140 } else if (flags == strictConversion) {
tomwalters@268 141 /* UTF-16 surrogate values are illegal in UTF-32 */
tomwalters@268 142 if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
tomwalters@268 143 --source; /* return to the illegal value itself */
tomwalters@268 144 result = sourceIllegal;
tomwalters@268 145 break;
tomwalters@268 146 }
tomwalters@268 147 }
tomwalters@268 148 if (target >= targetEnd) {
tomwalters@268 149 source = oldSource; /* Back up source pointer! */
tomwalters@268 150 result = targetExhausted; break;
tomwalters@268 151 }
tomwalters@268 152 *target++ = ch;
tomwalters@268 153 }
tomwalters@268 154 *sourceStart = source;
tomwalters@268 155 *targetStart = target;
tomwalters@268 156 #ifdef CVTUTF_DEBUG
tomwalters@268 157 if (result == sourceIllegal) {
tomwalters@268 158 fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
tomwalters@268 159 fflush(stderr);
tomwalters@268 160 }
tomwalters@268 161 #endif
tomwalters@268 162 return result;
tomwalters@268 163 }
tomwalters@268 164
tomwalters@268 165 /* --------------------------------------------------------------------- */
tomwalters@268 166
tomwalters@268 167 /*
tomwalters@268 168 * Index into the table below with the first byte of a UTF-8 sequence to
tomwalters@268 169 * get the number of trailing bytes that are supposed to follow it.
tomwalters@268 170 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
tomwalters@268 171 * left as-is for anyone who may want to do such conversion, which was
tomwalters@268 172 * allowed in earlier algorithms.
tomwalters@268 173 */
tomwalters@268 174 static const char trailingBytesForUTF8[256] = {
tomwalters@268 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,
tomwalters@268 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,
tomwalters@268 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,
tomwalters@268 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,
tomwalters@268 179 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 180 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 181 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 182 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 183 };
tomwalters@268 184
tomwalters@268 185 /*
tomwalters@268 186 * Magic values subtracted from a buffer value during UTF8 conversion.
tomwalters@268 187 * This table contains as many values as there might be trailing bytes
tomwalters@268 188 * in a UTF-8 sequence.
tomwalters@268 189 */
tomwalters@268 190 static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
tomwalters@268 191 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
tomwalters@268 192
tomwalters@268 193 /*
tomwalters@268 194 * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
tomwalters@268 195 * into the first byte, depending on how many bytes follow. There are
tomwalters@268 196 * as many entries in this table as there are UTF-8 sequence types.
tomwalters@268 197 * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
tomwalters@268 198 * for *legal* UTF-8 will be 4 or fewer bytes total.
tomwalters@268 199 */
tomwalters@268 200 static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
tomwalters@268 201
tomwalters@268 202 /* --------------------------------------------------------------------- */
tomwalters@268 203
tomwalters@268 204 /* The interface converts a whole buffer to avoid function-call overhead.
tomwalters@268 205 * Constants have been gathered. Loops & conditionals have been removed as
tomwalters@268 206 * much as possible for efficiency, in favor of drop-through switches.
tomwalters@268 207 * (See "Note A" at the bottom of the file for equivalent code.)
tomwalters@268 208 * If your compiler supports it, the "isLegalUTF8" call can be turned
tomwalters@268 209 * into an inline function.
tomwalters@268 210 */
tomwalters@268 211
tomwalters@268 212 /* --------------------------------------------------------------------- */
tomwalters@268 213
tomwalters@268 214 ConversionResult ConvertUTF16toUTF8 (
tomwalters@268 215 const UTF16** sourceStart, const UTF16* sourceEnd,
tomwalters@268 216 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
tomwalters@268 217 ConversionResult result = conversionOK;
tomwalters@268 218 const UTF16* source = *sourceStart;
tomwalters@268 219 UTF8* target = *targetStart;
tomwalters@268 220 while (source < sourceEnd) {
tomwalters@268 221 UTF32 ch;
tomwalters@268 222 unsigned short bytesToWrite = 0;
tomwalters@268 223 const UTF32 byteMask = 0xBF;
tomwalters@268 224 const UTF32 byteMark = 0x80;
tomwalters@268 225 const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
tomwalters@268 226 ch = *source++;
tomwalters@268 227 /* If we have a surrogate pair, convert to UTF32 first. */
tomwalters@268 228 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
tomwalters@268 229 /* If the 16 bits following the high surrogate are in the source buffer... */
tomwalters@268 230 if (source < sourceEnd) {
tomwalters@268 231 UTF32 ch2 = *source;
tomwalters@268 232 /* If it's a low surrogate, convert to UTF32. */
tomwalters@268 233 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
tomwalters@268 234 ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
tomwalters@268 235 + (ch2 - UNI_SUR_LOW_START) + halfBase;
tomwalters@268 236 ++source;
tomwalters@268 237 } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
tomwalters@268 238 --source; /* return to the illegal value itself */
tomwalters@268 239 result = sourceIllegal;
tomwalters@268 240 break;
tomwalters@268 241 }
tomwalters@268 242 } else { /* We don't have the 16 bits following the high surrogate. */
tomwalters@268 243 --source; /* return to the high surrogate */
tomwalters@268 244 result = sourceExhausted;
tomwalters@268 245 break;
tomwalters@268 246 }
tomwalters@268 247 } else if (flags == strictConversion) {
tomwalters@268 248 /* UTF-16 surrogate values are illegal in UTF-32 */
tomwalters@268 249 if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
tomwalters@268 250 --source; /* return to the illegal value itself */
tomwalters@268 251 result = sourceIllegal;
tomwalters@268 252 break;
tomwalters@268 253 }
tomwalters@268 254 }
tomwalters@268 255 /* Figure out how many bytes the result will require */
tomwalters@268 256 if (ch < (UTF32)0x80) { bytesToWrite = 1;
tomwalters@268 257 } else if (ch < (UTF32)0x800) { bytesToWrite = 2;
tomwalters@268 258 } else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
tomwalters@268 259 } else if (ch < (UTF32)0x110000) { bytesToWrite = 4;
tomwalters@268 260 } else { bytesToWrite = 3;
tomwalters@268 261 ch = UNI_REPLACEMENT_CHAR;
tomwalters@268 262 }
tomwalters@268 263
tomwalters@268 264 target += bytesToWrite;
tomwalters@268 265 if (target > targetEnd) {
tomwalters@268 266 source = oldSource; /* Back up source pointer! */
tomwalters@268 267 target -= bytesToWrite; result = targetExhausted; break;
tomwalters@268 268 }
tomwalters@268 269 switch (bytesToWrite) { /* note: everything falls through. */
tomwalters@268 270 case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
tomwalters@268 271 case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
tomwalters@268 272 case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
tomwalters@268 273 case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]);
tomwalters@268 274 }
tomwalters@268 275 target += bytesToWrite;
tomwalters@268 276 }
tomwalters@268 277 *sourceStart = source;
tomwalters@268 278 *targetStart = target;
tomwalters@268 279 return result;
tomwalters@268 280 }
tomwalters@268 281
tomwalters@268 282 /* --------------------------------------------------------------------- */
tomwalters@268 283
tomwalters@268 284 /*
tomwalters@268 285 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
tomwalters@268 286 * This must be called with the length pre-determined by the first byte.
tomwalters@268 287 * If not calling this from ConvertUTF8to*, then the length can be set by:
tomwalters@268 288 * length = trailingBytesForUTF8[*source]+1;
tomwalters@268 289 * and the sequence is illegal right away if there aren't that many bytes
tomwalters@268 290 * available.
tomwalters@268 291 * If presented with a length > 4, this returns false. The Unicode
tomwalters@268 292 * definition of UTF-8 goes up to 4-byte sequences.
tomwalters@268 293 */
tomwalters@268 294
tomwalters@268 295 static Boolean isLegalUTF8(const UTF8 *source, int length) {
tomwalters@268 296 UTF8 a;
tomwalters@268 297 const UTF8 *srcptr = source+length;
tomwalters@268 298 switch (length) {
tomwalters@268 299 default: return false;
tomwalters@268 300 /* Everything else falls through when "true"... */
tomwalters@268 301 case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
tomwalters@268 302 case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
tomwalters@268 303 case 2: if ((a = (*--srcptr)) > 0xBF) return false;
tomwalters@268 304
tomwalters@268 305 switch (*source) {
tomwalters@268 306 /* no fall-through in this inner switch */
tomwalters@268 307 case 0xE0: if (a < 0xA0) return false; break;
tomwalters@268 308 case 0xED: if (a > 0x9F) return false; break;
tomwalters@268 309 case 0xF0: if (a < 0x90) return false; break;
tomwalters@268 310 case 0xF4: if (a > 0x8F) return false; break;
tomwalters@268 311 default: if (a < 0x80) return false;
tomwalters@268 312 }
tomwalters@268 313
tomwalters@268 314 case 1: if (*source >= 0x80 && *source < 0xC2) return false;
tomwalters@268 315 }
tomwalters@268 316 if (*source > 0xF4) return false;
tomwalters@268 317 return true;
tomwalters@268 318 }
tomwalters@268 319
tomwalters@268 320 /* --------------------------------------------------------------------- */
tomwalters@268 321
tomwalters@268 322 /*
tomwalters@268 323 * Exported function to return whether a UTF-8 sequence is legal or not.
tomwalters@268 324 * This is not used here; it's just exported.
tomwalters@268 325 */
tomwalters@268 326 Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
tomwalters@268 327 int length = trailingBytesForUTF8[*source]+1;
tomwalters@268 328 if (source+length > sourceEnd) {
tomwalters@268 329 return false;
tomwalters@268 330 }
tomwalters@268 331 return isLegalUTF8(source, length);
tomwalters@268 332 }
tomwalters@268 333
tomwalters@268 334 /* --------------------------------------------------------------------- */
tomwalters@268 335
tomwalters@268 336 ConversionResult ConvertUTF8toUTF16 (
tomwalters@268 337 const UTF8** sourceStart, const UTF8* sourceEnd,
tomwalters@268 338 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
tomwalters@268 339 ConversionResult result = conversionOK;
tomwalters@268 340 const UTF8* source = *sourceStart;
tomwalters@268 341 UTF16* target = *targetStart;
tomwalters@268 342 while (source < sourceEnd) {
tomwalters@268 343 UTF32 ch = 0;
tomwalters@268 344 unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
tomwalters@268 345 if (source + extraBytesToRead >= sourceEnd) {
tomwalters@268 346 result = sourceExhausted; break;
tomwalters@268 347 }
tomwalters@268 348 /* Do this check whether lenient or strict */
tomwalters@268 349 if (! isLegalUTF8(source, extraBytesToRead+1)) {
tomwalters@268 350 result = sourceIllegal;
tomwalters@268 351 break;
tomwalters@268 352 }
tomwalters@268 353 /*
tomwalters@268 354 * The cases all fall through. See "Note A" below.
tomwalters@268 355 */
tomwalters@268 356 switch (extraBytesToRead) {
tomwalters@268 357 case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
tomwalters@268 358 case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
tomwalters@268 359 case 3: ch += *source++; ch <<= 6;
tomwalters@268 360 case 2: ch += *source++; ch <<= 6;
tomwalters@268 361 case 1: ch += *source++; ch <<= 6;
tomwalters@268 362 case 0: ch += *source++;
tomwalters@268 363 }
tomwalters@268 364 ch -= offsetsFromUTF8[extraBytesToRead];
tomwalters@268 365
tomwalters@268 366 if (target >= targetEnd) {
tomwalters@268 367 source -= (extraBytesToRead+1); /* Back up source pointer! */
tomwalters@268 368 result = targetExhausted; break;
tomwalters@268 369 }
tomwalters@268 370 if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
tomwalters@268 371 /* UTF-16 surrogate values are illegal in UTF-32 */
tomwalters@268 372 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
tomwalters@268 373 if (flags == strictConversion) {
tomwalters@268 374 source -= (extraBytesToRead+1); /* return to the illegal value itself */
tomwalters@268 375 result = sourceIllegal;
tomwalters@268 376 break;
tomwalters@268 377 } else {
tomwalters@268 378 *target++ = UNI_REPLACEMENT_CHAR;
tomwalters@268 379 }
tomwalters@268 380 } else {
tomwalters@268 381 *target++ = (UTF16)ch; /* normal case */
tomwalters@268 382 }
tomwalters@268 383 } else if (ch > UNI_MAX_UTF16) {
tomwalters@268 384 if (flags == strictConversion) {
tomwalters@268 385 result = sourceIllegal;
tomwalters@268 386 source -= (extraBytesToRead+1); /* return to the start */
tomwalters@268 387 break; /* Bail out; shouldn't continue */
tomwalters@268 388 } else {
tomwalters@268 389 *target++ = UNI_REPLACEMENT_CHAR;
tomwalters@268 390 }
tomwalters@268 391 } else {
tomwalters@268 392 /* target is a character in range 0xFFFF - 0x10FFFF. */
tomwalters@268 393 if (target + 1 >= targetEnd) {
tomwalters@268 394 source -= (extraBytesToRead+1); /* Back up source pointer! */
tomwalters@268 395 result = targetExhausted; break;
tomwalters@268 396 }
tomwalters@268 397 ch -= halfBase;
tomwalters@268 398 *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
tomwalters@268 399 *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
tomwalters@268 400 }
tomwalters@268 401 }
tomwalters@268 402 *sourceStart = source;
tomwalters@268 403 *targetStart = target;
tomwalters@268 404 return result;
tomwalters@268 405 }
tomwalters@268 406
tomwalters@268 407 /* --------------------------------------------------------------------- */
tomwalters@268 408
tomwalters@268 409 ConversionResult ConvertUTF32toUTF8 (
tomwalters@268 410 const UTF32** sourceStart, const UTF32* sourceEnd,
tomwalters@268 411 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
tomwalters@268 412 ConversionResult result = conversionOK;
tomwalters@268 413 const UTF32* source = *sourceStart;
tomwalters@268 414 UTF8* target = *targetStart;
tomwalters@268 415 while (source < sourceEnd) {
tomwalters@268 416 UTF32 ch;
tomwalters@268 417 unsigned short bytesToWrite = 0;
tomwalters@268 418 const UTF32 byteMask = 0xBF;
tomwalters@268 419 const UTF32 byteMark = 0x80;
tomwalters@268 420 ch = *source++;
tomwalters@268 421 if (flags == strictConversion ) {
tomwalters@268 422 /* UTF-16 surrogate values are illegal in UTF-32 */
tomwalters@268 423 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
tomwalters@268 424 --source; /* return to the illegal value itself */
tomwalters@268 425 result = sourceIllegal;
tomwalters@268 426 break;
tomwalters@268 427 }
tomwalters@268 428 }
tomwalters@268 429 /*
tomwalters@268 430 * Figure out how many bytes the result will require. Turn any
tomwalters@268 431 * illegally large UTF32 things (> Plane 17) into replacement chars.
tomwalters@268 432 */
tomwalters@268 433 if (ch < (UTF32)0x80) { bytesToWrite = 1;
tomwalters@268 434 } else if (ch < (UTF32)0x800) { bytesToWrite = 2;
tomwalters@268 435 } else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
tomwalters@268 436 } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4;
tomwalters@268 437 } else { bytesToWrite = 3;
tomwalters@268 438 ch = UNI_REPLACEMENT_CHAR;
tomwalters@268 439 result = sourceIllegal;
tomwalters@268 440 }
tomwalters@268 441
tomwalters@268 442 target += bytesToWrite;
tomwalters@268 443 if (target > targetEnd) {
tomwalters@268 444 --source; /* Back up source pointer! */
tomwalters@268 445 target -= bytesToWrite; result = targetExhausted; break;
tomwalters@268 446 }
tomwalters@268 447 switch (bytesToWrite) { /* note: everything falls through. */
tomwalters@268 448 case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
tomwalters@268 449 case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
tomwalters@268 450 case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
tomwalters@268 451 case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
tomwalters@268 452 }
tomwalters@268 453 target += bytesToWrite;
tomwalters@268 454 }
tomwalters@268 455 *sourceStart = source;
tomwalters@268 456 *targetStart = target;
tomwalters@268 457 return result;
tomwalters@268 458 }
tomwalters@268 459
tomwalters@268 460 /* --------------------------------------------------------------------- */
tomwalters@268 461
tomwalters@268 462 ConversionResult ConvertUTF8toUTF32 (
tomwalters@268 463 const UTF8** sourceStart, const UTF8* sourceEnd,
tomwalters@268 464 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
tomwalters@268 465 ConversionResult result = conversionOK;
tomwalters@268 466 const UTF8* source = *sourceStart;
tomwalters@268 467 UTF32* target = *targetStart;
tomwalters@268 468 while (source < sourceEnd) {
tomwalters@268 469 UTF32 ch = 0;
tomwalters@268 470 unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
tomwalters@268 471 if (source + extraBytesToRead >= sourceEnd) {
tomwalters@268 472 result = sourceExhausted; break;
tomwalters@268 473 }
tomwalters@268 474 /* Do this check whether lenient or strict */
tomwalters@268 475 if (! isLegalUTF8(source, extraBytesToRead+1)) {
tomwalters@268 476 result = sourceIllegal;
tomwalters@268 477 break;
tomwalters@268 478 }
tomwalters@268 479 /*
tomwalters@268 480 * The cases all fall through. See "Note A" below.
tomwalters@268 481 */
tomwalters@268 482 switch (extraBytesToRead) {
tomwalters@268 483 case 5: ch += *source++; ch <<= 6;
tomwalters@268 484 case 4: ch += *source++; ch <<= 6;
tomwalters@268 485 case 3: ch += *source++; ch <<= 6;
tomwalters@268 486 case 2: ch += *source++; ch <<= 6;
tomwalters@268 487 case 1: ch += *source++; ch <<= 6;
tomwalters@268 488 case 0: ch += *source++;
tomwalters@268 489 }
tomwalters@268 490 ch -= offsetsFromUTF8[extraBytesToRead];
tomwalters@268 491
tomwalters@268 492 if (target >= targetEnd) {
tomwalters@268 493 source -= (extraBytesToRead+1); /* Back up the source pointer! */
tomwalters@268 494 result = targetExhausted; break;
tomwalters@268 495 }
tomwalters@268 496 if (ch <= UNI_MAX_LEGAL_UTF32) {
tomwalters@268 497 /*
tomwalters@268 498 * UTF-16 surrogate values are illegal in UTF-32, and anything
tomwalters@268 499 * over Plane 17 (> 0x10FFFF) is illegal.
tomwalters@268 500 */
tomwalters@268 501 if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
tomwalters@268 502 if (flags == strictConversion) {
tomwalters@268 503 source -= (extraBytesToRead+1); /* return to the illegal value itself */
tomwalters@268 504 result = sourceIllegal;
tomwalters@268 505 break;
tomwalters@268 506 } else {
tomwalters@268 507 *target++ = UNI_REPLACEMENT_CHAR;
tomwalters@268 508 }
tomwalters@268 509 } else {
tomwalters@268 510 *target++ = ch;
tomwalters@268 511 }
tomwalters@268 512 } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
tomwalters@268 513 result = sourceIllegal;
tomwalters@268 514 *target++ = UNI_REPLACEMENT_CHAR;
tomwalters@268 515 }
tomwalters@268 516 }
tomwalters@268 517 *sourceStart = source;
tomwalters@268 518 *targetStart = target;
tomwalters@268 519 return result;
tomwalters@268 520 }
tomwalters@268 521
tomwalters@268 522 /* ---------------------------------------------------------------------
tomwalters@268 523
tomwalters@268 524 Note A.
tomwalters@268 525 The fall-through switches in UTF-8 reading code save a
tomwalters@268 526 temp variable, some decrements & conditionals. The switches
tomwalters@268 527 are equivalent to the following loop:
tomwalters@268 528 {
tomwalters@268 529 int tmpBytesToRead = extraBytesToRead+1;
tomwalters@268 530 do {
tomwalters@268 531 ch += *source++;
tomwalters@268 532 --tmpBytesToRead;
tomwalters@268 533 if (tmpBytesToRead) ch <<= 6;
tomwalters@268 534 } while (tmpBytesToRead > 0);
tomwalters@268 535 }
tomwalters@268 536 In UTF-8 writing code, the switches on "bytesToWrite" are
tomwalters@268 537 similarly unrolled loops.
tomwalters@268 538
tomwalters@268 539 --------------------------------------------------------------------- */