mirror of
https://github.com/capstone-engine/capstone.git
synced 2024-12-01 09:20:35 +00:00
python: support option CS_OPT_MNEMONIC
This commit is contained in:
parent
44adb35f6a
commit
bbde6d5c63
@ -140,6 +140,7 @@ 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
|
||||
CS_OPT_MNEMONIC = 7 # Customize instruction mnemonic
|
||||
|
||||
# Capstone option value
|
||||
CS_OPT_OFF = 0 # Turn OFF an option - default option of CS_OPT_DETAIL
|
||||
@ -286,6 +287,11 @@ class _cs_opt_skipdata(ctypes.Structure):
|
||||
('user_data', ctypes.c_void_p),
|
||||
)
|
||||
|
||||
class _cs_opt_mnem(ctypes.Structure):
|
||||
_fields_ = (
|
||||
('id', ctypes.c_uint),
|
||||
('mnemonic', ctypes.c_char_p),
|
||||
)
|
||||
|
||||
# setup all the function prototype
|
||||
def _setup_prototype(lib, fname, restype, *argtypes):
|
||||
@ -803,6 +809,19 @@ class Cs(object):
|
||||
self._skipdata_opt = _skipdata_opt
|
||||
|
||||
|
||||
# customize instruction mnemonic
|
||||
def mnemonic_setup(self, id, mnem):
|
||||
_mnem_opt = _cs_opt_mnem()
|
||||
_mnem_opt.id = id
|
||||
if mnem:
|
||||
_mnem_opt.mnemonic = mnem.encode()
|
||||
else:
|
||||
_mnem_opt.mnemonic = mnem
|
||||
status = _cs.cs_option(self.csh, CS_OPT_MNEMONIC, ctypes.cast(ctypes.byref(_mnem_opt), ctypes.c_void_p))
|
||||
if status != CS_ERR_OK:
|
||||
raise CsError(status)
|
||||
|
||||
|
||||
# check to see if this engine supports a particular arch,
|
||||
# or diet mode (depending on @query).
|
||||
def support(self, query):
|
||||
|
40
bindings/python/test_customized_mnem.py
Executable file
40
bindings/python/test_customized_mnem.py
Executable file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
|
||||
|
||||
from __future__ import print_function
|
||||
from capstone import *
|
||||
from capstone.x86 import *
|
||||
from xprint import to_hex
|
||||
|
||||
|
||||
X86_CODE32 = b"\x75\x01"
|
||||
|
||||
|
||||
def print_insn(md, code):
|
||||
print("%s\t" % to_hex(code, False), end="")
|
||||
|
||||
for insn in md.disasm(code, 0x1000):
|
||||
print("\t%s\t%s" % (insn.mnemonic, insn.op_str))
|
||||
|
||||
|
||||
def test():
|
||||
try:
|
||||
md = Cs(CS_ARCH_X86, CS_MODE_32)
|
||||
|
||||
print("Disassemble X86 code with default instruction mnemonic")
|
||||
print_insn(md, X86_CODE32)
|
||||
|
||||
print("\nNow customize engine to change mnemonic from 'JNE' to 'JNZ'")
|
||||
md.mnemonic_setup(X86_INS_JNE, "jnz")
|
||||
print_insn(md, X86_CODE32)
|
||||
|
||||
print("\nReset engine to use the default mnemonic")
|
||||
md.mnemonic_setup(X86_INS_JNE, None)
|
||||
print_insn(md, X86_CODE32)
|
||||
except CsError as e:
|
||||
print("ERROR: %s" % e)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
@ -6,11 +6,17 @@ import sys
|
||||
_python3 = sys.version_info.major == 3
|
||||
|
||||
|
||||
def to_hex(s):
|
||||
def to_hex(s, prefix_0x = True):
|
||||
if _python3:
|
||||
return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK
|
||||
if prefix_0x:
|
||||
return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK
|
||||
else:
|
||||
return " ".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK
|
||||
else:
|
||||
return " ".join("0x{0:02x}".format(ord(c)) for c in s)
|
||||
if prefix_0x:
|
||||
return " ".join("0x{0:02x}".format(ord(c)) for c in s)
|
||||
else:
|
||||
return " ".join("{0:02x}".format(ord(c)) for c in s)
|
||||
|
||||
def to_hex2(s):
|
||||
if _python3:
|
||||
|
@ -57,6 +57,7 @@ extern "C" {
|
||||
// result of cs_version() API.
|
||||
#define CS_MAKE_VERSION(major, minor) ((major << 8) + minor)
|
||||
|
||||
// Maximum size of an instruction mnemonic string.
|
||||
#define CS_MNEMONIC_SIZE 32
|
||||
|
||||
// Handle using with all API
|
||||
|
Loading…
Reference in New Issue
Block a user