summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2018-09-01 13:53:50 -0700
committerGitHub <noreply@github.com>2018-09-01 13:53:50 -0700
commit79eed130562212190140f6192b296d1142cb762f (patch)
treef3bd3029ba412031578d67d35e7fe7d5129341b5
parent41afcd5eab5203f8d4ac7d2ec2431db09aaeaf59 (diff)
parent50e8c85631bed343bf1be09f14a4f1be5fb69e5e (diff)
downloadpython-magic-79eed130562212190140f6192b296d1142cb762f.tar.gz
Merge pull request #179 from cbiedl/master
getparam/setparam support #163
-rw-r--r--magic.py33
-rwxr-xr-xtest/test.py8
2 files changed, 40 insertions, 1 deletions
diff --git a/magic.py b/magic.py
index 83b906d..a4729e6 100644
--- a/magic.py
+++ b/magic.py
@@ -23,7 +23,7 @@ import ctypes
import ctypes.util
import threading
-from ctypes import c_char_p, c_int, c_size_t, c_void_p
+from ctypes import c_char_p, c_int, c_size_t, c_void_p, byref, POINTER
class MagicException(Exception):
@@ -99,6 +99,12 @@ class Magic:
else:
raise e
+ def setparam(self, param, val):
+ return magic_setparam(self.cookie, param, val)
+
+ def getparam(self, param):
+ return magic_getparam(self.cookie, param)
+
def __del__(self):
# no _thread_check here because there can be no other
# references to this object at this point.
@@ -277,7 +283,24 @@ magic_compile = libmagic.magic_compile
magic_compile.restype = c_int
magic_compile.argtypes = [magic_t, c_char_p]
+_magic_setparam = libmagic.magic_setparam
+_magic_setparam.restype = c_int
+_magic_setparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
+_magic_setparam.errcheck = errorcheck_negative_one
+
+def magic_setparam(cookie, param, val):
+ v = c_size_t(val)
+ return _magic_setparam(cookie, param, byref(v))
+_magic_getparam = libmagic.magic_getparam
+_magic_getparam.restype = c_int
+_magic_getparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
+_magic_getparam.errcheck = errorcheck_negative_one
+
+def magic_getparam(cookie, param):
+ val = c_size_t()
+ _magic_getparam(cookie, param, byref(val))
+ return val
MAGIC_NONE = 0x000000 # No flags
MAGIC_DEBUG = 0x000001 # Turn on debugging
@@ -301,3 +324,11 @@ MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
+
+MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
+MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic
+MAGIC_PARAM_ELF_PHNUM_MAX = 2 # Max ELF notes processed
+MAGIC_PARAM_ELF_SHNUM_MAX = 3 # Max ELF program sections processed
+MAGIC_PARAM_ELF_NOTES_MAX = 4 # # Max ELF sections processed
+MAGIC_PARAM_REGEX_MAX = 5 # Length limit for regex searches
+MAGIC_PARAM_BYTES_MAX = 6 # Max number of bytes to read from file
diff --git a/test/test.py b/test/test.py
index 044af95..54ffe32 100755
--- a/test/test.py
+++ b/test/test.py
@@ -124,6 +124,14 @@ class MagicTest(unittest.TestCase):
finally:
magic.magic_buffer = old
+ def test_getparam(self):
+ filename = os.path.join(self.TESTDATA_DIR, 'keep-going.jpg')
+
+ m = magic.Magic(mime=True)
+ self.assertEqual(m.getparam(magic.MAGIC_PARAM_BYTES_MAX).value, 1048576)
+ m.setparam(magic.MAGIC_PARAM_BYTES_MAX, 1)
+ self.assertEqual(m.getparam(magic.MAGIC_PARAM_BYTES_MAX).value, 1)
+ self.assertEqual(m.from_file(filename), 'application/octet-stream')
if __name__ == '__main__':
unittest.main()