From 027afdc179d34a8fd5381725c525fbb10c4159b8 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Thu, 10 Jul 2014 15:42:16 +0800 Subject: [PATCH] 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); --- cs.c | 9 +++++++-- include/capstone.h | 3 ++- tests/test_skipdata.c | 7 +------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cs.c b/cs.c index a02de940..3f51d8f0 100644 --- a/cs.c +++ b/cs.c @@ -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; diff --git a/include/capstone.h b/include/capstone.h index 4f8b57e9..2872846f 100644 --- a/include/capstone.h +++ b/include/capstone.h @@ -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 { diff --git a/tests/test_skipdata.c b/tests/test_skipdata.c index ab6eeeb5..767cb05b 100644 --- a/tests/test_skipdata.c +++ b/tests/test_skipdata.c @@ -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"