summaryrefslogtreecommitdiff
path: root/src/cli/disasm.c
blob: db190593a3ea5601e4e34d4d4e24d3f867a2a78e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}