blob: e6689324062ca9155bcdf439cb80ad43c1e4cc9f [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
tandasat45e5eab2016-05-11 21:48:32 -070020#if defined(_KERNEL_MODE)
21#include "windows\winkernel_mm.h"
22#endif
23
24#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080025#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080026#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080027// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080028#define INSN_CACHE_SIZE 8
29#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080030
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080031// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080032#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080033
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080034cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080035cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
36void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080037
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080038extern void ARM_enable(void);
39extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020040extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080041extern void Mips_enable(void);
42extern void X86_enable(void);
43extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080044extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080045extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080046extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070047
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080048static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080049{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080050 static bool initialized = false;
51
52 if (initialized)
53 return;
54
danghvub33bd2c2014-01-09 12:22:56 +070055#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080056 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070057#endif
58#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080059 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070060#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020061#ifdef CAPSTONE_HAS_M68K
62 M68K_enable();
63#endif
danghvub33bd2c2014-01-09 12:22:56 +070064#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080065 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070066#endif
danghvub33bd2c2014-01-09 12:22:56 +070067#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080068 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070069#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080070#ifdef CAPSTONE_HAS_SPARC
71 Sparc_enable();
72#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080073#ifdef CAPSTONE_HAS_SYSZ
74 SystemZ_enable();
75#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080076#ifdef CAPSTONE_HAS_X86
77 X86_enable();
78#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080079#ifdef CAPSTONE_HAS_XCORE
80 XCore_enable();
81#endif
82
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080083
84 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070085}
86
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080087unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080088
tandasat45e5eab2016-05-11 21:48:32 -070089#if defined(CAPSTONE_USE_SYS_DYN_MEM)
90#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080091cs_malloc_t cs_mem_malloc = malloc;
92cs_calloc_t cs_mem_calloc = calloc;
93cs_realloc_t cs_mem_realloc = realloc;
94cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080095cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasat45e5eab2016-05-11 21:48:32 -070096#elif defined(_KERNEL_MODE)
97cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
98cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
99cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
100cs_free_t cs_mem_free = cs_winkernel_free;
101cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800102#else
Pb86ad3d62015-11-05 18:08:25 +0100103extern void* kern_os_malloc(size_t size);
104extern void kern_os_free(void* addr);
105extern void* kern_os_realloc(void* addr, size_t nsize);
106
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800107static void* cs_kern_os_calloc(size_t num, size_t size)
108{
Pb86ad3d62015-11-05 18:08:25 +0100109 return kern_os_malloc(num * size); // malloc bzeroes the buffer
110}
111
112cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100113cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100114cs_realloc_t cs_mem_realloc = kern_os_realloc;
115cs_free_t cs_mem_free = kern_os_free;
116cs_vsnprintf_t cs_vsnprintf = vsnprintf;
117#endif
118#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800119cs_malloc_t cs_mem_malloc = NULL;
120cs_calloc_t cs_mem_calloc = NULL;
121cs_realloc_t cs_mem_realloc = NULL;
122cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800123cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800124#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800125
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800126CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700127unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800128{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800129 archs_enable();
130
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800131 if (major != NULL && minor != NULL) {
132 *major = CS_API_MAJOR;
133 *minor = CS_API_MINOR;
134 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800135
136 return (CS_API_MAJOR << 8) + CS_API_MINOR;
137}
138
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800139CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700140bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800141{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800142 archs_enable();
143
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800144 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800145 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800146 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800147 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800148 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800149
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800150 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800151 return all_arch & (1 << query);
152
153 if (query == CS_SUPPORT_DIET) {
154#ifdef CAPSTONE_DIET
155 return true;
156#else
157 return false;
158#endif
159 }
160
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800161 if (query == CS_SUPPORT_X86_REDUCE) {
162#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800163 return true;
164#else
165 return false;
166#endif
167 }
168
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800169 // unsupported query
170 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800171}
172
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800173CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700174cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800175{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800176 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800177 if (!handle)
178 return CS_ERR_CSH;
179
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100180 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800182 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800183}
184
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800185CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700186const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800187{
188 switch(code) {
189 default:
190 return "Unknown error code";
191 case CS_ERR_OK:
192 return "OK (CS_ERR_OK)";
193 case CS_ERR_MEM:
194 return "Out of memory (CS_ERR_MEM)";
195 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800196 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800197 case CS_ERR_HANDLE:
198 return "Invalid handle (CS_ERR_HANDLE)";
199 case CS_ERR_CSH:
200 return "Invalid csh (CS_ERR_CSH)";
201 case CS_ERR_MODE:
202 return "Invalid mode (CS_ERR_MODE)";
203 case CS_ERR_OPTION:
204 return "Invalid option (CS_ERR_OPTION)";
205 case CS_ERR_DETAIL:
206 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800207 case CS_ERR_MEMSETUP:
208 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800209 case CS_ERR_VERSION:
210 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800211 case CS_ERR_DIET:
212 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800213 case CS_ERR_SKIPDATA:
214 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700215 case CS_ERR_X86_ATT:
216 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
217 case CS_ERR_X86_INTEL:
218 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
219 case CS_ERR_X86_MASM:
220 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800221 }
222}
223
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800224CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700225cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800226{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800227 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800228 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800229 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800230 // Error: before cs_open(), dynamic memory management must be initialized
231 // with cs_option(CS_OPT_MEM)
232 return CS_ERR_MEMSETUP;
233
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800234 archs_enable();
235
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800236 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800237 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800238 if (!ud) {
239 // memory insufficient
240 return CS_ERR_MEM;
241 }
242
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800243 ud->errnum = CS_ERR_OK;
244 ud->arch = arch;
245 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700246 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800247 // by default, do not break instruction into details
248 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800249
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800250 // default skipdata setup
251 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
252
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100253 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800254 if (err) {
255 cs_mem_free(ud);
256 *handle = 0;
257 return err;
258 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800259
260 *handle = (uintptr_t)ud;
261
262 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800263 } else {
264 *handle = 0;
265 return CS_ERR_ARCH;
266 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267}
268
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800269CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700270cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800271{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800272 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800273 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800274
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800275 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800276 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277 return CS_ERR_CSH;
278
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100279 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800280
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800281 if (ud->printer_info)
282 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800283
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800284 // free the linked list of customized mnemonic
285 tmp = ud->mnem_list;
286 while(tmp) {
287 next = tmp->next;
288 cs_mem_free(tmp);
289 tmp = next;
290 }
291
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800292 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800293
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800294 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800295 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800296
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800297 // invalidate this handle by ZERO out its value.
298 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800299 *handle = 0;
300
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301 return CS_ERR_OK;
302}
303
304// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800305static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800306 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800308#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700309 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800310#endif
tandasat45e5eab2016-05-11 21:48:32 -0700311 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800312
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800313 // fill the instruction bytes.
314 // we might skip some redundant bytes in front in the case of X86
315 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
316 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800317
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800318 // alias instruction might have ID saved in OpcodePub
319 if (MCInst_getOpcodePub(mci))
320 insn->id = MCInst_getOpcodePub(mci);
321
322 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800323 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800324 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800325
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800326#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800327 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800328 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100329 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800330 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800331 for (sp = buffer; *sp; sp++) {
332 if (*sp == ' '|| *sp == '\t')
333 break;
334 if (*sp == '|') // lock|rep prefix for x86
335 *sp = ' ';
336 // copy to @mnemonic
337 *mnem = *sp;
338 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800339 }
340
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800341 *mnem = '\0';
342
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800343 // we might have customized mnemonic
344 if (handle->mnem_list) {
345 struct insn_mnem *tmp = handle->mnem_list;
346 while(tmp) {
347 if (tmp->insn.id == insn->id) {
348 // found this instruction, so copy its mnemonic
349 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
350 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
351 break;
352 }
353 tmp = tmp->next;
354 }
355 }
356
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800357 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800358 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800359 // find the next non-space char
360 sp++;
361 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
362 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800363 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
364 } else
365 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800366#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800367}
368
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800369// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800370// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800371static uint8_t skipdata_size(cs_struct *handle)
372{
373 switch(handle->arch) {
374 default:
375 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700376 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800377 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800378 // skip 2 bytes on Thumb mode.
379 if (handle->mode & CS_MODE_THUMB)
380 return 2;
381 // otherwise, skip 4 bytes
382 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800383 case CS_ARCH_ARM64:
384 case CS_ARCH_MIPS:
385 case CS_ARCH_PPC:
386 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800387 // skip 4 bytes
388 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800389 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800390 // SystemZ instruction's length can be 2, 4 or 6 bytes,
391 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800392 return 2;
393 case CS_ARCH_X86:
394 // X86 has no restriction on instruction alignment
395 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800396 case CS_ARCH_XCORE:
397 // XCore instruction's length can be 2 or 4 bytes,
398 // so we just skip 2 bytes
399 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200400 case CS_ARCH_M68K:
401 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
402 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800403 }
404}
405
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800406CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700407cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800408{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800409 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800410 cs_opt_mnem *opt;
411
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800412 archs_enable();
413
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800414 // cs_option() can be called with NULL handle just for CS_OPT_MEM
415 // This is supposed to be executed before all other APIs (even cs_open())
416 if (type == CS_OPT_MEM) {
417 cs_opt_mem *mem = (cs_opt_mem *)value;
418
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800419 cs_mem_malloc = mem->malloc;
420 cs_mem_calloc = mem->calloc;
421 cs_mem_realloc = mem->realloc;
422 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800423 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800424
425 return CS_ERR_OK;
426 }
427
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100428 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600429 if (!handle)
430 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800431
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800432 switch(type) {
433 default:
434 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800435
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800436 case CS_OPT_UNSIGNED:
437 handle->imm_unsigned = (cs_opt_value)value;
438 return CS_ERR_OK;
439
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800440 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500441 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800442 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800443
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800444 case CS_OPT_SKIPDATA:
445 handle->skipdata = (value == CS_OPT_ON);
446 if (handle->skipdata) {
447 if (handle->skipdata_size == 0) {
448 // set the default skipdata size
449 handle->skipdata_size = skipdata_size(handle);
450 }
451 }
452 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800453
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800454 case CS_OPT_SKIPDATA_SETUP:
455 if (value)
456 handle->skipdata_setup = *((cs_opt_skipdata *)value);
457 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800458
459 case CS_OPT_MNEMONIC:
460 opt = (cs_opt_mnem *)value;
461 if (opt->id) {
462 if (opt->mnemonic) {
463 struct insn_mnem *tmp;
464
465 // add new instruction, or replace existing instruction
466 // 1. find if we already had this insn in the linked list
467 tmp = handle->mnem_list;
468 while(tmp) {
469 if (tmp->insn.id == opt->id) {
470 // found this instruction, so replace its mnemonic
471 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
472 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
473 break;
474 }
475 tmp = tmp->next;
476 }
477
478 // 2. add this instruction if we have not had it yet
479 if (!tmp) {
480 tmp = cs_mem_malloc(sizeof(*tmp));
481 tmp->insn.id = opt->id;
482 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
483 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
484 // this new instruction is heading the list
485 tmp->next = handle->mnem_list;
486 handle->mnem_list = tmp;
487 }
488 return CS_ERR_OK;
489 } else {
490 struct insn_mnem *prev, *tmp;
491
492 // we want to delete an existing instruction
493 // iterate the list to find the instruction to remove it
494 tmp = handle->mnem_list;
495 prev = tmp;
496 while(tmp) {
497 if (tmp->insn.id == opt->id) {
498 // delete this instruction
499 if (tmp == prev) {
500 // head of the list
501 handle->mnem_list = tmp->next;
502 } else {
503 prev->next = tmp->next;
504 }
505 cs_mem_free(tmp);
506 break;
507 }
508 prev = tmp;
509 tmp = tmp->next;
510 }
511 }
512 }
513 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800514 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800515
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800516 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800517}
518
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800519// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800520static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
521{
522 char *p = opstr;
523 int len;
524 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700525 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800526
527 if (!size) {
528 opstr[0] = '\0';
529 return;
530 }
531
tandasat45e5eab2016-05-11 21:48:32 -0700532 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800533 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700534 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800535
536 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700537 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
538 if (len < 0) {
539 break;
540 }
541 if ((size_t)len > available - 1) {
542 break;
543 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800544 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700545 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800546 }
547}
548
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800549// dynamicly allocate memory to contain disasm insn
550// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800551CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700552size_t CAPSTONE_API 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 +0800553{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800554 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800555 MCInst mci;
556 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800557 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800558 unsigned int f = 0; // index of the next instruction in the cache
559 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800560 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800561 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800562 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800563 void *tmp;
564 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800565 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800566 size_t size_org;
567 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800568 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500569 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800570
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800571 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800572 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800573 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800574 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800575 return 0;
576 }
577
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800578 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800580#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800581 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400582 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800583#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800584
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800585 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800586 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800587 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800588 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800589
590 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700591 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800592 if (total == NULL) {
593 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500594 handle->errnum = CS_ERR_MEM;
595 return 0;
596 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800597
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700598 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800599
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800600 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800601 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800602 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800603
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700604 // relative branches need to know the address & size of current insn
605 mci.address = offset;
606
607 if (handle->detail) {
608 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700609 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700610 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700611 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700612 }
613
614 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700615 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700616 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800617#ifdef CAPSTONE_DIET
618 // zero out mnemonic & op_str
619 mci.flat_insn->mnemonic[0] = '\0';
620 mci.flat_insn->op_str[0] = '\0';
621#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700622
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800623 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800624 if (r) {
625 SStream ss;
626 SStream_Init(&ss);
627
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700628 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800629
630 // map internal instruction opcode to public insn ID
631 handle->insn_id(handle, insn_cache, mci.Opcode);
632
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800633 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800634
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700635 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800636
danghvu0d1aad12014-10-01 23:12:18 -0500637 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800638 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800639 // encounter a broken instruction
640
641 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800642 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800643 cs_mem_free(insn_cache->detail);
644 }
645
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800646 // if there is no request to skip data, or remaining data is too small,
647 // then bail out
648 if (!handle->skipdata || handle->skipdata_size > size)
649 break;
650
651 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800652 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800653 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800654 if (skipdata_bytes > size)
655 // remaining data is not enough
656 break;
657
658 if (!skipdata_bytes)
659 // user requested not to skip data, so bail out
660 break;
661 } else
662 skipdata_bytes = handle->skipdata_size;
663
664 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700665 insn_cache->id = 0; // invalid ID for this "data" instruction
666 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800667 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700668 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
669 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
670 sizeof(insn_cache->mnemonic) - 1);
671 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
672 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800673
danghvu0d1aad12014-10-01 23:12:18 -0500674 next_offset = skipdata_bytes;
675 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800676
danghvu0d1aad12014-10-01 23:12:18 -0500677 // one more instruction entering the cache
678 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800679
danghvu0d1aad12014-10-01 23:12:18 -0500680 // one more instruction disassembled
681 c++;
682 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800683 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500684 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800685
danghvu0d1aad12014-10-01 23:12:18 -0500686 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800687 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500688 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500689 total_size += (sizeof(cs_insn) * cache_size);
690 tmp = cs_mem_realloc(total, total_size);
691 if (tmp == NULL) { // insufficient memory
692 if (handle->detail) {
693 insn_cache = (cs_insn *)total;
694 for (i = 0; i < c; i++, insn_cache++)
695 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800696 }
697
danghvu0d1aad12014-10-01 23:12:18 -0500698 cs_mem_free(total);
699 *insn = NULL;
700 handle->errnum = CS_ERR_MEM;
701 return 0;
702 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800703
danghvu0d1aad12014-10-01 23:12:18 -0500704 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800705 // continue to fill in the cache after the last instruction
706 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800707
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800708 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500709 f = 0;
710 } else
711 insn_cache++;
712
713 buffer += next_offset;
714 size -= next_offset;
715 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800716 }
717
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800718 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800719 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800720 cs_mem_free(total);
721 total = NULL;
722 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800723 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400724 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800725 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800726 // free all detail pointers
727 if (handle->detail) {
728 insn_cache = (cs_insn *)total;
729 for (i = 0; i < c; i++, insn_cache++)
730 cs_mem_free(insn_cache->detail);
731 }
732
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800733 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800734 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800735
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800736 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800737 return 0;
738 }
739
740 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800741 }
742
743 *insn = total;
744
745 return c;
746}
747
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800748CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800749CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700750size_t CAPSTONE_API cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800751{
752 return cs_disasm(ud, buffer, size, offset, count, insn);
753}
754
755CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700756void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800757{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800758 size_t i;
759
760 // free all detail pointers
761 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800762 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800763
764 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800765 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800766}
767
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800768CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700769cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800770{
771 cs_insn *insn;
772 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
773
774 insn = cs_mem_malloc(sizeof(cs_insn));
775 if (!insn) {
776 // insufficient memory
777 handle->errnum = CS_ERR_MEM;
778 return NULL;
779 } else {
780 if (handle->detail) {
781 // allocate memory for @detail pointer
782 insn->detail = cs_mem_malloc(sizeof(cs_detail));
783 if (insn->detail == NULL) { // insufficient memory
784 cs_mem_free(insn);
785 handle->errnum = CS_ERR_MEM;
786 return NULL;
787 }
788 } else
789 insn->detail = NULL;
790 }
791
792 return insn;
793}
794
hlide993f3622014-10-05 18:14:40 +0200795// iterator for instruction "single-stepping"
796CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700797bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800798 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200799{
800 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200801 uint16_t insn_size;
802 MCInst mci;
803 bool r;
804
805 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800806 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800807 return false;
hlide993f3622014-10-05 18:14:40 +0200808 }
809
810 handle->errnum = CS_ERR_OK;
811
hlide993f3622014-10-05 18:14:40 +0200812 MCInst_Init(&mci);
813 mci.csh = handle;
814
815 // relative branches need to know the address & size of current insn
816 mci.address = *address;
817
818 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800819 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200820 mci.flat_insn->address = *address;
821#ifdef CAPSTONE_DIET
822 // zero out mnemonic & op_str
823 mci.flat_insn->mnemonic[0] = '\0';
824 mci.flat_insn->op_str[0] = '\0';
825#endif
826
827 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800828 if (r) {
hlide993f3622014-10-05 18:14:40 +0200829 SStream ss;
830 SStream_Init(&ss);
831
832 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800833
834 // map internal instruction opcode to public insn ID
835 handle->insn_id(handle, insn, mci.Opcode);
836
hlide993f3622014-10-05 18:14:40 +0200837 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800838
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800839 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800840
hlide993f3622014-10-05 18:14:40 +0200841 *code += insn_size;
842 *size -= insn_size;
843 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800844 } else { // encounter a broken instruction
845 size_t skipdata_bytes;
846
847 // if there is no request to skip data, or remaining data is too small,
848 // then bail out
849 if (!handle->skipdata || handle->skipdata_size > *size)
850 return false;
851
852 if (handle->skipdata_setup.callback) {
853 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
854 0, handle->skipdata_setup.user_data);
855 if (skipdata_bytes > *size)
856 // remaining data is not enough
857 return false;
858
859 if (!skipdata_bytes)
860 // user requested not to skip data, so bail out
861 return false;
862 } else
863 skipdata_bytes = handle->skipdata_size;
864
865 // we have to skip some amount of data, depending on arch & mode
866 insn->id = 0; // invalid ID for this "data" instruction
867 insn->address = *address;
868 insn->size = (uint16_t)skipdata_bytes;
869 memcpy(insn->bytes, *code, skipdata_bytes);
870 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
871 sizeof(insn->mnemonic) - 1);
872 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
873
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800874 *code += skipdata_bytes;
875 *size -= skipdata_bytes;
876 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200877 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800878
879 return true;
hlide993f3622014-10-05 18:14:40 +0200880}
881
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800882// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800883CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700884const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800885{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800886 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800887
888 if (!handle || handle->reg_name == NULL) {
889 return NULL;
890 }
891
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800892 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800893}
894
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800895CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700896const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800897{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800898 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800899
900 if (!handle || handle->insn_name == NULL) {
901 return NULL;
902 }
903
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800904 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800905}
906
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800907CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700908const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800909{
910 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
911
912 if (!handle || handle->group_name == NULL) {
913 return NULL;
914 }
915
916 return handle->group_name(ud, group);
917}
918
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800919CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700920bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800921{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800922 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800923 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800924 return false;
925
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100926 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200927
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800928 if (!handle->detail) {
929 handle->errnum = CS_ERR_DETAIL;
930 return false;
931 }
932
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800933 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200934 handle->errnum = CS_ERR_SKIPDATA;
935 return false;
936 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200937
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800938 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200939 handle->errnum = CS_ERR_DETAIL;
940 return false;
941 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200942
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800943 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800944}
945
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800946CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700947bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800948{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800949 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800950 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800951 return false;
952
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100953 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200954
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800955 if (!handle->detail) {
956 handle->errnum = CS_ERR_DETAIL;
957 return false;
958 }
959
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800960 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200961 handle->errnum = CS_ERR_SKIPDATA;
962 return false;
963 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200964
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800965 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200966 handle->errnum = CS_ERR_DETAIL;
967 return false;
968 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200969
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800970 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800971}
972
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800973CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700974bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800976 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800977 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800978 return false;
979
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100980 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200981
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800982 if (!handle->detail) {
983 handle->errnum = CS_ERR_DETAIL;
984 return false;
985 }
986
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800987 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200988 handle->errnum = CS_ERR_SKIPDATA;
989 return false;
990 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200991
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800992 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200993 handle->errnum = CS_ERR_DETAIL;
994 return false;
995 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200996
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800997 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800998}
999
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001000CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001001int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001002{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001003 struct cs_struct *handle;
1004 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001005 if (!ud)
1006 return -1;
1007
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001008 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001009
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001010 if (!handle->detail) {
1011 handle->errnum = CS_ERR_DETAIL;
1012 return -1;
1013 }
1014
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001015 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001016 handle->errnum = CS_ERR_SKIPDATA;
1017 return -1;
1018 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001019
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001020 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001021 handle->errnum = CS_ERR_DETAIL;
1022 return -1;
1023 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001024
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001025 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001026
1027 switch (handle->arch) {
1028 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001029 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001030 return -1;
1031 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001032 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001033 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001034 count++;
1035 break;
1036 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001037 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001038 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001039 count++;
1040 break;
1041 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001042 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001043 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001044 count++;
1045 break;
1046 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001047 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001048 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001049 count++;
1050 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001051 case CS_ARCH_PPC:
1052 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001053 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001054 count++;
1055 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001056 case CS_ARCH_SPARC:
1057 for (i = 0; i < insn->detail->sparc.op_count; i++)
1058 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1059 count++;
1060 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001061 case CS_ARCH_SYSZ:
1062 for (i = 0; i < insn->detail->sysz.op_count; i++)
1063 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1064 count++;
1065 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001066 case CS_ARCH_XCORE:
1067 for (i = 0; i < insn->detail->xcore.op_count; i++)
1068 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1069 count++;
1070 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001071 }
1072
1073 return count;
1074}
1075
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001076CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001077int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001078 unsigned int post)
1079{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001080 struct cs_struct *handle;
1081 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001082 if (!ud)
1083 return -1;
1084
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001085 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001086
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001087 if (!handle->detail) {
1088 handle->errnum = CS_ERR_DETAIL;
1089 return -1;
1090 }
1091
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001092 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001093 handle->errnum = CS_ERR_SKIPDATA;
1094 return -1;
1095 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001096
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001097 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001098 handle->errnum = CS_ERR_DETAIL;
1099 return -1;
1100 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001101
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001102 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001103
1104 switch (handle->arch) {
1105 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001106 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001107 return -1;
1108 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001109 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001110 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001111 count++;
1112 if (count == post)
1113 return i;
1114 }
1115 break;
1116 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001117 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001118 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001119 count++;
1120 if (count == post)
1121 return i;
1122 }
1123 break;
1124 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001125 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001126 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001127 count++;
1128 if (count == post)
1129 return i;
1130 }
1131 break;
1132 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001133 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001134 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001135 count++;
1136 if (count == post)
1137 return i;
1138 }
1139 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001140 case CS_ARCH_PPC:
1141 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001142 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001143 count++;
1144 if (count == post)
1145 return i;
1146 }
1147 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001148 case CS_ARCH_SPARC:
1149 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1150 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1151 count++;
1152 if (count == post)
1153 return i;
1154 }
1155 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001156 case CS_ARCH_SYSZ:
1157 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1158 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1159 count++;
1160 if (count == post)
1161 return i;
1162 }
1163 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001164 case CS_ARCH_XCORE:
1165 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1166 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1167 count++;
1168 if (count == post)
1169 return i;
1170 }
1171 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001172 }
1173
1174 return -1;
1175}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001176
1177CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001178cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001179 cs_regs regs_read, uint8_t *regs_read_count,
1180 cs_regs regs_write, uint8_t *regs_write_count)
1181{
1182 struct cs_struct *handle;
1183
1184 if (!ud)
1185 return -1;
1186
1187 handle = (struct cs_struct *)(uintptr_t)ud;
1188
1189#ifdef CAPSTONE_DIET
1190 // This API does not work in DIET mode
1191 handle->errnum = CS_ERR_DIET;
1192 return CS_ERR_DIET;
1193#else
1194 if (!handle->detail) {
1195 handle->errnum = CS_ERR_DETAIL;
1196 return CS_ERR_DETAIL;
1197 }
1198
1199 if (!insn->id) {
1200 handle->errnum = CS_ERR_SKIPDATA;
1201 return CS_ERR_SKIPDATA;
1202 }
1203
1204 if (!insn->detail) {
1205 handle->errnum = CS_ERR_DETAIL;
1206 return CS_ERR_DETAIL;
1207 }
1208
1209 if (handle->reg_access) {
1210 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1211 } else {
1212 // this arch is unsupported yet
1213 handle->errnum = CS_ERR_ARCH;
1214 return CS_ERR_ARCH;
1215 }
1216
1217 return CS_ERR_OK;
1218#endif
1219}