#!/usr/bin/python """ 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 """ import logging, optparse, os, re, sys, string, struct import logging_config 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 = long(1) if n[:1] == 'x': n = '0' + n if n[:2] == '0x': # hex n = string.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 = string.atol(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 = [ [0L, 'leshort', '=', 1538L, 'application/x-alan-adventure-game'], [0L, 'string', '=', 'TADS', 'application/x-tads-game'], [0L, 'short', '=', 420L, 'application/x-executable-file'], [0L, 'short', '=', 421L, 'application/x-executable-file'], [0L, 'leshort', '=', 603L, 'application/x-executable-file'], [0L, 'string', '=', 'Core\001', 'application/x-executable-file'], [0L, 'string', '=', 'AMANDA: TAPESTART DATE', 'application/x-amanda-header'], [0L, 'belong', '=', 1011L, 'application/x-executable-file'], [0L, 'belong', '=', 999L, 'application/x-library-file'], [0L, 'belong', '=', 435L, 'video/mpeg'], [0L, 'belong', '=', 442L, 'video/mpeg'], [0L, 'beshort&0xfff0', '=', 65520L, 'audio/mpeg'], [4L, 'leshort', '=', 44817L, 'video/fli'], [4L, 'leshort', '=', 44818L, 'video/flc'], [0L, 'string', '=', 'MOVI', 'video/x-sgi-movie'], [4L, 'string', '=', 'moov', 'video/quicktime'], [4L, 'string', '=', 'mdat', 'video/quicktime'], [0L, 'long', '=', 100554L, 'application/x-apl-workspace'], [0L, 'string', '=', 'FiLeStArTfIlEsTaRt', 'text/x-apple-binscii'], [0L, 'string', '=', '\012GL', 'application/data'], [0L, 'string', '=', 'v\377', 'application/data'], [0L, 'string', '=', 'NuFile', 'application/data'], [0L, 'string', '=', 'N\365F\351l\345', 'application/data'], [0L, 'belong', '=', 333312L, 'application/data'], [0L, 'belong', '=', 333319L, 'application/data'], [257L, 'string', '=', 'ustar\000', 'application/x-tar'], [257L, 'string', '=', 'ustar \000', 'application/x-gtar'], [0L, 'short', '=', 70707L, 'application/x-cpio'], [0L, 'short', '=', 143561L, 'application/x-bcpio'], [0L, 'string', '=', '070707', 'application/x-cpio'], [0L, 'string', '=', '070701', 'application/x-cpio'], [0L, 'string', '=', '070702', 'application/x-cpio'], [0L, 'string', '=', '!\012debian', 'application/x-dpkg'], [0L, 'string', '=', '\xed\xab\xee\xdb', 'application/x-rpm'], [0L, 'long', '=', 177555L, 'application/x-ar'], [0L, 'short', '=', 177555L, 'application/data'], [0L, 'long', '=', 177545L, 'application/data'], [0L, 'short', '=', 177545L, 'application/data'], [0L, 'long', '=', 100554L, 'application/x-apl-workspace'], [0L, 'string', '=', '', 'application/x-ar'], [0L, 'string', '=', '!\012__________E', 'application/x-ar'], [0L, 'string', '=', '-h-', 'application/data'], [0L, 'string', '=', '!', 'application/x-ar'], [0L, 'string', '=', '', 'application/x-ar'], [0L, 'string', '=', '', 'application/x-ar'], [0L, 'belong', '=', 1711210496L, 'application/x-ar'], [0L, 'belong', '=', 1013019198L, 'application/x-ar'], [0L, 'long', '=', 557605234L, 'application/x-ar'], [0L, 'lelong', '=', 177555L, 'application/data'], [0L, 'leshort', '=', 177555L, 'application/data'], [0L, 'lelong', '=', 177545L, 'application/data'], [0L, 'leshort', '=', 177545L, 'application/data'], [0L, 'lelong', '=', 236525L, 'application/data'], [0L, 'lelong', '=', 236526L, 'application/data'], [0L, 'lelong&0x8080ffff', '=', 2074L, 'application/x-arc'], [0L, 'lelong&0x8080ffff', '=', 2330L, 'application/x-arc'], [0L, 'lelong&0x8080ffff', '=', 538L, 'application/x-arc'], [0L, 'lelong&0x8080ffff', '=', 794L, 'application/x-arc'], [0L, 'lelong&0x8080ffff', '=', 1050L, 'application/x-arc'], [0L, 'lelong&0x8080ffff', '=', 1562L, 'application/x-arc'], [0L, 'string', '=', '\032archive', 'application/data'], [0L, 'leshort', '=', 60000L, 'application/x-arj'], [0L, 'string', '=', 'HPAK', 'application/data'], [0L, 'string', '=', '\351,\001JAM application/data', ''], [2L, 'string', '=', '-lh0-', 'application/x-lha'], [2L, 'string', '=', '-lh1-', 'application/x-lha'], [2L, 'string', '=', '-lz4-', 'application/x-lha'], [2L, 'string', '=', '-lz5-', 'application/x-lha'], [2L, 'string', '=', '-lzs-', 'application/x-lha'], [2L, 'string', '=', '-lh -', 'application/x-lha'], [2L, 'string', '=', '-lhd-', 'application/x-lha'], [2L, 'string', '=', '-lh2-', 'application/x-lha'], [2L, 'string', '=', '-lh3-', 'application/x-lha'], [2L, 'string', '=', '-lh4-', 'application/x-lha'], [2L, 'string', '=', '-lh5-', 'application/x-lha'], [0L, 'string', '=', 'Rar!', 'application/x-rar'], [0L, 'string', '=', 'SQSH', 'application/data'], [0L, 'string', '=', 'UC2\032', 'application/data'], [0L, 'string', '=', 'PK\003\004', 'application/zip'], [20L, 'lelong', '=', 4257523676L, 'application/x-zoo'], [10L, 'string', '=', '# This is a shell archive', 'application/x-shar'], [0L, 'string', '=', '*STA', 'application/data'], [0L, 'string', '=', '2278', 'application/data'], [0L, 'beshort', '=', 560L, 'application/x-executable-file'], [0L, 'beshort', '=', 561L, 'application/x-executable-file'], [0L, 'string', '=', '\000\004\036\212\200', 'application/core'], [0L, 'string', '=', '.snd', 'audio/basic'], [0L, 'lelong', '=', 6583086L, 'audio/basic'], [0L, 'string', '=', 'MThd', 'audio/midi'], [0L, 'string', '=', 'CTMF', 'audio/x-cmf'], [0L, 'string', '=', 'SBI', 'audio/x-sbi'], [0L, 'string', '=', 'Creative Voice File', 'audio/x-voc'], [0L, 'belong', '=', 1314148939L, 'audio/x-multitrack'], [0L, 'string', '=', 'RIFF', 'audio/x-wav'], [0L, 'string', '=', 'EMOD', 'audio/x-emod'], [0L, 'belong', '=', 779248125L, 'audio/x-pn-realaudio'], [0L, 'string', '=', 'MTM', 'audio/x-multitrack'], [0L, 'string', '=', 'if', 'audio/x-669-mod'], [0L, 'string', '=', 'FAR', 'audio/mod'], [0L, 'string', '=', 'MAS_U', 'audio/x-multimate-mod'], [44L, 'string', '=', 'SCRM', 'audio/x-st3-mod'], [0L, 'string', '=', 'GF1PATCH110\000ID#000002\000', 'audio/x-gus-patch'], [0L, 'string', '=', 'GF1PATCH100\000ID#000002\000', 'audio/x-gus-patch'], [0L, 'string', '=', 'JN', 'audio/x-669-mod'], [0L, 'string', '=', 'UN05', 'audio/x-mikmod-uni'], [0L, 'string', '=', 'Extended Module:', 'audio/x-ft2-mod'], [21L, 'string', '=', '!SCREAM!', 'audio/x-st2-mod'], [1080L, 'string', '=', 'M.K.', 'audio/x-protracker-mod'], [1080L, 'string', '=', 'M!K!', 'audio/x-protracker-mod'], [1080L, 'string', '=', 'FLT4', 'audio/x-startracker-mod'], [1080L, 'string', '=', '4CHN', 'audio/x-fasttracker-mod'], [1080L, 'string', '=', '6CHN', 'audio/x-fasttracker-mod'], [1080L, 'string', '=', '8CHN', 'audio/x-fasttracker-mod'], [1080L, 'string', '=', 'CD81', 'audio/x-oktalyzer-mod'], [1080L, 'string', '=', 'OKTA', 'audio/x-oktalyzer-mod'], [1080L, 'string', '=', '16CN', 'audio/x-taketracker-mod'], [1080L, 'string', '=', '32CN', 'audio/x-taketracker-mod'], [0L, 'string', '=', 'TOC', 'audio/x-toc'], [0L, 'short', '=', 3401L, 'application/x-executable-file'], [0L, 'long', '=', 406L, 'application/x-executable-file'], [0L, 'short', '=', 406L, 'application/x-executable-file'], [0L, 'short', '=', 3001L, 'application/x-executable-file'], [0L, 'lelong', '=', 314L, 'application/x-executable-file'], [0L, 'string', '=', '//', 'text/cpp'], [0L, 'string', '=', '\\\\1cw\\', 'application/data'], [0L, 'string', '=', '\\\\1cw', 'application/data'], [0L, 'belong&0xffffff00', '=', 2231440384L, 'application/data'], [0L, 'belong&0xffffff00', '=', 2231487232L, 'application/data'], [0L, 'short', '=', 575L, 'application/x-executable-file'], [0L, 'short', '=', 577L, 'application/x-executable-file'], [4L, 'string', '=', 'pipe', 'application/data'], [4L, 'string', '=', 'prof', 'application/data'], [0L, 'string', '=', ': shell', 'application/data'], [0L, 'string', '=', '#!/bin/sh', 'application/x-sh'], [0L, 'string', '=', '#! /bin/sh', 'application/x-sh'], [0L, 'string', '=', '#! /bin/sh', 'application/x-sh'], [0L, 'string', '=', '#!/bin/csh', 'application/x-csh'], [0L, 'string', '=', '#! /bin/csh', 'application/x-csh'], [0L, 'string', '=', '#! /bin/csh', 'application/x-csh'], [0L, 'string', '=', '#!/bin/ksh', 'application/x-ksh'], [0L, 'string', '=', '#! /bin/ksh', 'application/x-ksh'], [0L, 'string', '=', '#! /bin/ksh', 'application/x-ksh'], [0L, 'string', '=', '#!/bin/tcsh', 'application/x-csh'], [0L, 'string', '=', '#! /bin/tcsh', 'application/x-csh'], [0L, 'string', '=', '#! /bin/tcsh', 'application/x-csh'], [0L, 'string', '=', '#!/usr/local/tcsh', 'application/x-csh'], [0L, 'string', '=', '#! /usr/local/tcsh', 'application/x-csh'], [0L, 'string', '=', '#!/usr/local/bin/tcsh', 'application/x-csh'], [0L, 'string', '=', '#! /usr/local/bin/tcsh', 'application/x-csh'], [0L, 'string', '=', '#! /usr/local/bin/tcsh', 'application/x-csh'], [0L, 'string', '=', '#!/usr/local/bin/zsh', 'application/x-zsh'], [0L, 'string', '=', '#! /usr/local/bin/zsh', 'application/x-zsh'], [0L, 'string', '=', '#! /usr/local/bin/zsh', 'application/x-zsh'], [0L, 'string', '=', '#!/usr/local/bin/ash', 'application/x-sh'], [0L, 'string', '=', '#! /usr/local/bin/ash', 'application/x-zsh'], [0L, 'string', '=', '#! /usr/local/bin/ash', 'application/x-zsh'], [0L, 'string', '=', '#!/usr/local/bin/ae', 'text/script'], [0L, 'string', '=', '#! /usr/local/bin/ae', 'text/script'], [0L, 'string', '=', '#! /usr/local/bin/ae', 'text/script'], [0L, 'string', '=', '#!/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#! /bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#! /bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#!/usr/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#!/usr/local/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/local/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/local/bin/nawk', 'application/x-awk'], [0L, 'string', '=', '#!/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#! /bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#! /bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#!/usr/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#!/usr/local/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/local/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/local/bin/gawk', 'application/x-awk'], [0L, 'string', '=', '#!/bin/awk', 'application/x-awk'], [0L, 'string', '=', '#! /bin/awk', 'application/x-awk'], [0L, 'string', '=', '#! /bin/awk', 'application/x-awk'], [0L, 'string', '=', '#!/usr/bin/awk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/bin/awk', 'application/x-awk'], [0L, 'string', '=', '#! /usr/bin/awk', 'application/x-awk'], [0L, 'string', '=', 'BEGIN', 'application/x-awk'], [0L, 'string', '=', '#!/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#! /bin/perl', 'application/x-perl'], [0L, 'string', '=', '#! /bin/perl', 'application/x-perl'], [0L, 'string', '=', 'eval "exec /bin/perl', 'application/x-perl'], [0L, 'string', '=', '#!/usr/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#! /usr/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#! /usr/bin/perl', 'application/x-perl'], [0L, 'string', '=', 'eval "exec /usr/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#!/usr/local/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#! /usr/local/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#! /usr/local/bin/perl', 'application/x-perl'], [0L, 'string', '=', 'eval "exec /usr/local/bin/perl', 'application/x-perl'], [0L, 'string', '=', '#!/bin/python', 'application/x-python'], [0L, 'string', '=', '#! /bin/python', 'application/x-python'], [0L, 'string', '=', '#! /bin/python', 'application/x-python'], [0L, 'string', '=', 'eval "exec /bin/python', 'application/x-python'], [0L, 'string', '=', '#!/usr/bin/python', 'application/x-python'], [0L, 'string', '=', '#! /usr/bin/python', 'application/x-python'], [0L, 'string', '=', '#! /usr/bin/python', 'application/x-python'], [0L, 'string', '=', 'eval "exec /usr/bin/python', 'application/x-python'], [0L, 'string', '=', '#!/usr/local/bin/python', 'application/x-python'], [0L, 'string', '=', '#! /usr/local/bin/python', 'application/x-python'], [0L, 'string', '=', '#! /usr/local/bin/python', 'application/x-python'], [0L, 'string', '=', 'eval "exec /usr/local/bin/python', 'application/x-python'], [0L, 'string', '=', '#!/usr/bin/env python', 'application/x-python'], [0L, 'string', '=', '#! /usr/bin/env python', 'application/x-python'], [0L, 'string', '=', '#!/bin/rc', 'text/script'], [0L, 'string', '=', '#! /bin/rc', 'text/script'], [0L, 'string', '=', '#! /bin/rc', 'text/script'], [0L, 'string', '=', '#!/bin/bash', 'application/x-sh'], [0L, 'string', '=', '#! /bin/bash', 'application/x-sh'], [0L, 'string', '=', '#! /bin/bash', 'application/x-sh'], [0L, 'string', '=', '#!/usr/local/bin/bash', 'application/x-sh'], [0L, 'string', '=', '#! /usr/local/bin/bash', 'application/x-sh'], [0L, 'string', '=', '#! /usr/local/bin/bash', 'application/x-sh'], [0L, 'string', '=', '#! /', 'text/script'], [0L, 'string', '=', '#! /', 'text/script'], [0L, 'string', '=', '#!/', 'text/script'], [0L, 'string', '=', '#! text/script', ''], [0L, 'string', '=', '\037\235', 'application/compress'], [0L, 'string', '=', '\037\213', 'application/x-gzip'], [0L, 'string', '=', '\037\036', 'application/data'], [0L, 'short', '=', 17437L, 'application/data'], [0L, 'short', '=', 8191L, 'application/data'], [0L, 'string', '=', '\377\037', 'application/data'], [0L, 'short', '=', 145405L, 'application/data'], [0L, 'string', '=', 'BZh', 'application/x-bzip2'], [0L, 'leshort', '=', 65398L, 'application/data'], [0L, 'leshort', '=', 65142L, 'application/data'], [0L, 'leshort', '=', 64886L, 'application/x-lzh'], [0L, 'string', '=', '\037\237', 'application/data'], [0L, 'string', '=', '\037\236', 'application/data'], [0L, 'string', '=', '\037\240', 'application/data'], [0L, 'string', '=', 'BZ', 'application/x-bzip'], [0L, 'string', '=', '\211LZO\000\015\012\032\012', 'application/data'], [0L, 'belong', '=', 507L, 'application/x-object-file'], [0L, 'belong', '=', 513L, 'application/x-executable-file'], [0L, 'belong', '=', 515L, 'application/x-executable-file'], [0L, 'belong', '=', 517L, 'application/x-executable-file'], [0L, 'belong', '=', 70231L, 'application/core'], [24L, 'belong', '=', 60011L, 'application/data'], [24L, 'belong', '=', 60012L, 'application/data'], [24L, 'belong', '=', 60013L, 'application/data'], [24L, 'belong', '=', 60014L, 'application/data'], [0L, 'belong', '=', 601L, 'application/x-object-file'], [0L, 'belong', '=', 607L, 'application/data'], [0L, 'belong', '=', 324508366L, 'application/x-gdbm'], [0L, 'lelong', '=', 324508366L, 'application/x-gdbm'], [0L, 'string', '=', 'GDBM', 'application/x-gdbm'], [0L, 'belong', '=', 398689L, 'application/x-db'], [0L, 'belong', '=', 340322L, 'application/x-db'], [0L, 'string', '=', '\012\012________64E', 'application/data'], [0L, 'leshort', '=', 387L, 'application/x-executable-file'], [0L, 'leshort', '=', 392L, 'application/x-executable-file'], [0L, 'leshort', '=', 399L, 'application/x-object-file'], [0L, 'string', '=', '\377\377\177', 'application/data'], [0L, 'string', '=', '\377\377|', 'application/data'], [0L, 'string', '=', '\377\377~', 'application/data'], [0L, 'string', '=', '\033c\033', 'application/data'], [0L, 'long', '=', 4553207L, 'image/x11'], [0L, 'string', '=', '!!\012', 'application/x-prof'], [0L, 'short', '=', 1281L, 'application/x-locale'], [24L, 'belong', '=', 60012L, 'application/x-dump'], [24L, 'belong', '=', 60011L, 'application/x-dump'], [24L, 'lelong', '=', 60012L, 'application/x-dump'], [24L, 'lelong', '=', 60011L, 'application/x-dump'], [0L, 'string', '=', '\177ELF', 'application/x-executable-file'], [0L, 'short', '=', 340L, 'application/data'], [0L, 'short', '=', 341L, 'application/x-executable-file'], [1080L, 'leshort', '=', 61267L, 'application/x-linux-ext2fs'], [0L, 'string', '=', '\366\366\366\366', 'application/x-pc-floppy'], [774L, 'beshort', '=', 55998L, 'application/data'], [510L, 'leshort', '=', 43605L, 'application/data'], [1040L, 'leshort', '=', 4991L, 'application/x-filesystem'], [1040L, 'leshort', '=', 5007L, 'application/x-filesystem'], [1040L, 'leshort', '=', 9320L, 'application/x-filesystem'], [1040L, 'leshort', '=', 9336L, 'application/x-filesystem'], [0L, 'string', '=', '-rom1fs-\000', 'application/x-filesystem'], [395L, 'string', '=', 'OS/2', 'application/x-bootable'], [0L, 'string', '=', 'FONT', 'font/x-vfont'], [0L, 'short', '=', 436L, 'font/x-vfont'], [0L, 'short', '=', 17001L, 'font/x-vfont'], [0L, 'string', '=', '%!PS-AdobeFont-1.0', 'font/type1'], [6L, 'string', '=', '%!PS-AdobeFont-1.0', 'font/type1'], [0L, 'belong', '=', 4L, 'font/x-snf'], [0L, 'lelong', '=', 4L, 'font/x-snf'], [0L, 'string', '=', 'STARTFONT font/x-bdf', ''], [0L, 'string', '=', '\001fcp', 'font/x-pcf'], [0L, 'string', '=', 'D1.0\015', 'font/x-speedo'], [0L, 'string', '=', 'flf', 'font/x-figlet'], [0L, 'string', '=', 'flc', 'application/x-font'], [0L, 'belong', '=', 335698201L, 'font/x-libgrx'], [0L, 'belong', '=', 4282797902L, 'font/x-dos'], [7L, 'belong', '=', 4540225L, 'font/x-dos'], [7L, 'belong', '=', 5654852L, 'font/x-dos'], [4098L, 'string', '=', 'DOSFONT', 'font/x-dos'], [0L, 'string', '=', '', 'archive'], [0L, 'string', '=', 'FORM', 'IFF data'], [0L, 'string', '=', 'P1', 'image/x-portable-bitmap'], [0L, 'string', '=', 'P2', 'image/x-portable-graymap'], [0L, 'string', '=', 'P3', 'image/x-portable-pixmap'], [0L, 'string', '=', 'P4', 'image/x-portable-bitmap'], [0L, 'string', '=', 'P5', 'image/x-portable-graymap'], [0L, 'string', '=', 'P6', 'image/x-portable-pixmap'], [0L, 'string', '=', 'IIN1', 'image/tiff'], [0L, 'string', '=', 'MM\000*', 'image/tiff'], [0L, 'string', '=', 'II*\000', 'image/tiff'], [0L, 'string', '=', '\211PNG', 'image/x-png'], [1L, 'string', '=', 'PNG', 'image/x-png'], [0L, 'string', '=', 'GIF8', 'image/gif'], [0L, 'string', '=', '\361\000@\273', 'image/x-cmu-raster'], [0L, 'string', '=', 'id=ImageMagick', 'MIFF image data'], [0L, 'long', '=', 1123028772L, 'Artisan image data'], [0L, 'string', '=', '#FIG', 'FIG image text'], [0L, 'string', '=', 'ARF_BEGARF', 'PHIGS clear text archive'], [0L, 'string', '=', '@(#)SunPHIGS', 'SunPHIGS'], [0L, 'string', '=', 'GKSM', 'GKS Metafile'], [0L, 'string', '=', 'BEGMF', 'clear text Computer Graphics Metafile'], [0L, 'beshort&0xffe0', '=', 32L, 'binary Computer Graphics Metafile'], [0L, 'beshort', '=', 12320L, 'character Computer Graphics Metafile'], [0L, 'string', '=', 'yz', 'MGR bitmap, modern format, 8-bit aligned'], [0L, 'string', '=', 'zz', 'MGR bitmap, old format, 1-bit deep, 16-bit aligned'], [0L, 'string', '=', 'xz', 'MGR bitmap, old format, 1-bit deep, 32-bit aligned'], [0L, 'string', '=', 'yx', 'MGR bitmap, modern format, squeezed'], [0L, 'string', '=', '%bitmap\000', 'FBM image data'], [1L, 'string', '=', 'PC Research, Inc', 'group 3 fax data'], [0L, 'beshort', '=', 65496L, 'image/jpeg'], [0L, 'string', '=', 'hsi1', 'image/x-jpeg-proprietary'], [0L, 'string', '=', 'BM', 'image/x-bmp'], [0L, 'string', '=', 'IC', 'image/x-ico'], [0L, 'string', '=', 'PI', 'PC pointer image data'], [0L, 'string', '=', 'CI', 'PC color icon data'], [0L, 'string', '=', 'CP', 'PC color pointer image data'], [0L, 'string', '=', '/* XPM */', 'X pixmap image text'], [0L, 'leshort', '=', 52306L, 'RLE image data,'], [0L, 'string', '=', 'Imagefile version-', 'iff image data'], [0L, 'belong', '=', 1504078485L, 'x/x-image-sun-raster'], [0L, 'beshort', '=', 474L, 'x/x-image-sgi'], [0L, 'string', '=', 'IT01', 'FIT image data'], [0L, 'string', '=', 'IT02', 'FIT image data'], [2048L, 'string', '=', 'PCD_IPI', 'x/x-photo-cd-pack-file'], [0L, 'string', '=', 'PCD_OPA', 'x/x-photo-cd-overfiew-file'], [0L, 'string', '=', 'SIMPLE =', 'FITS image data'], [0L, 'string', '=', 'This is a BitMap file', 'Lisp Machine bit-array-file'], [0L, 'string', '=', '!!', 'Bennet Yee\'s "face" format'], [0L, 'beshort', '=', 4112L, 'PEX Binary Archive'], [3000L, 'string', '=', 'Visio (TM) Drawing', '%s'], [0L, 'leshort', '=', 502L, 'basic-16 executable'], [0L, 'leshort', '=', 503L, 'basic-16 executable (TV)'], [0L, 'leshort', '=', 510L, 'application/x-executable-file'], [0L, 'leshort', '=', 511L, 'application/x-executable-file'], [0L, 'leshort', '=', 512L, 'application/x-executable-file'], [0L, 'leshort', '=', 522L, 'application/x-executable-file'], [0L, 'leshort', '=', 514L, 'application/x-executable-file'], [0L, 'string', '=', '\210OPS', 'Interleaf saved data'], [0L, 'string', '=', '', 'Compiled SGML rules file'], [0L, 'string', '=', '', 'A/E SGML Document binary'], [0L, 'string', '=', '', 'A/E SGML binary styles file'], [0L, 'short', '=', 49374L, 'Compiled PSI (v1) data'], [0L, 'short', '=', 49370L, 'Compiled PSI (v2) data'], [0L, 'short', '=', 125252L, 'SoftQuad DESC or font file binary'], [0L, 'string', '=', 'SQ BITMAP1', 'SoftQuad Raster Format text'], [0L, 'string', '=', 'X SoftQuad', 'troff Context intermediate'], [0L, 'belong&077777777', '=', 600413L, 'sparc demand paged'], [0L, 'belong&077777777', '=', 600410L, 'sparc pure'], [0L, 'belong&077777777', '=', 600407L, 'sparc'], [0L, 'belong&077777777', '=', 400413L, 'mc68020 demand paged'], [0L, 'belong&077777777', '=', 400410L, 'mc68020 pure'], [0L, 'belong&077777777', '=', 400407L, 'mc68020'], [0L, 'belong&077777777', '=', 200413L, 'mc68010 demand paged'], [0L, 'belong&077777777', '=', 200410L, 'mc68010 pure'], [0L, 'belong&077777777', '=', 200407L, 'mc68010'], [0L, 'belong', '=', 407L, 'old sun-2 executable'], [0L, 'belong', '=', 410L, 'old sun-2 pure executable'], [0L, 'belong', '=', 413L, 'old sun-2 demand paged executable'], [0L, 'belong', '=', 525398L, 'SunOS core file'], [0L, 'long', '=', 4197695630L, 'SunPC 4.0 Hard Disk'], [0L, 'string', '=', '#SUNPC_CONFIG', 'SunPC 4.0 Properties Values'], [0L, 'string', '=', 'snoop', 'Snoop capture file'], [36L, 'string', '=', 'acsp', 'Kodak Color Management System, ICC Profile'], [0L, 'string', '=', '#!teapot\012xdr', 'teapot work sheet (XDR format)'], [0L, 'string', '=', '\032\001', 'Compiled terminfo entry'], [0L, 'short', '=', 433L, 'Curses screen image'], [0L, 'short', '=', 434L, 'Curses screen image'], [0L, 'string', '=', '\367\002', 'TeX DVI file'], [0L, 'string', '=', '\367\203', 'font/x-tex'], [0L, 'string', '=', '\367Y', 'font/x-tex'], [0L, 'string', '=', '\367\312', 'font/x-tex'], [0L, 'string', '=', 'This is TeX,', 'TeX transcript text'], [0L, 'string', '=', 'This is METAFONT,', 'METAFONT transcript text'], [2L, 'string', '=', '\000\021', 'font/x-tex-tfm'], [2L, 'string', '=', '\000\022', 'font/x-tex-tfm'], [0L, 'string', '=', '\\\\input\\', 'texinfo Texinfo source text'], [0L, 'string', '=', 'This is Info file', 'GNU Info text'], [0L, 'string', '=', '\\\\input', 'TeX document text'], [0L, 'string', '=', '\\\\section', 'LaTeX document text'], [0L, 'string', '=', '\\\\setlength', 'LaTeX document text'], [0L, 'string', '=', '\\\\documentstyle', 'LaTeX document text'], [0L, 'string', '=', '\\\\chapter', 'LaTeX document text'], [0L, 'string', '=', '\\\\documentclass', 'LaTeX 2e document text'], [0L, 'string', '=', '\\\\relax', 'LaTeX auxiliary file'], [0L, 'string', '=', '\\\\contentsline', 'LaTeX table of contents'], [0L, 'string', '=', '\\\\indexentry', 'LaTeX raw index file'], [0L, 'string', '=', '\\\\begin{theindex}', 'LaTeX sorted index'], [0L, 'string', '=', '\\\\glossaryentry', 'LaTeX raw glossary'], [0L, 'string', '=', '\\\\begin{theglossary}', 'LaTeX sorted glossary'], [0L, 'string', '=', 'This is makeindex', 'Makeindex log file'], [0L, 'string', '=', '**TI82**', 'TI-82 Graphing Calculator'], [0L, 'string', '=', '**TI83**', 'TI-83 Graphing Calculator'], [0L, 'string', '=', '**TI85**', 'TI-85 Graphing Calculator'], [0L, 'string', '=', '**TI92**', 'TI-92 Graphing Calculator'], [0L, 'string', '=', '**TI80**', 'TI-80 Graphing Calculator File.'], [0L, 'string', '=', '**TI81**', 'TI-81 Graphing Calculator File.'], [0L, 'string', '=', 'TZif', 'timezone data'], [0L, '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'], [0L, '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'], [0L, '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'], [0L, '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'], [0L, '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'], [0L, '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'], [0L, 'string', '=', '.\\\\"', 'troff or preprocessor input text'], [0L, 'string', '=', '\'\\\\"', 'troff or preprocessor input text'], [0L, 'string', '=', '\'.\\\\"', 'troff or preprocessor input text'], [0L, 'string', '=', '\\\\"', 'troff or preprocessor input text'], [0L, 'string', '=', 'x T', 'ditroff text'], [0L, 'string', '=', '@\357', 'very old (C/A/T) troff output data'], [0L, 'string', '=', 'Interpress/Xerox', 'Xerox InterPress data'], [0L, 'short', '=', 263L, 'unknown machine executable'], [0L, 'short', '=', 264L, 'unknown pure executable'], [0L, 'short', '=', 265L, 'PDP-11 separate I&D'], [0L, 'short', '=', 267L, 'unknown pure executable'], [0L, 'long', '=', 268L, 'unknown demand paged pure executable'], [0L, 'long', '=', 269L, 'unknown demand paged pure executable'], [0L, 'long', '=', 270L, 'unknown readable demand paged pure executable'], [0L, 'string', '=', 'begin uuencoded', 'or xxencoded text'], [0L, 'string', '=', 'xbtoa Begin', "btoa'd text"], [0L, 'string', '=', '$\012ship', "ship'd binary text"], [0L, 'string', '=', 'Decode the following with bdeco', 'bencoded News text'], [11L, 'string', '=', 'must be converted with BinHex', 'BinHex binary text'], [0L, 'short', '=', 610L, 'Perkin-Elmer executable'], [0L, 'beshort', '=', 572L, 'amd 29k coff noprebar executable'], [0L, 'beshort', '=', 1572L, 'amd 29k coff prebar executable'], [0L, 'beshort', '=', 160007L, 'amd 29k coff archive'], [6L, 'beshort', '=', 407L, 'unicos (cray) executable'], [596L, 'string', '=', 'X\337\377\377', 'Ultrix core file'], [0L, 'string', '=', 'Joy!peffpwpc', 'header for PowerPC PEF executable'], [0L, 'lelong', '=', 101557L, 'VAX single precision APL workspace'], [0L, 'lelong', '=', 101556L, 'VAX double precision APL workspace'], [0L, 'lelong', '=', 407L, 'VAX executable'], [0L, 'lelong', '=', 410L, 'VAX pure executable'], [0L, 'lelong', '=', 413L, 'VAX demand paged pure executable'], [0L, 'leshort', '=', 570L, 'VAX COFF executable'], [0L, 'leshort', '=', 575L, 'VAX COFF pure executable'], [0L, 'string', '=', 'LBLSIZE=', 'VICAR image data'], [43L, 'string', '=', 'SFDU_LABEL', 'VICAR label file'], [0L, 'short', '=', 21845L, 'VISX image file'], [0L, 'string', '=', '\260\0000\000', 'VMS VAX executable'], [0L, 'belong', '=', 50331648L, 'VMS Alpha executable'], [1L, 'string', '=', 'WPC', '(Corel/WP)'], [0L, 'string', '=', 'core', 'core file (Xenix)'], [0L, 'byte', '=', 128L, '8086 relocatable (Microsoft)'], [0L, 'leshort', '=', 65381L, 'x.out'], [0L, 'leshort', '=', 518L, 'Microsoft a.out'], [0L, 'leshort', '=', 320L, 'old Microsoft 8086 x.out'], [0L, 'lelong', '=', 518L, 'b.out'], [0L, 'leshort', '=', 1408L, 'XENIX 8086 relocatable or 80286 small model'], [0L, 'long', '=', 59399L, 'object file (z8000 a.out)'], [0L, 'long', '=', 59400L, 'pure object file (z8000 a.out)'], [0L, 'long', '=', 59401L, 'separate object file (z8000 a.out)'], [0L, 'long', '=', 59397L, 'overlay object file (z8000 a.out)'], [0L, '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, 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)