diff options
author | metamuffin <metamuffin@disroot.org> | 2023-01-14 21:37:40 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-01-14 21:37:40 +0100 |
commit | 439dc00d1ce9ff3e5dc05f32a0426152fc5fab89 (patch) | |
tree | 7600a043a35456b7b2571f5806dcb7f838ef2487 /ebml | |
parent | 274fd00f24864736a9690bee5c3983b9716ab949 (diff) | |
download | jellything-439dc00d1ce9ff3e5dc05f32a0426152fc5fab89.tar jellything-439dc00d1ce9ff3e5dc05f32a0426152fc5fab89.tar.bz2 jellything-439dc00d1ce9ff3e5dc05f32a0426152fc5fab89.tar.zst |
reproduction *almost* perfect
Diffstat (limited to 'ebml')
-rw-r--r-- | ebml/src/bin/experiment.rs | 20 | ||||
-rw-r--r-- | ebml/src/lib.rs | 13 | ||||
-rw-r--r-- | ebml/src/read.rs | 6 | ||||
-rw-r--r-- | ebml/src/write.rs | 34 |
4 files changed, 41 insertions, 32 deletions
diff --git a/ebml/src/bin/experiment.rs b/ebml/src/bin/experiment.rs index c850753..d2f552a 100644 --- a/ebml/src/bin/experiment.rs +++ b/ebml/src/bin/experiment.rs @@ -18,14 +18,18 @@ fn main() -> anyhow::Result<()> { while let Some(tag) = r.next() { let tag = tag?; - w.write_tag(&tag)?; - // println!("{} {tag:?}", r.position) - // match tag { - // tag @ MatroskaTag::SeekHead(_) => { - // println!("{:?}", r.collect_master(tag)); - // } - // _ => (), - // } + // println!("{} {tag:?}", r.position); + match tag { + tag @ MatroskaTag::SeekHead(_) => { + eprintln!("{:?}", r.collect_master(tag)); + } + tag @ MatroskaTag::Cues(_) => { + eprintln!("{:?}", r.collect_master(tag)); + } + tag => { + w.write_tag(&tag)?; + } + } } Ok(()) } diff --git a/ebml/src/lib.rs b/ebml/src/lib.rs index a03ce0d..078995e 100644 --- a/ebml/src/lib.rs +++ b/ebml/src/lib.rs @@ -1,18 +1,15 @@ pub mod matroska; pub mod read; -pub mod write; pub mod size; +pub mod write; -use size::EbmlSize; - +use matroska::MatroskaTag; pub use read::ReadValue; pub use write::WriteValue; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub enum Master { - Start(EbmlSize), + Collected(Vec<MatroskaTag>), + Start, End, } - - - diff --git a/ebml/src/read.rs b/ebml/src/read.rs index 3853782..9e080be 100644 --- a/ebml/src/read.rs +++ b/ebml/src/read.rs @@ -23,10 +23,6 @@ pub struct EbmlReader { pub position: usize, } -pub trait EbmlRead: Sized { - fn read(r: &mut EbmlReader) -> Result<Self>; -} - impl EbmlReader { pub fn new<T: Seek + Read + 'static>(inner: T) -> Self { Self { @@ -96,7 +92,7 @@ impl EbmlReader { let size = self.read_tag_size()?; let is_master = MatroskaTag::is_master(id)?; let tag = if is_master { - MatroskaTag::construct_master(id, Master::Start(size))? + MatroskaTag::construct_master(id, Master::Start)? } else { let data = self.read_buf(size)?; MatroskaTag::parse(id, &data)? 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<u8>) -> 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<u8>) -> Result<()>; } @@ -138,8 +141,17 @@ impl WriteValue for EbmlSize { impl WriteValue for Master { fn write_to(&self, w: &mut Vec<u8>) -> 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(()) + } } } } |