From 757ee9164bd2ab7e1690ae2ea4e5a03f7b37f1b5 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Tue, 13 Apr 2021 11:32:28 +0200 Subject: [PATCH] flac: fix header parsing for variable block length case --- nihav-llaudio/src/demuxers/flac.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/nihav-llaudio/src/demuxers/flac.rs b/nihav-llaudio/src/demuxers/flac.rs index 0b917dd..b941bf4 100644 --- a/nihav-llaudio/src/demuxers/flac.rs +++ b/nihav-llaudio/src/demuxers/flac.rs @@ -78,9 +78,31 @@ impl<'a> FLACDemuxer<'a> { self.cur_samples = blkno * u64::from(self.blk_samples); (u64::from(self.blk_samples), blkno) } else { - validate!((buf[1] & 1) != 0); - let blksamps = u64::from(read_utf8(&buf[4..])?); - (blksamps, self.cur_samples) + let mut idx = 5; + while idx < buf.len() && (buf[idx] & 0x80) != 0 { + idx += 1; + } + + let bsz_id = buf[2] >> 4; + let blksamps = match bsz_id { + 0 => return Err(DemuxerError::InvalidData), + 1 => 192, + 2..=5 => 576 << (bsz_id - 2), + 6 => { + validate!(idx + 1 <= buf.len()); + u64::from(buf[idx]) + 1 + }, + 7 => { + validate!(idx + 2 <= buf.len()); + u64::from(buf[idx]) * 256 + u64::from(buf[idx + 1]) + 1 + }, + _ => 256 << (bsz_id - 8), + }; + let pts = u64::from(read_utf8(&buf[4..])?); + + validate!(idx < buf.len()); + + (blksamps, pts) }; let spos = if self.blk_samples != 0 { pts * u64::from(self.blk_samples) } else { pts }; -- 2.39.5