use super::key::Key;
use super::keypath::KeyPath;
use crate::webapi::serialize::{tag, FLAG_STORAGE, MAGIC, VERSION};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PKey {
Name(Vec<u16>),
Index(u32),
}
#[derive(Debug)]
enum Node {
Undefined,
Other,
Number(f64),
Str(Vec<u16>),
Obj { props: Vec<(PKey, usize)>, end: usize },
Arr { len: u32, props: Vec<(PKey, usize)>, end: usize },
Date(f64),
Buffer { start: usize, len: usize },
View { kind: u8, off: u64, len1: u64, buf: usize },
Blob { mime: Vec<u16>, size: u64, file: Option<(Vec<u16>, f64)> },
StrObj(Vec<u16>),
Error { props: Vec<(PKey, usize)> },
Ref(u32),
}
pub struct Parsed<'a> {
bytes: &'a [u8],
nodes: Vec<Node>,
offsets: Vec<usize>,
id_node: Vec<usize>,
id_offset: Vec<usize>,
refs: Vec<(usize, usize, u32)>,
}
enum Kind {
Props,
Seq,
Fixed,
}
struct Frame {
node: usize,
kind: Kind,
left: u32,
key: Option<PKey>,
}
struct Rd<'a> {
b: &'a [u8],
pos: usize,
}
impl Rd<'_> {
fn byte(&mut self) -> Option<u8> {
let v = *self.b.get(self.pos)?;
self.pos += 1;
Some(v)
}
fn peek(&self) -> Option<u8> {
self.b.get(self.pos).copied()
}
fn varint(&mut self) -> Option<u64> {
let mut v: u64 = 0;
for shift in (0..70).step_by(7) {
let b = self.byte()?;
if shift == 63 && b > 1 {
return None;
}
v |= ((b & 0x7f) as u64) << shift;
if b & 0x80 == 0 {
return Some(v);
}
}
None
}
fn f64(&mut self) -> Option<f64> {
let s = self.b.get(self.pos..self.pos + 8)?;
self.pos += 8;
Some(f64::from_le_bytes(s.try_into().ok()?))
}
fn skip(&mut self, n: usize) -> Option<()> {
if self.pos.checked_add(n)? > self.b.len() {
return None;
}
self.pos += n;
Some(())
}
fn string(&mut self) -> Option<Vec<u16>> {
let t = self.byte()?;
self.string_body(t)
}
fn string_body(&mut self, t: u8) -> Option<Vec<u16>> {
let n = self.varint()? as usize;
match t {
tag::STRING_LATIN1 => {
let s = self.b.get(self.pos..self.pos.checked_add(n)?)?;
self.pos += n;
Some(s.iter().map(|&c| c as u16).collect())
}
tag::STRING_UTF16 => {
let s = self.b.get(self.pos..self.pos.checked_add(n.checked_mul(2)?)?)?;
self.pos += n * 2;
Some(s.chunks_exact(2).map(|c| u16::from_le_bytes([c[0], c[1]])).collect())
}
_ => None,
}
}
}
impl<'a> Parsed<'a> {
pub fn parse(bytes: &'a [u8]) -> Option<Parsed<'a>> {
if bytes.len() < 5 || bytes[..3] != MAGIC || bytes[3] != VERSION || bytes[4] & !FLAG_STORAGE != 0 {
return None;
}
let mut p = Parsed { bytes, nodes: Vec::new(), offsets: Vec::new(), id_node: Vec::new(), id_offset: Vec::new(), refs: Vec::new() };
let mut r = Rd { b: bytes, pos: 5 };
let mut stack: Vec<Frame> = Vec::new();
let mut have_root = false;
loop {
if let Some(f) = stack.last_mut() {
match f.kind {
Kind::Props if f.key.is_none() => {
let t = r.byte()?;
if t == tag::END {
let end = r.pos - 1;
let f = stack.pop().unwrap();
match &mut p.nodes[f.node] {
Node::Obj { end: e, .. } | Node::Arr { end: e, .. } => *e = end,
_ => {}
}
continue;
}
let k = if t == tag::KEY_INDEX {
PKey::Index(u32::try_from(r.varint()?).ok()?)
} else {
PKey::Name(r.string_body(t)?)
};
f.key = Some(k);
continue;
}
Kind::Seq if r.peek()? == tag::END => {
r.pos += 1;
stack.pop();
continue;
}
Kind::Fixed if f.left == 0 => {
stack.pop();
continue;
}
_ => {}
}
} else if have_root {
break;
}
let (idx, frame) = p.value(&mut r)?;
match stack.last_mut() {
None => have_root = true,
Some(f) => match f.kind {
Kind::Props => {
let k = f.key.take()?;
match &mut p.nodes[f.node] {
Node::Obj { props, .. } | Node::Arr { props, .. } | Node::Error { props } => props.push((k, idx)),
_ => return None,
}
}
Kind::Seq => {}
Kind::Fixed => {
f.left -= 1;
match &mut p.nodes[f.node] {
Node::View { buf, .. } => *buf = idx,
Node::Error { props } => props.push((PKey::Name("cause".encode_utf16().collect()), idx)),
_ => {}
}
}
},
}
if let Some(fr) = frame {
stack.push(fr);
}
if stack.len() > 1_000_000 {
return None;
}
}
if r.pos != bytes.len() {
return None;
}
Some(p)
}
fn push(&mut self, n: Node, at: usize) -> usize {
self.nodes.push(n);
self.offsets.push(at);
self.nodes.len() - 1
}
fn object(&mut self, n: Node, at: usize) -> usize {
let idx = self.push(n, at);
self.id_node.push(idx);
self.id_offset.push(at);
idx
}
fn value(&mut self, r: &mut Rd) -> Option<(usize, Option<Frame>)> {
let at = r.pos;
let t = r.byte()?;
let leaf = |p: &mut Self, n: Node| Some((p.push(n, at), None));
match t {
tag::UNDEFINED => leaf(self, Node::Undefined),
tag::NULL | tag::FALSE | tag::TRUE => leaf(self, Node::Other),
tag::INT32 => {
let z = r.varint()? as u32;
let i = ((z >> 1) as i32) ^ -((z & 1) as i32);
leaf(self, Node::Number(i as f64))
}
tag::DOUBLE => {
let d = r.f64()?;
leaf(self, Node::Number(d))
}
tag::BIGINT => {
let n = r.varint()? as usize;
r.skip(n)?;
leaf(self, Node::Other)
}
tag::STRING_LATIN1 | tag::STRING_UTF16 => {
let s = r.string_body(t)?;
leaf(self, Node::Str(s))
}
tag::OBJECT_REF => {
let id = u32::try_from(r.varint()?).ok()?;
if id as usize >= self.id_node.len() {
return None;
}
self.refs.push((at, r.pos - at, id));
leaf(self, Node::Ref(id))
}
tag::OBJECT => {
let idx = self.object(Node::Obj { props: Vec::new(), end: 0 }, at);
Some((idx, Some(Frame { node: idx, kind: Kind::Props, left: 0, key: None })))
}
tag::ARRAY => {
let len = u32::try_from(r.varint()?).ok()?;
let idx = self.object(Node::Arr { len, props: Vec::new(), end: 0 }, at);
Some((idx, Some(Frame { node: idx, kind: Kind::Props, left: 0, key: None })))
}
tag::DATE => {
let d = r.f64()?;
Some((self.object(Node::Date(d), at), None))
}
tag::REGEXP => {
let idx = self.object(Node::Other, at);
r.string()?;
r.string()?;
Some((idx, None))
}
tag::MAP | tag::SET => {
let idx = self.object(Node::Other, at);
Some((idx, Some(Frame { node: idx, kind: Kind::Seq, left: 0, key: None })))
}
tag::BOOLEAN_OBJECT => {
let idx = self.object(Node::Other, at);
r.skip(1)?;
Some((idx, None))
}
tag::NUMBER_OBJECT => {
let idx = self.object(Node::Other, at);
r.skip(8)?;
Some((idx, None))
}
tag::STRING_OBJECT => {
let idx = self.object(Node::Other, at);
let s = r.string()?;
self.nodes[idx] = Node::StrObj(s);
Some((idx, None))
}
tag::BIGINT_OBJECT => {
let idx = self.object(Node::Other, at);
let n = r.varint()? as usize;
r.skip(n)?;
Some((idx, None))
}
tag::ERROR => {
let idx = self.object(Node::Error { props: Vec::new() }, at);
let _name = r.byte()?;
let mask = r.byte()?;
if mask & !7 != 0 {
return None;
}
let mut props = Vec::new();
if mask & 1 != 0 {
let o = r.pos;
let m = r.string()?;
let n = self.push(Node::Str(m), o);
props.push((PKey::Name("message".encode_utf16().collect()), n));
}
if mask & 2 != 0 {
let o = r.pos;
let s = r.string()?;
let n = self.push(Node::Str(s), o);
props.push((PKey::Name("stack".encode_utf16().collect()), n));
}
self.nodes[idx] = Node::Error { props };
let frame = (mask & 4 != 0).then_some(Frame { node: idx, kind: Kind::Fixed, left: 1, key: None });
Some((idx, frame))
}
tag::ARRAY_BUFFER | tag::RESIZABLE_ARRAY_BUFFER => {
let n = r.varint()? as usize;
if t == tag::RESIZABLE_ARRAY_BUFFER {
r.varint()?;
}
let start = r.pos;
r.skip(n)?;
Some((self.object(Node::Buffer { start, len: n }, at), None))
}
tag::VIEW => {
let kind = r.byte()?;
let off = r.varint()?;
let len1 = r.varint()?;
let idx = self.object(Node::View { kind, off, len1, buf: usize::MAX }, at);
Some((idx, Some(Frame { node: idx, kind: Kind::Fixed, left: 1, key: None })))
}
tag::BLOB | tag::FILE => {
let idx = self.object(Node::Other, at);
let mime = r.string()?;
let n = r.varint()?;
r.skip(usize::try_from(n).ok()?)?;
let file = if t == tag::FILE {
let name = r.string()?;
let last_modified = r.f64()?;
r.string()?; Some((name, last_modified))
} else {
None
};
self.nodes[idx] = Node::Blob { mime, size: n, file };
Some((idx, None))
}
tag::FILE_LIST => {
let idx = self.object(Node::Other, at);
let n = u32::try_from(r.varint()?).ok()?;
let frame = (n > 0).then_some(Frame { node: idx, kind: Kind::Fixed, left: n, key: None });
Some((idx, frame))
}
tag::IMAGE_DATA => {
let idx = self.object(Node::Other, at);
r.varint()?;
r.varint()?;
r.skip(1)?;
Some((idx, Some(Frame { node: idx, kind: Kind::Fixed, left: 1, key: None })))
}
tag::DOMPOINT => {
let idx = self.object(Node::Other, at);
r.skip(1 + 32)?;
Some((idx, None))
}
tag::DOMMATRIX => {
let idx = self.object(Node::Other, at);
r.skip(1)?;
let is2d = r.byte()?;
r.skip(if is2d != 0 { 48 } else { 128 })?;
Some((idx, None))
}
tag::DOMEXCEPTION => {
let idx = self.object(Node::Other, at);
r.skip(1)?;
r.string()?;
r.string()?;
let mask = r.byte()?;
if mask & 1 != 0 {
r.string()?;
}
if mask & 2 != 0 {
r.skip(8)?;
}
if mask & 4 != 0 {
r.skip(8)?;
}
Some((idx, None))
}
tag::HOST_OBJECT => {
let idx = self.object(Node::Other, at);
r.string()?;
let n = r.varint()?;
r.skip(usize::try_from(n).ok()?)?;
Some((idx, None))
}
_ => None, }
}
fn resolve(&self, mut i: usize) -> usize {
for _ in 0..4 {
match self.nodes[i] {
Node::Ref(id) => i = self.id_node[id as usize],
_ => return i,
}
}
i
}
fn own<'b>(&'b self, props: &'b [(PKey, usize)], name: &[u16]) -> Option<usize> {
props.iter().find(|(k, _)| matches!(k, PKey::Name(n) if n == name)).map(|(_, v)| *v)
}
fn root(&self) -> usize {
0
}
}
#[derive(Debug)]
pub enum Val {
Node(usize),
Num(f64),
Date(f64),
Str(Vec<u16>),
List(Vec<Val>),
}
const LENGTH: &[u16] = &[0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68];
const SIZE: &[u16] = &[0x73, 0x69, 0x7a, 0x65];
const TYPE: &[u16] = &[0x74, 0x79, 0x70, 0x65];
const NAME: &[u16] = &[0x6e, 0x61, 0x6d, 0x65];
const LAST_MODIFIED: &[u16] = &[0x6c, 0x61, 0x73, 0x74, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64];
const LAST_MODIFIED_DATE: &[u16] = &[
0x6c, 0x61, 0x73, 0x74, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x44, 0x61, 0x74, 0x65,
];
impl Parsed<'_> {
pub fn eval(&self, kp: &KeyPath) -> Option<Val> {
match kp {
KeyPath::List(items) => {
let mut out = Vec::with_capacity(items.len());
for p in items {
out.push(self.eval_one(self.root(), p)?);
}
Some(Val::List(out))
}
KeyPath::String(p) => self.eval_one(self.root(), p),
}
}
fn eval_one(&self, start: usize, path: &[u16]) -> Option<Val> {
let mut v = Val::Node(start);
if path.is_empty() {
return Some(v);
}
for ident in path.split(|&c| c == b'.' as u16) {
v = match v {
Val::Str(s) if ident == LENGTH => Val::Num(s.len() as f64),
Val::Num(_) | Val::Date(_) | Val::Str(_) | Val::List(_) => return None,
Val::Node(i) => {
let i = self.resolve(i);
match &self.nodes[i] {
Node::Str(s) if ident == LENGTH => Val::Num(s.len() as f64),
Node::StrObj(s) if ident == LENGTH => Val::Num(s.len() as f64),
Node::Arr { len, .. } if ident == LENGTH => Val::Num(*len as f64),
Node::Blob { size, .. } if ident == SIZE => Val::Num(*size as f64),
Node::Blob { mime, .. } if ident == TYPE => Val::Str(mime.clone()),
Node::Blob { file: Some((name, _)), .. } if ident == NAME => Val::Str(name.clone()),
Node::Blob { file: Some((_, lm)), .. } if ident == LAST_MODIFIED => Val::Num(*lm),
Node::Blob { file: Some((_, lm)), .. } if ident == LAST_MODIFIED_DATE => Val::Date(*lm),
Node::Obj { props, .. } | Node::Arr { props, .. } | Node::Error { props } => {
let c = self.own(props, ident)?;
if matches!(self.nodes[self.resolve(c)], Node::Undefined) {
return None;
}
Val::Node(c)
}
_ => return None,
}
}
};
}
Some(v)
}
pub fn to_key(&self, v: &Val) -> Option<Key> {
let mut seen = Vec::new();
self.convert(v, &mut seen, 0)
}
fn convert(&self, v: &Val, seen: &mut Vec<usize>, depth: u32) -> Option<Key> {
if depth > 100_000 {
return None;
}
match v {
Val::Num(d) => (!d.is_nan()).then_some(Key::Number(*d)),
Val::Date(d) => (!d.is_nan()).then_some(Key::Date(*d)),
Val::Str(s) => Some(Key::String(s.clone())),
Val::List(items) => {
let mut keys = Vec::with_capacity(items.len());
for it in items {
keys.push(self.convert(it, seen, depth + 1)?);
}
Some(Key::Array(keys))
}
Val::Node(i) => self.convert_node(*i, seen, depth),
}
}
fn convert_node(&self, i: usize, seen: &mut Vec<usize>, depth: u32) -> Option<Key> {
let i = self.resolve(i);
match &self.nodes[i] {
Node::Number(d) => (!d.is_nan()).then_some(Key::Number(*d)),
Node::Date(d) => (!d.is_nan()).then_some(Key::Date(*d)),
Node::Str(s) => Some(Key::String(s.clone())),
Node::Buffer { .. } | Node::View { .. } => self.binary(i).map(Key::Binary),
Node::Arr { len, props, .. } => {
if seen.contains(&i) {
return None;
}
seen.push(i);
let mut keys = Vec::with_capacity(*len as usize);
for ix in 0..*len {
let c = props.iter().find(|(k, _)| *k == PKey::Index(ix)).map(|(_, v)| *v)?;
keys.push(self.convert(&Val::Node(c), seen, depth + 1)?);
}
Some(Key::Array(keys))
}
_ => None,
}
}
fn binary(&self, i: usize) -> Option<Vec<u8>> {
match &self.nodes[i] {
Node::Buffer { start, len } => Some(self.bytes[*start..*start + *len].to_vec()),
Node::View { kind, off, len1, buf } => {
let b = self.resolve(*buf);
let Node::Buffer { start, len } = self.nodes[b] else { return None };
let elem: u64 = match kind {
1..=3 | 12 => 1,
4 | 5 | 13 => 2,
6..=8 => 4,
9..=11 => 8,
_ => return None,
};
let n = if *len1 == 0 { (len as u64).saturating_sub(*off) / elem * elem } else { (len1 - 1).saturating_mul(elem) };
if off.saturating_add(n) > len as u64 {
return None;
}
let s = start + *off as usize;
Some(self.bytes[s..s + n as usize].to_vec())
}
_ => None,
}
}
pub fn extract(&self, kp: &KeyPath) -> Result<Option<Key>, ()> {
let v = self.eval(kp).ok_or(())?;
Ok(self.to_key(&v))
}
pub fn extract_multi(&self, kp: &KeyPath) -> Result<Option<Key>, ()> {
let v = self.eval(kp).ok_or(())?;
if let Val::Node(i) = v {
let i = self.resolve(i);
if let Node::Arr { len, props, .. } = &self.nodes[i] {
let mut seen = vec![i];
let mut out: Vec<Key> = Vec::new();
let mut enc: Vec<Vec<u8>> = Vec::new();
for ix in 0..*len {
let Some(c) = props.iter().find(|(k, _)| *k == PKey::Index(ix)).map(|(_, v)| *v) else { continue };
if let Some(k) = self.convert(&Val::Node(c), &mut seen, 1) {
let e = k.encode();
if !enc.contains(&e) {
enc.push(e);
out.push(k);
}
}
}
return Ok(Some(Key::Array(out)));
}
return Ok(self.to_key(&Val::Node(i)));
}
Ok(self.to_key(&v))
}
pub fn can_inject(&self, path: &[u16]) -> bool {
let idents: Vec<&[u16]> = path.split(|&c| c == b'.' as u16).collect();
let mut v = self.root();
for ident in &idents[..idents.len() - 1] {
let i = self.resolve(v);
match &self.nodes[i] {
Node::Obj { props, .. } | Node::Arr { props, .. } => match self.own(props, ident) {
None => return true,
Some(c) => v = c,
},
_ => return false,
}
}
matches!(self.nodes[self.resolve(v)], Node::Obj { .. } | Node::Arr { .. })
}
}
fn put_varint(out: &mut Vec<u8>, mut v: u64) {
loop {
let b = (v & 0x7f) as u8;
v >>= 7;
if v == 0 {
out.push(b);
return;
}
out.push(b | 0x80);
}
}
fn put_name(out: &mut Vec<u8>, name: &[u16]) {
if name.iter().all(|&c| c <= 0xFF) {
out.push(tag::STRING_LATIN1);
put_varint(out, name.len() as u64);
out.extend(name.iter().map(|&c| c as u8));
} else {
out.push(tag::STRING_UTF16);
put_varint(out, name.len() as u64);
for &c in name {
out.extend_from_slice(&c.to_le_bytes());
}
}
}
pub fn number_bytes(d: f64) -> Vec<u8> {
let mut out = Vec::with_capacity(9);
let i = d as i32;
if i as f64 == d && !(i == 0 && d.is_sign_negative()) {
out.push(tag::INT32);
put_varint(&mut out, ((i << 1) ^ (i >> 31)) as u32 as u64);
} else {
out.push(tag::DOUBLE);
out.extend_from_slice(&d.to_le_bytes());
}
out
}
pub fn inject(bytes: &[u8], path: &[u16], key: f64) -> Option<Vec<u8>> {
let p = Parsed::parse(bytes)?;
let idents: Vec<&[u16]> = path.split(|&c| c == b'.' as u16).collect();
let (last, mids) = idents.split_last()?;
let mut v = p.root();
let mut missing_from: Option<usize> = None;
for (n, ident) in mids.iter().enumerate() {
let i = p.resolve(v);
match &p.nodes[i] {
Node::Obj { props, .. } | Node::Arr { props, .. } => match p.own(props, ident) {
None => {
v = i;
missing_from = Some(n);
break;
}
Some(c) => v = c,
},
_ => return None,
}
}
let target = p.resolve(v);
let (props, end) = match &p.nodes[target] {
Node::Obj { props, end } | Node::Arr { props, end, .. } => (props, *end),
_ => return None,
};
let value = number_bytes(key);
let (at, remove, insert, created) = match missing_from {
Some(n) => {
let mut ins = Vec::new();
let chain = &mids[n..];
for ident in chain {
put_name(&mut ins, ident);
ins.push(tag::OBJECT);
}
put_name(&mut ins, last);
ins.extend_from_slice(&value);
ins.extend(core::iter::repeat_n(tag::END, chain.len()));
(end, 0usize, ins, chain.len() as u32)
}
None => match p.own(props, last) {
Some(c) => {
if !matches!(p.nodes[c], Node::Undefined) {
return None;
}
(p.offsets[c], 1usize, value, 0)
}
None => {
let mut ins = Vec::new();
put_name(&mut ins, last);
ins.extend_from_slice(&value);
(end, 0usize, ins, 0)
}
},
};
let first_shifted = p.id_offset.iter().position(|&o| o >= at).unwrap_or(p.id_offset.len()) as u32;
let mut out = Vec::with_capacity(bytes.len() + insert.len() + 8);
let mut pos = 0usize;
let mut refs = p.refs.iter().peekable();
let mut inserted = false;
while pos < bytes.len() || !inserted {
if !inserted && pos == at {
out.extend_from_slice(&insert);
pos += remove;
inserted = true;
continue;
}
if let Some(&&(ro, rl, id)) = refs.peek() {
if ro == pos {
refs.next();
out.push(tag::OBJECT_REF);
let id = if created > 0 && id >= first_shifted { id + created } else { id };
put_varint(&mut out, id as u64);
pos += rl;
continue;
}
}
if pos >= bytes.len() {
break;
}
let mut next = bytes.len();
if !inserted {
next = next.min(at);
}
if let Some(&&(ro, _, _)) = refs.peek() {
next = next.min(ro);
}
out.extend_from_slice(&bytes[pos..next]);
pos = next;
}
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
fn u(s: &str) -> Vec<u16> {
s.encode_utf16().collect()
}
fn hdr() -> Vec<u8> {
let mut v = MAGIC.to_vec();
v.push(VERSION);
v.push(FLAG_STORAGE);
v
}
fn name(out: &mut Vec<u8>, s: &str) {
put_name(out, &u(s));
}
#[test]
fn evaluates_nested_paths_lengths_and_lists() {
let mut b = hdr();
b.push(tag::OBJECT);
name(&mut b, "a");
b.push(tag::OBJECT);
name(&mut b, "b");
b.extend(number_bytes(5.0));
b.push(tag::END);
name(&mut b, "s");
name(&mut b, "hey");
name(&mut b, "arr");
b.push(tag::ARRAY);
put_varint(&mut b, 2);
b.push(tag::KEY_INDEX);
put_varint(&mut b, 0);
b.extend(number_bytes(1.0));
b.push(tag::KEY_INDEX);
put_varint(&mut b, 1);
b.extend(number_bytes(2.0));
b.push(tag::END);
b.push(tag::END);
let p = Parsed::parse(&b).expect("parses");
let kp = |s: &str| KeyPath::String(u(s));
assert_eq!(p.extract(&kp("a.b")), Ok(Some(Key::Number(5.0))));
assert_eq!(p.extract(&kp("s.length")), Ok(Some(Key::Number(3.0))));
assert_eq!(p.extract(&kp("arr.length")), Ok(Some(Key::Number(2.0))));
assert_eq!(p.extract(&kp("arr")), Ok(Some(Key::Array(vec![Key::Number(1.0), Key::Number(2.0)]))));
assert_eq!(p.extract(&kp("missing")), Err(()));
assert_eq!(p.extract(&kp("a")), Ok(None), "an object is not a key");
assert_eq!(
p.extract(&KeyPath::List(vec![u("s"), u("a.b")])),
Ok(Some(Key::Array(vec![Key::String(u("hey")), Key::Number(5.0)])))
);
assert!(p.can_inject(&u("x")));
assert!(p.can_inject(&u("q.r.s")));
assert!(p.can_inject(&u("a.c")));
assert!(!p.can_inject(&u("s.x")), "cannot inject into a string");
}
#[test]
fn a_file_answers_name_and_last_modified_and_a_file_list_parses() {
let mut b = hdr();
b.push(tag::OBJECT); name(&mut b, "f");
b.push(tag::FILE); name(&mut b, "t/x");
put_varint(&mut b, 2);
b.extend(b"ab");
name(&mut b, "n.txt");
b.extend(7f64.to_le_bytes());
name(&mut b, "");
name(&mut b, "l");
b.push(tag::FILE_LIST); put_varint(&mut b, 1);
b.push(tag::OBJECT_REF);
put_varint(&mut b, 1);
name(&mut b, "after");
b.extend(number_bytes(3.0));
b.push(tag::END);
let p = Parsed::parse(&b).expect("parses");
let kp = |s: &str| KeyPath::String(u(s));
assert_eq!(p.extract(&kp("f.name")), Ok(Some(Key::String(u("n.txt")))));
assert_eq!(p.extract(&kp("f.lastModified")), Ok(Some(Key::Number(7.0))));
assert_eq!(p.extract(&kp("f.lastModifiedDate")), Ok(Some(Key::Date(7.0))), "Chromium's extra identifier");
assert_eq!(p.extract(&kp("f.size")), Ok(Some(Key::Number(2.0))));
assert_eq!(p.extract(&kp("f.type")), Ok(Some(Key::String(u("t/x")))));
assert_eq!(p.extract(&kp("after")), Ok(Some(Key::Number(3.0))));
assert_eq!(p.extract(&kp("l.length")), Err(()), "IndexedDB names no FileList identifier");
}
#[test]
fn a_blob_has_no_file_identifiers() {
let mut b = hdr();
b.push(tag::BLOB);
name(&mut b, "");
put_varint(&mut b, 0);
let p = Parsed::parse(&b).expect("parses");
assert_eq!(p.extract(&KeyPath::String(u("name"))), Err(()));
assert_eq!(p.extract(&KeyPath::String(u("size"))), Ok(Some(Key::Number(0.0))));
}
#[test]
fn a_host_object_is_skipped_whole_and_takes_an_id() {
let mut b = hdr();
b.push(tag::OBJECT); name(&mut b, "h");
b.push(tag::HOST_OBJECT); name(&mut b, "FileSystemHandle");
put_varint(&mut b, 3);
b.extend([tag::END, tag::OBJECT, 0xFF]); name(&mut b, "again");
b.push(tag::OBJECT_REF);
put_varint(&mut b, 1);
name(&mut b, "k");
b.extend(number_bytes(4.0));
b.push(tag::END);
let p = Parsed::parse(&b).expect("parses");
assert_eq!(p.extract(&KeyPath::String(u("k"))), Ok(Some(Key::Number(4.0))));
assert_eq!(p.extract(&KeyPath::String(u("h"))), Ok(None), "a handle is not a key");
assert_eq!(p.extract(&KeyPath::String(u("h.name"))), Err(()), "nor has identifiers");
let cut = b.iter().position(|&x| x == 0xFF).unwrap();
assert!(Parsed::parse(&b[..cut]).is_none());
}
#[test]
fn injection_splices_and_renumbers_references() {
let mut b = hdr();
b.push(tag::OBJECT); name(&mut b, "o");
b.push(tag::OBJECT); b.push(tag::END);
name(&mut b, "again");
b.push(tag::OBJECT_REF);
put_varint(&mut b, 1);
b.push(tag::END);
let out = inject(&b, &u("n.id"), 7.0).expect("injects");
let p = Parsed::parse(&out).expect("still parses");
assert_eq!(p.extract(&KeyPath::String(u("n.id"))), Ok(Some(Key::Number(7.0))));
let again = p.eval(&KeyPath::String(u("again"))).unwrap();
let o = p.eval(&KeyPath::String(u("o"))).unwrap();
match (again, o) {
(Val::Node(a), Val::Node(o)) => assert_eq!(p.resolve(a), p.resolve(o)),
_ => panic!(),
}
let mut b2 = hdr();
b2.push(tag::OBJECT); name(&mut b2, "a");
b2.push(tag::OBJECT); b2.push(tag::END);
name(&mut b2, "b");
b2.push(tag::OBJECT); name(&mut b2, "tag");
name(&mut b2, "B");
b2.push(tag::END);
name(&mut b2, "c");
b2.push(tag::OBJECT_REF);
put_varint(&mut b2, 2);
b2.push(tag::END);
let out = inject(&b2, &u("a.x.y"), 3.0).expect("injects");
let p = Parsed::parse(&out).expect("parses");
assert_eq!(p.extract(&KeyPath::String(u("a.x.y"))), Ok(Some(Key::Number(3.0))));
assert_eq!(p.extract(&KeyPath::String(u("c.tag"))), Ok(Some(Key::String(u("B")))));
let mut b3 = hdr();
b3.push(tag::OBJECT);
name(&mut b3, "id");
b3.push(tag::UNDEFINED);
name(&mut b3, "z");
b3.extend(number_bytes(1.0));
b3.push(tag::END);
let pb = Parsed::parse(&b3).unwrap();
assert_eq!(pb.extract(&KeyPath::String(u("id"))), Err(()), "undefined is failure");
let out = inject(&b3, &u("id"), 1e10).expect("injects");
let p = Parsed::parse(&out).unwrap();
assert_eq!(p.extract(&KeyPath::String(u("id"))), Ok(Some(Key::Number(1e10))));
assert_eq!(p.extract(&KeyPath::String(u("z"))), Ok(Some(Key::Number(1.0))));
}
#[test]
fn deep_values_parse_without_recursion() {
let depth = 200_000;
let mut b = hdr();
for _ in 0..depth {
b.push(tag::OBJECT);
name(&mut b, "a");
}
b.extend(number_bytes(1.0));
b.extend(core::iter::repeat_n(tag::END, depth));
assert!(Parsed::parse(&b).is_some());
b.pop();
assert!(Parsed::parse(&b).is_none());
}
}