/
string_decoder.rs
188 lines (172 loc) · 7.05 KB
/
string_decoder.rs
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
use std::marker::PhantomData;
use std::ops::Range;
use crate::errors::{json_err, json_error, JsonResult};
pub type Tape = Vec<u8>;
pub trait AbstractStringDecoder<'t> {
type Output;
fn decode<'d>(data: &'d [u8], index: usize, tape: &'t mut Tape) -> JsonResult<(Self::Output, usize)>
where
'd: 't;
}
pub struct StringDecoder<'t> {
_phantom: &'t PhantomData<()>,
}
impl<'t> AbstractStringDecoder<'t> for StringDecoder<'t> {
type Output = &'t str;
fn decode<'d>(data: &'d [u8], mut index: usize, tape: &'t mut Tape) -> JsonResult<(Self::Output, usize)>
where
'd: 't,
{
index += 1;
tape.clear();
let start = index;
let mut last_escape = start;
let mut found_escape = false;
let mut ascii_only = true;
while let Some(next) = data.get(index) {
match next {
b'"' => {
// in theory we could use `std::str::from_utf8_unchecked` here,
// it leads to big performance gains but some cases e.g. good_high_order_string
// passing when they error here, serde uses `std::str::from_utf8`, python's `json.loads`
// allows these higher order strings
let s = if found_escape {
tape.extend_from_slice(&data[last_escape..index]);
to_str(tape, ascii_only, start)?
} else {
to_str(&data[start..index], ascii_only, start)?
};
index += 1;
return Ok((s, index));
}
b'\\' => {
found_escape = true;
tape.extend_from_slice(&data[last_escape..index]);
index += 1;
if let Some(next_inner) = data.get(index) {
match next_inner {
b'"' | b'\\' | b'/' => tape.push(*next_inner),
b'b' => tape.push(b'\x08'),
b'f' => tape.push(b'\x0C'),
b'n' => tape.push(b'\n'),
b'r' => tape.push(b'\r'),
b't' => tape.push(b'\t'),
b'u' => {
let (c, new_index) = parse_escape(data, index)?;
index = new_index;
tape.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
}
_ => return json_err!(InvalidEscape, index),
}
last_escape = index + 1;
} else {
break;
}
}
// all values below 32 are invalid
next if *next < 32u8 => return json_err!(ControlCharacterWhileParsingString, index),
next if *next >= 128u8 && ascii_only => {
ascii_only = false;
}
_ => (),
}
index += 1;
}
json_err!(EofWhileParsingString, index) // -1 to help match serde's error locations
}
}
fn to_str(bytes: &[u8], ascii_only: bool, start: usize) -> JsonResult<&str> {
if ascii_only {
// safety: in this case we've already confirmed that all characters are ascii, we can safely
// transmute from bytes to str
Ok(unsafe { std::str::from_utf8_unchecked(bytes) })
} else {
std::str::from_utf8(bytes).map_err(|e| json_error!(InvalidUnicodeCodePoint, start + e.valid_up_to() + 1))
}
}
/// Taken approximately from https://github.com/serde-rs/json/blob/v1.0.107/src/read.rs#L872-L945
fn parse_escape(data: &[u8], index: usize) -> JsonResult<(char, usize)> {
let (n, index) = parse_u4(data, index)?;
match n {
0xDC00..=0xDFFF => json_err!(LoneLeadingSurrogateInHexEscape, index),
0xD800..=0xDBFF => match data.get(index + 1..index + 3) {
Some(slice) if slice == b"\\u" => {
let (n2, index) = parse_u4(data, index + 2)?;
if !(0xDC00..=0xDFFF).contains(&n2) {
return json_err!(LoneLeadingSurrogateInHexEscape, index);
}
let n2 = (((n - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
match char::from_u32(n2) {
Some(c) => Ok((c, index)),
None => json_err!(EofWhileParsingString, index),
}
}
Some(slice) if slice.starts_with(b"\\") => json_err!(UnexpectedEndOfHexEscape, index + 2),
Some(_) => json_err!(UnexpectedEndOfHexEscape, index + 1),
None => match data.get(index + 1) {
Some(b'\\') | None => json_err!(EofWhileParsingString, data.len()),
Some(_) => json_err!(UnexpectedEndOfHexEscape, index + 1),
},
},
_ => match char::from_u32(n as u32) {
Some(c) => Ok((c, index)),
None => json_err!(InvalidEscape, index),
},
}
}
fn parse_u4(data: &[u8], mut index: usize) -> JsonResult<(u16, usize)> {
let mut n = 0;
let u4 = data
.get(index + 1..index + 5)
.ok_or_else(|| json_error!(EofWhileParsingString, data.len()))?;
for c in u4.iter() {
index += 1;
let hex = match c {
b'0'..=b'9' => (c & 0x0f) as u16,
b'a'..=b'f' => (c - b'a' + 10) as u16,
b'A'..=b'F' => (c - b'A' + 10) as u16,
_ => return json_err!(InvalidEscape, index),
};
n = (n << 4) + hex;
}
Ok((n, index))
}
pub struct StringDecoderRange;
impl<'t> AbstractStringDecoder<'t> for StringDecoderRange {
type Output = Range<usize>;
fn decode<'d>(data: &'d [u8], mut index: usize, _tape: &'t mut Tape) -> JsonResult<(Self::Output, usize)>
where
'd: 't,
{
index += 1;
let start = index;
while let Some(next) = data.get(index) {
match next {
b'"' => {
let r = start..index;
index += 1;
return Ok((r, index));
}
b'\\' => {
index += 1;
if let Some(next_inner) = data.get(index) {
match next_inner {
// these escapes are easy to validate
b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => (),
// unicode escapes are harder to validate, we just prevent them here
b'u' => return json_err!(StringEscapeNotSupported, index),
_ => return json_err!(InvalidEscape, index),
}
} else {
return json_err!(EofWhileParsingString, index);
}
index += 1;
}
_ => {
index += 1;
}
}
}
json_err!(EofWhileParsingString, index)
}
}