• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #include "config.h"
27 #include "TextCodecUTF16.h"
28 
29 #include "CString.h"
30 #include "PlatformString.h"
31 #include "StringBuffer.h"
32 #include <wtf/PassOwnPtr.h>
33 
34 namespace WebCore {
35 
registerEncodingNames(EncodingNameRegistrar registrar)36 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar)
37 {
38     registrar("UTF-16LE", "UTF-16LE");
39     registrar("UTF-16BE", "UTF-16BE");
40 
41     registrar("ISO-10646-UCS-2", "UTF-16LE");
42     registrar("UCS-2", "UTF-16LE");
43     registrar("UTF-16", "UTF-16LE");
44     registrar("Unicode", "UTF-16LE");
45     registrar("csUnicode", "UTF-16LE");
46     registrar("unicodeFEFF", "UTF-16LE");
47 
48     registrar("unicodeFFFE", "UTF-16BE");
49 }
50 
newStreamingTextDecoderUTF16LE(const TextEncoding &,const void *)51 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16LE(const TextEncoding&, const void*)
52 {
53     return new TextCodecUTF16(true);
54 }
55 
newStreamingTextDecoderUTF16BE(const TextEncoding &,const void *)56 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16BE(const TextEncoding&, const void*)
57 {
58     return new TextCodecUTF16(false);
59 }
60 
registerCodecs(TextCodecRegistrar registrar)61 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar)
62 {
63     registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0);
64     registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0);
65 }
66 
decode(const char * bytes,size_t length,bool,bool,bool &)67 String TextCodecUTF16::decode(const char* bytes, size_t length, bool, bool, bool&)
68 {
69     if (!length)
70         return String();
71 
72     const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes);
73     size_t numBytes = length + m_haveBufferedByte;
74     size_t numChars = numBytes / 2;
75 
76     StringBuffer buffer(numChars);
77     UChar* q = buffer.characters();
78 
79     if (m_haveBufferedByte) {
80         UChar c;
81         if (m_littleEndian)
82             c = m_bufferedByte | (p[0] << 8);
83         else
84             c = (m_bufferedByte << 8) | p[0];
85         *q++ = c;
86         m_haveBufferedByte = false;
87         p += 1;
88         numChars -= 1;
89     }
90 
91     if (m_littleEndian) {
92         for (size_t i = 0; i < numChars; ++i) {
93             UChar c = p[0] | (p[1] << 8);
94             p += 2;
95             *q++ = c;
96         }
97     } else {
98         for (size_t i = 0; i < numChars; ++i) {
99             UChar c = (p[0] << 8) | p[1];
100             p += 2;
101             *q++ = c;
102         }
103     }
104 
105     if (numBytes & 1) {
106         ASSERT(!m_haveBufferedByte);
107         m_haveBufferedByte = true;
108         m_bufferedByte = p[0];
109     }
110 
111     buffer.shrink(q - buffer.characters());
112 
113     return String::adopt(buffer);
114 }
115 
encode(const UChar * characters,size_t length,UnencodableHandling)116 CString TextCodecUTF16::encode(const UChar* characters, size_t length, UnencodableHandling)
117 {
118     char* bytes;
119     CString string = CString::newUninitialized(length * 2, bytes);
120 
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?
123     if (m_littleEndian)
124         for (size_t i = 0; i < length; ++i) {
125             UChar c = characters[i];
126             bytes[i * 2] = c;
127             bytes[i * 2 + 1] = c >> 8;
128         }
129     else
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;
134         }
135 
136     return string;
137 }
138 
139 } // namespace WebCore
140