api: get back the old API cs_disasm() & mark cs_disasm_ex() deprecated. cs_disasm_ex() will be removed in the future

This commit is contained in:
Nguyen Anh Quynh 2014-08-27 22:55:29 +08:00
parent 237204970d
commit 0beb0d494b
18 changed files with 73 additions and 47 deletions

View File

@ -242,7 +242,7 @@ public class Capstone {
private interface CS extends Library {
public int cs_open(int arch, int mode, NativeLongByReference handle);
public NativeLong cs_disasm_ex(NativeLong handle, byte[] code, NativeLong code_len,
public NativeLong cs_disasm(NativeLong handle, byte[] code, NativeLong code_len,
long addr, NativeLong count, PointerByReference insn);
public void cs_free(Pointer p, NativeLong count);
public int cs_close(NativeLongByReference handle);
@ -402,7 +402,7 @@ public class Capstone {
public CsInsn[] disasm(byte[] code, long address, long count) {
PointerByReference insnRef = new PointerByReference();
NativeLong c = cs.cs_disasm_ex(ns.csh, code, new NativeLong(code.length), address, new NativeLong(count), insnRef);
NativeLong c = cs.cs_disasm(ns.csh, code, new NativeLong(code.length), address, new NativeLong(count), insnRef);
Pointer p = insnRef.getValue();
_cs_insn byref = new _cs_insn(p);

View File

@ -34,7 +34,7 @@ CAMLprim value _cs_disasm(cs_arch arch, csh handle, const uint8_t * code, size_t
list = Val_emptylist;
size_t c = cs_disasm_ex(handle, code, code_len, addr, count, &insn);
size_t c = cs_disasm(handle, code, code_len, addr, count, &insn);
if (c) {
//printf("Found %lu insn, addr: %lx\n", c, addr);

View File

@ -131,7 +131,7 @@ CS_OPT_SYNTAX_NOREGNAME = 3 # Asm syntax prints register name with only number
# Capstone error type
CS_ERR_OK = 0 # No error: everything was fine
CS_ERR_MEM = 1 # Out-Of-Memory error: cs_open(), cs_disasm_ex()
CS_ERR_MEM = 1 # Out-Of-Memory error: cs_open(), cs_disasm()
CS_ERR_ARCH = 2 # Unsupported architecture: cs_open()
CS_ERR_HANDLE = 3 # Invalid handle: cs_op_count(), cs_op_index()
CS_ERR_CSH = 4 # Invalid csh argument: cs_close(), cs_errno(), cs_option()
@ -250,7 +250,7 @@ def _setup_prototype(lib, fname, restype, *argtypes):
getattr(lib, fname).argtypes = argtypes
_setup_prototype(_cs, "cs_open", ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.POINTER(ctypes.c_size_t))
_setup_prototype(_cs, "cs_disasm_ex", ctypes.c_size_t, ctypes.c_size_t, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, \
_setup_prototype(_cs, "cs_disasm", ctypes.c_size_t, ctypes.c_size_t, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, \
ctypes.c_uint64, ctypes.c_size_t, ctypes.POINTER(ctypes.POINTER(_cs_insn)))
_setup_prototype(_cs, "cs_free", None, ctypes.c_void_p, ctypes.c_size_t)
_setup_prototype(_cs, "cs_close", ctypes.c_int, ctypes.POINTER(ctypes.c_size_t))
@ -316,7 +316,7 @@ def cs_disasm_quick(arch, mode, code, offset, count=0):
raise CsError(status)
all_insn = ctypes.POINTER(_cs_insn)()
res = _cs.cs_disasm_ex(csh, code, len(code), offset, count, ctypes.byref(all_insn))
res = _cs.cs_disasm(csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in range(res):
yield CsInsn(_dummy_cs(csh, arch), all_insn[i])
@ -356,7 +356,7 @@ def cs_disasm_lite(arch, mode, code, offset, count=0):
raise CsError(status)
all_insn = ctypes.POINTER(_cs_insn)()
res = _cs.cs_disasm_ex(csh, code, len(code), offset, count, ctypes.byref(all_insn))
res = _cs.cs_disasm(csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in range(res):
insn = all_insn[i]
@ -776,7 +776,7 @@ class Cs(object):
print(code)
code = code.encode()
print(code)'''
res = _cs.cs_disasm_ex(self.csh, code, len(code), offset, count, ctypes.byref(all_insn))
res = _cs.cs_disasm(self.csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in range(res):
yield CsInsn(self, all_insn[i])
@ -798,7 +798,7 @@ class Cs(object):
raise CsError(CS_ERR_DIET)
all_insn = ctypes.POINTER(_cs_insn)()
res = _cs.cs_disasm_ex(self.csh, code, len(code), offset, count, ctypes.byref(all_insn))
res = _cs.cs_disasm(self.csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in range(res):
insn = all_insn[i]

View File

@ -41,7 +41,7 @@ cdef extern from "<capstone/capstone.h>":
cs_err cs_errno(csh handle)
size_t cs_disasm_ex(csh handle,
size_t cs_disasm(csh handle,
const uint8_t *code, size_t code_size,
uint64_t address,
size_t count,

View File

@ -256,7 +256,7 @@ cdef class Cs(object):
def disasm(self, code, addr, count=0):
cdef cc.cs_insn *allinsn
cdef res = cc.cs_disasm_ex(self.csh, code, len(code), addr, count, &allinsn)
cdef res = cc.cs_disasm(self.csh, code, len(code), addr, count, &allinsn)
detail = self._cs.detail
arch = self._cs.arch
@ -284,7 +284,7 @@ cdef class Cs(object):
# Diet engine cannot provide @mnemonic & @op_str
raise CsError(capstone.CS_ERR_DIET)
cdef res = cc.cs_disasm_ex(self.csh, code, len(code), addr, count, &allinsn)
cdef res = cc.cs_disasm(self.csh, code, len(code), addr, count, &allinsn)
for i from 0 <= i < res:
insn = allinsn[i]

9
cs.c
View File

@ -409,7 +409,7 @@ static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
// dynamicly allocate memory to contain disasm insn
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
CAPSTONE_EXPORT
size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
{
struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
MCInst mci;
@ -606,6 +606,13 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
return c;
}
CAPSTONE_EXPORT
CAPSTONE_DEPRECATED
size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
{
return cs_disasm(ud, buffer, size, offset, count, insn);
}
CAPSTONE_EXPORT
void cs_free(cs_insn *insn, size_t count)
{

View File

@ -27,6 +27,15 @@ extern "C" {
#define CAPSTONE_EXPORT
#endif
#ifdef __GNUC__
#define CAPSTONE_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define CAPSTONE_DEPRECATED __declspec(deprecated)
#else
#pragma message("WARNING: You need to implement CAPSTONE_DEPRECATED for this compiler")
#define CAPSTONE_DEPRECATED
#endif
// Capstone API version
#define CS_API_MAJOR 3
#define CS_API_MINOR 0
@ -119,7 +128,7 @@ typedef enum cs_opt_value {
// User-defined callback function for SKIPDATA option
// @code: the input buffer containing code to be disassembled. This is the
// same buffer passed to cs_disasm_ex().
// same buffer passed to cs_disasm().
// @code_size: size (in bytes) of the above @code buffer.
// @offset: the position of the currently-examining byte in the input
// buffer @code mentioned above.
@ -139,7 +148,7 @@ typedef struct cs_opt_skipdata {
// If the returned value from this callback is positive (>0), Capstone
// will skip exactly that number of bytes & continue. Otherwise, if
// the callback returns 0, Capstone stops disassembling and returns
// immediately from cs_disasm_ex()
// immediately from cs_disasm()
// NOTE: if this callback pointer is NULL, Capstone would skip a number
// of bytes depending on architectures, as following:
// Arm: 2 bytes (Thumb mode) or 4 bytes.
@ -236,7 +245,7 @@ typedef struct cs_insn {
// These are values returned by cs_errno()
typedef enum cs_err {
CS_ERR_OK = 0, // No error: everything was fine
CS_ERR_MEM, // Out-Of-Memory error: cs_open(), cs_disasm_ex()
CS_ERR_MEM, // Out-Of-Memory error: cs_open(), cs_disasm()
CS_ERR_ARCH, // Unsupported architecture: cs_open()
CS_ERR_HANDLE, // Invalid handle: cs_op_count(), cs_op_index()
CS_ERR_CSH, // Invalid csh argument: cs_close(), cs_errno(), cs_option()
@ -380,17 +389,27 @@ const char *cs_strerror(cs_err code);
On failure, call cs_errno() for error code.
*/
CAPSTONE_EXPORT
size_t cs_disasm_ex(csh handle,
size_t cs_disasm(csh handle,
const uint8_t *code, size_t code_size,
uint64_t address,
size_t count,
cs_insn **insn);
/* Deprecated function - to be retired in the next version!
Use cs_disasm() instead of cs_disasm_ex()
*/
CAPSTONE_EXPORT
CAPSTONE_DEPRECATED
size_t cs_disasm_ex(csh handle,
const uint8_t *code, size_t code_size,
uint64_t address,
size_t count,
cs_insn **insn);
/*
Free memory allocated in @insn by cs_disasm_ex()
Free memory allocated in @insn by cs_disasm()
@insn: pointer returned by @insn argument in cs_disasm_ex()
@count: number of cs_insn structures returned by cs_disasm_ex()
@insn: pointer returned by @insn argument in cs_disasm()
@count: number of cs_insn structures returned by cs_disasm()
*/
CAPSTONE_EXPORT
void cs_free(cs_insn *insn, size_t count);
@ -451,7 +470,7 @@ const char *cs_group_name(csh handle, unsigned int insn_id);
update @groups array.
@handle: handle returned by cs_open()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm_ex()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
@group_id: group that you want to check if this instruction belong to.
@return: true if this instruction indeed belongs to aboved group, or false otherwise.
@ -469,7 +488,7 @@ bool cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id);
WARN: when in 'diet' mode, this API is irrelevant because the engine does not
update @regs_read array.
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm_ex()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
@reg_id: register that you want to check if this instruction used it.
@return: true if this instruction indeed implicitly used aboved register, or false otherwise.
@ -487,7 +506,7 @@ bool cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id);
WARN: when in 'diet' mode, this API is irrelevant because the engine does not
update @regs_write array.
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm_ex()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
@reg_id: register that you want to check if this instruction modified it.
@return: true if this instruction indeed implicitly modified aboved register, or false otherwise.
@ -502,7 +521,7 @@ bool cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id);
NOTE: this API is only valid when detail option is ON (which is OFF by default)
@handle: handle returned by cs_open()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm_ex()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
@op_type: Operand type to be found.
@return: number of operands of given type @op_type in instruction @insn,
@ -519,7 +538,7 @@ int cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type);
NOTE: this API is only valid when detail option is ON (which is OFF by default)
@handle: handle returned by cs_open()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm_ex()
@insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
@op_type: Operand type to be found.
@position: position of the operand to be found. This must be in the range
[1, cs_op_count(handle, insn, op_type)]

View File

@ -205,7 +205,7 @@ static void test()
if (platforms[i].opt_type)
cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -220,7 +220,7 @@ static void test()
// print out the next offset, after the last insn
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -213,7 +213,7 @@ static void test()
if (platforms[i].syntax)
cs_option(handle, CS_OPT_SYNTAX, platforms[i].syntax);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
printf("****************\n");
@ -227,7 +227,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -201,7 +201,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -216,7 +216,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -199,7 +199,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &all_insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &all_insn);
if (count) {
size_t j;
int n;
@ -249,7 +249,7 @@ static void test()
// print out the next offset, after the last insn
printf("0x%"PRIx64":\n", all_insn[j-1].address + all_insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(all_insn, count);
} else {
printf("****************\n");

View File

@ -112,7 +112,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -127,7 +127,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -104,7 +104,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -119,7 +119,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -111,7 +111,7 @@ static void test()
cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
cs_option(handle, platforms[i].opt_skipdata, platforms[i].skipdata);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -126,7 +126,7 @@ static void test()
// print out the next offset, after the last insn
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -113,7 +113,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -128,7 +128,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -106,7 +106,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -121,7 +121,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -216,7 +216,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -231,7 +231,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");

View File

@ -101,7 +101,7 @@ static void test()
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
@ -116,7 +116,7 @@ static void test()
}
printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm_ex()
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");