blob: 2b53ac7ba27c9781a60b18d4ff9fc2926569fe45 [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
vit9696a8c7e4a2017-05-25 11:01:48 +030048#ifndef CAPSTONE_DIET
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080049#define SKIPDATA_MNEM ".byte"
vit9696a8c7e4a2017-05-25 11:01:48 +030050#else // No printing is available in diet mode
51#define SKIPDATA_MNEM NULL
52#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080053
Alberto Garcia Illera5c302122018-03-29 16:17:37 +020054cs_err (*cs_arch_init[MAX_ARCH])(cs_struct *) = { NULL };
55cs_err (*cs_arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
56void (*cs_arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080057
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080058extern void ARM_enable(void);
59extern void AArch64_enable(void);
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020060extern void M680X_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020061extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080062extern void Mips_enable(void);
63extern void X86_enable(void);
64extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080065extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080066extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080067extern void XCore_enable(void);
Fotis Loukos0850d552016-05-03 15:52:11 +030068extern void TMS320C64x_enable(void);
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +080069extern void EVM_enable(void);
danghvu701b8502014-01-09 11:06:44 +070070
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080071static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080072{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080073 static bool initialized = false;
74
75 if (initialized)
76 return;
77
danghvub33bd2c2014-01-09 12:22:56 +070078#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080079 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070080#endif
81#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080082 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070083#endif
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020084#ifdef CAPSTONE_HAS_M680X
85 M680X_enable();
86#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020087#ifdef CAPSTONE_HAS_M68K
88 M68K_enable();
89#endif
danghvub33bd2c2014-01-09 12:22:56 +070090#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080091 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070092#endif
danghvub33bd2c2014-01-09 12:22:56 +070093#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080094 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070095#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080096#ifdef CAPSTONE_HAS_SPARC
97 Sparc_enable();
98#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080099#ifdef CAPSTONE_HAS_SYSZ
100 SystemZ_enable();
101#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800102#ifdef CAPSTONE_HAS_X86
103 X86_enable();
104#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800105#ifdef CAPSTONE_HAS_XCORE
106 XCore_enable();
107#endif
Fotis Loukos0850d552016-05-03 15:52:11 +0300108#ifdef CAPSTONE_HAS_TMS320C64X
109 TMS320C64x_enable();
110#endif
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800111#ifdef CAPSTONE_HAS_EVM
112 EVM_enable();
113#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800114
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800115
116 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700117}
118
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800119unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800120
tandasat45e5eab2016-05-11 21:48:32 -0700121#if defined(CAPSTONE_USE_SYS_DYN_MEM)
122#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700123// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800124cs_malloc_t cs_mem_malloc = malloc;
125cs_calloc_t cs_mem_calloc = calloc;
126cs_realloc_t cs_mem_realloc = realloc;
127cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200128#if defined(_WIN32_WCE)
129cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800130#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800131cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700132#endif // defined(_WIN32_WCE)
133
tandasat45e5eab2016-05-11 21:48:32 -0700134#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700135// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700136cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
137cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
138cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
139cs_free_t cs_mem_free = cs_winkernel_free;
140cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700141
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800142#else
tandasatc45f1db2016-05-11 22:52:34 -0700143// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100144extern void* kern_os_malloc(size_t size);
145extern void kern_os_free(void* addr);
146extern void* kern_os_realloc(void* addr, size_t nsize);
147
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800148static void* cs_kern_os_calloc(size_t num, size_t size)
149{
Pb86ad3d62015-11-05 18:08:25 +0100150 return kern_os_malloc(num * size); // malloc bzeroes the buffer
151}
152
153cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100154cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100155cs_realloc_t cs_mem_realloc = kern_os_realloc;
156cs_free_t cs_mem_free = kern_os_free;
157cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700158
159#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100160#else
tandasatc45f1db2016-05-11 22:52:34 -0700161// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800162cs_malloc_t cs_mem_malloc = NULL;
163cs_calloc_t cs_mem_calloc = NULL;
164cs_realloc_t cs_mem_realloc = NULL;
165cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800166cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700167
168#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800169
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800170CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700171unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800172{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800173 archs_enable();
174
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800175 if (major != NULL && minor != NULL) {
176 *major = CS_API_MAJOR;
177 *minor = CS_API_MINOR;
178 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800179
180 return (CS_API_MAJOR << 8) + CS_API_MINOR;
181}
182
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800183CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700184bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800185{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800186 archs_enable();
187
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800188 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800189 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800190 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800191 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300192 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200193 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800194 (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800195
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800196 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800197 return all_arch & (1 << query);
198
199 if (query == CS_SUPPORT_DIET) {
200#ifdef CAPSTONE_DIET
201 return true;
202#else
203 return false;
204#endif
205 }
206
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800207 if (query == CS_SUPPORT_X86_REDUCE) {
208#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800209 return true;
210#else
211 return false;
212#endif
213 }
214
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800215 // unsupported query
216 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800217}
218
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800219CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700220cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800221{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800222 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223 if (!handle)
224 return CS_ERR_CSH;
225
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100226 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800227
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800228 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229}
230
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800231CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700232const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800233{
234 switch(code) {
235 default:
236 return "Unknown error code";
237 case CS_ERR_OK:
238 return "OK (CS_ERR_OK)";
239 case CS_ERR_MEM:
240 return "Out of memory (CS_ERR_MEM)";
241 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800242 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800243 case CS_ERR_HANDLE:
244 return "Invalid handle (CS_ERR_HANDLE)";
245 case CS_ERR_CSH:
246 return "Invalid csh (CS_ERR_CSH)";
247 case CS_ERR_MODE:
248 return "Invalid mode (CS_ERR_MODE)";
249 case CS_ERR_OPTION:
250 return "Invalid option (CS_ERR_OPTION)";
251 case CS_ERR_DETAIL:
252 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800253 case CS_ERR_MEMSETUP:
254 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800255 case CS_ERR_VERSION:
256 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800257 case CS_ERR_DIET:
258 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800259 case CS_ERR_SKIPDATA:
260 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700261 case CS_ERR_X86_ATT:
262 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
263 case CS_ERR_X86_INTEL:
264 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
265 case CS_ERR_X86_MASM:
266 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800267 }
268}
269
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800270CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700271cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800272{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800273 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800274 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800275 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800276 // Error: before cs_open(), dynamic memory management must be initialized
277 // with cs_option(CS_OPT_MEM)
278 return CS_ERR_MEMSETUP;
279
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800280 archs_enable();
281
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200282 if (arch < CS_ARCH_MAX && cs_arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800283 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800284 if (!ud) {
285 // memory insufficient
286 return CS_ERR_MEM;
287 }
288
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800289 ud->errnum = CS_ERR_OK;
290 ud->arch = arch;
291 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700292 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800293 // by default, do not break instruction into details
294 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800295
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800296 // default skipdata setup
297 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
298
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200299 err = cs_arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800300 if (err) {
301 cs_mem_free(ud);
302 *handle = 0;
303 return err;
304 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800305
306 *handle = (uintptr_t)ud;
307
308 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800309 } else {
310 *handle = 0;
311 return CS_ERR_ARCH;
312 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313}
314
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800315CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700316cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800318 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800319 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800320
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800321 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800322 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800323 return CS_ERR_CSH;
324
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100325 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800326
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800327 if (ud->printer_info)
328 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800329
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800330 // free the linked list of customized mnemonic
331 tmp = ud->mnem_list;
332 while(tmp) {
333 next = tmp->next;
334 cs_mem_free(tmp);
335 tmp = next;
336 }
337
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800338 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800339
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800340 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800341 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800342
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800343 // invalidate this handle by ZERO out its value.
344 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800345 *handle = 0;
346
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800347 return CS_ERR_OK;
348}
349
350// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800351static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800352 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800354#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700355 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800356#endif
tandasat45e5eab2016-05-11 21:48:32 -0700357 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800358
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800359 // fill the instruction bytes.
360 // we might skip some redundant bytes in front in the case of X86
361 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
362 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800363
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800364 // alias instruction might have ID saved in OpcodePub
365 if (MCInst_getOpcodePub(mci))
366 insn->id = MCInst_getOpcodePub(mci);
367
368 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800369 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800370 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800371
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800372#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800373 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800374 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800375 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800376 for (sp = buffer; *sp; sp++) {
377 if (*sp == ' '|| *sp == '\t')
378 break;
379 if (*sp == '|') // lock|rep prefix for x86
380 *sp = ' ';
381 // copy to @mnemonic
382 *mnem = *sp;
383 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800384 }
385
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800386 *mnem = '\0';
387
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800388 // we might have customized mnemonic
389 if (handle->mnem_list) {
390 struct insn_mnem *tmp = handle->mnem_list;
391 while(tmp) {
392 if (tmp->insn.id == insn->id) {
393 // found this instruction, so copy its mnemonic
394 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
395 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
396 break;
397 }
398 tmp = tmp->next;
399 }
400 }
401
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800402 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800403 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800404 // find the next non-space char
405 sp++;
406 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
407 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800408 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
409 } else
410 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800411#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412}
413
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800414// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800415// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800416static uint8_t skipdata_size(cs_struct *handle)
417{
418 switch(handle->arch) {
419 default:
420 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700421 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800422 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800423 // skip 2 bytes on Thumb mode.
424 if (handle->mode & CS_MODE_THUMB)
425 return 2;
426 // otherwise, skip 4 bytes
427 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800428 case CS_ARCH_ARM64:
429 case CS_ARCH_MIPS:
430 case CS_ARCH_PPC:
431 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800432 // skip 4 bytes
433 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800434 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800435 // SystemZ instruction's length can be 2, 4 or 6 bytes,
436 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800437 return 2;
438 case CS_ARCH_X86:
439 // X86 has no restriction on instruction alignment
440 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800441 case CS_ARCH_XCORE:
442 // XCore instruction's length can be 2 or 4 bytes,
443 // so we just skip 2 bytes
444 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200445 case CS_ARCH_M68K:
446 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
447 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300448 case CS_ARCH_TMS320C64X:
449 // TMS320C64x alignment is 4.
450 return 4;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200451 case CS_ARCH_M680X:
452 // M680X alignment is 1.
453 return 1;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800454 case CS_ARCH_EVM:
455 // EVM alignment is 1.
456 return 1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800457 }
458}
459
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800460CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700461cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800462{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800463 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800464 cs_opt_mnem *opt;
465
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800466 archs_enable();
467
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800468 // cs_option() can be called with NULL handle just for CS_OPT_MEM
469 // This is supposed to be executed before all other APIs (even cs_open())
470 if (type == CS_OPT_MEM) {
471 cs_opt_mem *mem = (cs_opt_mem *)value;
472
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800473 cs_mem_malloc = mem->malloc;
474 cs_mem_calloc = mem->calloc;
475 cs_mem_realloc = mem->realloc;
476 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800477 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800478
479 return CS_ERR_OK;
480 }
481
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100482 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600483 if (!handle)
484 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800485
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800486 switch(type) {
487 default:
488 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800489
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800490 case CS_OPT_UNSIGNED:
491 handle->imm_unsigned = (cs_opt_value)value;
492 return CS_ERR_OK;
493
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800494 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500495 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800496 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800497
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800498 case CS_OPT_SKIPDATA:
499 handle->skipdata = (value == CS_OPT_ON);
500 if (handle->skipdata) {
501 if (handle->skipdata_size == 0) {
502 // set the default skipdata size
503 handle->skipdata_size = skipdata_size(handle);
504 }
505 }
506 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800507
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800508 case CS_OPT_SKIPDATA_SETUP:
509 if (value)
510 handle->skipdata_setup = *((cs_opt_skipdata *)value);
511 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800512
513 case CS_OPT_MNEMONIC:
514 opt = (cs_opt_mnem *)value;
515 if (opt->id) {
516 if (opt->mnemonic) {
517 struct insn_mnem *tmp;
518
519 // add new instruction, or replace existing instruction
520 // 1. find if we already had this insn in the linked list
521 tmp = handle->mnem_list;
522 while(tmp) {
523 if (tmp->insn.id == opt->id) {
524 // found this instruction, so replace its mnemonic
525 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
526 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
527 break;
528 }
529 tmp = tmp->next;
530 }
531
532 // 2. add this instruction if we have not had it yet
533 if (!tmp) {
534 tmp = cs_mem_malloc(sizeof(*tmp));
535 tmp->insn.id = opt->id;
536 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
537 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
538 // this new instruction is heading the list
539 tmp->next = handle->mnem_list;
540 handle->mnem_list = tmp;
541 }
542 return CS_ERR_OK;
543 } else {
544 struct insn_mnem *prev, *tmp;
545
546 // we want to delete an existing instruction
547 // iterate the list to find the instruction to remove it
548 tmp = handle->mnem_list;
549 prev = tmp;
550 while(tmp) {
551 if (tmp->insn.id == opt->id) {
552 // delete this instruction
553 if (tmp == prev) {
554 // head of the list
555 handle->mnem_list = tmp->next;
556 } else {
557 prev->next = tmp->next;
558 }
559 cs_mem_free(tmp);
560 break;
561 }
562 prev = tmp;
563 tmp = tmp->next;
564 }
565 }
566 }
567 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800568 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800569
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200570 return cs_arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800571}
572
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800573// generate @op_str for data instruction of SKIPDATA
vit9696a8c7e4a2017-05-25 11:01:48 +0300574#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800575static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
576{
577 char *p = opstr;
578 int len;
579 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700580 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800581
582 if (!size) {
583 opstr[0] = '\0';
584 return;
585 }
586
tandasat45e5eab2016-05-11 21:48:32 -0700587 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800588 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700589 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800590
591 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700592 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
593 if (len < 0) {
594 break;
595 }
596 if ((size_t)len > available - 1) {
597 break;
598 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800599 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700600 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800601 }
602}
vit9696a8c7e4a2017-05-25 11:01:48 +0300603#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800604
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800605// dynamicly allocate memory to contain disasm insn
606// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800607CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700608size_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 +0800609{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800610 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800611 MCInst mci;
612 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800613 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800614 unsigned int f = 0; // index of the next instruction in the cache
615 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800616 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800617 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800618 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800619 void *tmp;
620 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800621 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800622 size_t size_org;
623 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800624 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500625 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800626
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800627 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800628 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800629 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800630 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800631 return 0;
632 }
633
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800634 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800635
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800636 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800637 if (handle->arch == CS_ARCH_ARM)
638 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800639
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800640#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800641 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400642 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800643#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800644
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800645 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800646 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800647 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800648 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800649
650 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700651 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800652 if (total == NULL) {
653 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500654 handle->errnum = CS_ERR_MEM;
655 return 0;
656 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800657
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700658 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800659
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800660 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800661 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800662 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800663
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700664 // relative branches need to know the address & size of current insn
665 mci.address = offset;
666
667 if (handle->detail) {
668 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700669 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700670 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700671 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700672 }
673
674 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700675 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700676 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800677#ifdef CAPSTONE_DIET
678 // zero out mnemonic & op_str
679 mci.flat_insn->mnemonic[0] = '\0';
680 mci.flat_insn->op_str[0] = '\0';
681#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700682
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800683 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800684 if (r) {
685 SStream ss;
686 SStream_Init(&ss);
687
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700688 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800689
690 // map internal instruction opcode to public insn ID
691 handle->insn_id(handle, insn_cache, mci.Opcode);
692
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800693 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700694 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800695
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800696 // adjust for pseudo opcode (X86)
697 if (handle->arch == CS_ARCH_X86)
698 insn_cache->id += mci.popcode_adjust;
699
danghvu0d1aad12014-10-01 23:12:18 -0500700 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800701 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800702 // encounter a broken instruction
703
704 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800705 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800706 cs_mem_free(insn_cache->detail);
707 }
708
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800709 // if there is no request to skip data, or remaining data is too small,
710 // then bail out
711 if (!handle->skipdata || handle->skipdata_size > size)
712 break;
713
714 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800715 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800716 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800717 if (skipdata_bytes > size)
718 // remaining data is not enough
719 break;
720
721 if (!skipdata_bytes)
722 // user requested not to skip data, so bail out
723 break;
724 } else
725 skipdata_bytes = handle->skipdata_size;
726
727 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700728 insn_cache->id = 0; // invalid ID for this "data" instruction
729 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800730 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700731 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300732#ifdef CAPSTONE_DIET
733 insn_cache->mnemonic[0] = '\0';
734 insn_cache->op_str[0] = '\0';
735#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700736 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
737 sizeof(insn_cache->mnemonic) - 1);
738 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300739#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700740 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800741
danghvu0d1aad12014-10-01 23:12:18 -0500742 next_offset = skipdata_bytes;
743 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800744
danghvu0d1aad12014-10-01 23:12:18 -0500745 // one more instruction entering the cache
746 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800747
danghvu0d1aad12014-10-01 23:12:18 -0500748 // one more instruction disassembled
749 c++;
750 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800751 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500752 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800753
danghvu0d1aad12014-10-01 23:12:18 -0500754 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800755 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500756 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500757 total_size += (sizeof(cs_insn) * cache_size);
758 tmp = cs_mem_realloc(total, total_size);
759 if (tmp == NULL) { // insufficient memory
760 if (handle->detail) {
761 insn_cache = (cs_insn *)total;
762 for (i = 0; i < c; i++, insn_cache++)
763 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800764 }
765
danghvu0d1aad12014-10-01 23:12:18 -0500766 cs_mem_free(total);
767 *insn = NULL;
768 handle->errnum = CS_ERR_MEM;
769 return 0;
770 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800771
danghvu0d1aad12014-10-01 23:12:18 -0500772 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800773 // continue to fill in the cache after the last instruction
774 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800775
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800776 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500777 f = 0;
778 } else
779 insn_cache++;
780
781 buffer += next_offset;
782 size -= next_offset;
783 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800784 }
785
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800786 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800787 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800788 cs_mem_free(total);
789 total = NULL;
790 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800791 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400792 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800793 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800794 // free all detail pointers
795 if (handle->detail) {
796 insn_cache = (cs_insn *)total;
797 for (i = 0; i < c; i++, insn_cache++)
798 cs_mem_free(insn_cache->detail);
799 }
800
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800801 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800802 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800803
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800804 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800805 return 0;
806 }
807
808 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800809 }
810
811 *insn = total;
812
813 return c;
814}
815
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800816CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800817CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700818size_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 +0800819{
820 return cs_disasm(ud, buffer, size, offset, count, insn);
821}
822
823CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700824void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800825{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800826 size_t i;
827
828 // free all detail pointers
829 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800830 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800831
832 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800833 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800834}
835
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800836CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700837cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800838{
839 cs_insn *insn;
840 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
841
842 insn = cs_mem_malloc(sizeof(cs_insn));
843 if (!insn) {
844 // insufficient memory
845 handle->errnum = CS_ERR_MEM;
846 return NULL;
847 } else {
848 if (handle->detail) {
849 // allocate memory for @detail pointer
850 insn->detail = cs_mem_malloc(sizeof(cs_detail));
851 if (insn->detail == NULL) { // insufficient memory
852 cs_mem_free(insn);
853 handle->errnum = CS_ERR_MEM;
854 return NULL;
855 }
856 } else
857 insn->detail = NULL;
858 }
859
860 return insn;
861}
862
hlide993f3622014-10-05 18:14:40 +0200863// iterator for instruction "single-stepping"
864CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700865bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800866 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200867{
868 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200869 uint16_t insn_size;
870 MCInst mci;
871 bool r;
872
873 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800874 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800875 return false;
hlide993f3622014-10-05 18:14:40 +0200876 }
877
878 handle->errnum = CS_ERR_OK;
879
hlide993f3622014-10-05 18:14:40 +0200880 MCInst_Init(&mci);
881 mci.csh = handle;
882
883 // relative branches need to know the address & size of current insn
884 mci.address = *address;
885
886 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800887 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200888 mci.flat_insn->address = *address;
889#ifdef CAPSTONE_DIET
890 // zero out mnemonic & op_str
891 mci.flat_insn->mnemonic[0] = '\0';
892 mci.flat_insn->op_str[0] = '\0';
893#endif
894
895 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800896 if (r) {
hlide993f3622014-10-05 18:14:40 +0200897 SStream ss;
898 SStream_Init(&ss);
899
900 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800901
902 // map internal instruction opcode to public insn ID
903 handle->insn_id(handle, insn, mci.Opcode);
904
hlide993f3622014-10-05 18:14:40 +0200905 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800906
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800907 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800908
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800909 // adjust for pseudo opcode (X86)
910 if (handle->arch == CS_ARCH_X86)
911 insn->id += mci.popcode_adjust;
912
hlide993f3622014-10-05 18:14:40 +0200913 *code += insn_size;
914 *size -= insn_size;
915 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800916 } else { // encounter a broken instruction
917 size_t skipdata_bytes;
918
919 // if there is no request to skip data, or remaining data is too small,
920 // then bail out
921 if (!handle->skipdata || handle->skipdata_size > *size)
922 return false;
923
924 if (handle->skipdata_setup.callback) {
925 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
926 0, handle->skipdata_setup.user_data);
927 if (skipdata_bytes > *size)
928 // remaining data is not enough
929 return false;
930
931 if (!skipdata_bytes)
932 // user requested not to skip data, so bail out
933 return false;
934 } else
935 skipdata_bytes = handle->skipdata_size;
936
937 // we have to skip some amount of data, depending on arch & mode
938 insn->id = 0; // invalid ID for this "data" instruction
939 insn->address = *address;
940 insn->size = (uint16_t)skipdata_bytes;
vit9696a8c7e4a2017-05-25 11:01:48 +0300941#ifdef CAPSTONE_DIET
942 insn->mnemonic[0] = '\0';
943 insn->op_str[0] = '\0';
944#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800945 memcpy(insn->bytes, *code, skipdata_bytes);
946 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
947 sizeof(insn->mnemonic) - 1);
948 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300949#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800950
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800951 *code += skipdata_bytes;
952 *size -= skipdata_bytes;
953 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200954 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800955
956 return true;
hlide993f3622014-10-05 18:14:40 +0200957}
958
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800960CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700961const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800962{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800963 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800964
965 if (!handle || handle->reg_name == NULL) {
966 return NULL;
967 }
968
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800969 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800970}
971
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800972CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700973const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800974{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800975 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800976
977 if (!handle || handle->insn_name == NULL) {
978 return NULL;
979 }
980
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800981 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800982}
983
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800984CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700985const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800986{
987 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
988
989 if (!handle || handle->group_name == NULL) {
990 return NULL;
991 }
992
993 return handle->group_name(ud, group);
994}
995
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800996CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700997bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800998{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800999 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001000 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001001 return false;
1002
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001003 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001004
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001005 if (!handle->detail) {
1006 handle->errnum = CS_ERR_DETAIL;
1007 return false;
1008 }
1009
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001010 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001011 handle->errnum = CS_ERR_SKIPDATA;
1012 return false;
1013 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001014
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001015 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001016 handle->errnum = CS_ERR_DETAIL;
1017 return false;
1018 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001019
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001020 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001021}
1022
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001023CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001024bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001025{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001026 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001027 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001028 return false;
1029
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001030 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001031
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001032 if (!handle->detail) {
1033 handle->errnum = CS_ERR_DETAIL;
1034 return false;
1035 }
1036
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001037 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001038 handle->errnum = CS_ERR_SKIPDATA;
1039 return false;
1040 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001041
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001042 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001043 handle->errnum = CS_ERR_DETAIL;
1044 return false;
1045 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001046
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001047 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001048}
1049
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001050CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001051bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001052{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001053 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001054 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001055 return false;
1056
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001057 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001058
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001059 if (!handle->detail) {
1060 handle->errnum = CS_ERR_DETAIL;
1061 return false;
1062 }
1063
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001064 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001065 handle->errnum = CS_ERR_SKIPDATA;
1066 return false;
1067 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001068
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001069 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001070 handle->errnum = CS_ERR_DETAIL;
1071 return false;
1072 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001073
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001074 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001075}
1076
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001077CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001078int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001079{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001080 struct cs_struct *handle;
1081 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001082 if (!ud)
1083 return -1;
1084
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001085 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001086
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001087 if (!handle->detail) {
1088 handle->errnum = CS_ERR_DETAIL;
1089 return -1;
1090 }
1091
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001092 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001093 handle->errnum = CS_ERR_SKIPDATA;
1094 return -1;
1095 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001096
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001097 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001098 handle->errnum = CS_ERR_DETAIL;
1099 return -1;
1100 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001101
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001102 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001103
1104 switch (handle->arch) {
1105 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001106 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001107 return -1;
1108 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001109 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001110 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001111 count++;
1112 break;
1113 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001114 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001115 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001116 count++;
1117 break;
1118 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001119 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001120 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001121 count++;
1122 break;
1123 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001124 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001125 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001126 count++;
1127 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001128 case CS_ARCH_PPC:
1129 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001130 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001131 count++;
1132 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001133 case CS_ARCH_SPARC:
1134 for (i = 0; i < insn->detail->sparc.op_count; i++)
1135 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1136 count++;
1137 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001138 case CS_ARCH_SYSZ:
1139 for (i = 0; i < insn->detail->sysz.op_count; i++)
1140 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1141 count++;
1142 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001143 case CS_ARCH_XCORE:
1144 for (i = 0; i < insn->detail->xcore.op_count; i++)
1145 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1146 count++;
1147 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001148 case CS_ARCH_M68K:
1149 for (i = 0; i < insn->detail->m68k.op_count; i++)
1150 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1151 count++;
1152 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001153 case CS_ARCH_TMS320C64X:
1154 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1155 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1156 count++;
1157 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001158 case CS_ARCH_M680X:
1159 for (i = 0; i < insn->detail->m680x.op_count; i++)
1160 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1161 count++;
1162 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001163 case CS_ARCH_EVM:
1164#if 0
1165 for (i = 0; i < insn->detail->evm.op_count; i++)
1166 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1167 count++;
1168#endif
1169 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001170 }
1171
1172 return count;
1173}
1174
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001175CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001176int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001177 unsigned int post)
1178{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001179 struct cs_struct *handle;
1180 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001181 if (!ud)
1182 return -1;
1183
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001184 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001185
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001186 if (!handle->detail) {
1187 handle->errnum = CS_ERR_DETAIL;
1188 return -1;
1189 }
1190
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001191 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001192 handle->errnum = CS_ERR_SKIPDATA;
1193 return -1;
1194 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001195
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001196 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001197 handle->errnum = CS_ERR_DETAIL;
1198 return -1;
1199 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001200
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001201 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001202
1203 switch (handle->arch) {
1204 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001205 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001206 return -1;
1207 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001208 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001209 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001210 count++;
1211 if (count == post)
1212 return i;
1213 }
1214 break;
1215 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001216 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001217 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001218 count++;
1219 if (count == post)
1220 return i;
1221 }
1222 break;
1223 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001224 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001225 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001226 count++;
1227 if (count == post)
1228 return i;
1229 }
1230 break;
1231 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001232 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001233 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001234 count++;
1235 if (count == post)
1236 return i;
1237 }
1238 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001239 case CS_ARCH_PPC:
1240 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001241 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001242 count++;
1243 if (count == post)
1244 return i;
1245 }
1246 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001247 case CS_ARCH_SPARC:
1248 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1249 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1250 count++;
1251 if (count == post)
1252 return i;
1253 }
1254 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001255 case CS_ARCH_SYSZ:
1256 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1257 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1258 count++;
1259 if (count == post)
1260 return i;
1261 }
1262 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001263 case CS_ARCH_XCORE:
1264 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1265 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1266 count++;
1267 if (count == post)
1268 return i;
1269 }
1270 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001271 case CS_ARCH_M68K:
1272 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1273 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1274 count++;
1275 if (count == post)
1276 return i;
1277 }
1278 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001279 case CS_ARCH_TMS320C64X:
1280 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1281 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1282 count++;
1283 if (count == post)
1284 return i;
1285 }
1286 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001287 case CS_ARCH_M680X:
1288 for (i = 0; i < insn->detail->m680x.op_count; i++) {
1289 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1290 count++;
1291 if (count == post)
1292 return i;
1293 }
1294 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001295 case CS_ARCH_EVM:
1296#if 0
1297 for (i = 0; i < insn->detail->evm.op_count; i++) {
1298 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1299 count++;
1300 if (count == post)
1301 return i;
1302 }
1303#endif
1304 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001305 }
1306
1307 return -1;
1308}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001309
1310CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001311cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001312 cs_regs regs_read, uint8_t *regs_read_count,
1313 cs_regs regs_write, uint8_t *regs_write_count)
1314{
1315 struct cs_struct *handle;
1316
1317 if (!ud)
1318 return -1;
1319
1320 handle = (struct cs_struct *)(uintptr_t)ud;
1321
1322#ifdef CAPSTONE_DIET
1323 // This API does not work in DIET mode
1324 handle->errnum = CS_ERR_DIET;
1325 return CS_ERR_DIET;
1326#else
1327 if (!handle->detail) {
1328 handle->errnum = CS_ERR_DETAIL;
1329 return CS_ERR_DETAIL;
1330 }
1331
1332 if (!insn->id) {
1333 handle->errnum = CS_ERR_SKIPDATA;
1334 return CS_ERR_SKIPDATA;
1335 }
1336
1337 if (!insn->detail) {
1338 handle->errnum = CS_ERR_DETAIL;
1339 return CS_ERR_DETAIL;
1340 }
1341
1342 if (handle->reg_access) {
1343 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1344 } else {
1345 // this arch is unsupported yet
1346 handle->errnum = CS_ERR_ARCH;
1347 return CS_ERR_ARCH;
1348 }
1349
1350 return CS_ERR_OK;
1351#endif
1352}