From 18f43812ae740cce5834cf8f2a334ea4db9c21f1 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Tue, 23 Nov 2021 11:25:17 +0100 Subject: [PATCH] improve ID3 tag handling --- src/demux.rs | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/demux.rs b/src/demux.rs index 78743be..a058068 100644 --- a/src/demux.rs +++ b/src/demux.rs @@ -302,20 +302,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(); @@ -323,6 +330,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