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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
#![allow(clippy::zero_prefixed_literal)]
use musli::{Buf, Context};
use crate::parser::{Parser, SliceParser};
// Copied and adapter form the serde-json project under the MIT and Apache 2.0
// license.
//
// See: https://github.com/serde-rs/json
// Lookup table of bytes that must be escaped. A value of true at index i means
// that byte i requires an escape sequence in the input.
static ESCAPE: [bool; 256] = {
const CT: bool = true; // control character \x00..=\x1F
const QU: bool = true; // quote \x22
const BS: bool = true; // backslash \x5C
const __: bool = false; // allow unescaped
[
// 1 2 3 4 5 6 7 8 9 A B C D E F
CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
]
};
/// A parsed string reference.
#[doc(hidden)]
pub enum StringReference<'de, 'scratch> {
Borrowed(&'de str),
Scratch(&'scratch str),
}
/// Specialized reader implementation from a slice.
pub(crate) fn parse_string_slice_reader<'de, 'scratch, C, S>(
cx: &C,
reader: &mut SliceParser<'de>,
validate: bool,
start: C::Mark,
scratch: &'scratch mut S,
) -> Result<StringReference<'de, 'scratch>, C::Error>
where
C: ?Sized + Context,
S: ?Sized + Buf,
{
// Index of the first byte not yet copied into the scratch space.
let mut open_mark = cx.mark();
let mut open = reader.index;
loop {
while reader.index < reader.slice.len() && !ESCAPE[reader.slice[reader.index] as usize] {
reader.index = reader.index.wrapping_add(1);
cx.advance(1);
}
if reader.index == reader.slice.len() {
return Err(cx.message("End of input"));
}
match reader.slice[reader.index] {
b'"' => {
if scratch.is_empty() {
// Fast path: return a slice of the raw JSON without any
// copying.
let borrowed = &reader.slice[open..reader.index];
reader.index = reader.index.wrapping_add(1);
cx.advance(1);
check_utf8(cx, borrowed, start)?;
// SAFETY: we've checked each segment to be valid UTF-8.
let borrowed = unsafe { core::str::from_utf8_unchecked(borrowed) };
return Ok(StringReference::Borrowed(borrowed));
} else {
let slice = &reader.slice[open..reader.index];
check_utf8(cx, slice, start)?;
if !scratch.write(slice) {
return Err(cx.message("Scratch buffer overflow"));
}
reader.index = reader.index.wrapping_add(1);
cx.advance(1);
// SAFETY: we've checked each segment to be valid UTF-8.
let scratch = unsafe { core::str::from_utf8_unchecked(scratch.as_slice()) };
return Ok(StringReference::Scratch(scratch));
}
}
b'\\' => {
let slice = &reader.slice[open..reader.index];
check_utf8(cx, slice, start)?;
if !scratch.write(slice) {
return Err(cx.message("Scratch buffer overflow"));
}
reader.index = reader.index.wrapping_add(1);
cx.advance(1);
if !parse_escape(cx, reader, validate, scratch)? {
return Err(cx.marked_message(open_mark, "Buffer overflow"));
}
open = reader.index;
open_mark = cx.mark();
}
_ => {
if validate {
return Err(
cx.marked_message(open_mark, "Control character while parsing string")
);
}
reader.index = reader.index.wrapping_add(1);
cx.advance(1);
}
}
}
}
/// Check that the given slice is valid UTF-8.
#[inline]
fn check_utf8<C>(cx: &C, bytes: &[u8], start: C::Mark) -> Result<(), C::Error>
where
C: ?Sized + Context,
{
if musli_common::str::from_utf8(bytes).is_err() {
Err(cx.marked_message(start, "Invalid unicode string"))
} else {
Ok(())
}
}
/// Parses a JSON escape sequence and appends it into the scratch space. Assumes
/// the previous byte read was a backslash.
fn parse_escape<C, B>(
cx: &C,
parser: &mut SliceParser<'_>,
validate: bool,
scratch: &mut B,
) -> Result<bool, C::Error>
where
C: ?Sized + Context,
B: ?Sized + Buf,
{
let start = cx.mark();
let b = parser.read_byte(cx)?;
let extend = match b {
b'"' => scratch.push(b'"'),
b'\\' => scratch.push(b'\\'),
b'/' => scratch.push(b'/'),
b'b' => scratch.push(b'\x08'),
b'f' => scratch.push(b'\x0c'),
b'n' => scratch.push(b'\n'),
b'r' => scratch.push(b'\r'),
b't' => scratch.push(b'\t'),
b'u' => {
fn encode_surrogate<B>(scratch: &mut B, n: u16) -> bool
where
B: ?Sized + Buf,
{
scratch.write(&[
(n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
(n >> 6 & 0b0011_1111) as u8 | 0b1000_0000,
(n & 0b0011_1111) as u8 | 0b1000_0000,
])
}
let c = match parser.parse_hex_escape(cx)? {
n @ 0xDC00..=0xDFFF => {
return if validate {
Err(cx.marked_message(start, "Lone leading surrogate in hex escape"))
} else {
Ok(encode_surrogate(scratch, n))
};
}
// Non-BMP characters are encoded as a sequence of two hex
// escapes, representing UTF-16 surrogates. If deserializing a
// utf-8 string the surrogates are required to be paired,
// whereas deserializing a byte string accepts lone surrogates.
n1 @ 0xD800..=0xDBFF => {
let pos = cx.mark();
if parser.read_byte(cx)? != b'\\' {
return if validate {
Err(cx.marked_message(pos, "Unexpected end of hex escape"))
} else {
Ok(encode_surrogate(scratch, n1))
};
}
if parser.read_byte(cx)? != b'u' {
return if validate {
Err(cx.marked_message(pos, "Unexpected end of hex escape"))
} else {
if !encode_surrogate(scratch, n1) {
return Ok(false);
}
// The \ prior to this byte started an escape sequence,
// so we need to parse that now. This recursive call
// does not blow the stack on malicious input because
// the escape is not \u, so it will be handled by one
// of the easy nonrecursive cases.
parse_escape(cx, parser, validate, scratch)
};
}
let n2 = parser.parse_hex_escape(cx)?;
if !(0xDC00..=0xDFFF).contains(&n2) {
return Err(
cx.marked_message(start, "Lone leading surrogate in hex escape")
);
}
let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
match char::from_u32(n) {
Some(c) => c,
None => {
return Err(cx.marked_message(start, "Invalid unicode"));
}
}
}
// Every u16 outside of the surrogate ranges above is guaranteed
// to be a legal char.
n => char::from_u32(n as u32).unwrap(),
};
scratch.write(c.encode_utf8(&mut [0u8; 4]).as_bytes())
}
_ => {
return Err(cx.marked_message(start, "Invalid string escape"));
}
};
Ok(extend)
}
static HEX: [u8; 256] = {
const __: u8 = 255; // not a hex digit
[
// 1 2 3 4 5 6 7 8 9 A B C D E F
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 0
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 1
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, // 3
__, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 4
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 5
__, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 6
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
]
};
pub(crate) fn decode_hex_val(val: u8) -> Option<u16> {
let n = HEX[val as usize] as u16;
if n == 255 {
None
} else {
Some(n)
}
}
/// Specialized reader implementation from a slice.
pub(crate) fn skip_string<'de, P, C>(cx: &C, mut p: P, validate: bool) -> Result<(), C::Error>
where
P: Parser<'de>,
C: ?Sized + Context,
{
loop {
while let Some(b) = p.peek_byte(cx)? {
if ESCAPE[b as usize] {
break;
}
p.skip(cx, 1)?;
}
let b = p.read_byte(cx)?;
match b {
b'"' => {
return Ok(());
}
b'\\' => {
skip_escape(cx, p.borrow_mut(), validate)?;
}
_ => {
if validate {
return Err(cx.message("Control character while parsing string"));
}
}
}
}
}
/// Parses a JSON escape sequence and appends it into the scratch space. Assumes
/// the previous byte read was a backslash.
fn skip_escape<'de, P, C>(cx: &C, mut p: P, validate: bool) -> Result<(), C::Error>
where
P: Parser<'de>,
C: ?Sized + Context,
{
let start = cx.mark();
let b = p.read_byte(cx)?;
match b {
b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => (),
b'u' => {
match p.parse_hex_escape(cx)? {
0xDC00..=0xDFFF => {
return if validate {
Err(cx.marked_message(start, "Lone leading surrogate in hex escape"))
} else {
Ok(())
};
}
// Non-BMP characters are encoded as a sequence of two hex
// escapes, representing UTF-16 surrogates. If deserializing a
// utf-8 string the surrogates are required to be paired,
// whereas deserializing a byte string accepts lone surrogates.
n1 @ 0xD800..=0xDBFF => {
let pos = cx.mark();
if p.read_byte(cx)? != b'\\' {
return if validate {
Err(cx.marked_message(pos, "Unexpected end of hex escape"))
} else {
Ok(())
};
}
if p.read_byte(cx)? != b'u' {
return if validate {
Err(cx.marked_message(pos, "Unexpected end of hex escape"))
} else {
// The \ prior to this byte started an escape sequence,
// so we need to parse that now. This recursive call
// does not blow the stack on malicious input because
// the escape is not \u, so it will be handled by one
// of the easy nonrecursive cases.
skip_escape(cx, p, validate)
};
}
let n2 = p.parse_hex_escape(cx)?;
if !(0xDC00..=0xDFFF).contains(&n2) {
return Err(
cx.marked_message(start, "Lone leading surrogate in hex escape")
);
}
let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
if char::from_u32(n).is_none() {
return Err(cx.marked_message(start, "Invalid unicode"));
}
}
// Every u16 outside of the surrogate ranges above is guaranteed
// to be a legal char.
_ => (),
}
}
_ => {
return Err(cx.marked_message(start, "Invalid string escape"));
}
};
Ok(())
}