2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "ASCIICType.h"
31 #include <wtf/StringHasher.h>
32 #include <wtf/unicode/CharacterNames.h>
37 inline int inlineUTF8SequenceLengthNonASCII(char b0)
39 if ((b0 & 0xC0) != 0xC0)
41 if ((b0 & 0xE0) == 0xC0)
43 if ((b0 & 0xF0) == 0xE0)
45 if ((b0 & 0xF8) == 0xF0)
50 inline int inlineUTF8SequenceLength(char b0)
52 return isASCII(b0) ? 1 : inlineUTF8SequenceLengthNonASCII(b0);
55 int UTF8SequenceLength(char b0)
57 return isASCII(b0) ? 1 : inlineUTF8SequenceLengthNonASCII(b0);
60 int decodeUTF8Sequence(const char* sequence)
62 // Handle 0-byte sequences (never valid).
63 const unsigned char b0 = sequence[0];
64 const int length = inlineUTF8SequenceLength(b0);
68 // Handle 1-byte sequences (plain ASCII).
69 const unsigned char b1 = sequence[1];
76 // Handle 2-byte sequences.
77 if ((b1 & 0xC0) != 0x80)
79 const unsigned char b2 = sequence[2];
83 const int c = ((b0 & 0x1F) << 6) | (b1 & 0x3F);
89 // Handle 3-byte sequences.
90 if ((b2 & 0xC0) != 0x80)
92 const unsigned char b3 = sequence[3];
96 const int c = ((b0 & 0xF) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F);
99 // UTF-16 surrogates should never appear in UTF-8 data.
100 if (c >= 0xD800 && c <= 0xDFFF)
105 // Handle 4-byte sequences.
106 if ((b3 & 0xC0) != 0x80)
108 const unsigned char b4 = sequence[4];
112 const int c = ((b0 & 0x7) << 18) | ((b1 & 0x3F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F);
113 if (c < 0x10000 || c > 0x10FFFF)
121 // Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
122 // into the first byte, depending on how many bytes follow. There are
123 // as many entries in this table as there are UTF-8 sequence types.
124 // (I.e., one byte sequence, two byte... etc.). Remember that sequencs
125 // for *legal* UTF-8 will be 4 or fewer bytes total.
126 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
128 ConversionResult convertUTF16ToUTF8(
129 const UChar** sourceStart, const UChar* sourceEnd,
130 char** targetStart, char* targetEnd, bool strict)
132 ConversionResult result = conversionOK;
133 const UChar* source = *sourceStart;
134 char* target = *targetStart;
135 while (source < sourceEnd) {
137 unsigned short bytesToWrite = 0;
138 const UChar32 byteMask = 0xBF;
139 const UChar32 byteMark = 0x80;
140 const UChar* oldSource = source; // In case we have to back up because of target overflow.
141 ch = static_cast<unsigned short>(*source++);
142 // If we have a surrogate pair, convert to UChar32 first.
143 if (ch >= 0xD800 && ch <= 0xDBFF) {
144 // If the 16 bits following the high surrogate are in the source buffer...
145 if (source < sourceEnd) {
146 UChar32 ch2 = static_cast<unsigned short>(*source);
147 // If it's a low surrogate, convert to UChar32.
148 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
149 ch = ((ch - 0xD800) << 10) + (ch2 - 0xDC00) + 0x0010000;
151 } else if (strict) { // it's an unpaired high surrogate
152 --source; // return to the illegal value itself
153 result = sourceIllegal;
156 } else { // We don't have the 16 bits following the high surrogate.
157 --source; // return to the high surrogate
158 result = sourceExhausted;
162 // UTF-16 surrogate values are illegal in UTF-32
163 if (ch >= 0xDC00 && ch <= 0xDFFF) {
164 --source; // return to the illegal value itself
165 result = sourceIllegal;
169 // Figure out how many bytes the result will require
170 if (ch < (UChar32)0x80) {
172 } else if (ch < (UChar32)0x800) {
174 } else if (ch < (UChar32)0x10000) {
176 } else if (ch < (UChar32)0x110000) {
180 ch = replacementCharacter;
183 target += bytesToWrite;
184 if (target > targetEnd) {
185 source = oldSource; // Back up source pointer!
186 target -= bytesToWrite;
187 result = targetExhausted;
190 switch (bytesToWrite) { // note: everything falls through.
191 case 4: *--target = (char)((ch | byteMark) & byteMask); ch >>= 6;
192 case 3: *--target = (char)((ch | byteMark) & byteMask); ch >>= 6;
193 case 2: *--target = (char)((ch | byteMark) & byteMask); ch >>= 6;
194 case 1: *--target = (char)(ch | firstByteMark[bytesToWrite]);
196 target += bytesToWrite;
198 *sourceStart = source;
199 *targetStart = target;
203 // This must be called with the length pre-determined by the first byte.
204 // If presented with a length > 4, this returns false. The Unicode
205 // definition of UTF-8 goes up to 4-byte sequences.
206 static bool isLegalUTF8(const unsigned char* source, int length)
209 const unsigned char* srcptr = source + length;
211 default: return false;
212 // Everything else falls through when "true"...
213 case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
214 case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
215 case 2: if ((a = (*--srcptr)) > 0xBF) return false;
218 // no fall-through in this inner switch
219 case 0xE0: if (a < 0xA0) return false; break;
220 case 0xED: if (a > 0x9F) return false; break;
221 case 0xF0: if (a < 0x90) return false; break;
222 case 0xF4: if (a > 0x8F) return false; break;
223 default: if (a < 0x80) return false;
226 case 1: if (*source >= 0x80 && *source < 0xC2) return false;
233 // Magic values subtracted from a buffer value during UTF8 conversion.
234 // This table contains as many values as there might be trailing bytes
235 // in a UTF-8 sequence.
236 static const UChar32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
237 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
239 static inline UChar32 readUTF8Sequence(const char*& sequence, unsigned length)
241 UChar32 character = 0;
243 // The cases all fall through.
245 case 6: character += static_cast<unsigned char>(*sequence++); character <<= 6;
246 case 5: character += static_cast<unsigned char>(*sequence++); character <<= 6;
247 case 4: character += static_cast<unsigned char>(*sequence++); character <<= 6;
248 case 3: character += static_cast<unsigned char>(*sequence++); character <<= 6;
249 case 2: character += static_cast<unsigned char>(*sequence++); character <<= 6;
250 case 1: character += static_cast<unsigned char>(*sequence++);
253 return character - offsetsFromUTF8[length - 1];
256 ConversionResult convertUTF8ToUTF16(
257 const char** sourceStart, const char* sourceEnd,
258 UChar** targetStart, UChar* targetEnd, bool strict)
260 ConversionResult result = conversionOK;
261 const char* source = *sourceStart;
262 UChar* target = *targetStart;
263 while (source < sourceEnd) {
264 int utf8SequenceLength = inlineUTF8SequenceLength(*source);
265 if (sourceEnd - source < utf8SequenceLength) {
266 result = sourceExhausted;
269 // Do this check whether lenient or strict
270 if (!isLegalUTF8(reinterpret_cast<const unsigned char*>(source), utf8SequenceLength)) {
271 result = sourceIllegal;
275 UChar32 character = readUTF8Sequence(source, utf8SequenceLength);
277 if (target >= targetEnd) {
278 source -= utf8SequenceLength; // Back up source pointer!
279 result = targetExhausted;
283 if (U_IS_BMP(character)) {
284 // UTF-16 surrogate values are illegal in UTF-32
285 if (U_IS_SURROGATE(character)) {
287 source -= utf8SequenceLength; // return to the illegal value itself
288 result = sourceIllegal;
291 *target++ = replacementCharacter;
293 *target++ = character; // normal case
294 } else if (U_IS_SUPPLEMENTARY(character)) {
295 // target is a character in range 0xFFFF - 0x10FFFF
296 if (target + 1 >= targetEnd) {
297 source -= utf8SequenceLength; // Back up source pointer!
298 result = targetExhausted;
301 *target++ = U16_LEAD(character);
302 *target++ = U16_TRAIL(character);
305 source -= utf8SequenceLength; // return to the start
306 result = sourceIllegal;
307 break; // Bail out; shouldn't continue
309 *target++ = replacementCharacter;
312 *sourceStart = source;
313 *targetStart = target;
317 unsigned calculateStringHashAndLengthFromUTF8(const char* data, const char* dataEnd, unsigned& dataLength, unsigned& utf16Length)
322 StringHasher stringHasher;
326 while (data < dataEnd || (!dataEnd && *data)) {
327 if (isASCII(*data)) {
328 stringHasher.addCharacter(*data++);
334 int utf8SequenceLength = inlineUTF8SequenceLengthNonASCII(*data);
335 dataLength += utf8SequenceLength;
338 for (int i = 1; i < utf8SequenceLength; ++i) {
342 } else if (dataEnd - data < utf8SequenceLength)
345 if (!isLegalUTF8(reinterpret_cast<const unsigned char*>(data), utf8SequenceLength))
348 UChar32 character = readUTF8Sequence(data, utf8SequenceLength);
349 ASSERT(!isASCII(character));
351 if (U_IS_BMP(character)) {
352 // UTF-16 surrogate values are illegal in UTF-32
353 if (U_IS_SURROGATE(character))
355 stringHasher.addCharacter(static_cast<UChar>(character)); // normal case
357 } else if (U_IS_SUPPLEMENTARY(character)) {
358 stringHasher.addCharacters(static_cast<UChar>(U16_LEAD(character)),
359 static_cast<UChar>(U16_TRAIL(character)));
365 return stringHasher.hash();
368 bool equalUTF16WithUTF8(const UChar* a, const UChar* aEnd, const char* b, const char* bEnd)
377 int utf8SequenceLength = inlineUTF8SequenceLengthNonASCII(*b);
379 if (bEnd - b < utf8SequenceLength)
382 if (!isLegalUTF8(reinterpret_cast<const unsigned char*>(b), utf8SequenceLength))
385 UChar32 character = readUTF8Sequence(b, utf8SequenceLength);
386 ASSERT(!isASCII(character));
388 if (U_IS_BMP(character)) {
389 // UTF-16 surrogate values are illegal in UTF-32
390 if (U_IS_SURROGATE(character))
392 if (*a++ != character)
394 } else if (U_IS_SUPPLEMENTARY(character)) {
395 if (*a++ != U16_LEAD(character))
397 if (*a++ != U16_TRAIL(character))
406 } // namespace Unicode