Repository navigation
Expand file tree
/
Copy pathStringDecoder.java
More file actions
278 lines (254 loc) · 9.5 KB
/
Copy pathStringDecoder.java
File metadata and controls
278 lines (254 loc) · 9.5 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
/*
* This code is copyrighted work by Daniel Luz <dev at mernen dot com>.
*
* Distributed under the Ruby license: https://www.ruby-lang.org/en/about/license.txt
*/
package json.ext;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
import org.jruby.util.ByteList;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* A decoder that reads a JSON-encoded string from the given sources and
* returns its decoded form on a new ByteList. Escaped Unicode characters
* are encoded as UTF-8.
*/
final class StringDecoder extends ByteListTranscoder {
/**
* Stores the offset of the high surrogate when reading a surrogate pair,
* or -1 when not.
*/
private int surrogatePairStart = -1;
private boolean allowControlCharacters = false;
private boolean allowInvalidEscape = false;
private final StringScanner scanner = StringScanner.getInstance();
private ByteList out;
// Array used for writing multibyte characters into the buffer at once
private final byte[] aux = new byte[4];
public StringDecoder(boolean allowControlCharacters, boolean allowInvalidEscape) {
this.allowControlCharacters = allowControlCharacters;
this.allowInvalidEscape = allowInvalidEscape;
}
ByteList decode(ThreadContext context, ByteList src, int start, int end) {
try {
init(src, start, end);
this.out = new ByteList(end - start);
while (hasNext()) {
handleChar(context, readUtf8Char(context));
}
quoteStop(pos);
return out;
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
}
}
// Decodes strings with no UTF-8 validation. It is assumed the string is either ASCII-only
// or UTF-8 avlidation has been disabled.
ByteList decodeNoValidate(ThreadContext context, ByteList src, int start, int end, ByteBuffer chunks) {
try {
init(src, start, end);
this.out = new ByteList(end - start);
final byte[] data = src.unsafeBytes();
final int base = src.begin();
final int absEnd = base + srcEnd;
int cursor = base + pos;
int runStart = cursor;
while (cursor < absEnd) {
cursor = scanner.scanEscape(data, chunks, cursor, absEnd);
if (cursor >= absEnd) {
break;
}
int b = Byte.toUnsignedInt(data[cursor]);
if (b == '\\') {
if (cursor > runStart) {
append(data, runStart, cursor - runStart);
}
charStart = cursor - base;
pos = charStart + 1;
handleEscapeSequence(context);
cursor = base + pos;
runStart = cursor;
} else {
if (!allowControlCharacters) {
charStart = cursor - base;
throw invalidControlChar(context);
}
cursor++;
}
}
if (absEnd > runStart) {
append(data, runStart, absEnd - runStart);
}
return out;
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
}
}
@Override
protected int readUtf8Char(ThreadContext context) {
int c = super.readUtf8Char(context);
if (c < 0x20 && !allowControlCharacters) {
throw invalidControlChar(context);
}
return c;
}
private void handleChar(ThreadContext context, int c) throws IOException {
if (c == '\\') {
quoteStop(charStart);
handleEscapeSequence(context);
} else {
quoteStart();
}
}
private void handleEscapeSequence(ThreadContext context) throws IOException {
ensureMin(context, 1);
int character = readUtf8Char(context);
switch (character) {
case 'b':
append('\b');
break;
case 'f':
append('\f');
break;
case 'n':
append('\n');
break;
case 'r':
append('\r');
break;
case 't':
append('\t');
break;
case '/':
append('/');
break;
case '"':
append('"');
break;
case '\\':
append('\\');
break;
case 'u':
ensureMin(context, 4);
int cp = readHex(context);
if (Character.isHighSurrogate((char)cp)) {
handleLowSurrogate(context, (char)cp);
} else if (Character.isLowSurrogate((char)cp)) {
// low surrogate with no high surrogate
throw invalidUtf8(context);
} else {
writeUtf8Char(cp);
}
break;
default:
if (allowInvalidEscape) {
writeUtf8Char(character);
} else {
throw invalidEscape(context);
}
}
}
protected void append(int b) throws IOException {
out.append(b);
}
protected void append(byte[] origin, int start, int length) throws IOException {
out.append(origin, start, length);
}
private void handleLowSurrogate(ThreadContext context, char highSurrogate) throws IOException {
surrogatePairStart = charStart;
ensureMin(context, 1);
int lowSurrogate = readUtf8Char(context);
if (lowSurrogate == '\\') {
ensureMin(context, 5);
if (readUtf8Char(context) != 'u') throw invalidUtf8(context);
lowSurrogate = readHex(context);
}
if (Character.isLowSurrogate((char)lowSurrogate)) {
writeUtf8Char(Character.toCodePoint(highSurrogate,
(char)lowSurrogate));
surrogatePairStart = -1;
} else {
throw invalidUtf8(context);
}
}
private void writeUtf8Char(int codePoint) throws IOException {
if (codePoint < 0x80) {
append(codePoint);
} else if (codePoint < 0x800) {
aux[0] = (byte)(0xc0 | (codePoint >>> 6));
aux[1] = tailByte(codePoint & 0x3f);
append(aux, 0, 2);
} else if (codePoint < 0x10000) {
aux[0] = (byte)(0xe0 | (codePoint >>> 12));
aux[1] = tailByte(codePoint >>> 6);
aux[2] = tailByte(codePoint);
append(aux, 0, 3);
} else {
aux[0] = (byte)(0xf0 | codePoint >>> 18);
aux[1] = tailByte(codePoint >>> 12);
aux[2] = tailByte(codePoint >>> 6);
aux[3] = tailByte(codePoint);
append(aux, 0, 4);
}
}
private byte tailByte(int value) {
return (byte)(0x80 | (value & 0x3f));
}
/**
* Reads a 4-digit unsigned hexadecimal number from the source.
*/
private int readHex(ThreadContext context) {
int numberStart = pos;
int result = 0;
int length = 4;
for (int i = 0; i < length; i++) {
int digit = readUtf8Char(context);
int digitValue;
if (digit >= '0' && digit <= '9') {
digitValue = digit - '0';
} else if (digit >= 'a' && digit <= 'f') {
digitValue = 10 + digit - 'a';
} else if (digit >= 'A' && digit <= 'F') {
digitValue = 10 + digit - 'A';
} else {
throw invalidUnicodeEscape(context, numberStart);
}
result = result * 16 + digitValue;
}
return result;
}
@Override
protected RaiseException invalidUtf8(ThreadContext context) {
ByteList message = new ByteList(
ByteList.plain("partial character in source, " +
"but hit end near "));
int start = surrogatePairStart != -1 ? surrogatePairStart : charStart;
message.append(src, start, srcEnd - start);
return Utils.newException(context, Utils.M_PARSER_ERROR,
context.runtime.newString(message));
}
protected RaiseException invalidControlChar(ThreadContext context) {
ByteList message = new ByteList(
ByteList.plain("invalid ASCII control character in string: "));
message.append(src, charStart, srcEnd - charStart);
return Utils.newException(context, Utils.M_PARSER_ERROR,
context.runtime.newString(message));
}
protected RaiseException invalidEscape(ThreadContext context) {
ByteList message = new ByteList(
ByteList.plain("invalid escape character in string: "));
message.append(src, charStart, srcEnd - charStart);
return Utils.newException(context, Utils.M_PARSER_ERROR,
context.runtime.newString(message));
}
protected RaiseException invalidUnicodeEscape(ThreadContext context, int escapeStart) {
ByteList message = new ByteList(
ByteList.plain("incomplete unicode character escape sequence at "));
// Point at the backslash-u that introduced the escape.
int start = Math.max(escapeStart - 2, 0);
message.append(src, start, srcEnd - start);
return Utils.newException(context, Utils.M_PARSER_ERROR,
context.runtime.newString(message));
}
}