magic.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/env python
  2. '''
  3. Python bindings for libmagic
  4. '''
  5. import ctypes
  6. from ctypes import *
  7. from ctypes.util import find_library
  8. def _init():
  9. """
  10. Loads the shared library through ctypes and returns a library
  11. L{ctypes.CDLL} instance
  12. """
  13. return ctypes.cdll.LoadLibrary(find_library('magic'))
  14. _libraries = {}
  15. _libraries['magic'] = _init()
  16. # Flag constants for open and setflags
  17. MAGIC_NONE = NONE = 0
  18. MAGIC_DEBUG = DEBUG = 1
  19. MAGIC_SYMLINK = SYMLINK = 2
  20. MAGIC_COMPRESS = COMPRESS = 4
  21. MAGIC_DEVICES = DEVICES = 8
  22. MAGIC_MIME_TYPE = MIME_TYPE = 16
  23. MAGIC_CONTINUE = CONTINUE = 32
  24. MAGIC_CHECK = CHECK = 64
  25. MAGIC_PRESERVE_ATIME = PRESERVE_ATIME = 128
  26. MAGIC_RAW = RAW = 256
  27. MAGIC_ERROR = ERROR = 512
  28. MAGIC_MIME_ENCODING = MIME_ENCODING = 1024
  29. MAGIC_MIME = MIME = 1040
  30. MAGIC_APPLE = APPLE = 2048
  31. MAGIC_NO_CHECK_COMPRESS = NO_CHECK_COMPRESS = 4096
  32. MAGIC_NO_CHECK_TAR = NO_CHECK_TAR = 8192
  33. MAGIC_NO_CHECK_SOFT = NO_CHECK_SOFT = 16384
  34. MAGIC_NO_CHECK_APPTYPE = NO_CHECK_APPTYPE = 32768
  35. MAGIC_NO_CHECK_ELF = NO_CHECK_ELF = 65536
  36. MAGIC_NO_CHECK_TEXT = NO_CHECK_TEXT = 131072
  37. MAGIC_NO_CHECK_CDF = NO_CHECK_CDF = 262144
  38. MAGIC_NO_CHECK_TOKENS = NO_CHECK_TOKENS = 1048576
  39. MAGIC_NO_CHECK_ENCODING = NO_CHECK_ENCODING = 2097152
  40. MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
  41. class magic_set(Structure):
  42. pass
  43. magic_set._fields_ = []
  44. magic_t = POINTER(magic_set)
  45. _open = _libraries['magic'].magic_open
  46. _open.restype = magic_t
  47. _open.argtypes = [c_int]
  48. _close = _libraries['magic'].magic_close
  49. _close.restype = None
  50. _close.argtypes = [magic_t]
  51. _file = _libraries['magic'].magic_file
  52. _file.restype = c_char_p
  53. _file.argtypes = [magic_t, c_char_p]
  54. _descriptor = _libraries['magic'].magic_descriptor
  55. _descriptor.restype = c_char_p
  56. _descriptor.argtypes = [magic_t, c_int]
  57. _buffer = _libraries['magic'].magic_buffer
  58. _buffer.restype = c_char_p
  59. _buffer.argtypes = [magic_t, c_void_p, c_size_t]
  60. _error = _libraries['magic'].magic_error
  61. _error.restype = c_char_p
  62. _error.argtypes = [magic_t]
  63. _setflags = _libraries['magic'].magic_setflags
  64. _setflags.restype = c_int
  65. _setflags.argtypes = [magic_t, c_int]
  66. _load = _libraries['magic'].magic_load
  67. _load.restype = c_int
  68. _load.argtypes = [magic_t, c_char_p]
  69. _compile = _libraries['magic'].magic_compile
  70. _compile.restype = c_int
  71. _compile.argtypes = [magic_t, c_char_p]
  72. _check = _libraries['magic'].magic_check
  73. _check.restype = c_int
  74. _check.argtypes = [magic_t, c_char_p]
  75. _list = _libraries['magic'].magic_list
  76. _list.restype = c_int
  77. _list.argtypes = [magic_t, c_char_p]
  78. _errno = _libraries['magic'].magic_errno
  79. _errno.restype = c_int
  80. _errno.argtypes = [magic_t]
  81. class Magic(object):
  82. def __init__(self, ms):
  83. self._magic_t = ms
  84. def close(self):
  85. """
  86. Closes the magic database and deallocates any resources used.
  87. """
  88. _close(self._magic_t)
  89. def file(self, filename):
  90. """
  91. Returns a textual description of the contents of the argument passed
  92. as a filename or None if an error occurred and the MAGIC_ERROR flag
  93. is set. A call to errno() will return the numeric error code.
  94. """
  95. try: # attempt python3 approach first
  96. if isinstance(filename, bytes):
  97. bi = filename
  98. else:
  99. bi = bytes(filename, 'utf-8')
  100. return str(_file(self._magic_t, bi), 'utf-8')
  101. except:
  102. return _file(self._magic_t, filename.encode('utf-8'))
  103. def descriptor(self, fd):
  104. """
  105. Like the file method, but the argument is a file descriptor.
  106. """
  107. return _descriptor(self._magic_t, fd)
  108. def buffer(self, buf):
  109. """
  110. Returns a textual description of the contents of the argument passed
  111. as a buffer or None if an error occurred and the MAGIC_ERROR flag
  112. is set. A call to errno() will return the numeric error code.
  113. """
  114. try: # attempt python3 approach first
  115. return str(_buffer(self._magic_t, buf, len(buf)), 'utf-8')
  116. except:
  117. return _buffer(self._magic_t, buf, len(buf))
  118. def error(self):
  119. """
  120. Returns a textual explanation of the last error or None
  121. if there was no error.
  122. """
  123. try: # attempt python3 approach first
  124. return str(_error(self._magic_t), 'utf-8')
  125. except:
  126. return _error(self._magic_t)
  127. def setflags(self, flags):
  128. """
  129. Set flags on the magic object which determine how magic checking
  130. behaves; a bitwise OR of the flags described in libmagic(3), but
  131. without the MAGIC_ prefix.
  132. Returns -1 on systems that don't support utime(2) or utimes(2)
  133. when PRESERVE_ATIME is set.
  134. """
  135. return _setflags(self._magic_t, flags)
  136. def load(self, filename=None):
  137. """
  138. Must be called to load entries in the colon separated list of database
  139. files passed as argument or the default database file if no argument
  140. before any magic queries can be performed.
  141. Returns 0 on success and -1 on failure.
  142. """
  143. return _load(self._magic_t, filename)
  144. def compile(self, dbs):
  145. """
  146. Compile entries in the colon separated list of database files
  147. passed as argument or the default database file if no argument.
  148. Returns 0 on success and -1 on failure.
  149. The compiled files created are named from the basename(1) of each file
  150. argument with ".mgc" appended to it.
  151. """
  152. return _compile(self._magic_t, dbs)
  153. def check(self, dbs):
  154. """
  155. Check the validity of entries in the colon separated list of
  156. database files passed as argument or the default database file
  157. if no argument.
  158. Returns 0 on success and -1 on failure.
  159. """
  160. return _check(self._magic_t, dbs)
  161. def list(self, dbs):
  162. """
  163. Check the validity of entries in the colon separated list of
  164. database files passed as argument or the default database file
  165. if no argument.
  166. Returns 0 on success and -1 on failure.
  167. """
  168. return _list(self._magic_t, dbs)
  169. def errno(self):
  170. """
  171. Returns a numeric error code. If return value is 0, an internal
  172. magic error occurred. If return value is non-zero, the value is
  173. an OS error code. Use the errno module or os.strerror() can be used
  174. to provide detailed error information.
  175. """
  176. return _errno(self._magic_t)
  177. def open(flags):
  178. """
  179. Returns a magic object on success and None on failure.
  180. Flags argument as for setflags.
  181. """
  182. return Magic(_open(flags))