13 | 13 |
'PDF document, version 1.2'
|
14 | 14 |
>>>
|
15 | 15 |
|
16 | |
|
17 | 16 |
"""
|
18 | 17 |
|
19 | 18 |
import sys
|
|
34 | 33 |
class Magic:
|
35 | 34 |
"""
|
36 | 35 |
Magic is a wrapper around the libmagic C library.
|
37 | |
|
38 | 36 |
"""
|
39 | 37 |
|
40 | 38 |
def __init__(self, mime=False, magic_file=None, mime_encoding=False,
|
41 | |
keep_going=False, uncompress=False):
|
|
39 |
keep_going=False, uncompress=False, raw=False):
|
42 | 40 |
"""
|
43 | 41 |
Create a new libmagic wrapper.
|
44 | 42 |
|
|
47 | 45 |
magic_file - use a mime database other than the system default
|
48 | 46 |
keep_going - don't stop at the first match, keep going
|
49 | 47 |
uncompress - Try to look inside compressed files.
|
|
48 |
raw - Do not try to decode "non-printable" chars.
|
50 | 49 |
"""
|
51 | 50 |
self.flags = MAGIC_NONE
|
52 | 51 |
if mime:
|
|
55 | 54 |
self.flags |= MAGIC_MIME_ENCODING
|
56 | 55 |
if keep_going:
|
57 | 56 |
self.flags |= MAGIC_CONTINUE
|
58 | |
|
59 | 57 |
if uncompress:
|
60 | 58 |
self.flags |= MAGIC_COMPRESS
|
|
59 |
if raw:
|
|
60 |
self.flags |= MAGIC_RAW
|
61 | 61 |
|
62 | 62 |
self.cookie = magic_open(self.flags)
|
63 | 63 |
self.lock = threading.Lock()
|
|
189 | 189 |
|
190 | 190 |
magic_t = ctypes.c_void_p
|
191 | 191 |
|
|
192 |
|
192 | 193 |
def errorcheck_null(result, func, args):
|
193 | 194 |
if result is None:
|
194 | 195 |
err = magic_error(args[0])
|
195 | 196 |
raise MagicException(err)
|
196 | 197 |
else:
|
197 | 198 |
return result
|
|
199 |
|
198 | 200 |
|
199 | 201 |
def errorcheck_negative_one(result, func, args):
|
200 | 202 |
if result is -1:
|
|
211 | 213 |
return s
|
212 | 214 |
else:
|
213 | 215 |
return s.decode('utf-8')
|
|
216 |
|
214 | 217 |
|
215 | 218 |
def coerce_filename(filename):
|
216 | 219 |
if filename is None:
|
|
229 | 232 |
else:
|
230 | 233 |
return filename
|
231 | 234 |
|
|
235 |
|
232 | 236 |
magic_open = libmagic.magic_open
|
233 | 237 |
magic_open.restype = magic_t
|
234 | 238 |
magic_open.argtypes = [c_int]
|
|
250 | 254 |
_magic_file.argtypes = [magic_t, c_char_p]
|
251 | 255 |
_magic_file.errcheck = errorcheck_null
|
252 | 256 |
|
|
257 |
|
253 | 258 |
def magic_file(cookie, filename):
|
254 | 259 |
return _magic_file(cookie, coerce_filename(filename))
|
255 | 260 |
|
|
258 | 263 |
_magic_buffer.argtypes = [magic_t, c_void_p, c_size_t]
|
259 | 264 |
_magic_buffer.errcheck = errorcheck_null
|
260 | 265 |
|
|
266 |
|
261 | 267 |
def magic_buffer(cookie, buf):
|
262 | 268 |
return _magic_buffer(cookie, buf, len(buf))
|
263 | 269 |
|
|
267 | 273 |
_magic_load.argtypes = [magic_t, c_char_p]
|
268 | 274 |
_magic_load.errcheck = errorcheck_negative_one
|
269 | 275 |
|
|
276 |
|
270 | 277 |
def magic_load(cookie, filename):
|
271 | 278 |
return _magic_load(cookie, coerce_filename(filename))
|
272 | 279 |
|
|
287 | 294 |
_magic_setparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
|
288 | 295 |
_magic_setparam.errcheck = errorcheck_negative_one
|
289 | 296 |
|
|
297 |
|
290 | 298 |
def magic_setparam(cookie, param, val):
|
291 | 299 |
v = c_size_t(val)
|
292 | 300 |
return _magic_setparam(cookie, param, byref(v))
|
|
296 | 304 |
_magic_getparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
|
297 | 305 |
_magic_getparam.errcheck = errorcheck_negative_one
|
298 | 306 |
|
|
307 |
|
299 | 308 |
def magic_getparam(cookie, param):
|
300 | 309 |
val = c_size_t()
|
301 | 310 |
_magic_getparam(cookie, param, byref(val))
|
302 | 311 |
return val.value
|
|
312 |
|
303 | 313 |
|
304 | 314 |
MAGIC_NONE = 0x000000 # No flags
|
305 | 315 |
MAGIC_DEBUG = 0x000001 # Turn on debugging
|