From dfb5a6a4ee2a6769232664c81e60c6955ec72057 Mon Sep 17 00:00:00 2001 From: Yoshihiro Tanaka Date: Sat, 2 Dec 2023 14:15:49 +0900 Subject: [PATCH] Detect HEIC/HEIF format by checking header bytes --- CHANGELOG.md | 2 + lib/src/magic_number.dart | 95 +++++++++++++++++++++++++++++++++++++++ test/mime_type_test.dart | 47 +++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23be731..0700349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## 1.0.5-wip * Update video/mp4 mimeType lookup by header bytes. +* Add image/heic mimeType lookup by header bytes. +* Add image/heif mimeType lookup by header bytes. * Add m4b mimeType lookup by extension. * Add `text/markdown` mimeType lookup by extension. * Require Dart 3.0.0. diff --git a/lib/src/magic_number.dart b/lib/src/magic_number.dart index cf735f8..c8b5c3b 100644 --- a/lib/src/magic_number.dart +++ b/lib/src/magic_number.dart @@ -309,4 +309,99 @@ const List initialMagicNumbers = [ ]), MagicNumber('font/woff2', [0x77, 0x4f, 0x46, 0x32]), + + /// High Efficiency Image File Format (ISO/IEC 23008-12). + /// -> 4 bytes indicating the ftyp box length. + /// -> 4 bytes have the ASCII characters 'f' 't' 'y' 'p'. + /// -> 4 bytes have the ASCII characters 'h' 'e' 'i' 'c'. + /// https://www.iana.org/assignments/media-types/image/heic + MagicNumber('image/heic', [ + 0x00, + 0x00, + 0x00, + 0x00, + 0x66, + 0x74, + 0x79, + 0x70, + 0x68, + 0x65, + 0x69, + 0x63 + ], mask: [ + 0x00, + 0x00, + 0x00, + 0x00, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF + ]), + + /// -> 4 bytes indicating the ftyp box length. + /// -> 4 bytes have the ASCII characters 'f' 't' 'y' 'p'. + /// -> 4 bytes have the ASCII characters 'h' 'e' 'i' 'x'. + MagicNumber('image/heic', [ + 0x00, + 0x00, + 0x00, + 0x00, + 0x66, + 0x74, + 0x79, + 0x70, + 0x68, + 0x65, + 0x69, + 0x78 + ], mask: [ + 0x00, + 0x00, + 0x00, + 0x00, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF + ]), + + /// -> 4 bytes indicating the ftyp box length. + /// -> 4 bytes have the ASCII characters 'f' 't' 'y' 'p'. + /// -> 4 bytes have the ASCII characters 'm' 'i' 'f' '1'. + MagicNumber('image/heif', [ + 0x00, + 0x00, + 0x00, + 0x00, + 0x66, + 0x74, + 0x79, + 0x70, + 0x6D, + 0x69, + 0x66, + 0x31 + ], mask: [ + 0x00, + 0x00, + 0x00, + 0x00, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF, + 0xFF + ]), ]; diff --git a/test/mime_type_test.dart b/test/mime_type_test.dart index f10d847..f7388ff 100644 --- a/test/mime_type_test.dart +++ b/test/mime_type_test.dart @@ -52,6 +52,8 @@ void main() { _expectMimeType('file.toml', 'application/toml'); _expectMimeType('file.md', 'text/markdown'); _expectMimeType('file.markdown', 'text/markdown'); + _expectMimeType('file.heif', 'image/heif'); + _expectMimeType('file.heic', 'image/heic'); }); test('unknown-mime-type', () { @@ -233,6 +235,51 @@ void main() { 0x56, 0x45 ]); + _expectMimeType('file', 'image/heic', headerBytes: [ + 0x00, + 0x00, + 0x00, + 0x18, + 0x66, + 0x74, + 0x79, + 0x70, + 0x68, + 0x65, + 0x69, + 0x63, + 0x00 + ]); + _expectMimeType('file', 'image/heic', headerBytes: [ + 0x00, + 0x00, + 0x00, + 0x18, + 0x66, + 0x74, + 0x79, + 0x70, + 0x68, + 0x65, + 0x69, + 0x78, + 0x00 + ]); + _expectMimeType('file', 'image/heif', headerBytes: [ + 0x00, + 0x00, + 0x00, + 0x18, + 0x66, + 0x74, + 0x79, + 0x70, + 0x6D, + 0x69, + 0x66, + 0x31, + 0x00 + ]); }); });