blob: f8a5229495bc303f29f21fbdff8b87845df51ebb [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
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
reverser160e1982015-04-09 18:28:19 +01006#if defined(CAPSTONE_HAS_OSXKERNEL)
7#include <libkern/libkern.h>
8#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08009#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <stdio.h>
11#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010012#endif
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010015#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080018#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010020#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080021#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080022#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080023// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080024#define INSN_CACHE_SIZE 8
25#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080026
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080027// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080028#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080029
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080030cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080031cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
32void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080033
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034extern void ARM_enable(void);
35extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020036extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037extern void Mips_enable(void);
38extern void X86_enable(void);
39extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080040extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080041extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080042extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070043
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080044static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080045{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 static bool initialized = false;
47
48 if (initialized)
49 return;
50
danghvub33bd2c2014-01-09 12:22:56 +070051#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070053#endif
54#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080055 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070056#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020057#ifdef CAPSTONE_HAS_M68K
58 M68K_enable();
59#endif
danghvub33bd2c2014-01-09 12:22:56 +070060#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080061 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070062#endif
danghvub33bd2c2014-01-09 12:22:56 +070063#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080064 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070065#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080066#ifdef CAPSTONE_HAS_SPARC
67 Sparc_enable();
68#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080069#ifdef CAPSTONE_HAS_SYSZ
70 SystemZ_enable();
71#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080072#ifdef CAPSTONE_HAS_X86
73 X86_enable();
74#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080075#ifdef CAPSTONE_HAS_XCORE
76 XCore_enable();
77#endif
78
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080079
80 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070081}
82
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080084
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010085#ifdef CAPSTONE_USE_SYS_DYN_MEM
Pb86ad3d62015-11-05 18:08:25 +010086#ifndef CAPSTONE_HAS_OSXKERNEL
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080087cs_malloc_t cs_mem_malloc = malloc;
88cs_calloc_t cs_mem_calloc = calloc;
89cs_realloc_t cs_mem_realloc = realloc;
90cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080091cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080092#else
Pb86ad3d62015-11-05 18:08:25 +010093extern void* kern_os_malloc(size_t size);
94extern void kern_os_free(void* addr);
95extern void* kern_os_realloc(void* addr, size_t nsize);
96
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +080097static void* cs_kern_os_calloc(size_t num, size_t size)
98{
Pb86ad3d62015-11-05 18:08:25 +010099 return kern_os_malloc(num * size); // malloc bzeroes the buffer
100}
101
102cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100103cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100104cs_realloc_t cs_mem_realloc = kern_os_realloc;
105cs_free_t cs_mem_free = kern_os_free;
106cs_vsnprintf_t cs_vsnprintf = vsnprintf;
107#endif
108#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800109cs_malloc_t cs_mem_malloc = NULL;
110cs_calloc_t cs_mem_calloc = NULL;
111cs_realloc_t cs_mem_realloc = NULL;
112cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800113cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800114#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800115
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800116CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800117unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800118{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800119 archs_enable();
120
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800121 if (major != NULL && minor != NULL) {
122 *major = CS_API_MAJOR;
123 *minor = CS_API_MINOR;
124 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800125
126 return (CS_API_MAJOR << 8) + CS_API_MINOR;
127}
128
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800129CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800130bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800131{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800132 archs_enable();
133
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800134 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800135 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800136 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800137 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800138 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800139
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800140 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800141 return all_arch & (1 << query);
142
143 if (query == CS_SUPPORT_DIET) {
144#ifdef CAPSTONE_DIET
145 return true;
146#else
147 return false;
148#endif
149 }
150
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800151 if (query == CS_SUPPORT_X86_REDUCE) {
152#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800153 return true;
154#else
155 return false;
156#endif
157 }
158
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800159 // unsupported query
160 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800161}
162
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800163CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800164cs_err cs_errno(csh handle)
165{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800166 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800167 if (!handle)
168 return CS_ERR_CSH;
169
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100170 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800172 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173}
174
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800175CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800176const char *cs_strerror(cs_err code)
177{
178 switch(code) {
179 default:
180 return "Unknown error code";
181 case CS_ERR_OK:
182 return "OK (CS_ERR_OK)";
183 case CS_ERR_MEM:
184 return "Out of memory (CS_ERR_MEM)";
185 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800186 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800187 case CS_ERR_HANDLE:
188 return "Invalid handle (CS_ERR_HANDLE)";
189 case CS_ERR_CSH:
190 return "Invalid csh (CS_ERR_CSH)";
191 case CS_ERR_MODE:
192 return "Invalid mode (CS_ERR_MODE)";
193 case CS_ERR_OPTION:
194 return "Invalid option (CS_ERR_OPTION)";
195 case CS_ERR_DETAIL:
196 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800197 case CS_ERR_MEMSETUP:
198 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800199 case CS_ERR_VERSION:
200 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800201 case CS_ERR_DIET:
202 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800203 case CS_ERR_SKIPDATA:
204 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700205 case CS_ERR_X86_ATT:
206 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
207 case CS_ERR_X86_INTEL:
208 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
209 case CS_ERR_X86_MASM:
210 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800211 }
212}
213
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800214CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
216{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800217 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800218 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800219 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800220 // Error: before cs_open(), dynamic memory management must be initialized
221 // with cs_option(CS_OPT_MEM)
222 return CS_ERR_MEMSETUP;
223
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800224 archs_enable();
225
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800226 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800227 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800228 if (!ud) {
229 // memory insufficient
230 return CS_ERR_MEM;
231 }
232
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800233 ud->errnum = CS_ERR_OK;
234 ud->arch = arch;
235 ud->mode = mode;
236 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800237 // by default, do not break instruction into details
238 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800240 // default skipdata setup
241 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
242
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100243 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800244 if (err) {
245 cs_mem_free(ud);
246 *handle = 0;
247 return err;
248 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800249
250 *handle = (uintptr_t)ud;
251
252 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800253 } else {
254 *handle = 0;
255 return CS_ERR_ARCH;
256 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800257}
258
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800259CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800260cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800262 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800263 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800264
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800265 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800266 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267 return CS_ERR_CSH;
268
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100269 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800270
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800271 if (ud->printer_info)
272 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800274 // free the linked list of customized mnemonic
275 tmp = ud->mnem_list;
276 while(tmp) {
277 next = tmp->next;
278 cs_mem_free(tmp);
279 tmp = next;
280 }
281
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800282 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800283
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800284 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800285 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800286
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800287 // invalidate this handle by ZERO out its value.
288 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800289 *handle = 0;
290
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800291 return CS_ERR_OK;
292}
293
294// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800295static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800296 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800298#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700299 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800300#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800301 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800303 // fill the instruction bytes.
304 // we might skip some redundant bytes in front in the case of X86
305 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
306 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800307
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800308 // alias instruction might have ID saved in OpcodePub
309 if (MCInst_getOpcodePub(mci))
310 insn->id = MCInst_getOpcodePub(mci);
311
312 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800313 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800314 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800315
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800316#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800318 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100319 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800320 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800321 for (sp = buffer; *sp; sp++) {
322 if (*sp == ' '|| *sp == '\t')
323 break;
324 if (*sp == '|') // lock|rep prefix for x86
325 *sp = ' ';
326 // copy to @mnemonic
327 *mnem = *sp;
328 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800329 }
330
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800331 *mnem = '\0';
332
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800333 // we might have customized mnemonic
334 if (handle->mnem_list) {
335 struct insn_mnem *tmp = handle->mnem_list;
336 while(tmp) {
337 if (tmp->insn.id == insn->id) {
338 // found this instruction, so copy its mnemonic
339 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
340 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
341 break;
342 }
343 tmp = tmp->next;
344 }
345 }
346
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800347 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800348 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800349 // find the next non-space char
350 sp++;
351 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
352 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
354 } else
355 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800356#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357}
358
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800359// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800360// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800361static uint8_t skipdata_size(cs_struct *handle)
362{
363 switch(handle->arch) {
364 default:
365 // should never reach
366 return -1;
367 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800368 // skip 2 bytes on Thumb mode.
369 if (handle->mode & CS_MODE_THUMB)
370 return 2;
371 // otherwise, skip 4 bytes
372 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800373 case CS_ARCH_ARM64:
374 case CS_ARCH_MIPS:
375 case CS_ARCH_PPC:
376 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800377 // skip 4 bytes
378 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800379 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800380 // SystemZ instruction's length can be 2, 4 or 6 bytes,
381 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800382 return 2;
383 case CS_ARCH_X86:
384 // X86 has no restriction on instruction alignment
385 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800386 case CS_ARCH_XCORE:
387 // XCore instruction's length can be 2 or 4 bytes,
388 // so we just skip 2 bytes
389 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200390 case CS_ARCH_M68K:
391 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
392 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800393 }
394}
395
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800396CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800397cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800398{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800399 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800400 cs_opt_mnem *opt;
401
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800402 archs_enable();
403
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800404 // cs_option() can be called with NULL handle just for CS_OPT_MEM
405 // This is supposed to be executed before all other APIs (even cs_open())
406 if (type == CS_OPT_MEM) {
407 cs_opt_mem *mem = (cs_opt_mem *)value;
408
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800409 cs_mem_malloc = mem->malloc;
410 cs_mem_calloc = mem->calloc;
411 cs_mem_realloc = mem->realloc;
412 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800413 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800414
415 return CS_ERR_OK;
416 }
417
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100418 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600419 if (!handle)
420 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800421
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800422 switch(type) {
423 default:
424 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800425
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800426 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500427 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800428 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800429
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800430 case CS_OPT_SKIPDATA:
431 handle->skipdata = (value == CS_OPT_ON);
432 if (handle->skipdata) {
433 if (handle->skipdata_size == 0) {
434 // set the default skipdata size
435 handle->skipdata_size = skipdata_size(handle);
436 }
437 }
438 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800439
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800440 case CS_OPT_SKIPDATA_SETUP:
441 if (value)
442 handle->skipdata_setup = *((cs_opt_skipdata *)value);
443 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800444
445 case CS_OPT_MNEMONIC:
446 opt = (cs_opt_mnem *)value;
447 if (opt->id) {
448 if (opt->mnemonic) {
449 struct insn_mnem *tmp;
450
451 // add new instruction, or replace existing instruction
452 // 1. find if we already had this insn in the linked list
453 tmp = handle->mnem_list;
454 while(tmp) {
455 if (tmp->insn.id == opt->id) {
456 // found this instruction, so replace its mnemonic
457 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
458 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
459 break;
460 }
461 tmp = tmp->next;
462 }
463
464 // 2. add this instruction if we have not had it yet
465 if (!tmp) {
466 tmp = cs_mem_malloc(sizeof(*tmp));
467 tmp->insn.id = opt->id;
468 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
469 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
470 // this new instruction is heading the list
471 tmp->next = handle->mnem_list;
472 handle->mnem_list = tmp;
473 }
474 return CS_ERR_OK;
475 } else {
476 struct insn_mnem *prev, *tmp;
477
478 // we want to delete an existing instruction
479 // iterate the list to find the instruction to remove it
480 tmp = handle->mnem_list;
481 prev = tmp;
482 while(tmp) {
483 if (tmp->insn.id == opt->id) {
484 // delete this instruction
485 if (tmp == prev) {
486 // head of the list
487 handle->mnem_list = tmp->next;
488 } else {
489 prev->next = tmp->next;
490 }
491 cs_mem_free(tmp);
492 break;
493 }
494 prev = tmp;
495 tmp = tmp->next;
496 }
497 }
498 }
499 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800500 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800501
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800502 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800503}
504
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800505// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800506static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
507{
508 char *p = opstr;
509 int len;
510 size_t i;
511
512 if (!size) {
513 opstr[0] = '\0';
514 return;
515 }
516
517 len = sprintf(p, "0x%02x", buffer[0]);
518 p+= len;
519
520 for(i = 1; i < size; i++) {
521 len = sprintf(p, ", 0x%02x", buffer[i]);
522 p+= len;
523 }
524}
525
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800526// dynamicly allocate memory to contain disasm insn
527// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800528CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800529size_t cs_disasm(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 +0800530{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800531 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800532 MCInst mci;
533 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800534 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800535 unsigned int f = 0; // index of the next instruction in the cache
536 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800537 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800538 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800539 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800540 void *tmp;
541 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800542 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800543 size_t size_org;
544 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800545 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500546 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800547
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800548 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800549 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800550 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800551 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800552 return 0;
553 }
554
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800555 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800556
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800557#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800558 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400559 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800560#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800561
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800562 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800563 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800564 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800565 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800566
567 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700568 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800569 if (total == NULL) {
570 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500571 handle->errnum = CS_ERR_MEM;
572 return 0;
573 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800574
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700575 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800576
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800577 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800578 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800579 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800580
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700581 // relative branches need to know the address & size of current insn
582 mci.address = offset;
583
584 if (handle->detail) {
585 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700586 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700587 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700588 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700589 }
590
591 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700592 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700593 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800594#ifdef CAPSTONE_DIET
595 // zero out mnemonic & op_str
596 mci.flat_insn->mnemonic[0] = '\0';
597 mci.flat_insn->op_str[0] = '\0';
598#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700599
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800600 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800601 if (r) {
602 SStream ss;
603 SStream_Init(&ss);
604
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700605 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800606
607 // map internal instruction opcode to public insn ID
608 handle->insn_id(handle, insn_cache, mci.Opcode);
609
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800610 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800611
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700612 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800613
danghvu0d1aad12014-10-01 23:12:18 -0500614 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800615 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800616 // encounter a broken instruction
617
618 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800619 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800620 cs_mem_free(insn_cache->detail);
621 }
622
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800623 // if there is no request to skip data, or remaining data is too small,
624 // then bail out
625 if (!handle->skipdata || handle->skipdata_size > size)
626 break;
627
628 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800629 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800630 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800631 if (skipdata_bytes > size)
632 // remaining data is not enough
633 break;
634
635 if (!skipdata_bytes)
636 // user requested not to skip data, so bail out
637 break;
638 } else
639 skipdata_bytes = handle->skipdata_size;
640
641 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700642 insn_cache->id = 0; // invalid ID for this "data" instruction
643 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800644 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700645 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
646 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
647 sizeof(insn_cache->mnemonic) - 1);
648 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
649 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800650
danghvu0d1aad12014-10-01 23:12:18 -0500651 next_offset = skipdata_bytes;
652 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800653
danghvu0d1aad12014-10-01 23:12:18 -0500654 // one more instruction entering the cache
655 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800656
danghvu0d1aad12014-10-01 23:12:18 -0500657 // one more instruction disassembled
658 c++;
659 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800660 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500661 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800662
danghvu0d1aad12014-10-01 23:12:18 -0500663 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800664 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500665 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500666 total_size += (sizeof(cs_insn) * cache_size);
667 tmp = cs_mem_realloc(total, total_size);
668 if (tmp == NULL) { // insufficient memory
669 if (handle->detail) {
670 insn_cache = (cs_insn *)total;
671 for (i = 0; i < c; i++, insn_cache++)
672 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800673 }
674
danghvu0d1aad12014-10-01 23:12:18 -0500675 cs_mem_free(total);
676 *insn = NULL;
677 handle->errnum = CS_ERR_MEM;
678 return 0;
679 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800680
danghvu0d1aad12014-10-01 23:12:18 -0500681 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800682 // continue to fill in the cache after the last instruction
683 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800684
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800685 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500686 f = 0;
687 } else
688 insn_cache++;
689
690 buffer += next_offset;
691 size -= next_offset;
692 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800693 }
694
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800695 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800696 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800697 cs_mem_free(total);
698 total = NULL;
699 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800700 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400701 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800702 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800703 // free all detail pointers
704 if (handle->detail) {
705 insn_cache = (cs_insn *)total;
706 for (i = 0; i < c; i++, insn_cache++)
707 cs_mem_free(insn_cache->detail);
708 }
709
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800710 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800711 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800712
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800713 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800714 return 0;
715 }
716
717 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800718 }
719
720 *insn = total;
721
722 return c;
723}
724
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800725CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800726CAPSTONE_DEPRECATED
727size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
728{
729 return cs_disasm(ud, buffer, size, offset, count, insn);
730}
731
732CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800733void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800734{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800735 size_t i;
736
737 // free all detail pointers
738 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800739 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800740
741 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800742 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800743}
744
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800745CAPSTONE_EXPORT
746cs_insn *cs_malloc(csh ud)
747{
748 cs_insn *insn;
749 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
750
751 insn = cs_mem_malloc(sizeof(cs_insn));
752 if (!insn) {
753 // insufficient memory
754 handle->errnum = CS_ERR_MEM;
755 return NULL;
756 } else {
757 if (handle->detail) {
758 // allocate memory for @detail pointer
759 insn->detail = cs_mem_malloc(sizeof(cs_detail));
760 if (insn->detail == NULL) { // insufficient memory
761 cs_mem_free(insn);
762 handle->errnum = CS_ERR_MEM;
763 return NULL;
764 }
765 } else
766 insn->detail = NULL;
767 }
768
769 return insn;
770}
771
hlide993f3622014-10-05 18:14:40 +0200772// iterator for instruction "single-stepping"
773CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800774bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
775 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200776{
777 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200778 uint16_t insn_size;
779 MCInst mci;
780 bool r;
781
782 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800783 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800784 return false;
hlide993f3622014-10-05 18:14:40 +0200785 }
786
787 handle->errnum = CS_ERR_OK;
788
hlide993f3622014-10-05 18:14:40 +0200789 MCInst_Init(&mci);
790 mci.csh = handle;
791
792 // relative branches need to know the address & size of current insn
793 mci.address = *address;
794
795 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800796 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200797 mci.flat_insn->address = *address;
798#ifdef CAPSTONE_DIET
799 // zero out mnemonic & op_str
800 mci.flat_insn->mnemonic[0] = '\0';
801 mci.flat_insn->op_str[0] = '\0';
802#endif
803
804 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800805 if (r) {
hlide993f3622014-10-05 18:14:40 +0200806 SStream ss;
807 SStream_Init(&ss);
808
809 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800810
811 // map internal instruction opcode to public insn ID
812 handle->insn_id(handle, insn, mci.Opcode);
813
hlide993f3622014-10-05 18:14:40 +0200814 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800815
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800816 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800817
hlide993f3622014-10-05 18:14:40 +0200818 *code += insn_size;
819 *size -= insn_size;
820 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800821 } else { // encounter a broken instruction
822 size_t skipdata_bytes;
823
824 // if there is no request to skip data, or remaining data is too small,
825 // then bail out
826 if (!handle->skipdata || handle->skipdata_size > *size)
827 return false;
828
829 if (handle->skipdata_setup.callback) {
830 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
831 0, handle->skipdata_setup.user_data);
832 if (skipdata_bytes > *size)
833 // remaining data is not enough
834 return false;
835
836 if (!skipdata_bytes)
837 // user requested not to skip data, so bail out
838 return false;
839 } else
840 skipdata_bytes = handle->skipdata_size;
841
842 // we have to skip some amount of data, depending on arch & mode
843 insn->id = 0; // invalid ID for this "data" instruction
844 insn->address = *address;
845 insn->size = (uint16_t)skipdata_bytes;
846 memcpy(insn->bytes, *code, skipdata_bytes);
847 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
848 sizeof(insn->mnemonic) - 1);
849 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
850
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800851 *code += skipdata_bytes;
852 *size -= skipdata_bytes;
853 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200854 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800855
856 return true;
hlide993f3622014-10-05 18:14:40 +0200857}
858
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800859// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800860CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100861const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800862{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800863 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800864
865 if (!handle || handle->reg_name == NULL) {
866 return NULL;
867 }
868
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800869 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800870}
871
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800872CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100873const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800874{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800875 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800876
877 if (!handle || handle->insn_name == NULL) {
878 return NULL;
879 }
880
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800881 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800882}
883
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800884CAPSTONE_EXPORT
885const char *cs_group_name(csh ud, unsigned int group)
886{
887 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
888
889 if (!handle || handle->group_name == NULL) {
890 return NULL;
891 }
892
893 return handle->group_name(ud, group);
894}
895
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800896CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200897bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800898{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800899 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800900 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800901 return false;
902
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100903 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200904
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800905 if (!handle->detail) {
906 handle->errnum = CS_ERR_DETAIL;
907 return false;
908 }
909
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800910 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200911 handle->errnum = CS_ERR_SKIPDATA;
912 return false;
913 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200914
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800915 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200916 handle->errnum = CS_ERR_DETAIL;
917 return false;
918 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200919
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800920 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800921}
922
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800923CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200924bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800925{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800926 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800927 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800928 return false;
929
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100930 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200931
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800932 if (!handle->detail) {
933 handle->errnum = CS_ERR_DETAIL;
934 return false;
935 }
936
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800937 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200938 handle->errnum = CS_ERR_SKIPDATA;
939 return false;
940 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200941
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800942 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200943 handle->errnum = CS_ERR_DETAIL;
944 return false;
945 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200946
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800947 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800948}
949
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800950CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200951bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800952{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800953 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800954 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800955 return false;
956
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100957 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200958
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800959 if (!handle->detail) {
960 handle->errnum = CS_ERR_DETAIL;
961 return false;
962 }
963
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800964 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200965 handle->errnum = CS_ERR_SKIPDATA;
966 return false;
967 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200968
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800969 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200970 handle->errnum = CS_ERR_DETAIL;
971 return false;
972 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200973
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800974 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975}
976
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800977CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200978int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800979{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800980 struct cs_struct *handle;
981 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800982 if (!ud)
983 return -1;
984
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100985 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200986
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800987 if (!handle->detail) {
988 handle->errnum = CS_ERR_DETAIL;
989 return -1;
990 }
991
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800992 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200993 handle->errnum = CS_ERR_SKIPDATA;
994 return -1;
995 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200996
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800997 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200998 handle->errnum = CS_ERR_DETAIL;
999 return -1;
1000 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001001
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001002 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001003
1004 switch (handle->arch) {
1005 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001006 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001007 return -1;
1008 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001009 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001010 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001011 count++;
1012 break;
1013 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001014 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001015 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001016 count++;
1017 break;
1018 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001019 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001020 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001021 count++;
1022 break;
1023 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001024 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001025 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001026 count++;
1027 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001028 case CS_ARCH_PPC:
1029 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001030 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001031 count++;
1032 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001033 case CS_ARCH_SPARC:
1034 for (i = 0; i < insn->detail->sparc.op_count; i++)
1035 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1036 count++;
1037 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001038 case CS_ARCH_SYSZ:
1039 for (i = 0; i < insn->detail->sysz.op_count; i++)
1040 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1041 count++;
1042 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001043 case CS_ARCH_XCORE:
1044 for (i = 0; i < insn->detail->xcore.op_count; i++)
1045 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1046 count++;
1047 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001048 }
1049
1050 return count;
1051}
1052
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001053CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +02001054int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001055 unsigned int post)
1056{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001057 struct cs_struct *handle;
1058 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001059 if (!ud)
1060 return -1;
1061
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001062 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001063
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001064 if (!handle->detail) {
1065 handle->errnum = CS_ERR_DETAIL;
1066 return -1;
1067 }
1068
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001069 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001070 handle->errnum = CS_ERR_SKIPDATA;
1071 return -1;
1072 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001073
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001074 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001075 handle->errnum = CS_ERR_DETAIL;
1076 return -1;
1077 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001078
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001079 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001080
1081 switch (handle->arch) {
1082 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001083 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001084 return -1;
1085 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001086 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001087 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001088 count++;
1089 if (count == post)
1090 return i;
1091 }
1092 break;
1093 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001094 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001095 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001096 count++;
1097 if (count == post)
1098 return i;
1099 }
1100 break;
1101 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001102 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001103 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001104 count++;
1105 if (count == post)
1106 return i;
1107 }
1108 break;
1109 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001110 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001111 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001112 count++;
1113 if (count == post)
1114 return i;
1115 }
1116 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001117 case CS_ARCH_PPC:
1118 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001119 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001120 count++;
1121 if (count == post)
1122 return i;
1123 }
1124 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001125 case CS_ARCH_SPARC:
1126 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1127 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1128 count++;
1129 if (count == post)
1130 return i;
1131 }
1132 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001133 case CS_ARCH_SYSZ:
1134 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1135 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1136 count++;
1137 if (count == post)
1138 return i;
1139 }
1140 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001141 case CS_ARCH_XCORE:
1142 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1143 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1144 count++;
1145 if (count == post)
1146 return i;
1147 }
1148 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001149 }
1150
1151 return -1;
1152}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001153
1154CAPSTONE_EXPORT
1155cs_err cs_regs_access(csh ud, const cs_insn *insn,
1156 cs_regs regs_read, uint8_t *regs_read_count,
1157 cs_regs regs_write, uint8_t *regs_write_count)
1158{
1159 struct cs_struct *handle;
1160
1161 if (!ud)
1162 return -1;
1163
1164 handle = (struct cs_struct *)(uintptr_t)ud;
1165
1166#ifdef CAPSTONE_DIET
1167 // This API does not work in DIET mode
1168 handle->errnum = CS_ERR_DIET;
1169 return CS_ERR_DIET;
1170#else
1171 if (!handle->detail) {
1172 handle->errnum = CS_ERR_DETAIL;
1173 return CS_ERR_DETAIL;
1174 }
1175
1176 if (!insn->id) {
1177 handle->errnum = CS_ERR_SKIPDATA;
1178 return CS_ERR_SKIPDATA;
1179 }
1180
1181 if (!insn->detail) {
1182 handle->errnum = CS_ERR_DETAIL;
1183 return CS_ERR_DETAIL;
1184 }
1185
1186 if (handle->reg_access) {
1187 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1188 } else {
1189 // this arch is unsupported yet
1190 handle->errnum = CS_ERR_ARCH;
1191 return CS_ERR_ARCH;
1192 }
1193
1194 return CS_ERR_OK;
1195#endif
1196}