From 34f96389a93d0a96fad6406a112596a953e3c80a Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Fri, 3 Jan 2014 22:49:07 +0800 Subject: [PATCH] add new API: cs_strerror() return a string describing a given error code. this should be used together with cs_errno() --- bindings/python/capstone/capstone.py | 12 ++---------- cs.c | 24 ++++++++++++++++++++++++ include/capstone.h | 11 +++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/bindings/python/capstone/capstone.py b/bindings/python/capstone/capstone.py index 9a0492d6..7581b5b9 100644 --- a/bindings/python/capstone/capstone.py +++ b/bindings/python/capstone/capstone.py @@ -196,6 +196,7 @@ _setup_prototype(_cs, "cs_errno", ctypes.c_int, ctypes.c_size_t) _setup_prototype(_cs, "cs_option", ctypes.c_int, ctypes.c_size_t, ctypes.c_int, ctypes.c_size_t) _setup_prototype(_cs, "cs_version", ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) _setup_prototype(_cs, "cs_support", ctypes.c_bool, ctypes.c_int) +_setup_prototype(_cs, "cs_strerror", ctypes.c_char_p, ctypes.c_int) # access to error code via @errno of CsError @@ -204,16 +205,7 @@ class CsError(Exception): self.errno = errno def __str__(self): - messages = { \ - CS_ERR_MEM: "Out of memory (CsError)", - CS_ERR_ARCH: "Invalid architecture (CsError)", - CS_ERR_HANDLE: "Invalid handle (CsError)", - CS_ERR_CSH: "Invalid csh (CsError)", - CS_ERR_MODE: "Invalid mode (CsError)", - CS_ERR_OPTION: "Invalid option (CsError)", - CS_ERR_DETAIL: "Details are unavailable (CsError)", - } - return messages[self.errno] + return _cs.cs_strerror(self.errno) def cs_version(): diff --git a/cs.c b/cs.c index fa50770f..ded7cffd 100644 --- a/cs.c +++ b/cs.c @@ -50,6 +50,30 @@ cs_err cs_errno(csh handle) return ud->errnum; } +const char *cs_strerror(cs_err code) +{ + switch(code) { + default: + return "Unknown error code"; + case CS_ERR_OK: + return "OK (CS_ERR_OK)"; + case CS_ERR_MEM: + return "Out of memory (CS_ERR_MEM)"; + case CS_ERR_ARCH: + return "Invalid architecture (CS_ERR_ARCH)"; + case CS_ERR_HANDLE: + return "Invalid handle (CS_ERR_HANDLE)"; + case CS_ERR_CSH: + return "Invalid csh (CS_ERR_CSH)"; + case CS_ERR_MODE: + return "Invalid mode (CS_ERR_MODE)"; + case CS_ERR_OPTION: + return "Invalid option (CS_ERR_OPTION)"; + case CS_ERR_DETAIL: + return "Details are unavailable (CS_ERR_DETAIL)"; + } +} + cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) { cs_struct *ud; diff --git a/include/capstone.h b/include/capstone.h index 64c889a8..cc5af49d 100644 --- a/include/capstone.h +++ b/include/capstone.h @@ -224,6 +224,17 @@ cs_err cs_option(csh handle, cs_opt_type type, size_t value); */ cs_err cs_errno(csh handle); + +/* + Return a string describing given error code. + + @code: error code (see CS_ERR_* above) + + @return: returns a pointer to a string that describes the error code + passed in the argument @code +*/ +const char *cs_strerror(cs_err code); + /* Dynamicly allocate memory to contain disasm insn Disassembled instructions will be put into @*insn