From 439dc00d1ce9ff3e5dc05f32a0426152fc5fab89 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 14 Jan 2023 21:37:40 +0100 Subject: reproduction *almost* perfect --- ebml/src/write.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'ebml/src/write.rs') diff --git a/ebml/src/write.rs b/ebml/src/write.rs index 89268d5..2191693 100644 --- a/ebml/src/write.rs +++ b/ebml/src/write.rs @@ -23,20 +23,12 @@ impl EbmlWriter { } pub fn write_tag(&mut self, tag: &MatroskaTag) -> Result<()> { - self.write_tag_id(tag.id())?; let mut buf = vec![]; - tag.write(&mut buf)?; + tag.write_full(&mut buf)?; self.write(&buf)?; Ok(()) } - pub fn write_tag_id(&mut self, id: u64) -> Result<()> { - for n in id.to_be_bytes().iter().skip_while(|&v| *v == 0u8) { - self.write(&[*n])?; - } - Ok(()) - } - pub fn write_vint(&mut self, i: u64) -> Result<()> { if i > (1 << 56) - 1 { bail!("vint does not fit"); @@ -55,6 +47,17 @@ impl EbmlWriter { } } +impl MatroskaTag { + pub fn write_full(&self, w: &mut Vec) -> Result<()> { + let mut buf = vec![]; + buf.extend(self.id().to_be_bytes().iter().skip_while(|&v| *v == 0u8)); + // note: it is relevant here, to pass the buffer with the id, such that closing tags, can clear it + self.write(&mut buf)?; + w.extend_from_slice(&buf); + Ok(()) + } +} + pub trait WriteValue { fn write_to(&self, w: &mut Vec) -> Result<()>; } @@ -138,8 +141,17 @@ impl WriteValue for EbmlSize { impl WriteValue for Master { fn write_to(&self, w: &mut Vec) -> Result<()> { match self { - Master::Start(size) => size.write_to(w), - Master::End => Ok(()), + Master::Start => EbmlSize::Unknown.write_to(w), + Master::End => Ok(w.clear()), + Master::Collected(c) => { + let mut ib = vec![]; + for c in c { + c.write_full(&mut ib)?; + } + EbmlSize::Exact(ib.len()).write_to(w); + w.extend_from_slice(&ib); + Ok(()) + } } } } -- cgit v1.2.3-70-g09d2