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