forge
Explore
Log in

Don't you dare go Hollow

S11: ac3 one Syntax::of(data) for the bsid rule (bsid = byte 5 >> 3; <= 10 is AC-3) at the five call sites

Browse files at this commit

6789dd10aec01fcc2266c0945234fcd9dabbd854Ahmet Kaan GümüşParent: e478793
crates/ec-ac3/src/decode.rsmodified+9 -0
@@ -35,6 +35,15 @@ pub enum Syntax {
3535 Eac3,
3636 }
3737
38+impl Syntax {
39+ /// The syntax of the syncframe at `data`, read from bsid, which sits in
40+ /// the sixth byte of both. `None` when there are not six bytes yet.
41+ pub(crate) fn of(data: &[u8]) -> Option<Syntax> {
42+ let bsid = *data.get(5)? >> 3;
43+ Some(if bsid <= 10 { Syntax::Ac3 } else { Syntax::Eac3 })
44+ }
45+}
46+
3847 /// The decoder state a stream carries across blocks and frames.
3948 pub(crate) struct Core {
4049 pub(crate) syntax: Syntax,
crates/ec-ac3/src/decoder.rsmodified+3 -4
@@ -175,7 +175,7 @@ impl Ac3Decoder {
175175 let mut pos = first.consumed;
176176 while begins_syncframe(&data[pos..]) {
177177 let tail = &data[pos..];
178- if tail[5] >> 3 > 10 {
178+ if Syntax::of(tail) == Some(Syntax::Eac3) {
179179 let hdr = eac3::parse_from(&mut ec_core::BitReader::new(tail))?;
180180 if hdr.substreamid != 0 {
181181 return Err(Error::unsupported(
@@ -270,9 +270,8 @@ impl Ac3Decoder {
270270 // two for E-AC-3 — so read it where both agree it can be found: after
271271 // the sync word, E-AC-3 puts strmtyp/substreamid/frmsiz first, and
272272 // bsid is at a fixed offset from the frame start in both.
273- let bsid = data[5] >> 3;
274273 let mut r = ec_core::BitReader::new(data);
275- if bsid <= 10 {
274+ if Syntax::of(data) == Some(Syntax::Ac3) {
276275 let sync = syncinfo::parse_from(&mut r)?;
277276 let bsi = bsi::parse_from(&mut r)?;
278277 if data.len() < sync.frame_size {
@@ -543,7 +542,7 @@ fn begins_syncframe(tail: &[u8]) -> bool {
543542 if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD {
544543 return false;
545544 }
546- if tail[5] >> 3 > 10 {
545+ if Syntax::of(tail) == Some(Syntax::Eac3) {
547546 eac3::parse_from(&mut ec_core::BitReader::new(tail)).is_ok()
548547 } else {
549548 syncinfo::parse(tail).is_ok()
crates/ec-ac3/src/dependent.rsmodified+2 -1
@@ -29,6 +29,7 @@
2929 use ec_core::{BitReader, ChannelPosition, Error, Result};
3030
3131 use crate::bsi::Acmod;
32+use crate::decode::Syntax;
3233 use crate::eac3::{self, Eac3Bsi, StreamType};
3334 use crate::syncinfo::SYNCWORD;
3435
@@ -46,7 +47,7 @@ pub(crate) struct Channel {
4647 pub(crate) fn peek(data: &[u8]) -> Option<Eac3Bsi> {
4748 // Six bytes are needed for the sync word and bsid; AC-3 (bsid <= 10) has
4849 // no dependent substreams.
49- if data.len() < 6 || u16::from_be_bytes([data[0], data[1]]) != SYNCWORD || data[5] >> 3 <= 10 {
50+ if Syntax::of(data) != Some(Syntax::Eac3) || u16::from_be_bytes([data[0], data[1]]) != SYNCWORD {
5051 return None;
5152 }
5253 let hdr = eac3::parse_from(&mut BitReader::new(data)).ok()?;
crates/ec-ac3/src/lib.rsmodified+1 -1
@@ -100,7 +100,7 @@ pub fn frame_size(data: &[u8]) -> ec_core::Result<usize> {
100100 "AC-3: no syncword at the start of the frame",
101101 ));
102102 }
103- if data[5] >> 3 <= 10 {
103+ if decode::Syntax::of(data) == Some(decode::Syntax::Ac3) {
104104 Ok(syncinfo::parse(data)?.frame_size)
105105 } else {
106106 Ok(eac3::parse(&data[2..])?.frame_size)