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