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 commitba413cfb1414acf9546a23ef8e876f4531d396ceParent: b822bed2 changed files +31 -8
crates/ec-matroska/src/demux.rsmodified+30 -7
| @@ -1008,9 +1008,9 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader | ||
| 1008 | 1008 | let flags = rest[2]; |
| 1009 | 1009 | let start = range.start + len + 3; |
| 1010 | 1010 | 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)?, | |
| 1014 | 1014 | }; |
| 1015 | 1015 | Ok(BlockHeader { |
| 1016 | 1016 | track, |
| @@ -1020,13 +1020,36 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader | ||
| 1020 | 1020 | }) |
| 1021 | 1021 | } |
| 1022 | 1022 | |
| 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 | + | |
| 1023 | 1041 | /// The frames of a laced block. Lacing packs several frames — always whole |
| 1024 | 1042 | /// ones, and only ones nothing else references — behind a header of sizes, in |
| 1025 | 1043 | /// one of the three shapes the spec defines. Video muxers write none, but a |
| 1026 | 1044 | /// streaming service's audio is laced by the thousand: every E-AC-3 block of a |
| 1027 | 1045 | /// WEB remux is fixed-laced, and a reader that refuses those is a film that |
| 1028 | 1046 | /// 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>>> { | |
| 1030 | 1053 | let head = buf |
| 1031 | 1054 | .get(start..end) |
| 1032 | 1055 | .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 | ||
| 1040 | 1063 | let mut sizes = Vec::with_capacity(count); |
| 1041 | 1064 | match lacing { |
| 1042 | 1065 | // Xiph: every size but the last as a run of 255s and a remainder. |
| 1043 | - 1 => { | |
| 1066 | + Lacing::Xiph => { | |
| 1044 | 1067 | for _ in 1..count { |
| 1045 | 1068 | let mut size = 0usize; |
| 1046 | 1069 | loop { |
| @@ -1055,7 +1078,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V | ||
| 1055 | 1078 | } |
| 1056 | 1079 | } |
| 1057 | 1080 | // Fixed: no sizes written at all, the frames divide the rest evenly. |
| 1058 | - 2 => { | |
| 1081 | + Lacing::Fixed => { | |
| 1059 | 1082 | let rest = head.len() - at; |
| 1060 | 1083 | if count == 0 || !rest.is_multiple_of(count) { |
| 1061 | 1084 | return Err(Error::corrupt( |
| @@ -1067,7 +1090,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V | ||
| 1067 | 1090 | // EBML: the first size outright, the rest as differences from the one |
| 1068 | 1091 | // before — signed vints, which is the same encoding with the middle of |
| 1069 | 1092 | // its range taken as zero. |
| 1070 | - _ => { | |
| 1093 | + Lacing::Ebml => { | |
| 1071 | 1094 | let mut size = 0i64; |
| 1072 | 1095 | for i in 1..count { |
| 1073 | 1096 | 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 { | ||
| 42 | 42 | /// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed, |
| 43 | 43 | /// 3 EBML). Returns the frame ranges. |
| 44 | 44 | 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)) | |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /// `buf` as a whole `SimpleBlock` or `Block` body: track number, timecode, flags and |