blob: 3317db3962f2ecb8201d6c4b8311b5ab1ca73885 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4#pragma warning(disable:4996)
5#endif
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08006#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <capstone.h>
11
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080013#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010015#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080016#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080017#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080018// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080019#define INSN_CACHE_SIZE 8
20#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080021
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080022// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080023#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080024
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080025cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080026cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
27void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080028
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080029extern void ARM_enable(void);
30extern void AArch64_enable(void);
31extern void Mips_enable(void);
32extern void X86_enable(void);
33extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080034extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080035extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080036extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070037
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080038static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080039{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080040 static bool initialized = false;
41
42 if (initialized)
43 return;
44
danghvub33bd2c2014-01-09 12:22:56 +070045#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070047#endif
48#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080049 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070050#endif
51#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070053#endif
danghvub33bd2c2014-01-09 12:22:56 +070054#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080055 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070056#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080057#ifdef CAPSTONE_HAS_SPARC
58 Sparc_enable();
59#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080060#ifdef CAPSTONE_HAS_SYSZ
61 SystemZ_enable();
62#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080063#ifdef CAPSTONE_HAS_X86
64 X86_enable();
65#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080066#ifdef CAPSTONE_HAS_XCORE
67 XCore_enable();
68#endif
69
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080070
71 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070072}
73
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080074unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080075
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010076#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080077cs_malloc_t cs_mem_malloc = malloc;
78cs_calloc_t cs_mem_calloc = calloc;
79cs_realloc_t cs_mem_realloc = realloc;
80cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080081cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080082#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080083cs_malloc_t cs_mem_malloc = NULL;
84cs_calloc_t cs_mem_calloc = NULL;
85cs_realloc_t cs_mem_realloc = NULL;
86cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080087cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080088#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080089
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080090unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080091{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080092 archs_enable();
93
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080094 if (major != NULL && minor != NULL) {
95 *major = CS_API_MAJOR;
96 *minor = CS_API_MINOR;
97 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080098
99 return (CS_API_MAJOR << 8) + CS_API_MINOR;
100}
101
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800102bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800103{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800104 archs_enable();
105
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800106 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800107 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800108 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800109 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800110 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800111
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800112 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800113 return all_arch & (1 << query);
114
115 if (query == CS_SUPPORT_DIET) {
116#ifdef CAPSTONE_DIET
117 return true;
118#else
119 return false;
120#endif
121 }
122
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800123 if (query == CS_SUPPORT_X86_REDUCE) {
124#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800125 return true;
126#else
127 return false;
128#endif
129 }
130
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800131 // unsupported query
132 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800133}
134
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800135cs_err cs_errno(csh handle)
136{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800137 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800138 if (!handle)
139 return CS_ERR_CSH;
140
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100141 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800142
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800143 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800144}
145
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800146const char *cs_strerror(cs_err code)
147{
148 switch(code) {
149 default:
150 return "Unknown error code";
151 case CS_ERR_OK:
152 return "OK (CS_ERR_OK)";
153 case CS_ERR_MEM:
154 return "Out of memory (CS_ERR_MEM)";
155 case CS_ERR_ARCH:
156 return "Invalid architecture (CS_ERR_ARCH)";
157 case CS_ERR_HANDLE:
158 return "Invalid handle (CS_ERR_HANDLE)";
159 case CS_ERR_CSH:
160 return "Invalid csh (CS_ERR_CSH)";
161 case CS_ERR_MODE:
162 return "Invalid mode (CS_ERR_MODE)";
163 case CS_ERR_OPTION:
164 return "Invalid option (CS_ERR_OPTION)";
165 case CS_ERR_DETAIL:
166 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800167 case CS_ERR_MEMSETUP:
168 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800169 case CS_ERR_VERSION:
170 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800171 case CS_ERR_DIET:
172 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800173 case CS_ERR_SKIPDATA:
174 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800175 }
176}
177
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800178cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
179{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800180 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800181 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800182 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800183 // Error: before cs_open(), dynamic memory management must be initialized
184 // with cs_option(CS_OPT_MEM)
185 return CS_ERR_MEMSETUP;
186
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800187 archs_enable();
188
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800189 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800190 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800191 if (!ud) {
192 // memory insufficient
193 return CS_ERR_MEM;
194 }
195
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800196 ud->errnum = CS_ERR_OK;
197 ud->arch = arch;
198 ud->mode = mode;
199 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800200 // by default, do not break instruction into details
201 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800202
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800203 // default skipdata setup
204 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
205
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100206 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800207 if (err) {
208 cs_mem_free(ud);
209 *handle = 0;
210 return err;
211 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800212
213 *handle = (uintptr_t)ud;
214
215 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800216 } else {
217 *handle = 0;
218 return CS_ERR_ARCH;
219 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220}
221
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800222cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800224 struct cs_struct *ud;
225
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800226 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800227 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228 return CS_ERR_CSH;
229
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100230 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800232 if (ud->printer_info)
233 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800235 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800236
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800237 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800238 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800239 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800240
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800241 // invalidate this handle by ZERO out its value.
242 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800243 *handle = 0;
244
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800245 return CS_ERR_OK;
246}
247
248// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800249static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800250 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800252#ifndef CAPSTONE_DIET
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800253 char *sp;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800254#endif
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800255
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800256 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800257 // avoiding copy insn->detail
258 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800260 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
261 // copy from @regs_read until @arm
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100262 memcpy(insn->detail, (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800263 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
264 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800265 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
266 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800267 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800268 } else {
269 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800270 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800271 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800272
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800273 // fill the instruction bytes
274 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
275
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800276 // map internal instruction opcode to public insn ID
277 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800278 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800279
280 // alias instruction might have ID saved in OpcodePub
281 if (MCInst_getOpcodePub(mci))
282 insn->id = MCInst_getOpcodePub(mci);
283
284 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800285 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800286 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800287
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800288#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800290 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100291 sp = buffer;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800292 for (sp = buffer; *sp; sp++) {
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800293 if (*sp == ' '||*sp == '\t')
294 break;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800295 if (*sp == '|')
296 *sp = ' ';
297 }
298
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800299 if (*sp) {
300 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800301 // find the next non-space char
302 sp++;
303 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
304 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
306 } else
307 insn->op_str[0] = '\0';
308
Nguyen Anh Quynh1dbc9592014-05-07 14:14:07 +0800309 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800311#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800312}
313
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800314// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800315// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800316static uint8_t skipdata_size(cs_struct *handle)
317{
318 switch(handle->arch) {
319 default:
320 // should never reach
321 return -1;
322 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800323 // skip 2 bytes on Thumb mode.
324 if (handle->mode & CS_MODE_THUMB)
325 return 2;
326 // otherwise, skip 4 bytes
327 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800328 case CS_ARCH_ARM64:
329 case CS_ARCH_MIPS:
330 case CS_ARCH_PPC:
331 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800332 // skip 4 bytes
333 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800334 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800335 // SystemZ instruction's length can be 2, 4 or 6 bytes,
336 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800337 return 2;
338 case CS_ARCH_X86:
339 // X86 has no restriction on instruction alignment
340 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800341 case CS_ARCH_XCORE:
342 // XCore instruction's length can be 2 or 4 bytes,
343 // so we just skip 2 bytes
344 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800345 }
346}
347
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800348cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800349{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800350 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800351 archs_enable();
352
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800353 // cs_option() can be called with NULL handle just for CS_OPT_MEM
354 // This is supposed to be executed before all other APIs (even cs_open())
355 if (type == CS_OPT_MEM) {
356 cs_opt_mem *mem = (cs_opt_mem *)value;
357
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800358 cs_mem_malloc = mem->malloc;
359 cs_mem_calloc = mem->calloc;
360 cs_mem_realloc = mem->realloc;
361 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800362 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800363
364 return CS_ERR_OK;
365 }
366
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100367 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600368 if (!handle)
369 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800370
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800371 switch(type) {
372 default:
373 break;
374 case CS_OPT_DETAIL:
375 handle->detail = value;
376 return CS_ERR_OK;
377 case CS_OPT_SKIPDATA:
378 handle->skipdata = (value == CS_OPT_ON);
379 if (handle->skipdata) {
380 if (handle->skipdata_size == 0) {
381 // set the default skipdata size
382 handle->skipdata_size = skipdata_size(handle);
383 }
384 }
385 return CS_ERR_OK;
386 case CS_OPT_SKIPDATA_SETUP:
387 if (value)
388 handle->skipdata_setup = *((cs_opt_skipdata *)value);
389 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800390 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800391
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800392 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800393}
394
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800395// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800396static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
397{
398 char *p = opstr;
399 int len;
400 size_t i;
401
402 if (!size) {
403 opstr[0] = '\0';
404 return;
405 }
406
407 len = sprintf(p, "0x%02x", buffer[0]);
408 p+= len;
409
410 for(i = 1; i < size; i++) {
411 len = sprintf(p, ", 0x%02x", buffer[i]);
412 p+= len;
413 }
414}
415
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800416// dynamicly allocate memory to contain disasm insn
417// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800418size_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 +0800419{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800420 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421 MCInst mci;
422 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800423 size_t c = 0;
424 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800425 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800426 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800427 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800428 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800429 void *tmp;
430 size_t skipdata_bytes;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800431 uint64_t offset_org;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800433 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800434 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800435 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 return 0;
437 }
438
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800439 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800441 memset(insn_cache, 0, sizeof(insn_cache));
442
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800443 // save the original offset for SKIPDATA
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800444 offset_org = offset;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800445
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600447 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800448 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800449
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800450 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800451 if (r) {
452 SStream ss;
453 SStream_Init(&ss);
454
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800455 // relative branches need to know the address & size of current insn
456 mci.insn_size = insn_size;
457 mci.address = offset;
458
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800459 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800460 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800461 mci.flat_insn.address = offset;
462 mci.flat_insn.size = insn_size;
463 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800464 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800465 }
466
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800467 handle->printer(&mci, &ss, handle->printer_info);
468
Joxean114df0e2013-12-04 07:11:32 +0100469 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800470
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800471 f++;
472 if (f == ARR_SIZE(insn_cache)) {
473 // resize total to contain newly disasm insns
474 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
475 tmp = cs_mem_realloc(total, total_size);
476 if (tmp == NULL) { // insufficient memory
477 cs_mem_free(total);
478 handle->errnum = CS_ERR_MEM;
479 return 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480 }
481
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800482 total = tmp;
483 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
484
485 // reset f back to 0
486 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800487 }
488
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800489 c++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490 buffer += insn_size;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800491
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800492 size -= insn_size;
493 offset += insn_size;
494
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800495 if (count > 0 && c == count)
496 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800497 } else {
498 // encounter a broken instruction
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800499 // if there is no request to skip data, or remaining data is too small,
500 // then bail out
501 if (!handle->skipdata || handle->skipdata_size > size)
502 break;
503
504 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh42288ac2014-04-14 14:53:13 +0800505 skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800506 handle->skipdata_setup.user_data);
507 if (skipdata_bytes > size)
508 // remaining data is not enough
509 break;
510
511 if (!skipdata_bytes)
512 // user requested not to skip data, so bail out
513 break;
514 } else
515 skipdata_bytes = handle->skipdata_size;
516
517 // we have to skip some amount of data, depending on arch & mode
518 insn_cache[f].id = 0; // invalid ID for this "data" instruction
519 insn_cache[f].address = offset;
520 insn_cache[f].size = skipdata_bytes;
521 memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
522 strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
523 sizeof(insn_cache[f].mnemonic) - 1);
524 skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
525 insn_cache[f].detail = NULL;
526
527 f++;
528 if (f == ARR_SIZE(insn_cache)) {
529 // resize total to contain newly disasm insns
530
531 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
532 tmp = cs_mem_realloc(total, total_size);
533 if (tmp == NULL) { // insufficient memory
534 cs_mem_free(total);
535 handle->errnum = CS_ERR_MEM;
536 return 0;
537 }
538
539 total = tmp;
540 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
541
542 // reset f back to 0
543 f = 0;
544 }
545
546 buffer += skipdata_bytes;
547 size -= skipdata_bytes;
548 offset += skipdata_bytes;
549 c++;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800550 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800551 }
552
553 if (f) {
554 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800555 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800556 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800557 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800558 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800559 return 0;
560 }
561
562 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800563 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800564
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800565 }
566
567 *insn = total;
568
569 return c;
570}
571
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800572void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800573{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800574 size_t i;
575
576 // free all detail pointers
577 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800578 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800579
580 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800581 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800582}
583
584// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100585const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800586{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800587 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588
589 if (!handle || handle->reg_name == NULL) {
590 return NULL;
591 }
592
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800593 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800594}
595
pancakef0e4eed2013-12-11 22:14:42 +0100596const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800597{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800598 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800599
600 if (!handle || handle->insn_name == NULL) {
601 return NULL;
602 }
603
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800604 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800605}
606
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800607static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800608{
609 int i;
610
611 for (i = 0; i < max; i++) {
612 if (arr[i] == id)
613 return true;
614 }
615
616 return false;
617}
618
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800619bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800620{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800621 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800622 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800623 return false;
624
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100625 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200626
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800627 if (!handle->detail) {
628 handle->errnum = CS_ERR_DETAIL;
629 return false;
630 }
631
Giovanni Condello95657e02014-05-07 17:31:27 +0200632 if(!insn->id) {
633 handle->errnum = CS_ERR_SKIPDATA;
634 return false;
635 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200636
Giovanni Condello95657e02014-05-07 17:31:27 +0200637 if(!insn->detail) {
638 handle->errnum = CS_ERR_DETAIL;
639 return false;
640 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200641
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800642 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800643}
644
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800645bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800646{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800647 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800648 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800649 return false;
650
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100651 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200652
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800653 if (!handle->detail) {
654 handle->errnum = CS_ERR_DETAIL;
655 return false;
656 }
657
Giovanni Condello95657e02014-05-07 17:31:27 +0200658 if(!insn->id) {
659 handle->errnum = CS_ERR_SKIPDATA;
660 return false;
661 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200662
Giovanni Condello95657e02014-05-07 17:31:27 +0200663 if(!insn->detail) {
664 handle->errnum = CS_ERR_DETAIL;
665 return false;
666 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200667
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800668 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800669}
670
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800671bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800672{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800673 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800674 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800675 return false;
676
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100677 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200678
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800679 if (!handle->detail) {
680 handle->errnum = CS_ERR_DETAIL;
681 return false;
682 }
683
Giovanni Condello95657e02014-05-07 17:31:27 +0200684 if(!insn->id) {
685 handle->errnum = CS_ERR_SKIPDATA;
686 return false;
687 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200688
Giovanni Condello95657e02014-05-07 17:31:27 +0200689 if(!insn->detail) {
690 handle->errnum = CS_ERR_DETAIL;
691 return false;
692 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200693
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800694 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800695}
696
697int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
698{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800699 struct cs_struct *handle;
700 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800701 if (!ud)
702 return -1;
703
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100704 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200705
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800706 if (!handle->detail) {
707 handle->errnum = CS_ERR_DETAIL;
708 return -1;
709 }
710
Giovanni Condello95657e02014-05-07 17:31:27 +0200711 if(!insn->id) {
712 handle->errnum = CS_ERR_SKIPDATA;
713 return -1;
714 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200715
Giovanni Condello95657e02014-05-07 17:31:27 +0200716 if(!insn->detail) {
717 handle->errnum = CS_ERR_DETAIL;
718 return -1;
719 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200720
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800721 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800722
723 switch (handle->arch) {
724 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800725 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800726 return -1;
727 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800728 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800729 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800730 count++;
731 break;
732 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800733 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800734 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800735 count++;
736 break;
737 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800738 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800739 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800740 count++;
741 break;
742 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800743 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800744 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800745 count++;
746 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800747 case CS_ARCH_PPC:
748 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800749 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800750 count++;
751 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800752 case CS_ARCH_SPARC:
753 for (i = 0; i < insn->detail->sparc.op_count; i++)
754 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
755 count++;
756 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800757 case CS_ARCH_SYSZ:
758 for (i = 0; i < insn->detail->sysz.op_count; i++)
759 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
760 count++;
761 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800762 case CS_ARCH_XCORE:
763 for (i = 0; i < insn->detail->xcore.op_count; i++)
764 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
765 count++;
766 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800767 }
768
769 return count;
770}
771
772int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
773 unsigned int post)
774{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800775 struct cs_struct *handle;
776 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800777 if (!ud)
778 return -1;
779
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100780 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200781
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800782 if (!handle->detail) {
783 handle->errnum = CS_ERR_DETAIL;
784 return -1;
785 }
786
Giovanni Condello95657e02014-05-07 17:31:27 +0200787 if(!insn->id) {
788 handle->errnum = CS_ERR_SKIPDATA;
789 return -1;
790 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200791
Giovanni Condello95657e02014-05-07 17:31:27 +0200792 if(!insn->detail) {
793 handle->errnum = CS_ERR_DETAIL;
794 return -1;
795 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200796
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800797 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800798
799 switch (handle->arch) {
800 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800801 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800802 return -1;
803 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800804 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800805 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800806 count++;
807 if (count == post)
808 return i;
809 }
810 break;
811 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800812 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800813 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800814 count++;
815 if (count == post)
816 return i;
817 }
818 break;
819 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800820 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800821 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800822 count++;
823 if (count == post)
824 return i;
825 }
826 break;
827 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800828 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800829 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800830 count++;
831 if (count == post)
832 return i;
833 }
834 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800835 case CS_ARCH_PPC:
836 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800837 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800838 count++;
839 if (count == post)
840 return i;
841 }
842 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800843 case CS_ARCH_SPARC:
844 for (i = 0; i < insn->detail->sparc.op_count; i++) {
845 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
846 count++;
847 if (count == post)
848 return i;
849 }
850 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800851 case CS_ARCH_SYSZ:
852 for (i = 0; i < insn->detail->sysz.op_count; i++) {
853 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
854 count++;
855 if (count == post)
856 return i;
857 }
858 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800859 case CS_ARCH_XCORE:
860 for (i = 0; i < insn->detail->xcore.op_count; i++) {
861 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
862 count++;
863 if (count == post)
864 return i;
865 }
866 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800867 }
868
869 return -1;
870}