#!/usr/bin/python2 """ Library used to determine a file MIME type by its magic number, it doesn't have any external dependencies. Based on work of Jason Petrone (jp_py@jsnp.net), adapted to autotest. Command Line Usage: Running as 'python magic.py file_path' will print a mime string (or just a description) of the file present on file_path. API Usage: magic.guess_type(file_path) - Returns a description of what the file on path 'file' contains. This function name was chosen due to a similar function on python standard library 'mimetypes'. @license: GPL v2 @copyright: Jason Petrone (jp_py@jsnp.net) 2000 @copyright: Lucas Meneghel Rodrigues (lmr@redhat.com) 2010 @see: http://www.jsnp.net/code/magic.py """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from six.moves import range import logging, optparse, os, re, sys, string, struct from autotest_lib.client.common_lib import logging_config from autotest_lib.client.common_lib import logging_manager def _str_to_num(n): """ Convert a hex or octal string to a decimal number. @param n: Hex or octal string to be converted. @return: Resulting decimal number. """ val = 0 col = int(1) if n[:1] == 'x': n = '0' + n if n[:2] == '0x': # hex n = str.lower(n[2:]) while len(n) > 0: l = n[len(n) - 1] val = val + string.hexdigits.index(l) * col col = col * 16 n = n[:len(n)-1] elif n[0] == '\\': # octal n = n[1:] while len(n) > 0: l = n[len(n) - 1] if ord(l) < 48 or ord(l) > 57: break val = val + int(l) * col col = col * 8 n = n[:len(n)-1] else: val = int(n) return val class MagicLoggingConfig(logging_config.LoggingConfig): def configure_logging(self, results_dir=None, verbose=False): super(MagicLoggingConfig, self).configure_logging(use_console=True, verbose=verbose) class MagicTest(object): """ Compile a magic database entry so it can be compared with data read from files. """ def __init__(self, offset, t, op, value, msg, mask=None): """ Reads magic database data. Maps the list fields into class attributes. @param offset: Offset from start of the file. @param t: Type of the magic data. @param op: Operation to be performed when comparing the data. @param value: Expected value of the magic data for a given data type. @param msg: String representing the file mimetype. """ if t.count('&') > 0: mask = _str_to_num(t[t.index('&')+1:]) t = t[:t.index('&')] if type(offset) == type('a'): self.offset = _str_to_num(offset) else: self.offset = offset self.type = t self.msg = msg self.subTests = [] self.op = op self.mask = mask self.value = value def test(self, data): """ Compare data read from file with self.value if operator is '='. @param data: Data read from the file. @return: None if no match between data and expected value string. Else, print matching mime type information. """ if self.mask: data = data & self.mask if self.op == '=': if self.value == data: return self.msg elif self.op == '<': pass elif self.op == '>': pass elif self.op == '&': pass elif self.op == '^': pass return None def compare(self, data): """ Compare data read from the file with the expected data for this particular mime type register. @param data: Data read from the file. """ try: if self.type == 'string': c = ''; s = '' for i in range(0, len(self.value)+1): if i + self.offset > len(data) - 1: break s = s + c [c] = struct.unpack('c', data[self.offset + i]) data = s elif self.type == 'short': [data] = struct.unpack('h', data[self.offset:self.offset + 2]) elif self.type == 'leshort': [data] = struct.unpack('H', data[self.offset:self.offset + 2]) elif self.type == 'long': [data] = struct.unpack('l', data[self.offset:self.offset + 4]) elif self.type == 'lelong': [data] = struct.unpack('l', data[self.offset:self.offset + 4]) else: pass except: return None return self.test(data) magic_database = [ [0, 'leshort', '=', 1538, 'application/x-alan-adventure-game'], [0, 'string', '=', 'TADS', 'application/x-tads-game'], [0, 'short', '=', 420, 'application/x-executable-file'], [0, 'short', '=', 421, 'application/x-executable-file'], [0, 'leshort', '=', 603, 'application/x-executable-file'], [0, 'string', '=', 'Core\001', 'application/x-executable-file'], [0, 'string', '=', 'AMANDA: TAPESTART DATE', 'application/x-amanda-header'], [0, 'belong', '=', 1011, 'application/x-executable-file'], [0, 'belong', '=', 999, 'application/x-library-file'], [0, 'belong', '=', 435, 'video/mpeg'], [0, 'belong', '=', 442, 'video/mpeg'], [0, 'beshort&0xfff0', '=', 65520, 'audio/mpeg'], [4, 'leshort', '=', 44817, 'video/fli'], [4, 'leshort', '=', 44818, 'video/flc'], [0, 'string', '=', 'MOVI', 'video/x-sgi-movie'], [4, 'string', '=', 'moov', 'video/quicktime'], [4, 'string', '=', 'mdat', 'video/quicktime'], [0, 'long', '=', 100554, 'application/x-apl-workspace'], [0, 'string', '=', 'FiLeStArTfIlEsTaRt', 'text/x-apple-binscii'], [0, 'string', '=', '\012GL', 'application/data'], [0, 'string', '=', 'v\377', 'application/data'], [0, 'string', '=', 'NuFile', 'application/data'], [0, 'string', '=', 'N\365F\351l\345', 'application/data'], [0, 'belong', '=', 333312, 'application/data'], [0, 'belong', '=', 333319, 'application/data'], [257, 'string', '=', 'ustar\000', 'application/x-tar'], [257, 'string', '=', 'ustar \000', 'application/x-gtar'], [0, 'short', '=', 70707, 'application/x-cpio'], [0, 'short', '=', 143561, 'application/x-bcpio'], [0, 'string', '=', '070707', 'application/x-cpio'], [0, 'string', '=', '070701', 'application/x-cpio'], [0, 'string', '=', '070702', 'application/x-cpio'], [0, 'string', '=', '!\012debian', 'application/x-dpkg'], [0, 'string', '=', '\xed\xab\xee\xdb', 'application/x-rpm'], [0, 'long', '=', 177555, 'application/x-ar'], [0, 'short', '=', 177555, 'application/data'], [0, 'long', '=', 177545, 'application/data'], [0, 'short', '=', 177545, 'application/data'], [0, 'long', '=', 100554, 'application/x-apl-workspace'], [0, 'string', '=', '', 'application/x-ar'], [0, 'string', '=', '!\012__________E', 'application/x-ar'], [0, 'string', '=', '-h-', 'application/data'], [0, 'string', '=', '!', 'application/x-ar'], [0, 'string', '=', '', 'application/x-ar'], [0, 'string', '=', '', 'application/x-ar'], [0, 'belong', '=', 1711210496, 'application/x-ar'], [0, 'belong', '=', 1013019198, 'application/x-ar'], [0, 'long', '=', 557605234, 'application/x-ar'], [0, 'lelong', '=', 177555, 'application/data'], [0, 'leshort', '=', 177555, 'application/data'], [0, 'lelong', '=', 177545, 'application/data'], [0, 'leshort', '=', 177545, 'application/data'], [0, 'lelong', '=', 236525, 'application/data'], [0, 'lelong', '=', 236526, 'application/data'], [0, 'lelong&0x8080ffff', '=', 2074, 'application/x-arc'], [0, 'lelong&0x8080ffff', '=', 2330, 'application/x-arc'], [0, 'lelong&0x8080ffff', '=', 538, 'application/x-arc'], [0, 'lelong&0x8080ffff', '=', 794, 'application/x-arc'], [0, 'lelong&0x8080ffff', '=', 1050, 'application/x-arc'], [0, 'lelong&0x8080ffff', '=', 1562, 'application/x-arc'], [0, 'string', '=', '\032archive', 'application/data'], [0, 'leshort', '=', 60000, 'application/x-arj'], [0, 'string', '=', 'HPAK', 'application/data'], [0, 'string', '=', '\351,\001JAM application/data', ''], [2, 'string', '=', '-lh0-', 'application/x-lha'], [2, 'string', '=', '-lh1-', 'application/x-lha'], [2, 'string', '=', '-lz4-', 'application/x-lha'], [2, 'string', '=', '-lz5-', 'application/x-lha'], [2, 'string', '=', '-lzs-', 'application/x-lha'], [2, 'string', '=', '-lh -', 'application/x-lha'], [2, 'string', '=', '-lhd-', 'application/x-lha'], [2, 'string', '=', '-lh2-', 'application/x-lha'], [2, 'string', '=', '-lh3-', 'application/x-lha'], [2, 'string', '=', '-lh4-', 'application/x-lha'], [2, 'string', '=', '-lh5-', 'application/x-lha'], [0, 'string', '=', 'Rar!', 'application/x-rar'], [0, 'string', '=', 'SQSH', 'application/data'], [0, 'string', '=', 'UC2\032', 'application/data'], [0, 'string', '=', 'PK\003\004', 'application/zip'], [20, 'lelong', '=', 4257523676, 'application/x-zoo'], [10, 'string', '=', '# This is a shell archive', 'application/x-shar'], [0, 'string', '=', '*STA', 'application/data'], [0, 'string', '=', '2278', 'application/data'], [0, 'beshort', '=', 560, 'application/x-executable-file'], [0, 'beshort', '=', 561, 'application/x-executable-file'], [0, 'string', '=', '\000\004\036\212\200', 'application/core'], [0, 'string', '=', '.snd', 'audio/basic'], [0, 'lelong', '=', 6583086, 'audio/basic'], [0, 'string', '=', 'MThd', 'audio/midi'], [0, 'string', '=', 'CTMF', 'audio/x-cmf'], [0, 'string', '=', 'SBI', 'audio/x-sbi'], [0, 'string', '=', 'Creative Voice File', 'audio/x-voc'], [0, 'belong', '=', 1314148939, 'audio/x-multitrack'], [0, 'string', '=', 'RIFF', 'audio/x-wav'], [0, 'string', '=', 'EMOD', 'audio/x-emod'], [0, 'belong', '=', 779248125, 'audio/x-pn-realaudio'], [0, 'string', '=', 'MTM', 'audio/x-multitrack'], [0, 'string', '=', 'if', 'audio/x-669-mod'], [0, 'string', '=', 'FAR', 'audio/mod'], [0, 'string', '=', 'MAS_U', 'audio/x-multimate-mod'], [44, 'string', '=', 'SCRM', 'audio/x-st3-mod'], [0, 'string', '=', 'GF1PATCH110\000ID#000002\000', 'audio/x-gus-patch'], [0, 'string', '=', 'GF1PATCH100\000ID#000002\000', 'audio/x-gus-patch'], [0, 'string', '=', 'JN', 'audio/x-669-mod'], [0, 'string', '=', 'UN05', 'audio/x-mikmod-uni'], [0, 'string', '=', 'Extended Module:', 'audio/x-ft2-mod'], [21, 'string', '=', '!SCREAM!', 'audio/x-st2-mod'], [1080, 'string', '=', 'M.K.', 'audio/x-protracker-mod'], [1080, 'string', '=', 'M!K!', 'audio/x-protracker-mod'], [1080, 'string', '=', 'FLT4', 'audio/x-startracker-mod'], [1080, 'string', '=', '4CHN', 'audio/x-fasttracker-mod'], [1080, 'string', '=', '6CHN', 'audio/x-fasttracker-mod'], [1080, 'string', '=', '8CHN', 'audio/x-fasttracker-mod'], [1080, 'string', '=', 'CD81', 'audio/x-oktalyzer-mod'], [1080, 'string', '=', 'OKTA', 'audio/x-oktalyzer-mod'], [1080, 'string', '=', '16CN', 'audio/x-taketracker-mod'], [1080, 'string', '=', '32CN', 'audio/x-taketracker-mod'], [0, 'string', '=', 'TOC', 'audio/x-toc'], [0, 'short', '=', 3401, 'application/x-executable-file'], [0, 'long', '=', 406, 'application/x-executable-file'], [0, 'short', '=', 406, 'application/x-executable-file'], [0, 'short', '=', 3001, 'application/x-executable-file'], [0, 'lelong', '=', 314, 'application/x-executable-file'], [0, 'string', '=', '//', 'text/cpp'], [0, 'string', '=', '\\\\1cw\\', 'application/data'], [0, 'string', '=', '\\\\1cw', 'application/data'], [0, 'belong&0xffffff00', '=', 2231440384, 'application/data'], [0, 'belong&0xffffff00', '=', 2231487232, 'application/data'], [0, 'short', '=', 575, 'application/x-executable-file'], [0, 'short', '=', 577, 'application/x-executable-file'], [4, 'string', '=', 'pipe', 'application/data'], [4, 'string', '=', 'prof', 'application/data'], [0, 'string', '=', ': shell', 'application/data'], [0, 'string', '=', '#!/bin/sh', 'application/x-sh'], [0, 'string', '=', '#! /bin/sh', 'application/x-sh'], [0, 'string', '=', '#! /bin/sh', 'application/x-sh'], [0, 'string', '=', '#!/bin/csh', 'application/x-csh'], [0, 'string', '=', '#! /bin/csh', 'application/x-csh'], [0, 'string', '=', '#! /bin/csh', 'application/x-csh'], [0, 'string', '=', '#!/bin/ksh', 'application/x-ksh'], [0, 'string', '=', '#! /bin/ksh', 'application/x-ksh'], [0, 'string', '=', '#! /bin/ksh', 'application/x-ksh'], [0, 'string', '=', '#!/bin/tcsh', 'application/x-csh'], [0, 'string', '=', '#! /bin/tcsh', 'application/x-csh'], [0, 'string', '=', '#! /bin/tcsh', 'application/x-csh'], [0, 'string', '=', '#!/usr/local/tcsh', 'application/x-csh'], [0, 'string', '=', '#! /usr/local/tcsh', 'application/x-csh'], [0, 'string', '=', '#!/usr/local/bin/tcsh', 'application/x-csh'], [0, 'string', '=', '#! /usr/local/bin/tcsh', 'application/x-csh'], [0, 'string', '=', '#! /usr/local/bin/tcsh', 'application/x-csh'], [0, 'string', '=', '#!/usr/local/bin/zsh', 'application/x-zsh'], [0, 'string', '=', '#! /usr/local/bin/zsh', 'application/x-zsh'], [0, 'string', '=', '#! /usr/local/bin/zsh', 'application/x-zsh'], [0, 'string', '=', '#!/usr/local/bin/ash', 'application/x-sh'], [0, 'string', '=', '#! /usr/local/bin/ash', 'application/x-zsh'], [0, 'string', '=', '#! /usr/local/bin/ash', 'application/x-zsh'], [0, 'string', '=', '#!/usr/local/bin/ae', 'text/script'], [0, 'string', '=', '#! /usr/local/bin/ae', 'text/script'], [0, 'string', '=', '#! /usr/local/bin/ae', 'text/script'], [0, 'string', '=', '#!/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#! /bin/nawk', 'application/x-awk'], [0, 'string', '=', '#! /bin/nawk', 'application/x-awk'], [0, 'string', '=', '#!/usr/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#!/usr/local/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/local/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/local/bin/nawk', 'application/x-awk'], [0, 'string', '=', '#!/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#! /bin/gawk', 'application/x-awk'], [0, 'string', '=', '#! /bin/gawk', 'application/x-awk'], [0, 'string', '=', '#!/usr/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#!/usr/local/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/local/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#! /usr/local/bin/gawk', 'application/x-awk'], [0, 'string', '=', '#!/bin/awk', 'application/x-awk'], [0, 'string', '=', '#! /bin/awk', 'application/x-awk'], [0, 'string', '=', '#! /bin/awk', 'application/x-awk'], [0, 'string', '=', '#!/usr/bin/awk', 'application/x-awk'], [0, 'string', '=', '#! /usr/bin/awk', 'application/x-awk'], [0, 'string', '=', '#! /usr/bin/awk', 'application/x-awk'], [0, 'string', '=', 'BEGIN', 'application/x-awk'], [0, 'string', '=', '#!/bin/perl', 'application/x-perl'], [0, 'string', '=', '#! /bin/perl', 'application/x-perl'], [0, 'string', '=', '#! /bin/perl', 'application/x-perl'], [0, 'string', '=', 'eval "exec /bin/perl', 'application/x-perl'], [0, 'string', '=', '#!/usr/bin/perl', 'application/x-perl'], [0, 'string', '=', '#! /usr/bin/perl', 'application/x-perl'], [0, 'string', '=', '#! /usr/bin/perl', 'application/x-perl'], [0, 'string', '=', 'eval "exec /usr/bin/perl', 'application/x-perl'], [0, 'string', '=', '#!/usr/local/bin/perl', 'application/x-perl'], [0, 'string', '=', '#! /usr/local/bin/perl', 'application/x-perl'], [0, 'string', '=', '#! /usr/local/bin/perl', 'application/x-perl'], [0, 'string', '=', 'eval "exec /usr/local/bin/perl', 'application/x-perl'], [0, 'string', '=', '#!/bin/python', 'application/x-python'], [0, 'string', '=', '#! /bin/python', 'application/x-python'], [0, 'string', '=', '#! /bin/python', 'application/x-python'], [0, 'string', '=', 'eval "exec /bin/python', 'application/x-python'], [0, 'string', '=', '#!/usr/bin/python', 'application/x-python'], [0, 'string', '=', '#! /usr/bin/python', 'application/x-python'], [0, 'string', '=', '#! /usr/bin/python', 'application/x-python'], [0, 'string', '=', 'eval "exec /usr/bin/python', 'application/x-python'], [0, 'string', '=', '#!/usr/local/bin/python', 'application/x-python'], [0, 'string', '=', '#! /usr/local/bin/python', 'application/x-python'], [0, 'string', '=', '#! /usr/local/bin/python', 'application/x-python'], [0, 'string', '=', 'eval "exec /usr/local/bin/python', 'application/x-python'], [0, 'string', '=', '#!/usr/bin/env python', 'application/x-python'], [0, 'string', '=', '#! /usr/bin/env python', 'application/x-python'], [0, 'string', '=', '#!/bin/rc', 'text/script'], [0, 'string', '=', '#! /bin/rc', 'text/script'], [0, 'string', '=', '#! /bin/rc', 'text/script'], [0, 'string', '=', '#!/bin/bash', 'application/x-sh'], [0, 'string', '=', '#! /bin/bash', 'application/x-sh'], [0, 'string', '=', '#! /bin/bash', 'application/x-sh'], [0, 'string', '=', '#!/usr/local/bin/bash', 'application/x-sh'], [0, 'string', '=', '#! /usr/local/bin/bash', 'application/x-sh'], [0, 'string', '=', '#! /usr/local/bin/bash', 'application/x-sh'], [0, 'string', '=', '#! /', 'text/script'], [0, 'string', '=', '#! /', 'text/script'], [0, 'string', '=', '#!/', 'text/script'], [0, 'string', '=', '#! text/script', ''], [0, 'string', '=', '\037\235', 'application/compress'], [0, 'string', '=', '\037\213', 'application/x-gzip'], [0, 'string', '=', '\037\036', 'application/data'], [0, 'short', '=', 17437, 'application/data'], [0, 'short', '=', 8191, 'application/data'], [0, 'string', '=', '\377\037', 'application/data'], [0, 'short', '=', 145405, 'application/data'], [0, 'string', '=', 'BZh', 'application/x-bzip2'], [0, 'leshort', '=', 65398, 'application/data'], [0, 'leshort', '=', 65142, 'application/data'], [0, 'leshort', '=', 64886, 'application/x-lzh'], [0, 'string', '=', '\037\237', 'application/data'], [0, 'string', '=', '\037\236', 'application/data'], [0, 'string', '=', '\037\240', 'application/data'], [0, 'string', '=', 'BZ', 'application/x-bzip'], [0, 'string', '=', '\211LZO\000\015\012\032\012', 'application/data'], [0, 'belong', '=', 507, 'application/x-object-file'], [0, 'belong', '=', 513, 'application/x-executable-file'], [0, 'belong', '=', 515, 'application/x-executable-file'], [0, 'belong', '=', 517, 'application/x-executable-file'], [0, 'belong', '=', 70231, 'application/core'], [24, 'belong', '=', 60011, 'application/data'], [24, 'belong', '=', 60012, 'application/data'], [24, 'belong', '=', 60013, 'application/data'], [24, 'belong', '=', 60014, 'application/data'], [0, 'belong', '=', 601, 'application/x-object-file'], [0, 'belong', '=', 607, 'application/data'], [0, 'belong', '=', 324508366, 'application/x-gdbm'], [0, 'lelong', '=', 324508366, 'application/x-gdbm'], [0, 'string', '=', 'GDBM', 'application/x-gdbm'], [0, 'belong', '=', 398689, 'application/x-db'], [0, 'belong', '=', 340322, 'application/x-db'], [0, 'string', '=', '\012\012________64E', 'application/data'], [0, 'leshort', '=', 387, 'application/x-executable-file'], [0, 'leshort', '=', 392, 'application/x-executable-file'], [0, 'leshort', '=', 399, 'application/x-object-file'], [0, 'string', '=', '\377\377\177', 'application/data'], [0, 'string', '=', '\377\377|', 'application/data'], [0, 'string', '=', '\377\377~', 'application/data'], [0, 'string', '=', '\033c\033', 'application/data'], [0, 'long', '=', 4553207, 'image/x11'], [0, 'string', '=', '!!\012', 'application/x-prof'], [0, 'short', '=', 1281, 'application/x-locale'], [24, 'belong', '=', 60012, 'application/x-dump'], [24, 'belong', '=', 60011, 'application/x-dump'], [24, 'lelong', '=', 60012, 'application/x-dump'], [24, 'lelong', '=', 60011, 'application/x-dump'], [0, 'string', '=', '\177ELF', 'application/x-executable-file'], [0, 'short', '=', 340, 'application/data'], [0, 'short', '=', 341, 'application/x-executable-file'], [1080, 'leshort', '=', 61267, 'application/x-linux-ext2fs'], [0, 'string', '=', '\366\366\366\366', 'application/x-pc-floppy'], [774, 'beshort', '=', 55998, 'application/data'], [510, 'leshort', '=', 43605, 'application/data'], [1040, 'leshort', '=', 4991, 'application/x-filesystem'], [1040, 'leshort', '=', 5007, 'application/x-filesystem'], [1040, 'leshort', '=', 9320, 'application/x-filesystem'], [1040, 'leshort', '=', 9336, 'application/x-filesystem'], [0, 'string', '=', '-rom1fs-\000', 'application/x-filesystem'], [395, 'string', '=', 'OS/2', 'application/x-bootable'], [0, 'string', '=', 'FONT', 'font/x-vfont'], [0, 'short', '=', 436, 'font/x-vfont'], [0, 'short', '=', 17001, 'font/x-vfont'], [0, 'string', '=', '%!PS-AdobeFont-1.0', 'font/type1'], [6, 'string', '=', '%!PS-AdobeFont-1.0', 'font/type1'], [0, 'belong', '=', 4, 'font/x-snf'], [0, 'lelong', '=', 4, 'font/x-snf'], [0, 'string', '=', 'STARTFONT font/x-bdf', ''], [0, 'string', '=', '\001fcp', 'font/x-pcf'], [0, 'string', '=', 'D1.0\015', 'font/x-speedo'], [0, 'string', '=', 'flf', 'font/x-figlet'], [0, 'string', '=', 'flc', 'application/x-font'], [0, 'belong', '=', 335698201, 'font/x-libgrx'], [0, 'belong', '=', 4282797902, 'font/x-dos'], [7, 'belong', '=', 4540225, 'font/x-dos'], [7, 'belong', '=', 5654852, 'font/x-dos'], [4098, 'string', '=', 'DOSFONT', 'font/x-dos'], [0, 'string', '=', '', 'archive'], [0, 'string', '=', 'FORM', 'IFF data'], [0, 'string', '=', 'P1', 'image/x-portable-bitmap'], [0, 'string', '=', 'P2', 'image/x-portable-graymap'], [0, 'string', '=', 'P3', 'image/x-portable-pixmap'], [0, 'string', '=', 'P4', 'image/x-portable-bitmap'], [0, 'string', '=', 'P5', 'image/x-portable-graymap'], [0, 'string', '=', 'P6', 'image/x-portable-pixmap'], [0, 'string', '=', 'IIN1', 'image/tiff'], [0, 'string', '=', 'MM\000*', 'image/tiff'], [0, 'string', '=', 'II*\000', 'image/tiff'], [0, 'string', '=', '\211PNG', 'image/x-png'], [1, 'string', '=', 'PNG', 'image/x-png'], [0, 'string', '=', 'GIF8', 'image/gif'], [0, 'string', '=', '\361\000@\273', 'image/x-cmu-raster'], [0, 'string', '=', 'id=ImageMagick', 'MIFF image data'], [0, 'long', '=', 1123028772, 'Artisan image data'], [0, 'string', '=', '#FIG', 'FIG image text'], [0, 'string', '=', 'ARF_BEGARF', 'PHIGS clear text archive'], [0, 'string', '=', '@(#)SunPHIGS', 'SunPHIGS'], [0, 'string', '=', 'GKSM', 'GKS Metafile'], [0, 'string', '=', 'BEGMF', 'clear text Computer Graphics Metafile'], [0, 'beshort&0xffe0', '=', 32, 'binary Computer Graphics Metafile'], [0, 'beshort', '=', 12320, 'character Computer Graphics Metafile'], [0, 'string', '=', 'yz', 'MGR bitmap, modern format, 8-bit aligned'], [0, 'string', '=', 'zz', 'MGR bitmap, old format, 1-bit deep, 16-bit aligned'], [0, 'string', '=', 'xz', 'MGR bitmap, old format, 1-bit deep, 32-bit aligned'], [0, 'string', '=', 'yx', 'MGR bitmap, modern format, squeezed'], [0, 'string', '=', '%bitmap\000', 'FBM image data'], [1, 'string', '=', 'PC Research, Inc', 'group 3 fax data'], [0, 'beshort', '=', 65496, 'image/jpeg'], [0, 'string', '=', 'hsi1', 'image/x-jpeg-proprietary'], [0, 'string', '=', 'BM', 'image/x-bmp'], [0, 'string', '=', 'IC', 'image/x-ico'], [0, 'string', '=', 'PI', 'PC pointer image data'], [0, 'string', '=', 'CI', 'PC color icon data'], [0, 'string', '=', 'CP', 'PC color pointer image data'], [0, 'string', '=', '/* XPM */', 'X pixmap image text'], [0, 'leshort', '=', 52306, 'RLE image data,'], [0, 'string', '=', 'Imagefile version-', 'iff image data'], [0, 'belong', '=', 1504078485, 'x/x-image-sun-raster'], [0, 'beshort', '=', 474, 'x/x-image-sgi'], [0, 'string', '=', 'IT01', 'FIT image data'], [0, 'string', '=', 'IT02', 'FIT image data'], [2048, 'string', '=', 'PCD_IPI', 'x/x-photo-cd-pack-file'], [0, 'string', '=', 'PCD_OPA', 'x/x-photo-cd-overfiew-file'], [0, 'string', '=', 'SIMPLE =', 'FITS image data'], [0, 'string', '=', 'This is a BitMap file', 'Lisp Machine bit-array-file'], [0, 'string', '=', '!!', 'Bennet Yee\'s "face" format'], [0, 'beshort', '=', 4112, 'PEX Binary Archive'], [3000, 'string', '=', 'Visio (TM) Drawing', '%s'], [0, 'leshort', '=', 502, 'basic-16 executable'], [0, 'leshort', '=', 503, 'basic-16 executable (TV)'], [0, 'leshort', '=', 510, 'application/x-executable-file'], [0, 'leshort', '=', 511, 'application/x-executable-file'], [0, 'leshort', '=', 512, 'application/x-executable-file'], [0, 'leshort', '=', 522, 'application/x-executable-file'], [0, 'leshort', '=', 514, 'application/x-executable-file'], [0, 'string', '=', '\210OPS', 'Interleaf saved data'], [0, 'string', '=', '', 'Compiled SGML rules file'], [0, 'string', '=', '', 'A/E SGML Document binary'], [0, 'string', '=', '', 'A/E SGML binary styles file'], [0, 'short', '=', 49374, 'Compiled PSI (v1) data'], [0, 'short', '=', 49370, 'Compiled PSI (v2) data'], [0, 'short', '=', 125252, 'SoftQuad DESC or font file binary'], [0, 'string', '=', 'SQ BITMAP1', 'SoftQuad Raster Format text'], [0, 'string', '=', 'X SoftQuad', 'troff Context intermediate'], [0, 'belong&077777777', '=', 600413, 'sparc demand paged'], [0, 'belong&077777777', '=', 600410, 'sparc pure'], [0, 'belong&077777777', '=', 600407, 'sparc'], [0, 'belong&077777777', '=', 400413, 'mc68020 demand paged'], [0, 'belong&077777777', '=', 400410, 'mc68020 pure'], [0, 'belong&077777777', '=', 400407, 'mc68020'], [0, 'belong&077777777', '=', 200413, 'mc68010 demand paged'], [0, 'belong&077777777', '=', 200410, 'mc68010 pure'], [0, 'belong&077777777', '=', 200407, 'mc68010'], [0, 'belong', '=', 407, 'old sun-2 executable'], [0, 'belong', '=', 410, 'old sun-2 pure executable'], [0, 'belong', '=', 413, 'old sun-2 demand paged executable'], [0, 'belong', '=', 525398, 'SunOS core file'], [0, 'long', '=', 4197695630, 'SunPC 4.0 Hard Disk'], [0, 'string', '=', '#SUNPC_CONFIG', 'SunPC 4.0 Properties Values'], [0, 'string', '=', 'snoop', 'Snoop capture file'], [36, 'string', '=', 'acsp', 'Kodak Color Management System, ICC Profile'], [0, 'string', '=', '#!teapot\012xdr', 'teapot work sheet (XDR format)'], [0, 'string', '=', '\032\001', 'Compiled terminfo entry'], [0, 'short', '=', 433, 'Curses screen image'], [0, 'short', '=', 434, 'Curses screen image'], [0, 'string', '=', '\367\002', 'TeX DVI file'], [0, 'string', '=', '\367\203', 'font/x-tex'], [0, 'string', '=', '\367Y', 'font/x-tex'], [0, 'string', '=', '\367\312', 'font/x-tex'], [0, 'string', '=', 'This is TeX,', 'TeX transcript text'], [0, 'string', '=', 'This is METAFONT,', 'METAFONT transcript text'], [2, 'string', '=', '\000\021', 'font/x-tex-tfm'], [2, 'string', '=', '\000\022', 'font/x-tex-tfm'], [0, 'string', '=', '\\\\input\\', 'texinfo Texinfo source text'], [0, 'string', '=', 'This is Info file', 'GNU Info text'], [0, 'string', '=', '\\\\input', 'TeX document text'], [0, 'string', '=', '\\\\section', 'LaTeX document text'], [0, 'string', '=', '\\\\setlength', 'LaTeX document text'], [0, 'string', '=', '\\\\documentstyle', 'LaTeX document text'], [0, 'string', '=', '\\\\chapter', 'LaTeX document text'], [0, 'string', '=', '\\\\documentclass', 'LaTeX 2e document text'], [0, 'string', '=', '\\\\relax', 'LaTeX auxiliary file'], [0, 'string', '=', '\\\\contentsline', 'LaTeX table of contents'], [0, 'string', '=', '\\\\indexentry', 'LaTeX raw index file'], [0, 'string', '=', '\\\\begin{theindex}', 'LaTeX sorted index'], [0, 'string', '=', '\\\\glossaryentry', 'LaTeX raw glossary'], [0, 'string', '=', '\\\\begin{theglossary}', 'LaTeX sorted glossary'], [0, 'string', '=', 'This is makeindex', 'Makeindex log file'], [0, 'string', '=', '**TI82**', 'TI-82 Graphing Calculator'], [0, 'string', '=', '**TI83**', 'TI-83 Graphing Calculator'], [0, 'string', '=', '**TI85**', 'TI-85 Graphing Calculator'], [0, 'string', '=', '**TI92**', 'TI-92 Graphing Calculator'], [0, 'string', '=', '**TI80**', 'TI-80 Graphing Calculator File.'], [0, 'string', '=', '**TI81**', 'TI-81 Graphing Calculator File.'], [0, 'string', '=', 'TZif', 'timezone data'], [0, 'string', '=', '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000', 'old timezone data'], [0, 'string', '=', '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002\000', 'old timezone data'], [0, 'string', '=', '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\003\000', 'old timezone data'], [0, 'string', '=', '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\004\000', 'old timezone data'], [0, 'string', '=', '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\005\000', 'old timezone data'], [0, 'string', '=', '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\006\000', 'old timezone data'], [0, 'string', '=', '.\\\\"', 'troff or preprocessor input text'], [0, 'string', '=', '\'\\\\"', 'troff or preprocessor input text'], [0, 'string', '=', '\'.\\\\"', 'troff or preprocessor input text'], [0, 'string', '=', '\\\\"', 'troff or preprocessor input text'], [0, 'string', '=', 'x T', 'ditroff text'], [0, 'string', '=', '@\357', 'very old (C/A/T) troff output data'], [0, 'string', '=', 'Interpress/Xerox', 'Xerox InterPress data'], [0, 'short', '=', 263, 'unknown machine executable'], [0, 'short', '=', 264, 'unknown pure executable'], [0, 'short', '=', 265, 'PDP-11 separate I&D'], [0, 'short', '=', 267, 'unknown pure executable'], [0, 'long', '=', 268, 'unknown demand paged pure executable'], [0, 'long', '=', 269, 'unknown demand paged pure executable'], [0, 'long', '=', 270, 'unknown readable demand paged pure executable'], [0, 'string', '=', 'begin uuencoded', 'or xxencoded text'], [0, 'string', '=', 'xbtoa Begin', "btoa'd text"], [0, 'string', '=', '$\012ship', "ship'd binary text"], [0, 'string', '=', 'Decode the following with bdeco', 'bencoded News text'], [11, 'string', '=', 'must be converted with BinHex', 'BinHex binary text'], [0, 'short', '=', 610, 'Perkin-Elmer executable'], [0, 'beshort', '=', 572, 'amd 29k coff noprebar executable'], [0, 'beshort', '=', 1572, 'amd 29k coff prebar executable'], [0, 'beshort', '=', 160007, 'amd 29k coff archive'], [6, 'beshort', '=', 407, 'unicos (cray) executable'], [596, 'string', '=', 'X\337\377\377', 'Ultrix core file'], [0, 'string', '=', 'Joy!peffpwpc', 'header for PowerPC PEF executable'], [0, 'lelong', '=', 101557, 'VAX single precision APL workspace'], [0, 'lelong', '=', 101556, 'VAX double precision APL workspace'], [0, 'lelong', '=', 407, 'VAX executable'], [0, 'lelong', '=', 410, 'VAX pure executable'], [0, 'lelong', '=', 413, 'VAX demand paged pure executable'], [0, 'leshort', '=', 570, 'VAX COFF executable'], [0, 'leshort', '=', 575, 'VAX COFF pure executable'], [0, 'string', '=', 'LBLSIZE=', 'VICAR image data'], [43, 'string', '=', 'SFDU_LABEL', 'VICAR label file'], [0, 'short', '=', 21845, 'VISX image file'], [0, 'string', '=', '\260\0000\000', 'VMS VAX executable'], [0, 'belong', '=', 50331648, 'VMS Alpha executable'], [1, 'string', '=', 'WPC', '(Corel/WP)'], [0, 'string', '=', 'core', 'core file (Xenix)'], [0, 'byte', '=', 128, '8086 relocatable (Microsoft)'], [0, 'leshort', '=', 65381, 'x.out'], [0, 'leshort', '=', 518, 'Microsoft a.out'], [0, 'leshort', '=', 320, 'old Microsoft 8086 x.out'], [0, 'lelong', '=', 518, 'b.out'], [0, 'leshort', '=', 1408, 'XENIX 8086 relocatable or 80286 small model'], [0, 'long', '=', 59399, 'object file (z8000 a.out)'], [0, 'long', '=', 59400, 'pure object file (z8000 a.out)'], [0, 'long', '=', 59401, 'separate object file (z8000 a.out)'], [0, 'long', '=', 59397, 'overlay object file (z8000 a.out)'], [0, 'string', '=', 'ZyXEL\002', 'ZyXEL voice data'], ] magic_tests = [] for record in magic_database: magic_tests.append(MagicTest(record[0], record[1], record[2], record[3], record[4])) def guess_type(filename): """ Guess the mimetype of a file based on its filename. @param filename: File name. @return: Mimetype string or description, when appropriate mime not available. """ if not os.path.isfile(filename): logging.debug('%s is not a file', filename) return None try: data = open(filename, 'r').read(8192) except Exception as e: logging.error(str(e)) return None for test in magic_tests: type = test.compare(data) if type: return type # No matching magic number in the database. is it binary or text? for c in data: if ord(c) > 128: # Non ASCII (binary) data return 'Data' # ASCII, do some text tests if string.find('The', data, 0, 8192) > -1: return 'English text' if string.find('def', data, 0, 8192) > -1: return 'Python Source' return 'ASCII text' if __name__ == '__main__': parser = optparse.OptionParser("usage: %prog [options] [filenames]") options, args = parser.parse_args() logging_manager.configure_logging(MagicLoggingConfig(), verbose=True) if not args: parser.print_help() sys.exit(1) for arg in args: msg = None if os.path.isfile(arg): msg = guess_type(arg) if msg: logging.info('%s: %s', arg, msg) else: logging.info('%s: unknown', arg)