blob: 3083905114f7b771d509ef5f545a8d253924b3c2 [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)
Satoshi Tanda2df9a8e2016-09-27 08:08:58 -07004#pragma warning(disable:4996) // disable MSVC's warning on strcpy()
5#pragma warning(disable:28719) // disable MSVC's warning on strcpy()
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01006#endif
reverser160e1982015-04-09 18:28:19 +01007#if defined(CAPSTONE_HAS_OSXKERNEL)
8#include <libkern/libkern.h>
9#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080010#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011#include <stdio.h>
12#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010013#endif
14
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010016#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080019#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
tandasat45e5eab2016-05-11 21:48:32 -070021#if defined(_KERNEL_MODE)
22#include "windows\winkernel_mm.h"
23#endif
24
tandasatf7fe6402016-05-16 20:32:36 -070025// Issue #681: Windows kernel does not support formatting float point
26#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
27#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || defined(CAPSTONE_HAS_M68K)
28#define CAPSTONE_STR_INTERNAL(x) #x
29#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
30#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
31
32#pragma message(CAPSTONE_MSVC_WRANING_PREFIX "Windows driver does not support full features for selected architecture(s). Define CAPSTONE_DIET to compile Capstone with only supported features. See issue #681 for details.")
33
34#undef CAPSTONE_MSVC_WRANING_PREFIX
35#undef CAPSTONE_STR
36#undef CAPSTONE_STR_INTERNAL
37#endif
38#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
39
tandasat45e5eab2016-05-11 21:48:32 -070040#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080041#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080042#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080043// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080044#define INSN_CACHE_SIZE 8
45#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080046
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080047// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080048#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080049
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080050cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080051cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
52void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080053
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080054extern void ARM_enable(void);
55extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020056extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080057extern void Mips_enable(void);
58extern void X86_enable(void);
59extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080060extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080061extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080062extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070063
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080064static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080065{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080066 static bool initialized = false;
67
68 if (initialized)
69 return;
70
danghvub33bd2c2014-01-09 12:22:56 +070071#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080072 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070073#endif
74#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080075 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070076#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020077#ifdef CAPSTONE_HAS_M68K
78 M68K_enable();
79#endif
danghvub33bd2c2014-01-09 12:22:56 +070080#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080081 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070082#endif
danghvub33bd2c2014-01-09 12:22:56 +070083#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080084 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070085#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080086#ifdef CAPSTONE_HAS_SPARC
87 Sparc_enable();
88#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080089#ifdef CAPSTONE_HAS_SYSZ
90 SystemZ_enable();
91#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080092#ifdef CAPSTONE_HAS_X86
93 X86_enable();
94#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080095#ifdef CAPSTONE_HAS_XCORE
96 XCore_enable();
97#endif
98
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080099
100 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700101}
102
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800103unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800104
tandasat45e5eab2016-05-11 21:48:32 -0700105#if defined(CAPSTONE_USE_SYS_DYN_MEM)
106#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700107// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800108cs_malloc_t cs_mem_malloc = malloc;
109cs_calloc_t cs_mem_calloc = calloc;
110cs_realloc_t cs_mem_realloc = realloc;
111cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200112#if defined(_WIN32_WCE)
113cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800114#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800115cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700116#endif // defined(_WIN32_WCE)
117
tandasat45e5eab2016-05-11 21:48:32 -0700118#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700119// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700120cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
121cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
122cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
123cs_free_t cs_mem_free = cs_winkernel_free;
124cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700125
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800126#else
tandasatc45f1db2016-05-11 22:52:34 -0700127// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100128extern void* kern_os_malloc(size_t size);
129extern void kern_os_free(void* addr);
130extern void* kern_os_realloc(void* addr, size_t nsize);
131
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800132static void* cs_kern_os_calloc(size_t num, size_t size)
133{
Pb86ad3d62015-11-05 18:08:25 +0100134 return kern_os_malloc(num * size); // malloc bzeroes the buffer
135}
136
137cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100138cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100139cs_realloc_t cs_mem_realloc = kern_os_realloc;
140cs_free_t cs_mem_free = kern_os_free;
141cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700142
143#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100144#else
tandasatc45f1db2016-05-11 22:52:34 -0700145// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800146cs_malloc_t cs_mem_malloc = NULL;
147cs_calloc_t cs_mem_calloc = NULL;
148cs_realloc_t cs_mem_realloc = NULL;
149cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800150cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700151
152#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800153
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800154CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700155unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800156{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800157 archs_enable();
158
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800159 if (major != NULL && minor != NULL) {
160 *major = CS_API_MAJOR;
161 *minor = CS_API_MINOR;
162 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800163
164 return (CS_API_MAJOR << 8) + CS_API_MINOR;
165}
166
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800167CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700168bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800169{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800170 archs_enable();
171
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800172 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800173 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800174 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800175 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800176 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800177
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800178 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800179 return all_arch & (1 << query);
180
181 if (query == CS_SUPPORT_DIET) {
182#ifdef CAPSTONE_DIET
183 return true;
184#else
185 return false;
186#endif
187 }
188
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800189 if (query == CS_SUPPORT_X86_REDUCE) {
190#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800191 return true;
192#else
193 return false;
194#endif
195 }
196
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800197 // unsupported query
198 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800199}
200
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800201CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700202cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800203{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800204 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800205 if (!handle)
206 return CS_ERR_CSH;
207
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100208 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800209
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800210 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800211}
212
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800213CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700214const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800215{
216 switch(code) {
217 default:
218 return "Unknown error code";
219 case CS_ERR_OK:
220 return "OK (CS_ERR_OK)";
221 case CS_ERR_MEM:
222 return "Out of memory (CS_ERR_MEM)";
223 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800224 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800225 case CS_ERR_HANDLE:
226 return "Invalid handle (CS_ERR_HANDLE)";
227 case CS_ERR_CSH:
228 return "Invalid csh (CS_ERR_CSH)";
229 case CS_ERR_MODE:
230 return "Invalid mode (CS_ERR_MODE)";
231 case CS_ERR_OPTION:
232 return "Invalid option (CS_ERR_OPTION)";
233 case CS_ERR_DETAIL:
234 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800235 case CS_ERR_MEMSETUP:
236 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800237 case CS_ERR_VERSION:
238 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800239 case CS_ERR_DIET:
240 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800241 case CS_ERR_SKIPDATA:
242 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700243 case CS_ERR_X86_ATT:
244 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
245 case CS_ERR_X86_INTEL:
246 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
247 case CS_ERR_X86_MASM:
248 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800249 }
250}
251
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800252CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700253cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800254{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800255 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800256 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800257 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800258 // Error: before cs_open(), dynamic memory management must be initialized
259 // with cs_option(CS_OPT_MEM)
260 return CS_ERR_MEMSETUP;
261
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800262 archs_enable();
263
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800264 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800265 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800266 if (!ud) {
267 // memory insufficient
268 return CS_ERR_MEM;
269 }
270
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800271 ud->errnum = CS_ERR_OK;
272 ud->arch = arch;
273 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700274 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800275 // by default, do not break instruction into details
276 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800278 // default skipdata setup
279 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
280
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100281 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800282 if (err) {
283 cs_mem_free(ud);
284 *handle = 0;
285 return err;
286 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800287
288 *handle = (uintptr_t)ud;
289
290 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800291 } else {
292 *handle = 0;
293 return CS_ERR_ARCH;
294 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800295}
296
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800297CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700298cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800299{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800300 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800301 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800302
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800303 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800304 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 return CS_ERR_CSH;
306
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100307 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800309 if (ud->printer_info)
310 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800311
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800312 // free the linked list of customized mnemonic
313 tmp = ud->mnem_list;
314 while(tmp) {
315 next = tmp->next;
316 cs_mem_free(tmp);
317 tmp = next;
318 }
319
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800320 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800321
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800322 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800323 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800324
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800325 // invalidate this handle by ZERO out its value.
326 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800327 *handle = 0;
328
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800329 return CS_ERR_OK;
330}
331
332// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800333static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800334 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800335{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800336#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700337 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800338#endif
tandasat45e5eab2016-05-11 21:48:32 -0700339 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800341 // fill the instruction bytes.
342 // we might skip some redundant bytes in front in the case of X86
343 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
344 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800345
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800346 // alias instruction might have ID saved in OpcodePub
347 if (MCInst_getOpcodePub(mci))
348 insn->id = MCInst_getOpcodePub(mci);
349
350 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800351 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800352 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800353
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800354#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800355 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800356 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100357 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800358 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800359 for (sp = buffer; *sp; sp++) {
360 if (*sp == ' '|| *sp == '\t')
361 break;
362 if (*sp == '|') // lock|rep prefix for x86
363 *sp = ' ';
364 // copy to @mnemonic
365 *mnem = *sp;
366 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800367 }
368
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800369 *mnem = '\0';
370
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800371 // we might have customized mnemonic
372 if (handle->mnem_list) {
373 struct insn_mnem *tmp = handle->mnem_list;
374 while(tmp) {
375 if (tmp->insn.id == insn->id) {
376 // found this instruction, so copy its mnemonic
377 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
378 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
379 break;
380 }
381 tmp = tmp->next;
382 }
383 }
384
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800385 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800386 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800387 // find the next non-space char
388 sp++;
389 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
390 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800391 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
392 } else
393 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800394#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395}
396
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800397// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800398// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800399static uint8_t skipdata_size(cs_struct *handle)
400{
401 switch(handle->arch) {
402 default:
403 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700404 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800405 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800406 // skip 2 bytes on Thumb mode.
407 if (handle->mode & CS_MODE_THUMB)
408 return 2;
409 // otherwise, skip 4 bytes
410 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800411 case CS_ARCH_ARM64:
412 case CS_ARCH_MIPS:
413 case CS_ARCH_PPC:
414 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800415 // skip 4 bytes
416 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800417 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800418 // SystemZ instruction's length can be 2, 4 or 6 bytes,
419 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800420 return 2;
421 case CS_ARCH_X86:
422 // X86 has no restriction on instruction alignment
423 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800424 case CS_ARCH_XCORE:
425 // XCore instruction's length can be 2 or 4 bytes,
426 // so we just skip 2 bytes
427 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200428 case CS_ARCH_M68K:
429 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
430 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800431 }
432}
433
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800434CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700435cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800436{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800437 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800438 cs_opt_mnem *opt;
439
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800440 archs_enable();
441
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800442 // cs_option() can be called with NULL handle just for CS_OPT_MEM
443 // This is supposed to be executed before all other APIs (even cs_open())
444 if (type == CS_OPT_MEM) {
445 cs_opt_mem *mem = (cs_opt_mem *)value;
446
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800447 cs_mem_malloc = mem->malloc;
448 cs_mem_calloc = mem->calloc;
449 cs_mem_realloc = mem->realloc;
450 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800451 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800452
453 return CS_ERR_OK;
454 }
455
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100456 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600457 if (!handle)
458 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800459
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800460 switch(type) {
461 default:
462 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800463
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800464 case CS_OPT_UNSIGNED:
465 handle->imm_unsigned = (cs_opt_value)value;
466 return CS_ERR_OK;
467
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800468 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500469 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800470 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800471
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800472 case CS_OPT_SKIPDATA:
473 handle->skipdata = (value == CS_OPT_ON);
474 if (handle->skipdata) {
475 if (handle->skipdata_size == 0) {
476 // set the default skipdata size
477 handle->skipdata_size = skipdata_size(handle);
478 }
479 }
480 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800481
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800482 case CS_OPT_SKIPDATA_SETUP:
483 if (value)
484 handle->skipdata_setup = *((cs_opt_skipdata *)value);
485 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800486
487 case CS_OPT_MNEMONIC:
488 opt = (cs_opt_mnem *)value;
489 if (opt->id) {
490 if (opt->mnemonic) {
491 struct insn_mnem *tmp;
492
493 // add new instruction, or replace existing instruction
494 // 1. find if we already had this insn in the linked list
495 tmp = handle->mnem_list;
496 while(tmp) {
497 if (tmp->insn.id == opt->id) {
498 // found this instruction, so replace its mnemonic
499 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
500 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
501 break;
502 }
503 tmp = tmp->next;
504 }
505
506 // 2. add this instruction if we have not had it yet
507 if (!tmp) {
508 tmp = cs_mem_malloc(sizeof(*tmp));
509 tmp->insn.id = opt->id;
510 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
511 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
512 // this new instruction is heading the list
513 tmp->next = handle->mnem_list;
514 handle->mnem_list = tmp;
515 }
516 return CS_ERR_OK;
517 } else {
518 struct insn_mnem *prev, *tmp;
519
520 // we want to delete an existing instruction
521 // iterate the list to find the instruction to remove it
522 tmp = handle->mnem_list;
523 prev = tmp;
524 while(tmp) {
525 if (tmp->insn.id == opt->id) {
526 // delete this instruction
527 if (tmp == prev) {
528 // head of the list
529 handle->mnem_list = tmp->next;
530 } else {
531 prev->next = tmp->next;
532 }
533 cs_mem_free(tmp);
534 break;
535 }
536 prev = tmp;
537 tmp = tmp->next;
538 }
539 }
540 }
541 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800542 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800543
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800544 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800545}
546
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800547// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800548static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
549{
550 char *p = opstr;
551 int len;
552 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700553 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800554
555 if (!size) {
556 opstr[0] = '\0';
557 return;
558 }
559
tandasat45e5eab2016-05-11 21:48:32 -0700560 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800561 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700562 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800563
564 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700565 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
566 if (len < 0) {
567 break;
568 }
569 if ((size_t)len > available - 1) {
570 break;
571 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800572 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700573 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800574 }
575}
576
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800577// dynamicly allocate memory to contain disasm insn
578// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800579CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700580size_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 +0800581{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800582 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800583 MCInst mci;
584 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800585 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800586 unsigned int f = 0; // index of the next instruction in the cache
587 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800589 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800590 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800591 void *tmp;
592 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800593 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800594 size_t size_org;
595 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800596 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500597 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800598
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800599 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800600 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800601 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800602 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800603 return 0;
604 }
605
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800606 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800607
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800608 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800609 if (handle->arch == CS_ARCH_ARM)
610 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800611
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800612#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800613 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400614 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800615#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800616
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800617 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800618 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800619 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800620 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800621
622 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700623 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800624 if (total == NULL) {
625 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500626 handle->errnum = CS_ERR_MEM;
627 return 0;
628 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800629
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700630 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800631
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800632 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800633 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800634 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800635
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700636 // relative branches need to know the address & size of current insn
637 mci.address = offset;
638
639 if (handle->detail) {
640 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700641 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700642 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700643 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700644 }
645
646 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700647 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700648 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800649#ifdef CAPSTONE_DIET
650 // zero out mnemonic & op_str
651 mci.flat_insn->mnemonic[0] = '\0';
652 mci.flat_insn->op_str[0] = '\0';
653#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700654
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800655 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800656 if (r) {
657 SStream ss;
658 SStream_Init(&ss);
659
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700660 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800661
662 // map internal instruction opcode to public insn ID
663 handle->insn_id(handle, insn_cache, mci.Opcode);
664
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800665 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700666 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800667
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800668 // adjust for pseudo opcode (X86)
669 if (handle->arch == CS_ARCH_X86)
670 insn_cache->id += mci.popcode_adjust;
671
danghvu0d1aad12014-10-01 23:12:18 -0500672 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800673 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800674 // encounter a broken instruction
675
676 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800677 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800678 cs_mem_free(insn_cache->detail);
679 }
680
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800681 // if there is no request to skip data, or remaining data is too small,
682 // then bail out
683 if (!handle->skipdata || handle->skipdata_size > size)
684 break;
685
686 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800687 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800688 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800689 if (skipdata_bytes > size)
690 // remaining data is not enough
691 break;
692
693 if (!skipdata_bytes)
694 // user requested not to skip data, so bail out
695 break;
696 } else
697 skipdata_bytes = handle->skipdata_size;
698
699 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700700 insn_cache->id = 0; // invalid ID for this "data" instruction
701 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800702 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700703 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
704 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
705 sizeof(insn_cache->mnemonic) - 1);
706 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
707 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800708
danghvu0d1aad12014-10-01 23:12:18 -0500709 next_offset = skipdata_bytes;
710 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800711
danghvu0d1aad12014-10-01 23:12:18 -0500712 // one more instruction entering the cache
713 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800714
danghvu0d1aad12014-10-01 23:12:18 -0500715 // one more instruction disassembled
716 c++;
717 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800718 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500719 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800720
danghvu0d1aad12014-10-01 23:12:18 -0500721 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800722 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500723 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500724 total_size += (sizeof(cs_insn) * cache_size);
725 tmp = cs_mem_realloc(total, total_size);
726 if (tmp == NULL) { // insufficient memory
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);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800731 }
732
danghvu0d1aad12014-10-01 23:12:18 -0500733 cs_mem_free(total);
734 *insn = NULL;
735 handle->errnum = CS_ERR_MEM;
736 return 0;
737 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800738
danghvu0d1aad12014-10-01 23:12:18 -0500739 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800740 // continue to fill in the cache after the last instruction
741 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800742
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800743 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500744 f = 0;
745 } else
746 insn_cache++;
747
748 buffer += next_offset;
749 size -= next_offset;
750 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800751 }
752
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800753 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800754 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800755 cs_mem_free(total);
756 total = NULL;
757 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800758 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400759 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800760 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800761 // free all detail pointers
762 if (handle->detail) {
763 insn_cache = (cs_insn *)total;
764 for (i = 0; i < c; i++, insn_cache++)
765 cs_mem_free(insn_cache->detail);
766 }
767
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800768 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800769 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800770
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800771 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800772 return 0;
773 }
774
775 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800776 }
777
778 *insn = total;
779
780 return c;
781}
782
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800783CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800784CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700785size_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 +0800786{
787 return cs_disasm(ud, buffer, size, offset, count, insn);
788}
789
790CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700791void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800792{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800793 size_t i;
794
795 // free all detail pointers
796 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800797 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800798
799 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800800 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800801}
802
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800803CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700804cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800805{
806 cs_insn *insn;
807 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
808
809 insn = cs_mem_malloc(sizeof(cs_insn));
810 if (!insn) {
811 // insufficient memory
812 handle->errnum = CS_ERR_MEM;
813 return NULL;
814 } else {
815 if (handle->detail) {
816 // allocate memory for @detail pointer
817 insn->detail = cs_mem_malloc(sizeof(cs_detail));
818 if (insn->detail == NULL) { // insufficient memory
819 cs_mem_free(insn);
820 handle->errnum = CS_ERR_MEM;
821 return NULL;
822 }
823 } else
824 insn->detail = NULL;
825 }
826
827 return insn;
828}
829
hlide993f3622014-10-05 18:14:40 +0200830// iterator for instruction "single-stepping"
831CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700832bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800833 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200834{
835 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200836 uint16_t insn_size;
837 MCInst mci;
838 bool r;
839
840 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800841 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800842 return false;
hlide993f3622014-10-05 18:14:40 +0200843 }
844
845 handle->errnum = CS_ERR_OK;
846
hlide993f3622014-10-05 18:14:40 +0200847 MCInst_Init(&mci);
848 mci.csh = handle;
849
850 // relative branches need to know the address & size of current insn
851 mci.address = *address;
852
853 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800854 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200855 mci.flat_insn->address = *address;
856#ifdef CAPSTONE_DIET
857 // zero out mnemonic & op_str
858 mci.flat_insn->mnemonic[0] = '\0';
859 mci.flat_insn->op_str[0] = '\0';
860#endif
861
862 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800863 if (r) {
hlide993f3622014-10-05 18:14:40 +0200864 SStream ss;
865 SStream_Init(&ss);
866
867 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800868
869 // map internal instruction opcode to public insn ID
870 handle->insn_id(handle, insn, mci.Opcode);
871
hlide993f3622014-10-05 18:14:40 +0200872 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800873
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800874 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800875
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800876 // adjust for pseudo opcode (X86)
877 if (handle->arch == CS_ARCH_X86)
878 insn->id += mci.popcode_adjust;
879
hlide993f3622014-10-05 18:14:40 +0200880 *code += insn_size;
881 *size -= insn_size;
882 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800883 } else { // encounter a broken instruction
884 size_t skipdata_bytes;
885
886 // if there is no request to skip data, or remaining data is too small,
887 // then bail out
888 if (!handle->skipdata || handle->skipdata_size > *size)
889 return false;
890
891 if (handle->skipdata_setup.callback) {
892 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
893 0, handle->skipdata_setup.user_data);
894 if (skipdata_bytes > *size)
895 // remaining data is not enough
896 return false;
897
898 if (!skipdata_bytes)
899 // user requested not to skip data, so bail out
900 return false;
901 } else
902 skipdata_bytes = handle->skipdata_size;
903
904 // we have to skip some amount of data, depending on arch & mode
905 insn->id = 0; // invalid ID for this "data" instruction
906 insn->address = *address;
907 insn->size = (uint16_t)skipdata_bytes;
908 memcpy(insn->bytes, *code, skipdata_bytes);
909 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
910 sizeof(insn->mnemonic) - 1);
911 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
912
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800913 *code += skipdata_bytes;
914 *size -= skipdata_bytes;
915 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200916 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800917
918 return true;
hlide993f3622014-10-05 18:14:40 +0200919}
920
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800921// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800922CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700923const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800924{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800925 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800926
927 if (!handle || handle->reg_name == NULL) {
928 return NULL;
929 }
930
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800931 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800932}
933
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800934CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700935const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800936{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800937 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800938
939 if (!handle || handle->insn_name == NULL) {
940 return NULL;
941 }
942
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800943 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800944}
945
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800946CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700947const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800948{
949 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
950
951 if (!handle || handle->group_name == NULL) {
952 return NULL;
953 }
954
955 return handle->group_name(ud, group);
956}
957
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800958CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700959bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800960{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800961 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800962 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963 return false;
964
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100965 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200966
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800967 if (!handle->detail) {
968 handle->errnum = CS_ERR_DETAIL;
969 return false;
970 }
971
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800972 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200973 handle->errnum = CS_ERR_SKIPDATA;
974 return false;
975 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200976
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800977 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200978 handle->errnum = CS_ERR_DETAIL;
979 return false;
980 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200981
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800982 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800983}
984
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800985CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700986bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800987{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800988 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800989 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800990 return false;
991
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100992 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200993
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800994 if (!handle->detail) {
995 handle->errnum = CS_ERR_DETAIL;
996 return false;
997 }
998
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800999 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001000 handle->errnum = CS_ERR_SKIPDATA;
1001 return false;
1002 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001003
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001004 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001005 handle->errnum = CS_ERR_DETAIL;
1006 return false;
1007 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001008
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001009 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001010}
1011
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001012CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001013bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001014{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001015 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001016 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001017 return false;
1018
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001019 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001020
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001021 if (!handle->detail) {
1022 handle->errnum = CS_ERR_DETAIL;
1023 return false;
1024 }
1025
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001026 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001027 handle->errnum = CS_ERR_SKIPDATA;
1028 return false;
1029 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001030
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001031 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001032 handle->errnum = CS_ERR_DETAIL;
1033 return false;
1034 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001035
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001036 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001037}
1038
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001039CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001040int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001041{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001042 struct cs_struct *handle;
1043 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001044 if (!ud)
1045 return -1;
1046
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001047 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001048
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001049 if (!handle->detail) {
1050 handle->errnum = CS_ERR_DETAIL;
1051 return -1;
1052 }
1053
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001054 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001055 handle->errnum = CS_ERR_SKIPDATA;
1056 return -1;
1057 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001058
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001059 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001060 handle->errnum = CS_ERR_DETAIL;
1061 return -1;
1062 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001063
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001064 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001065
1066 switch (handle->arch) {
1067 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001068 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001069 return -1;
1070 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001071 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001072 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001073 count++;
1074 break;
1075 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001076 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001077 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001078 count++;
1079 break;
1080 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001081 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001082 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001083 count++;
1084 break;
1085 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001086 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001087 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001088 count++;
1089 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001090 case CS_ARCH_PPC:
1091 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001092 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001093 count++;
1094 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001095 case CS_ARCH_SPARC:
1096 for (i = 0; i < insn->detail->sparc.op_count; i++)
1097 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1098 count++;
1099 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001100 case CS_ARCH_SYSZ:
1101 for (i = 0; i < insn->detail->sysz.op_count; i++)
1102 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1103 count++;
1104 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001105 case CS_ARCH_XCORE:
1106 for (i = 0; i < insn->detail->xcore.op_count; i++)
1107 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1108 count++;
1109 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001110 }
1111
1112 return count;
1113}
1114
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001115CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001116int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001117 unsigned int post)
1118{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001119 struct cs_struct *handle;
1120 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001121 if (!ud)
1122 return -1;
1123
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001124 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001125
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001126 if (!handle->detail) {
1127 handle->errnum = CS_ERR_DETAIL;
1128 return -1;
1129 }
1130
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001131 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001132 handle->errnum = CS_ERR_SKIPDATA;
1133 return -1;
1134 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001135
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001136 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001137 handle->errnum = CS_ERR_DETAIL;
1138 return -1;
1139 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001140
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001141 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001142
1143 switch (handle->arch) {
1144 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001145 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001146 return -1;
1147 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001148 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001149 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001150 count++;
1151 if (count == post)
1152 return i;
1153 }
1154 break;
1155 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001156 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001157 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001158 count++;
1159 if (count == post)
1160 return i;
1161 }
1162 break;
1163 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001164 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001165 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001166 count++;
1167 if (count == post)
1168 return i;
1169 }
1170 break;
1171 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001172 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001173 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001174 count++;
1175 if (count == post)
1176 return i;
1177 }
1178 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001179 case CS_ARCH_PPC:
1180 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001181 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001182 count++;
1183 if (count == post)
1184 return i;
1185 }
1186 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001187 case CS_ARCH_SPARC:
1188 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1189 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1190 count++;
1191 if (count == post)
1192 return i;
1193 }
1194 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001195 case CS_ARCH_SYSZ:
1196 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1197 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1198 count++;
1199 if (count == post)
1200 return i;
1201 }
1202 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001203 case CS_ARCH_XCORE:
1204 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1205 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1206 count++;
1207 if (count == post)
1208 return i;
1209 }
1210 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001211 }
1212
1213 return -1;
1214}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001215
1216CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001217cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001218 cs_regs regs_read, uint8_t *regs_read_count,
1219 cs_regs regs_write, uint8_t *regs_write_count)
1220{
1221 struct cs_struct *handle;
1222
1223 if (!ud)
1224 return -1;
1225
1226 handle = (struct cs_struct *)(uintptr_t)ud;
1227
1228#ifdef CAPSTONE_DIET
1229 // This API does not work in DIET mode
1230 handle->errnum = CS_ERR_DIET;
1231 return CS_ERR_DIET;
1232#else
1233 if (!handle->detail) {
1234 handle->errnum = CS_ERR_DETAIL;
1235 return CS_ERR_DETAIL;
1236 }
1237
1238 if (!insn->id) {
1239 handle->errnum = CS_ERR_SKIPDATA;
1240 return CS_ERR_SKIPDATA;
1241 }
1242
1243 if (!insn->detail) {
1244 handle->errnum = CS_ERR_DETAIL;
1245 return CS_ERR_DETAIL;
1246 }
1247
1248 if (handle->reg_access) {
1249 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1250 } else {
1251 // this arch is unsupported yet
1252 handle->errnum = CS_ERR_ARCH;
1253 return CS_ERR_ARCH;
1254 }
1255
1256 return CS_ERR_OK;
1257#endif
1258}