From 704706de5926f18f87c35ca1336ab3ee6a9235b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 28 May 2022 14:32:51 +0200 Subject: [PATCH 1/4] Do not return error on garbage data after last Ogg page Some admittely badly-encoded Ogg Vorbis files in the wild contain trailing garbage data after the last Ogg page, such as this one: https://freesound.org/people/stomachache/sounds/157587/. The page reading code explicitly handled this case by returning an error, which makes sense to do given that the Ogg specification is not clear on how this condition should be dealt with. Moreover, programs like `ogginfo` also show warnings for these kind of files. However, being this strict causes codec crates that depend on this one to not be able to play back such files properly, as discussed in https://github.com/RustAudio/ogg/issues/7. Fix the situation by being more lenient with this nonsense. Some additional status information is kept to distinguish this case from empty files. --- src/reading.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/reading.rs b/src/reading.rs index 784ff3e..00568f9 100644 --- a/src/reading.rs +++ b/src/reading.rs @@ -606,19 +606,17 @@ impl UntilPageHeaderReader { })); if rd_len == 0 { - // Reached EOF. - if self.read_amount == 0 { - // If we have read nothing yet, there is no data - // but ogg data, meaning the stream ends legally - // and without corruption. - return Ok(Res::Eof); - } else { - // There is most likely a corruption here. - // I'm not sure, but the ogg spec doesn't say that - // random data past the last ogg page is allowed, - // so we just assume it's not allowed. - tri!(Err(OggReadError::NoCapturePatternFound)); - } + // Reached EOF. This means we're in one of these cases: + // 1. If we have read nothing yet (self.read_amount == 0), + // there is no data but ogg data, meaning the stream + // ends legally and without corruption. + // 2. If we have read something, there is corruption here. + // The ogg spec doesn't say whether random data past the + // last ogg page is allowed, and ogginfo complains about + // that. But files with trailing garbarge can be played + // back just fine by oggdec, VLC and other players, so + // just ignore that to meet user expectations. + return Ok(Res::Eof); } self.read_amount += rd_len; @@ -721,12 +719,14 @@ pub struct PacketReader { rdr :T, base_pck_rdr :BasePacketReader, + + read_some_pg :bool } impl PacketReader { /// Constructs a new `PacketReader` with a given `Read`. pub fn new(rdr :T) -> PacketReader { - PacketReader { rdr, base_pck_rdr : BasePacketReader::new() } + PacketReader { rdr, base_pck_rdr : BasePacketReader::new(), read_some_pg : false } } /// Returns the wrapped reader, consuming the `PacketReader`. pub fn into_inner(self) -> T { @@ -778,7 +778,12 @@ impl PacketReader { loop { res = match res { Eof => return Ok(None), - Found => break, + Found => { + // Keep track whether a page was read to distinguish empty + // files from non-Ogg files at read_ogg_page. + self.read_some_pg = true; + break + }, ReadNeeded => tri!(r.do_read(&mut self.rdr)), SeekNeeded => tri!(r.do_seek(&mut self.rdr)) } @@ -794,7 +799,8 @@ impl PacketReader { fn read_ogg_page(&mut self) -> Result, OggReadError> { let header_buf :[u8; 27] = match tri!(self.read_until_pg_header()) { Some(s) => s, - None => return Ok(None) + None if self.read_some_pg => return Ok(None), + None => return Err(OggReadError::NoCapturePatternFound) }; let (mut pg_prs, page_segments) = tri!(PageParser::new(header_buf)); @@ -835,7 +841,7 @@ impl PacketReader { /// streams with the serial number `n`. /// Note that the `None` case is only intended for streams /// where only one logical stream exists, the seek may misbehave - /// if `Ǹone` gets passed when multiple streams exist. + /// if `None` gets passed when multiple streams exist. /// /// The returned bool indicates whether the seek was successful. pub fn seek_absgp(&mut self, stream_serial :Option, From 22a6f912d56140d7ab67481b19122d92d8b976d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 28 May 2022 17:01:42 +0200 Subject: [PATCH 2/4] Tweak comment to make it clearer --- src/reading.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reading.rs b/src/reading.rs index 00568f9..1be37bf 100644 --- a/src/reading.rs +++ b/src/reading.rs @@ -779,8 +779,8 @@ impl PacketReader { res = match res { Eof => return Ok(None), Found => { - // Keep track whether a page was read to distinguish empty - // files from non-Ogg files at read_ogg_page. + // Keep track whether a page was read to distinguish non-Ogg + // files from Ogg files with trailing junk at read_ogg_page. self.read_some_pg = true; break }, From 63766182dbfc4d16d3d4ace8947b882d6caaeac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 28 May 2022 17:30:48 +0200 Subject: [PATCH 3/4] Add unit test for proper trailing data after last page handling --- src/test.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/test.rs b/src/test.rs index 5d750dc..f83ea6c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -8,7 +8,7 @@ use super::*; -use std::io::{Cursor, Seek, SeekFrom}; +use std::io::{Cursor, Seek, SeekFrom, Write}; macro_rules! test_arr_eq { ($a_arr:expr, $b_arr:expr) => { @@ -464,7 +464,6 @@ fn test_issue_14() { let test_arr_2 = [2, 4, 8, 16, 32, 64, 128, 127, 126, 125, 124]; let test_arr_3 = [3, 5, 9, 17, 33, 65, 129, 129, 127, 126, 125]; { - use std::io::Write; c.write_all(&[b'O']).unwrap(); let mut w = PacketWriter::new(&mut c); let np = PacketWriteEndInfo::NormalPacket; @@ -537,3 +536,43 @@ fn test_issue_14() { assert_eq!(test_arr_3, *p3.data); } } + +// Regression test for issue 7: +// Ignore junk after the last Ogg page, while ensuring that non-Ogg +// data is treated as invalid. +#[test] +fn test_issue_7() { + let mut c = Cursor::new(Vec::new()); + let test_arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let test_arr_2 = [2, 4, 8, 16, 32, 64, 128, 127, 126, 125, 124]; + { + let mut w = PacketWriter::new(&mut c); + w.write_packet(&test_arr[..], 0xdeadb33f, + PacketWriteEndInfo::EndStream, 0).unwrap(); + } + // Write trailing garbage. + c.write_all(&test_arr_2[..]).unwrap(); + //print_u8_slice(c.get_ref()); + + // Trailing garbage should be ignored. + assert_eq!(c.seek(SeekFrom::Start(0)).unwrap(), 0); + { + let mut r = PacketReader::new(c); + let p1 = r.read_packet().unwrap().unwrap(); + assert_eq!(test_arr, *p1.data); + } + + // Non-Ogg data should return an error. + let c = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + { + let mut r = PacketReader::new(c); + assert!(matches!(r.read_packet(), Err(OggReadError::NoCapturePatternFound))); + } + + // Empty data is considered non-Ogg data. + let c = Cursor::new(&[]); + { + let mut r = PacketReader::new(c); + assert!(matches!(r.read_packet(), Err(OggReadError::NoCapturePatternFound))); + } +} From 0823ba9771df8f198cc9b486f0f3bb6692498f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 28 May 2022 17:36:43 +0200 Subject: [PATCH 4/4] Ensure that trailing data is not returned in a packet in unit test I don't expect this additional check to be useful in practice, but it is an invariant that should be held in the test case after all. --- src/test.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test.rs b/src/test.rs index f83ea6c..ff55ae7 100644 --- a/src/test.rs +++ b/src/test.rs @@ -560,6 +560,8 @@ fn test_issue_7() { let mut r = PacketReader::new(c); let p1 = r.read_packet().unwrap().unwrap(); assert_eq!(test_arr, *p1.data); + // Make sure we don't yield the trailing garbage. + assert!(r.read_packet().unwrap().is_none()); } // Non-Ogg data should return an error.