diff --git a/crates/ec-matroska/src/demux.rs b/crates/ec-matroska/src/demux.rs index 2f82b8af..0c336069 100644 --- a/crates/ec-matroska/src/demux.rs +++ b/crates/ec-matroska/src/demux.rs @@ -1008,9 +1008,9 @@ pub(crate) fn parse_block(buf: &[u8], range: Range) -> Result> 1) & 0x03 { - 0 => std::iter::once(start..end).collect(), - lacing => lace(buf, start, end, lacing)?, + let frames = match Lacing::of_flags((flags >> 1) & 0x03) { + None => std::iter::once(start..end).collect(), + Some(lacing) => lace(buf, start, end, lacing)?, }; Ok(BlockHeader { track, @@ -1020,13 +1020,36 @@ pub(crate) fn parse_block(buf: &[u8], range: Range) -> Result Option { + match bits { + 0 => None, + 1 => Some(Lacing::Xiph), + 2 => Some(Lacing::Fixed), + _ => Some(Lacing::Ebml), + } + } +} + /// The frames of a laced block. Lacing packs several frames — always whole /// ones, and only ones nothing else references — behind a header of sizes, in /// one of the three shapes the spec defines. Video muxers write none, but a /// streaming service's audio is laced by the thousand: every E-AC-3 block of a /// WEB remux is fixed-laced, and a reader that refuses those is a film that /// plays silent. -pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result>> { +pub(crate) fn lace( + buf: &[u8], + start: usize, + end: usize, + lacing: Lacing, +) -> Result>> { let head = buf .get(start..end) .ok_or_else(|| Error::corrupt("Matroska: lace header past its block"))?; @@ -1040,7 +1063,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result { + Lacing::Xiph => { for _ in 1..count { let mut size = 0usize; loop { @@ -1055,7 +1078,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result { + Lacing::Fixed => { let rest = head.len() - at; if count == 0 || !rest.is_multiple_of(count) { return Err(Error::corrupt( @@ -1067,7 +1090,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result { + Lacing::Ebml => { let mut size = 0i64; for i in 1..count { let (raw, len) = ebml::vint(head.get(at..).ok_or_else(short)?, true)?; diff --git a/crates/ec-matroska/src/fuzz_api.rs b/crates/ec-matroska/src/fuzz_api.rs index 855ee317..7cdb7b10 100644 --- a/crates/ec-matroska/src/fuzz_api.rs +++ b/crates/ec-matroska/src/fuzz_api.rs @@ -42,7 +42,7 @@ fn walk(buf: &[u8], depth: u32) -> usize { /// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed, /// 3 EBML). Returns the frame ranges. pub fn lace(buf: &[u8], lacing: u8) -> Result>> { - demux::lace(buf, 0, buf.len(), lacing) + demux::lace(buf, 0, buf.len(), demux::Lacing::of_flags(lacing).unwrap_or(demux::Lacing::Ebml)) } /// `buf` as a whole `SimpleBlock` or `Block` body: track number, timecode, flags and