From: Kostya Shishkov Date: Tue, 23 Nov 2021 10:30:20 +0000 (+0100) Subject: improve ID3 handling X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=695000c46091c478a9ed4bd7e5d3e7860eaad2c6;p=nihav-player.git improve ID3 handling --- 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];