blob: b57d02f100a1975c1607138ca0cba6d6c2458e19 [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)
tandasatc45f1db2016-05-11 22:52:34 -070091// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080092cs_malloc_t cs_mem_malloc = malloc;
93cs_calloc_t cs_mem_calloc = calloc;
94cs_realloc_t cs_mem_realloc = realloc;
95cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +020096#if defined(_WIN32_WCE)
97cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080098#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080099cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700100#endif // defined(_WIN32_WCE)
101
tandasat45e5eab2016-05-11 21:48:32 -0700102#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700103// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700104cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
105cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
106cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
107cs_free_t cs_mem_free = cs_winkernel_free;
108cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700109
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800110#else
tandasatc45f1db2016-05-11 22:52:34 -0700111// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100112extern void* kern_os_malloc(size_t size);
113extern void kern_os_free(void* addr);
114extern void* kern_os_realloc(void* addr, size_t nsize);
115
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800116static void* cs_kern_os_calloc(size_t num, size_t size)
117{
Pb86ad3d62015-11-05 18:08:25 +0100118 return kern_os_malloc(num * size); // malloc bzeroes the buffer
119}
120
121cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100122cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100123cs_realloc_t cs_mem_realloc = kern_os_realloc;
124cs_free_t cs_mem_free = kern_os_free;
125cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700126
127#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100128#else
tandasatc45f1db2016-05-11 22:52:34 -0700129// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800130cs_malloc_t cs_mem_malloc = NULL;
131cs_calloc_t cs_mem_calloc = NULL;
132cs_realloc_t cs_mem_realloc = NULL;
133cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800134cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700135
136#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800137
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800138CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700139unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800140{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800141 archs_enable();
142
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800143 if (major != NULL && minor != NULL) {
144 *major = CS_API_MAJOR;
145 *minor = CS_API_MINOR;
146 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800147
148 return (CS_API_MAJOR << 8) + CS_API_MINOR;
149}
150
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800151CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700152bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800153{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800154 archs_enable();
155
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800156 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800157 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800158 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800159 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800160 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800161
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800162 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800163 return all_arch & (1 << query);
164
165 if (query == CS_SUPPORT_DIET) {
166#ifdef CAPSTONE_DIET
167 return true;
168#else
169 return false;
170#endif
171 }
172
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800173 if (query == CS_SUPPORT_X86_REDUCE) {
174#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800175 return true;
176#else
177 return false;
178#endif
179 }
180
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800181 // unsupported query
182 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800183}
184
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800185CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700186cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800188 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189 if (!handle)
190 return CS_ERR_CSH;
191
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100192 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800194 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800195}
196
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800197CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700198const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800199{
200 switch(code) {
201 default:
202 return "Unknown error code";
203 case CS_ERR_OK:
204 return "OK (CS_ERR_OK)";
205 case CS_ERR_MEM:
206 return "Out of memory (CS_ERR_MEM)";
207 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800208 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800209 case CS_ERR_HANDLE:
210 return "Invalid handle (CS_ERR_HANDLE)";
211 case CS_ERR_CSH:
212 return "Invalid csh (CS_ERR_CSH)";
213 case CS_ERR_MODE:
214 return "Invalid mode (CS_ERR_MODE)";
215 case CS_ERR_OPTION:
216 return "Invalid option (CS_ERR_OPTION)";
217 case CS_ERR_DETAIL:
218 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800219 case CS_ERR_MEMSETUP:
220 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800221 case CS_ERR_VERSION:
222 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800223 case CS_ERR_DIET:
224 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800225 case CS_ERR_SKIPDATA:
226 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700227 case CS_ERR_X86_ATT:
228 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
229 case CS_ERR_X86_INTEL:
230 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
231 case CS_ERR_X86_MASM:
232 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800233 }
234}
235
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800236CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700237cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800238{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800239 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800240 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800241 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800242 // Error: before cs_open(), dynamic memory management must be initialized
243 // with cs_option(CS_OPT_MEM)
244 return CS_ERR_MEMSETUP;
245
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800246 archs_enable();
247
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800248 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800249 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800250 if (!ud) {
251 // memory insufficient
252 return CS_ERR_MEM;
253 }
254
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800255 ud->errnum = CS_ERR_OK;
256 ud->arch = arch;
257 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700258 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800259 // by default, do not break instruction into details
260 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800262 // default skipdata setup
263 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
264
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100265 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800266 if (err) {
267 cs_mem_free(ud);
268 *handle = 0;
269 return err;
270 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800271
272 *handle = (uintptr_t)ud;
273
274 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800275 } else {
276 *handle = 0;
277 return CS_ERR_ARCH;
278 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800279}
280
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800281CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700282cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800283{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800284 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800285 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800286
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800287 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800288 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289 return CS_ERR_CSH;
290
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100291 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800292
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800293 if (ud->printer_info)
294 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800295
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800296 // free the linked list of customized mnemonic
297 tmp = ud->mnem_list;
298 while(tmp) {
299 next = tmp->next;
300 cs_mem_free(tmp);
301 tmp = next;
302 }
303
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800304 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800305
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800306 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800307 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800308
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800309 // invalidate this handle by ZERO out its value.
310 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800311 *handle = 0;
312
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313 return CS_ERR_OK;
314}
315
316// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800317static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800318 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800320#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700321 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800322#endif
tandasat45e5eab2016-05-11 21:48:32 -0700323 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800324
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800325 // fill the instruction bytes.
326 // we might skip some redundant bytes in front in the case of X86
327 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
328 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800329
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800330 // alias instruction might have ID saved in OpcodePub
331 if (MCInst_getOpcodePub(mci))
332 insn->id = MCInst_getOpcodePub(mci);
333
334 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800335 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800336 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800337
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800338#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800339 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800340 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100341 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800342 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800343 for (sp = buffer; *sp; sp++) {
344 if (*sp == ' '|| *sp == '\t')
345 break;
346 if (*sp == '|') // lock|rep prefix for x86
347 *sp = ' ';
348 // copy to @mnemonic
349 *mnem = *sp;
350 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800351 }
352
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800353 *mnem = '\0';
354
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800355 // we might have customized mnemonic
356 if (handle->mnem_list) {
357 struct insn_mnem *tmp = handle->mnem_list;
358 while(tmp) {
359 if (tmp->insn.id == insn->id) {
360 // found this instruction, so copy its mnemonic
361 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
362 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
363 break;
364 }
365 tmp = tmp->next;
366 }
367 }
368
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800369 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800370 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800371 // find the next non-space char
372 sp++;
373 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
374 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800375 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
376 } else
377 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800378#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379}
380
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800381// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800382// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800383static uint8_t skipdata_size(cs_struct *handle)
384{
385 switch(handle->arch) {
386 default:
387 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700388 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800389 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800390 // skip 2 bytes on Thumb mode.
391 if (handle->mode & CS_MODE_THUMB)
392 return 2;
393 // otherwise, skip 4 bytes
394 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800395 case CS_ARCH_ARM64:
396 case CS_ARCH_MIPS:
397 case CS_ARCH_PPC:
398 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800399 // skip 4 bytes
400 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800401 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800402 // SystemZ instruction's length can be 2, 4 or 6 bytes,
403 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800404 return 2;
405 case CS_ARCH_X86:
406 // X86 has no restriction on instruction alignment
407 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800408 case CS_ARCH_XCORE:
409 // XCore instruction's length can be 2 or 4 bytes,
410 // so we just skip 2 bytes
411 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200412 case CS_ARCH_M68K:
413 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
414 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800415 }
416}
417
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800418CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700419cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800420{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800421 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800422 cs_opt_mnem *opt;
423
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800424 archs_enable();
425
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800426 // cs_option() can be called with NULL handle just for CS_OPT_MEM
427 // This is supposed to be executed before all other APIs (even cs_open())
428 if (type == CS_OPT_MEM) {
429 cs_opt_mem *mem = (cs_opt_mem *)value;
430
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800431 cs_mem_malloc = mem->malloc;
432 cs_mem_calloc = mem->calloc;
433 cs_mem_realloc = mem->realloc;
434 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800435 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800436
437 return CS_ERR_OK;
438 }
439
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100440 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600441 if (!handle)
442 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800443
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800444 switch(type) {
445 default:
446 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800447
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800448 case CS_OPT_UNSIGNED:
449 handle->imm_unsigned = (cs_opt_value)value;
450 return CS_ERR_OK;
451
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800452 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500453 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800454 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800455
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800456 case CS_OPT_SKIPDATA:
457 handle->skipdata = (value == CS_OPT_ON);
458 if (handle->skipdata) {
459 if (handle->skipdata_size == 0) {
460 // set the default skipdata size
461 handle->skipdata_size = skipdata_size(handle);
462 }
463 }
464 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800465
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800466 case CS_OPT_SKIPDATA_SETUP:
467 if (value)
468 handle->skipdata_setup = *((cs_opt_skipdata *)value);
469 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800470
471 case CS_OPT_MNEMONIC:
472 opt = (cs_opt_mnem *)value;
473 if (opt->id) {
474 if (opt->mnemonic) {
475 struct insn_mnem *tmp;
476
477 // add new instruction, or replace existing instruction
478 // 1. find if we already had this insn in the linked list
479 tmp = handle->mnem_list;
480 while(tmp) {
481 if (tmp->insn.id == opt->id) {
482 // found this instruction, so replace its mnemonic
483 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
484 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
485 break;
486 }
487 tmp = tmp->next;
488 }
489
490 // 2. add this instruction if we have not had it yet
491 if (!tmp) {
492 tmp = cs_mem_malloc(sizeof(*tmp));
493 tmp->insn.id = opt->id;
494 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
495 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
496 // this new instruction is heading the list
497 tmp->next = handle->mnem_list;
498 handle->mnem_list = tmp;
499 }
500 return CS_ERR_OK;
501 } else {
502 struct insn_mnem *prev, *tmp;
503
504 // we want to delete an existing instruction
505 // iterate the list to find the instruction to remove it
506 tmp = handle->mnem_list;
507 prev = tmp;
508 while(tmp) {
509 if (tmp->insn.id == opt->id) {
510 // delete this instruction
511 if (tmp == prev) {
512 // head of the list
513 handle->mnem_list = tmp->next;
514 } else {
515 prev->next = tmp->next;
516 }
517 cs_mem_free(tmp);
518 break;
519 }
520 prev = tmp;
521 tmp = tmp->next;
522 }
523 }
524 }
525 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800526 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800527
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800528 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800529}
530
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800531// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800532static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
533{
534 char *p = opstr;
535 int len;
536 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700537 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800538
539 if (!size) {
540 opstr[0] = '\0';
541 return;
542 }
543
tandasat45e5eab2016-05-11 21:48:32 -0700544 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800545 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700546 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800547
548 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700549 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
550 if (len < 0) {
551 break;
552 }
553 if ((size_t)len > available - 1) {
554 break;
555 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800556 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700557 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800558 }
559}
560
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800561// dynamicly allocate memory to contain disasm insn
562// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800563CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700564size_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 +0800565{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800566 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800567 MCInst mci;
568 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800569 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800570 unsigned int f = 0; // index of the next instruction in the cache
571 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800572 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800573 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800574 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800575 void *tmp;
576 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800577 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800578 size_t size_org;
579 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800580 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500581 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800582
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800583 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800584 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800585 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800586 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800587 return 0;
588 }
589
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800590 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800591
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800592 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800593 if (handle->arch == CS_ARCH_ARM)
594 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800595
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800596#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800597 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400598 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800599#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800600
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800601 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800602 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800603 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800604 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800605
606 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700607 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800608 if (total == NULL) {
609 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500610 handle->errnum = CS_ERR_MEM;
611 return 0;
612 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800613
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700614 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800615
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800616 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800617 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800618 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800619
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700620 // relative branches need to know the address & size of current insn
621 mci.address = offset;
622
623 if (handle->detail) {
624 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700625 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700626 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700627 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700628 }
629
630 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700631 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700632 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800633#ifdef CAPSTONE_DIET
634 // zero out mnemonic & op_str
635 mci.flat_insn->mnemonic[0] = '\0';
636 mci.flat_insn->op_str[0] = '\0';
637#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700638
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800639 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800640 if (r) {
641 SStream ss;
642 SStream_Init(&ss);
643
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700644 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800645
646 // map internal instruction opcode to public insn ID
647 handle->insn_id(handle, insn_cache, mci.Opcode);
648
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800649 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800650
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700651 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800652
danghvu0d1aad12014-10-01 23:12:18 -0500653 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800654 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800655 // encounter a broken instruction
656
657 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800658 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800659 cs_mem_free(insn_cache->detail);
660 }
661
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800662 // if there is no request to skip data, or remaining data is too small,
663 // then bail out
664 if (!handle->skipdata || handle->skipdata_size > size)
665 break;
666
667 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800668 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800669 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800670 if (skipdata_bytes > size)
671 // remaining data is not enough
672 break;
673
674 if (!skipdata_bytes)
675 // user requested not to skip data, so bail out
676 break;
677 } else
678 skipdata_bytes = handle->skipdata_size;
679
680 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700681 insn_cache->id = 0; // invalid ID for this "data" instruction
682 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800683 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700684 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
685 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
686 sizeof(insn_cache->mnemonic) - 1);
687 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
688 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800689
danghvu0d1aad12014-10-01 23:12:18 -0500690 next_offset = skipdata_bytes;
691 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800692
danghvu0d1aad12014-10-01 23:12:18 -0500693 // one more instruction entering the cache
694 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800695
danghvu0d1aad12014-10-01 23:12:18 -0500696 // one more instruction disassembled
697 c++;
698 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800699 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500700 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800701
danghvu0d1aad12014-10-01 23:12:18 -0500702 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800703 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500704 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500705 total_size += (sizeof(cs_insn) * cache_size);
706 tmp = cs_mem_realloc(total, total_size);
707 if (tmp == NULL) { // insufficient memory
708 if (handle->detail) {
709 insn_cache = (cs_insn *)total;
710 for (i = 0; i < c; i++, insn_cache++)
711 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800712 }
713
danghvu0d1aad12014-10-01 23:12:18 -0500714 cs_mem_free(total);
715 *insn = NULL;
716 handle->errnum = CS_ERR_MEM;
717 return 0;
718 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800719
danghvu0d1aad12014-10-01 23:12:18 -0500720 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800721 // continue to fill in the cache after the last instruction
722 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800723
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800724 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500725 f = 0;
726 } else
727 insn_cache++;
728
729 buffer += next_offset;
730 size -= next_offset;
731 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800732 }
733
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800734 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800735 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800736 cs_mem_free(total);
737 total = NULL;
738 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800739 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400740 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800741 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800742 // free all detail pointers
743 if (handle->detail) {
744 insn_cache = (cs_insn *)total;
745 for (i = 0; i < c; i++, insn_cache++)
746 cs_mem_free(insn_cache->detail);
747 }
748
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800749 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800750 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800751
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800752 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800753 return 0;
754 }
755
756 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800757 }
758
759 *insn = total;
760
761 return c;
762}
763
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800764CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800765CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700766size_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 +0800767{
768 return cs_disasm(ud, buffer, size, offset, count, insn);
769}
770
771CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700772void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800773{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800774 size_t i;
775
776 // free all detail pointers
777 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800778 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800779
780 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800781 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800782}
783
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800784CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700785cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800786{
787 cs_insn *insn;
788 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
789
790 insn = cs_mem_malloc(sizeof(cs_insn));
791 if (!insn) {
792 // insufficient memory
793 handle->errnum = CS_ERR_MEM;
794 return NULL;
795 } else {
796 if (handle->detail) {
797 // allocate memory for @detail pointer
798 insn->detail = cs_mem_malloc(sizeof(cs_detail));
799 if (insn->detail == NULL) { // insufficient memory
800 cs_mem_free(insn);
801 handle->errnum = CS_ERR_MEM;
802 return NULL;
803 }
804 } else
805 insn->detail = NULL;
806 }
807
808 return insn;
809}
810
hlide993f3622014-10-05 18:14:40 +0200811// iterator for instruction "single-stepping"
812CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700813bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800814 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200815{
816 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200817 uint16_t insn_size;
818 MCInst mci;
819 bool r;
820
821 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800822 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800823 return false;
hlide993f3622014-10-05 18:14:40 +0200824 }
825
826 handle->errnum = CS_ERR_OK;
827
hlide993f3622014-10-05 18:14:40 +0200828 MCInst_Init(&mci);
829 mci.csh = handle;
830
831 // relative branches need to know the address & size of current insn
832 mci.address = *address;
833
834 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800835 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200836 mci.flat_insn->address = *address;
837#ifdef CAPSTONE_DIET
838 // zero out mnemonic & op_str
839 mci.flat_insn->mnemonic[0] = '\0';
840 mci.flat_insn->op_str[0] = '\0';
841#endif
842
843 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800844 if (r) {
hlide993f3622014-10-05 18:14:40 +0200845 SStream ss;
846 SStream_Init(&ss);
847
848 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800849
850 // map internal instruction opcode to public insn ID
851 handle->insn_id(handle, insn, mci.Opcode);
852
hlide993f3622014-10-05 18:14:40 +0200853 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800854
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800855 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800856
hlide993f3622014-10-05 18:14:40 +0200857 *code += insn_size;
858 *size -= insn_size;
859 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800860 } else { // encounter a broken instruction
861 size_t skipdata_bytes;
862
863 // if there is no request to skip data, or remaining data is too small,
864 // then bail out
865 if (!handle->skipdata || handle->skipdata_size > *size)
866 return false;
867
868 if (handle->skipdata_setup.callback) {
869 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
870 0, handle->skipdata_setup.user_data);
871 if (skipdata_bytes > *size)
872 // remaining data is not enough
873 return false;
874
875 if (!skipdata_bytes)
876 // user requested not to skip data, so bail out
877 return false;
878 } else
879 skipdata_bytes = handle->skipdata_size;
880
881 // we have to skip some amount of data, depending on arch & mode
882 insn->id = 0; // invalid ID for this "data" instruction
883 insn->address = *address;
884 insn->size = (uint16_t)skipdata_bytes;
885 memcpy(insn->bytes, *code, skipdata_bytes);
886 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
887 sizeof(insn->mnemonic) - 1);
888 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
889
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800890 *code += skipdata_bytes;
891 *size -= skipdata_bytes;
892 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200893 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800894
895 return true;
hlide993f3622014-10-05 18:14:40 +0200896}
897
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800898// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800899CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700900const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800901{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800902 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800903
904 if (!handle || handle->reg_name == NULL) {
905 return NULL;
906 }
907
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800908 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800909}
910
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800911CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700912const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800913{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800914 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800915
916 if (!handle || handle->insn_name == NULL) {
917 return NULL;
918 }
919
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800920 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800921}
922
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800923CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700924const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800925{
926 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
927
928 if (!handle || handle->group_name == NULL) {
929 return NULL;
930 }
931
932 return handle->group_name(ud, group);
933}
934
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800935CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700936bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800937{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800938 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800939 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800940 return false;
941
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100942 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200943
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800944 if (!handle->detail) {
945 handle->errnum = CS_ERR_DETAIL;
946 return false;
947 }
948
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800949 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200950 handle->errnum = CS_ERR_SKIPDATA;
951 return false;
952 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200953
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800954 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200955 handle->errnum = CS_ERR_DETAIL;
956 return false;
957 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200958
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800959 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800960}
961
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800962CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700963bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800964{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800965 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800966 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800967 return false;
968
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100969 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200970
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800971 if (!handle->detail) {
972 handle->errnum = CS_ERR_DETAIL;
973 return false;
974 }
975
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800976 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200977 handle->errnum = CS_ERR_SKIPDATA;
978 return false;
979 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200980
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800981 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200982 handle->errnum = CS_ERR_DETAIL;
983 return false;
984 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200985
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800986 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800987}
988
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800989CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700990bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800991{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800992 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800993 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800994 return false;
995
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100996 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200997
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800998 if (!handle->detail) {
999 handle->errnum = CS_ERR_DETAIL;
1000 return false;
1001 }
1002
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001003 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001004 handle->errnum = CS_ERR_SKIPDATA;
1005 return false;
1006 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001007
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001008 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001009 handle->errnum = CS_ERR_DETAIL;
1010 return false;
1011 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001012
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001013 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001014}
1015
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001016CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001017int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001018{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001019 struct cs_struct *handle;
1020 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001021 if (!ud)
1022 return -1;
1023
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001024 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001025
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001026 if (!handle->detail) {
1027 handle->errnum = CS_ERR_DETAIL;
1028 return -1;
1029 }
1030
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001031 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001032 handle->errnum = CS_ERR_SKIPDATA;
1033 return -1;
1034 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001035
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001036 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001037 handle->errnum = CS_ERR_DETAIL;
1038 return -1;
1039 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001040
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001041 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001042
1043 switch (handle->arch) {
1044 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001045 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001046 return -1;
1047 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001048 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001049 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001050 count++;
1051 break;
1052 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001053 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001054 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001055 count++;
1056 break;
1057 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001058 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001059 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001060 count++;
1061 break;
1062 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001063 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001064 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001065 count++;
1066 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001067 case CS_ARCH_PPC:
1068 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001069 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001070 count++;
1071 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001072 case CS_ARCH_SPARC:
1073 for (i = 0; i < insn->detail->sparc.op_count; i++)
1074 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1075 count++;
1076 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001077 case CS_ARCH_SYSZ:
1078 for (i = 0; i < insn->detail->sysz.op_count; i++)
1079 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1080 count++;
1081 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001082 case CS_ARCH_XCORE:
1083 for (i = 0; i < insn->detail->xcore.op_count; i++)
1084 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1085 count++;
1086 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001087 }
1088
1089 return count;
1090}
1091
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001092CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001093int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001094 unsigned int post)
1095{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001096 struct cs_struct *handle;
1097 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001098 if (!ud)
1099 return -1;
1100
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001101 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001102
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001103 if (!handle->detail) {
1104 handle->errnum = CS_ERR_DETAIL;
1105 return -1;
1106 }
1107
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001108 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001109 handle->errnum = CS_ERR_SKIPDATA;
1110 return -1;
1111 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001112
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001113 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001114 handle->errnum = CS_ERR_DETAIL;
1115 return -1;
1116 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001117
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001118 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001119
1120 switch (handle->arch) {
1121 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001122 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001123 return -1;
1124 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001125 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001126 if (insn->detail->arm.operands[i].type == (arm_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_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001133 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001134 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001135 count++;
1136 if (count == post)
1137 return i;
1138 }
1139 break;
1140 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001141 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001142 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001143 count++;
1144 if (count == post)
1145 return i;
1146 }
1147 break;
1148 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001149 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001150 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001151 count++;
1152 if (count == post)
1153 return i;
1154 }
1155 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001156 case CS_ARCH_PPC:
1157 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001158 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001159 count++;
1160 if (count == post)
1161 return i;
1162 }
1163 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001164 case CS_ARCH_SPARC:
1165 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1166 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1167 count++;
1168 if (count == post)
1169 return i;
1170 }
1171 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001172 case CS_ARCH_SYSZ:
1173 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1174 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1175 count++;
1176 if (count == post)
1177 return i;
1178 }
1179 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001180 case CS_ARCH_XCORE:
1181 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1182 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1183 count++;
1184 if (count == post)
1185 return i;
1186 }
1187 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001188 }
1189
1190 return -1;
1191}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001192
1193CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001194cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001195 cs_regs regs_read, uint8_t *regs_read_count,
1196 cs_regs regs_write, uint8_t *regs_write_count)
1197{
1198 struct cs_struct *handle;
1199
1200 if (!ud)
1201 return -1;
1202
1203 handle = (struct cs_struct *)(uintptr_t)ud;
1204
1205#ifdef CAPSTONE_DIET
1206 // This API does not work in DIET mode
1207 handle->errnum = CS_ERR_DIET;
1208 return CS_ERR_DIET;
1209#else
1210 if (!handle->detail) {
1211 handle->errnum = CS_ERR_DETAIL;
1212 return CS_ERR_DETAIL;
1213 }
1214
1215 if (!insn->id) {
1216 handle->errnum = CS_ERR_SKIPDATA;
1217 return CS_ERR_SKIPDATA;
1218 }
1219
1220 if (!insn->detail) {
1221 handle->errnum = CS_ERR_DETAIL;
1222 return CS_ERR_DETAIL;
1223 }
1224
1225 if (handle->reg_access) {
1226 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1227 } else {
1228 // this arch is unsupported yet
1229 handle->errnum = CS_ERR_ARCH;
1230 return CS_ERR_ARCH;
1231 }
1232
1233 return CS_ERR_OK;
1234#endif
1235}