forge
Explore
Log in

Don't you dare go Hollow

S4: matroska lacing is a Lacing enum (Xiph, Fixed, Ebml) instead of a raw 0-3 code; fuzz_api keeps its u8 signature and maps any other value to Ebml as before

Browse files at this commit

ba413cfb1414acf9546a23ef8e876f4531d396ceAhmet Kaan GümüşParent: b822bed
crates/ec-matroska/src/demux.rsmodified+30 -7
@@ -1008,9 +1008,9 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader
10081008 let flags = rest[2];
10091009 let start = range.start + len + 3;
10101010 let end = range.end;
1011- let frames = match (flags >> 1) & 0x03 {
1012- 0 => std::iter::once(start..end).collect(),
1013- lacing => lace(buf, start, end, lacing)?,
1011+ let frames = match Lacing::of_flags((flags >> 1) & 0x03) {
1012+ None => std::iter::once(start..end).collect(),
1013+ Some(lacing) => lace(buf, start, end, lacing)?,
10141014 };
10151015 Ok(BlockHeader {
10161016 track,
@@ -1020,13 +1020,36 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader
10201020 })
10211021 }
10221022
1023+#[derive(Clone, Copy)]
1024+pub(crate) enum Lacing {
1025+ Xiph,
1026+ Fixed,
1027+ Ebml,
1028+}
1029+
1030+impl Lacing {
1031+ pub(crate) fn of_flags(bits: u8) -> Option<Lacing> {
1032+ match bits {
1033+ 0 => None,
1034+ 1 => Some(Lacing::Xiph),
1035+ 2 => Some(Lacing::Fixed),
1036+ _ => Some(Lacing::Ebml),
1037+ }
1038+ }
1039+}
1040+
10231041 /// The frames of a laced block. Lacing packs several frames — always whole
10241042 /// ones, and only ones nothing else references — behind a header of sizes, in
10251043 /// one of the three shapes the spec defines. Video muxers write none, but a
10261044 /// streaming service's audio is laced by the thousand: every E-AC-3 block of a
10271045 /// WEB remux is fixed-laced, and a reader that refuses those is a film that
10281046 /// plays silent.
1029-pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<Vec<Range<usize>>> {
1047+pub(crate) fn lace(
1048+ buf: &[u8],
1049+ start: usize,
1050+ end: usize,
1051+ lacing: Lacing,
1052+) -> Result<Vec<Range<usize>>> {
10301053 let head = buf
10311054 .get(start..end)
10321055 .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<V
10401063 let mut sizes = Vec::with_capacity(count);
10411064 match lacing {
10421065 // Xiph: every size but the last as a run of 255s and a remainder.
1043- 1 => {
1066+ Lacing::Xiph => {
10441067 for _ in 1..count {
10451068 let mut size = 0usize;
10461069 loop {
@@ -1055,7 +1078,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V
10551078 }
10561079 }
10571080 // Fixed: no sizes written at all, the frames divide the rest evenly.
1058- 2 => {
1081+ Lacing::Fixed => {
10591082 let rest = head.len() - at;
10601083 if count == 0 || !rest.is_multiple_of(count) {
10611084 return Err(Error::corrupt(
@@ -1067,7 +1090,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V
10671090 // EBML: the first size outright, the rest as differences from the one
10681091 // before — signed vints, which is the same encoding with the middle of
10691092 // its range taken as zero.
1070- _ => {
1093+ Lacing::Ebml => {
10711094 let mut size = 0i64;
10721095 for i in 1..count {
10731096 let (raw, len) = ebml::vint(head.get(at..).ok_or_else(short)?, true)?;
crates/ec-matroska/src/fuzz_api.rsmodified+1 -1
@@ -42,7 +42,7 @@ fn walk(buf: &[u8], depth: u32) -> usize {
4242 /// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed,
4343 /// 3 EBML). Returns the frame ranges.
4444 pub fn lace(buf: &[u8], lacing: u8) -> Result<Vec<Range<usize>>> {
45- demux::lace(buf, 0, buf.len(), lacing)
45+ demux::lace(buf, 0, buf.len(), demux::Lacing::of_flags(lacing).unwrap_or(demux::Lacing::Ebml))
4646 }
4747
4848 /// `buf` as a whole `SimpleBlock` or `Block` body: track number, timecode, flags and