diff --git a/cs.c b/cs.c index b824b1091..c426c1013 100644 --- a/cs.c +++ b/cs.c @@ -110,7 +110,7 @@ cs_vsnprintf_t cs_vsnprintf = NULL; #endif CAPSTONE_EXPORT -unsigned int cs_version(int *major, int *minor) +unsigned int CAPSTONE_API cs_version(int *major, int *minor) { archs_enable(); @@ -123,7 +123,7 @@ unsigned int cs_version(int *major, int *minor) } CAPSTONE_EXPORT -bool cs_support(int query) +bool CAPSTONE_API cs_support(int query) { archs_enable(); @@ -157,7 +157,7 @@ bool cs_support(int query) } CAPSTONE_EXPORT -cs_err cs_errno(csh handle) +cs_err CAPSTONE_API cs_errno(csh handle) { struct cs_struct *ud; if (!handle) @@ -169,7 +169,7 @@ cs_err cs_errno(csh handle) } CAPSTONE_EXPORT -const char *cs_strerror(cs_err code) +const char * CAPSTONE_API cs_strerror(cs_err code) { switch(code) { default: @@ -202,7 +202,7 @@ const char *cs_strerror(cs_err code) } CAPSTONE_EXPORT -cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) +cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle) { cs_err err; struct cs_struct *ud; @@ -247,7 +247,7 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) } CAPSTONE_EXPORT -cs_err cs_close(csh *handle) +cs_err CAPSTONE_API cs_close(csh *handle) { struct cs_struct *ud; @@ -358,7 +358,7 @@ static uint8_t skipdata_size(cs_struct *handle) } CAPSTONE_EXPORT -cs_err cs_option(csh ud, cs_opt_type type, size_t value) +cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value) { struct cs_struct *handle; archs_enable(); @@ -411,25 +411,34 @@ static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size) char *p = opstr; int len; size_t i; + size_t available = sizeof(((cs_insn*)NULL)->op_str); if (!size) { opstr[0] = '\0'; return; } - len = sprintf(p, "0x%02x", buffer[0]); + len = cs_snprintf(p, available, "0x%02x", buffer[0]); p+= len; + available -= len; for(i = 1; i < size; i++) { - len = sprintf(p, ", 0x%02x", buffer[i]); + len = cs_snprintf(p, available, ", 0x%02x", buffer[i]); + if (len < 0) { + break; + } + if ((size_t)len > available - 1) { + break; + } p+= len; + available -= len; } } // 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(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) +size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) { struct cs_struct *handle; MCInst mci; @@ -627,13 +636,13 @@ size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, si 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) +size_t CAPSTONE_API 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) +void CAPSTONE_API cs_free(cs_insn *insn, size_t count) { size_t i; @@ -646,7 +655,7 @@ void cs_free(cs_insn *insn, size_t count) } CAPSTONE_EXPORT -cs_insn *cs_malloc(csh ud) +cs_insn * CAPSTONE_API cs_malloc(csh ud) { cs_insn *insn; struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -674,7 +683,7 @@ cs_insn *cs_malloc(csh ud) // iterator for instruction "single-stepping" CAPSTONE_EXPORT -bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size, +bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size, uint64_t *address, cs_insn *insn) { struct cs_struct *handle; @@ -761,7 +770,7 @@ bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size, // return friendly name of regiser in a string CAPSTONE_EXPORT -const char *cs_reg_name(csh ud, unsigned int reg) +const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg) { struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -773,7 +782,7 @@ const char *cs_reg_name(csh ud, unsigned int reg) } CAPSTONE_EXPORT -const char *cs_insn_name(csh ud, unsigned int insn) +const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn) { struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -785,7 +794,7 @@ const char *cs_insn_name(csh ud, unsigned int insn) } CAPSTONE_EXPORT -const char *cs_group_name(csh ud, unsigned int group) +const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group) { struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -809,7 +818,7 @@ static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id) } CAPSTONE_EXPORT -bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id) +bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id) { struct cs_struct *handle; if (!ud) @@ -836,7 +845,7 @@ bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id) } CAPSTONE_EXPORT -bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id) +bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id) { struct cs_struct *handle; if (!ud) @@ -863,7 +872,7 @@ bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id) } CAPSTONE_EXPORT -bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id) +bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id) { struct cs_struct *handle; if (!ud) @@ -890,7 +899,7 @@ bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id) } CAPSTONE_EXPORT -int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type) +int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type) { struct cs_struct *handle; unsigned int count = 0, i; @@ -966,7 +975,7 @@ int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type) } CAPSTONE_EXPORT -int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type, +int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type, unsigned int post) { struct cs_struct *handle; diff --git a/include/capstone.h b/include/capstone.h index 5a79ddcd2..fab6c9c58 100644 --- a/include/capstone.h +++ b/include/capstone.h @@ -8,7 +8,7 @@ extern "C" { #endif -#include +#include "../myinttypes.h" #include #if defined(CAPSTONE_HAS_OSXKERNEL) #include @@ -22,12 +22,16 @@ extern "C" { #ifdef _MSC_VER #pragma warning(disable:4201) #pragma warning(disable:4100) +#define CAPSTONE_API __stdcall +#define CAPSTONE_CDECL __cdecl #ifdef CAPSTONE_SHARED #define CAPSTONE_EXPORT __declspec(dllexport) #else // defined(CAPSTONE_STATIC) #define CAPSTONE_EXPORT #endif #else +#define CAPSTONE_API +#define CAPSTONE_CDECL #ifdef __GNUC__ #define CAPSTONE_EXPORT __attribute__((visibility("default"))) #else @@ -99,11 +103,11 @@ typedef enum cs_mode { CS_MODE_MIPS64 = CS_MODE_64, // Mips64 ISA (Mips) } cs_mode; -typedef void* (*cs_malloc_t)(size_t size); -typedef void* (*cs_calloc_t)(size_t nmemb, size_t size); -typedef void* (*cs_realloc_t)(void *ptr, size_t size); -typedef void (*cs_free_t)(void *ptr); -typedef int (*cs_vsnprintf_t)(char *str, size_t size, const char *format, va_list ap); +typedef void* (CAPSTONE_CDECL*cs_malloc_t)(size_t size); +typedef void* (CAPSTONE_CDECL*cs_calloc_t)(size_t nmemb, size_t size); +typedef void* (CAPSTONE_CDECL*cs_realloc_t)(void *ptr, size_t size); +typedef void (CAPSTONE_CDECL*cs_free_t)(void *ptr); +typedef int (CAPSTONE_CDECL*cs_vsnprintf_t)(char *str, size_t size, const char *format, va_list ap); // User-defined dynamic memory related functions: malloc/calloc/realloc/free/vsnprintf() @@ -118,7 +122,8 @@ typedef struct cs_opt_mem { // Runtime option for the disassembled engine typedef enum cs_opt_type { - CS_OPT_SYNTAX = 1, // Assembly output syntax + CS_OPT_NONE = 0, // No opetion specified + CS_OPT_SYNTAX, // Assembly output syntax CS_OPT_DETAIL, // Break down instruction structure into details CS_OPT_MODE, // Change engine's mode at run-time CS_OPT_MEM, // User-defined dynamic memory related functions @@ -316,7 +321,7 @@ typedef enum cs_err { set both @major & @minor arguments to NULL. */ CAPSTONE_EXPORT -unsigned int cs_version(int *major, int *minor); +unsigned int CAPSTONE_API cs_version(int *major, int *minor); /* @@ -333,7 +338,7 @@ unsigned int cs_version(int *major, int *minor); @return True if this library supports the given arch, or in 'diet' mode. */ CAPSTONE_EXPORT -bool cs_support(int query); +bool CAPSTONE_API cs_support(int query); /* Initialize CS handle: this must be done before any usage of CS. @@ -346,7 +351,7 @@ bool cs_support(int query); for detailed error). */ CAPSTONE_EXPORT -cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle); +cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle); /* Close CS handle: MUST do to release the handle when it is not used anymore. @@ -363,7 +368,7 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle); for detailed error). */ CAPSTONE_EXPORT -cs_err cs_close(csh *handle); +cs_err CAPSTONE_API cs_close(csh *handle); /* Set option for disassembling engine at runtime @@ -380,7 +385,7 @@ cs_err cs_close(csh *handle); even before cs_open() */ CAPSTONE_EXPORT -cs_err cs_option(csh handle, cs_opt_type type, size_t value); +cs_err CAPSTONE_API cs_option(csh handle, cs_opt_type type, size_t value); /* Report the last error number when some API function fail. @@ -391,7 +396,7 @@ cs_err cs_option(csh handle, cs_opt_type type, size_t value); @return: error code of cs_err enum type (CS_ERR_*, see above) */ CAPSTONE_EXPORT -cs_err cs_errno(csh handle); +cs_err CAPSTONE_API cs_errno(csh handle); /* @@ -403,7 +408,7 @@ cs_err cs_errno(csh handle); passed in the argument @code */ CAPSTONE_EXPORT -const char *cs_strerror(cs_err code); +const char * CAPSTONE_API cs_strerror(cs_err code); /* Disassemble binary code, given the code buffer, size, address and number @@ -439,7 +444,7 @@ const char *cs_strerror(cs_err code); On failure, call cs_errno() for error code. */ CAPSTONE_EXPORT -size_t cs_disasm(csh handle, +size_t CAPSTONE_API cs_disasm(csh handle, const uint8_t *code, size_t code_size, uint64_t address, size_t count, @@ -451,7 +456,7 @@ size_t cs_disasm(csh handle, */ CAPSTONE_EXPORT CAPSTONE_DEPRECATED -size_t cs_disasm_ex(csh handle, +size_t CAPSTONE_API cs_disasm_ex(csh handle, const uint8_t *code, size_t code_size, uint64_t address, size_t count, @@ -465,7 +470,7 @@ size_t cs_disasm_ex(csh handle, to free memory allocated by cs_malloc(). */ CAPSTONE_EXPORT -void cs_free(cs_insn *insn, size_t count); +void CAPSTONE_API cs_free(cs_insn *insn, size_t count); /* @@ -477,7 +482,7 @@ void cs_free(cs_insn *insn, size_t count); this instruction with cs_free(insn, 1) */ CAPSTONE_EXPORT -cs_insn *cs_malloc(csh handle); +cs_insn * CAPSTONE_API cs_malloc(csh handle); /* Fast API to disassemble binary code, given the code buffer, size, address @@ -515,7 +520,7 @@ cs_insn *cs_malloc(csh handle); On failure, call cs_errno() for error code. */ CAPSTONE_EXPORT -bool cs_disasm_iter(csh handle, +bool CAPSTONE_API cs_disasm_iter(csh handle, const uint8_t **code, size_t *size, uint64_t *address, cs_insn *insn); @@ -533,7 +538,7 @@ bool cs_disasm_iter(csh handle, @return: string name of the register, or NULL if @reg_id is invalid. */ CAPSTONE_EXPORT -const char *cs_reg_name(csh handle, unsigned int reg_id); +const char * CAPSTONE_API cs_reg_name(csh handle, unsigned int reg_id); /* Return friendly name of an instruction in a string. @@ -548,7 +553,7 @@ const char *cs_reg_name(csh handle, unsigned int reg_id); @return: string name of the instruction, or NULL if @insn_id is invalid. */ CAPSTONE_EXPORT -const char *cs_insn_name(csh handle, unsigned int insn_id); +const char * CAPSTONE_API cs_insn_name(csh handle, unsigned int insn_id); /* Return friendly name of a group id (that an instruction can belong to) @@ -563,7 +568,7 @@ const char *cs_insn_name(csh handle, unsigned int insn_id); @return: string name of the group, or NULL if @group_id is invalid. */ CAPSTONE_EXPORT -const char *cs_group_name(csh handle, unsigned int group_id); +const char * CAPSTONE_API cs_group_name(csh handle, unsigned int group_id); /* Check if a disassembled instruction belong to a particular group. @@ -582,7 +587,7 @@ const char *cs_group_name(csh handle, unsigned int group_id); @return: true if this instruction indeed belongs to aboved group, or false otherwise. */ CAPSTONE_EXPORT -bool cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id); +bool CAPSTONE_API cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id); /* Check if a disassembled instruction IMPLICITLY used a particular register. @@ -600,7 +605,7 @@ bool cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id); @return: true if this instruction indeed implicitly used aboved register, or false otherwise. */ CAPSTONE_EXPORT -bool cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id); +bool CAPSTONE_API cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id); /* Check if a disassembled instruction IMPLICITLY modified a particular register. @@ -618,7 +623,7 @@ bool cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id); @return: true if this instruction indeed implicitly modified aboved register, or false otherwise. */ CAPSTONE_EXPORT -bool cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id); +bool CAPSTONE_API cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id); /* Count the number of operands of a given type. @@ -634,7 +639,7 @@ bool cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id); or -1 on failure. */ CAPSTONE_EXPORT -int cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type); +int CAPSTONE_API cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type); /* Retrieve the position of operand of given type in .operands[] array. @@ -653,7 +658,7 @@ int cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type); in instruction @insn, or -1 on failure. */ CAPSTONE_EXPORT -int cs_op_index(csh handle, const cs_insn *insn, unsigned int op_type, +int CAPSTONE_API cs_op_index(csh handle, const cs_insn *insn, unsigned int op_type, unsigned int position); #ifdef __cplusplus