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 {
35 Eac3,35 Eac3,
36 }36 }
37 37
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+
38 /// The decoder state a stream carries across blocks and frames.47 /// The decoder state a stream carries across blocks and frames.
39 pub(crate) struct Core {48 pub(crate) struct Core {
40 pub(crate) syntax: Syntax,49 pub(crate) syntax: Syntax,
crates/ec-ac3/src/decoder.rsmodified+3 -4
@@ -175,7 +175,7 @@ impl Ac3Decoder {
175 let mut pos = first.consumed;175 let mut pos = first.consumed;
176 while begins_syncframe(&data[pos..]) {176 while begins_syncframe(&data[pos..]) {
177 let tail = &data[pos..];177 let tail = &data[pos..];
178- if tail[5] >> 3 > 10 {178+ if Syntax::of(tail) == Some(Syntax::Eac3) {
179 let hdr = eac3::parse_from(&mut ec_core::BitReader::new(tail))?;179 let hdr = eac3::parse_from(&mut ec_core::BitReader::new(tail))?;
180 if hdr.substreamid != 0 {180 if hdr.substreamid != 0 {
181 return Err(Error::unsupported(181 return Err(Error::unsupported(
@@ -270,9 +270,8 @@ impl Ac3Decoder {
270 // two for E-AC-3 — so read it where both agree it can be found: after270 // two for E-AC-3 — so read it where both agree it can be found: after
271 // the sync word, E-AC-3 puts strmtyp/substreamid/frmsiz first, and271 // the sync word, E-AC-3 puts strmtyp/substreamid/frmsiz first, and
272 // bsid is at a fixed offset from the frame start in both.272 // bsid is at a fixed offset from the frame start in both.
273- let bsid = data[5] >> 3;
274 let mut r = ec_core::BitReader::new(data);273 let mut r = ec_core::BitReader::new(data);
275- if bsid <= 10 {274+ if Syntax::of(data) == Some(Syntax::Ac3) {
276 let sync = syncinfo::parse_from(&mut r)?;275 let sync = syncinfo::parse_from(&mut r)?;
277 let bsi = bsi::parse_from(&mut r)?;276 let bsi = bsi::parse_from(&mut r)?;
278 if data.len() < sync.frame_size {277 if data.len() < sync.frame_size {
@@ -543,7 +542,7 @@ fn begins_syncframe(tail: &[u8]) -> bool {
543 if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD {542 if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD {
544 return false;543 return false;
545 }544 }
546- if tail[5] >> 3 > 10 {545+ if Syntax::of(tail) == Some(Syntax::Eac3) {
547 eac3::parse_from(&mut ec_core::BitReader::new(tail)).is_ok()546 eac3::parse_from(&mut ec_core::BitReader::new(tail)).is_ok()
548 } else {547 } else {
549 syncinfo::parse(tail).is_ok()548 syncinfo::parse(tail).is_ok()
crates/ec-ac3/src/dependent.rsmodified+2 -1
@@ -29,6 +29,7 @@
29 use ec_core::{BitReader, ChannelPosition, Error, Result};29 use ec_core::{BitReader, ChannelPosition, Error, Result};
30 30
31 use crate::bsi::Acmod;31 use crate::bsi::Acmod;
32+use crate::decode::Syntax;
32 use crate::eac3::{self, Eac3Bsi, StreamType};33 use crate::eac3::{self, Eac3Bsi, StreamType};
33 use crate::syncinfo::SYNCWORD;34 use crate::syncinfo::SYNCWORD;
34 35
@@ -46,7 +47,7 @@ pub(crate) struct Channel {
46 pub(crate) fn peek(data: &[u8]) -> Option<Eac3Bsi> {47 pub(crate) fn peek(data: &[u8]) -> Option<Eac3Bsi> {
47 // Six bytes are needed for the sync word and bsid; AC-3 (bsid <= 10) has48 // Six bytes are needed for the sync word and bsid; AC-3 (bsid <= 10) has
48 // no dependent substreams.49 // 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 {
50 return None;51 return None;
51 }52 }
52 let hdr = eac3::parse_from(&mut BitReader::new(data)).ok()?;53 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> {
100 "AC-3: no syncword at the start of the frame",100 "AC-3: no syncword at the start of the frame",
101 ));101 ));
102 }102 }
103- if data[5] >> 3 <= 10 {103+ if decode::Syntax::of(data) == Some(decode::Syntax::Ac3) {
104 Ok(syncinfo::parse(data)?.frame_size)104 Ok(syncinfo::parse(data)?.frame_size)
105 } else {105 } else {
106 Ok(eac3::parse(&data[2..])?.frame_size)106 Ok(eac3::parse(&data[2..])?.frame_size)