diff options
Diffstat (limited to 'src/cli/disasm.c')
| -rw-r--r-- | src/cli/disasm.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/cli/disasm.c b/src/cli/disasm.c new file mode 100644 index 0000000..db19059 --- /dev/null +++ b/src/cli/disasm.c @@ -0,0 +1,20 @@ +// The one thing asked of capstone, in C so that its structs stay its own. +#include <capstone/capstone.h> +#include <stdio.h> + +static csh handle; + +// The instruction at `code`, which is at `addr`: its text into `text`, its +// length returned. 0 where there is no instruction. +size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap) { + if (!handle) { + if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0; + cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL); + } + cs_insn *insn; + if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0; + size_t size = insn->size; + snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str); + cs_free(insn, 1); + return size; +} |
