Merge branch 'master' of https://github.com/joxeankoret/capstone into hexcode

This commit is contained in:
Nguyen Anh Quynh 2013-12-04 19:28:54 +08:00
commit c45b1588ef
5 changed files with 22 additions and 11 deletions

View File

@ -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 []

View File

@ -3,6 +3,7 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
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

18
cs.c
View File

@ -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) {

View File

@ -11,6 +11,8 @@ extern "C" {
#include <stdint.h>
#include <stdbool.h>
#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

View File

@ -179,7 +179,7 @@ static void test()
int main()
{
test();
test();
#if 0
#define offsetof(type, member) (int)(&((type *)0)->member)