use std::io;
use crate::decoder::{Decoder, MAX_NODE_DEPTH, OpenContent};
use crate::error::{BinaryError, Result};
use crate::node::{AttrsRef, NodeRef, NodeStr, ValueRef};
use crate::util::{FORMAT_COMPRESSED, MAX_DECOMPRESSED_SIZE};
use crate::zlib_pool::InflateReader;
const FRAME_INFLATE_CHUNK: usize = 4096;
enum Source<'a> {
Plain { data: &'a [u8], pos: usize },
Inflate(InflateReader<'a>),
}
impl Source<'_> {
#[inline]
fn available(&self) -> &[u8] {
match self {
Self::Plain { data, pos } => &data[*pos..],
Self::Inflate(reader) => reader.available(),
}
}
#[inline]
fn consume(&mut self, n: usize) {
match self {
Self::Plain { data, pos } => *pos = (*pos + n).min(data.len()),
Self::Inflate(reader) => reader.consume(n),
}
}
fn ensure(&mut self, need: usize) -> Result<bool> {
match self {
Self::Plain { data, pos } => Ok(data.len() - *pos >= need),
Self::Inflate(reader) => reader.ensure(need).map_err(zlib_error),
}
}
}
fn zlib_error(e: io::Error) -> BinaryError {
BinaryError::Zlib(e.to_string())
}
#[derive(Debug)]
pub struct OpenNode<'b> {
pub tag: NodeStr<'b>,
pub attrs: AttrsRef<'b>,
pub content: OpenContent<'b>,
}
impl<'b> OpenNode<'b> {
pub fn get_attr(&self, key: &str) -> Option<&ValueRef<'b>> {
self.attrs.iter().find(|(k, _)| k == key).map(|(_, v)| v)
}
pub fn attr_str(&self, key: &str) -> Option<std::borrow::Cow<'_, str>> {
self.get_attr(key).map(|v| v.as_str())
}
pub fn child_count(&self) -> usize {
match self.content {
OpenContent::Children(n) => n,
_ => 0,
}
}
}
struct Level {
remaining: usize,
}
pub struct NodeStream<'a> {
source: Source<'a>,
pending: usize,
consumed: usize,
levels: Vec<Level>,
root_done: bool,
}
impl<'a> NodeStream<'a> {
pub fn from_packed(packed: &'a [u8]) -> Result<Self> {
let Some((&format, node_bytes)) = packed.split_first() else {
return Err(BinaryError::EmptyData);
};
if format & FORMAT_COMPRESSED != 0 {
Ok(Self::with_source(Source::Inflate(
InflateReader::with_chunk(node_bytes, MAX_DECOMPRESSED_SIZE, FRAME_INFLATE_CHUNK),
)))
} else {
Ok(Self::from_node_bytes(node_bytes))
}
}
pub fn from_node_bytes(node_bytes: &'a [u8]) -> Self {
Self::with_source(Source::Plain {
data: node_bytes,
pos: 0,
})
}
fn with_source(source: Source<'a>) -> Self {
Self {
source,
pending: 0,
consumed: 0,
levels: Vec::new(),
root_done: false,
}
}
pub fn is_compressed(&self) -> bool {
matches!(self.source, Source::Inflate(_))
}
pub fn depth(&self) -> usize {
self.levels.len()
}
pub fn open(&mut self) -> Result<Option<OpenNode<'_>>> {
if !self.take_slot() {
return Ok(None);
}
if self.levels.len() >= MAX_NODE_DEPTH {
return Err(BinaryError::MaxDepthExceeded);
}
let Self {
source,
pending,
levels,
..
} = self;
let (head, used) = Self::decode_next(source, |decoder| decoder.read_node_open())?;
*pending = used;
levels.push(Level {
remaining: match head.content {
OpenContent::Children(n) => n,
_ => 0,
},
});
Ok(Some(OpenNode {
tag: head.tag,
attrs: head.attrs,
content: head.content,
}))
}
pub fn next_child(&mut self) -> Result<Option<NodeRef<'_>>> {
if !self.take_slot() {
return Ok(None);
}
let depth = self.levels.len();
let Self {
source, pending, ..
} = self;
let (node, used) = Self::decode_next(source, |decoder| decoder.read_node_ref_at(depth))?;
*pending = used;
Ok(Some(node))
}
pub fn close(&mut self) -> Result<()> {
self.settle();
let Some(level) = self.levels.last() else {
return Ok(());
};
let mut remaining = level.remaining;
while remaining > 0 {
let depth = self.levels.len();
let used = Self::skip_next(&mut self.source, |decoder| decoder.skip_node_at(depth))?;
self.source.consume(used);
self.consumed += used;
remaining -= 1;
}
self.levels.pop();
Ok(())
}
pub fn finish(&mut self) -> Result<()> {
while !self.levels.is_empty() {
self.close()?;
}
if !self.root_done {
self.root_done = true;
let used = Self::skip_next(&mut self.source, |decoder| decoder.skip_node_at(0))?;
self.source.consume(used);
self.consumed += used;
}
self.settle();
match &mut self.source {
Source::Plain { data, pos } => {
if *pos < data.len() {
return Err(BinaryError::LeftoverData(data.len() - *pos));
}
}
Source::Inflate(reader) => {
if reader.ensure(1).map_err(zlib_error)? {
return Err(BinaryError::LeftoverData(reader.available().len()));
}
if !reader.stream_ended() {
return Err(BinaryError::Zlib("zlib stream truncated".into()));
}
}
}
Ok(())
}
pub fn into_inflated(self) -> Option<Result<Vec<u8>>> {
if self.consumed != 0 {
return None;
}
match self.source {
Source::Plain { .. } => None,
Source::Inflate(reader) => Some(reader.read_to_end().map_err(zlib_error)),
}
}
fn settle(&mut self) {
if self.pending != 0 {
self.source.consume(self.pending);
self.consumed += self.pending;
self.pending = 0;
}
}
fn take_slot(&mut self) -> bool {
self.settle();
match self.levels.last_mut() {
Some(level) => {
if level.remaining == 0 {
return false;
}
level.remaining -= 1;
true
}
None => {
if self.root_done {
return false;
}
self.root_done = true;
true
}
}
}
fn decode_next<'s, T>(
source: &'s mut Source<'_>,
decode: impl Fn(&mut Decoder<'s>) -> Result<T>,
) -> Result<(T, usize)> {
loop {
let window: *const [u8] = source.available();
let bytes: &'s [u8] = unsafe { &*window };
let have = bytes.len();
let mut decoder = Decoder::new(bytes);
match decode(&mut decoder) {
Ok(value) => return Ok((value, decoder.position())),
Err(BinaryError::UnexpectedEof) => {}
Err(e) => return Err(e),
}
if !Self::grow(source, have)? {
return Err(BinaryError::UnexpectedEof);
}
}
}
fn skip_next(
source: &mut Source<'_>,
walk: impl Fn(&mut Decoder<'_>) -> Result<()>,
) -> Result<usize> {
loop {
let have = source.available().len();
let mut decoder = Decoder::new(source.available());
match walk(&mut decoder) {
Ok(()) => return Ok(decoder.position()),
Err(BinaryError::UnexpectedEof) => {}
Err(e) => return Err(e),
}
if !Self::grow(source, have)? {
return Err(BinaryError::UnexpectedEof);
}
}
}
fn grow(source: &mut Source<'_>, have: usize) -> Result<bool> {
let want = have.max(64).saturating_mul(2);
Ok(source.ensure(want)? || source.available().len() > have)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::builder::NodeBuilder;
use crate::marshal::marshal;
use crate::node::{Node, NodeContent, NodeContentRef};
use crate::zlib_pool::test_support::stored_zlib;
fn compress(node_bytes: &[u8]) -> Vec<u8> {
let mut out = vec![FORMAT_COMPRESSED];
out.extend_from_slice(&stored_zlib(node_bytes));
out
}
fn both_forms(node: &Node) -> Vec<(&'static str, Vec<u8>)> {
let packed = marshal(node).unwrap();
let compressed = compress(&packed[1..]);
vec![("plain", packed), ("compressed", compressed)]
}
fn prop(code: u32, value: &str) -> Node {
NodeBuilder::new("prop")
.attr("config_code", code)
.attr("config_value", value)
.build()
}
fn props_iq(count: usize) -> Node {
let mut children: Vec<Node> = (0..count as u32)
.map(|i| prop(1000 + i, if i % 3 == 0 { "true" } else { "17" }))
.collect();
let big_value = "x".repeat(3 * FRAME_INFLATE_CHUNK);
children.insert(count / 2, prop(7, &big_value));
let props = NodeBuilder::new("props")
.attr("protocol", "1")
.attr("hash", "abc")
.children(children)
.build();
NodeBuilder::new("iq")
.attr("type", "result")
.attr("id", "42")
.attr("from", "s.whatsapp.net")
.children(vec![props])
.build()
}
#[test]
#[cfg_attr(miri, ignore)]
fn walks_children_one_at_a_time_in_both_forms() {
let node = props_iq(2000);
let expected: Vec<Node> = match &node.content {
Some(NodeContent::Nodes(c)) => match &c[0].content {
Some(NodeContent::Nodes(props)) => props.clone(),
_ => unreachable!(),
},
_ => unreachable!(),
};
for (form, packed) in both_forms(&node) {
let mut stream = NodeStream::from_packed(&packed).unwrap();
assert_eq!(stream.is_compressed(), form == "compressed");
let root = stream.open().unwrap().expect("root");
assert_eq!(root.tag, "iq");
assert_eq!(root.attr_str("id").as_deref(), Some("42"));
assert_eq!(root.child_count(), 1);
let props = stream.open().unwrap().expect("props");
assert_eq!(props.tag, "props");
assert_eq!(props.attr_str("hash").as_deref(), Some("abc"));
assert_eq!(props.child_count(), expected.len());
assert_eq!(stream.depth(), 2);
let mut seen = 0;
while let Some(child) = stream.next_child().unwrap() {
assert_eq!(child.to_owned(), expected[seen], "{form}: child {seen}");
seen += 1;
}
assert_eq!(seen, expected.len(), "{form}");
stream.close().unwrap();
assert!(
stream.next_child().unwrap().is_none(),
"{form}: iq has one child"
);
stream.close().unwrap();
assert!(
stream.open().unwrap().is_none(),
"{form}: nothing after the root"
);
stream.finish().unwrap();
}
}
#[test]
fn close_skips_unread_children_and_continues_with_siblings() {
let node = NodeBuilder::new("iq")
.children(vec![
NodeBuilder::new("first")
.children((0..50).map(|i| prop(i, "v")))
.build(),
NodeBuilder::new("second")
.children(vec![prop(1, "a"), prop(2, "b")])
.build(),
NodeBuilder::new("third").attr("k", "v").build(),
])
.build();
for (form, packed) in both_forms(&node) {
let mut stream = NodeStream::from_packed(&packed).unwrap();
stream.open().unwrap().expect("root");
let first = stream.open().unwrap().expect("first");
assert_eq!(first.tag, "first");
for _ in 0..3 {
stream.next_child().unwrap().expect("child");
}
stream.close().unwrap();
let second = stream.next_child().unwrap().expect("second, whole");
assert_eq!(second.tag, "second");
assert_eq!(second.children().map(|c| c.len()), Some(2));
let third = stream.open().unwrap().expect("third");
assert_eq!(third.tag, "third");
assert_eq!(third.attr_str("k").as_deref(), Some("v"));
assert_eq!(third.content, OpenContent::None);
assert!(stream.next_child().unwrap().is_none());
stream.close().unwrap();
assert!(stream.next_child().unwrap().is_none(), "{form}");
stream.finish().unwrap();
}
}
#[test]
fn scalar_content_and_empty_nodes_open_without_children() {
let node = NodeBuilder::new("message")
.children(vec![
NodeBuilder::new("body").string_content("hello").build(),
NodeBuilder::new("enc").bytes(vec![1, 2, 3]).build(),
NodeBuilder::new("empty").build(),
])
.build();
for (_, packed) in both_forms(&node) {
let mut stream = NodeStream::from_packed(&packed).unwrap();
stream.open().unwrap().expect("root");
let body = stream.open().unwrap().expect("body");
assert!(matches!(
&body.content,
OpenContent::Scalar(NodeContentRef::Bytes(b)) if b.as_ref() == b"hello"
));
assert!(stream.next_child().unwrap().is_none());
stream.close().unwrap();
let enc = stream.open().unwrap().expect("enc");
assert!(matches!(
&enc.content,
OpenContent::Scalar(NodeContentRef::Bytes(b)) if b.as_ref() == [1, 2, 3]
));
stream.close().unwrap();
let empty = stream.open().unwrap().expect("empty");
assert_eq!(empty.content, OpenContent::None);
stream.close().unwrap();
stream.finish().unwrap();
}
}
#[test]
#[cfg_attr(miri, ignore)]
fn finish_rejects_leftover_bytes_and_a_truncated_stream() {
let node = props_iq(10);
let mut packed = marshal(&node).unwrap();
packed.push(0);
let mut stream = NodeStream::from_packed(&packed).unwrap();
stream.open().unwrap();
assert!(matches!(stream.finish(), Err(BinaryError::LeftoverData(1))));
let node = props_iq(300);
let compressed = compress(&marshal(&node).unwrap()[1..]);
let truncated = &compressed[..compressed.len() - 2];
let mut stream = NodeStream::from_packed(truncated).unwrap();
stream.open().unwrap();
stream.open().unwrap();
while stream.next_child().unwrap().is_some() {}
assert!(matches!(stream.finish(), Err(BinaryError::Zlib(_))));
let truncated = &compressed[..compressed.len() * 9 / 10];
let mut stream = NodeStream::from_packed(truncated).unwrap();
stream.open().unwrap();
stream.open().unwrap();
let mut result = Ok(());
loop {
match stream.next_child() {
Ok(Some(_)) => {}
Ok(None) => break,
Err(e) => {
result = Err(e);
break;
}
}
}
assert!(
matches!(result, Err(BinaryError::UnexpectedEof)),
"{result:?}"
);
let mut stream = NodeStream::from_packed(&compressed).unwrap();
stream.finish().unwrap();
}
#[test]
#[cfg_attr(miri, ignore)]
fn into_inflated_after_peeking_the_root_matches_unpack() {
let node = props_iq(500);
let packed = marshal(&node).unwrap();
let compressed = compress(&packed[1..]);
let mut stream = NodeStream::from_packed(&compressed).unwrap();
let root = stream.open().unwrap().expect("root");
assert_eq!(root.tag, "iq");
let whole = stream.into_inflated().expect("compressed").unwrap();
assert_eq!(&whole[..], &packed[1..]);
let mut stream = NodeStream::from_packed(&packed).unwrap();
stream.open().unwrap();
assert!(stream.into_inflated().is_none());
let mut stream = NodeStream::from_packed(&compressed).unwrap();
stream.open().unwrap();
stream.open().unwrap();
assert!(stream.into_inflated().is_none());
}
#[test]
#[cfg_attr(miri, ignore)]
fn depth_is_capped_like_the_tree_decoder() {
let mut node = NodeBuilder::new("leaf").build();
for _ in 0..(MAX_NODE_DEPTH + 2) {
node = NodeBuilder::new("n").children(vec![node]).build();
}
let packed = marshal(&node).unwrap();
let mut stream = NodeStream::from_packed(&packed).unwrap();
let mut result = Ok(());
loop {
match stream.open() {
Ok(Some(_)) => {}
Ok(None) => break,
Err(e) => {
result = Err(e);
break;
}
}
}
assert!(matches!(result, Err(BinaryError::MaxDepthExceeded)));
}
#[test]
fn empty_payload_is_refused() {
assert!(matches!(
NodeStream::from_packed(&[]),
Err(BinaryError::EmptyData)
));
}
#[test]
fn skipping_a_malformed_child_fails_like_the_tree_decoder() {
let node = NodeBuilder::new("iq")
.children(vec![
NodeBuilder::new("first").build(),
prop(1, "hello"),
prop(2, "12345"),
])
.build();
let packed = marshal(&node).unwrap();
let mut bad_utf8 = packed.clone();
let at = bad_utf8
.windows(5)
.position(|w| w == b"hello")
.expect("the value is stored verbatim");
bad_utf8[at] = 0xFF;
let mut bad_nibble = packed.clone();
let at = bad_nibble
.windows(3)
.position(|w| w == [0x12, 0x34, 0x5F])
.expect("the digits are nibble-packed");
bad_nibble[at] = 0xC2;
for (name, bytes) in [("utf-8", bad_utf8), ("nibble", bad_nibble)] {
assert!(
crate::marshal::unmarshal_ref(&bytes[1..]).is_err(),
"{name}: the tree decoder must reject the fixture"
);
let mut stream = NodeStream::from_packed(&bytes).unwrap();
stream.open().unwrap().expect("root");
stream.open().unwrap().expect("first");
stream.close().unwrap();
assert!(
stream.close().is_err(),
"{name}: skipping the rest must fail"
);
let mut stream = NodeStream::from_packed(&bytes).unwrap();
assert!(stream.finish().is_err(), "{name}: finish must fail");
}
}
#[test]
fn skipping_a_child_with_no_tag_or_a_non_string_key_fails_like_the_tree_decoder() {
use crate::token::{BINARY_8, LIST_8, LIST_EMPTY};
let root_head = [LIST_8, 2, BINARY_8, 2, b'i', b'q', LIST_8, 2];
let first = [LIST_8, 1, BINARY_8, 5, b'f', b'i', b'r', b's', b't'];
let no_tag = vec![LIST_8, 1, LIST_EMPTY];
let bad_key = vec![LIST_8, 3, BINARY_8, 1, b'p', LIST_EMPTY, BINARY_8, 1, b'v'];
for (name, child, expected) in [
("no tag", no_tag, BinaryError::InvalidNode),
("non-string key", bad_key, BinaryError::NonStringKey),
] {
let mut packed = vec![crate::util::FORMAT_PLAIN];
packed.extend_from_slice(&root_head);
packed.extend_from_slice(&first);
packed.extend_from_slice(&child);
assert!(
matches!(&crate::marshal::unmarshal_ref(&packed[1..]), Err(e) if std::mem::discriminant(e) == std::mem::discriminant(&expected)),
"{name}: the tree decoder must reject the fixture with {expected:?}"
);
let mut stream = NodeStream::from_packed(&packed).unwrap();
stream.open().unwrap().expect("root");
stream.open().unwrap().expect("first");
stream.close().unwrap();
let err = stream.close().expect_err("skipping the rest must fail");
assert_eq!(
std::mem::discriminant(&err),
std::mem::discriminant(&expected),
"{name}: {err:?}"
);
let mut stream = NodeStream::from_packed(&packed).unwrap();
assert!(stream.finish().is_err(), "{name}: finish must fail");
}
}
}