Skip to content

Commit 84c2869

Browse files
authored
1 parent 2de3edc commit 84c2869

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

filetype/types/image.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,46 @@ def match(self, buf):
4848
)
4949

5050

51+
class Apng(Type):
52+
"""
53+
Implements the APNG image type matcher.
54+
"""
55+
MIME = 'image/apng'
56+
EXTENSION = 'apng'
57+
58+
def __init__(self):
59+
super(Apng, self).__init__(
60+
mime=Apng.MIME,
61+
extension=Apng.EXTENSION
62+
)
63+
64+
def match(self, buf):
65+
if(len(buf) > 8 and
66+
buf[:8] == bytearray([0x89, 0x50, 0x4e, 0x47,
67+
0x0d, 0x0a, 0x1a, 0x0a])):
68+
#cursor in buf, skip already readed 8 bytes
69+
i = 8
70+
while len(buf) > i:
71+
data_length = int.from_bytes(buf[i:i+4], byteorder="big")
72+
i += 4
73+
74+
chunk_type = buf[i:i+4].decode("ascii")
75+
i += 4
76+
77+
#acTL chunk in APNG should appears first than IDAT
78+
#IEND is end of PNG
79+
if (chunk_type == "IDAT" or chunk_type == "IEND"):
80+
return 0
81+
elif (chunk_type == "acTL"):
82+
return 1
83+
84+
#move to the next chank by skipping data and crc (4 bytes)
85+
i += data_length + 4
86+
return 0
87+
else:
88+
return 0
89+
90+
5191
class Png(Type):
5292
"""
5393
Implements the PNG image type matcher.

0 commit comments

Comments
 (0)