diff --git a/bindings/python/capstone/capstone.py b/bindings/python/capstone/capstone.py index 3995afd3..0e965e01 100644 --- a/bindings/python/capstone/capstone.py +++ b/bindings/python/capstone/capstone.py @@ -135,6 +135,7 @@ class _cs_insn(ctypes.Structure): ('regs_write_count', ctypes.c_uint), ('groups', ctypes.c_uint * 8), ('groups_count', ctypes.c_uint), + ('hex_code', ctypes.c_ubyte * 15), ('arch', _cs_arch), ) @@ -197,6 +198,7 @@ class cs_insn: self.regs_read = all_info.regs_read[:all_info.regs_read_count] self.regs_write = all_info.regs_write[:all_info.regs_write_count] self.groups = all_info.groups[:all_info.groups_count] + self.hex_code = bytearray(all_info.hex_code)[:self.size] if arch == CS_ARCH_ARM: (self.cc, self.update_flags, self.writeback, self.operands) = \ @@ -215,7 +217,6 @@ class cs_insn: self.raw_insn = all_info self.csh = csh - def errno(): return _cs.cs_errno(self.csh) @@ -254,7 +255,6 @@ class cs_insn: raise ValueError("Error: Failed to initialize!") return _cs.cs_op_index(self.csh, self.raw_insn, op_type, position) - class cs: def __init__(self, arch, mode): self.arch, self.mode = arch, mode @@ -279,7 +279,6 @@ class cs: if res > 0: for i in xrange(res): yield cs_insn(self.csh, all_insn[i], self.arch) - _cs.cs_free(all_insn) else: yield [] diff --git a/bindings/python/test.py b/bindings/python/test.py index 766619e2..d1c6438d 100755 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -3,6 +3,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh from capstone import * +import binascii X86_CODE16 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00" X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00" @@ -62,7 +63,8 @@ def test_class(): all_ins = list(md.disasm(code, 0x1000)) for insn in all_ins: - print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str)) + hex_code = binascii.hexlify(insn.hex_code) + print("0x%x:\t%s\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str, hex_code)) print("0x%x:" % (all_ins[-1].address + all_ins[-1].size)) print diff --git a/cs.c b/cs.c index 8b07261d..7e070a99 100644 --- a/cs.c +++ b/cs.c @@ -168,7 +168,7 @@ cs_err cs_close(csh handle) // fill insn with mnemonic & operands info static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, - PostPrinter_t printer) + PostPrinter_t printer, unsigned char *code) { memcpy(insn, &mci->pub_insn, sizeof(*insn)); @@ -194,6 +194,9 @@ static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mc strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; + + // fill the instruction bytes + memcpy(insn->hex_code, code, MIN(sizeof(insn->hex_code), insn->size)); } cs_err cs_option(csh ud, cs_opt_type type, size_t value) @@ -252,7 +255,7 @@ size_t cs_disasm(csh ud, unsigned char *buffer, size_t size, uint64_t offset, si mci.mode = handle->mode; handle->printer(&mci, &ss, handle->printer_info); - fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer); + fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer); c++; insn++; @@ -264,8 +267,9 @@ size_t cs_disasm(csh ud, unsigned char *buffer, size_t size, uint64_t offset, si if (c == count) return c; } - } else // face a broken instruction? + } else { // face a broken instruction? return c; + } } return c; @@ -304,7 +308,7 @@ size_t cs_disasm_dyn(csh ud, unsigned char *buffer, size_t size, uint64_t offset mci.mode = handle->mode; handle->printer(&mci, &ss, handle->printer_info); - fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer); + fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer); f++; if (f == ARR_SIZE(insn_cache)) { @@ -330,8 +334,10 @@ size_t cs_disasm_dyn(csh ud, unsigned char *buffer, size_t size, uint64_t offset if (count > 0 && c == count) break; - } else // encounter a broken instruction - break; + } else { // encounter a broken instruction + // XXX: TODO: JOXEAN continue here + break; + } } if (f) { diff --git a/include/capstone.h b/include/capstone.h index f04af712..e577ad0a 100644 --- a/include/capstone.h +++ b/include/capstone.h @@ -11,6 +11,8 @@ extern "C" { #include #include +#define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) + // Handle using with all API typedef size_t csh; @@ -82,6 +84,8 @@ typedef struct cs_insn { unsigned int groups[8]; // list of group this instruction belong to unsigned int groups_count; // number of groups this insn belongs to + unsigned char hex_code[15]; // bytes of the instruction + // Architecture-specific instruction info union { cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode diff --git a/tests/test.c b/tests/test.c index 721ce8ff..8ea07849 100644 --- a/tests/test.c +++ b/tests/test.c @@ -179,7 +179,7 @@ static void test() int main() { - test(); + test(); #if 0 #define offsetof(type, member) (int)(&((type *)0)->member)