2 * Copyright (C) 2004, 2006, 2008 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 "TextCodecUTF16.h"
30 #include "PlatformString.h"
31 #include "StringBuffer.h"
37 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar)
39 registrar("UTF-16LE", "UTF-16LE");
40 registrar("UTF-16BE", "UTF-16BE");
42 registrar("ISO-10646-UCS-2", "UTF-16LE");
43 registrar("UCS-2", "UTF-16LE");
44 registrar("UTF-16", "UTF-16LE");
45 registrar("Unicode", "UTF-16LE");
46 registrar("csUnicode", "UTF-16LE");
47 registrar("unicodeFEFF", "UTF-16LE");
49 registrar("unicodeFFFE", "UTF-16BE");
52 static auto_ptr<TextCodec> newStreamingTextDecoderUTF16LE(const TextEncoding&, const void*)
54 return auto_ptr<TextCodec>(new TextCodecUTF16(true));
57 static auto_ptr<TextCodec> newStreamingTextDecoderUTF16BE(const TextEncoding&, const void*)
59 return auto_ptr<TextCodec>(new TextCodecUTF16(false));
62 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar)
64 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0);
65 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0);
68 String TextCodecUTF16::decode(const char* bytes, size_t length, bool, bool stopOnError, bool& sawError)
73 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes);
74 size_t numBytes = length + m_haveBufferedByte;
75 size_t numChars = numBytes / 2;
77 StringBuffer buffer(numChars);
78 UChar* q = buffer.characters();
80 if (m_haveBufferedByte) {
83 c = m_bufferedByte | (p[0] << 8);
85 c = (m_bufferedByte << 8) | p[0];
87 m_haveBufferedByte = false;
93 for (size_t i = 0; i < numChars; ++i) {
94 UChar c = p[0] | (p[1] << 8);
99 for (size_t i = 0; i < numChars; ++i) {
100 UChar c = (p[0] << 8) | p[1];
106 ASSERT(!m_haveBufferedByte);
107 m_haveBufferedByte = true;
108 m_bufferedByte = p[0];
111 buffer.shrink(q - buffer.characters());
113 return String::adopt(buffer);
116 CString TextCodecUTF16::encode(const UChar* characters, size_t length, UnencodableHandling)
119 CString string = CString::newUninitialized(length * 2, bytes);
121 // FIXME: CString is not a reasonable data structure for encoded UTF-16, which will have
122 // null characters inside it. Perhaps the result of encode should not be a CString?
124 for (size_t i = 0; i < length; ++i) {
125 UChar c = characters[i];
127 bytes[i * 2 + 1] = c >> 8;
130 for (size_t i = 0; i < length; ++i) {
131 UChar c = characters[i];
132 bytes[i * 2] = c >> 8;
133 bytes[i * 2 + 1] = c;
139 } // namespace WebCore