From 7edd4dbc4d2e424fed37fbbffbce501587e59282 Mon Sep 17 00:00:00 2001 From: Hyunseok Oh Date: Wed, 19 Aug 2020 01:03:19 +0900 Subject: [PATCH] fix APIC picture type error --- lib/src/frames/id3v2/apic_frame.dart | 28 ++++++++++++++++------------ test/dart_tags_test.dart | 24 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/src/frames/id3v2/apic_frame.dart b/lib/src/frames/id3v2/apic_frame.dart index 7f833d0..1dcccc2 100644 --- a/lib/src/frames/id3v2/apic_frame.dart +++ b/lib/src/frames/id3v2/apic_frame.dart @@ -64,23 +64,27 @@ class ApicFrame with ID3V2Frame { @override AttachedPicture decodeBody(List data, Encoding enc) { - final splitIndex1 = data.indexOf(0x00); + final endOfMimeType = data.indexOf(0x00); - final mime = latin1.decode(data.sublist(0, splitIndex1)); - final imageType = data[splitIndex1 + 1]; + final mime = latin1.decode(data.sublist(0, endOfMimeType)); + final imageType = data[endOfMimeType + 1]; - final splitIndex2 = enc is UTF16 - ? indexOfSplitPattern( - data.sublist(splitIndex1 + 1), [0x00, 0x00], splitIndex1) - : data.sublist(splitIndex1 + 1).indexOf(0x00) + splitIndex1 + 1; + final startOfDescription = endOfMimeType + 2; + final endOfDescription = indexOfSplitPattern( + data, enc is UTF16 ? [0x00, 0x00] : [0x00], startOfDescription); - final description = enc.decode(data.sublist(splitIndex1 + 2, splitIndex2)); + final description = + enc.decode(data.sublist(startOfDescription, endOfDescription)); - final imageData = _imageExtractors.containsKey(mime) - ? _imageExtractors[mime]().parse(data.sublist(splitIndex2)) - : data.sublist(splitIndex2); + final startOfImageData = endOfDescription + (enc is UTF16 ? 2 : 1); - return AttachedPicture(mime, imageType, description, imageData); + final imageData = data.sublist(startOfImageData); + + final extractedImageData = _imageExtractors.containsKey(mime) + ? _imageExtractors[mime]().parse(imageData) + : imageData; + + return AttachedPicture(mime, imageType, description, extractedImageData); } @override diff --git a/test/dart_tags_test.dart b/test/dart_tags_test.dart index 0d2c07f..41e693d 100644 --- a/test/dart_tags_test.dart +++ b/test/dart_tags_test.dart @@ -442,7 +442,7 @@ void main() { ..writeAsBytesSync(pic.imageData); final html = - '

${pic.description}}

Red dot
'; + '

${pic.description}

Red dot
'; File('$outputDir/${pic.description}.html') ..createSync(recursive: true) @@ -454,6 +454,28 @@ void main() { expect(pic, equals(pic1)); }); + //https://github.com/NiKoTron/dart-tags/issues/19 + test('Wrong APIC tags decoding on on Other picture type.', () async { + final pic1 = AttachedPicture( + 'image/jpeg', 0x00, 'foo.jpg', picture.readAsBytesSync()); + + final tag = Tag() + ..tags = {'picture': pic1} + ..type = 'ID3' + ..version = '2.4'; + + final writer = ID3V2Writer(); + + final blocks = writer.write(await file2.readAsBytes(), tag); + + final r = ID3V2Reader(); + final f = await r.read(blocks); + + // ignore: avoid_as + final AttachedPicture pic = (f.tags['picture'] as Map)['Other']; + expect(pic, equals(pic1)); + }); + //https://github.com/NiKoTron/dart-tags/issues/13 test('Wrong utf16 decoding', () async { final expectedArtist = 'Полина Гагарина&Егор Крид';