B1: ec-probe input bugs (AAC AVI sample count, Xing underflow, Ogg comment offset, window/underflow saturation, FLAC block scan bound and length clamp, WAV A/mu-law block_align 0, duration sample_rate 0 panic, TRCK first-wins)
Browse files at this commitede1525ff4954ad522fabe32ac79dea433b5486bParent: 22f0f3f4 changed files +33 -24
- crates/ec-probe/src/decoder.rs +2 -1
- crates/ec-probe/src/lib.rs +21 -19
- crates/ec-probe/src/raw.rs +8 -3
- crates/ec-probe/src/tags.rs +2 -1
crates/ec-probe/src/decoder.rsmodified+2 -1
| @@ -353,7 +353,8 @@ fn flac_stream_info(data: &[u8]) -> Option<&[u8]> { | |||
| 353 | if data.len() == 34 { | 353 | if data.len() == 34 { |
| 354 | return Some(data); | 354 | return Some(data); |
| 355 | } | 355 | } |
| 356 | - for at in 0..data.len().saturating_sub(38).min(16) { | 356 | + let last = data.len().checked_sub(38)?; |
| 357 | + for at in 0..=last.min(16) { | ||
| 357 | let head = &data[at..at + 4]; | 358 | let head = &data[at..at + 4]; |
| 358 | let len = u32::from_be_bytes([0, head[1], head[2], head[3]]); | 359 | let len = u32::from_be_bytes([0, head[1], head[2], head[3]]); |
| 359 | if head[0] & 0x7f == 0 && len == 34 { | 360 | if head[0] & 0x7f == 0 && len == 34 { |
crates/ec-probe/src/lib.rsmodified+21 -19
| @@ -315,7 +315,12 @@ impl Reader { | |||
| 315 | .find(|s| s.params.codec == CodecId::Vorbis) | 315 | .find(|s| s.params.codec == CodecId::Vorbis) |
| 316 | .and_then(|s| s.params.extradata.as_deref()) | 316 | .and_then(|s| s.params.extradata.as_deref()) |
| 317 | .and_then(|data| ec_ogg::xiph_unlace(data).ok()) | 317 | .and_then(|data| ec_ogg::xiph_unlace(data).ok()) |
| 318 | - .and_then(|packets| packets.get(1).map(|p| tags::from_vorbis_comment(&p[7..]))) | 318 | + .and_then(|packets| { |
| 319 | + packets | ||
| 320 | + .get(1) | ||
| 321 | + .and_then(|p| p.get(7..)) | ||
| 322 | + .map(tags::from_vorbis_comment) | ||
| 323 | + }) | ||
| 319 | .unwrap_or_default(); | 324 | .unwrap_or_default(); |
| 320 | let native = (0..demuxer.streams().len() as u64).collect(); | 325 | let native = (0..demuxer.streams().len() as u64).collect(); |
| 321 | Ok(Reader::wrap(format, Box::new(demuxer), tags, native)) | 326 | Ok(Reader::wrap(format, Box::new(demuxer), tags, native)) |
| @@ -407,7 +412,7 @@ impl Reader { | |||
| 407 | let padding_ticks = s | 412 | let padding_ticks = s |
| 408 | .params | 413 | .params |
| 409 | .audio() | 414 | .audio() |
| 410 | - .filter(|_| s.initial_padding > 0) | 415 | + .filter(|a| a.sample_rate > 0 && s.initial_padding > 0) |
| 411 | .map(|a| { | 416 | .map(|a| { |
| 412 | TimeBase::from_rate(a.sample_rate).rescale( | 417 | TimeBase::from_rate(a.sample_rate).rescale( |
| 413 | i64::from(s.initial_padding), | 418 | i64::from(s.initial_padding), |
| @@ -416,14 +421,10 @@ impl Reader { | |||
| 416 | ) | 421 | ) |
| 417 | }) | 422 | }) |
| 418 | .unwrap_or(0); | 423 | .unwrap_or(0); |
| 419 | - Timestamp::new(d - padding_ticks, s.time_base) | 424 | + Timestamp::new(d.saturating_sub(padding_ticks), s.time_base) |
| 420 | - }) | ||
| 421 | }) | 425 | }) |
| 422 | - .max_by(|a, b| { | ||
| 423 | - a.as_secs_f64() | ||
| 424 | - .partial_cmp(&b.as_secs_f64()) | ||
| 425 | - .unwrap_or(std::cmp::Ordering::Equal) | ||
| 426 | }) | 426 | }) |
| 427 | + .max() | ||
| 427 | } | 428 | } |
| 428 | 429 | ||
| 429 | /// Every audio stream with no decoder behind it, and why. | 430 | /// Every audio stream with no decoder behind it, and why. |
| @@ -559,7 +560,7 @@ fn open_dts_span<R: Read + Seek + Send + 'static>( | |||
| 559 | start: u64, | 560 | start: u64, |
| 560 | end: u64, | 561 | end: u64, |
| 561 | ) -> Result<Reader> { | 562 | ) -> Result<Reader> { |
| 562 | - let mut window = vec![0u8; HEAD.min((end - start) as usize)]; | 563 | + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; |
| 563 | src.seek(SeekFrom::Start(start))?; | 564 | src.seek(SeekFrom::Start(start))?; |
| 564 | let got = read_upto(&mut src, &mut window)?; | 565 | let got = read_upto(&mut src, &mut window)?; |
| 565 | window.truncate(got); | 566 | window.truncate(got); |
| @@ -655,7 +656,8 @@ fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) | |||
| 655 | } | 656 | } |
| 656 | let last = header[0] & 0x80 != 0; | 657 | let last = header[0] & 0x80 != 0; |
| 657 | let kind = header[0] & 0x7f; | 658 | let kind = header[0] & 0x7f; |
| 658 | - let len = u64::from(u32::from_be_bytes([0, header[1], header[2], header[3]])).min(1 << 24); | 659 | + let len = u64::from(u32::from_be_bytes([0, header[1], header[2], header[3]])) |
| 660 | + .min(end.saturating_sub(at + 4)); | ||
| 659 | let mut body = vec![0u8; len as usize]; | 661 | let mut body = vec![0u8; len as usize]; |
| 660 | read_upto(&mut src, &mut body)?; | 662 | read_upto(&mut src, &mut body)?; |
| 661 | at += 4 + len; | 663 | at += 4 + len; |
| @@ -680,7 +682,7 @@ fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) | |||
| 680 | 682 | ||
| 681 | fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { | 683 | fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { |
| 682 | let (start, tags) = id3(&mut src, head, end)?; | 684 | let (start, tags) = id3(&mut src, head, end)?; |
| 683 | - let mut window = vec![0u8; HEAD.min((end - start) as usize)]; | 685 | + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; |
| 684 | src.seek(SeekFrom::Start(start))?; | 686 | src.seek(SeekFrom::Start(start))?; |
| 685 | let got = read_upto(&mut src, &mut window)?; | 687 | let got = read_upto(&mut src, &mut window)?; |
| 686 | window.truncate(got); | 688 | window.truncate(got); |
| @@ -688,7 +690,7 @@ fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) | |||
| 688 | .ok_or_else(|| Error::corrupt("mp3: no frame in the first 64 KiB"))?; | 690 | .ok_or_else(|| Error::corrupt("mp3: no frame in the first 64 KiB"))?; |
| 689 | let header = ec_mp3::FrameHeader::parse(&window[at..])?; | 691 | let header = ec_mp3::FrameHeader::parse(&window[at..])?; |
| 690 | let mut start = start + at as u64; | 692 | let mut start = start + at as u64; |
| 691 | - let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?); | 693 | + let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?).max(start); |
| 692 | // A Xing/Info header states the frame count exactly; without one the | 694 | // A Xing/Info header states the frame count exactly; without one the |
| 693 | // duration is the bitrate estimate the oracle also falls back to, which is | 695 | // duration is the bitrate estimate the oracle also falls back to, which is |
| 694 | // right for CBR and approximate for VBR. | 696 | // right for CBR and approximate for VBR. |
| @@ -708,7 +710,10 @@ fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) | |||
| 708 | let (padding, total) = match &xing { | 710 | let (padding, total) = match &xing { |
| 709 | Some(x) => ( | 711 | Some(x) => ( |
| 710 | x.delay + MP3_DECODER_DELAY, | 712 | x.delay + MP3_DECODER_DELAY, |
| 711 | - Some(x.frames * per_frame - u64::from(x.padding.saturating_sub(MP3_DECODER_DELAY))), | 713 | + Some( |
| 714 | + (x.frames * per_frame) | ||
| 715 | + .saturating_sub(u64::from(x.padding.saturating_sub(MP3_DECODER_DELAY))), | ||
| 716 | + ), | ||
| 712 | ), | 717 | ), |
| 713 | None => ( | 718 | None => ( |
| 714 | 0, | 719 | 0, |
| @@ -732,7 +737,7 @@ fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) | |||
| 732 | 737 | ||
| 733 | fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { | 738 | fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> { |
| 734 | let (start, tags) = id3(&mut src, head, end)?; | 739 | let (start, tags) = id3(&mut src, head, end)?; |
| 735 | - let mut window = vec![0u8; HEAD.min((end - start) as usize)]; | 740 | + let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)]; |
| 736 | src.seek(SeekFrom::Start(start))?; | 741 | src.seek(SeekFrom::Start(start))?; |
| 737 | let got = read_upto(&mut src, &mut window)?; | 742 | let got = read_upto(&mut src, &mut window)?; |
| 738 | window.truncate(got); | 743 | window.truncate(got); |
| @@ -740,7 +745,7 @@ fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) | |||
| 740 | .ok_or_else(|| Error::corrupt("aac: no ADTS frame in the first 64 KiB"))?; | 745 | .ok_or_else(|| Error::corrupt("aac: no ADTS frame in the first 64 KiB"))?; |
| 741 | let header = ec_aac::parse_adts(&window[at..])?; | 746 | let header = ec_aac::parse_adts(&window[at..])?; |
| 742 | let start = start + at as u64; | 747 | let start = start + at as u64; |
| 743 | - let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?); | 748 | + let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?).max(start); |
| 744 | // ADTS states no duration anywhere: every frame is 1024 samples, so the | 749 | // ADTS states no duration anywhere: every frame is 1024 samples, so the |
| 745 | // file's size over its *average* frame is the estimate — the same one | 750 | // file's size over its *average* frame is the estimate — the same one |
| 746 | // the oracle prints "estimating duration from bitrate" about. Averaged rather | 751 | // the oracle prints "estimating duration from bitrate" about. Averaged rather |
| @@ -1075,10 +1080,7 @@ fn avi_elementary_frame(codec: CodecId, data: &[u8]) -> Result<Option<(usize, u6 | |||
| 1075 | return Ok(None); | 1080 | return Ok(None); |
| 1076 | } | 1081 | } |
| 1077 | let header = ec_aac::parse_adts(data)?; | 1082 | let header = ec_aac::parse_adts(data)?; |
| 1078 | - Ok(Some(( | 1083 | + Ok(Some((header.frame_length, raw::adts_samples(&header)))) |
| 1079 | - header.frame_length, | ||
| 1080 | - (u64::from(header.raw_blocks) + 1) * ec_aac::FRAME_LEN as u64, | ||
| 1081 | - ))) | ||
| 1082 | } | 1084 | } |
| 1083 | _ => Ok(None), | 1085 | _ => Ok(None), |
| 1084 | } | 1086 | } |
crates/ec-probe/src/raw.rsmodified+8 -3
| @@ -293,7 +293,7 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu | |||
| 293 | } | 293 | } |
| 294 | Ok(Framing { | 294 | Ok(Framing { |
| 295 | bytes: header.frame_length, | 295 | bytes: header.frame_length, |
| 296 | - samples: 1024 * u64::from(header.raw_blocks), | 296 | + samples: adts_samples(&header), |
| 297 | }) | 297 | }) |
| 298 | } | 298 | } |
| 299 | Kind::Dts => { | 299 | Kind::Dts => { |
| @@ -362,7 +362,7 @@ impl<R: Read + Seek + Send> Demuxer for RawDemuxer<R> { | |||
| 362 | // PCM needs no index and no rounding: every sample is a random access | 362 | // PCM needs no index and no rounding: every sample is a random access |
| 363 | // point, at a byte offset arithmetic gives. | 363 | // point, at a byte offset arithmetic gives. |
| 364 | if let Kind::Wav { block_align } = self.kind { | 364 | if let Kind::Wav { block_align } = self.kind { |
| 365 | - let at = self.start + target * block_align as u64; | 365 | + let at = self.start + target.saturating_mul(block_align as u64); |
| 366 | self.jump(at.min(self.end))?; | 366 | self.jump(at.min(self.end))?; |
| 367 | self.sample = target; | 367 | self.sample = target; |
| 368 | return Ok(()); | 368 | return Ok(()); |
| @@ -413,7 +413,7 @@ pub(crate) fn wav_compressed_parameters( | |||
| 413 | } | 413 | } |
| 414 | let kind = match c.tag { | 414 | let kind = match c.tag { |
| 415 | 0x0006 | 0x0007 => Kind::Wav { | 415 | 0x0006 | 0x0007 => Kind::Wav { |
| 416 | - block_align: usize::from(spec.channels), | 416 | + block_align: usize::from(spec.channels.max(1)), |
| 417 | }, | 417 | }, |
| 418 | _ => Kind::WavBlocks { | 418 | _ => Kind::WavBlocks { |
| 419 | block: usize::from(c.block_align), | 419 | block: usize::from(c.block_align), |
| @@ -451,6 +451,11 @@ pub(crate) fn wav_parameters(spec: ec_riff::WavSpec) -> Result<(CodecParameters, | |||
| 451 | Ok((params, spec.block_align())) | 451 | Ok((params, spec.block_align())) |
| 452 | } | 452 | } |
| 453 | 453 | ||
| 454 | +/// Samples one ADTS frame decodes to: `raw_blocks` already counts the blocks. | ||
| 455 | +pub(crate) fn adts_samples(header: &ec_aac::AdtsHeader) -> u64 { | ||
| 456 | + u64::from(header.raw_blocks) * ec_aac::FRAME_LEN as u64 | ||
| 457 | +} | ||
| 458 | + | ||
| 454 | /// Codec parameters for an ADTS stream, from its first frame header. | 459 | /// Codec parameters for an ADTS stream, from its first frame header. |
| 455 | pub(crate) fn adts_parameters(header: &ec_aac::AdtsHeader) -> CodecParameters { | 460 | pub(crate) fn adts_parameters(header: &ec_aac::AdtsHeader) -> CodecParameters { |
| 456 | let mut params = CodecParameters::new(CodecId::Aac); | 461 | let mut params = CodecParameters::new(CodecId::Aac); |
crates/ec-probe/src/tags.rsmodified+2 -1
| @@ -50,10 +50,11 @@ impl Tags { | |||
| 50 | "TRCK" | "TRK" | "TRACKNUMBER" => { | 50 | "TRCK" | "TRK" | "TRACKNUMBER" => { |
| 51 | // "3/12" is a track number and a total; the number is the part | 51 | // "3/12" is a track number and a total; the number is the part |
| 52 | // anything displays. | 52 | // anything displays. |
| 53 | - self.track = value | 53 | + let track = value |
| 54 | .split(['/', ' ']) | 54 | .split(['/', ' ']) |
| 55 | .next() | 55 | .next() |
| 56 | .and_then(|n| n.trim().parse().ok()); | 56 | .and_then(|n| n.trim().parse().ok()); |
| 57 | + self.track = self.track.or(track); | ||
| 57 | return; | 58 | return; |
| 58 | } | 59 | } |
| 59 | _ => return, | 60 | _ => return, |