From 9a0ecc29c9ec8f5032bf12a21a06a59df6733edb Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:08:50 +0100 Subject: [PATCH 1/6] Add dwg type. --- README.rst | 1 + filetype/types/__init__.py | 1 + filetype/types/image.py | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/README.rst b/README.rst index 87ce898..da17b06 100644 --- a/README.rst +++ b/README.rst @@ -62,6 +62,7 @@ Supported types Image ^^^^^ +- **dwg** - ``image/vnd.dwg`` - **jpg** - ``image/jpeg`` - **jpx** - ``image/jpx`` - **png** - ``image/png`` diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index 87d95b8..78244dc 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -11,6 +11,7 @@ # Supported image types IMAGE = ( + image.Dwg(), image.Jpeg(), image.Jpx(), image.Png(), diff --git a/filetype/types/image.py b/filetype/types/image.py index 0c62e32..da5cd72 100644 --- a/filetype/types/image.py +++ b/filetype/types/image.py @@ -283,3 +283,19 @@ def match(self, buf): 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]) From 760905f7e546a1272587732f7d2e4f9cc776bd1e Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:09:21 +0100 Subject: [PATCH 2/6] Add aac type. --- README.rst | 1 + filetype/types/__init__.py | 1 + filetype/types/audio.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/README.rst b/README.rst index da17b06..638c051 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,7 @@ Video Audio ^^^^^ +- **aac** - ``audio/aac`` - **mid** - ``audio/midi`` - **mp3** - ``audio/mpeg`` - **m4a** - ``audio/m4a`` diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index 78244dc..f624723 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -42,6 +42,7 @@ # Supported audio types AUDIO = ( + audio.Aac(), audio.Midi(), audio.Mp3(), audio.M4a(), diff --git a/filetype/types/audio.py b/filetype/types/audio.py index c25a2c6..b8170c3 100644 --- a/filetype/types/audio.py +++ b/filetype/types/audio.py @@ -168,3 +168,20 @@ def match(self, buf): buf[3] == 0x4D and buf[4] == 0x52 and buf[5] == 0x0A) + + +class Aac(Type): + """Implements the Aac image type matcher.""" + + MIME = 'audio/aac' + EXTENSION = 'aac' + + def __init__(self): + super(Aac, self).__init__( + mime=Aac.MIME, + extension=Aac.EXTENSION + ) + + def match(self, buf): + return (buf[:2] == bytearray([0xff, 0xf1]) or + buf[:2] == bytearray([0xff, 0xf9])) From c518f58f63517d32e1d3820d942bcab538cfdb3e Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:09:58 +0100 Subject: [PATCH 3/6] Add 3gp type. --- README.rst | 1 + filetype/types/__init__.py | 1 + filetype/types/video.py | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/README.rst b/README.rst index 638c051..6a55e6e 100644 --- a/README.rst +++ b/README.rst @@ -79,6 +79,7 @@ Image Video ^^^^^ +- **3gp** - ``video/3gpp`` - **mp4** - ``video/mp4`` - **m4v** - ``video/x-m4v`` - **mkv** - ``video/x-matroska`` diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index f624723..193818d 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -29,6 +29,7 @@ # Supported video types VIDEO = ( + video.M3gp(), video.Mp4(), video.M4v(), video.Mkv(), diff --git a/filetype/types/video.py b/filetype/types/video.py index ea33671..f589c25 100644 --- a/filetype/types/video.py +++ b/filetype/types/video.py @@ -214,3 +214,19 @@ def match(self, buf): buf[2] == 0x1 and buf[3] >= 0xb0 and buf[3] <= 0xbf) + + +class M3gp(Type): + """Implements the 3gp image type matcher.""" + + MIME = 'video/3gpp' + EXTENSION = '3gp' + + def __init__(self): + super(M3gp, self).__init__( + mime=M3gp.MIME, + extension=M3gp.EXTENSION + ) + + def match(self, buf): + return buf[:7] == bytearray([0x66, 0x74, 0x79, 0x70, 0x33, 0x67, 0x70]) From 85fb57f3bcf0724dbba51e6a59ccb76d6b38c7fe Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:10:31 +0100 Subject: [PATCH 4/6] Add rpm and dcm type. --- README.rst | 2 ++ filetype/types/__init__.py | 2 ++ filetype/types/archive.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/README.rst b/README.rst index 6a55e6e..da6a568 100644 --- a/README.rst +++ b/README.rst @@ -105,6 +105,8 @@ Audio Archive ^^^^^^^ +- **rpm** - ``application/x-rpm`` +- **dcm** - ``application/dicom`` - **epub** - ``application/epub+zip`` - **zip** - ``application/zip`` - **tar** - ``application/x-tar`` diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index 193818d..2f90303 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -58,6 +58,8 @@ # Supported archive container types ARCHIVE = ( + archive.Rpm(), + archive.Dcm(), archive.Epub(), archive.Zip(), archive.Tar(), diff --git a/filetype/types/archive.py b/filetype/types/archive.py index 5cde4b3..96f0548 100644 --- a/filetype/types/archive.py +++ b/filetype/types/archive.py @@ -513,3 +513,35 @@ def match(self, buf): buf[1] == 0x5A and buf[2] == 0x49 and buf[3] == 0x50) + + +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]) From 2177fd8ae1d359be43e15f81dce015a3bfbd952c Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:11:40 +0100 Subject: [PATCH 5/6] Add application abstraction and wasm type. --- README.rst | 5 +++++ filetype/match.py | 18 ++++++++++++++++++ filetype/types/__init__.py | 9 ++++++++- filetype/types/application.py | 22 ++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 filetype/types/application.py diff --git a/README.rst b/README.rst index da6a568..d503404 100644 --- a/README.rst +++ b/README.rst @@ -139,6 +139,11 @@ Font - **ttf** - ``application/font-sfnt`` - **otf** - ``application/font-sfnt`` +Application +^^^^ + +- **wasm** - ``application/wasm`` + .. _Python: http://python.org .. _magic numbers: https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files .. _filetype: https://github.com/h2non/filetype diff --git a/filetype/match.py b/filetype/match.py index fe820d9..6c3c606 100644 --- a/filetype/match.py +++ b/filetype/match.py @@ -4,6 +4,7 @@ from .types import ARCHIVE as archive_matchers from .types import AUDIO as audio_matchers +from .types import APPLICATION as application_matchers from .types import FONT as font_matchers from .types import IMAGE as image_matchers from .types import VIDEO as video_matchers @@ -117,3 +118,20 @@ def archive_match(obj): TypeError: if obj is not a supported type. """ return match(obj, archive_matchers) + + +def application_match(obj): + """ + Matches the given input againts the available + application type matchers. + + Args: + obj: path to file, bytes or bytearray. + + Returns: + Type instance if matches. Otherwise None. + + Raises: + TypeError: if obj is not a supported type. + """ + return match(obj, application_matchers) diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index 2f90303..0174476 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -4,6 +4,7 @@ from . import archive from . import audio +from . import application from . import font from . import image from . import video @@ -84,5 +85,11 @@ archive.Lz(), ) +# Supported archive container types +APPLICATION = ( + application.Wasm(), +) + + # Expose supported type matchers -TYPES = list(VIDEO + IMAGE + AUDIO + FONT + ARCHIVE) +TYPES = list(VIDEO + IMAGE + AUDIO + FONT + ARCHIVE + APPLICATION) diff --git a/filetype/types/application.py b/filetype/types/application.py new file mode 100644 index 0000000..6f02370 --- /dev/null +++ b/filetype/types/application.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import + +from .base import Type + + +class Wasm(Type): + """Implements the Wasm image type matcher.""" + + MIME = 'application/wasm' + EXTENSION = 'wasm' + + def __init__(self): + super(Wasm, self).__init__( + mime=Wasm.MIME, + extension=Wasm.EXTENSION + ) + + def match(self, buf): + return buf[:8] == bytearray([0x00, 0x61, 0x73, 0x6d, + 0x01, 0x00, 0x00, 0x00]) From b254b9558ba674218e5cf756e915cf54b1afc55e Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:28:17 +0100 Subject: [PATCH 6/6] Add xcf type. --- README.rst | 1 + filetype/types/__init__.py | 1 + filetype/types/image.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/README.rst b/README.rst index d503404..379b6e4 100644 --- a/README.rst +++ b/README.rst @@ -63,6 +63,7 @@ Image ^^^^^ - **dwg** - ``image/vnd.dwg`` +- **xcf** - ``image/x-xcf`` - **jpg** - ``image/jpeg`` - **jpx** - ``image/jpx`` - **png** - ``image/png`` diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index 0174476..791285a 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -13,6 +13,7 @@ # Supported image types IMAGE = ( image.Dwg(), + image.Xcf(), image.Jpeg(), image.Jpx(), image.Png(), diff --git a/filetype/types/image.py b/filetype/types/image.py index da5cd72..4e76573 100644 --- a/filetype/types/image.py +++ b/filetype/types/image.py @@ -299,3 +299,20 @@ def __init__(self): 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])