2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "TextCodecUTF8.h"
29 #include "TextCodecASCIIFastPath.h"
30 #include <wtf/text/CString.h>
31 #include <wtf/text/StringBuffer.h>
32 #include <wtf/unicode/CharacterNames.h>
34 using namespace WTF::Unicode;
39 const int nonCharacter = -1;
41 PassOwnPtr<TextCodec> TextCodecUTF8::create(const TextEncoding&, const void*)
43 return adoptPtr(new TextCodecUTF8);
46 void TextCodecUTF8::registerEncodingNames(EncodingNameRegistrar registrar)
48 registrar("UTF-8", "UTF-8");
50 // Additional aliases that originally were present in the encoding
51 // table in WebKit on Macintosh, and subsequently added by
52 // TextCodecICU. Perhaps we can prove some are not used on the web
54 registrar("unicode11utf8", "UTF-8");
55 registrar("unicode20utf8", "UTF-8");
56 registrar("utf8", "UTF-8");
57 registrar("x-unicode20utf8", "UTF-8");
60 void TextCodecUTF8::registerCodecs(TextCodecRegistrar registrar)
62 registrar("UTF-8", create, 0);
65 static inline int nonASCIISequenceLength(uint8_t firstByte)
67 static const uint8_t lengths[256] = {
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
77 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
78 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
79 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
83 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
85 return lengths[firstByte];
88 static inline int decodeNonASCIISequence(const uint8_t* sequence, unsigned length)
90 ASSERT(!isASCII(sequence[0]));
92 ASSERT(sequence[0] <= 0xDF);
93 if (sequence[0] < 0xC2)
95 if (sequence[1] < 0x80 || sequence[1] > 0xBF)
97 return ((sequence[0] << 6) + sequence[1]) - 0x00003080;
100 ASSERT(sequence[0] >= 0xE0 && sequence[0] <= 0xEF);
101 switch (sequence[0]) {
103 if (sequence[1] < 0xA0 || sequence[1] > 0xBF)
107 if (sequence[1] < 0x80 || sequence[1] > 0x9F)
111 if (sequence[1] < 0x80 || sequence[1] > 0xBF)
114 if (sequence[2] < 0x80 || sequence[2] > 0xBF)
116 return ((sequence[0] << 12) + (sequence[1] << 6) + sequence[2]) - 0x000E2080;
119 ASSERT(sequence[0] >= 0xF0 && sequence[0] <= 0xF4);
120 switch (sequence[0]) {
122 if (sequence[1] < 0x90 || sequence[1] > 0xBF)
126 if (sequence[1] < 0x80 || sequence[1] > 0x8F)
130 if (sequence[1] < 0x80 || sequence[1] > 0xBF)
133 if (sequence[2] < 0x80 || sequence[2] > 0xBF)
135 if (sequence[3] < 0x80 || sequence[3] > 0xBF)
137 return ((sequence[0] << 18) + (sequence[1] << 12) + (sequence[2] << 6) + sequence[3]) - 0x03C82080;
140 static inline UChar* appendCharacter(UChar* destination, int character)
142 ASSERT(character != nonCharacter);
143 ASSERT(!U_IS_SURROGATE(character));
144 if (U_IS_BMP(character))
145 *destination++ = character;
147 *destination++ = U16_LEAD(character);
148 *destination++ = U16_TRAIL(character);
153 void TextCodecUTF8::consumePartialSequenceByte()
155 --m_partialSequenceSize;
156 memmove(m_partialSequence, m_partialSequence + 1, m_partialSequenceSize);
159 void TextCodecUTF8::handleError(UChar*& destination, bool stopOnError, bool& sawError)
164 // Each error generates a replacement character and consumes one byte.
165 *destination++ = replacementCharacter;
166 consumePartialSequenceByte();
169 void TextCodecUTF8::handlePartialSequence(UChar*& destination, const uint8_t*& source, const uint8_t* end, bool flush, bool stopOnError, bool& sawError)
171 ASSERT(m_partialSequenceSize);
173 if (isASCII(m_partialSequence[0])) {
174 *destination++ = m_partialSequence[0];
175 consumePartialSequenceByte();
178 int count = nonASCIISequenceLength(m_partialSequence[0]);
180 handleError(destination, stopOnError, sawError);
185 if (count > m_partialSequenceSize) {
186 if (count - m_partialSequenceSize > end - source) {
188 // The new data is not enough to complete the sequence, so
189 // add it to the existing partial sequence.
190 memcpy(m_partialSequence + m_partialSequenceSize, source, end - source);
191 m_partialSequenceSize += end - source;
194 // An incomplete partial sequence at the end is an error.
195 handleError(destination, stopOnError, sawError);
200 memcpy(m_partialSequence + m_partialSequenceSize, source, count - m_partialSequenceSize);
201 source += count - m_partialSequenceSize;
202 m_partialSequenceSize = count;
204 int character = decodeNonASCIISequence(m_partialSequence, count);
205 if (character == nonCharacter) {
206 handleError(destination, stopOnError, sawError);
211 m_partialSequenceSize -= count;
212 destination = appendCharacter(destination, character);
213 } while (m_partialSequenceSize);
216 String TextCodecUTF8::decode(const char* bytes, size_t length, bool flush, bool stopOnError, bool& sawError)
218 // Each input byte might turn into a character.
219 // That includes all bytes in the partial-sequence buffer because
220 // each byte in an invalid sequence will turn into a replacement character.
221 StringBuffer buffer(m_partialSequenceSize + length);
223 const uint8_t* source = reinterpret_cast<const uint8_t*>(bytes);
224 const uint8_t* end = source + length;
225 const uint8_t* alignedEnd = alignToMachineWord(end);
226 UChar* destination = buffer.characters();
229 if (m_partialSequenceSize) {
230 // Explicitly copy destination and source pointers to avoid taking pointers to the
231 // local variables, which may harm code generation by disabling some optimizations
232 // in some compilers.
233 UChar* destinationForHandlePartialSequence = destination;
234 const uint8_t* sourceForHandlePartialSequence = source;
235 handlePartialSequence(destinationForHandlePartialSequence, sourceForHandlePartialSequence, end, flush, stopOnError, sawError);
236 destination = destinationForHandlePartialSequence;
237 source = sourceForHandlePartialSequence;
238 if (m_partialSequenceSize)
242 while (source < end) {
243 if (isASCII(*source)) {
244 // Fast path for ASCII. Most UTF-8 text will be ASCII.
245 if (isAlignedToMachineWord(source)) {
246 while (source < alignedEnd) {
247 MachineWord chunk = *reinterpret_cast_ptr<const MachineWord*>(source);
248 if (!isAllASCII(chunk))
250 copyASCIIMachineWord(destination, source);
251 source += sizeof(MachineWord);
252 destination += sizeof(MachineWord);
256 if (!isASCII(*source))
259 *destination++ = *source++;
262 int count = nonASCIISequenceLength(*source);
265 character = nonCharacter;
267 if (count > end - source) {
268 ASSERT(end - source < static_cast<ptrdiff_t>(sizeof(m_partialSequence)));
269 ASSERT(!m_partialSequenceSize);
270 m_partialSequenceSize = end - source;
271 memcpy(m_partialSequence, source, m_partialSequenceSize);
275 character = decodeNonASCIISequence(source, count);
277 if (character == nonCharacter) {
281 // Each error generates a replacement character and consumes one byte.
282 *destination++ = replacementCharacter;
287 destination = appendCharacter(destination, character);
289 } while (flush && m_partialSequenceSize);
291 buffer.shrink(destination - buffer.characters());
293 return String::adopt(buffer);
296 CString TextCodecUTF8::encode(const UChar* characters, size_t length, UnencodableHandling)
298 // The maximum number of UTF-8 bytes needed per UTF-16 code unit is 3.
299 // BMP characters take only one UTF-16 code unit and can take up to 3 bytes (3x).
300 // Non-BMP characters take two UTF-16 code units and can take up to 4 bytes (2x).
301 if (length > numeric_limits<size_t>::max() / 3)
303 Vector<uint8_t> bytes(length * 3);
306 size_t bytesWritten = 0;
309 U16_NEXT(characters, i, length, character);
310 U8_APPEND_UNSAFE(bytes.data(), bytesWritten, character);
313 return CString(reinterpret_cast<char*>(bytes.data()), bytesWritten);
316 } // namespace WebCore