use wide::u8x32;
#[cfg(feature = "json-keys")]
mod keys;
#[cfg(feature = "json-paths")]
mod literals;
#[cfg(feature = "json-paths")]
mod paths;
#[cfg(feature = "json-keys")]
pub use keys::{Edit, KeyError, KeyIndex, Member, Members, Park, Spliced, Text};
#[cfg(feature = "json-paths")]
pub use literals::{Compare, LiteralIndex};
#[cfg(feature = "json-paths")]
pub use paths::{Kind, MAX_DOTTED_PARTS, Path, PathError, Paths, Raw, Slot, Slots, Values};
pub const MAX_DEPTH: usize = 64;
const BLOCK: usize = 64;
const ODD_BITS: u64 = 0xAAAA_AAAA_AAAA_AAAA;
#[must_use]
pub fn depth_within(payload: &[u8], max: usize) -> bool {
let (blocks, tail) = payload.as_chunks::<BLOCK>();
let mut scan = Scan::default();
for (i, block) in blocks.iter().enumerate() {
match scan.block(Classes::of(block), max) {
Step::Within => {}
Step::TooDeep => return false,
Step::Irregular => return scan.bytes(&payload[i * BLOCK..], max),
}
}
if tail.is_empty() {
return true;
}
let classes = payload.last_chunk::<BLOCK>().map_or_else(
|| {
let mut padded = [0; BLOCK];
padded[..tail.len()].copy_from_slice(tail);
Classes::of(&padded)
},
|last| Classes::of(last).skip(BLOCK - tail.len()),
);
match scan.block(classes, max) {
Step::Within => true,
Step::TooDeep => false,
Step::Irregular => scan.bytes(tail, max),
}
}
#[derive(Clone, Copy)]
struct Classes {
quotes: u64,
backslashes: u64,
opens: u64,
closes: u64,
}
impl Classes {
#[inline]
fn of(block: &[u8; BLOCK]) -> Self {
let quote = u8x32::splat(b'"');
let backslash = u8x32::splat(b'\\');
let fold = u8x32::splat(0x20);
let open = u8x32::splat(b'{');
let close = u8x32::splat(b'}');
let mut classes = Self {
quotes: 0,
backslashes: 0,
opens: 0,
closes: 0,
};
let (halves, _) = block.as_chunks::<32>();
for (half, shift) in halves.iter().zip([0_u32, 32]) {
let bytes = u8x32::new(*half);
let folded = bytes | fold;
classes.quotes |= u64::from(bytes.simd_eq(quote).to_bitmask()) << shift;
classes.backslashes |= u64::from(bytes.simd_eq(backslash).to_bitmask()) << shift;
classes.opens |= u64::from(folded.simd_eq(open).to_bitmask()) << shift;
classes.closes |= u64::from(folded.simd_eq(close).to_bitmask()) << shift;
}
classes
}
#[inline]
const fn skip(self, seen: usize) -> Self {
Self {
quotes: self.quotes >> seen,
backslashes: self.backslashes >> seen,
opens: self.opens >> seen,
closes: self.closes >> seen,
}
}
}
#[derive(Default)]
struct Scan {
depth: usize,
in_string: u64,
escaped: u64,
}
enum Step {
Within,
TooDeep,
Irregular,
}
impl Scan {
#[inline]
const fn block(&mut self, classes: Classes, max: usize) -> Step {
let Classes {
quotes,
backslashes,
opens,
closes,
} = classes;
let (escaped, next_escaped) = if backslashes == 0 {
(self.escaped, 0)
} else {
let potential = backslashes & !self.escaped;
let codes = ((potential << 1) | ODD_BITS).wrapping_sub(potential) ^ ODD_BITS;
(
codes ^ (backslashes | self.escaped),
(codes & backslashes) >> 63,
)
};
let strings = prefix_xor(quotes & !escaped) ^ self.in_string;
if backslashes & !strings != 0 {
return Step::Irregular;
}
if !self.walk(opens & !strings, closes & !strings, max) {
return Step::TooDeep;
}
self.in_string = 0_u64.wrapping_sub(strings >> 63);
self.escaped = next_escaped;
Step::Within
}
#[inline]
const fn walk(&mut self, opens: u64, closes: u64, max: usize) -> bool {
let n_open = opens.count_ones() as usize;
let n_close = closes.count_ones() as usize;
if n_close <= self.depth && n_open <= max - self.depth {
self.depth = self.depth + n_open - n_close;
return true;
}
let mut marks = opens | closes;
while marks != 0 {
if (opens >> marks.trailing_zeros()) & 1 == 0 {
self.depth = self.depth.saturating_sub(1);
} else {
self.depth += 1;
if self.depth > max {
return false;
}
}
marks &= marks - 1;
}
true
}
fn bytes(&self, rest: &[u8], max: usize) -> bool {
let mut depth = self.depth;
let mut in_string = self.in_string != 0;
let mut escaped = self.escaped != 0;
for &b in rest {
if in_string {
if escaped {
escaped = false;
} else if b == b'\\' {
escaped = true;
} else if b == b'"' {
in_string = false;
}
continue;
}
match b {
b'"' => in_string = true,
b'{' | b'[' => {
depth += 1;
if depth > max {
return false;
}
}
b'}' | b']' => depth = depth.saturating_sub(1),
_ => {}
}
}
true
}
}
#[inline]
const fn prefix_xor(mut bits: u64) -> u64 {
bits ^= bits << 1;
bits ^= bits << 2;
bits ^= bits << 4;
bits ^= bits << 8;
bits ^= bits << 16;
bits ^= bits << 32;
bits
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
#[test]
fn flat_and_shallow_pass() {
assert!(depth_within(br"{}", 64));
assert!(depth_within(br#"{"a":1,"b":[1,2,3]}"#, 64));
assert!(depth_within(br#"{"a":{"b":{"c":1}}}"#, 64));
}
#[test]
fn exactly_at_bound_passes_one_over_fails() {
assert!(depth_within(br"[[[1]]]", 3));
assert!(!depth_within(br"[[[[1]]]]", 3));
}
#[test]
fn braces_inside_strings_do_not_count() {
assert!(depth_within(br#"{"k":"{{{{{{{{[[[[["}"#, 2));
}
#[test]
fn escaped_quote_keeps_string_open() {
assert!(depth_within(br#"{"k":"a\"{{{{{"}"#, 2));
}
#[test]
fn pathological_depth_is_rejected_cheaply() {
let mut deep = vec![b'['; 5000];
deep.extend_from_slice(b"1");
deep.extend(std::iter::repeat_n(b']', 5000));
assert!(!depth_within(&deep, MAX_DEPTH));
}
fn nested(open: &str, close: &str, depth: usize) -> Vec<u8> {
let mut payload = open.repeat(depth).into_bytes();
payload.extend_from_slice(b"1");
payload.extend_from_slice(close.repeat(depth).as_bytes());
payload
}
fn after(n: usize, tail: &[u8]) -> Vec<u8> {
let mut payload = vec![b'x'; n];
payload.extend_from_slice(tail);
payload
}
#[test]
fn empty_payload_passes() {
assert!(depth_within(b"", 0));
assert!(depth_within(b"", MAX_DEPTH));
}
#[test]
fn object_nesting_at_the_default_bound() {
assert!(depth_within(&nested("{\"a\":", "}", MAX_DEPTH), MAX_DEPTH));
assert!(!depth_within(
&nested("{\"a\":", "}", MAX_DEPTH + 1),
MAX_DEPTH
));
}
#[test]
fn sibling_containers_do_not_add_up() {
let wide = format!("[{}]", vec!["[[1]]"; 1000].join(","));
assert!(depth_within(wide.as_bytes(), 3));
}
#[test]
fn an_escaped_backslash_closes_the_string() {
assert!(!depth_within(br#"["\\"[[[1]]]]"#, 3));
}
#[test]
fn pathological_depth_is_refused() {
for depth in [5_000, 20_000, 100_000] {
assert!(!depth_within(&nested("[", "]", depth), MAX_DEPTH));
assert!(!depth_within(&nested("{\"a\":", "}", depth), MAX_DEPTH));
}
}
#[test]
fn a_backslash_outside_a_string_escapes_nothing() {
let payload = format!("[\\\"{}", "[".repeat(200));
assert!(depth_within(payload.as_bytes(), 1));
assert!(reference(payload.as_bytes(), 1));
}
#[test]
fn a_close_with_nothing_open_does_not_go_below_zero() {
let payload = format!("{}{}", "]".repeat(70), "[".repeat(4));
assert!(!depth_within(payload.as_bytes(), 3));
assert!(!reference(payload.as_bytes(), 3));
}
#[test]
fn a_quote_ending_a_lane_or_block_opens_and_closes_a_string() {
for edge in [15, 31, 63, 127] {
let opened = after(edge, br#""[[[["#);
assert!(depth_within(&opened, 0), "edge {edge}");
let mut closed = vec![b'"'];
closed.extend(std::iter::repeat_n(b'a', edge - 1));
closed.extend_from_slice(br#""[["#);
assert!(depth_within(&closed, 2), "edge {edge}");
assert!(!depth_within(&closed, 1), "edge {edge}");
for max in 0..4 {
agrees(&opened, max);
agrees(&closed, max);
}
}
}
#[test]
fn a_backslash_ending_a_lane_or_block_escapes_the_next_byte() {
for edge in [15, 31, 63, 127] {
let mut one = vec![b'"'];
one.extend(std::iter::repeat_n(b'a', edge - 1));
one.extend_from_slice(br#"\"[[["#);
assert!(depth_within(&one, 0), "edge {edge}");
let mut two = vec![b'"'];
two.extend(std::iter::repeat_n(b'a', edge - 2));
two.extend_from_slice(br#"\\"[[["#);
assert!(!depth_within(&two, 2), "edge {edge}");
assert!(depth_within(&two, 3), "edge {edge}");
for max in 0..4 {
agrees(&one, max);
agrees(&two, max);
}
}
}
#[test]
fn backslash_runs_across_a_block_edge_keep_their_parity() {
for run in 1..=9_usize {
for before_edge in 1..=run {
let mut payload = vec![b'"'];
payload.extend(std::iter::repeat_n(b'a', 64 - before_edge - 1));
payload.extend(std::iter::repeat_n(b'\\', run));
payload.extend_from_slice(br#""[["#);
assert_eq!(
depth_within(&payload, 1),
run % 2 == 1,
"run {run}, {before_edge} before the edge"
);
for max in 0..4 {
agrees(&payload, max);
}
}
}
}
#[test]
fn an_unterminated_string_hides_everything_after_it() {
assert!(depth_within(br#"{"a":"[[[[[[[["#, 1));
let long = format!(r#"{{"a":"{}"#, "[{".repeat(200));
assert!(depth_within(long.as_bytes(), 1));
let trailing_escape = format!(r#"{{"a":"{}\"#, "x".repeat(126));
assert!(depth_within(trailing_escape.as_bytes(), 1));
for payload in [long.as_bytes(), trailing_escape.as_bytes()] {
for max in 0..4 {
agrees(payload, max);
}
}
}
#[test]
fn only_open_brackets_add_depth_for_every_byte_value() {
for b in 0_u8..=u8::MAX {
let payload = vec![b; 150];
assert_eq!(
depth_within(&payload, 0),
b != b'[' && b != b'{',
"byte {b:#04x}"
);
for max in 0..4 {
agrees(&payload, max);
}
}
}
#[test]
fn bytes_past_ascii_are_filler_inside_and_outside_strings() {
for b in 0x80_u8..=u8::MAX {
let mut outside = vec![b'['];
outside.extend(std::iter::repeat_n(b, 100));
outside.push(b']');
assert!(depth_within(&outside, 1), "byte {b:#04x}");
assert!(!depth_within(&outside, 0), "byte {b:#04x}");
let mut inside = b"[\"".to_vec();
inside.extend(std::iter::repeat_n(b, 100));
inside.extend_from_slice(b"\"[]]");
assert!(depth_within(&inside, 2), "byte {b:#04x}");
assert!(!depth_within(&inside, 1), "byte {b:#04x}");
}
}
fn reference(payload: &[u8], max: usize) -> bool {
let mut depth: usize = 0;
let mut in_string = false;
let mut escaped = false;
for &b in payload {
if in_string {
if escaped {
escaped = false;
} else if b == b'\\' {
escaped = true;
} else if b == b'"' {
in_string = false;
}
continue;
}
match b {
b'"' => in_string = true,
b'{' | b'[' => {
depth += 1;
if depth > max {
return false;
}
}
b'}' | b']' => depth = depth.saturating_sub(1),
_ => {}
}
}
true
}
fn agrees(payload: &[u8], max: usize) {
assert_eq!(
depth_within(payload, max),
reference(payload, max),
"max {max}, payload {:?}",
String::from_utf8_lossy(payload)
);
}
const STRUCTURAL: &[u8] = b"{}[]\"\\x:, 1";
const EDGE_ALPHABET: &[u8] = b"{}[]\"\\\\\\x \x80\xdb\xfb";
const EDGE_LENGTHS: &[usize] = &[
0, 1, 15, 16, 17, 31, 32, 33, 63, 64, 65, 95, 96, 97, 127, 128, 129, 191, 192, 193,
];
const CASES: u32 = 4096;
const JSON_CASES: u32 = 1024;
fn bound() -> impl Strategy<Value = usize> {
prop_oneof![
Just(0_usize),
Just(1_usize),
Just(2_usize),
Just(MAX_DEPTH),
0_usize..130,
]
}
fn token() -> impl Strategy<Value = Vec<u8>> {
prop_oneof![
4 => prop::sample::select(b"{}[]\"".as_slice()).prop_map(|b| vec![b]),
3 => (1_usize..10).prop_map(|n| vec![b'\\'; n]),
1 => (0x80_u8..=u8::MAX).prop_map(|b| vec![b]),
2 => prop::sample::select(b"x:, 1".as_slice()).prop_map(|b| vec![b]),
]
}
fn json_value() -> impl Strategy<Value = String> {
let string = "[a-z{}\\[\\]\"\\\\]{0,12}".prop_map(|s| {
let mut out = String::from("\"");
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
c => out.push(c),
}
}
out.push('"');
out
});
let leaf = prop_oneof![Just("1".to_string()), Just("null".to_string()), string];
leaf.prop_recursive(120, 4000, 6, |inner| {
prop_oneof![
prop::collection::vec(inner.clone(), 0..6)
.prop_map(|items| format!("[{}]", items.join(","))),
prop::collection::vec(("[a-z\\[{\"\\\\]{0,6}", inner), 0..6).prop_map(|fields| {
let body: Vec<String> = fields
.into_iter()
.map(|(key, value)| format!("{key:?}:{value}"))
.collect();
format!("{{{}}}", body.join(","))
}),
]
})
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(CASES))]
#[test]
fn block_scan_agrees_on_arbitrary_bytes(
payload in prop::collection::vec(any::<u8>(), 0..700),
max in bound(),
) {
agrees(&payload, max);
}
#[test]
fn block_scan_agrees_on_structural_bytes(
payload in prop::collection::vec(prop::sample::select(STRUCTURAL), 0..700),
max in bound(),
) {
agrees(&payload, max);
}
#[test]
fn block_scan_agrees_on_backslash_runs_and_structure(
tokens in prop::collection::vec(token(), 0..160),
max in bound(),
) {
agrees(&tokens.concat(), max);
}
#[test]
fn block_scan_agrees_either_side_of_lane_and_block_edges(
payload in prop::sample::select(EDGE_LENGTHS)
.prop_flat_map(|len| prop::collection::vec(prop::sample::select(EDGE_ALPHABET), len)),
max in bound(),
) {
agrees(&payload, max);
}
#[test]
fn block_scan_agrees_on_deep_and_mismatched_nesting(
depth in 0_usize..400,
offset in 0_usize..130,
max in 0_usize..300,
opens in prop::sample::select(vec!["[", "{\"a\":", "{"]),
closes in prop::sample::select(vec!["]", "}"]),
) {
let payload = format!("{}{}1{}", " ".repeat(offset), opens.repeat(depth), closes.repeat(depth));
agrees(payload.as_bytes(), max);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(JSON_CASES))]
#[test]
fn block_scan_agrees_on_generated_json(json in json_value(), max in bound()) {
agrees(json.as_bytes(), max);
}
}
#[test]
fn block_scan_agrees_on_every_short_string_across_lane_and_block_edges() {
let alphabet = b"{]\"\\x[";
for len in 0..=5_u32 {
for n in 0..alphabet.len().pow(len) {
let mut s = Vec::new();
let mut k = n;
for _ in 0..len {
s.push(alphabet[k % alphabet.len()]);
k /= alphabet.len();
}
for offset in [0, 15, 16, 31, 32, 33, 59, 62, 63, 64, 65, 123, 127, 128] {
let payload = after(offset, &s);
for max in 0..4 {
agrees(&payload, max);
}
}
}
}
}
}