frdt 0.2.4

Edit and inspect Resonite FrDT (.brson) documents without expanding them to JSON
Documentation
//! One streaming walk over a Brotli decoder.
//!
//! Completed siblings are discarded in chunks. What remains is the type table,
//! the nesting state, and the extracted dynamic variables.

use std::io::Read;

use crate::bson::{self, ARRAY, BOOL, DOCUMENT, DOUBLE, INT32, INT64, NULL, STRING};
use crate::datatree::{self, DynKind};
use crate::error::{Error, Result};
use crate::value::DynValue;

/// Counts and dynamic variables from a single pass.
#[derive(Debug, Clone, PartialEq)]
pub struct Summary {
    pub slot_count: u64,
    pub component_count: u64,
    pub types: Vec<String>,
    pub dynamic_variables: Vec<DynamicVariable>,
}

/// A dynamic variable component, or a dynamic variable space.
#[derive(Debug, Clone, PartialEq)]
pub struct DynamicVariable {
    pub kind: DynKind,
    pub type_name: String,
    pub variable_name: Option<String>,
    pub space_name: Option<String>,
    pub value: Option<DynValue>,
    pub reference: Option<DynValue>,
}

#[derive(Default)]
struct PartialComp {
    type_index: Option<i64>,
    variable_name: Option<String>,
    space_name: Option<String>,
    value: Option<DynValue>,
    reference: Option<DynValue>,
}

struct State {
    slot_count: u64,
    component_count: u64,
    types: Vec<String>,
    types_seen: bool,
    dynamic_variables: Vec<DynamicVariable>,
    pending: Vec<PartialComp>,
}

pub fn summarize_reader<R: Read>(reader: &mut R) -> Result<Summary> {
    let mut r = Rdr {
        inner: reader,
        pos: 0,
    };
    let mut state = State {
        slot_count: 0,
        component_count: 0,
        types: Vec::new(),
        types_seen: false,
        dynamic_variables: Vec::new(),
        pending: Vec::new(),
    };
    walk_root(&mut r, &mut state)?;
    flush_pending(&mut state);
    // Drive the decoder to completion. A truncated Brotli frame or bytes after
    // the root document must fail the same way `Document::open` does.
    match r.inner.read(&mut [0u8; 1]) {
        Ok(0) => {}
        Ok(_) => return Err(Error::Invalid("trailing bytes after BSON".into())),
        Err(err) => return Err(err.into()),
    }
    Ok(Summary {
        slot_count: state.slot_count,
        component_count: state.component_count,
        types: state.types,
        dynamic_variables: state.dynamic_variables,
    })
}

struct Rdr<'a, R> {
    inner: &'a mut R,
    pos: u64,
}

impl<R: Read> Rdr<'_, R> {
    fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
        self.inner.read_exact(buf)?;
        self.pos += buf.len() as u64;
        Ok(())
    }

    fn read_u8(&mut self) -> Result<u8> {
        let mut b = [0u8; 1];
        self.read_exact(&mut b)?;
        Ok(b[0])
    }

    fn read_i32(&mut self) -> Result<i32> {
        let mut b = [0u8; 4];
        self.read_exact(&mut b)?;
        Ok(i32::from_le_bytes(b))
    }

    fn read_i64(&mut self) -> Result<i64> {
        let mut b = [0u8; 8];
        self.read_exact(&mut b)?;
        Ok(i64::from_le_bytes(b))
    }

    fn read_f64(&mut self) -> Result<f64> {
        let mut b = [0u8; 8];
        self.read_exact(&mut b)?;
        Ok(f64::from_le_bytes(b))
    }

    fn discard(&mut self, mut n: u64) -> Result<()> {
        let mut buf = [0u8; 8192];
        while n > 0 {
            let chunk = usize::try_from(n.min(8192)).unwrap();
            self.read_exact(&mut buf[..chunk])?;
            n -= chunk as u64;
        }
        Ok(())
    }

    fn read_cstring(&mut self, limit: u64) -> Result<String> {
        let mut raw = Vec::new();
        loop {
            if self.pos >= limit {
                return Err(Error::Truncated);
            }
            let b = self.read_u8()?;
            if b == 0 {
                break;
            }
            if raw.len() > 1024 * 1024 {
                return Err(Error::Invalid("field name is too long".into()));
            }
            raw.push(b);
        }
        String::from_utf8(raw).map_err(|_| Error::Invalid("field name is not utf-8".into()))
    }
}

fn for_each_field<R: Read>(
    r: &mut Rdr<'_, R>,
    mut visit: impl FnMut(&mut Rdr<'_, R>, &str, u8) -> Result<()>,
) -> Result<()> {
    let len = r.read_i32()?;
    if len < 5 {
        return Err(Error::Invalid(format!("document length {len}")));
    }
    let end = r.pos + u64::from(len as u32) - 4;
    while r.pos + 1 < end {
        let ty = r.read_u8()?;
        let key = r.read_cstring(end)?;
        let before = r.pos;
        visit(r, &key, ty)?;
        if r.pos < before {
            return Err(Error::Invalid("reader moved backwards".into()));
        }
        if r.pos == before && ty != NULL {
            return Err(Error::Invalid(format!("field {key} was not consumed")));
        }
        if r.pos > end {
            return Err(Error::Invalid("field ran past its document".into()));
        }
    }
    if r.pos != end - 1 {
        return Err(Error::Invalid("document ended early".into()));
    }
    let term = r.read_u8()?;
    if term != 0 || r.pos != end {
        return Err(Error::Invalid(
            "document is missing its trailing NUL".into(),
        ));
    }
    Ok(())
}

fn skip_value<R: Read>(r: &mut Rdr<'_, R>, ty: u8) -> Result<()> {
    match ty {
        DOUBLE => r.discard(8),
        STRING => {
            let n = r.read_i32()?;
            if n < 1 {
                return Err(Error::Invalid("string length".into()));
            }
            r.discard(n as u64)
        }
        DOCUMENT | ARRAY => {
            let n = r.read_i32()?;
            if n < 5 {
                return Err(Error::Invalid("nested document length".into()));
            }
            r.discard(u64::from(n as u32) - 4)
        }
        bson::BINARY => {
            let n = r.read_i32()?;
            if n < 0 {
                return Err(Error::Invalid("binary length".into()));
            }
            r.discard(1 + n as u64)
        }
        BOOL => r.discard(1),
        NULL => Ok(()),
        INT32 => r.discard(4),
        INT64 => r.discard(8),
        other => Err(Error::UnsupportedType(other)),
    }
}

fn walk_root<R: Read>(r: &mut Rdr<'_, R>, state: &mut State) -> Result<()> {
    for_each_field(r, |r, key, ty| match key {
        "Types" if ty == ARRAY => read_types(r, state),
        "Object" if ty == DOCUMENT => {
            state.slot_count += 1;
            walk_slot(r, state)
        }
        _ => skip_value(r, ty),
    })
}

fn read_types<R: Read>(r: &mut Rdr<'_, R>, state: &mut State) -> Result<()> {
    for_each_field(r, |r, _key, ty| {
        if ty != STRING {
            return Err(Error::Invalid("Types entry is not a string".into()));
        }
        state.types.push(read_string(r)?);
        Ok(())
    })?;
    state.types_seen = true;
    flush_pending(state);
    Ok(())
}

fn walk_slot<R: Read>(r: &mut Rdr<'_, R>, state: &mut State) -> Result<()> {
    for_each_field(r, |r, key, ty| match key {
        "Children" if ty == ARRAY => walk_children(r, state),
        "Components" if ty == DOCUMENT => walk_components_ptr(r, state),
        _ => skip_value(r, ty),
    })
}

fn walk_children<R: Read>(r: &mut Rdr<'_, R>, state: &mut State) -> Result<()> {
    for_each_field(r, |r, _key, ty| {
        if ty != DOCUMENT {
            return Err(Error::Invalid(
                "Children entry is not a slot document".into(),
            ));
        }
        state.slot_count += 1;
        walk_slot(r, state)
    })
}

fn walk_components_ptr<R: Read>(r: &mut Rdr<'_, R>, state: &mut State) -> Result<()> {
    for_each_field(r, |r, key, ty| {
        if key == "Data" && ty == ARRAY {
            for_each_field(r, |r, _key, ty| {
                if ty != DOCUMENT {
                    return Err(Error::Invalid("component is not a document".into()));
                }
                walk_component(r, state)
            })
        } else {
            skip_value(r, ty)
        }
    })
}

fn walk_component<R: Read>(r: &mut Rdr<'_, R>, state: &mut State) -> Result<()> {
    let mut partial = PartialComp::default();
    for_each_field(r, |r, key, ty| match key {
        "Type" if ty == INT32 || ty == INT64 => {
            partial.type_index = Some(if ty == INT32 {
                i64::from(r.read_i32()?)
            } else {
                r.read_i64()?
            });
            Ok(())
        }
        "Data" if ty == DOCUMENT => read_component_data(r, &mut partial),
        _ => skip_value(r, ty),
    })?;
    state.component_count += 1;
    if state.types_seen {
        if let Some(var) = resolve(&state.types, &partial) {
            state.dynamic_variables.push(var);
        }
    } else {
        state.pending.push(partial);
    }
    Ok(())
}

fn read_component_data<R: Read>(r: &mut Rdr<'_, R>, partial: &mut PartialComp) -> Result<()> {
    for_each_field(r, |r, key, ty| match key {
        "VariableName" => {
            partial.variable_name = dyn_string(read_dyn(r, ty, 0)?);
            Ok(())
        }
        "SpaceName" => {
            partial.space_name = dyn_string(read_dyn(r, ty, 0)?);
            Ok(())
        }
        "Value" => {
            partial.value = Some(read_dyn(r, ty, 0)?);
            Ok(())
        }
        "Reference" => {
            partial.reference = Some(read_dyn(r, ty, 0)?);
            Ok(())
        }
        _ => skip_value(r, ty),
    })
}

fn read_dyn<R: Read>(r: &mut Rdr<'_, R>, ty: u8, depth: usize) -> Result<DynValue> {
    if depth > 8 {
        skip_value(r, ty)?;
        return Ok(DynValue::Null);
    }
    Ok(match ty {
        DOUBLE => DynValue::F64(r.read_f64()?),
        STRING => DynValue::String(read_string(r)?),
        BOOL => DynValue::Bool(r.read_u8()? != 0),
        INT32 => DynValue::I64(i64::from(r.read_i32()?)),
        INT64 => DynValue::I64(r.read_i64()?),
        NULL => DynValue::Null,
        DOCUMENT => read_doc_dyn(r, depth + 1)?,
        ARRAY => {
            skip_value(r, ty)?;
            DynValue::Null
        }
        bson::BINARY => {
            skip_value(r, ty)?;
            DynValue::Null
        }
        other => return Err(Error::UnsupportedType(other)),
    })
}

fn read_doc_dyn<R: Read>(r: &mut Rdr<'_, R>, depth: usize) -> Result<DynValue> {
    let mut fields = Vec::new();
    for_each_field(r, |r, key, ty| {
        if matches!(key, "ID" | "Data" | "x" | "y" | "z" | "w") {
            fields.push((key.to_string(), read_dyn(r, ty, depth)?));
        } else {
            skip_value(r, ty)?;
        }
        Ok(())
    })?;
    Ok(datatree::interpret_dyn_fields(&fields))
}

fn read_string<R: Read>(r: &mut Rdr<'_, R>) -> Result<String> {
    let n = r.read_i32()?;
    if n < 1 {
        return Err(Error::Invalid("string length".into()));
    }
    let mut raw = vec![0u8; n as usize];
    r.read_exact(&mut raw)?;
    if raw.last() != Some(&0) {
        return Err(Error::Invalid("string is missing NUL".into()));
    }
    raw.pop();
    String::from_utf8(raw).map_err(|_| Error::Invalid("string is not utf-8".into()))
}

fn dyn_string(value: DynValue) -> Option<String> {
    match value {
        DynValue::String(s) => Some(s),
        _ => None,
    }
}

fn resolve(types: &[String], partial: &PartialComp) -> Option<DynamicVariable> {
    let index = partial.type_index?;
    let type_name = types.get(usize::try_from(index).ok()?)?.clone();
    let kind = datatree::is_dyn_type(&type_name)?;
    Some(DynamicVariable {
        kind,
        type_name,
        variable_name: partial.variable_name.clone(),
        space_name: partial.space_name.clone(),
        value: partial.value.clone(),
        reference: partial.reference.clone(),
    })
}

fn flush_pending(state: &mut State) {
    let pending = std::mem::take(&mut state.pending);
    for partial in pending {
        if let Some(var) = resolve(&state.types, &partial) {
            state.dynamic_variables.push(var);
        }
    }
}