1 | /* |
2 | * Copyright (C) 2004-2019 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 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 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 <wtf/text/CString.h> |
30 | #include <wtf/text/WTFString.h> |
31 | |
32 | namespace WebCore { |
33 | |
34 | inline TextCodecUTF16::TextCodecUTF16(bool littleEndian) |
35 | : m_littleEndian(littleEndian) |
36 | { |
37 | } |
38 | |
39 | void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) |
40 | { |
41 | registrar("UTF-16LE" , "UTF-16LE" ); |
42 | registrar("UTF-16BE" , "UTF-16BE" ); |
43 | |
44 | registrar("ISO-10646-UCS-2" , "UTF-16LE" ); |
45 | registrar("UCS-2" , "UTF-16LE" ); |
46 | registrar("UTF-16" , "UTF-16LE" ); |
47 | registrar("Unicode" , "UTF-16LE" ); |
48 | registrar("csUnicode" , "UTF-16LE" ); |
49 | registrar("unicodeFEFF" , "UTF-16LE" ); |
50 | |
51 | registrar("unicodeFFFE" , "UTF-16BE" ); |
52 | } |
53 | |
54 | void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) |
55 | { |
56 | registrar("UTF-16LE" , [] { |
57 | return std::make_unique<TextCodecUTF16>(true); |
58 | }); |
59 | registrar("UTF-16BE" , [] { |
60 | return std::make_unique<TextCodecUTF16>(false); |
61 | }); |
62 | } |
63 | |
64 | String TextCodecUTF16::decode(const char* bytes, size_t length, bool, bool, bool&) |
65 | { |
66 | if (!length) |
67 | return String(); |
68 | |
69 | // FIXME: This should generate an error if there is an unpaired surrogate. |
70 | |
71 | const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); |
72 | size_t numBytes = length + m_haveBufferedByte; |
73 | size_t numCodeUnits = numBytes / 2; |
74 | RELEASE_ASSERT(numCodeUnits <= std::numeric_limits<unsigned>::max()); |
75 | |
76 | UChar* q; |
77 | auto result = String::createUninitialized(numCodeUnits, q); |
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 | numCodeUnits -= 1; |
89 | } |
90 | |
91 | if (m_littleEndian) { |
92 | for (size_t i = 0; i < numCodeUnits; ++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 < numCodeUnits; ++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 | return result; |
112 | } |
113 | |
114 | Vector<uint8_t> TextCodecUTF16::encode(StringView string, UnencodableHandling) |
115 | { |
116 | Vector<uint8_t> result(WTF::checkedProduct<size_t>(string.length(), 2).unsafeGet()); |
117 | auto* bytes = result.data(); |
118 | |
119 | if (m_littleEndian) { |
120 | for (auto character : string.codeUnits()) { |
121 | *bytes++ = character; |
122 | *bytes++ = character >> 8; |
123 | } |
124 | } else { |
125 | for (auto character : string.codeUnits()) { |
126 | *bytes++ = character >> 8; |
127 | *bytes++ = character; |
128 | } |
129 | } |
130 | |
131 | return result; |
132 | } |
133 | |
134 | } // namespace WebCore |
135 | |