add CS_OPT_MODE option. this allows us to change engine's mode at run-time

This commit is contained in:
Nguyen Anh Quynh 2013-12-20 00:04:26 +08:00
parent d06e2f5ac1
commit 1bdb23a76d
4 changed files with 70 additions and 24 deletions

View File

@ -243,6 +243,7 @@ public class Capstone {
// Capstone option type
public static final int CS_OPT_SYNTAX = 1; // Intel X86 asm syntax (CS_ARCH_X86 arch)
public static final int CS_OPT_DETAIL = 2; // Break down instruction structure into details
public static final int CS_OPT_MODE = 3; // Change engine's mode at run-time
//Capstone option value
public static final int CS_OPT_OFF = 0; // Turn OFF an option (CS_OPT_DETAIL)
@ -287,6 +288,14 @@ public class Capstone {
}
}
public void setMode(int opt) {
if (cs.cs_option(ns.csh, CS_OPT_MODE, new NativeLong(opt)) == CS_ERR_OK) {
this.mode = opt;
} else {
throw new RuntimeException("ERROR: Unknown mode option");
}
}
public String getRegName(int reg) {
return cs.cs_reg_name(ns.csh, reg);
}

View File

@ -32,6 +32,7 @@ __all__ = [
'CS_OPT_SYNTAX_ATT',
'CS_OPT_DETAIL',
'CS_OPT_MODE',
'CS_OPT_ON',
'CS_OPT_OFF',
@ -69,7 +70,8 @@ CS_MODE_BIG_ENDIAN = (1 << 31) # big-endian mode
# Capstone option type
CS_OPT_SYNTAX = 1 # Intel X86 asm syntax (CS_ARCH_X86 arch)
CS_OPT_DETAIL = 2 # Break down instruction structure into details
CS_OPT_DETAIL = 2 # Break down instruction structure into details
CS_OPT_MODE = 3 # Change engine's mode at run-time
# Capstone option value
CS_OPT_OFF = 0 # Turn OFF an option (CS_OPT_DETAIL)
@ -299,7 +301,7 @@ class CsInsn(object):
class Cs(object):
def __init__(self, arch, mode):
self.arch, self.mode = arch, mode
self.arch, self._mode = arch, mode
self.csh = ctypes.c_size_t()
status = _cs.cs_open(arch, mode, ctypes.byref(self.csh))
if status != CS_ERR_OK:
@ -350,6 +352,18 @@ class Cs(object):
# save detail
self._detail = opt
@property
def mode(self):
return self._mode
@mode.setter
def mode(self, opt): # opt is new disasm mode, of int type
status = _cs.cs_option(self.csh, CS_OPT_MODE, opt)
if status != CS_ERR_OK:
raise CsError(status)
# save mode
self._mode = opt
def disasm(self, code, offset, count = 0):
insns = []
all_insn = ctypes.POINTER(_cs_insn)()

66
cs.c
View File

@ -219,32 +219,54 @@ cs_err cs_option(csh ud, cs_opt_type type, size_t value)
case CS_OPT_DETAIL:
handle->detail = value;
return CS_ERR_OK;
}
case CS_OPT_SYNTAX:
switch (handle->arch) {
default:
// only selected archs care about CS_OPT_SYNTAX
handle->errnum = CS_ERR_OPTION;
return CS_ERR_OPTION;
// only selected archs care about CS_OPT_SYNTAX
switch (handle->arch) {
default:
handle->errnum = CS_ERR_OPTION;
return CS_ERR_OPTION;
case CS_ARCH_X86:
switch(value) {
default:
// wrong syntax value
handle->errnum = CS_ERR_OPTION;
return CS_ERR_OPTION;
case CS_ARCH_X86:
if (type & CS_OPT_SYNTAX) {
switch(value) {
default:
handle->errnum = CS_ERR_OPTION;
return CS_ERR_OPTION;
case CS_OPT_SYNTAX_INTEL:
handle->printer = X86_Intel_printInst;
break;
case CS_OPT_SYNTAX_INTEL:
handle->printer = X86_Intel_printInst;
break;
case CS_OPT_SYNTAX_ATT:
handle->printer = X86_ATT_printInst;
break;
}
break;
}
break;
case CS_OPT_SYNTAX_ATT:
handle->printer = X86_ATT_printInst;
break;
}
} else {
handle->errnum = CS_ERR_OPTION;
return CS_ERR_OPTION;
case CS_OPT_MODE: // change engine's mode at run-time
handle->mode = value;
switch (handle->arch) {
default:
// only selected archs care about CS_OPT_SYNTAX
break;
case CS_ARCH_ARM:
if (value & CS_MODE_THUMB)
handle->disasm = Thumb_getInstruction;
else
handle->disasm = ARM_getInstruction;
handle->mode = value;
break;
case CS_ARCH_MIPS:
if (value & CS_MODE_32)
handle->disasm = Mips_getInstruction;
else
handle->disasm = Mips64_getInstruction;
handle->mode = value;
break;
}
break;
}

View File

@ -45,6 +45,7 @@ typedef enum cs_mode {
typedef enum cs_opt_type {
CS_OPT_SYNTAX = 1, // Asssembly output syntax
CS_OPT_DETAIL, // Break down instruction structure into details
CS_OPT_MODE, // Change engine's mode at run-time
} cs_opt_type;
// Runtime option value (associated with option type above)