1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from .base import Type
from .isobmff import IsoBmff
class Jpeg(Type):
"""
Implements the JPEG image type matcher.
"""
MIME = 'image/jpeg'
EXTENSION = 'jpg'
def __init__(self):
super(Jpeg, self).__init__(
mime=Jpeg.MIME,
extension=Jpeg.EXTENSION
)
def match(self, buf):
return (len(buf) > 2 and
buf[0] == 0xFF and
buf[1] == 0xD8 and
buf[2] == 0xFF)
class Jpx(Type):
"""
Implements the JPEG2000 image type matcher.
"""
MIME = "image/jpx"
EXTENSION = "jpx"
def __init__(self):
super(Jpx, self).__init__(mime=Jpx.MIME, extension=Jpx.EXTENSION)
def match(self, buf):
return (
len(buf) > 50
and buf[0] == 0x00
and buf[1] == 0x00
and buf[2] == 0x00
and buf[3] == 0x0C
and buf[16:24] == b"ftypjp2 "
)
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", errors='ignore')
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 False
elif (chunk_type == "acTL"):
return True
# move to the next chunk by skipping data and crc (4 bytes)
i += data_length + 4
return False
class Png(Type):
"""
Implements the PNG image type matcher.
"""
MIME = 'image/png'
EXTENSION = 'png'
def __init__(self):
super(Png, self).__init__(
mime=Png.MIME,
extension=Png.EXTENSION
)
def match(self, buf):
return (len(buf) > 3 and
buf[0] == 0x89 and
buf[1] == 0x50 and
buf[2] == 0x4E and
buf[3] == 0x47)
class Gif(Type):
"""
Implements the GIF image type matcher.
"""
MIME = 'image/gif'
EXTENSION = 'gif'
def __init__(self):
super(Gif, self).__init__(
mime=Gif.MIME,
extension=Gif.EXTENSION,
)
def match(self, buf):
return (len(buf) > 2 and
buf[0] == 0x47 and
buf[1] == 0x49 and
buf[2] == 0x46)
class Webp(Type):
"""
Implements the WEBP image type matcher.
"""
MIME = 'image/webp'
EXTENSION = 'webp'
def __init__(self):
super(Webp, self).__init__(
mime=Webp.MIME,
extension=Webp.EXTENSION,
)
def match(self, buf):
return (len(buf) > 13 and
buf[0] == 0x52 and
buf[1] == 0x49 and
buf[2] == 0x46 and
buf[3] == 0x46 and
buf[8] == 0x57 and
buf[9] == 0x45 and
buf[10] == 0x42 and
buf[11] == 0x50 and
buf[12] == 0x56 and
buf[13] == 0x50)
class Cr2(Type):
"""
Implements the CR2 image type matcher.
"""
MIME = 'image/x-canon-cr2'
EXTENSION = 'cr2'
def __init__(self):
super(Cr2, self).__init__(
mime=Cr2.MIME,
extension=Cr2.EXTENSION,
)
def match(self, buf):
return (len(buf) > 9 and
((buf[0] == 0x49 and buf[1] == 0x49 and
buf[2] == 0x2A and buf[3] == 0x0) or
(buf[0] == 0x4D and buf[1] == 0x4D and
buf[2] == 0x0 and buf[3] == 0x2A)) and
buf[8] == 0x43 and buf[9] == 0x52)
class Tiff(Type):
"""
Implements the TIFF image type matcher.
"""
MIME = 'image/tiff'
EXTENSION = 'tif'
def __init__(self):
super(Tiff, self).__init__(
mime=Tiff.MIME,
extension=Tiff.EXTENSION,
)
def match(self, buf):
return (len(buf) > 9 and
((buf[0] == 0x49 and buf[1] == 0x49 and
buf[2] == 0x2A and buf[3] == 0x0) or
(buf[0] == 0x4D and buf[1] == 0x4D and
buf[2] == 0x0 and buf[3] == 0x2A))
and not (buf[8] == 0x43 and buf[9] == 0x52))
class Bmp(Type):
"""
Implements the BMP image type matcher.
"""
MIME = 'image/bmp'
EXTENSION = 'bmp'
def __init__(self):
super(Bmp, self).__init__(
mime=Bmp.MIME,
extension=Bmp.EXTENSION,
)
def match(self, buf):
return (len(buf) > 1 and
buf[0] == 0x42 and
buf[1] == 0x4D)
class Jxr(Type):
"""
Implements the JXR image type matcher.
"""
MIME = 'image/vnd.ms-photo'
EXTENSION = 'jxr'
def __init__(self):
super(Jxr, self).__init__(
mime=Jxr.MIME,
extension=Jxr.EXTENSION,
)
def match(self, buf):
return (len(buf) > 2 and
buf[0] == 0x49 and
buf[1] == 0x49 and
buf[2] == 0xBC)
class Psd(Type):
"""
Implements the PSD image type matcher.
"""
MIME = 'image/vnd.adobe.photoshop'
EXTENSION = 'psd'
def __init__(self):
super(Psd, self).__init__(
mime=Psd.MIME,
extension=Psd.EXTENSION,
)
def match(self, buf):
return (len(buf) > 3 and
buf[0] == 0x38 and
buf[1] == 0x42 and
buf[2] == 0x50 and
buf[3] == 0x53)
class Ico(Type):
"""
Implements the ICO image type matcher.
"""
MIME = 'image/x-icon'
EXTENSION = 'ico'
def __init__(self):
super(Ico, self).__init__(
mime=Ico.MIME,
extension=Ico.EXTENSION,
)
def match(self, buf):
return (len(buf) > 3 and
buf[0] == 0x00 and
buf[1] == 0x00 and
buf[2] == 0x01 and
buf[3] == 0x00)
class Heic(IsoBmff):
"""
Implements the HEIC image type matcher.
"""
MIME = 'image/heic'
EXTENSION = 'heic'
def __init__(self):
super(Heic, self).__init__(
mime=Heic.MIME,
extension=Heic.EXTENSION
)
def match(self, buf):
if not self._is_isobmff(buf):
return False
major_brand, minor_version, compatible_brands = self._get_ftyp(buf)
if major_brand == 'heic':
return True
if major_brand in ['mif1', 'msf1'] and 'heic' in compatible_brands:
return True
return False
class Dcm(Type):
MIME = 'application/dicom'
EXTENSION = 'dcm'
OFFSET = 128
def __init__(self):
super(Dcm, self).__init__(
mime=Dcm.MIME,
extension=Dcm.EXTENSION
)
def match(self, buf):
return (len(buf) > Dcm.OFFSET + 4 and
buf[Dcm.OFFSET + 0] == 0x44 and
buf[Dcm.OFFSET + 1] == 0x49 and
buf[Dcm.OFFSET + 2] == 0x43 and
buf[Dcm.OFFSET + 3] == 0x4D)
class Dwg(Type):
"""Implements the Dwg image type matcher."""
MIME = 'image/vnd.dwg'
EXTENSION = 'dwg'
def __init__(self):
super(Dwg, self).__init__(
mime=Dwg.MIME,
extension=Dwg.EXTENSION
)
def match(self, buf):
return buf[:4] == bytearray([0x41, 0x43, 0x31, 0x30])
class Xcf(Type):
"""Implements the Xcf image type matcher."""
MIME = 'image/x-xcf'
EXTENSION = 'xcf'
def __init__(self):
super(Xcf, self).__init__(
mime=Xcf.MIME,
extension=Xcf.EXTENSION
)
def match(self, buf):
return buf[:10] == bytearray([0x67, 0x69, 0x6d, 0x70, 0x20,
0x78, 0x63, 0x66, 0x20, 0x76])
class Avif(IsoBmff):
"""
Implements the AVIF image type matcher.
"""
MIME = 'image/avif'
EXTENSION = 'avif'
def __init__(self):
super(Avif, self).__init__(
mime=Avif.MIME,
extension=Avif.EXTENSION
)
def match(self, buf):
if not self._is_isobmff(buf):
return False
major_brand, minor_version, compatible_brands = self._get_ftyp(buf)
if major_brand == 'avif':
return True
if major_brand in ['mif1', 'msf1'] and 'avif' in compatible_brands:
return True
return False
|