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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Range.h" // mozilla::Range
#include "mozilla/Span.h" // mozilla::Span
#include "mozilla/Utf8.h" // mozilla::ConvertUtf8toUtf16
#include "js/CharacterEncoding.h"
#include "jsapi-tests/tests.h"
BEGIN_TEST(testUTF8_badUTF8) {
static const char badUTF8[] = "...\xC0...";
JSString* str = JS_NewStringCopyZ(cx, badUTF8);
CHECK(str);
char16_t ch;
if (!JS_GetStringCharAt(cx, str, 3, &ch)) {
return false;
}
CHECK(ch == 0x00C0);
return true;
}
END_TEST(testUTF8_badUTF8)
BEGIN_TEST(testUTF8_bigUTF8) {
static const char bigUTF8[] = "...\xFB\xBF\xBF\xBF\xBF...";
JSString* str = JS_NewStringCopyZ(cx, bigUTF8);
CHECK(str);
char16_t ch;
if (!JS_GetStringCharAt(cx, str, 3, &ch)) {
return false;
}
CHECK(ch == 0x00FB);
return true;
}
END_TEST(testUTF8_bigUTF8)
BEGIN_TEST(testUTF8_badSurrogate) {
static const char16_t badSurrogate[] = {'A', 'B', 'C', 0xDEEE, 'D', 'E', 0};
mozilla::Range<const char16_t> tbchars(badSurrogate, js_strlen(badSurrogate));
JS::Latin1CharsZ latin1 = JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars);
CHECK(latin1);
CHECK(latin1[3] == 0x00EE);
return true;
}
END_TEST(testUTF8_badSurrogate)
BEGIN_TEST(testUTF8_LossyConversion) {
// Maximal subparts of an ill-formed subsequence should be replaced with
// single REPLACEMENT CHARACTER.
// Input ends with partial sequence.
// clang-format off
const char* inputs1[] = {
"\xC2",
"\xDF",
"\xE0",
"\xE0\xA0",
"\xF0",
"\xF0\x90",
"\xF0\x90\x80",
};
// clang-format on
char16_t outputBuf[8];
mozilla::Span output(outputBuf, 8);
for (const char* input : inputs1) {
size_t len;
JS::TwoByteCharsZ utf16 = JS::LossyUTF8CharsToNewTwoByteCharsZ(
cx, JS::UTF8Chars(input, js_strlen(input)), &len,
js::StringBufferArena);
CHECK(utf16);
CHECK(len == 1);
CHECK(utf16[0] == 0xFFFD);
// Make sure the behavior matches to encoding_rs.
len = mozilla::ConvertUtf8toUtf16(mozilla::Span(input, js_strlen(input)),
output);
CHECK(len == 1);
CHECK(outputBuf[0] == 0xFFFD);
}
// Partial sequence followed by ASCII range.
// clang-format off
const char* inputs2[] = {
"\xC2 ",
"\xDF ",
"\xE0 ",
"\xE0\xA0 ",
"\xF0 ",
"\xF0\x90 ",
"\xF0\x90\x80 ",
};
// clang-format on
for (const char* input : inputs2) {
size_t len;
JS::TwoByteCharsZ utf16 = JS::LossyUTF8CharsToNewTwoByteCharsZ(
cx, JS::UTF8Chars(input, js_strlen(input)), &len,
js::StringBufferArena);
CHECK(utf16);
CHECK(len == 2);
CHECK(utf16[0] == 0xFFFD);
CHECK(utf16[1] == 0x20);
len = mozilla::ConvertUtf8toUtf16(mozilla::Span(input, js_strlen(input)),
output);
CHECK(len == 2);
CHECK(outputBuf[0] == 0xFFFD);
CHECK(outputBuf[1] == 0x20);
}
// Partial sequence followed by other first code unit.
// clang-format off
const char* inputs3[] = {
"\xC2\xC2\x80",
"\xDF\xC2\x80",
"\xE0\xC2\x80",
"\xE0\xA0\xC2\x80",
"\xF0\xC2\x80",
"\xF0\x90\xC2\x80",
"\xF0\x90\x80\xC2\x80",
};
// clang-format on
for (const char* input : inputs3) {
size_t len;
JS::TwoByteCharsZ utf16 = JS::LossyUTF8CharsToNewTwoByteCharsZ(
cx, JS::UTF8Chars(input, js_strlen(input)), &len,
js::StringBufferArena);
CHECK(utf16);
CHECK(len == 2);
CHECK(utf16[0] == 0xFFFD);
CHECK(utf16[1] == 0x80);
len = mozilla::ConvertUtf8toUtf16(mozilla::Span(input, js_strlen(input)),
output);
CHECK(len == 2);
CHECK(outputBuf[0] == 0xFFFD);
CHECK(outputBuf[1] == 0x80);
}
// Invalid second byte.
// clang-format off
const char* inputs4[] = {
"\xE0\x9F\x80\x80",
"\xED\xA0\x80\x80",
"\xF0\x80\x80\x80",
"\xF4\x90\x80\x80",
};
// clang-format on
for (const char* input : inputs4) {
size_t len;
JS::TwoByteCharsZ utf16 = JS::LossyUTF8CharsToNewTwoByteCharsZ(
cx, JS::UTF8Chars(input, js_strlen(input)), &len,
js::StringBufferArena);
CHECK(utf16);
CHECK(len == 4);
CHECK(utf16[0] == 0xFFFD);
CHECK(utf16[1] == 0xFFFD);
CHECK(utf16[2] == 0xFFFD);
CHECK(utf16[3] == 0xFFFD);
len = mozilla::ConvertUtf8toUtf16(mozilla::Span(input, js_strlen(input)),
output);
CHECK(len == 4);
CHECK(outputBuf[0] == 0xFFFD);
CHECK(outputBuf[1] == 0xFFFD);
CHECK(outputBuf[2] == 0xFFFD);
CHECK(outputBuf[3] == 0xFFFD);
}
// Invalid second byte, with not sufficient number of units.
// clang-format off
const char* inputs5[] = {
"\xE0\x9F\x80",
"\xED\xA0\x80",
"\xF0\x80\x80",
"\xF4\x90\x80",
};
const char* inputs6[] = {
"\xE0\x9F",
"\xED\xA0",
"\xF0\x80",
"\xF4\x90",
};
// clang-format on
for (const char* input : inputs5) {
size_t len;
JS::TwoByteCharsZ utf16 = JS::LossyUTF8CharsToNewTwoByteCharsZ(
cx, JS::UTF8Chars(input, js_strlen(input)), &len,
js::StringBufferArena);
CHECK(utf16);
CHECK(len == 3);
CHECK(utf16[0] == 0xFFFD);
CHECK(utf16[1] == 0xFFFD);
CHECK(utf16[2] == 0xFFFD);
len = mozilla::ConvertUtf8toUtf16(mozilla::Span(input, js_strlen(input)),
output);
CHECK(len == 3);
CHECK(outputBuf[0] == 0xFFFD);
CHECK(outputBuf[1] == 0xFFFD);
CHECK(outputBuf[2] == 0xFFFD);
}
for (const char* input : inputs6) {
size_t len;
JS::TwoByteCharsZ utf16 = JS::LossyUTF8CharsToNewTwoByteCharsZ(
cx, JS::UTF8Chars(input, js_strlen(input)), &len,
js::StringBufferArena);
CHECK(utf16);
CHECK(len == 2);
CHECK(utf16[0] == 0xFFFD);
CHECK(utf16[1] == 0xFFFD);
len = mozilla::ConvertUtf8toUtf16(mozilla::Span(input, js_strlen(input)),
output);
CHECK(len == 2);
CHECK(outputBuf[0] == 0xFFFD);
CHECK(outputBuf[1] == 0xFFFD);
}
return true;
}
END_TEST(testUTF8_LossyConversion)
|