// The one thing asked of capstone, in C so that its structs stay its own. #include #include 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; }