blob: c627657f1c2f50e4614ee8492a9c98143d8a8595 [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
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080011#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080013#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080014#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080015#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080016// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080017#define INSN_CACHE_SIZE 8
18#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080019
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080020// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080021#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080022
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080023cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080024cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
25void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080026
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080027extern void ARM_enable(void);
28extern void AArch64_enable(void);
29extern void Mips_enable(void);
30extern void X86_enable(void);
31extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080032extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080033extern void SystemZ_enable(void);
danghvu701b8502014-01-09 11:06:44 +070034
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080035static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080036{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037 static bool initialized = false;
38
39 if (initialized)
40 return;
41
danghvub33bd2c2014-01-09 12:22:56 +070042#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080043 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070044#endif
45#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070047#endif
48#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080049 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070050#endif
danghvub33bd2c2014-01-09 12:22:56 +070051#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070053#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080054#ifdef CAPSTONE_HAS_SPARC
55 Sparc_enable();
56#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080057#ifdef CAPSTONE_HAS_SYSZ
58 SystemZ_enable();
59#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080060#ifdef CAPSTONE_HAS_X86
61 X86_enable();
62#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080063
64 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070065}
66
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080067unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080068
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080069#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080070cs_malloc_t cs_mem_malloc = malloc;
71cs_calloc_t cs_mem_calloc = calloc;
72cs_realloc_t cs_mem_realloc = realloc;
73cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080074cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080075#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080076cs_malloc_t cs_mem_malloc = NULL;
77cs_calloc_t cs_mem_calloc = NULL;
78cs_realloc_t cs_mem_realloc = NULL;
79cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080080cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080081#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080082
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080083unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080084{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080085 archs_enable();
86
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080087 if (major != NULL && minor != NULL) {
88 *major = CS_API_MAJOR;
89 *minor = CS_API_MINOR;
90 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080091
92 return (CS_API_MAJOR << 8) + CS_API_MINOR;
93}
94
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080095bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080096{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080097 archs_enable();
98
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080099 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800100 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800101 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800102 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
103 (1 << CS_ARCH_SYSZ));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800104
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800105 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800106 return all_arch & (1 << query);
107
108 if (query == CS_SUPPORT_DIET) {
109#ifdef CAPSTONE_DIET
110 return true;
111#else
112 return false;
113#endif
114 }
115
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800116 if (query == CS_SUPPORT_X86_REDUCE) {
117#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800118 return true;
119#else
120 return false;
121#endif
122 }
123
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800124 // unsupported query
125 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800126}
127
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800128cs_err cs_errno(csh handle)
129{
130 if (!handle)
131 return CS_ERR_CSH;
132
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800133 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800134
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800135 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800136}
137
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800138const char *cs_strerror(cs_err code)
139{
140 switch(code) {
141 default:
142 return "Unknown error code";
143 case CS_ERR_OK:
144 return "OK (CS_ERR_OK)";
145 case CS_ERR_MEM:
146 return "Out of memory (CS_ERR_MEM)";
147 case CS_ERR_ARCH:
148 return "Invalid architecture (CS_ERR_ARCH)";
149 case CS_ERR_HANDLE:
150 return "Invalid handle (CS_ERR_HANDLE)";
151 case CS_ERR_CSH:
152 return "Invalid csh (CS_ERR_CSH)";
153 case CS_ERR_MODE:
154 return "Invalid mode (CS_ERR_MODE)";
155 case CS_ERR_OPTION:
156 return "Invalid option (CS_ERR_OPTION)";
157 case CS_ERR_DETAIL:
158 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800159 case CS_ERR_MEMSETUP:
160 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800161 case CS_ERR_VERSION:
162 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800163 case CS_ERR_DIET:
164 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800165 case CS_ERR_SKIPDATA:
166 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800167 }
168}
169
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
171{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800172 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800173 // Error: before cs_open(), dynamic memory management must be initialized
174 // with cs_option(CS_OPT_MEM)
175 return CS_ERR_MEMSETUP;
176
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800177 archs_enable();
178
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800179 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800180 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800182 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800183 if (!ud) {
184 // memory insufficient
185 return CS_ERR_MEM;
186 }
187
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800188 ud->errnum = CS_ERR_OK;
189 ud->arch = arch;
190 ud->mode = mode;
191 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800192 // by default, do not break instruction into details
193 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800194
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800195 // default skipdata setup
196 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
197
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800198 cs_err err = arch_init[ud->arch](ud);
199 if (err) {
200 cs_mem_free(ud);
201 *handle = 0;
202 return err;
203 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800204
205 *handle = (uintptr_t)ud;
206
207 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800208 } else {
209 *handle = 0;
210 return CS_ERR_ARCH;
211 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212}
213
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800214cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215{
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800216 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800217 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800218 return CS_ERR_CSH;
219
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800220 struct cs_struct *ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800221
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800222 if (ud->printer_info)
223 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800225 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800226
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800227 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800228 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800229 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800230
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800231 // invalidate this handle by ZERO out its value.
232 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800233 *handle = 0;
234
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800235 return CS_ERR_OK;
236}
237
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800238#define MIN(x, y) ((x) < (y) ? (x) : (y))
239
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800241static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800242 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800243{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800244 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800245 // avoiding copy insn->detail
246 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800247
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800248 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
249 // copy from @regs_read until @arm
250 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
251 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
252 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800253 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
254 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800255 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800256 } else {
257 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800258 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800259 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800260
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800261 // fill the instruction bytes
262 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
263
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800264 // map internal instruction opcode to public insn ID
265 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800266 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800267
268 // alias instruction might have ID saved in OpcodePub
269 if (MCInst_getOpcodePub(mci))
270 insn->id = MCInst_getOpcodePub(mci);
271
272 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800273 if (postprinter)
274 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800275
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800276#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800278 // find first space or tab
279 char *sp = buffer;
280 for (sp = buffer; *sp; sp++)
281 if (*sp == ' '||*sp == '\t')
282 break;
283 if (*sp) {
284 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800285 // find the next non-space char
286 sp++;
287 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
288 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
290 } else
291 insn->op_str[0] = '\0';
292
293 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
294 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800295#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800296}
297
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800298// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800299// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800300static uint8_t skipdata_size(cs_struct *handle)
301{
302 switch(handle->arch) {
303 default:
304 // should never reach
305 return -1;
306 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800307 // skip 2 bytes on Thumb mode.
308 if (handle->mode & CS_MODE_THUMB)
309 return 2;
310 // otherwise, skip 4 bytes
311 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800312 case CS_ARCH_ARM64:
313 case CS_ARCH_MIPS:
314 case CS_ARCH_PPC:
315 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800316 // skip 4 bytes
317 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800318 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800319 // SystemZ instruction's length can be 2, 4 or 6 bytes,
320 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800321 return 2;
322 case CS_ARCH_X86:
323 // X86 has no restriction on instruction alignment
324 return 1;
325 }
326}
327
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800328cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800329{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800330 archs_enable();
331
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800332 // cs_option() can be called with NULL handle just for CS_OPT_MEM
333 // This is supposed to be executed before all other APIs (even cs_open())
334 if (type == CS_OPT_MEM) {
335 cs_opt_mem *mem = (cs_opt_mem *)value;
336
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800337 cs_mem_malloc = mem->malloc;
338 cs_mem_calloc = mem->calloc;
339 cs_mem_realloc = mem->realloc;
340 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800341 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800342
343 return CS_ERR_OK;
344 }
345
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800346 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600347 if (!handle)
348 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800349
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800350 switch(type) {
351 default:
352 break;
353 case CS_OPT_DETAIL:
354 handle->detail = value;
355 return CS_ERR_OK;
356 case CS_OPT_SKIPDATA:
357 handle->skipdata = (value == CS_OPT_ON);
358 if (handle->skipdata) {
359 if (handle->skipdata_size == 0) {
360 // set the default skipdata size
361 handle->skipdata_size = skipdata_size(handle);
362 }
363 }
364 return CS_ERR_OK;
365 case CS_OPT_SKIPDATA_SETUP:
366 if (value)
367 handle->skipdata_setup = *((cs_opt_skipdata *)value);
368 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800369 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800370
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800371 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800372}
373
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800374// get previous instruction, which can be in the cache, or in total buffer
375static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
376{
377 if (f == 0) {
378 if (total == NULL)
379 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800380 // get the trailing insn from total buffer, which is at
381 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800382 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800383 } else
384 return &cache[f - 1];
385}
386
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800387// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800388static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
389{
390 char *p = opstr;
391 int len;
392 size_t i;
393
394 if (!size) {
395 opstr[0] = '\0';
396 return;
397 }
398
399 len = sprintf(p, "0x%02x", buffer[0]);
400 p+= len;
401
402 for(i = 1; i < size; i++) {
403 len = sprintf(p, ", 0x%02x", buffer[i]);
404 p+= len;
405 }
406}
407
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800408// dynamicly allocate memory to contain disasm insn
409// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800410size_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 +0800411{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800412 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413 MCInst mci;
414 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800415 size_t c = 0;
416 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800417 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800419 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800420 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800421 void *tmp;
422 size_t skipdata_bytes;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800423 uint64_t offset_org;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800425 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800426 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800427 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800428 return 0;
429 }
430
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800431 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800433 // reset previous prefix for X86
434 handle->prev_prefix = 0;
435
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800436 memset(insn_cache, 0, sizeof(insn_cache));
437
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800438 offset_org = offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600440 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800441 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800443 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800444 if (r) {
445 SStream ss;
446 SStream_Init(&ss);
447
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800448 // relative branches need to know the address & size of current insn
449 mci.insn_size = insn_size;
450 mci.address = offset;
451
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800452 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800453 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800454 mci.flat_insn.address = offset;
455 mci.flat_insn.size = insn_size;
456 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800457 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800458 }
459
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460 handle->printer(&mci, &ss, handle->printer_info);
461
Joxean114df0e2013-12-04 07:11:32 +0100462 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800463
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800464 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
465 f++;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800466 if (f == ARR_SIZE(insn_cache)) {
467 // resize total to contain newly disasm insns
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800468 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800469 tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800470 if (tmp == NULL) { // insufficient memory
471 cs_mem_free(total);
472 handle->errnum = CS_ERR_MEM;
473 return 0;
474 }
475
476 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800477 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800478
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800479 // reset f back to 0
480 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481 }
482
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800483 c++;
484 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800485 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800486 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
487 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800488 }
489
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490 buffer += insn_size;
491 size -= insn_size;
492 offset += insn_size;
493
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800494 if (count > 0) {
495 // x86 hacky
496 if (!handle->prev_prefix) {
497 if (c == count)
498 break;
499 } else {
500 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800501 if (c == count + 1) {
502 // the last insn is redundant
503 c--;
504 f--;
505 // free allocated detail pointer of the last redundant instruction
506 if (handle->detail)
507 cs_mem_free(insn_cache[f].detail);
508
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800509 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800510 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800511 }
512 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800513 } else {
514 // encounter a broken instruction
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800515 // if there is no request to skip data, or remaining data is too small,
516 // then bail out
517 if (!handle->skipdata || handle->skipdata_size > size)
518 break;
519
520 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800521 skipdata_bytes = handle->skipdata_setup.callback(offset - offset_org,
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800522 handle->skipdata_setup.user_data);
523 if (skipdata_bytes > size)
524 // remaining data is not enough
525 break;
526
527 if (!skipdata_bytes)
528 // user requested not to skip data, so bail out
529 break;
530 } else
531 skipdata_bytes = handle->skipdata_size;
532
533 // we have to skip some amount of data, depending on arch & mode
534 insn_cache[f].id = 0; // invalid ID for this "data" instruction
535 insn_cache[f].address = offset;
536 insn_cache[f].size = skipdata_bytes;
537 memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
538 strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
539 sizeof(insn_cache[f].mnemonic) - 1);
540 skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
541 insn_cache[f].detail = NULL;
542
543 f++;
544 if (f == ARR_SIZE(insn_cache)) {
545 // resize total to contain newly disasm insns
546
547 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
548 tmp = cs_mem_realloc(total, total_size);
549 if (tmp == NULL) { // insufficient memory
550 cs_mem_free(total);
551 handle->errnum = CS_ERR_MEM;
552 return 0;
553 }
554
555 total = tmp;
556 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
557
558 // reset f back to 0
559 f = 0;
560 }
561
562 buffer += skipdata_bytes;
563 size -= skipdata_bytes;
564 offset += skipdata_bytes;
565 c++;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800566 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800567 }
568
569 if (f) {
570 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800571 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800572 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800573 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800574 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800575 return 0;
576 }
577
578 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800579 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800580
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800581 }
582
583 *insn = total;
584
585 return c;
586}
587
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800588void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800589{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800590 size_t i;
591
592 // free all detail pointers
593 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800594 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800595
596 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800597 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800598}
599
600// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100601const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800602{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800603 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800604
605 if (!handle || handle->reg_name == NULL) {
606 return NULL;
607 }
608
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800609 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800610}
611
pancakef0e4eed2013-12-11 22:14:42 +0100612const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800613{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800614 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800615
616 if (!handle || handle->insn_name == NULL) {
617 return NULL;
618 }
619
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800620 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800621}
622
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800623static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800624{
625 int i;
626
627 for (i = 0; i < max; i++) {
628 if (arr[i] == id)
629 return true;
630 }
631
632 return false;
633}
634
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800635bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800636{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800637 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800638 return false;
639
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800640 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800641 if (!handle->detail) {
642 handle->errnum = CS_ERR_DETAIL;
643 return false;
644 }
645
646 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800647}
648
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800649bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800650{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800651 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800652 return false;
653
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800654 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800655 if (!handle->detail) {
656 handle->errnum = CS_ERR_DETAIL;
657 return false;
658 }
659
660 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800661}
662
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800663bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800664{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800665 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800666 return false;
667
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800668 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800669 if (!handle->detail) {
670 handle->errnum = CS_ERR_DETAIL;
671 return false;
672 }
673
674 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800675}
676
677int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
678{
679 if (!ud)
680 return -1;
681
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800682 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800683 if (!handle->detail) {
684 handle->errnum = CS_ERR_DETAIL;
685 return -1;
686 }
687
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800688 unsigned int count = 0, i;
689
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800690 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800691
692 switch (handle->arch) {
693 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800694 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800695 return -1;
696 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800697 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800698 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800699 count++;
700 break;
701 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800702 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800703 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800704 count++;
705 break;
706 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800707 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800708 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800709 count++;
710 break;
711 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800712 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800713 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800714 count++;
715 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800716 case CS_ARCH_PPC:
717 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800718 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800719 count++;
720 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800721 case CS_ARCH_SPARC:
722 for (i = 0; i < insn->detail->sparc.op_count; i++)
723 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
724 count++;
725 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800726 case CS_ARCH_SYSZ:
727 for (i = 0; i < insn->detail->sysz.op_count; i++)
728 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
729 count++;
730 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800731 }
732
733 return count;
734}
735
736int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
737 unsigned int post)
738{
739 if (!ud)
740 return -1;
741
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800742 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800743 if (!handle->detail) {
744 handle->errnum = CS_ERR_DETAIL;
745 return -1;
746 }
747
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800748 unsigned int count = 0, i;
749
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800750 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800751
752 switch (handle->arch) {
753 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800754 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800755 return -1;
756 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800757 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800758 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800759 count++;
760 if (count == post)
761 return i;
762 }
763 break;
764 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800765 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800766 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800767 count++;
768 if (count == post)
769 return i;
770 }
771 break;
772 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800773 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800774 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800775 count++;
776 if (count == post)
777 return i;
778 }
779 break;
780 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800781 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800782 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800783 count++;
784 if (count == post)
785 return i;
786 }
787 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800788 case CS_ARCH_PPC:
789 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800790 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800791 count++;
792 if (count == post)
793 return i;
794 }
795 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800796 case CS_ARCH_SPARC:
797 for (i = 0; i < insn->detail->sparc.op_count; i++) {
798 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
799 count++;
800 if (count == post)
801 return i;
802 }
803 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800804 case CS_ARCH_SYSZ:
805 for (i = 0; i < insn->detail->sysz.op_count; i++) {
806 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
807 count++;
808 if (count == post)
809 return i;
810 }
811 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800812 }
813
814 return -1;
815}