blob: 4f181c2a0107ef742806acd41c8c130ab11851b2 [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)
vit969636d45852018-06-15 00:12:26 +03008#include <Availability.h>
reverser160e1982015-04-09 18:28:19 +01009#include <libkern/libkern.h>
10#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080011#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012#include <stdio.h>
13#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010014#endif
15
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010017#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080020#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080021
tandasat45e5eab2016-05-11 21:48:32 -070022#if defined(_KERNEL_MODE)
23#include "windows\winkernel_mm.h"
24#endif
25
tandasatf7fe6402016-05-16 20:32:36 -070026// Issue #681: Windows kernel does not support formatting float point
27#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
28#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || defined(CAPSTONE_HAS_M68K)
29#define CAPSTONE_STR_INTERNAL(x) #x
30#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
31#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
32
33#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.")
34
35#undef CAPSTONE_MSVC_WRANING_PREFIX
36#undef CAPSTONE_STR
37#undef CAPSTONE_STR_INTERNAL
38#endif
39#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
40
tandasat45e5eab2016-05-11 21:48:32 -070041#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080042#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080043#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080044// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080045#define INSN_CACHE_SIZE 8
46#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080047
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080048// default SKIPDATA mnemonic
vit9696a8c7e4a2017-05-25 11:01:48 +030049#ifndef CAPSTONE_DIET
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080050#define SKIPDATA_MNEM ".byte"
vit9696a8c7e4a2017-05-25 11:01:48 +030051#else // No printing is available in diet mode
52#define SKIPDATA_MNEM NULL
53#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080054
Alberto Garcia Illera5c302122018-03-29 16:17:37 +020055cs_err (*cs_arch_init[MAX_ARCH])(cs_struct *) = { NULL };
56cs_err (*cs_arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
57void (*cs_arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080058
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080059extern void ARM_enable(void);
60extern void AArch64_enable(void);
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020061extern void M680X_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020062extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080063extern void Mips_enable(void);
64extern void X86_enable(void);
65extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080066extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080067extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080068extern void XCore_enable(void);
Fotis Loukos0850d552016-05-03 15:52:11 +030069extern void TMS320C64x_enable(void);
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +080070extern void EVM_enable(void);
danghvu701b8502014-01-09 11:06:44 +070071
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080072static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080073{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080074 static bool initialized = false;
75
76 if (initialized)
77 return;
78
danghvub33bd2c2014-01-09 12:22:56 +070079#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080080 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070081#endif
82#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080083 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070084#endif
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020085#ifdef CAPSTONE_HAS_M680X
86 M680X_enable();
87#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020088#ifdef CAPSTONE_HAS_M68K
89 M68K_enable();
90#endif
danghvub33bd2c2014-01-09 12:22:56 +070091#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080092 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070093#endif
danghvub33bd2c2014-01-09 12:22:56 +070094#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080095 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070096#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080097#ifdef CAPSTONE_HAS_SPARC
98 Sparc_enable();
99#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800100#ifdef CAPSTONE_HAS_SYSZ
101 SystemZ_enable();
102#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800103#ifdef CAPSTONE_HAS_X86
104 X86_enable();
105#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800106#ifdef CAPSTONE_HAS_XCORE
107 XCore_enable();
108#endif
Fotis Loukos0850d552016-05-03 15:52:11 +0300109#ifdef CAPSTONE_HAS_TMS320C64X
110 TMS320C64x_enable();
111#endif
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800112#ifdef CAPSTONE_HAS_EVM
113 EVM_enable();
114#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800115
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800116
117 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700118}
119
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800120unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800121
tandasat45e5eab2016-05-11 21:48:32 -0700122#if defined(CAPSTONE_USE_SYS_DYN_MEM)
123#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700124// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800125cs_malloc_t cs_mem_malloc = malloc;
126cs_calloc_t cs_mem_calloc = calloc;
127cs_realloc_t cs_mem_realloc = realloc;
128cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200129#if defined(_WIN32_WCE)
130cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800131#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800132cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700133#endif // defined(_WIN32_WCE)
134
tandasat45e5eab2016-05-11 21:48:32 -0700135#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700136// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700137cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
138cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
139cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
140cs_free_t cs_mem_free = cs_winkernel_free;
141cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700142
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800143#else
tandasatc45f1db2016-05-11 22:52:34 -0700144// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100145extern void* kern_os_malloc(size_t size);
146extern void kern_os_free(void* addr);
147extern void* kern_os_realloc(void* addr, size_t nsize);
148
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800149static void* cs_kern_os_calloc(size_t num, size_t size)
150{
Pb86ad3d62015-11-05 18:08:25 +0100151 return kern_os_malloc(num * size); // malloc bzeroes the buffer
152}
153
154cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100155cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100156cs_realloc_t cs_mem_realloc = kern_os_realloc;
157cs_free_t cs_mem_free = kern_os_free;
158cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700159
160#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100161#else
tandasatc45f1db2016-05-11 22:52:34 -0700162// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800163cs_malloc_t cs_mem_malloc = NULL;
164cs_calloc_t cs_mem_calloc = NULL;
165cs_realloc_t cs_mem_realloc = NULL;
166cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800167cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700168
169#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800170
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800171CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700172unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800173{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800174 archs_enable();
175
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800176 if (major != NULL && minor != NULL) {
177 *major = CS_API_MAJOR;
178 *minor = CS_API_MINOR;
179 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800180
181 return (CS_API_MAJOR << 8) + CS_API_MINOR;
182}
183
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800184CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700185bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800186{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800187 archs_enable();
188
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800189 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800190 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800191 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800192 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300193 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200194 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800195 (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800196
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800197 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800198 return all_arch & (1 << query);
199
200 if (query == CS_SUPPORT_DIET) {
201#ifdef CAPSTONE_DIET
202 return true;
203#else
204 return false;
205#endif
206 }
207
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800208 if (query == CS_SUPPORT_X86_REDUCE) {
209#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800210 return true;
211#else
212 return false;
213#endif
214 }
215
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800216 // unsupported query
217 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800218}
219
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800220CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700221cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800222{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800223 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224 if (!handle)
225 return CS_ERR_CSH;
226
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100227 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800229 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800230}
231
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800232CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700233const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800234{
235 switch(code) {
236 default:
237 return "Unknown error code";
238 case CS_ERR_OK:
239 return "OK (CS_ERR_OK)";
240 case CS_ERR_MEM:
241 return "Out of memory (CS_ERR_MEM)";
242 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800243 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800244 case CS_ERR_HANDLE:
245 return "Invalid handle (CS_ERR_HANDLE)";
246 case CS_ERR_CSH:
247 return "Invalid csh (CS_ERR_CSH)";
248 case CS_ERR_MODE:
249 return "Invalid mode (CS_ERR_MODE)";
250 case CS_ERR_OPTION:
251 return "Invalid option (CS_ERR_OPTION)";
252 case CS_ERR_DETAIL:
253 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800254 case CS_ERR_MEMSETUP:
255 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800256 case CS_ERR_VERSION:
257 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800258 case CS_ERR_DIET:
259 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800260 case CS_ERR_SKIPDATA:
261 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700262 case CS_ERR_X86_ATT:
263 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
264 case CS_ERR_X86_INTEL:
265 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
266 case CS_ERR_X86_MASM:
267 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800268 }
269}
270
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800271CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700272cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800274 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800275 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800276 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800277 // Error: before cs_open(), dynamic memory management must be initialized
278 // with cs_option(CS_OPT_MEM)
279 return CS_ERR_MEMSETUP;
280
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800281 archs_enable();
282
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200283 if (arch < CS_ARCH_MAX && cs_arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800284 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800285 if (!ud) {
286 // memory insufficient
287 return CS_ERR_MEM;
288 }
289
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800290 ud->errnum = CS_ERR_OK;
291 ud->arch = arch;
292 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700293 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800294 // by default, do not break instruction into details
295 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800296
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800297 // default skipdata setup
298 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
299
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200300 err = cs_arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800301 if (err) {
302 cs_mem_free(ud);
303 *handle = 0;
304 return err;
305 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800306
307 *handle = (uintptr_t)ud;
308
309 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800310 } else {
311 *handle = 0;
312 return CS_ERR_ARCH;
313 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800314}
315
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800316CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700317cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800318{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800319 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800320 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800321
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800322 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800323 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800324 return CS_ERR_CSH;
325
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100326 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800327
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800328 if (ud->printer_info)
329 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800331 // free the linked list of customized mnemonic
332 tmp = ud->mnem_list;
333 while(tmp) {
334 next = tmp->next;
335 cs_mem_free(tmp);
336 tmp = next;
337 }
338
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800339 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800340
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800341 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800342 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800343
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800344 // invalidate this handle by ZERO out its value.
345 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800346 *handle = 0;
347
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800348 return CS_ERR_OK;
349}
350
351// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800352static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800353 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800355#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700356 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800357#endif
tandasat45e5eab2016-05-11 21:48:32 -0700358 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800359
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800360 // fill the instruction bytes.
361 // we might skip some redundant bytes in front in the case of X86
362 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
363 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800364
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800365 // alias instruction might have ID saved in OpcodePub
366 if (MCInst_getOpcodePub(mci))
367 insn->id = MCInst_getOpcodePub(mci);
368
369 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800370 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800371 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800372
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800373#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800374 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800375 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800376 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800377 for (sp = buffer; *sp; sp++) {
378 if (*sp == ' '|| *sp == '\t')
379 break;
380 if (*sp == '|') // lock|rep prefix for x86
381 *sp = ' ';
382 // copy to @mnemonic
383 *mnem = *sp;
384 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800385 }
386
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800387 *mnem = '\0';
388
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800389 // we might have customized mnemonic
390 if (handle->mnem_list) {
391 struct insn_mnem *tmp = handle->mnem_list;
392 while(tmp) {
393 if (tmp->insn.id == insn->id) {
394 // found this instruction, so copy its mnemonic
395 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
396 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
397 break;
398 }
399 tmp = tmp->next;
400 }
401 }
402
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800403 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800404 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800405 // find the next non-space char
406 sp++;
407 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
408 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800409 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
410 } else
411 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800412#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413}
414
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800415// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800416// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800417static uint8_t skipdata_size(cs_struct *handle)
418{
419 switch(handle->arch) {
420 default:
421 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700422 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800423 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800424 // skip 2 bytes on Thumb mode.
425 if (handle->mode & CS_MODE_THUMB)
426 return 2;
427 // otherwise, skip 4 bytes
428 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800429 case CS_ARCH_ARM64:
430 case CS_ARCH_MIPS:
431 case CS_ARCH_PPC:
432 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800433 // skip 4 bytes
434 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800435 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800436 // SystemZ instruction's length can be 2, 4 or 6 bytes,
437 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800438 return 2;
439 case CS_ARCH_X86:
440 // X86 has no restriction on instruction alignment
441 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800442 case CS_ARCH_XCORE:
443 // XCore instruction's length can be 2 or 4 bytes,
444 // so we just skip 2 bytes
445 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200446 case CS_ARCH_M68K:
447 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
448 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300449 case CS_ARCH_TMS320C64X:
450 // TMS320C64x alignment is 4.
451 return 4;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200452 case CS_ARCH_M680X:
453 // M680X alignment is 1.
454 return 1;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800455 case CS_ARCH_EVM:
456 // EVM alignment is 1.
457 return 1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800458 }
459}
460
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800461CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700462cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800463{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800464 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800465 cs_opt_mnem *opt;
466
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800467 archs_enable();
468
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800469 // cs_option() can be called with NULL handle just for CS_OPT_MEM
470 // This is supposed to be executed before all other APIs (even cs_open())
471 if (type == CS_OPT_MEM) {
472 cs_opt_mem *mem = (cs_opt_mem *)value;
473
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800474 cs_mem_malloc = mem->malloc;
475 cs_mem_calloc = mem->calloc;
476 cs_mem_realloc = mem->realloc;
477 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800478 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800479
480 return CS_ERR_OK;
481 }
482
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100483 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600484 if (!handle)
485 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800486
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800487 switch(type) {
488 default:
489 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800490
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800491 case CS_OPT_UNSIGNED:
492 handle->imm_unsigned = (cs_opt_value)value;
493 return CS_ERR_OK;
494
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800495 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500496 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800497 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800498
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800499 case CS_OPT_SKIPDATA:
500 handle->skipdata = (value == CS_OPT_ON);
501 if (handle->skipdata) {
502 if (handle->skipdata_size == 0) {
503 // set the default skipdata size
504 handle->skipdata_size = skipdata_size(handle);
505 }
506 }
507 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800508
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800509 case CS_OPT_SKIPDATA_SETUP:
510 if (value)
511 handle->skipdata_setup = *((cs_opt_skipdata *)value);
512 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800513
514 case CS_OPT_MNEMONIC:
515 opt = (cs_opt_mnem *)value;
516 if (opt->id) {
517 if (opt->mnemonic) {
518 struct insn_mnem *tmp;
519
520 // add new instruction, or replace existing instruction
521 // 1. find if we already had this insn in the linked list
522 tmp = handle->mnem_list;
523 while(tmp) {
524 if (tmp->insn.id == opt->id) {
525 // found this instruction, so replace its mnemonic
526 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
527 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
528 break;
529 }
530 tmp = tmp->next;
531 }
532
533 // 2. add this instruction if we have not had it yet
534 if (!tmp) {
535 tmp = cs_mem_malloc(sizeof(*tmp));
536 tmp->insn.id = opt->id;
537 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
538 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
539 // this new instruction is heading the list
540 tmp->next = handle->mnem_list;
541 handle->mnem_list = tmp;
542 }
543 return CS_ERR_OK;
544 } else {
545 struct insn_mnem *prev, *tmp;
546
547 // we want to delete an existing instruction
548 // iterate the list to find the instruction to remove it
549 tmp = handle->mnem_list;
550 prev = tmp;
551 while(tmp) {
552 if (tmp->insn.id == opt->id) {
553 // delete this instruction
554 if (tmp == prev) {
555 // head of the list
556 handle->mnem_list = tmp->next;
557 } else {
558 prev->next = tmp->next;
559 }
560 cs_mem_free(tmp);
561 break;
562 }
563 prev = tmp;
564 tmp = tmp->next;
565 }
566 }
567 }
568 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800569 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800570
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200571 return cs_arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800572}
573
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800574// generate @op_str for data instruction of SKIPDATA
vit9696a8c7e4a2017-05-25 11:01:48 +0300575#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800576static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
577{
578 char *p = opstr;
579 int len;
580 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700581 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800582
583 if (!size) {
584 opstr[0] = '\0';
585 return;
586 }
587
tandasat45e5eab2016-05-11 21:48:32 -0700588 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800589 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700590 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800591
592 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700593 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
594 if (len < 0) {
595 break;
596 }
597 if ((size_t)len > available - 1) {
598 break;
599 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800600 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700601 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800602 }
603}
vit9696a8c7e4a2017-05-25 11:01:48 +0300604#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800605
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800606// dynamicly allocate memory to contain disasm insn
607// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800608CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700609size_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 +0800610{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800611 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800612 MCInst mci;
613 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800614 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800615 unsigned int f = 0; // index of the next instruction in the cache
616 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800617 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800618 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800619 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800620 void *tmp;
621 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800622 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800623 size_t size_org;
624 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800625 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500626 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800627
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800628 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800629 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800630 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800631 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800632 return 0;
633 }
634
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800635 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800636
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800637 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800638 if (handle->arch == CS_ARCH_ARM)
639 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800640
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800641#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800642 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400643 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800644#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800645
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800646 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800647 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800648 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800649 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800650
651 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700652 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800653 if (total == NULL) {
654 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500655 handle->errnum = CS_ERR_MEM;
656 return 0;
657 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800658
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700659 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800660
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800661 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800662 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800663 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800664
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700665 // relative branches need to know the address & size of current insn
666 mci.address = offset;
667
668 if (handle->detail) {
669 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700670 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700671 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700672 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700673 }
674
675 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700676 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700677 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800678#ifdef CAPSTONE_DIET
679 // zero out mnemonic & op_str
680 mci.flat_insn->mnemonic[0] = '\0';
681 mci.flat_insn->op_str[0] = '\0';
682#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700683
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800684 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800685 if (r) {
686 SStream ss;
687 SStream_Init(&ss);
688
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700689 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800690
691 // map internal instruction opcode to public insn ID
692 handle->insn_id(handle, insn_cache, mci.Opcode);
693
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800694 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700695 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800696
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800697 // adjust for pseudo opcode (X86)
698 if (handle->arch == CS_ARCH_X86)
699 insn_cache->id += mci.popcode_adjust;
700
danghvu0d1aad12014-10-01 23:12:18 -0500701 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800702 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800703 // encounter a broken instruction
704
705 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800706 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800707 cs_mem_free(insn_cache->detail);
708 }
709
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800710 // if there is no request to skip data, or remaining data is too small,
711 // then bail out
712 if (!handle->skipdata || handle->skipdata_size > size)
713 break;
714
715 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800716 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800717 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800718 if (skipdata_bytes > size)
719 // remaining data is not enough
720 break;
721
722 if (!skipdata_bytes)
723 // user requested not to skip data, so bail out
724 break;
725 } else
726 skipdata_bytes = handle->skipdata_size;
727
728 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700729 insn_cache->id = 0; // invalid ID for this "data" instruction
730 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800731 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700732 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300733#ifdef CAPSTONE_DIET
734 insn_cache->mnemonic[0] = '\0';
735 insn_cache->op_str[0] = '\0';
736#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700737 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
738 sizeof(insn_cache->mnemonic) - 1);
739 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300740#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700741 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800742
danghvu0d1aad12014-10-01 23:12:18 -0500743 next_offset = skipdata_bytes;
744 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800745
danghvu0d1aad12014-10-01 23:12:18 -0500746 // one more instruction entering the cache
747 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800748
danghvu0d1aad12014-10-01 23:12:18 -0500749 // one more instruction disassembled
750 c++;
751 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800752 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500753 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800754
danghvu0d1aad12014-10-01 23:12:18 -0500755 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800756 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500757 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500758 total_size += (sizeof(cs_insn) * cache_size);
759 tmp = cs_mem_realloc(total, total_size);
760 if (tmp == NULL) { // insufficient memory
761 if (handle->detail) {
762 insn_cache = (cs_insn *)total;
763 for (i = 0; i < c; i++, insn_cache++)
764 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800765 }
766
danghvu0d1aad12014-10-01 23:12:18 -0500767 cs_mem_free(total);
768 *insn = NULL;
769 handle->errnum = CS_ERR_MEM;
770 return 0;
771 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800772
danghvu0d1aad12014-10-01 23:12:18 -0500773 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800774 // continue to fill in the cache after the last instruction
775 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800776
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800777 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500778 f = 0;
779 } else
780 insn_cache++;
781
782 buffer += next_offset;
783 size -= next_offset;
784 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800785 }
786
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800787 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800788 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800789 cs_mem_free(total);
790 total = NULL;
791 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800792 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400793 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800794 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800795 // free all detail pointers
796 if (handle->detail) {
797 insn_cache = (cs_insn *)total;
798 for (i = 0; i < c; i++, insn_cache++)
799 cs_mem_free(insn_cache->detail);
800 }
801
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800802 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800803 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800804
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800805 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800806 return 0;
807 }
808
809 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800810 }
811
812 *insn = total;
813
814 return c;
815}
816
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800817CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800818CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700819size_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 +0800820{
821 return cs_disasm(ud, buffer, size, offset, count, insn);
822}
823
824CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700825void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800826{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800827 size_t i;
828
829 // free all detail pointers
830 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800831 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800832
833 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800834 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800835}
836
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800837CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700838cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800839{
840 cs_insn *insn;
841 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
842
843 insn = cs_mem_malloc(sizeof(cs_insn));
844 if (!insn) {
845 // insufficient memory
846 handle->errnum = CS_ERR_MEM;
847 return NULL;
848 } else {
849 if (handle->detail) {
850 // allocate memory for @detail pointer
851 insn->detail = cs_mem_malloc(sizeof(cs_detail));
852 if (insn->detail == NULL) { // insufficient memory
853 cs_mem_free(insn);
854 handle->errnum = CS_ERR_MEM;
855 return NULL;
856 }
857 } else
858 insn->detail = NULL;
859 }
860
861 return insn;
862}
863
hlide993f3622014-10-05 18:14:40 +0200864// iterator for instruction "single-stepping"
865CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700866bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800867 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200868{
869 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200870 uint16_t insn_size;
871 MCInst mci;
872 bool r;
873
874 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800875 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800876 return false;
hlide993f3622014-10-05 18:14:40 +0200877 }
878
879 handle->errnum = CS_ERR_OK;
880
hlide993f3622014-10-05 18:14:40 +0200881 MCInst_Init(&mci);
882 mci.csh = handle;
883
884 // relative branches need to know the address & size of current insn
885 mci.address = *address;
886
887 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800888 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200889 mci.flat_insn->address = *address;
890#ifdef CAPSTONE_DIET
891 // zero out mnemonic & op_str
892 mci.flat_insn->mnemonic[0] = '\0';
893 mci.flat_insn->op_str[0] = '\0';
894#endif
895
896 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800897 if (r) {
hlide993f3622014-10-05 18:14:40 +0200898 SStream ss;
899 SStream_Init(&ss);
900
901 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800902
903 // map internal instruction opcode to public insn ID
904 handle->insn_id(handle, insn, mci.Opcode);
905
hlide993f3622014-10-05 18:14:40 +0200906 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800907
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800908 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800909
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800910 // adjust for pseudo opcode (X86)
911 if (handle->arch == CS_ARCH_X86)
912 insn->id += mci.popcode_adjust;
913
hlide993f3622014-10-05 18:14:40 +0200914 *code += insn_size;
915 *size -= insn_size;
916 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800917 } else { // encounter a broken instruction
918 size_t skipdata_bytes;
919
920 // if there is no request to skip data, or remaining data is too small,
921 // then bail out
922 if (!handle->skipdata || handle->skipdata_size > *size)
923 return false;
924
925 if (handle->skipdata_setup.callback) {
926 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
927 0, handle->skipdata_setup.user_data);
928 if (skipdata_bytes > *size)
929 // remaining data is not enough
930 return false;
931
932 if (!skipdata_bytes)
933 // user requested not to skip data, so bail out
934 return false;
935 } else
936 skipdata_bytes = handle->skipdata_size;
937
938 // we have to skip some amount of data, depending on arch & mode
939 insn->id = 0; // invalid ID for this "data" instruction
940 insn->address = *address;
941 insn->size = (uint16_t)skipdata_bytes;
vit9696a8c7e4a2017-05-25 11:01:48 +0300942#ifdef CAPSTONE_DIET
943 insn->mnemonic[0] = '\0';
944 insn->op_str[0] = '\0';
945#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800946 memcpy(insn->bytes, *code, skipdata_bytes);
947 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
948 sizeof(insn->mnemonic) - 1);
949 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300950#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800951
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800952 *code += skipdata_bytes;
953 *size -= skipdata_bytes;
954 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200955 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800956
957 return true;
hlide993f3622014-10-05 18:14:40 +0200958}
959
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800960// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800961CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700962const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800964 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800965
966 if (!handle || handle->reg_name == NULL) {
967 return NULL;
968 }
969
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800970 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800971}
972
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800973CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700974const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800976 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800977
978 if (!handle || handle->insn_name == NULL) {
979 return NULL;
980 }
981
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800982 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800983}
984
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800985CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700986const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800987{
988 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
989
990 if (!handle || handle->group_name == NULL) {
991 return NULL;
992 }
993
994 return handle->group_name(ud, group);
995}
996
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800997CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700998bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800999{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001000 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001001 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001002 return false;
1003
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001004 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001005
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001006 if (!handle->detail) {
1007 handle->errnum = CS_ERR_DETAIL;
1008 return false;
1009 }
1010
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001011 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001012 handle->errnum = CS_ERR_SKIPDATA;
1013 return false;
1014 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001015
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001016 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001017 handle->errnum = CS_ERR_DETAIL;
1018 return false;
1019 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001020
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001021 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001022}
1023
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001024CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001025bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001026{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001027 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001028 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001029 return false;
1030
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001031 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001032
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001033 if (!handle->detail) {
1034 handle->errnum = CS_ERR_DETAIL;
1035 return false;
1036 }
1037
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001038 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001039 handle->errnum = CS_ERR_SKIPDATA;
1040 return false;
1041 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001042
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001043 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001044 handle->errnum = CS_ERR_DETAIL;
1045 return false;
1046 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001047
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001048 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001049}
1050
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001051CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001052bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001053{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001054 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001055 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001056 return false;
1057
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001058 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001059
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001060 if (!handle->detail) {
1061 handle->errnum = CS_ERR_DETAIL;
1062 return false;
1063 }
1064
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001065 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001066 handle->errnum = CS_ERR_SKIPDATA;
1067 return false;
1068 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001069
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001070 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001071 handle->errnum = CS_ERR_DETAIL;
1072 return false;
1073 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001074
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001075 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001076}
1077
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001078CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001079int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001080{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001081 struct cs_struct *handle;
1082 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001083 if (!ud)
1084 return -1;
1085
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001086 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001087
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001088 if (!handle->detail) {
1089 handle->errnum = CS_ERR_DETAIL;
1090 return -1;
1091 }
1092
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001093 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001094 handle->errnum = CS_ERR_SKIPDATA;
1095 return -1;
1096 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001097
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001098 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001099 handle->errnum = CS_ERR_DETAIL;
1100 return -1;
1101 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001102
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001103 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001104
1105 switch (handle->arch) {
1106 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001107 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001108 return -1;
1109 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001110 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001111 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001112 count++;
1113 break;
1114 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001115 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001116 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001117 count++;
1118 break;
1119 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001120 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001121 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001122 count++;
1123 break;
1124 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001125 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001126 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001127 count++;
1128 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001129 case CS_ARCH_PPC:
1130 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001131 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001132 count++;
1133 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001134 case CS_ARCH_SPARC:
1135 for (i = 0; i < insn->detail->sparc.op_count; i++)
1136 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1137 count++;
1138 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001139 case CS_ARCH_SYSZ:
1140 for (i = 0; i < insn->detail->sysz.op_count; i++)
1141 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1142 count++;
1143 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001144 case CS_ARCH_XCORE:
1145 for (i = 0; i < insn->detail->xcore.op_count; i++)
1146 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1147 count++;
1148 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001149 case CS_ARCH_M68K:
1150 for (i = 0; i < insn->detail->m68k.op_count; i++)
1151 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1152 count++;
1153 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001154 case CS_ARCH_TMS320C64X:
1155 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1156 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1157 count++;
1158 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001159 case CS_ARCH_M680X:
1160 for (i = 0; i < insn->detail->m680x.op_count; i++)
1161 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1162 count++;
1163 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001164 case CS_ARCH_EVM:
1165#if 0
1166 for (i = 0; i < insn->detail->evm.op_count; i++)
1167 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1168 count++;
1169#endif
1170 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001171 }
1172
1173 return count;
1174}
1175
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001176CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001177int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001178 unsigned int post)
1179{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001180 struct cs_struct *handle;
1181 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001182 if (!ud)
1183 return -1;
1184
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001185 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001186
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001187 if (!handle->detail) {
1188 handle->errnum = CS_ERR_DETAIL;
1189 return -1;
1190 }
1191
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001192 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001193 handle->errnum = CS_ERR_SKIPDATA;
1194 return -1;
1195 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001196
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001197 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001198 handle->errnum = CS_ERR_DETAIL;
1199 return -1;
1200 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001201
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001202 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001203
1204 switch (handle->arch) {
1205 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001206 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001207 return -1;
1208 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001209 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001210 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001211 count++;
1212 if (count == post)
1213 return i;
1214 }
1215 break;
1216 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001217 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001218 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001219 count++;
1220 if (count == post)
1221 return i;
1222 }
1223 break;
1224 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001225 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001226 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001227 count++;
1228 if (count == post)
1229 return i;
1230 }
1231 break;
1232 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001233 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001234 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001235 count++;
1236 if (count == post)
1237 return i;
1238 }
1239 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001240 case CS_ARCH_PPC:
1241 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001242 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001243 count++;
1244 if (count == post)
1245 return i;
1246 }
1247 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001248 case CS_ARCH_SPARC:
1249 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1250 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1251 count++;
1252 if (count == post)
1253 return i;
1254 }
1255 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001256 case CS_ARCH_SYSZ:
1257 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1258 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1259 count++;
1260 if (count == post)
1261 return i;
1262 }
1263 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001264 case CS_ARCH_XCORE:
1265 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1266 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1267 count++;
1268 if (count == post)
1269 return i;
1270 }
1271 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001272 case CS_ARCH_M68K:
1273 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1274 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1275 count++;
1276 if (count == post)
1277 return i;
1278 }
1279 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001280 case CS_ARCH_TMS320C64X:
1281 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1282 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1283 count++;
1284 if (count == post)
1285 return i;
1286 }
1287 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001288 case CS_ARCH_M680X:
1289 for (i = 0; i < insn->detail->m680x.op_count; i++) {
1290 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1291 count++;
1292 if (count == post)
1293 return i;
1294 }
1295 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001296 case CS_ARCH_EVM:
1297#if 0
1298 for (i = 0; i < insn->detail->evm.op_count; i++) {
1299 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1300 count++;
1301 if (count == post)
1302 return i;
1303 }
1304#endif
1305 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001306 }
1307
1308 return -1;
1309}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001310
1311CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001312cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001313 cs_regs regs_read, uint8_t *regs_read_count,
1314 cs_regs regs_write, uint8_t *regs_write_count)
1315{
1316 struct cs_struct *handle;
1317
1318 if (!ud)
1319 return -1;
1320
1321 handle = (struct cs_struct *)(uintptr_t)ud;
1322
1323#ifdef CAPSTONE_DIET
1324 // This API does not work in DIET mode
1325 handle->errnum = CS_ERR_DIET;
1326 return CS_ERR_DIET;
1327#else
1328 if (!handle->detail) {
1329 handle->errnum = CS_ERR_DETAIL;
1330 return CS_ERR_DETAIL;
1331 }
1332
1333 if (!insn->id) {
1334 handle->errnum = CS_ERR_SKIPDATA;
1335 return CS_ERR_SKIPDATA;
1336 }
1337
1338 if (!insn->detail) {
1339 handle->errnum = CS_ERR_DETAIL;
1340 return CS_ERR_DETAIL;
1341 }
1342
1343 if (handle->reg_access) {
1344 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1345 } else {
1346 // this arch is unsupported yet
1347 handle->errnum = CS_ERR_ARCH;
1348 return CS_ERR_ARCH;
1349 }
1350
1351 return CS_ERR_OK;
1352#endif
1353}