python: support SKIPDATA option. this also adds test_skipdata.py
diff --git a/bindings/python/capstone/capstone.py b/bindings/python/capstone/capstone.py
index 1f63e09..3acd0bf 100644
--- a/bindings/python/capstone/capstone.py
+++ b/bindings/python/capstone/capstone.py
@@ -94,6 +94,9 @@
CS_OPT_SYNTAX = 1 # Intel X86 asm syntax (CS_ARCH_X86 arch)
CS_OPT_DETAIL = 2 # Break down instruction structure into details
CS_OPT_MODE = 3 # Change engine's mode at run-time
+CS_OPT_MEM = 4 # Change engine's mode at run-time
+CS_OPT_SKIPDATA = 5 # Skip data when disassembling
+CS_OPT_SKIPDATA_SETUP = 6 # Setup user-defined function for SKIPDATA option
# Capstone option value
CS_OPT_OFF = 0 # Turn OFF an option - default option of CS_OPT_DETAIL
@@ -204,6 +207,16 @@
('detail', ctypes.POINTER(_cs_detail)),
)
+# callback for SKIPDATA option
+SKIPDATA_CB = ctypes.CFUNCTYPE(ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p)
+
+class _cs_opt_skipdata(ctypes.Structure):
+ _fields_ = (
+ ('mnemonic', ctypes.c_char_p),
+ ('callback', SKIPDATA_CB),
+ ('user_data', ctypes.c_void_p),
+ )
+
# setup all the function prototype
def _setup_prototype(lib, fname, restype, *argtypes):
getattr(lib, fname).restype = restype
@@ -219,7 +232,7 @@
_setup_prototype(_cs, "cs_op_count", ctypes.c_int, ctypes.c_size_t, ctypes.POINTER(_cs_insn), ctypes.c_uint)
_setup_prototype(_cs, "cs_op_index", ctypes.c_int, ctypes.c_size_t, ctypes.POINTER(_cs_insn), ctypes.c_uint, ctypes.c_uint)
_setup_prototype(_cs, "cs_errno", ctypes.c_int, ctypes.c_size_t)
-_setup_prototype(_cs, "cs_option", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_size_t)
+_setup_prototype(_cs, "cs_option", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_void_p)
_setup_prototype(_cs, "cs_version", ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
_setup_prototype(_cs, "cs_support", ctypes.c_bool, ctypes.c_int)
_setup_prototype(_cs, "cs_strerror", ctypes.c_char_p, ctypes.c_int)
@@ -549,6 +562,10 @@
self._diet = cs_support(CS_SUPPORT_DIET)
self._x86_compact = cs_support(CS_SUPPORT_X86_REDUCE)
+ # default mnemonic for SKIPDATA
+ self._skipdata_mnem = ".byte"
+ self._skipdata = False
+
# destructor to be called automatically when object is destroyed.
def __del__(self):
@@ -590,6 +607,43 @@
self._syntax = style
+ # return current skipdata status
+ @property
+ def skipdata(self):
+ return self._skipdata
+
+
+ # setter: modify skipdata status
+ @syntax.setter
+ def skipdata(self, opt):
+ if opt == False:
+ status = _cs.cs_option(self.csh, CS_OPT_SKIPDATA, CS_OPT_OFF)
+ else:
+ status = _cs.cs_option(self.csh, CS_OPT_SKIPDATA, CS_OPT_ON)
+ if status != CS_ERR_OK:
+ raise CsError(status)
+
+ # save this option
+ self._skipdata = opt
+
+
+ # setter: modify "data" instruction's mnemonic for SKIPDATA
+ @syntax.setter
+ def skipdata_opt(self, opt):
+ _skipdata_opt = _cs_opt_skipdata()
+ _mnem, _cb, _ud = opt
+ _skipdata_opt.mnemonic = _mnem
+ _skipdata_opt.callback = ctypes.cast(_cb, SKIPDATA_CB)
+ #_skipdata_opt.callback = _cb
+ _skipdata_opt.user_data = _ud
+ status = _cs.cs_option(self.csh, CS_OPT_SKIPDATA_SETUP, ctypes.cast(ctypes.byref(_skipdata_opt), ctypes.c_void_p))
+ if status != CS_ERR_OK:
+ raise CsError(status)
+
+ # save the "data" instruction mnem
+ self._skipdata_opt = _skipdata_opt
+
+
# is detail mode enable?
@property
def detail(self):
diff --git a/bindings/python/test_skipdata.py b/bindings/python/test_skipdata.py
new file mode 100755
index 0000000..7f7aed5
--- /dev/null
+++ b/bindings/python/test_skipdata.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+
+# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
+
+from capstone import *
+import binascii
+
+X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
+RANDOM_CODE = "\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78"
+
+all_tests = (
+ (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", 0),
+ (CS_ARCH_ARM, 0, RANDOM_CODE, "Arm", 0),
+ )
+
+
+def to_hex(s):
+ return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK
+
+
+### Test cs_disasm_quick()
+def test_cs_disasm_quick():
+ for (arch, mode, code, comment, syntax) in all_tests:
+ print('*' * 40)
+ print("Platform: %s" %comment)
+ print("Disasm:"),
+ print to_hex(code)
+ for insn in cs_disasm_quick(arch, mode, code, 0x1000):
+ print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str))
+ print
+
+
+### Test class Cs
+def test_class():
+ for (arch, mode, code, comment, syntax) in all_tests:
+ print('*' * 16)
+ print("Platform: %s" %comment)
+ print("Code: %s" % to_hex(code))
+ print("Disasm:")
+
+ try:
+ md = Cs(arch, mode)
+
+ if syntax != 0:
+ md.syntax = syntax
+
+ md.skipdata = True
+ # To rename "data" instruction's mnemonic to "db", uncomment the line below
+ # This example ignores SKIPDATA's callback (first None) & user_data (second None)
+ md.skipdata_opt = ("db", None, None)
+
+ for insn in md.disasm(code, 0x1000):
+ #bytes = binascii.hexlify(insn.bytes)
+ #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
+ print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str))
+
+ print("0x%x:" % (insn.address + insn.size))
+ print
+ except CsError as e:
+ print("ERROR: %s" %e)
+
+
+test_class()