From 84c2869032fdca29acff4f8089d841f9ded12908 Mon Sep 17 00:00:00 2001 From: CatKasha Date: Wed, 20 Apr 2022 22:40:01 +0300 Subject: [PATCH 1/4] add APNG support (part 1) references: https://wiki.mozilla.org/APNG_Specification#Structure http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html --- filetype/types/image.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/filetype/types/image.py b/filetype/types/image.py index ed1779c..4fabd69 100644 --- a/filetype/types/image.py +++ b/filetype/types/image.py @@ -48,6 +48,46 @@ def match(self, buf): ) +class Apng(Type): + """ + Implements the APNG image type matcher. + """ + MIME = 'image/apng' + EXTENSION = 'apng' + + def __init__(self): + super(Apng, self).__init__( + mime=Apng.MIME, + extension=Apng.EXTENSION + ) + + def match(self, buf): + if(len(buf) > 8 and + buf[:8] == bytearray([0x89, 0x50, 0x4e, 0x47, + 0x0d, 0x0a, 0x1a, 0x0a])): + #cursor in buf, skip already readed 8 bytes + i = 8 + while len(buf) > i: + data_length = int.from_bytes(buf[i:i+4], byteorder="big") + i += 4 + + chunk_type = buf[i:i+4].decode("ascii") + i += 4 + + #acTL chunk in APNG should appears first than IDAT + #IEND is end of PNG + if (chunk_type == "IDAT" or chunk_type == "IEND"): + return 0 + elif (chunk_type == "acTL"): + return 1 + + #move to the next chank by skipping data and crc (4 bytes) + i += data_length + 4 + return 0 + else: + return 0 + + class Png(Type): """ Implements the PNG image type matcher. From 96cf5be538ca6df3c1da819e18933d35df8b51e8 Mon Sep 17 00:00:00 2001 From: CatKasha Date: Wed, 20 Apr 2022 22:40:59 +0300 Subject: [PATCH 2/4] add APNG support (part 2) --- filetype/types/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index e513edb..70d8789 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -16,6 +16,7 @@ image.Xcf(), image.Jpeg(), image.Jpx(), + image.Apng(), image.Png(), image.Gif(), image.Webp(), From 27fc3342ac689730598214cc851a751455741075 Mon Sep 17 00:00:00 2001 From: CatKasha Date: Wed, 20 Apr 2022 22:43:32 +0300 Subject: [PATCH 3/4] add APNG support (part 3) --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index b4039c3..19098d5 100644 --- a/README.rst +++ b/README.rst @@ -67,6 +67,7 @@ Image - **jpg** - ``image/jpeg`` - **jpx** - ``image/jpx`` - **png** - ``image/png`` +- **apng** - ``image/apng`` - **gif** - ``image/gif`` - **webp** - ``image/webp`` - **cr2** - ``image/x-canon-cr2`` From 598e179f8c4197804a998989293b579b98ac9f71 Mon Sep 17 00:00:00 2001 From: CatKasha Date: Thu, 21 Apr 2022 01:24:53 +0300 Subject: [PATCH 4/4] fix typo --- filetype/types/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filetype/types/image.py b/filetype/types/image.py index 4fabd69..b64c674 100644 --- a/filetype/types/image.py +++ b/filetype/types/image.py @@ -81,7 +81,7 @@ def match(self, buf): elif (chunk_type == "acTL"): return 1 - #move to the next chank by skipping data and crc (4 bytes) + #move to the next chunk by skipping data and crc (4 bytes) i += data_length + 4 return 0 else: