File: archive.py

package info (click to toggle)
filetype.py 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,560 kB
  • sloc: python: 1,841; makefile: 42; sh: 21
file content (687 lines) | stat: -rw-r--r-- 17,006 bytes parent folder | download
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import struct

from .base import Type


class Epub(Type):
    """
    Implements the EPUB archive type matcher.
    """
    MIME = 'application/epub+zip'
    EXTENSION = 'epub'

    def __init__(self):
        super(Epub, self).__init__(
            mime=Epub.MIME,
            extension=Epub.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 57 and
                buf[0] == 0x50 and buf[1] == 0x4B and
                buf[2] == 0x3 and buf[3] == 0x4 and
                buf[30] == 0x6D and buf[31] == 0x69 and
                buf[32] == 0x6D and buf[33] == 0x65 and
                buf[34] == 0x74 and buf[35] == 0x79 and
                buf[36] == 0x70 and buf[37] == 0x65 and
                buf[38] == 0x61 and buf[39] == 0x70 and
                buf[40] == 0x70 and buf[41] == 0x6C and
                buf[42] == 0x69 and buf[43] == 0x63 and
                buf[44] == 0x61 and buf[45] == 0x74 and
                buf[46] == 0x69 and buf[47] == 0x6F and
                buf[48] == 0x6E and buf[49] == 0x2F and
                buf[50] == 0x65 and buf[51] == 0x70 and
                buf[52] == 0x75 and buf[53] == 0x62 and
                buf[54] == 0x2B and buf[55] == 0x7A and
                buf[56] == 0x69 and buf[57] == 0x70)


class Zip(Type):
    """
    Implements the Zip archive type matcher.
    """
    MIME = 'application/zip'
    EXTENSION = 'zip'

    def __init__(self):
        super(Zip, self).__init__(
            mime=Zip.MIME,
            extension=Zip.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                buf[0] == 0x50 and buf[1] == 0x4B and
                (buf[2] == 0x3 or buf[2] == 0x5 or
                    buf[2] == 0x7) and
                (buf[3] == 0x4 or buf[3] == 0x6 or
                    buf[3] == 0x8))


class Tar(Type):
    """
    Implements the Tar archive type matcher.
    """
    MIME = 'application/x-tar'
    EXTENSION = 'tar'

    def __init__(self):
        super(Tar, self).__init__(
            mime=Tar.MIME,
            extension=Tar.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 261 and
                buf[257] == 0x75 and
                buf[258] == 0x73 and
                buf[259] == 0x74 and
                buf[260] == 0x61 and
                buf[261] == 0x72)


class Rar(Type):
    """
    Implements the RAR archive type matcher.
    """
    MIME = 'application/x-rar-compressed'
    EXTENSION = 'rar'

    def __init__(self):
        super(Rar, self).__init__(
            mime=Rar.MIME,
            extension=Rar.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 6 and
                buf[0] == 0x52 and
                buf[1] == 0x61 and
                buf[2] == 0x72 and
                buf[3] == 0x21 and
                buf[4] == 0x1A and
                buf[5] == 0x7 and
                (buf[6] == 0x0 or
                    buf[6] == 0x1))


class Gz(Type):
    """
    Implements the GZ archive type matcher.
    """
    MIME = 'application/gzip'
    EXTENSION = 'gz'

    def __init__(self):
        super(Gz, self).__init__(
            mime=Gz.MIME,
            extension=Gz.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 2 and
                buf[0] == 0x1F and
                buf[1] == 0x8B and
                buf[2] == 0x8)


class Bz2(Type):
    """
    Implements the BZ2 archive type matcher.
    """
    MIME = 'application/x-bzip2'
    EXTENSION = 'bz2'

    def __init__(self):
        super(Bz2, self).__init__(
            mime=Bz2.MIME,
            extension=Bz2.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 2 and
                buf[0] == 0x42 and
                buf[1] == 0x5A and
                buf[2] == 0x68)


class SevenZ(Type):
    """
    Implements the SevenZ (7z) archive type matcher.
    """
    MIME = 'application/x-7z-compressed'
    EXTENSION = '7z'

    def __init__(self):
        super(SevenZ, self).__init__(
            mime=SevenZ.MIME,
            extension=SevenZ.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 5 and
                buf[0] == 0x37 and
                buf[1] == 0x7A and
                buf[2] == 0xBC and
                buf[3] == 0xAF and
                buf[4] == 0x27 and
                buf[5] == 0x1C)


class Pdf(Type):
    """
    Implements the PDF archive type matcher.
    """
    MIME = 'application/pdf'
    EXTENSION = 'pdf'

    def __init__(self):
        super(Pdf, self).__init__(
            mime=Pdf.MIME,
            extension=Pdf.EXTENSION
        )

    def match(self, buf):
        # Detect BOM and skip first 3 bytes
        if (len(buf) > 3 and
            buf[0] == 0xEF and
            buf[1] == 0xBB and
            buf[2] == 0xBF):  # noqa E129
            buf = buf[3:]

        return (len(buf) > 3 and
                buf[0] == 0x25 and
                buf[1] == 0x50 and
                buf[2] == 0x44 and
                buf[3] == 0x46)


class Exe(Type):
    """
    Implements the EXE archive type matcher.
    """
    MIME = 'application/x-msdownload'
    EXTENSION = 'exe'

    def __init__(self):
        super(Exe, self).__init__(
            mime=Exe.MIME,
            extension=Exe.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 1 and
                buf[0] == 0x4D and
                buf[1] == 0x5A)


class Swf(Type):
    """
    Implements the SWF archive type matcher.
    """
    MIME = 'application/x-shockwave-flash'
    EXTENSION = 'swf'

    def __init__(self):
        super(Swf, self).__init__(
            mime=Swf.MIME,
            extension=Swf.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 2 and
                (buf[0] == 0x43 or
                    buf[0] == 0x46) and
                buf[1] == 0x57 and
                buf[2] == 0x53)


class Rtf(Type):
    """
    Implements the RTF archive type matcher.
    """
    MIME = 'application/rtf'
    EXTENSION = 'rtf'

    def __init__(self):
        super(Rtf, self).__init__(
            mime=Rtf.MIME,
            extension=Rtf.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 4 and
                buf[0] == 0x7B and
                buf[1] == 0x5C and
                buf[2] == 0x72 and
                buf[3] == 0x74 and
                buf[4] == 0x66)


class Nes(Type):
    """
    Implements the NES archive type matcher.
    """
    MIME = 'application/x-nintendo-nes-rom'
    EXTENSION = 'nes'

    def __init__(self):
        super(Nes, self).__init__(
            mime=Nes.MIME,
            extension=Nes.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                buf[0] == 0x4E and
                buf[1] == 0x45 and
                buf[2] == 0x53 and
                buf[3] == 0x1A)


class Crx(Type):
    """
    Implements the CRX archive type matcher.
    """
    MIME = 'application/x-google-chrome-extension'
    EXTENSION = 'crx'

    def __init__(self):
        super(Crx, self).__init__(
            mime=Crx.MIME,
            extension=Crx.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                buf[0] == 0x43 and
                buf[1] == 0x72 and
                buf[2] == 0x32 and
                buf[3] == 0x34)


class Cab(Type):
    """
    Implements the CAB archive type matcher.
    """
    MIME = 'application/vnd.ms-cab-compressed'
    EXTENSION = 'cab'

    def __init__(self):
        super(Cab, self).__init__(
            mime=Cab.MIME,
            extension=Cab.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                ((buf[0] == 0x4D and
                    buf[1] == 0x53 and
                    buf[2] == 0x43 and
                    buf[3] == 0x46) or
                    (buf[0] == 0x49 and
                        buf[1] == 0x53 and
                        buf[2] == 0x63 and
                        buf[3] == 0x28)))


class Eot(Type):
    """
    Implements the EOT archive type matcher.
    """
    MIME = 'application/octet-stream'
    EXTENSION = 'eot'

    def __init__(self):
        super(Eot, self).__init__(
            mime=Eot.MIME,
            extension=Eot.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 35 and
                buf[34] == 0x4C and
                buf[35] == 0x50 and
                ((buf[8] == 0x02 and
                    buf[9] == 0x00 and
                    buf[10] == 0x01) or
                (buf[8] == 0x01 and
                    buf[9] == 0x00 and
                    buf[10] == 0x00) or
                    (buf[8] == 0x02 and
                        buf[9] == 0x00 and
                        buf[10] == 0x02)))


class Ps(Type):
    """
    Implements the PS archive type matcher.
    """
    MIME = 'application/postscript'
    EXTENSION = 'ps'

    def __init__(self):
        super(Ps, self).__init__(
            mime=Ps.MIME,
            extension=Ps.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 1 and
                buf[0] == 0x25 and
                buf[1] == 0x21)


class Xz(Type):
    """
    Implements the XS archive type matcher.
    """
    MIME = 'application/x-xz'
    EXTENSION = 'xz'

    def __init__(self):
        super(Xz, self).__init__(
            mime=Xz.MIME,
            extension=Xz.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 5 and
                buf[0] == 0xFD and
                buf[1] == 0x37 and
                buf[2] == 0x7A and
                buf[3] == 0x58 and
                buf[4] == 0x5A and
                buf[5] == 0x00)


class Sqlite(Type):
    """
    Implements the Sqlite DB archive type matcher.
    """
    MIME = 'application/x-sqlite3'
    EXTENSION = 'sqlite'

    def __init__(self):
        super(Sqlite, self).__init__(
            mime=Sqlite.MIME,
            extension=Sqlite.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                buf[0] == 0x53 and
                buf[1] == 0x51 and
                buf[2] == 0x4C and
                buf[3] == 0x69)


class Deb(Type):
    """
    Implements the DEB archive type matcher.
    """
    MIME = 'application/x-deb'
    EXTENSION = 'deb'

    def __init__(self):
        super(Deb, self).__init__(
            mime=Deb.MIME,
            extension=Deb.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 20 and
                buf[0] == 0x21 and
                buf[1] == 0x3C and
                buf[2] == 0x61 and
                buf[3] == 0x72 and
                buf[4] == 0x63 and
                buf[5] == 0x68 and
                buf[6] == 0x3E and
                buf[7] == 0x0A and
                buf[8] == 0x64 and
                buf[9] == 0x65 and
                buf[10] == 0x62 and
                buf[11] == 0x69 and
                buf[12] == 0x61 and
                buf[13] == 0x6E and
                buf[14] == 0x2D and
                buf[15] == 0x62 and
                buf[16] == 0x69 and
                buf[17] == 0x6E and
                buf[18] == 0x61 and
                buf[19] == 0x72 and
                buf[20] == 0x79)


class Ar(Type):
    """
    Implements the AR archive type matcher.
    """
    MIME = 'application/x-unix-archive'
    EXTENSION = 'ar'

    def __init__(self):
        super(Ar, self).__init__(
            mime=Ar.MIME,
            extension=Ar.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 6 and
                buf[0] == 0x21 and
                buf[1] == 0x3C and
                buf[2] == 0x61 and
                buf[3] == 0x72 and
                buf[4] == 0x63 and
                buf[5] == 0x68 and
                buf[6] == 0x3E)


class Z(Type):
    """
    Implements the Z archive type matcher.
    """
    MIME = 'application/x-compress'
    EXTENSION = 'Z'

    def __init__(self):
        super(Z, self).__init__(
            mime=Z.MIME,
            extension=Z.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 1 and
                ((buf[0] == 0x1F and
                    buf[1] == 0xA0) or
                (buf[0] == 0x1F and
                    buf[1] == 0x9D)))


class Lzop(Type):
    """
    Implements the Lzop archive type matcher.
    """
    MIME = 'application/x-lzop'
    EXTENSION = 'lzo'

    def __init__(self):
        super(Lzop, self).__init__(
            mime=Lzop.MIME,
            extension=Lzop.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 7 and
                buf[0] == 0x89 and
                buf[1] == 0x4C and
                buf[2] == 0x5A and
                buf[3] == 0x4F and
                buf[4] == 0x00 and
                buf[5] == 0x0D and
                buf[6] == 0x0A and
                buf[7] == 0x1A)


class Lz(Type):
    """
    Implements the Lz archive type matcher.
    """
    MIME = 'application/x-lzip'
    EXTENSION = 'lz'

    def __init__(self):
        super(Lz, self).__init__(
            mime=Lz.MIME,
            extension=Lz.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                buf[0] == 0x4C and
                buf[1] == 0x5A and
                buf[2] == 0x49 and
                buf[3] == 0x50)


class Elf(Type):
    """
    Implements the Elf archive type matcher
    """
    MIME = 'application/x-executable'
    EXTENSION = 'elf'

    def __init__(self):
        super(Elf, self).__init__(
            mime=Elf.MIME,
            extension=Elf.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 52 and
                buf[0] == 0x7F and
                buf[1] == 0x45 and
                buf[2] == 0x4C and
                buf[3] == 0x46)


class Lz4(Type):
    """
    Implements the Lz4 archive type matcher.
    """
    MIME = 'application/x-lz4'
    EXTENSION = 'lz4'

    def __init__(self):
        super(Lz4, self).__init__(
            mime=Lz4.MIME,
            extension=Lz4.EXTENSION
        )

    def match(self, buf):
        return (len(buf) > 3 and
                buf[0] == 0x04 and
                buf[1] == 0x22 and
                buf[2] == 0x4D and
                buf[3] == 0x18)


class Br(Type):
    """Implements the Br image type matcher."""

    MIME = 'application/x-brotli'
    EXTENSION = 'br'

    def __init__(self):
        super(Br, self).__init__(
            mime=Br.MIME,
            extension=Br.EXTENSION
        )

    def match(self, buf):
        return buf[:4] == bytearray([0xce, 0xb2, 0xcf, 0x81])


class Dcm(Type):
    """Implements the Dcm image type matcher."""

    MIME = 'application/dicom'
    EXTENSION = 'dcm'

    def __init__(self):
        super(Dcm, self).__init__(
            mime=Dcm.MIME,
            extension=Dcm.EXTENSION
        )

    def match(self, buf):
        return buf[128:131] == bytearray([0x44, 0x49, 0x43, 0x4d])


class Rpm(Type):
    """Implements the Rpm image type matcher."""

    MIME = 'application/x-rpm'
    EXTENSION = 'rpm'

    def __init__(self):
        super(Rpm, self).__init__(
            mime=Rpm.MIME,
            extension=Rpm.EXTENSION
        )

    def match(self, buf):
        return buf[:4] == bytearray([0xed, 0xab, 0xee, 0xdb])


class Zstd(Type):
    """
    Implements the Zstd archive type matcher.
    https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
    """
    MIME = 'application/zstd'
    EXTENSION = 'zst'
    MAGIC_SKIPPABLE_START = 0x184D2A50
    MAGIC_SKIPPABLE_MASK = 0xFFFFFFF0

    def __init__(self):
        super(Zstd, self).__init__(
            mime=Zstd.MIME,
            extension=Zstd.EXTENSION
        )

    @staticmethod
    def _to_little_endian_int(buf):
        # return int.from_bytes(buf, byteorder='little')
        return struct.unpack('<L', buf)[0]

    def match(self, buf):
        # Zstandard compressed data is made of one or more frames.
        # There are two frame formats defined by Zstandard:
        # Zstandard frames and Skippable frames.
        # See more details from
        # https://tools.ietf.org/id/draft-kucherawy-dispatch-zstd-00.html#rfc.section.2
        is_zstd = (
            len(buf) > 3 and
            buf[0] in (0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28) and
            buf[1] == 0xb5 and
            buf[2] == 0x2f and
            buf[3] == 0xfd)
        if is_zstd:
            return True
        # skippable frames
        if len(buf) < 8:
            return False
        magic = self._to_little_endian_int(buf[:4]) & Zstd.MAGIC_SKIPPABLE_MASK
        if magic == Zstd.MAGIC_SKIPPABLE_START:
            user_data_len = self._to_little_endian_int(buf[4:8])
            if len(buf) < 8 + user_data_len:
                return False
            next_frame = buf[8 + user_data_len:]
            return self.match(next_frame)
        return False