blob: d2ffecc75950452a9ae69a24b5b481b12bae22db [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
Travis Finkenauer65da43d2018-06-24 09:05:04 -040055#include "arch/AArch64/AArch64Module.h"
56#include "arch/ARM/ARMModule.h"
57#include "arch/EVM/EVMModule.h"
58#include "arch/M680X/M680XModule.h"
59#include "arch/M68K/M68KModule.h"
60#include "arch/Mips/MipsModule.h"
61#include "arch/PowerPC/PPCModule.h"
62#include "arch/Sparc/SparcModule.h"
63#include "arch/SystemZ/SystemZModule.h"
64#include "arch/TMS320C64x/TMS320C64xModule.h"
65#include "arch/X86/X86Module.h"
66#include "arch/XCore/XCoreModule.h"
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080067
Travis Finkenauer65da43d2018-06-24 09:05:04 -040068// constructor initialization for all archs
69static cs_err (*cs_arch_init[MAX_ARCH])(cs_struct *) = {
danghvub33bd2c2014-01-09 12:22:56 +070070#ifdef CAPSTONE_HAS_ARM
Travis Finkenauer65da43d2018-06-24 09:05:04 -040071 ARM_global_init,
72#else
73 NULL,
danghvub33bd2c2014-01-09 12:22:56 +070074#endif
75#ifdef CAPSTONE_HAS_ARM64
Travis Finkenauer65da43d2018-06-24 09:05:04 -040076 AArch64_global_init,
77#else
78 NULL,
Daniel Collin2ee675c2015-08-03 18:45:08 +020079#endif
danghvub33bd2c2014-01-09 12:22:56 +070080#ifdef CAPSTONE_HAS_MIPS
Travis Finkenauer65da43d2018-06-24 09:05:04 -040081 Mips_global_init,
82#else
83 NULL,
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080084#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080085#ifdef CAPSTONE_HAS_X86
Travis Finkenauer65da43d2018-06-24 09:05:04 -040086 X86_global_init,
87#else
88 NULL,
89#endif
90#ifdef CAPSTONE_HAS_POWERPC
91 PPC_global_init,
92#else
93 NULL,
94#endif
95#ifdef CAPSTONE_HAS_SPARC
96 Sparc_global_init,
97#else
98 NULL,
99#endif
100#ifdef CAPSTONE_HAS_SYSZ
101 SystemZ_global_init,
102#else
103 NULL,
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800104#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800105#ifdef CAPSTONE_HAS_XCORE
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400106 XCore_global_init,
107#else
108 NULL,
109#endif
110#ifdef CAPSTONE_HAS_M68K
111 M68K_global_init,
112#else
113 NULL,
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800114#endif
Fotis Loukos0850d552016-05-03 15:52:11 +0300115#ifdef CAPSTONE_HAS_TMS320C64X
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400116 TMS320C64x_global_init,
117#else
118 NULL,
119#endif
120#ifdef CAPSTONE_HAS_M680X
121 M680X_global_init,
122#else
123 NULL,
Fotis Loukos0850d552016-05-03 15:52:11 +0300124#endif
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800125#ifdef CAPSTONE_HAS_EVM
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400126 EVM_global_init,
127#else
128 NULL,
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800129#endif
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400130};
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800131
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400132// support cs_option() for all archs
133static cs_err (*cs_arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = {
134#ifdef CAPSTONE_HAS_ARM
135 ARM_option,
136#else
137 NULL,
138#endif
139#ifdef CAPSTONE_HAS_ARM64
140 AArch64_option,
141#else
142 NULL,
143#endif
144#ifdef CAPSTONE_HAS_MIPS
145 Mips_option,
146#else
147 NULL,
148#endif
149#ifdef CAPSTONE_HAS_X86
150 X86_option,
151#else
152 NULL,
153#endif
154#ifdef CAPSTONE_HAS_POWERPC
155 PPC_option,
156#else
157 NULL,
158#endif
159#ifdef CAPSTONE_HAS_SPARC
160 Sparc_option,
161#else
162 NULL,
163#endif
164#ifdef CAPSTONE_HAS_SYSZ
165 SystemZ_option,
166#else
167 NULL,
168#endif
169#ifdef CAPSTONE_HAS_XCORE
170 XCore_option,
171#else
172 NULL,
173#endif
174#ifdef CAPSTONE_HAS_M68K
175 M68K_option,
176#else
177 NULL,
178#endif
179#ifdef CAPSTONE_HAS_TMS320C64X
180 TMS320C64x_option,
181#else
182 NULL,
183#endif
184#ifdef CAPSTONE_HAS_M680X
185 M680X_option,
186#else
187 NULL,
188#endif
189#ifdef CAPSTONE_HAS_EVM
190 EVM_option,
191#else
192 NULL,
193#endif
194};
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800195
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400196// bitmask for finding disallowed modes for an arch:
197// to be called in cs_open()/cs_option()
198static cs_mode cs_arch_disallowed_mode_mask[MAX_ARCH] = {
199#ifdef CAPSTONE_HAS_ARM
200 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_V8 | CS_MODE_MCLASS
201 | CS_MODE_THUMB | CS_MODE_BIG_ENDIAN),
202#else
203 0,
204#endif
205#ifdef CAPSTONE_HAS_ARM64
206 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN),
207#else
208 0,
209#endif
210#ifdef CAPSTONE_HAS_MIPS
211 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_MICRO
212 | CS_MODE_MIPS32R6 | CS_MODE_BIG_ENDIAN | CS_MODE_MIPS2 | CS_MODE_MIPS3),
213#else
214 0,
215#endif
216#ifdef CAPSTONE_HAS_X86
217 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_16),
218#else
219 0,
220#endif
221#ifdef CAPSTONE_HAS_POWERPC
222 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_BIG_ENDIAN
223 | CS_MODE_QPX),
224#else
225 0,
226#endif
227#ifdef CAPSTONE_HAS_SPARC
228 ~(CS_MODE_BIG_ENDIAN | CS_MODE_V9),
229#else
230 0,
231#endif
232#ifdef CAPSTONE_HAS_SYSZ
233 ~(CS_MODE_BIG_ENDIAN),
234#else
235 0,
236#endif
237#ifdef CAPSTONE_HAS_XCORE
238 ~(CS_MODE_BIG_ENDIAN),
239#else
240 0,
241#endif
242#ifdef CAPSTONE_HAS_M68K
243 ~(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_000 | CS_MODE_M68K_010 | CS_MODE_M68K_020
244 | CS_MODE_M68K_030 | CS_MODE_M68K_040 | CS_MODE_M68K_060),
245#else
246 0,
247#endif
248#ifdef CAPSTONE_HAS_TMS320C64X
249 ~(CS_MODE_BIG_ENDIAN),
250#else
251 0,
252#endif
253#ifdef CAPSTONE_HAS_M680X
254 ~(CS_MODE_M680X_6301 | CS_MODE_M680X_6309 | CS_MODE_M680X_6800
255 | CS_MODE_M680X_6801 | CS_MODE_M680X_6805 | CS_MODE_M680X_6808
256 | CS_MODE_M680X_6809 | CS_MODE_M680X_6811 | CS_MODE_M680X_CPU12
257 | CS_MODE_M680X_HCS08),
258#else
259 0,
260#endif
261#ifdef CAPSTONE_HAS_EVM
262 0,
263#else
264 0,
265#endif
266};
danghvub33bd2c2014-01-09 12:22:56 +0700267
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400268// bitmask of enabled architectures
269static uint32_t all_arch = 0
270#ifdef CAPSTONE_HAS_ARM
271 | (1 << CS_ARCH_ARM)
272#endif
273#ifdef CAPSTONE_HAS_ARM64
274 | (1 << CS_ARCH_ARM64)
275#endif
276#ifdef CAPSTONE_HAS_MIPS
277 | (1 << CS_ARCH_MIPS)
278#endif
279#ifdef CAPSTONE_HAS_X86
280 | (1 << CS_ARCH_X86)
281#endif
282#ifdef CAPSTONE_HAS_POWERPC
283 | (1 << CS_ARCH_PPC)
284#endif
285#ifdef CAPSTONE_HAS_SPARC
286 | (1 << CS_ARCH_SPARC)
287#endif
288#ifdef CAPSTONE_HAS_SYSZ
289 | (1 << CS_ARCH_SYSZ)
290#endif
291#ifdef CAPSTONE_HAS_XCORE
292 | (1 << CS_ARCH_XCORE)
293#endif
294#ifdef CAPSTONE_HAS_M68K
295 | (1 << CS_ARCH_M68K)
296#endif
297#ifdef CAPSTONE_HAS_TMS320C64X
298 | (1 << CS_ARCH_TMS320C64X)
299#endif
300#ifdef CAPSTONE_HAS_M680X
301 | (1 << CS_ARCH_M680X)
302#endif
303#ifdef CAPSTONE_HAS_EVM
304 | (1 << CS_ARCH_EVM)
305#endif
306;
307
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800308
tandasat45e5eab2016-05-11 21:48:32 -0700309#if defined(CAPSTONE_USE_SYS_DYN_MEM)
310#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700311// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800312cs_malloc_t cs_mem_malloc = malloc;
313cs_calloc_t cs_mem_calloc = calloc;
314cs_realloc_t cs_mem_realloc = realloc;
315cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200316#if defined(_WIN32_WCE)
317cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800318#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800319cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700320#endif // defined(_WIN32_WCE)
321
tandasat45e5eab2016-05-11 21:48:32 -0700322#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700323// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700324cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
325cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
326cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
327cs_free_t cs_mem_free = cs_winkernel_free;
328cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700329
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800330#else
tandasatc45f1db2016-05-11 22:52:34 -0700331// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100332extern void* kern_os_malloc(size_t size);
333extern void kern_os_free(void* addr);
334extern void* kern_os_realloc(void* addr, size_t nsize);
335
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800336static void* cs_kern_os_calloc(size_t num, size_t size)
337{
Pb86ad3d62015-11-05 18:08:25 +0100338 return kern_os_malloc(num * size); // malloc bzeroes the buffer
339}
340
341cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100342cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100343cs_realloc_t cs_mem_realloc = kern_os_realloc;
344cs_free_t cs_mem_free = kern_os_free;
345cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700346
347#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100348#else
tandasatc45f1db2016-05-11 22:52:34 -0700349// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800350cs_malloc_t cs_mem_malloc = NULL;
351cs_calloc_t cs_mem_calloc = NULL;
352cs_realloc_t cs_mem_realloc = NULL;
353cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800354cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700355
356#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800357
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800358CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700359unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800360{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800361 if (major != NULL && minor != NULL) {
362 *major = CS_API_MAJOR;
363 *minor = CS_API_MINOR;
364 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800365
366 return (CS_API_MAJOR << 8) + CS_API_MINOR;
367}
368
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800369CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700370bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800371{
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800372 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800373 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800374 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800375 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300376 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200377 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800378 (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800379
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800380 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800381 return all_arch & (1 << query);
382
383 if (query == CS_SUPPORT_DIET) {
384#ifdef CAPSTONE_DIET
385 return true;
386#else
387 return false;
388#endif
389 }
390
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800391 if (query == CS_SUPPORT_X86_REDUCE) {
392#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800393 return true;
394#else
395 return false;
396#endif
397 }
398
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800399 // unsupported query
400 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800401}
402
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800403CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700404cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800405{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800406 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407 if (!handle)
408 return CS_ERR_CSH;
409
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100410 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800411
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800412 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413}
414
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800415CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700416const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800417{
418 switch(code) {
419 default:
420 return "Unknown error code";
421 case CS_ERR_OK:
422 return "OK (CS_ERR_OK)";
423 case CS_ERR_MEM:
424 return "Out of memory (CS_ERR_MEM)";
425 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800426 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800427 case CS_ERR_HANDLE:
428 return "Invalid handle (CS_ERR_HANDLE)";
429 case CS_ERR_CSH:
430 return "Invalid csh (CS_ERR_CSH)";
431 case CS_ERR_MODE:
432 return "Invalid mode (CS_ERR_MODE)";
433 case CS_ERR_OPTION:
434 return "Invalid option (CS_ERR_OPTION)";
435 case CS_ERR_DETAIL:
436 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800437 case CS_ERR_MEMSETUP:
438 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800439 case CS_ERR_VERSION:
440 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800441 case CS_ERR_DIET:
442 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800443 case CS_ERR_SKIPDATA:
444 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700445 case CS_ERR_X86_ATT:
446 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
447 case CS_ERR_X86_INTEL:
448 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
449 case CS_ERR_X86_MASM:
450 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800451 }
452}
453
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800454CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700455cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800456{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800457 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800458 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800459 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800460 // Error: before cs_open(), dynamic memory management must be initialized
461 // with cs_option(CS_OPT_MEM)
462 return CS_ERR_MEMSETUP;
463
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200464 if (arch < CS_ARCH_MAX && cs_arch_init[arch]) {
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400465 // verify if requested mode is valid
466 if (mode & cs_arch_disallowed_mode_mask[arch]) {
467 *handle = 0;
468 return CS_ERR_MODE;
469 }
470
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800471 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800472 if (!ud) {
473 // memory insufficient
474 return CS_ERR_MEM;
475 }
476
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800477 ud->errnum = CS_ERR_OK;
478 ud->arch = arch;
479 ud->mode = mode;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800480 // by default, do not break instruction into details
481 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800483 // default skipdata setup
484 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
485
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200486 err = cs_arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800487 if (err) {
488 cs_mem_free(ud);
489 *handle = 0;
490 return err;
491 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800492
493 *handle = (uintptr_t)ud;
494
495 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800496 } else {
497 *handle = 0;
498 return CS_ERR_ARCH;
499 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800500}
501
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800502CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700503cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800504{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800505 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800506 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800507
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800508 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800509 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800510 return CS_ERR_CSH;
511
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100512 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800514 if (ud->printer_info)
515 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800516
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800517 // free the linked list of customized mnemonic
518 tmp = ud->mnem_list;
519 while(tmp) {
520 next = tmp->next;
521 cs_mem_free(tmp);
522 tmp = next;
523 }
524
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800525 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800526
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800527 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800528 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800529
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800530 // invalidate this handle by ZERO out its value.
531 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800532 *handle = 0;
533
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800534 return CS_ERR_OK;
535}
536
537// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800538static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800539 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800540{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800541#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700542 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800543#endif
tandasat45e5eab2016-05-11 21:48:32 -0700544 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800545
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800546 // fill the instruction bytes.
547 // we might skip some redundant bytes in front in the case of X86
548 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
549 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800550
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800551 // alias instruction might have ID saved in OpcodePub
552 if (MCInst_getOpcodePub(mci))
553 insn->id = MCInst_getOpcodePub(mci);
554
555 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800556 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800557 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800558
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800559#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800560 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800561 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800562 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800563 for (sp = buffer; *sp; sp++) {
564 if (*sp == ' '|| *sp == '\t')
565 break;
566 if (*sp == '|') // lock|rep prefix for x86
567 *sp = ' ';
568 // copy to @mnemonic
569 *mnem = *sp;
570 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800571 }
572
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800573 *mnem = '\0';
574
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800575 // we might have customized mnemonic
576 if (handle->mnem_list) {
577 struct insn_mnem *tmp = handle->mnem_list;
578 while(tmp) {
579 if (tmp->insn.id == insn->id) {
580 // found this instruction, so copy its mnemonic
581 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
582 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
583 break;
584 }
585 tmp = tmp->next;
586 }
587 }
588
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800589 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800590 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800591 // find the next non-space char
592 sp++;
593 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
594 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800595 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
596 } else
597 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800598#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800599}
600
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800601// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800602// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800603static uint8_t skipdata_size(cs_struct *handle)
604{
605 switch(handle->arch) {
606 default:
607 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700608 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800609 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800610 // skip 2 bytes on Thumb mode.
611 if (handle->mode & CS_MODE_THUMB)
612 return 2;
613 // otherwise, skip 4 bytes
614 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800615 case CS_ARCH_ARM64:
616 case CS_ARCH_MIPS:
617 case CS_ARCH_PPC:
618 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800619 // skip 4 bytes
620 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800621 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800622 // SystemZ instruction's length can be 2, 4 or 6 bytes,
623 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800624 return 2;
625 case CS_ARCH_X86:
626 // X86 has no restriction on instruction alignment
627 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800628 case CS_ARCH_XCORE:
629 // XCore instruction's length can be 2 or 4 bytes,
630 // so we just skip 2 bytes
631 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200632 case CS_ARCH_M68K:
633 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
634 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300635 case CS_ARCH_TMS320C64X:
636 // TMS320C64x alignment is 4.
637 return 4;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200638 case CS_ARCH_M680X:
639 // M680X alignment is 1.
640 return 1;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800641 case CS_ARCH_EVM:
642 // EVM alignment is 1.
643 return 1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800644 }
645}
646
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800647CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700648cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800649{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800650 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800651 cs_opt_mnem *opt;
652
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800653 // cs_option() can be called with NULL handle just for CS_OPT_MEM
654 // This is supposed to be executed before all other APIs (even cs_open())
655 if (type == CS_OPT_MEM) {
656 cs_opt_mem *mem = (cs_opt_mem *)value;
657
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800658 cs_mem_malloc = mem->malloc;
659 cs_mem_calloc = mem->calloc;
660 cs_mem_realloc = mem->realloc;
661 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800662 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800663
664 return CS_ERR_OK;
665 }
666
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100667 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600668 if (!handle)
669 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800670
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800671 switch(type) {
672 default:
673 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800674
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800675 case CS_OPT_UNSIGNED:
676 handle->imm_unsigned = (cs_opt_value)value;
677 return CS_ERR_OK;
678
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800679 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500680 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800681 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800682
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800683 case CS_OPT_SKIPDATA:
684 handle->skipdata = (value == CS_OPT_ON);
685 if (handle->skipdata) {
686 if (handle->skipdata_size == 0) {
687 // set the default skipdata size
688 handle->skipdata_size = skipdata_size(handle);
689 }
690 }
691 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800692
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800693 case CS_OPT_SKIPDATA_SETUP:
694 if (value)
695 handle->skipdata_setup = *((cs_opt_skipdata *)value);
696 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800697
698 case CS_OPT_MNEMONIC:
699 opt = (cs_opt_mnem *)value;
700 if (opt->id) {
701 if (opt->mnemonic) {
702 struct insn_mnem *tmp;
703
704 // add new instruction, or replace existing instruction
705 // 1. find if we already had this insn in the linked list
706 tmp = handle->mnem_list;
707 while(tmp) {
708 if (tmp->insn.id == opt->id) {
709 // found this instruction, so replace its mnemonic
710 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
711 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
712 break;
713 }
714 tmp = tmp->next;
715 }
716
717 // 2. add this instruction if we have not had it yet
718 if (!tmp) {
719 tmp = cs_mem_malloc(sizeof(*tmp));
720 tmp->insn.id = opt->id;
721 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
722 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
723 // this new instruction is heading the list
724 tmp->next = handle->mnem_list;
725 handle->mnem_list = tmp;
726 }
727 return CS_ERR_OK;
728 } else {
729 struct insn_mnem *prev, *tmp;
730
731 // we want to delete an existing instruction
732 // iterate the list to find the instruction to remove it
733 tmp = handle->mnem_list;
734 prev = tmp;
735 while(tmp) {
736 if (tmp->insn.id == opt->id) {
737 // delete this instruction
738 if (tmp == prev) {
739 // head of the list
740 handle->mnem_list = tmp->next;
741 } else {
742 prev->next = tmp->next;
743 }
744 cs_mem_free(tmp);
745 break;
746 }
747 prev = tmp;
748 tmp = tmp->next;
749 }
750 }
751 }
752 return CS_ERR_OK;
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400753
754 case CS_OPT_MODE:
755 // verify if requested mode is valid
756 if (value & cs_arch_disallowed_mode_mask[handle->arch]) {
757 return CS_ERR_OPTION;
758 }
759 break;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800760 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800761
Alberto Garcia Illera5c302122018-03-29 16:17:37 +0200762 return cs_arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800763}
764
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800765// generate @op_str for data instruction of SKIPDATA
vit9696a8c7e4a2017-05-25 11:01:48 +0300766#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800767static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
768{
769 char *p = opstr;
770 int len;
771 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700772 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800773
774 if (!size) {
775 opstr[0] = '\0';
776 return;
777 }
778
tandasat45e5eab2016-05-11 21:48:32 -0700779 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800780 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700781 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800782
783 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700784 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
785 if (len < 0) {
786 break;
787 }
788 if ((size_t)len > available - 1) {
789 break;
790 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800791 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700792 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800793 }
794}
vit9696a8c7e4a2017-05-25 11:01:48 +0300795#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800796
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800797// dynamicly allocate memory to contain disasm insn
798// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800799CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700800size_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 +0800801{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800802 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800803 MCInst mci;
804 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800805 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800806 unsigned int f = 0; // index of the next instruction in the cache
807 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800808 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800809 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800810 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800811 void *tmp;
812 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800813 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800814 size_t size_org;
815 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800816 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500817 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800818
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800819 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800820 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800821 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800822 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800823 return 0;
824 }
825
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800826 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800827
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800828 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800829 if (handle->arch == CS_ARCH_ARM)
830 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800831
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800832#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800833 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400834 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800835#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800836
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800837 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800838 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800839 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800840 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800841
842 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700843 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800844 if (total == NULL) {
845 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500846 handle->errnum = CS_ERR_MEM;
847 return 0;
848 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800849
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700850 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800851
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800852 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800853 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800854 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800855
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700856 // relative branches need to know the address & size of current insn
857 mci.address = offset;
858
859 if (handle->detail) {
860 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700861 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700862 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700863 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700864 }
865
866 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700867 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700868 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800869#ifdef CAPSTONE_DIET
870 // zero out mnemonic & op_str
871 mci.flat_insn->mnemonic[0] = '\0';
872 mci.flat_insn->op_str[0] = '\0';
873#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700874
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800875 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800876 if (r) {
877 SStream ss;
878 SStream_Init(&ss);
879
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700880 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800881
882 // map internal instruction opcode to public insn ID
883 handle->insn_id(handle, insn_cache, mci.Opcode);
884
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800885 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700886 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800887
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800888 // adjust for pseudo opcode (X86)
889 if (handle->arch == CS_ARCH_X86)
890 insn_cache->id += mci.popcode_adjust;
891
danghvu0d1aad12014-10-01 23:12:18 -0500892 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800893 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800894 // encounter a broken instruction
895
896 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800897 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800898 cs_mem_free(insn_cache->detail);
899 }
900
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800901 // if there is no request to skip data, or remaining data is too small,
902 // then bail out
903 if (!handle->skipdata || handle->skipdata_size > size)
904 break;
905
906 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800907 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800908 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800909 if (skipdata_bytes > size)
910 // remaining data is not enough
911 break;
912
913 if (!skipdata_bytes)
914 // user requested not to skip data, so bail out
915 break;
916 } else
917 skipdata_bytes = handle->skipdata_size;
918
919 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700920 insn_cache->id = 0; // invalid ID for this "data" instruction
921 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800922 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700923 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300924#ifdef CAPSTONE_DIET
925 insn_cache->mnemonic[0] = '\0';
926 insn_cache->op_str[0] = '\0';
927#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700928 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
929 sizeof(insn_cache->mnemonic) - 1);
930 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300931#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700932 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800933
danghvu0d1aad12014-10-01 23:12:18 -0500934 next_offset = skipdata_bytes;
935 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800936
danghvu0d1aad12014-10-01 23:12:18 -0500937 // one more instruction entering the cache
938 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800939
danghvu0d1aad12014-10-01 23:12:18 -0500940 // one more instruction disassembled
941 c++;
942 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800943 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500944 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800945
danghvu0d1aad12014-10-01 23:12:18 -0500946 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800947 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500948 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500949 total_size += (sizeof(cs_insn) * cache_size);
950 tmp = cs_mem_realloc(total, total_size);
951 if (tmp == NULL) { // insufficient memory
952 if (handle->detail) {
953 insn_cache = (cs_insn *)total;
954 for (i = 0; i < c; i++, insn_cache++)
955 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800956 }
957
danghvu0d1aad12014-10-01 23:12:18 -0500958 cs_mem_free(total);
959 *insn = NULL;
960 handle->errnum = CS_ERR_MEM;
961 return 0;
962 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800963
danghvu0d1aad12014-10-01 23:12:18 -0500964 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800965 // continue to fill in the cache after the last instruction
966 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800967
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800968 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500969 f = 0;
970 } else
971 insn_cache++;
972
973 buffer += next_offset;
974 size -= next_offset;
975 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800976 }
977
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800978 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800979 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800980 cs_mem_free(total);
981 total = NULL;
982 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800983 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400984 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800985 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800986 // free all detail pointers
987 if (handle->detail) {
988 insn_cache = (cs_insn *)total;
989 for (i = 0; i < c; i++, insn_cache++)
990 cs_mem_free(insn_cache->detail);
991 }
992
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800993 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800994 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800995
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800996 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800997 return 0;
998 }
999
1000 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001001 }
1002
1003 *insn = total;
1004
1005 return c;
1006}
1007
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001008CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +08001009CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -07001010size_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 +08001011{
1012 return cs_disasm(ud, buffer, size, offset, count, insn);
1013}
1014
1015CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001016void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001017{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001018 size_t i;
1019
1020 // free all detail pointers
1021 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +08001022 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001023
1024 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +08001025 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001026}
1027
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001028CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001029cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001030{
1031 cs_insn *insn;
1032 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1033
1034 insn = cs_mem_malloc(sizeof(cs_insn));
1035 if (!insn) {
1036 // insufficient memory
1037 handle->errnum = CS_ERR_MEM;
1038 return NULL;
1039 } else {
1040 if (handle->detail) {
1041 // allocate memory for @detail pointer
1042 insn->detail = cs_mem_malloc(sizeof(cs_detail));
1043 if (insn->detail == NULL) { // insufficient memory
1044 cs_mem_free(insn);
1045 handle->errnum = CS_ERR_MEM;
1046 return NULL;
1047 }
1048 } else
1049 insn->detail = NULL;
1050 }
1051
1052 return insn;
1053}
1054
hlide993f3622014-10-05 18:14:40 +02001055// iterator for instruction "single-stepping"
1056CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001057bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001058 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +02001059{
1060 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +02001061 uint16_t insn_size;
1062 MCInst mci;
1063 bool r;
1064
1065 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001066 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +08001067 return false;
hlide993f3622014-10-05 18:14:40 +02001068 }
1069
1070 handle->errnum = CS_ERR_OK;
1071
hlide993f3622014-10-05 18:14:40 +02001072 MCInst_Init(&mci);
1073 mci.csh = handle;
1074
1075 // relative branches need to know the address & size of current insn
1076 mci.address = *address;
1077
1078 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001079 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +02001080 mci.flat_insn->address = *address;
1081#ifdef CAPSTONE_DIET
1082 // zero out mnemonic & op_str
1083 mci.flat_insn->mnemonic[0] = '\0';
1084 mci.flat_insn->op_str[0] = '\0';
1085#endif
1086
1087 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001088 if (r) {
hlide993f3622014-10-05 18:14:40 +02001089 SStream ss;
1090 SStream_Init(&ss);
1091
1092 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +08001093
1094 // map internal instruction opcode to public insn ID
1095 handle->insn_id(handle, insn, mci.Opcode);
1096
hlide993f3622014-10-05 18:14:40 +02001097 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +08001098
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001099 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +08001100
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +08001101 // adjust for pseudo opcode (X86)
1102 if (handle->arch == CS_ARCH_X86)
1103 insn->id += mci.popcode_adjust;
1104
hlide993f3622014-10-05 18:14:40 +02001105 *code += insn_size;
1106 *size -= insn_size;
1107 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001108 } else { // encounter a broken instruction
1109 size_t skipdata_bytes;
1110
1111 // if there is no request to skip data, or remaining data is too small,
1112 // then bail out
1113 if (!handle->skipdata || handle->skipdata_size > *size)
1114 return false;
1115
1116 if (handle->skipdata_setup.callback) {
1117 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
1118 0, handle->skipdata_setup.user_data);
1119 if (skipdata_bytes > *size)
1120 // remaining data is not enough
1121 return false;
1122
1123 if (!skipdata_bytes)
1124 // user requested not to skip data, so bail out
1125 return false;
1126 } else
1127 skipdata_bytes = handle->skipdata_size;
1128
1129 // we have to skip some amount of data, depending on arch & mode
1130 insn->id = 0; // invalid ID for this "data" instruction
1131 insn->address = *address;
1132 insn->size = (uint16_t)skipdata_bytes;
vit9696a8c7e4a2017-05-25 11:01:48 +03001133#ifdef CAPSTONE_DIET
1134 insn->mnemonic[0] = '\0';
1135 insn->op_str[0] = '\0';
1136#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001137 memcpy(insn->bytes, *code, skipdata_bytes);
1138 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1139 sizeof(insn->mnemonic) - 1);
1140 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +03001141#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001142
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001143 *code += skipdata_bytes;
1144 *size -= skipdata_bytes;
1145 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +02001146 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001147
1148 return true;
hlide993f3622014-10-05 18:14:40 +02001149}
1150
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001151// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001152CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001153const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001154{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +08001155 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001156
1157 if (!handle || handle->reg_name == NULL) {
1158 return NULL;
1159 }
1160
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +08001161 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001162}
1163
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001164CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001165const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001166{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +08001167 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001168
1169 if (!handle || handle->insn_name == NULL) {
1170 return NULL;
1171 }
1172
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +08001173 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001174}
1175
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +08001176CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001177const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +08001178{
1179 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1180
1181 if (!handle || handle->group_name == NULL) {
1182 return NULL;
1183 }
1184
1185 return handle->group_name(ud, group);
1186}
1187
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001188CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001189bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001190{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001191 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001192 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001193 return false;
1194
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001195 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001196
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001197 if (!handle->detail) {
1198 handle->errnum = CS_ERR_DETAIL;
1199 return false;
1200 }
1201
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001202 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001203 handle->errnum = CS_ERR_SKIPDATA;
1204 return false;
1205 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001206
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001207 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001208 handle->errnum = CS_ERR_DETAIL;
1209 return false;
1210 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001211
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001212 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001213}
1214
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001215CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001216bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001217{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001218 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001219 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001220 return false;
1221
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001222 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001223
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001224 if (!handle->detail) {
1225 handle->errnum = CS_ERR_DETAIL;
1226 return false;
1227 }
1228
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001229 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001230 handle->errnum = CS_ERR_SKIPDATA;
1231 return false;
1232 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001233
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001234 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001235 handle->errnum = CS_ERR_DETAIL;
1236 return false;
1237 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001238
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001239 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001240}
1241
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001242CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001243bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001244{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001245 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001246 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001247 return false;
1248
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001249 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001250
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001251 if (!handle->detail) {
1252 handle->errnum = CS_ERR_DETAIL;
1253 return false;
1254 }
1255
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001256 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001257 handle->errnum = CS_ERR_SKIPDATA;
1258 return false;
1259 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001260
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001261 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001262 handle->errnum = CS_ERR_DETAIL;
1263 return false;
1264 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001265
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001266 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001267}
1268
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001269CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001270int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001271{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001272 struct cs_struct *handle;
1273 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001274 if (!ud)
1275 return -1;
1276
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001277 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001278
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001279 if (!handle->detail) {
1280 handle->errnum = CS_ERR_DETAIL;
1281 return -1;
1282 }
1283
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001284 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001285 handle->errnum = CS_ERR_SKIPDATA;
1286 return -1;
1287 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001288
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001289 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001290 handle->errnum = CS_ERR_DETAIL;
1291 return -1;
1292 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001293
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001294 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001295
1296 switch (handle->arch) {
1297 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001298 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001299 return -1;
1300 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001301 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001302 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001303 count++;
1304 break;
1305 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001306 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001307 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001308 count++;
1309 break;
1310 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001311 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001312 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001313 count++;
1314 break;
1315 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001316 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001317 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001318 count++;
1319 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001320 case CS_ARCH_PPC:
1321 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001322 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001323 count++;
1324 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001325 case CS_ARCH_SPARC:
1326 for (i = 0; i < insn->detail->sparc.op_count; i++)
1327 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1328 count++;
1329 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001330 case CS_ARCH_SYSZ:
1331 for (i = 0; i < insn->detail->sysz.op_count; i++)
1332 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1333 count++;
1334 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001335 case CS_ARCH_XCORE:
1336 for (i = 0; i < insn->detail->xcore.op_count; i++)
1337 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1338 count++;
1339 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001340 case CS_ARCH_M68K:
1341 for (i = 0; i < insn->detail->m68k.op_count; i++)
1342 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1343 count++;
1344 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001345 case CS_ARCH_TMS320C64X:
1346 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1347 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1348 count++;
1349 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001350 case CS_ARCH_M680X:
1351 for (i = 0; i < insn->detail->m680x.op_count; i++)
1352 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1353 count++;
1354 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001355 case CS_ARCH_EVM:
1356#if 0
1357 for (i = 0; i < insn->detail->evm.op_count; i++)
1358 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1359 count++;
1360#endif
1361 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001362 }
1363
1364 return count;
1365}
1366
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001367CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001368int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001369 unsigned int post)
1370{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001371 struct cs_struct *handle;
1372 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001373 if (!ud)
1374 return -1;
1375
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001376 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001377
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001378 if (!handle->detail) {
1379 handle->errnum = CS_ERR_DETAIL;
1380 return -1;
1381 }
1382
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001383 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001384 handle->errnum = CS_ERR_SKIPDATA;
1385 return -1;
1386 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001387
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001388 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001389 handle->errnum = CS_ERR_DETAIL;
1390 return -1;
1391 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001392
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001393 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001394
1395 switch (handle->arch) {
1396 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001397 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001398 return -1;
1399 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001400 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001401 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001402 count++;
1403 if (count == post)
1404 return i;
1405 }
1406 break;
1407 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001408 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001409 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001410 count++;
1411 if (count == post)
1412 return i;
1413 }
1414 break;
1415 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001416 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001417 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001418 count++;
1419 if (count == post)
1420 return i;
1421 }
1422 break;
1423 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001424 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001425 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001426 count++;
1427 if (count == post)
1428 return i;
1429 }
1430 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001431 case CS_ARCH_PPC:
1432 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001433 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001434 count++;
1435 if (count == post)
1436 return i;
1437 }
1438 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001439 case CS_ARCH_SPARC:
1440 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1441 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1442 count++;
1443 if (count == post)
1444 return i;
1445 }
1446 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001447 case CS_ARCH_SYSZ:
1448 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1449 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1450 count++;
1451 if (count == post)
1452 return i;
1453 }
1454 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001455 case CS_ARCH_XCORE:
1456 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1457 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1458 count++;
1459 if (count == post)
1460 return i;
1461 }
1462 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001463 case CS_ARCH_M68K:
1464 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1465 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1466 count++;
1467 if (count == post)
1468 return i;
1469 }
1470 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001471 case CS_ARCH_TMS320C64X:
1472 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1473 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1474 count++;
1475 if (count == post)
1476 return i;
1477 }
1478 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001479 case CS_ARCH_M680X:
1480 for (i = 0; i < insn->detail->m680x.op_count; i++) {
1481 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1482 count++;
1483 if (count == post)
1484 return i;
1485 }
1486 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001487 case CS_ARCH_EVM:
1488#if 0
1489 for (i = 0; i < insn->detail->evm.op_count; i++) {
1490 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1491 count++;
1492 if (count == post)
1493 return i;
1494 }
1495#endif
1496 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001497 }
1498
1499 return -1;
1500}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001501
1502CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001503cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001504 cs_regs regs_read, uint8_t *regs_read_count,
1505 cs_regs regs_write, uint8_t *regs_write_count)
1506{
1507 struct cs_struct *handle;
1508
1509 if (!ud)
1510 return -1;
1511
1512 handle = (struct cs_struct *)(uintptr_t)ud;
1513
1514#ifdef CAPSTONE_DIET
1515 // This API does not work in DIET mode
1516 handle->errnum = CS_ERR_DIET;
1517 return CS_ERR_DIET;
1518#else
1519 if (!handle->detail) {
1520 handle->errnum = CS_ERR_DETAIL;
1521 return CS_ERR_DETAIL;
1522 }
1523
1524 if (!insn->id) {
1525 handle->errnum = CS_ERR_SKIPDATA;
1526 return CS_ERR_SKIPDATA;
1527 }
1528
1529 if (!insn->detail) {
1530 handle->errnum = CS_ERR_DETAIL;
1531 return CS_ERR_DETAIL;
1532 }
1533
1534 if (handle->reg_access) {
1535 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1536 } else {
1537 // this arch is unsupported yet
1538 handle->errnum = CS_ERR_ARCH;
1539 return CS_ERR_ARCH;
1540 }
1541
1542 return CS_ERR_OK;
1543#endif
1544}