From 695000c46091c478a9ed4bd7e5d3e7860eaad2c6 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Tue, 23 Nov 2021 11:30:20 +0100 Subject: [PATCH] improve ID3 handling --- sndplay/src/demux.rs | 45 ++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/sndplay/src/demux.rs b/sndplay/src/demux.rs index ac483da..de9a067 100644 --- a/sndplay/src/demux.rs +++ b/sndplay/src/demux.rs @@ -281,20 +281,27 @@ pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option) { // check for ID3v{2-4} let mut buf = [0; 5]; - br.peek_buf(&mut buf).unwrap(); - if &buf[0..3] == b"ID3" && buf[3] > 0 && buf[3] < 5 && buf[4] == 0 { //ID3 tag found, must be a raw stream - br.read_skip(6).unwrap(); - let mut size = 0; - for _ in 0..4 { - let b = br.read_byte().unwrap(); - if (b & 0x80) != 0 { - println!("Invalid ID3 size"); - break; + loop { + if br.peek_buf(&mut buf).is_err() { + break; + } + if &buf[0..3] == b"ID3" && buf[3] > 0 && buf[3] < 5 && buf[4] == 0 { //ID3 tag found, must be a raw stream + br.read_skip(6).unwrap(); + let mut size = 0; + for _ in 0..4 { + let b = br.read_byte().unwrap(); + if (b & 0x80) != 0 { + println!("Invalid ID3 size"); + break; + } + size = (size << 7) | u64::from(b); } - size = (size << 7) | u64::from(b); + start += size + 10; + br.read_skip(size as usize).unwrap(); + is_raw = true; + } else { + break; } - start = size + 10; - is_raw = true; } // check for ID3v1 br.seek(SeekFrom::End(-128)).unwrap(); @@ -302,6 +309,20 @@ pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option) { br.peek_buf(&mut buf[..3]).unwrap(); if &buf[0..3] == b"TAG" { end = Some(off); + // check for Lyrics v2 + let mut sig = [0; 9]; + br.seek(SeekFrom::End(-128 - 9)).unwrap(); + br.peek_buf(&mut sig).unwrap(); + if &sig == b"LYRICS200" { + br.seek(SeekFrom::Current(-6)).unwrap(); + let mut sizestr = [0; 6]; + br.peek_buf(&mut sizestr).unwrap(); + if let Ok(sstr) = std::str::from_utf8(&sizestr) { + if let Ok(size) = sstr.parse::() { + end = Some(br.tell() - size); + } + } + } } // check for APETAG let mut buf = [0; 8]; -- 2.39.5