Change the prototype of the callback in SKIPDATA option. Suggested by Ben Nagy.

Original prototype:
  typedef size_t (*cs_skipdata_cb_t)(const uint8_t *code, uint64_t offset, void* user_data);

Now we add @code_size argument to reflect the size of the input buffer @code.
Also, we change the data type of @offset to size_t because this argument indicates the
distance from currently examining bytes to @code, but not the address of the byte.

  typedef size_t (*cs_skipdata_cb_t)(const uint8_t *code, size_t code_size, size_t offset, void* user_data);
This commit is contained in:
Nguyen Anh Quynh 2014-07-10 15:42:16 +08:00
parent 3c0f43611d
commit 0df7e93a3c
3 changed files with 10 additions and 9 deletions

9
cs.c
View File

@ -422,7 +422,10 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
bool r;
void *tmp;
size_t skipdata_bytes;
// save all the original info of the buffer
uint64_t offset_org;
size_t size_org;
const uint8_t *buffer_org;
if (!handle) {
// FIXME: how to handle this case:
@ -433,7 +436,9 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
handle->errnum = CS_ERR_OK;
// save the original offset for SKIPDATA
buffer_org = buffer;
offset_org = offset;
size_org = size;
total_size = (sizeof(cs_insn) * INSN_CACHE_SIZE);
total = cs_mem_malloc(total_size);
insn_cache = total;
@ -512,8 +517,8 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
break;
if (handle->skipdata_setup.callback) {
skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
handle->skipdata_setup.user_data);
skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
offset - offset_org, handle->skipdata_setup.user_data);
if (skipdata_bytes > size)
// remaining data is not enough
break;

View File

@ -116,12 +116,13 @@ 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().
// @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.
// @user_data: user-data passed to cs_option() via @user_data field in
// cs_opt_skipdata struct below.
// @return: return number of bytes to skip, or 0 to immediately stop disassembling.
typedef size_t (*cs_skipdata_cb_t)(const uint8_t *code, uint64_t offset, void* user_data);
typedef size_t (*cs_skipdata_cb_t)(const uint8_t *code, size_t code_size, size_t offset, void* user_data);
// User-customized setup for SKIPDATA option
typedef struct cs_opt_skipdata {

View File

@ -30,17 +30,12 @@ static void print_string_hex(unsigned char *str, int len)
printf("\n");
}
size_t mycallback(const uint8_t *buffer, uint64_t offset, void *p)
static size_t mycallback(const uint8_t *buffer, size_t buffer_size, size_t offset, void *p)
{
// always skip 2 bytes when encountering data
return 2;
}
cs_opt_skipdata skipdata = {
// rename default "data" instruction from ".byte" to "db"
"db",
};
static void test()
{
#define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"