Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 3 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 4 | #include <stddef.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <capstone.h> |
| 9 | |
| 10 | #include "cs_priv.h" |
| 11 | |
| 12 | #include "MCRegisterInfo.h" |
| 13 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | #include "utils.h" |
| 15 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 16 | cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL }; |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 17 | cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL }; |
| 18 | void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL }; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 19 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 20 | unsigned int all_arch = 0; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 21 | |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 22 | |
Nguyen Anh Quynh | b90cb99 | 2013-12-28 13:59:09 +0800 | [diff] [blame] | 23 | unsigned int cs_version(int *major, int *minor) |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 24 | { |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 25 | if (major != NULL && minor != NULL) { |
| 26 | *major = CS_API_MAJOR; |
| 27 | *minor = CS_API_MINOR; |
| 28 | } |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 29 | |
| 30 | return (CS_API_MAJOR << 8) + CS_API_MINOR; |
| 31 | } |
| 32 | |
| 33 | bool cs_support(cs_arch arch) |
| 34 | { |
| 35 | if (arch == CS_ARCH_ALL) |
| 36 | return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 37 | (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) | |
| 38 | (1 << CS_ARCH_PPC)); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 39 | |
| 40 | return all_arch & (1 << arch); |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 43 | cs_err cs_errno(csh handle) |
| 44 | { |
| 45 | if (!handle) |
| 46 | return CS_ERR_CSH; |
| 47 | |
| 48 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 49 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 50 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame^] | 53 | const char *cs_strerror(cs_err code) |
| 54 | { |
| 55 | switch(code) { |
| 56 | default: |
| 57 | return "Unknown error code"; |
| 58 | case CS_ERR_OK: |
| 59 | return "OK (CS_ERR_OK)"; |
| 60 | case CS_ERR_MEM: |
| 61 | return "Out of memory (CS_ERR_MEM)"; |
| 62 | case CS_ERR_ARCH: |
| 63 | return "Invalid architecture (CS_ERR_ARCH)"; |
| 64 | case CS_ERR_HANDLE: |
| 65 | return "Invalid handle (CS_ERR_HANDLE)"; |
| 66 | case CS_ERR_CSH: |
| 67 | return "Invalid csh (CS_ERR_CSH)"; |
| 68 | case CS_ERR_MODE: |
| 69 | return "Invalid mode (CS_ERR_MODE)"; |
| 70 | case CS_ERR_OPTION: |
| 71 | return "Invalid option (CS_ERR_OPTION)"; |
| 72 | case CS_ERR_DETAIL: |
| 73 | return "Details are unavailable (CS_ERR_DETAIL)"; |
| 74 | } |
| 75 | } |
| 76 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 77 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 78 | { |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 79 | cs_struct *ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 80 | |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 81 | ud = calloc(1, sizeof(*ud)); |
| 82 | if (!ud) { |
| 83 | // memory insufficient |
| 84 | return CS_ERR_MEM; |
| 85 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 86 | |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 87 | if (arch < CS_ARCH_MAX && arch_init[ud->arch]) { |
| 88 | ud->errnum = CS_ERR_OK; |
| 89 | ud->arch = arch; |
| 90 | ud->mode = mode; |
| 91 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
| 92 | ud->reg_name = NULL; |
| 93 | ud->detail = CS_OPT_ON; // by default break instruction into details |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 94 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 95 | arch_init[ud->arch](ud); |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 96 | } else { |
| 97 | *handle = 0; |
| 98 | return CS_ERR_ARCH; |
| 99 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 100 | |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 101 | *handle = (uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 102 | |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 103 | return CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | cs_err cs_close(csh handle) |
| 107 | { |
| 108 | if (!handle) |
| 109 | return CS_ERR_CSH; |
| 110 | |
| 111 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 112 | |
| 113 | switch (ud->arch) { |
| 114 | case CS_ARCH_X86: |
| 115 | break; |
| 116 | case CS_ARCH_ARM: |
| 117 | case CS_ARCH_MIPS: |
| 118 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 119 | case CS_ARCH_PPC: |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 120 | free(ud->printer_info); |
| 121 | break; |
| 122 | default: // unsupported architecture |
| 123 | return CS_ERR_HANDLE; |
| 124 | } |
| 125 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 126 | if (arch_destroy[ud->arch]) |
| 127 | arch_destroy[ud->arch](ud); |
| 128 | |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 129 | memset(ud, 0, sizeof(*ud)); |
| 130 | free(ud); |
| 131 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 132 | return CS_ERR_OK; |
| 133 | } |
| 134 | |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 135 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 136 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 137 | // fill insn with mnemonic & operands info |
| 138 | static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 139 | PostPrinter_t postprinter, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 140 | { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 141 | if (handle->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 142 | // avoiding copy insn->detail |
| 143 | memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 144 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 145 | // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8 |
| 146 | // copy from @regs_read until @arm |
| 147 | memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read), |
| 148 | offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read)); |
| 149 | // then copy from @arm until end |
| 150 | memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm), |
| 151 | sizeof(cs_detail) - offsetof(cs_detail, arm)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 152 | } else { |
| 153 | insn->address = mci->address; |
| 154 | insn->size = mci->insn_size; |
| 155 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 156 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 157 | // fill the instruction bytes |
| 158 | memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size)); |
| 159 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 160 | // map internal instruction opcode to public insn ID |
| 161 | if (handle->insn_id) |
| 162 | handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail); |
| 163 | |
| 164 | // alias instruction might have ID saved in OpcodePub |
| 165 | if (MCInst_getOpcodePub(mci)) |
| 166 | insn->id = MCInst_getOpcodePub(mci); |
| 167 | |
| 168 | // post printer handles some corner cases (hacky) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 169 | if (postprinter) |
| 170 | postprinter((csh)handle, insn, buffer); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 171 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 172 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 173 | // find first space or tab |
| 174 | char *sp = buffer; |
| 175 | for (sp = buffer; *sp; sp++) |
| 176 | if (*sp == ' '||*sp == '\t') |
| 177 | break; |
| 178 | if (*sp) { |
| 179 | *sp = '\0'; |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 180 | // find the next non-space char |
| 181 | sp++; |
| 182 | for (; ((*sp == ' ') || (*sp == '\t')); sp++); |
| 183 | strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 184 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 185 | } else |
| 186 | insn->op_str[0] = '\0'; |
| 187 | |
| 188 | strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); |
| 189 | insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; |
| 190 | } |
| 191 | |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 192 | cs_err cs_option(csh ud, cs_opt_type type, size_t value) |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 193 | { |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 194 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 195 | if (!handle) |
| 196 | return CS_ERR_CSH; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 197 | |
danghvu | 0b6ea04 | 2013-12-19 23:07:26 -0600 | [diff] [blame] | 198 | if (type == CS_OPT_DETAIL) { |
Nguyen Anh Quynh | 7d02c92 | 2013-12-21 09:59:31 +0800 | [diff] [blame] | 199 | handle->detail = value; |
| 200 | return CS_ERR_OK; |
| 201 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 202 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 203 | return arch_option[handle->arch](handle, type, value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 206 | // dynamicly allocate memory to contain disasm insn |
| 207 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
Nguyen Anh Quynh | 04c19be | 2013-12-25 13:26:22 +0800 | [diff] [blame] | 208 | size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 209 | { |
| 210 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 211 | MCInst mci; |
| 212 | uint16_t insn_size; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 213 | size_t c = 0, f = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 214 | cs_insn insn_cache[64]; |
| 215 | void *total = NULL; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 216 | size_t total_size = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 217 | |
| 218 | if (!handle) { |
| 219 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 220 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 224 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 225 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 226 | memset(insn_cache, 0, sizeof(insn_cache)); |
| 227 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 228 | while (size > 0) { |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 229 | MCInst_Init(&mci); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 230 | mci.csh = handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 231 | |
| 232 | bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
| 233 | if (r) { |
| 234 | SStream ss; |
| 235 | SStream_Init(&ss); |
| 236 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 237 | // relative branches need to know the address & size of current insn |
| 238 | mci.insn_size = insn_size; |
| 239 | mci.address = offset; |
| 240 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 241 | if (handle->detail) { |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 242 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 243 | mci.flat_insn.address = offset; |
| 244 | mci.flat_insn.size = insn_size; |
| 245 | // allocate memory for @detail pointer |
| 246 | insn_cache[f].detail = calloc(1, sizeof(cs_detail)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 249 | handle->printer(&mci, &ss, handle->printer_info); |
| 250 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 251 | fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 252 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 253 | f++; |
| 254 | |
| 255 | if (f == ARR_SIZE(insn_cache)) { |
| 256 | // resize total to contain newly disasm insns |
| 257 | total_size += sizeof(insn_cache); |
| 258 | void *tmp = realloc(total, total_size); |
| 259 | if (tmp == NULL) { // insufficient memory |
| 260 | free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 261 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | total = tmp; |
| 266 | memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache)); |
| 267 | // reset f back to 0 |
| 268 | f = 0; |
| 269 | } |
| 270 | |
| 271 | c++; |
| 272 | buffer += insn_size; |
| 273 | size -= insn_size; |
| 274 | offset += insn_size; |
| 275 | |
| 276 | if (count > 0 && c == count) |
| 277 | break; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 278 | } else { |
| 279 | // encounter a broken instruction |
| 280 | // XXX: TODO: JOXEAN continue here |
| 281 | break; |
| 282 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (f) { |
| 286 | // resize total to contain newly disasm insns |
| 287 | void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0])); |
| 288 | if (tmp == NULL) { // insufficient memory |
| 289 | free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 290 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | total = tmp; |
| 295 | memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0])); |
| 296 | } |
| 297 | |
| 298 | *insn = total; |
| 299 | |
| 300 | return c; |
| 301 | } |
| 302 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 303 | void cs_free(cs_insn *insn, size_t count) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 304 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 305 | size_t i; |
| 306 | |
| 307 | // free all detail pointers |
| 308 | for (i = 0; i < count; i++) |
| 309 | free(insn[i].detail); |
| 310 | |
| 311 | // then free pointer to cs_insn array |
| 312 | free(insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // return friendly name of regiser in a string |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 316 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 317 | { |
| 318 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 319 | |
| 320 | if (!handle || handle->reg_name == NULL) { |
| 321 | return NULL; |
| 322 | } |
| 323 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 324 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 325 | } |
| 326 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 327 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 328 | { |
| 329 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 330 | |
| 331 | if (!handle || handle->insn_name == NULL) { |
| 332 | return NULL; |
| 333 | } |
| 334 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 335 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 336 | } |
| 337 | |
Nguyen Anh Quynh | 7008356 | 2013-12-20 22:02:20 +0800 | [diff] [blame] | 338 | static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 339 | { |
| 340 | int i; |
| 341 | |
| 342 | for (i = 0; i < max; i++) { |
| 343 | if (arr[i] == id) |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | return false; |
| 348 | } |
| 349 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 350 | bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 351 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 352 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 353 | return false; |
| 354 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 355 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 356 | |
| 357 | if (!handle->detail) { |
| 358 | handle->errnum = CS_ERR_DETAIL; |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 365 | bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 366 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 367 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 368 | return false; |
| 369 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 370 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 371 | |
| 372 | if (!handle->detail) { |
| 373 | handle->errnum = CS_ERR_DETAIL; |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 378 | } |
| 379 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 380 | bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 381 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 382 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 383 | return false; |
| 384 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 385 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 386 | |
| 387 | if (!handle->detail) { |
| 388 | handle->errnum = CS_ERR_DETAIL; |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) |
| 396 | { |
| 397 | if (!ud) |
| 398 | return -1; |
| 399 | |
| 400 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 401 | unsigned int count = 0, i; |
| 402 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 403 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 404 | |
| 405 | switch (handle->arch) { |
| 406 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 407 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 408 | return -1; |
| 409 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 410 | for (i = 0; i < insn->detail->arm.op_count; i++) |
| 411 | if (insn->detail->arm.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 412 | count++; |
| 413 | break; |
| 414 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 415 | for (i = 0; i < insn->detail->arm64.op_count; i++) |
| 416 | if (insn->detail->arm64.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 417 | count++; |
| 418 | break; |
| 419 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 420 | for (i = 0; i < insn->detail->x86.op_count; i++) |
| 421 | if (insn->detail->x86.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 422 | count++; |
| 423 | break; |
| 424 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 425 | for (i = 0; i < insn->detail->mips.op_count; i++) |
| 426 | if (insn->detail->mips.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 427 | count++; |
| 428 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 429 | case CS_ARCH_PPC: |
| 430 | for (i = 0; i < insn->detail->ppc.op_count; i++) |
| 431 | if (insn->detail->ppc.operands[i].type == op_type) |
| 432 | count++; |
| 433 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return count; |
| 437 | } |
| 438 | |
| 439 | int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, |
| 440 | unsigned int post) |
| 441 | { |
| 442 | if (!ud) |
| 443 | return -1; |
| 444 | |
| 445 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 446 | unsigned int count = 0, i; |
| 447 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 448 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 449 | |
| 450 | switch (handle->arch) { |
| 451 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 452 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 453 | return -1; |
| 454 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 455 | for (i = 0; i < insn->detail->arm.op_count; i++) { |
| 456 | if (insn->detail->arm.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 457 | count++; |
| 458 | if (count == post) |
| 459 | return i; |
| 460 | } |
| 461 | break; |
| 462 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 463 | for (i = 0; i < insn->detail->arm64.op_count; i++) { |
| 464 | if (insn->detail->arm64.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 465 | count++; |
| 466 | if (count == post) |
| 467 | return i; |
| 468 | } |
| 469 | break; |
| 470 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 471 | for (i = 0; i < insn->detail->x86.op_count; i++) { |
| 472 | if (insn->detail->x86.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 473 | count++; |
| 474 | if (count == post) |
| 475 | return i; |
| 476 | } |
| 477 | break; |
| 478 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 479 | for (i = 0; i < insn->detail->mips.op_count; i++) { |
| 480 | if (insn->detail->mips.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 481 | count++; |
| 482 | if (count == post) |
| 483 | return i; |
| 484 | } |
| 485 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 486 | case CS_ARCH_PPC: |
| 487 | for (i = 0; i < insn->detail->ppc.op_count; i++) { |
| 488 | if (insn->detail->ppc.operands[i].type == op_type) |
| 489 | count++; |
| 490 | if (count == post) |
| 491 | return i; |
| 492 | } |
| 493 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | return -1; |
| 497 | } |