blob: 740268fd59a74613d196ea1bf157bd796d2b6c15 [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 Quynh07c36932014-06-03 18:33:15 +080090CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080091unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080092{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080093 archs_enable();
94
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080095 if (major != NULL && minor != NULL) {
96 *major = CS_API_MAJOR;
97 *minor = CS_API_MINOR;
98 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080099
100 return (CS_API_MAJOR << 8) + CS_API_MINOR;
101}
102
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800103CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800104bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800105{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800106 archs_enable();
107
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800108 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800109 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800110 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800111 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800112 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800113
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800114 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800115 return all_arch & (1 << query);
116
117 if (query == CS_SUPPORT_DIET) {
118#ifdef CAPSTONE_DIET
119 return true;
120#else
121 return false;
122#endif
123 }
124
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800125 if (query == CS_SUPPORT_X86_REDUCE) {
126#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800127 return true;
128#else
129 return false;
130#endif
131 }
132
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800133 // unsupported query
134 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800135}
136
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800137CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800138cs_err cs_errno(csh handle)
139{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800140 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800141 if (!handle)
142 return CS_ERR_CSH;
143
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100144 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800145
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800146 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147}
148
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800149CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800150const char *cs_strerror(cs_err code)
151{
152 switch(code) {
153 default:
154 return "Unknown error code";
155 case CS_ERR_OK:
156 return "OK (CS_ERR_OK)";
157 case CS_ERR_MEM:
158 return "Out of memory (CS_ERR_MEM)";
159 case CS_ERR_ARCH:
160 return "Invalid architecture (CS_ERR_ARCH)";
161 case CS_ERR_HANDLE:
162 return "Invalid handle (CS_ERR_HANDLE)";
163 case CS_ERR_CSH:
164 return "Invalid csh (CS_ERR_CSH)";
165 case CS_ERR_MODE:
166 return "Invalid mode (CS_ERR_MODE)";
167 case CS_ERR_OPTION:
168 return "Invalid option (CS_ERR_OPTION)";
169 case CS_ERR_DETAIL:
170 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800171 case CS_ERR_MEMSETUP:
172 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800173 case CS_ERR_VERSION:
174 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800175 case CS_ERR_DIET:
176 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800177 case CS_ERR_SKIPDATA:
178 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800179 }
180}
181
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800182CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800183cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
184{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800185 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800186 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800187 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800188 // Error: before cs_open(), dynamic memory management must be initialized
189 // with cs_option(CS_OPT_MEM)
190 return CS_ERR_MEMSETUP;
191
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800192 archs_enable();
193
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800194 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800195 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800196 if (!ud) {
197 // memory insufficient
198 return CS_ERR_MEM;
199 }
200
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800201 ud->errnum = CS_ERR_OK;
202 ud->arch = arch;
203 ud->mode = mode;
204 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800205 // by default, do not break instruction into details
206 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800207
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800208 // default skipdata setup
209 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
210
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100211 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800212 if (err) {
213 cs_mem_free(ud);
214 *handle = 0;
215 return err;
216 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800217
218 *handle = (uintptr_t)ud;
219
220 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800221 } else {
222 *handle = 0;
223 return CS_ERR_ARCH;
224 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225}
226
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800227CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800228cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800230 struct cs_struct *ud;
231
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800232 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800233 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234 return CS_ERR_CSH;
235
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100236 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800237
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800238 if (ud->printer_info)
239 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800241 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800242
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800243 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800244 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800245 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800246
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800247 // invalidate this handle by ZERO out its value.
248 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800249 *handle = 0;
250
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251 return CS_ERR_OK;
252}
253
254// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800255static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800256 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800257{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800258#ifndef CAPSTONE_DIET
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800259 char *sp;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800260#endif
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800261 char *mnem;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800262
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800263 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800264 // avoiding copy insn->detail
265 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800267 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
268 // copy from @regs_read until @arm
Nguyen Anh Quynh97865662014-05-28 14:32:39 +0800269 memcpy(insn->detail, (char *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800270 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
271 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800272 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
Nguyen Anh Quynh97865662014-05-28 14:32:39 +0800273 (char *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800274 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800275 } else {
276 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800277 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800278 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800279
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800280 // fill the instruction bytes
281 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
282
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800283 // map internal instruction opcode to public insn ID
Nguyen Anh Quynh07e84a22014-06-08 19:27:22 +0800284 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800285
286 // alias instruction might have ID saved in OpcodePub
287 if (MCInst_getOpcodePub(mci))
288 insn->id = MCInst_getOpcodePub(mci);
289
290 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800291 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800292 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800293
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800294#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800295 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800296 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100297 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800298 mnem = insn->mnemonic;
Nguyen Anh Quynh177dd9b2014-06-08 00:17:10 +0800299 if (mci->x86_prefix[0]) {
300 for (sp = buffer; *sp; sp++) {
301 //if (*sp == ' '||*sp == '\t')
302 if (*sp == ' ')
303 break;
304 if (*sp == '|') // lock|rep prefix for x86
305 *sp = ' ';
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800306 // copy to @mnemonic
307 *mnem = *sp;
308 mnem++;
Nguyen Anh Quynh177dd9b2014-06-08 00:17:10 +0800309 }
310 } else {
311 for (sp = buffer; *sp; sp++) {
Nguyen Anh Quynh6ddd7152014-06-08 19:11:38 +0800312 if (*sp == ' '||*sp == '\t')
Nguyen Anh Quynh177dd9b2014-06-08 00:17:10 +0800313 break;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800314 // copy to @mnemonic
315 *mnem = *sp;
316 mnem++;
Nguyen Anh Quynh177dd9b2014-06-08 00:17:10 +0800317 }
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800318 }
319
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800320 *mnem = '\0';
321
322 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800323 if (*sp) {
324 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800325 // find the next non-space char
326 sp++;
327 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
328 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800329 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
330 } else
331 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800332#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800333}
334
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800335// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800336// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800337static uint8_t skipdata_size(cs_struct *handle)
338{
339 switch(handle->arch) {
340 default:
341 // should never reach
342 return -1;
343 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800344 // skip 2 bytes on Thumb mode.
345 if (handle->mode & CS_MODE_THUMB)
346 return 2;
347 // otherwise, skip 4 bytes
348 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800349 case CS_ARCH_ARM64:
350 case CS_ARCH_MIPS:
351 case CS_ARCH_PPC:
352 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800353 // skip 4 bytes
354 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800355 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800356 // SystemZ instruction's length can be 2, 4 or 6 bytes,
357 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800358 return 2;
359 case CS_ARCH_X86:
360 // X86 has no restriction on instruction alignment
361 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800362 case CS_ARCH_XCORE:
363 // XCore instruction's length can be 2 or 4 bytes,
364 // so we just skip 2 bytes
365 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800366 }
367}
368
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800369CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800370cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800371{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800372 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800373 archs_enable();
374
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800375 // cs_option() can be called with NULL handle just for CS_OPT_MEM
376 // This is supposed to be executed before all other APIs (even cs_open())
377 if (type == CS_OPT_MEM) {
378 cs_opt_mem *mem = (cs_opt_mem *)value;
379
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800380 cs_mem_malloc = mem->malloc;
381 cs_mem_calloc = mem->calloc;
382 cs_mem_realloc = mem->realloc;
383 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800384 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800385
386 return CS_ERR_OK;
387 }
388
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100389 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600390 if (!handle)
391 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800392
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800393 switch(type) {
394 default:
395 break;
396 case CS_OPT_DETAIL:
397 handle->detail = value;
398 return CS_ERR_OK;
399 case CS_OPT_SKIPDATA:
400 handle->skipdata = (value == CS_OPT_ON);
401 if (handle->skipdata) {
402 if (handle->skipdata_size == 0) {
403 // set the default skipdata size
404 handle->skipdata_size = skipdata_size(handle);
405 }
406 }
407 return CS_ERR_OK;
408 case CS_OPT_SKIPDATA_SETUP:
409 if (value)
410 handle->skipdata_setup = *((cs_opt_skipdata *)value);
411 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800412 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800413
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800414 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800415}
416
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800417// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800418static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
419{
420 char *p = opstr;
421 int len;
422 size_t i;
423
424 if (!size) {
425 opstr[0] = '\0';
426 return;
427 }
428
429 len = sprintf(p, "0x%02x", buffer[0]);
430 p+= len;
431
432 for(i = 1; i < size; i++) {
433 len = sprintf(p, ", 0x%02x", buffer[i]);
434 p+= len;
435 }
436}
437
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438// dynamicly allocate memory to contain disasm insn
439// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800440CAPSTONE_EXPORT
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800441size_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 +0800442{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800443 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800444 MCInst mci;
445 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800446 size_t c = 0;
447 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800448 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800449 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800450 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800451 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800452 void *tmp;
453 size_t skipdata_bytes;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800454 uint64_t offset_org;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800456 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800457 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800458 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800459 return 0;
460 }
461
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800462 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800463
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800464 // save the original offset for SKIPDATA
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800465 offset_org = offset;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800466
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800467 while (size > 0) {
Nguyen Anh Quynh30c06592014-06-07 13:30:59 +0800468 MCInst_Init(handle, &mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800469 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800470
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800471 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800472 if (r) {
473 SStream ss;
474 SStream_Init(&ss);
475
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800476 // relative branches need to know the address & size of current insn
477 mci.insn_size = insn_size;
478 mci.address = offset;
479
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800480 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800481 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800482 mci.flat_insn.address = offset;
483 mci.flat_insn.size = insn_size;
484 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800485 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynhdf1acfd2014-06-07 15:39:32 +0700486 } else {
487 insn_cache[f].detail = NULL;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800488 }
489
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490 handle->printer(&mci, &ss, handle->printer_info);
491
Joxean114df0e2013-12-04 07:11:32 +0100492 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800493
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800494 f++;
495 if (f == ARR_SIZE(insn_cache)) {
496 // resize total to contain newly disasm insns
497 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
498 tmp = cs_mem_realloc(total, total_size);
499 if (tmp == NULL) { // insufficient memory
500 cs_mem_free(total);
501 handle->errnum = CS_ERR_MEM;
502 return 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800503 }
504
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800505 total = tmp;
506 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
507
508 // reset f back to 0
509 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800510 }
511
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800512 c++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513 buffer += insn_size;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800514
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800515 size -= insn_size;
516 offset += insn_size;
517
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800518 if (count > 0 && c == count)
519 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800520 } else {
521 // encounter a broken instruction
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800522 // if there is no request to skip data, or remaining data is too small,
523 // then bail out
524 if (!handle->skipdata || handle->skipdata_size > size)
525 break;
526
527 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh42288ac2014-04-14 14:53:13 +0800528 skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800529 handle->skipdata_setup.user_data);
530 if (skipdata_bytes > size)
531 // remaining data is not enough
532 break;
533
534 if (!skipdata_bytes)
535 // user requested not to skip data, so bail out
536 break;
537 } else
538 skipdata_bytes = handle->skipdata_size;
539
540 // we have to skip some amount of data, depending on arch & mode
541 insn_cache[f].id = 0; // invalid ID for this "data" instruction
542 insn_cache[f].address = offset;
543 insn_cache[f].size = skipdata_bytes;
544 memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
545 strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
546 sizeof(insn_cache[f].mnemonic) - 1);
547 skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
548 insn_cache[f].detail = NULL;
549
550 f++;
551 if (f == ARR_SIZE(insn_cache)) {
552 // resize total to contain newly disasm insns
553
554 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
555 tmp = cs_mem_realloc(total, total_size);
556 if (tmp == NULL) { // insufficient memory
557 cs_mem_free(total);
558 handle->errnum = CS_ERR_MEM;
559 return 0;
560 }
561
562 total = tmp;
563 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
564
565 // reset f back to 0
566 f = 0;
567 }
568
569 buffer += skipdata_bytes;
570 size -= skipdata_bytes;
571 offset += skipdata_bytes;
572 c++;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800573 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800574 }
575
576 if (f) {
577 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800578 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800580 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800581 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800582 return 0;
583 }
584
585 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800586 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800587
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588 }
589
590 *insn = total;
591
592 return c;
593}
594
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800595CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800596void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800597{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800598 size_t i;
599
600 // free all detail pointers
601 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800602 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800603
604 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800605 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800606}
607
608// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800609CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100610const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800611{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800612 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800613
614 if (!handle || handle->reg_name == NULL) {
615 return NULL;
616 }
617
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800618 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800619}
620
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800621CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100622const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800623{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800624 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800625
626 if (!handle || handle->insn_name == NULL) {
627 return NULL;
628 }
629
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800630 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800631}
632
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800633static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634{
635 int i;
636
637 for (i = 0; i < max; i++) {
638 if (arr[i] == id)
639 return true;
640 }
641
642 return false;
643}
644
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800645CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800646bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800647{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800648 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800649 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800650 return false;
651
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100652 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200653
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800654 if (!handle->detail) {
655 handle->errnum = CS_ERR_DETAIL;
656 return false;
657 }
658
Giovanni Condello95657e02014-05-07 17:31:27 +0200659 if(!insn->id) {
660 handle->errnum = CS_ERR_SKIPDATA;
661 return false;
662 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200663
Giovanni Condello95657e02014-05-07 17:31:27 +0200664 if(!insn->detail) {
665 handle->errnum = CS_ERR_DETAIL;
666 return false;
667 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200668
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800669 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800670}
671
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800672CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800673bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800674{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800675 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800676 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800677 return false;
678
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100679 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200680
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800681 if (!handle->detail) {
682 handle->errnum = CS_ERR_DETAIL;
683 return false;
684 }
685
Giovanni Condello95657e02014-05-07 17:31:27 +0200686 if(!insn->id) {
687 handle->errnum = CS_ERR_SKIPDATA;
688 return false;
689 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200690
Giovanni Condello95657e02014-05-07 17:31:27 +0200691 if(!insn->detail) {
692 handle->errnum = CS_ERR_DETAIL;
693 return false;
694 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200695
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800696 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800697}
698
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800699CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800700bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800701{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800702 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800703 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800704 return false;
705
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100706 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200707
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800708 if (!handle->detail) {
709 handle->errnum = CS_ERR_DETAIL;
710 return false;
711 }
712
Giovanni Condello95657e02014-05-07 17:31:27 +0200713 if(!insn->id) {
714 handle->errnum = CS_ERR_SKIPDATA;
715 return false;
716 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200717
Giovanni Condello95657e02014-05-07 17:31:27 +0200718 if(!insn->detail) {
719 handle->errnum = CS_ERR_DETAIL;
720 return false;
721 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200722
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800723 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800724}
725
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800726CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800727int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
728{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800729 struct cs_struct *handle;
730 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800731 if (!ud)
732 return -1;
733
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100734 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200735
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800736 if (!handle->detail) {
737 handle->errnum = CS_ERR_DETAIL;
738 return -1;
739 }
740
Giovanni Condello95657e02014-05-07 17:31:27 +0200741 if(!insn->id) {
742 handle->errnum = CS_ERR_SKIPDATA;
743 return -1;
744 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200745
Giovanni Condello95657e02014-05-07 17:31:27 +0200746 if(!insn->detail) {
747 handle->errnum = CS_ERR_DETAIL;
748 return -1;
749 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200750
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800751 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800752
753 switch (handle->arch) {
754 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800755 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800756 return -1;
757 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800758 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800759 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800760 count++;
761 break;
762 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800763 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800764 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800765 count++;
766 break;
767 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800768 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800769 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800770 count++;
771 break;
772 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800773 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800774 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800775 count++;
776 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800777 case CS_ARCH_PPC:
778 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800779 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800780 count++;
781 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800782 case CS_ARCH_SPARC:
783 for (i = 0; i < insn->detail->sparc.op_count; i++)
784 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
785 count++;
786 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800787 case CS_ARCH_SYSZ:
788 for (i = 0; i < insn->detail->sysz.op_count; i++)
789 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
790 count++;
791 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800792 case CS_ARCH_XCORE:
793 for (i = 0; i < insn->detail->xcore.op_count; i++)
794 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
795 count++;
796 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800797 }
798
799 return count;
800}
801
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800802CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800803int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
804 unsigned int post)
805{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800806 struct cs_struct *handle;
807 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800808 if (!ud)
809 return -1;
810
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100811 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200812
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800813 if (!handle->detail) {
814 handle->errnum = CS_ERR_DETAIL;
815 return -1;
816 }
817
Giovanni Condello95657e02014-05-07 17:31:27 +0200818 if(!insn->id) {
819 handle->errnum = CS_ERR_SKIPDATA;
820 return -1;
821 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200822
Giovanni Condello95657e02014-05-07 17:31:27 +0200823 if(!insn->detail) {
824 handle->errnum = CS_ERR_DETAIL;
825 return -1;
826 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200827
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800828 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800829
830 switch (handle->arch) {
831 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800832 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800833 return -1;
834 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800835 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800836 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800837 count++;
838 if (count == post)
839 return i;
840 }
841 break;
842 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800843 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800844 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800845 count++;
846 if (count == post)
847 return i;
848 }
849 break;
850 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800851 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800852 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800853 count++;
854 if (count == post)
855 return i;
856 }
857 break;
858 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800859 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800860 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800861 count++;
862 if (count == post)
863 return i;
864 }
865 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800866 case CS_ARCH_PPC:
867 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800868 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800869 count++;
870 if (count == post)
871 return i;
872 }
873 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800874 case CS_ARCH_SPARC:
875 for (i = 0; i < insn->detail->sparc.op_count; i++) {
876 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
877 count++;
878 if (count == post)
879 return i;
880 }
881 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800882 case CS_ARCH_SYSZ:
883 for (i = 0; i < insn->detail->sysz.op_count; i++) {
884 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
885 count++;
886 if (count == post)
887 return i;
888 }
889 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800890 case CS_ARCH_XCORE:
891 for (i = 0; i < insn->detail->xcore.op_count; i++) {
892 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
893 count++;
894 if (count == post)
895 return i;
896 }
897 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800898 }
899
900 return -1;
901}