From 8b9348b4fcadb43a5d6f5fa7f4ed67a3e68e38ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sun, 15 Dec 2024 01:12:33 +0900 Subject: [PATCH] fix: detect mp3 with multiple id3 tag --- .../src/__fixtures__/mp3_with_id3v2_x3.bin | Bin 0 -> 128 bytes um_audio/src/lib.rs | 7 +++++ um_audio/src/metadata.rs | 24 +++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 um_audio/src/__fixtures__/mp3_with_id3v2_x3.bin diff --git a/um_audio/src/__fixtures__/mp3_with_id3v2_x3.bin b/um_audio/src/__fixtures__/mp3_with_id3v2_x3.bin new file mode 100644 index 0000000000000000000000000000000000000000..dc4e1ba950c636d60fc9ea6367e5f1e419a5fa0c GIT binary patch literal 128 zcmeZtF=k-^0p*b3U{@f`&%nU!lUSB!YN2Otre|ni0G5Rio-oB?kw5?x i32 { const MIN_ID3_HEADER_LEN: usize = 10; fn get_id3_header_size(buffer: &[u8], offset: usize) -> Result { - if buffer.len() < MIN_ID3_HEADER_LEN { + if buffer.len() < offset + MIN_ID3_HEADER_LEN { Err(AudioError::NeedMoreHeader(offset + MIN_ID3_HEADER_LEN))?; } + let buffer = &buffer[offset..]; + // TAG: ID3v1, 128 bytes if buffer.starts_with(b"TAG") { return Ok(128); @@ -66,10 +68,20 @@ fn get_ape_v2_size(buffer: &[u8], offset: usize) -> Result { } pub fn get_header_metadata_size(buffer: &[u8], offset: usize) -> Result { - let len = get_id3_header_size(buffer, offset)?; - if len != 0 { - Ok(len) - } else { - get_ape_v2_size(buffer, offset) + let mut offset = offset; + + // Workaround: Some files have multiple ID3v2 tags, max 5 times. + for _ in 0..5 { + let len = match get_id3_header_size(buffer, offset)? { + 0 => get_ape_v2_size(buffer, offset)?, + len => len, + }; + + if len == 0 { + break; + } + offset += len; } + + Ok(offset) }