1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
//
// UTF16Encoding.cpp
//
// Library: Foundation
// Package: Text
// Module: UTF16Encoding
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/UTF16Encoding.h"
#include "Poco/ByteOrder.h"
#include "Poco/String.h"
namespace Poco {
const char* UTF16Encoding::_names[] =
{
"UTF-16",
"UTF16",
NULL
};
const TextEncoding::CharacterMap UTF16Encoding::_charMap =
{
/* 00 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 10 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 20 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 30 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 40 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 50 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 60 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* 70 */ -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,
/* 90 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* a0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* b0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* c0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* d0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* e0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
/* f0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
};
UTF16Encoding::UTF16Encoding(ByteOrderType byteOrder)
{
setByteOrder(byteOrder);
}
UTF16Encoding::UTF16Encoding(int byteOrderMark)
{
setByteOrder(byteOrderMark);
}
UTF16Encoding::~UTF16Encoding()
{
}
UTF16Encoding::ByteOrderType UTF16Encoding::getByteOrder() const
{
#if defined(POCO_ARCH_BIG_ENDIAN)
return _flipBytes ? LITTLE_ENDIAN_BYTE_ORDER : BIG_ENDIAN_BYTE_ORDER;
#else
return _flipBytes ? BIG_ENDIAN_BYTE_ORDER : LITTLE_ENDIAN_BYTE_ORDER;
#endif
}
void UTF16Encoding::setByteOrder(ByteOrderType byteOrder)
{
#if defined(POCO_ARCH_BIG_ENDIAN)
_flipBytes = byteOrder == LITTLE_ENDIAN_BYTE_ORDER;
#else
_flipBytes = byteOrder == BIG_ENDIAN_BYTE_ORDER;;
#endif
}
void UTF16Encoding::setByteOrder(int byteOrderMark)
{
_flipBytes = byteOrderMark != 0xFEFF;
}
const char* UTF16Encoding::canonicalName() const
{
return _names[0];
}
bool UTF16Encoding::isA(const std::string& encodingName) const
{
for (const char** name = _names; *name; ++name)
{
if (Poco::icompare(encodingName, *name) == 0)
return true;
}
return false;
}
const TextEncoding::CharacterMap& UTF16Encoding::characterMap() const
{
return _charMap;
}
int UTF16Encoding::convert(const unsigned char* bytes) const
{
UInt16 uc;
unsigned char* p = (unsigned char*) &uc;
*p++ = *bytes++;
*p++ = *bytes++;
if (_flipBytes)
{
ByteOrder::flipBytes(uc);
}
if (uc >= 0xd800 && uc < 0xdc00)
{
UInt16 uc2;
p = (unsigned char*) &uc2;
*p++ = *bytes++;
*p++ = *bytes++;
if (_flipBytes)
{
ByteOrder::flipBytes(uc2);
}
if (uc2 >= 0xdc00 && uc2 < 0xe000)
{
return ((uc & 0x3ff) << 10) + (uc2 & 0x3ff) + 0x10000;
}
else
{
return -1;
}
}
else
{
return uc;
}
}
int UTF16Encoding::convert(int ch, unsigned char* bytes, int length) const
{
if (ch <= 0xFFFF)
{
if (bytes && length >= 2)
{
UInt16 ch1 = _flipBytes ? ByteOrder::flipBytes((UInt16) ch) : (UInt16) ch;
unsigned char* p = (unsigned char*) &ch1;
*bytes++ = *p++;
*bytes++ = *p++;
}
return 2;
}
else
{
if (bytes && length >= 4)
{
int ch1 = ch - 0x10000;
UInt16 w1 = 0xD800 + ((ch1 >> 10) & 0x3FF);
UInt16 w2 = 0xDC00 + (ch1 & 0x3FF);
if (_flipBytes)
{
w1 = ByteOrder::flipBytes(w1);
w2 = ByteOrder::flipBytes(w2);
}
unsigned char* p = (unsigned char*) &w1;
*bytes++ = *p++;
*bytes++ = *p++;
p = (unsigned char*) &w2;
*bytes++ = *p++;
*bytes++ = *p++;
}
return 4;
}
}
int UTF16Encoding::queryConvert(const unsigned char* bytes, int length) const
{
int ret = -2;
if (length >= 2)
{
UInt16 uc;
unsigned char* p = (unsigned char*) &uc;
*p++ = *bytes++;
*p++ = *bytes++;
if (_flipBytes)
ByteOrder::flipBytes(uc);
if (uc >= 0xd800 && uc < 0xdc00)
{
if (length >= 4)
{
UInt16 uc2;
p = (unsigned char*) &uc2;
*p++ = *bytes++;
*p++ = *bytes++;
if (_flipBytes)
ByteOrder::flipBytes(uc2);
if (uc2 >= 0xdc00 && uc < 0xe000)
{
ret = ((uc & 0x3ff) << 10) + (uc2 & 0x3ff) + 0x10000;
}
else
{
ret = -1; // Malformed sequence
}
}
else
{
ret = -4; // surrogate pair, four bytes needed
}
}
else
{
ret = uc;
}
}
return ret;
}
int UTF16Encoding::sequenceLength(const unsigned char* bytes, int length) const
{
int ret = -2;
if (_flipBytes)
{
if (length >= 1)
{
unsigned char c = *bytes;
if (c >= 0xd8 && c < 0xdc)
ret = 4;
else
ret = 2;
}
}
else
{
if (length >= 2)
{
UInt16 uc;
unsigned char* p = (unsigned char*) &uc;
*p++ = *bytes++;
*p++ = *bytes++;
if (uc >= 0xd800 && uc < 0xdc00)
ret = 4;
else
ret = 2;
}
}
return ret;
}
} // namespace Poco
|