Classify EOF truncation as incomplete
Browse files at this commitfe4764e251e9db18a6e66a17e51960a3314e4126Parent: 680a94a- fix
- classify EOF truncation as incomplete
4 changed files +114 -21
- crates/ec-matroska/tests/matroska.rs +38 -1
- crates/ec-mp4/src/boxes.rs +10 -7
- crates/ec-mp4/src/demux.rs +18 -13
- crates/ec-mp4/tests/mp4.rs +48 -0
crates/ec-matroska/tests/matroska.rsmodified+38 -1
| @@ -4,7 +4,7 @@ | |||
| 4 | //! was opened with. | 4 | //! was opened with. |
| 5 | 5 | ||
| 6 | use std::fs::File; | 6 | use std::fs::File; |
| 7 | -use std::io::BufReader; | 7 | +use std::io::{BufReader, Cursor}; |
| 8 | use std::path::{Path, PathBuf}; | 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::Command; | 9 | use std::process::Command; |
| 10 | 10 | ||
| @@ -786,6 +786,43 @@ fn the_first_cluster_is_reachable_when_no_cue_names_it() { | |||
| 786 | } | 786 | } |
| 787 | } | 787 | } |
| 788 | 788 | ||
| 789 | +#[test] | ||
| 790 | +fn truncated_prefixes_are_eof_not_corrupt() { | ||
| 791 | + let path = fixtures().join("audio/av-h264-aac-stereo-48000.mkv"); | ||
| 792 | + let data = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); | ||
| 793 | + let mut table = Vec::new(); | ||
| 794 | + for i in 1..=64usize { | ||
| 795 | + let cut = data.len() * i / 64; | ||
| 796 | + let (class, packets) = matroska_prefix_class(&data[..cut]); | ||
| 797 | + table.push(format!("{i:02},{cut},{class},{packets}")); | ||
| 798 | + assert!( | ||
| 799 | + matches!(class, "NeedMore" | "Open+Eof"), | ||
| 800 | + "{}\nidx,cut,class,packets\n{}", | ||
| 801 | + path.display(), | ||
| 802 | + table.join("\n") | ||
| 803 | + ); | ||
| 804 | + } | ||
| 805 | + println!("{}\nidx,cut,class,packets\n{}", path.display(), table.join("\n")); | ||
| 806 | +} | ||
| 807 | + | ||
| 808 | +fn matroska_prefix_class(data: &[u8]) -> (&'static str, u64) { | ||
| 809 | + let mut demuxer = match MatroskaDemuxer::new(Cursor::new(data.to_vec())) { | ||
| 810 | + Ok(demuxer) => demuxer, | ||
| 811 | + Err(Error::NeedMore) => return ("NeedMore", 0), | ||
| 812 | + Err(Error::Corrupt { .. }) => return ("Corrupt", 0), | ||
| 813 | + Err(e) => panic!("unexpected Matroska prefix error: {e}"), | ||
| 814 | + }; | ||
| 815 | + let mut packets = 0; | ||
| 816 | + loop { | ||
| 817 | + match demuxer.next_packet() { | ||
| 818 | + Ok(_) => packets += 1, | ||
| 819 | + Err(Error::Eof) => return ("Open+Eof", packets), | ||
| 820 | + Err(Error::NeedMore | Error::Corrupt { .. }) => return ("Corrupt", packets), | ||
| 821 | + Err(e) => panic!("unexpected Matroska packet error: {e}"), | ||
| 822 | + } | ||
| 823 | + } | ||
| 824 | +} | ||
| 825 | + | ||
| 789 | 826 | ||
| 790 | /// Where our own demuxer's seek lands on a real 5.1 E-AC-3 Matroska, in | 827 | /// Where our own demuxer's seek lands on a real 5.1 E-AC-3 Matroska, in |
| 791 | /// *timestamp* -- split from decode, per the round's charter: this measures | 828 | /// *timestamp* -- split from decode, per the round's charter: this measures |
crates/ec-mp4/src/boxes.rsmodified+10 -7
| @@ -179,9 +179,10 @@ impl<R: Read + Seek> Src<R> { | |||
| 179 | Ok(out) | 179 | Ok(out) |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | - /// The header of the box at `at`: its type, where its body starts and where | 182 | + /// The header of the box at `at`: its type, where its body starts, where |
| 183 | - /// it ends. `None` once `at` reaches `end`. | 183 | + /// the readable bytes end, and whether the stated size fits inside `end`. |
| 184 | - pub fn header_at(&mut self, at: u64, end: u64) -> Result<Option<(FourCc, u64, u64)>> { | 184 | + /// `None` once `at` reaches `end` or a final header is cut off by EOF. |
| 185 | + pub fn header_at(&mut self, at: u64, end: u64) -> Result<Option<(FourCc, u64, u64, bool)>> { | ||
| 185 | if at.saturating_add(8) > end { | 186 | if at.saturating_add(8) > end { |
| 186 | return Ok(None); | 187 | return Ok(None); |
| 187 | } | 188 | } |
| @@ -196,7 +197,7 @@ impl<R: Read + Seek> Src<R> { | |||
| 196 | let mut head_len = 8u64; | 197 | let mut head_len = 8u64; |
| 197 | if size == 1 { | 198 | if size == 1 { |
| 198 | if got < 16 { | 199 | if got < 16 { |
| 199 | - return Err(Error::corrupt("mp4: 64-bit box size past the end of file")); | 200 | + return Ok(None); |
| 200 | } | 201 | } |
| 201 | size = u64::from_be_bytes(head[8..16].try_into().unwrap()); | 202 | size = u64::from_be_bytes(head[8..16].try_into().unwrap()); |
| 202 | head_len = 16; | 203 | head_len = 16; |
| @@ -209,9 +210,11 @@ impl<R: Read + Seek> Src<R> { | |||
| 209 | "mp4: box '{name}' is {size} bytes, shorter than its own header" | 210 | "mp4: box '{name}' is {size} bytes, shorter than its own header" |
| 210 | ))); | 211 | ))); |
| 211 | } | 212 | } |
| 212 | - let body = at + head_len; | 213 | + let body = at.saturating_add(head_len); |
| 213 | - let stop = at.saturating_add(size).min(end); | 214 | + let full_stop = at.saturating_add(size); |
| 214 | - Ok(Some((kind, body, stop))) | 215 | + let complete = full_stop <= end; |
| 216 | + let stop = full_stop.min(end); | ||
| 217 | + Ok(Some((kind, body, stop, complete))) | ||
| 215 | } | 218 | } |
| 216 | } | 219 | } |
| 217 | 220 | ||
crates/ec-mp4/src/demux.rsmodified+18 -13
| @@ -138,10 +138,10 @@ impl<R: Read + Seek> Mp4Demuxer<R> { | |||
| 138 | // The top level, box by box: the header of each is 8 bytes and the rest | 138 | // The top level, box by box: the header of each is 8 bytes and the rest |
| 139 | // is skipped by seeking, so opening a 12 GB film costs a handful of | 139 | // is skipped by seeking, so opening a 12 GB film costs a handful of |
| 140 | // reads. | 140 | // reads. |
| 141 | - while let Some((kind, body, stop)) = src.header_at(at, end)? { | 141 | + while let Some((kind, body, stop, complete)) = src.header_at(at, end)? { |
| 142 | match &kind { | 142 | match &kind { |
| 143 | - b"moov" => moov = Some((body, stop)), | 143 | + b"moov" if complete => moov = Some((body, stop)), |
| 144 | - b"moof" => moofs.push((at, body, stop)), | 144 | + b"moof" if complete => moofs.push((at, body, stop)), |
| 145 | _ => {} | 145 | _ => {} |
| 146 | } | 146 | } |
| 147 | if stop <= at { | 147 | if stop <= at { |
| @@ -149,12 +149,7 @@ impl<R: Read + Seek> Mp4Demuxer<R> { | |||
| 149 | } | 149 | } |
| 150 | at = stop; | 150 | at = stop; |
| 151 | } | 151 | } |
| 152 | - let (body, stop) = moov.ok_or_else(|| match moofs.is_empty() { | 152 | + let (body, stop) = moov.ok_or(Error::NeedMore)?; |
| 153 | - // A file that ends before its trailing moov is a truncated | ||
| 154 | - // download, not a broken container. | ||
| 155 | - true => Error::NeedMore, | ||
| 156 | - false => Error::corrupt("mp4: fragments with no moov to describe them"), | ||
| 157 | - })?; | ||
| 158 | let data = src.read_vec(body, stop.saturating_sub(body), MOOV_LIMIT)?; | 153 | let data = src.read_vec(body, stop.saturating_sub(body), MOOV_LIMIT)?; |
| 159 | let mut me = Mp4Demuxer { | 154 | let mut me = Mp4Demuxer { |
| 160 | src, | 155 | src, |
| @@ -268,9 +263,14 @@ impl<R: Read + Seek> Mp4Demuxer<R> { | |||
| 268 | return Err(Error::Eof); | 263 | return Err(Error::Eof); |
| 269 | }; | 264 | }; |
| 270 | let time_base = trak.time_base; | 265 | let time_base = trak.time_base; |
| 271 | - let data = self | 266 | + let data = match self |
| 272 | .src | 267 | .src |
| 273 | - .read_vec(sample.offset, u64::from(sample.size), SAMPLE_LIMIT)?; | 268 | + .read_vec(sample.offset, u64::from(sample.size), SAMPLE_LIMIT) |
| 269 | + { | ||
| 270 | + Ok(data) => data, | ||
| 271 | + Err(e) if e.is_need_more() => return Err(Error::Eof), | ||
| 272 | + Err(e) => return Err(e), | ||
| 273 | + }; | ||
| 274 | Ok(packet_of(stream, time_base, &sample, data)) | 274 | Ok(packet_of(stream, time_base, &sample, data)) |
| 275 | } | 275 | } |
| 276 | 276 | ||
| @@ -645,9 +645,14 @@ impl<R: Read + Seek + Send> Demuxer for Mp4Demuxer<R> { | |||
| 645 | trak.cursor += 1; | 645 | trak.cursor += 1; |
| 646 | let stream = trak.stream.unwrap_or(0); | 646 | let stream = trak.stream.unwrap_or(0); |
| 647 | let time_base = trak.time_base; | 647 | let time_base = trak.time_base; |
| 648 | - let data = self | 648 | + let data = match self |
| 649 | .src | 649 | .src |
| 650 | - .read_vec(sample.offset, u64::from(sample.size), SAMPLE_LIMIT)?; | 650 | + .read_vec(sample.offset, u64::from(sample.size), SAMPLE_LIMIT) |
| 651 | + { | ||
| 652 | + Ok(data) => data, | ||
| 653 | + Err(e) if e.is_need_more() => return Err(Error::Eof), | ||
| 654 | + Err(e) => return Err(e), | ||
| 655 | + }; | ||
| 651 | Ok(packet_of(stream, time_base, &sample, data)) | 656 | Ok(packet_of(stream, time_base, &sample, data)) |
| 652 | } | 657 | } |
| 653 | 658 | ||
crates/ec-mp4/tests/mp4.rsmodified+48 -0
| @@ -621,6 +621,54 @@ fn a_truncated_file_is_need_more() { | |||
| 621 | assert!(Mp4Demuxer::new(Cursor::new(b"not an mp4 at all".to_vec())).is_err()); | 621 | assert!(Mp4Demuxer::new(Cursor::new(b"not an mp4 at all".to_vec())).is_err()); |
| 622 | } | 622 | } |
| 623 | 623 | ||
| 624 | +#[test] | ||
| 625 | +fn truncated_prefixes_are_need_more_or_eof() { | ||
| 626 | + let mut paths = vec![fixtures().join("audio/aac-mp4-stereo-48000.mp4")]; | ||
| 627 | + let real = PathBuf::from("/home/user/Downloads/reelj (11).mp4"); | ||
| 628 | + if real.exists() { | ||
| 629 | + paths.push(real); | ||
| 630 | + } | ||
| 631 | + | ||
| 632 | + for path in paths { | ||
| 633 | + let data = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); | ||
| 634 | + let mut table = Vec::new(); | ||
| 635 | + for i in 1..=64usize { | ||
| 636 | + let cut = data.len() * i / 64; | ||
| 637 | + let (class, packets) = mp4_prefix_class(&data[..cut]); | ||
| 638 | + table.push(format!("{i:02},{cut},{class},{packets}")); | ||
| 639 | + assert!( | ||
| 640 | + matches!(class, "NeedMore" | "Open+Eof"), | ||
| 641 | + "{}\nidx,cut,class,packets\n{}", | ||
| 642 | + path.display(), | ||
| 643 | + table.join("\n") | ||
| 644 | + ); | ||
| 645 | + } | ||
| 646 | + println!( | ||
| 647 | + "{}\nidx,cut,class,packets\n{}", | ||
| 648 | + path.display(), | ||
| 649 | + table.join("\n") | ||
| 650 | + ); | ||
| 651 | + } | ||
| 652 | +} | ||
| 653 | + | ||
| 654 | +fn mp4_prefix_class(data: &[u8]) -> (&'static str, u64) { | ||
| 655 | + let mut demuxer = match Mp4Demuxer::new(Cursor::new(data.to_vec())) { | ||
| 656 | + Ok(demuxer) => demuxer, | ||
| 657 | + Err(Error::NeedMore) => return ("NeedMore", 0), | ||
| 658 | + Err(Error::Corrupt { .. }) => return ("Corrupt", 0), | ||
| 659 | + Err(e) => panic!("unexpected mp4 prefix error: {e}"), | ||
| 660 | + }; | ||
| 661 | + let mut packets = 0; | ||
| 662 | + loop { | ||
| 663 | + match demuxer.next_packet() { | ||
| 664 | + Ok(_) => packets += 1, | ||
| 665 | + Err(Error::Eof) => return ("Open+Eof", packets), | ||
| 666 | + Err(Error::NeedMore | Error::Corrupt { .. }) => return ("Corrupt", packets), | ||
| 667 | + Err(e) => panic!("unexpected mp4 packet error: {e}"), | ||
| 668 | + } | ||
| 669 | + } | ||
| 670 | +} | ||
| 671 | + | ||
| 624 | /// A muxer round trip in memory, with the timing checked at the tick: the file | 672 | /// A muxer round trip in memory, with the timing checked at the tick: the file |
| 625 | /// this writes states 1001 ticks a frame on a 24000 clock, which is the only way | 673 | /// this writes states 1001 ticks a frame on a 24000 clock, which is the only way |
| 626 | /// 23.976 stays itself. | 674 | /// 23.976 stays itself. |