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