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