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