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 | |
| 13 | #include "arch/X86/X86Disassembler.h" |
| 14 | #include "arch/X86/X86InstPrinter.h" |
| 15 | #include "arch/X86/mapping.h" |
| 16 | |
| 17 | #include "arch/ARM/ARMDisassembler.h" |
| 18 | #include "arch/ARM/ARMInstPrinter.h" |
| 19 | #include "arch/ARM/mapping.h" |
| 20 | |
| 21 | #include "arch/Mips/MipsDisassembler.h" |
| 22 | #include "arch/Mips/MipsInstPrinter.h" |
| 23 | #include "arch/Mips/mapping.h" |
| 24 | |
| 25 | #include "arch/AArch64/AArch64Disassembler.h" |
| 26 | #include "arch/AArch64/AArch64InstPrinter.h" |
| 27 | #include "arch/AArch64/mapping.h" |
| 28 | |
| 29 | #include "utils.h" |
| 30 | |
Nguyen Anh Quynh | 5dbe12a | 2013-12-03 12:27:46 +0800 | [diff] [blame] | 31 | // Package version |
| 32 | #define PKG_MAJOR 1 |
| 33 | #define PKG_MINOR 0 |
| 34 | |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 35 | |
| 36 | void cs_version(int *major, int *minor) |
| 37 | { |
| 38 | *major = CS_API_MAJOR; |
| 39 | *minor = CS_API_MINOR; |
| 40 | } |
| 41 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 42 | cs_err cs_errno(csh handle) |
| 43 | { |
| 44 | if (!handle) |
| 45 | return CS_ERR_CSH; |
| 46 | |
| 47 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 48 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 49 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 52 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 53 | { |
| 54 | cs_struct *ud; |
| 55 | |
| 56 | ud = calloc(1, sizeof(*ud)); |
| 57 | if (!ud) { |
| 58 | // memory insufficient |
| 59 | return CS_ERR_MEM; |
| 60 | } |
| 61 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 62 | ud->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 63 | ud->arch = arch; |
| 64 | ud->mode = mode; |
| 65 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
| 66 | ud->reg_name = NULL; |
| 67 | |
| 68 | switch (ud->arch) { |
| 69 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 70 | // by default, we use Intel syntax |
| 71 | ud->printer = X86_Intel_printInst; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 72 | ud->printer_info = NULL; |
| 73 | ud->disasm = X86_getInstruction; |
| 74 | ud->reg_name = X86_reg_name; |
| 75 | ud->insn_id = X86_get_insn_id; |
| 76 | ud->insn_name = X86_insn_name; |
| 77 | break; |
| 78 | case CS_ARCH_ARM: { |
| 79 | MCRegisterInfo *mri = malloc(sizeof(*mri)); |
| 80 | |
| 81 | ARM_init(mri); |
| 82 | |
| 83 | ud->printer = ARM_printInst; |
| 84 | ud->printer_info = mri; |
| 85 | ud->reg_name = ARM_reg_name; |
| 86 | ud->insn_id = ARM_get_insn_id; |
| 87 | ud->insn_name = ARM_insn_name; |
| 88 | ud->post_printer = ARM_post_printer; |
| 89 | |
| 90 | if (ud->mode & CS_MODE_THUMB) |
| 91 | ud->disasm = Thumb_getInstruction; |
| 92 | else |
| 93 | ud->disasm = ARM_getInstruction; |
| 94 | break; |
| 95 | } |
| 96 | case CS_ARCH_MIPS: { |
| 97 | MCRegisterInfo *mri = malloc(sizeof(*mri)); |
| 98 | |
| 99 | Mips_init(mri); |
| 100 | ud->printer = Mips_printInst; |
| 101 | ud->printer_info = mri; |
| 102 | ud->getinsn_info = mri; |
| 103 | ud->reg_name = Mips_reg_name; |
| 104 | ud->insn_id = Mips_get_insn_id; |
| 105 | ud->insn_name = Mips_insn_name; |
| 106 | |
| 107 | if (ud->mode & CS_MODE_32) |
| 108 | ud->disasm = Mips_getInstruction; |
| 109 | else |
| 110 | ud->disasm = Mips64_getInstruction; |
| 111 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | case CS_ARCH_ARM64: { |
| 115 | MCRegisterInfo *mri = malloc(sizeof(*mri)); |
| 116 | |
| 117 | AArch64_init(mri); |
| 118 | ud->printer = AArch64_printInst; |
| 119 | ud->printer_info = mri; |
| 120 | ud->getinsn_info = mri; |
| 121 | ud->disasm = AArch64_getInstruction; |
| 122 | ud->reg_name = AArch64_reg_name; |
| 123 | ud->insn_id = AArch64_get_insn_id; |
| 124 | ud->insn_name = AArch64_insn_name; |
| 125 | ud->post_printer = AArch64_post_printer; |
| 126 | break; |
| 127 | } |
| 128 | default: // unsupported architecture |
| 129 | free(ud); |
| 130 | return CS_ERR_ARCH; |
| 131 | } |
| 132 | |
| 133 | *handle = (uintptr_t)ud; |
| 134 | |
| 135 | return CS_ERR_OK; |
| 136 | } |
| 137 | |
| 138 | cs_err cs_close(csh handle) |
| 139 | { |
| 140 | if (!handle) |
| 141 | return CS_ERR_CSH; |
| 142 | |
| 143 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 144 | |
| 145 | switch (ud->arch) { |
| 146 | case CS_ARCH_X86: |
| 147 | break; |
| 148 | case CS_ARCH_ARM: |
| 149 | case CS_ARCH_MIPS: |
| 150 | case CS_ARCH_ARM64: |
| 151 | free(ud->printer_info); |
| 152 | break; |
| 153 | default: // unsupported architecture |
| 154 | return CS_ERR_HANDLE; |
| 155 | } |
| 156 | |
| 157 | memset(ud, 0, sizeof(*ud)); |
| 158 | free(ud); |
| 159 | |
| 160 | return CS_ERR_OK; |
| 161 | } |
| 162 | |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 163 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 164 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 165 | // fill insn with mnemonic & operands info |
| 166 | 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] | 167 | PostPrinter_t printer, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 168 | { |
| 169 | memcpy(insn, &mci->pub_insn, sizeof(*insn)); |
| 170 | |
| 171 | // map internal instruction opcode to public insn ID |
Nguyen Anh Quynh | ad61c49 | 2013-11-30 16:23:31 +0800 | [diff] [blame] | 172 | if (handle->insn_id) |
| 173 | handle->insn_id(insn, MCInst_getOpcode(mci)); |
| 174 | |
| 175 | // alias instruction might have ID saved in OpcodePub |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 176 | if (MCInst_getOpcodePub(mci)) |
Nguyen Anh Quynh | ad61c49 | 2013-11-30 16:23:31 +0800 | [diff] [blame] | 177 | insn->id = MCInst_getOpcodePub(mci); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 178 | |
| 179 | if (printer) |
| 180 | printer(insn->id, insn, buffer); |
| 181 | |
| 182 | // fill in mnemonic & operands |
| 183 | char *tab = strchr(buffer, '\t'); |
| 184 | if (tab) { |
| 185 | *tab = '\0'; |
| 186 | strncpy(insn->op_str, tab + 1, sizeof(insn->op_str) - 1); |
| 187 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 188 | } else |
| 189 | insn->op_str[0] = '\0'; |
| 190 | |
| 191 | strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); |
| 192 | insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 193 | |
| 194 | // fill the instruction bytes |
| 195 | memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 198 | 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] | 199 | { |
| 200 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 201 | if (!handle) |
| 202 | return CS_ERR_CSH; |
| 203 | |
Nguyen Anh Quynh | 4a60a56 | 2013-12-03 21:56:54 +0800 | [diff] [blame] | 204 | switch (handle->arch) { |
| 205 | default: |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 206 | handle->errnum = CS_ERR_OPTION; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 207 | return CS_ERR_OPTION; |
| 208 | |
Nguyen Anh Quynh | 4a60a56 | 2013-12-03 21:56:54 +0800 | [diff] [blame] | 209 | case CS_ARCH_X86: |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 210 | if (type & CS_OPT_SYNTAX) { |
| 211 | switch(value) { |
| 212 | default: |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 213 | handle->errnum = CS_ERR_OPTION; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 214 | return CS_ERR_OPTION; |
| 215 | |
Nguyen Anh Quynh | c618db4 | 2013-12-04 00:05:04 +0800 | [diff] [blame] | 216 | case CS_OPT_SYNTAX_INTEL: |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 217 | handle->printer = X86_Intel_printInst; |
| 218 | break; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 219 | |
Nguyen Anh Quynh | c618db4 | 2013-12-04 00:05:04 +0800 | [diff] [blame] | 220 | case CS_OPT_SYNTAX_ATT: |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 221 | handle->printer = X86_ATT_printInst; |
| 222 | break; |
| 223 | } |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 224 | } else { |
| 225 | handle->errnum = CS_ERR_OPTION; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 226 | return CS_ERR_OPTION; |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 227 | } |
Nguyen Anh Quynh | 4a60a56 | 2013-12-03 21:56:54 +0800 | [diff] [blame] | 228 | break; |
| 229 | } |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 230 | |
| 231 | return CS_ERR_OK; |
| 232 | } |
| 233 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 234 | 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] | 235 | { |
| 236 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 237 | MCInst mci; |
| 238 | uint16_t insn_size; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 239 | size_t c = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 240 | |
| 241 | if (!handle) { |
| 242 | // FIXME: handle this case? |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 243 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 247 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 248 | |
| 249 | while (size > 0) { |
| 250 | MCInst_Init(&mci); |
| 251 | |
| 252 | bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
| 253 | if (r) { |
| 254 | SStream ss; |
| 255 | SStream_Init(&ss); |
| 256 | |
| 257 | mci.pub_insn.size = insn_size; |
| 258 | mci.pub_insn.address = offset; |
| 259 | mci.mode = handle->mode; |
| 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, ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 263 | |
| 264 | c++; |
| 265 | insn++; |
| 266 | buffer += insn_size; |
| 267 | size -= insn_size; |
| 268 | offset += insn_size; |
| 269 | |
| 270 | if (count > 0) { |
| 271 | if (c == count) |
| 272 | return c; |
| 273 | } |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 274 | } else |
| 275 | // face a broken instruction? |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 276 | return c; |
| 277 | } |
| 278 | |
| 279 | return c; |
| 280 | } |
| 281 | |
| 282 | // dynamicly allocate memory to contain disasm insn |
| 283 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 284 | 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] | 285 | { |
| 286 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 287 | MCInst mci; |
| 288 | uint16_t insn_size; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 289 | size_t c = 0, f = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 290 | cs_insn insn_cache[64]; |
| 291 | void *total = NULL; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 292 | size_t total_size = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 293 | |
| 294 | if (!handle) { |
| 295 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 296 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 300 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 301 | |
| 302 | while (size > 0) { |
| 303 | MCInst_Init(&mci); |
| 304 | |
| 305 | bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
| 306 | if (r) { |
| 307 | SStream ss; |
| 308 | SStream_Init(&ss); |
| 309 | |
| 310 | mci.pub_insn.size = insn_size; |
| 311 | mci.pub_insn.address = offset; |
| 312 | mci.mode = handle->mode; |
| 313 | handle->printer(&mci, &ss, handle->printer_info); |
| 314 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 315 | fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 316 | f++; |
| 317 | |
| 318 | if (f == ARR_SIZE(insn_cache)) { |
| 319 | // resize total to contain newly disasm insns |
| 320 | total_size += sizeof(insn_cache); |
| 321 | void *tmp = realloc(total, total_size); |
| 322 | if (tmp == NULL) { // insufficient memory |
| 323 | free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 324 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | total = tmp; |
| 329 | memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache)); |
| 330 | // reset f back to 0 |
| 331 | f = 0; |
| 332 | } |
| 333 | |
| 334 | c++; |
| 335 | buffer += insn_size; |
| 336 | size -= insn_size; |
| 337 | offset += insn_size; |
| 338 | |
| 339 | if (count > 0 && c == count) |
| 340 | break; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 341 | } else { |
| 342 | // encounter a broken instruction |
| 343 | // XXX: TODO: JOXEAN continue here |
| 344 | break; |
| 345 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (f) { |
| 349 | // resize total to contain newly disasm insns |
| 350 | void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0])); |
| 351 | if (tmp == NULL) { // insufficient memory |
| 352 | free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 353 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | total = tmp; |
| 358 | memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0])); |
| 359 | } |
| 360 | |
| 361 | *insn = total; |
| 362 | |
| 363 | return c; |
| 364 | } |
| 365 | |
| 366 | void cs_free(void *m) |
| 367 | { |
| 368 | free(m); |
| 369 | } |
| 370 | |
| 371 | // return friendly name of regiser in a string |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 372 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 373 | { |
| 374 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 375 | |
| 376 | if (!handle || handle->reg_name == NULL) { |
| 377 | return NULL; |
| 378 | } |
| 379 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 380 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 381 | } |
| 382 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 383 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 384 | { |
| 385 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 386 | |
| 387 | if (!handle || handle->insn_name == NULL) { |
| 388 | return NULL; |
| 389 | } |
| 390 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 391 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static bool arr_exist(unsigned int *arr, int max, unsigned int id) |
| 395 | { |
| 396 | int i; |
| 397 | |
| 398 | for (i = 0; i < max; i++) { |
| 399 | if (arr[i] == id) |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id) |
| 407 | { |
| 408 | if (!handle) |
| 409 | return false; |
| 410 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 411 | return arr_exist(insn->groups, insn->groups_count, group_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id) |
| 415 | { |
| 416 | if (!handle) |
| 417 | return false; |
| 418 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 419 | 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] | 420 | } |
| 421 | |
| 422 | bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id) |
| 423 | { |
| 424 | if (!handle) |
| 425 | return false; |
| 426 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 427 | 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] | 428 | } |
| 429 | |
| 430 | int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) |
| 431 | { |
| 432 | if (!ud) |
| 433 | return -1; |
| 434 | |
| 435 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 436 | unsigned int count = 0, i; |
| 437 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 438 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 439 | |
| 440 | switch (handle->arch) { |
| 441 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 442 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 443 | return -1; |
| 444 | case CS_ARCH_ARM: |
| 445 | for (i = 0; i < insn->arm.op_count; i++) |
| 446 | if (insn->arm.operands[i].type == op_type) |
| 447 | count++; |
| 448 | break; |
| 449 | case CS_ARCH_ARM64: |
| 450 | for (i = 0; i < insn->arm64.op_count; i++) |
| 451 | if (insn->arm64.operands[i].type == op_type) |
| 452 | count++; |
| 453 | break; |
| 454 | case CS_ARCH_X86: |
| 455 | for (i = 0; i < insn->x86.op_count; i++) |
| 456 | if (insn->x86.operands[i].type == op_type) |
| 457 | count++; |
| 458 | break; |
| 459 | case CS_ARCH_MIPS: |
| 460 | for (i = 0; i < insn->mips.op_count; i++) |
| 461 | if (insn->mips.operands[i].type == op_type) |
| 462 | count++; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | return count; |
| 467 | } |
| 468 | |
| 469 | int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, |
| 470 | unsigned int post) |
| 471 | { |
| 472 | if (!ud) |
| 473 | return -1; |
| 474 | |
| 475 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 476 | unsigned int count = 0, i; |
| 477 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 478 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 479 | |
| 480 | switch (handle->arch) { |
| 481 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 482 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 483 | return -1; |
| 484 | case CS_ARCH_ARM: |
| 485 | for (i = 0; i < insn->arm.op_count; i++) { |
| 486 | if (insn->arm.operands[i].type == op_type) |
| 487 | count++; |
| 488 | if (count == post) |
| 489 | return i; |
| 490 | } |
| 491 | break; |
| 492 | case CS_ARCH_ARM64: |
| 493 | for (i = 0; i < insn->arm64.op_count; i++) { |
| 494 | if (insn->arm64.operands[i].type == op_type) |
| 495 | count++; |
| 496 | if (count == post) |
| 497 | return i; |
| 498 | } |
| 499 | break; |
| 500 | case CS_ARCH_X86: |
| 501 | for (i = 0; i < insn->x86.op_count; i++) { |
| 502 | if (insn->x86.operands[i].type == op_type) |
| 503 | count++; |
| 504 | if (count == post) |
| 505 | return i; |
| 506 | } |
| 507 | break; |
| 508 | case CS_ARCH_MIPS: |
| 509 | for (i = 0; i < insn->mips.op_count; i++) { |
| 510 | if (insn->mips.operands[i].type == op_type) |
| 511 | count++; |
| 512 | if (count == post) |
| 513 | return i; |
| 514 | } |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | return -1; |
| 519 | } |