blob: fca0f120fcc99c7690573e248340953ddfb60222 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
10#include "cs_priv.h"
11
12#include "MCRegisterInfo.h"
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include "utils.h"
15
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080016cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080017cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
18void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080019
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080020unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080021
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080022malloc_t my_malloc = malloc;
23calloc_t my_calloc = calloc;
24realloc_t my_realloc = realloc;
25free_t my_free = free;
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080026
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080027unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080028{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080029 if (major != NULL && minor != NULL) {
30 *major = CS_API_MAJOR;
31 *minor = CS_API_MINOR;
32 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080033
34 return (CS_API_MAJOR << 8) + CS_API_MINOR;
35}
36
37bool cs_support(cs_arch arch)
38{
39 if (arch == CS_ARCH_ALL)
40 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080041 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
42 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080043
44 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080045}
46
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080047cs_err cs_errno(csh handle)
48{
49 if (!handle)
50 return CS_ERR_CSH;
51
52 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
53
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080054 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055}
56
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080057const char *cs_strerror(cs_err code)
58{
59 switch(code) {
60 default:
61 return "Unknown error code";
62 case CS_ERR_OK:
63 return "OK (CS_ERR_OK)";
64 case CS_ERR_MEM:
65 return "Out of memory (CS_ERR_MEM)";
66 case CS_ERR_ARCH:
67 return "Invalid architecture (CS_ERR_ARCH)";
68 case CS_ERR_HANDLE:
69 return "Invalid handle (CS_ERR_HANDLE)";
70 case CS_ERR_CSH:
71 return "Invalid csh (CS_ERR_CSH)";
72 case CS_ERR_MODE:
73 return "Invalid mode (CS_ERR_MODE)";
74 case CS_ERR_OPTION:
75 return "Invalid option (CS_ERR_OPTION)";
76 case CS_ERR_DETAIL:
77 return "Details are unavailable (CS_ERR_DETAIL)";
78 }
79}
80
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080081cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
82{
danghvu2b192962013-12-19 22:40:28 -060083 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080084
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080085 ud = my_calloc(1, sizeof(*ud));
danghvu2b192962013-12-19 22:40:28 -060086 if (!ud) {
87 // memory insufficient
88 return CS_ERR_MEM;
89 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080090
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080091 if (arch < CS_ARCH_MAX && arch_init[ud->arch]) {
92 ud->errnum = CS_ERR_OK;
93 ud->arch = arch;
94 ud->mode = mode;
95 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
96 ud->reg_name = NULL;
97 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080098
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080099 arch_init[ud->arch](ud);
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800100 } else {
101 *handle = 0;
102 return CS_ERR_ARCH;
103 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800104
danghvu2b192962013-12-19 22:40:28 -0600105 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800106
danghvu2b192962013-12-19 22:40:28 -0600107 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800108}
109
110cs_err cs_close(csh handle)
111{
112 if (!handle)
113 return CS_ERR_CSH;
114
115 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
116
117 switch (ud->arch) {
118 case CS_ARCH_X86:
119 break;
120 case CS_ARCH_ARM:
121 case CS_ARCH_MIPS:
122 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800123 case CS_ARCH_PPC:
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800124 my_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125 break;
126 default: // unsupported architecture
127 return CS_ERR_HANDLE;
128 }
129
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800130 if (arch_destroy[ud->arch])
131 arch_destroy[ud->arch](ud);
132
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800133 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800134 my_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800135
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800136 return CS_ERR_OK;
137}
138
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800139#define MIN(x, y) ((x) < (y) ? (x) : (y))
140
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800141// fill insn with mnemonic & operands info
142static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800143 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800144{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800145 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800146 // avoiding copy insn->detail
147 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800148
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800149 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
150 // copy from @regs_read until @arm
151 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
152 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
153 // then copy from @arm until end
154 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
155 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800156 } else {
157 insn->address = mci->address;
158 insn->size = mci->insn_size;
159 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800160
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800161 // fill the instruction bytes
162 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
163
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800164 // map internal instruction opcode to public insn ID
165 if (handle->insn_id)
166 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
167
168 // alias instruction might have ID saved in OpcodePub
169 if (MCInst_getOpcodePub(mci))
170 insn->id = MCInst_getOpcodePub(mci);
171
172 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800173 if (postprinter)
174 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800175
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800177 // find first space or tab
178 char *sp = buffer;
179 for (sp = buffer; *sp; sp++)
180 if (*sp == ' '||*sp == '\t')
181 break;
182 if (*sp) {
183 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800184 // find the next non-space char
185 sp++;
186 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
187 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800188 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
189 } else
190 insn->op_str[0] = '\0';
191
192 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
193 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
194}
195
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800196cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800197{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800198 // cs_option() can be called with NULL handle just for CS_OPT_MEM
199 // This is supposed to be executed before all other APIs (even cs_open())
200 if (type == CS_OPT_MEM) {
201 cs_opt_mem *mem = (cs_opt_mem *)value;
202
203 my_malloc = mem->malloc;
204 my_calloc = mem->calloc;
205 my_realloc = mem->realloc;
206 my_free = mem->free;
207
208 return CS_ERR_OK;
209 }
210
danghvu2b192962013-12-19 22:40:28 -0600211 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
212 if (!handle)
213 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800214
danghvu0b6ea042013-12-19 23:07:26 -0600215 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800216 handle->detail = value;
217 return CS_ERR_OK;
218 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800219
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800220 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800221}
222
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223// dynamicly allocate memory to contain disasm insn
224// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800225size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800226{
227 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
228 MCInst mci;
229 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800230 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231 cs_insn insn_cache[64];
232 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800233 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234
235 if (!handle) {
236 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800237 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800238 return 0;
239 }
240
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800241 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800242
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800243 memset(insn_cache, 0, sizeof(insn_cache));
244
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800245 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600246 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800247 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800248
249 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
250 if (r) {
251 SStream ss;
252 SStream_Init(&ss);
253
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800254 // relative branches need to know the address & size of current insn
255 mci.insn_size = insn_size;
256 mci.address = offset;
257
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800258 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800259 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800260 mci.flat_insn.address = offset;
261 mci.flat_insn.size = insn_size;
262 // allocate memory for @detail pointer
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800263 insn_cache[f].detail = my_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800264 }
265
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266 handle->printer(&mci, &ss, handle->printer_info);
267
Joxean114df0e2013-12-04 07:11:32 +0100268 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800269
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800270 f++;
271
272 if (f == ARR_SIZE(insn_cache)) {
273 // resize total to contain newly disasm insns
274 total_size += sizeof(insn_cache);
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800275 void *tmp = my_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800277 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800278 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800279 return 0;
280 }
281
282 total = tmp;
283 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
284 // reset f back to 0
285 f = 0;
286 }
287
288 c++;
289 buffer += insn_size;
290 size -= insn_size;
291 offset += insn_size;
292
293 if (count > 0 && c == count)
294 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800295 } else {
296 // encounter a broken instruction
297 // XXX: TODO: JOXEAN continue here
298 break;
299 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300 }
301
302 if (f) {
303 // resize total to contain newly disasm insns
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800304 void *tmp = my_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800306 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800307 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308 return 0;
309 }
310
311 total = tmp;
312 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
313 }
314
315 *insn = total;
316
317 return c;
318}
319
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800320void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800321{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800322 size_t i;
323
324 // free all detail pointers
325 for (i = 0; i < count; i++)
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800326 my_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800327
328 // then free pointer to cs_insn array
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800329 my_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330}
331
332// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100333const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334{
335 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
336
337 if (!handle || handle->reg_name == NULL) {
338 return NULL;
339 }
340
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800341 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800342}
343
pancakef0e4eed2013-12-11 22:14:42 +0100344const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345{
346 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
347
348 if (!handle || handle->insn_name == NULL) {
349 return NULL;
350 }
351
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800352 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353}
354
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800355static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356{
357 int i;
358
359 for (i = 0; i < max; i++) {
360 if (arr[i] == id)
361 return true;
362 }
363
364 return false;
365}
366
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800367bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800368{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800369 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370 return false;
371
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800372 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
373
374 if (!handle->detail) {
375 handle->errnum = CS_ERR_DETAIL;
376 return false;
377 }
378
379 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800380}
381
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800382bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800383{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800384 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800385 return false;
386
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800387 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
388
389 if (!handle->detail) {
390 handle->errnum = CS_ERR_DETAIL;
391 return false;
392 }
393
394 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395}
396
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800397bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800398{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800399 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800400 return false;
401
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800402 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
403
404 if (!handle->detail) {
405 handle->errnum = CS_ERR_DETAIL;
406 return false;
407 }
408
409 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800410}
411
412int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
413{
414 if (!ud)
415 return -1;
416
417 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
418 unsigned int count = 0, i;
419
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800420 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421
422 switch (handle->arch) {
423 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800424 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 return -1;
426 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800427 for (i = 0; i < insn->detail->arm.op_count; i++)
428 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429 count++;
430 break;
431 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800432 for (i = 0; i < insn->detail->arm64.op_count; i++)
433 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800434 count++;
435 break;
436 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800437 for (i = 0; i < insn->detail->x86.op_count; i++)
438 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439 count++;
440 break;
441 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800442 for (i = 0; i < insn->detail->mips.op_count; i++)
443 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800444 count++;
445 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800446 case CS_ARCH_PPC:
447 for (i = 0; i < insn->detail->ppc.op_count; i++)
448 if (insn->detail->ppc.operands[i].type == op_type)
449 count++;
450 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800451 }
452
453 return count;
454}
455
456int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
457 unsigned int post)
458{
459 if (!ud)
460 return -1;
461
462 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
463 unsigned int count = 0, i;
464
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800465 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466
467 switch (handle->arch) {
468 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800469 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800470 return -1;
471 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800472 for (i = 0; i < insn->detail->arm.op_count; i++) {
473 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800474 count++;
475 if (count == post)
476 return i;
477 }
478 break;
479 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800480 for (i = 0; i < insn->detail->arm64.op_count; i++) {
481 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482 count++;
483 if (count == post)
484 return i;
485 }
486 break;
487 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800488 for (i = 0; i < insn->detail->x86.op_count; i++) {
489 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490 count++;
491 if (count == post)
492 return i;
493 }
494 break;
495 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800496 for (i = 0; i < insn->detail->mips.op_count; i++) {
497 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800498 count++;
499 if (count == post)
500 return i;
501 }
502 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800503 case CS_ARCH_PPC:
504 for (i = 0; i < insn->detail->ppc.op_count; i++) {
505 if (insn->detail->ppc.operands[i].type == op_type)
506 count++;
507 if (count == post)
508 return i;
509 }
510 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800511 }
512
513 return -1;
514}