-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathJavaModifiedUtf8.cpp
More file actions
375 lines (338 loc) · 10.8 KB
/
JavaModifiedUtf8.cpp
File metadata and controls
375 lines (338 loc) · 10.8 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "JavaModifiedUtf8.hpp"
#include <codecvt>
#include <locale>
#include <set>
#include "geode/ExceptionTypes.hpp"
#include "string.hpp"
namespace apache {
namespace geode {
namespace client {
namespace internal {
size_t JavaModifiedUtf8::encodedLength(const std::string& utf8) {
if (utf8.empty()) {
return 0;
}
return encodedLength(to_utf16(utf8));
}
size_t JavaModifiedUtf8::encodedLength(const std::u16string& utf16) {
return encodedLength(utf16.data(), utf16.length());
}
size_t JavaModifiedUtf8::encodedLength(const char16_t* data, size_t length) {
size_t encodedLen = 0;
while (length-- > 0) {
const char16_t c = *(data++);
if (c == 0) {
// NUL
encodedLen += 2;
} else if (c < 0x80) {
// ASCII
encodedLen++;
} else if (c < 0x800) {
encodedLen += 2;
} else {
encodedLen += 3;
}
}
return encodedLen;
}
std::set<int> utf16_surrogate_codes = {{0xD800}, {0xDB7F}, {0xDB80}, {0xDBFF},
{0xDC00}, {0xDF80}, {0xDFFF}};
bool JavaModifiedUtf8::IsValidCodePoint(uint16_t code_point) {
return (code_point > 0x7FF) && (utf16_surrogate_codes.find(code_point) ==
utf16_surrogate_codes.end());
}
enum class UtfScanState : int32_t {
Initial = 0,
Continuing = 1,
};
ju8string JavaModifiedUtf8::decode2byte(const std::string& utf8char) {
ju8string jmutf8char;
auto byte1 = utf8char[0];
auto byte2 = utf8char[1];
if (((byte1 & 0xE0) == 0xC0) && ((byte2 & 0x80) == 0x80)) {
int32_t code_point = ((byte1 & 0x1f) << 6) + (byte2 & 0x3f);
if (code_point > 0x7F) {
jmutf8char += byte1;
jmutf8char += byte2;
} else {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method (overly long "
"ASCII character)");
}
} else {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method");
}
return jmutf8char;
}
ju8string JavaModifiedUtf8::decode3byte(const std::string& utf8char) {
ju8string jmutf8char;
auto byte1 = utf8char[0];
auto byte2 = utf8char[1];
auto byte3 = utf8char[2];
if (((byte1 & 0xF0) == 0xE0) && ((byte2 & 0x80) == 0x80) &&
((byte3 & 0x80) == 0x80)) {
uint16_t code_point =
((byte1 & 0x0F) << 12) + ((byte2 & 0x3F) << 6) + (byte3 & 0x3F);
if (IsValidCodePoint(code_point)) {
jmutf8char += byte1;
jmutf8char += byte2;
jmutf8char += byte3;
} else {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method (overly long "
"3-byte encoding)");
}
} else {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method");
}
return jmutf8char;
}
ju8string JavaModifiedUtf8::decode4byte(const std::string& utf8char) {
ju8string jmutf8char;
auto byte1 = utf8char[0];
auto byte2 = utf8char[1];
auto byte3 = utf8char[2];
auto byte4 = utf8char[3];
if (((byte1 & 0xF8) == 0xF0) && ((byte2 & 0x80) == 0x80) &&
((byte3 & 0x80) == 0x80) && ((byte4 & 0x80) == 0x80)) {
uint32_t code_point = (byte1 & 0x07) << 18;
code_point += (byte2 & 0x3F) << 12;
code_point += (byte3 & 0x3F) << 6;
code_point += byte4 & 0x3F;
if (code_point > 0xFFFF) {
jmutf8char += static_cast<uint8_t>(0xED);
jmutf8char +=
static_cast<uint8_t>((0xA0 + (((code_point >> 16) - 1) & 0x0F)));
jmutf8char += static_cast<uint8_t>((0x80 + ((code_point >> 10) & 0x3F)));
jmutf8char += static_cast<uint8_t>(0xED);
jmutf8char += static_cast<uint8_t>((0xB0 + ((code_point >> 6) & 0x0F)));
jmutf8char += byte4;
} else {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method (overly long "
"4-byte encoding)");
}
} else {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method");
}
return jmutf8char;
}
ju8string JavaModifiedUtf8::decode(const std::string& utf8char) {
ju8string jmutf8char;
switch (utf8char.size()) {
case 2:
jmutf8char = decode2byte(utf8char);
break;
case 3:
jmutf8char = decode3byte(utf8char);
break;
case 4:
jmutf8char = decode4byte(utf8char);
break;
default:
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method");
}
return jmutf8char;
}
ju8string JavaModifiedUtf8::fromString(const std::string& utf8) {
ju8string jmutf8;
size_t cursor = 0;
auto state = UtfScanState::Initial;
std::string current;
while (cursor < utf8.size()) {
auto byte = utf8[cursor++];
switch (state) {
case UtfScanState::Initial:
if ((byte & 0x80) == 0) {
if (byte) {
jmutf8 += byte;
} else {
jmutf8 += static_cast<uint8_t>(0xC0);
jmutf8 += static_cast<uint8_t>(0x80);
}
} else if ((byte & 0xc0) == 0x80) {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method");
} else {
current += byte;
state = UtfScanState::Continuing;
}
break;
case UtfScanState::Continuing: {
if ((byte & 0xC0) == 0x80) {
current += byte;
} else {
cursor--;
state = UtfScanState::Initial;
jmutf8 += JavaModifiedUtf8::decode(current);
current.clear();
}
} break;
}
}
if (current.size() && state == UtfScanState::Continuing) {
state = UtfScanState::Initial;
jmutf8 += JavaModifiedUtf8::decode(current);
current.clear();
}
if (state != UtfScanState::Initial) {
throw IllegalArgumentException(
"Invalid utf-8 string passed to conversion method");
}
return jmutf8;
}
std::string JavaModifiedUtf8::toString(const ju8string& jmutf8) {
// std::string utf8;
// size_t cursor = 0;
// while (cursor < jmutf8.size()) {
// auto byte1 = jmutf8[cursor++];
// if ((byte1 & 0x80) == 0) {
// utf8.push_back(byte1);
// } else if ((byte1 & 0xE0) == 0xC0) {
// auto byte2 = jmutf8[cursor++];
// if () }
//}
return "";
}
void JavaModifiedUtf8::encode(const char16_t c, ju8string& jmutf8) {
if (c == 0) {
// NUL
jmutf8 += static_cast<uint8_t>(0xc0);
jmutf8 += static_cast<uint8_t>(0x80);
} else if (c < 0x80) {
// ASCII character
jmutf8 += static_cast<uint8_t>(c);
} else if (c < 0x800) {
jmutf8 += static_cast<uint8_t>(0xC0 | c >> 6);
jmutf8 += static_cast<uint8_t>(0x80 | (c & 0x3F));
} else {
jmutf8 += static_cast<uint8_t>(0xE0 | c >> 12);
jmutf8 += static_cast<uint8_t>(0x80 | ((c >> 6) & 0x3F));
jmutf8 += static_cast<uint8_t>(0x80 | (c & 0x3F));
}
}
std::u32string JavaModifiedUtf8::decodeU32(const char* buf, uint16_t len) {
std::u32string result;
uint16_t i = 0;
while (i < len) {
auto byte1 = buf[i++];
if (!(byte1 & 0x80)) {
result += static_cast<int32_t>(byte1) & 0x000000FF;
} else if ((i < len) && ((byte1 & 0xE0) == 0xC0)) {
auto byte2 = buf[i++];
if (((byte1 & 0xFF) == 0xC0) && ((byte2 & 0xFF) == 0x80)) {
result.push_back(static_cast<int32_t>(0));
} else {
int32_t code_point = static_cast<int32_t>(byte1 & 0x1F) << 6;
code_point += static_cast<int32_t>(byte2 & 0x3F);
result.push_back(code_point);
}
} else if ((i < len - 4) && ((byte1 & 0xED) == 0xED)) {
auto byte2 = buf[i++];
auto byte3 = buf[i++];
auto byte4 = buf[i++];
auto byte5 = buf[i++];
auto byte6 = buf[i++];
if ((byte4 & 0xED) == 0xED) {
int32_t code_point =
0x10000 + (static_cast<int32_t>(byte2 & 0xF) << 16);
code_point += static_cast<int32_t>(byte3 & 0x3F) << 10;
code_point += static_cast<int32_t>(byte5 & 0xF) << 6;
code_point += static_cast<int32_t>(byte6 & 0x3F);
result.push_back(code_point);
} else {
throw IllegalArgumentException("Bad encoding in jmutf-8 string");
}
} else if ((i < len - 1) && ((byte1 & 0xE0) == 0xE0)) {
auto byte2 = buf[i++];
auto byte3 = buf[i++];
int32_t code_point = static_cast<int32_t>(byte1 & 0xF) << 12;
code_point += static_cast<int32_t>(byte2 & 0x3F) << 6;
code_point += static_cast<int32_t>(byte3 & 0x3F);
result.push_back(code_point);
} else {
throw IllegalArgumentException("Bad encoding in jmutf-8 string");
}
}
return result;
}
std::u16string JavaModifiedUtf8::decode(const char* buf, uint16_t len) {
std::u16string value;
const auto end = buf + len;
while (buf < end) {
value += decodeJavaModifiedUtf8Char(&buf);
}
return value;
}
char16_t JavaModifiedUtf8::decodeJavaModifiedUtf8Char(const char** pbuf) {
char16_t c;
// get next byte unsigned
int32_t b = **pbuf & 0xff;
(*pbuf)++;
int32_t k = b >> 5;
// classify based on the high order 3 bits
switch (k) {
case 6: {
// two byte encoding
// 110yyyyy 10xxxxxx
// use low order 6 bits
int32_t y = b & 0x1f;
// use low order 6 bits of the next byte
// It should have high order bits 10, which we don't check.
int32_t x = **pbuf & 0x3f;
(*pbuf)++;
// 00000yyy yyxxxxxx
c = (y << 6 | x);
break;
}
case 7: {
// three byte encoding
// 1110zzzz 10yyyyyy 10xxxxxx
// use low order 4 bits
int32_t z = b & 0x0f;
// use low order 6 bits of the next byte
// It should have high order bits 10, which we don't check.
int32_t y = **pbuf & 0x3f;
(*pbuf)++;
// use low order 6 bits of the next byte
// It should have high order bits 10, which we don't check.
int32_t x = **pbuf & 0x3f;
(*pbuf)++;
// zzzzyyyy yyxxxxxx
c = static_cast<char16_t>(z << 12 | y << 6 | x);
break;
}
default:
// one byte encoding
// 0xxxxxxx
// use just low order 7 bits
// 00000000 0xxxxxxx
c = static_cast<char16_t>(b & 0x7f);
break;
}
return c;
}
} // namespace internal
} // namespace client
} // namespace geode
} // namespace apache