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