blob: 1d1634dd96e0dd41ab59bdb2b73abdfa2db9d318 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4#pragma warning(disable:4996)
5#endif
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08006#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <capstone.h>
11
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080013#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010015#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080016#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080017#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080018// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080019#define INSN_CACHE_SIZE 8
20#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080021
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080022// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080023#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080024
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080025cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080026cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
27void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080028
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080029extern void ARM_enable(void);
30extern void AArch64_enable(void);
31extern void Mips_enable(void);
32extern void X86_enable(void);
33extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080034extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080035extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080036extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070037
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080038static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080039{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080040 static bool initialized = false;
41
42 if (initialized)
43 return;
44
danghvub33bd2c2014-01-09 12:22:56 +070045#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070047#endif
48#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080049 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070050#endif
51#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070053#endif
danghvub33bd2c2014-01-09 12:22:56 +070054#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080055 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070056#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080057#ifdef CAPSTONE_HAS_SPARC
58 Sparc_enable();
59#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080060#ifdef CAPSTONE_HAS_SYSZ
61 SystemZ_enable();
62#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080063#ifdef CAPSTONE_HAS_X86
64 X86_enable();
65#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080066#ifdef CAPSTONE_HAS_XCORE
67 XCore_enable();
68#endif
69
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080070
71 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070072}
73
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080074unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080075
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010076#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080077cs_malloc_t cs_mem_malloc = malloc;
78cs_calloc_t cs_mem_calloc = calloc;
79cs_realloc_t cs_mem_realloc = realloc;
80cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080081cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080082#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080083cs_malloc_t cs_mem_malloc = NULL;
84cs_calloc_t cs_mem_calloc = NULL;
85cs_realloc_t cs_mem_realloc = NULL;
86cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080087cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080088#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080089
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +080090CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080091unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080092{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080093 archs_enable();
94
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080095 if (major != NULL && minor != NULL) {
96 *major = CS_API_MAJOR;
97 *minor = CS_API_MINOR;
98 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080099
100 return (CS_API_MAJOR << 8) + CS_API_MINOR;
101}
102
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800103CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800104bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800105{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800106 archs_enable();
107
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800108 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800109 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800110 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800111 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800112 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800113
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800114 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800115 return all_arch & (1 << query);
116
117 if (query == CS_SUPPORT_DIET) {
118#ifdef CAPSTONE_DIET
119 return true;
120#else
121 return false;
122#endif
123 }
124
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800125 if (query == CS_SUPPORT_X86_REDUCE) {
126#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800127 return true;
128#else
129 return false;
130#endif
131 }
132
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800133 // unsupported query
134 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800135}
136
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800137CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800138cs_err cs_errno(csh handle)
139{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800140 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800141 if (!handle)
142 return CS_ERR_CSH;
143
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100144 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800145
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800146 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147}
148
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800149CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800150const char *cs_strerror(cs_err code)
151{
152 switch(code) {
153 default:
154 return "Unknown error code";
155 case CS_ERR_OK:
156 return "OK (CS_ERR_OK)";
157 case CS_ERR_MEM:
158 return "Out of memory (CS_ERR_MEM)";
159 case CS_ERR_ARCH:
160 return "Invalid architecture (CS_ERR_ARCH)";
161 case CS_ERR_HANDLE:
162 return "Invalid handle (CS_ERR_HANDLE)";
163 case CS_ERR_CSH:
164 return "Invalid csh (CS_ERR_CSH)";
165 case CS_ERR_MODE:
166 return "Invalid mode (CS_ERR_MODE)";
167 case CS_ERR_OPTION:
168 return "Invalid option (CS_ERR_OPTION)";
169 case CS_ERR_DETAIL:
170 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800171 case CS_ERR_MEMSETUP:
172 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800173 case CS_ERR_VERSION:
174 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800175 case CS_ERR_DIET:
176 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800177 case CS_ERR_SKIPDATA:
178 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800179 }
180}
181
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800182CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800183cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
184{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800185 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800186 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800187 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800188 // Error: before cs_open(), dynamic memory management must be initialized
189 // with cs_option(CS_OPT_MEM)
190 return CS_ERR_MEMSETUP;
191
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800192 archs_enable();
193
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800194 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800195 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800196 if (!ud) {
197 // memory insufficient
198 return CS_ERR_MEM;
199 }
200
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800201 ud->errnum = CS_ERR_OK;
202 ud->arch = arch;
203 ud->mode = mode;
204 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800205 // by default, do not break instruction into details
206 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800207
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800208 // default skipdata setup
209 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
210
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100211 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800212 if (err) {
213 cs_mem_free(ud);
214 *handle = 0;
215 return err;
216 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800217
218 *handle = (uintptr_t)ud;
219
220 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800221 } else {
222 *handle = 0;
223 return CS_ERR_ARCH;
224 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225}
226
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800227CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800228cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800230 struct cs_struct *ud;
231
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800232 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800233 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234 return CS_ERR_CSH;
235
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100236 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800237
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800238 if (ud->printer_info)
239 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800241 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800242
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800243 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800244 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800245
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800246 // invalidate this handle by ZERO out its value.
247 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800248 *handle = 0;
249
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800250 return CS_ERR_OK;
251}
252
253// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800254static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800255 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800256{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800257#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700258 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800259#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800260 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800262 // fill the instruction bytes.
263 // we might skip some redundant bytes in front in the case of X86
264 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
265 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800266
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800267 // alias instruction might have ID saved in OpcodePub
268 if (MCInst_getOpcodePub(mci))
269 insn->id = MCInst_getOpcodePub(mci);
270
271 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800272 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800273 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800274
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800275#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800277 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100278 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800279 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800280 for (sp = buffer; *sp; sp++) {
281 if (*sp == ' '|| *sp == '\t')
282 break;
283 if (*sp == '|') // lock|rep prefix for x86
284 *sp = ' ';
285 // copy to @mnemonic
286 *mnem = *sp;
287 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800288 }
289
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800290 *mnem = '\0';
291
292 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800293 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800294 // find the next non-space char
295 sp++;
296 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
297 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800298 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
299 } else
300 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800301#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302}
303
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800304// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800305// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800306static uint8_t skipdata_size(cs_struct *handle)
307{
308 switch(handle->arch) {
309 default:
310 // should never reach
311 return -1;
312 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800313 // skip 2 bytes on Thumb mode.
314 if (handle->mode & CS_MODE_THUMB)
315 return 2;
316 // otherwise, skip 4 bytes
317 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800318 case CS_ARCH_ARM64:
319 case CS_ARCH_MIPS:
320 case CS_ARCH_PPC:
321 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800322 // skip 4 bytes
323 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800324 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800325 // SystemZ instruction's length can be 2, 4 or 6 bytes,
326 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800327 return 2;
328 case CS_ARCH_X86:
329 // X86 has no restriction on instruction alignment
330 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800331 case CS_ARCH_XCORE:
332 // XCore instruction's length can be 2 or 4 bytes,
333 // so we just skip 2 bytes
334 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800335 }
336}
337
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800338CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800339cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800340{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800341 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800342 archs_enable();
343
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800344 // cs_option() can be called with NULL handle just for CS_OPT_MEM
345 // This is supposed to be executed before all other APIs (even cs_open())
346 if (type == CS_OPT_MEM) {
347 cs_opt_mem *mem = (cs_opt_mem *)value;
348
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800349 cs_mem_malloc = mem->malloc;
350 cs_mem_calloc = mem->calloc;
351 cs_mem_realloc = mem->realloc;
352 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800353 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800354
355 return CS_ERR_OK;
356 }
357
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100358 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600359 if (!handle)
360 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800361
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800362 switch(type) {
363 default:
364 break;
365 case CS_OPT_DETAIL:
366 handle->detail = value;
367 return CS_ERR_OK;
368 case CS_OPT_SKIPDATA:
369 handle->skipdata = (value == CS_OPT_ON);
370 if (handle->skipdata) {
371 if (handle->skipdata_size == 0) {
372 // set the default skipdata size
373 handle->skipdata_size = skipdata_size(handle);
374 }
375 }
376 return CS_ERR_OK;
377 case CS_OPT_SKIPDATA_SETUP:
378 if (value)
379 handle->skipdata_setup = *((cs_opt_skipdata *)value);
380 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800381 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800382
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800383 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800384}
385
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800386// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800387static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
388{
389 char *p = opstr;
390 int len;
391 size_t i;
392
393 if (!size) {
394 opstr[0] = '\0';
395 return;
396 }
397
398 len = sprintf(p, "0x%02x", buffer[0]);
399 p+= len;
400
401 for(i = 1; i < size; i++) {
402 len = sprintf(p, ", 0x%02x", buffer[i]);
403 p+= len;
404 }
405}
406
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407// dynamicly allocate memory to contain disasm insn
408// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800409CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800410size_t 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 +0800411{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800412 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413 MCInst mci;
414 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800415 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800416 unsigned int f = 0; // index of the next instruction in the cache
417 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800419 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800420 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800421 void *tmp;
422 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800423 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800424 size_t size_org;
425 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800426 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500427 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800428
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800429 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800430 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800432 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433 return 0;
434 }
435
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800436 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800437
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800438#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800439 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400440 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800441#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800442
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800443 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800444 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800445 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800446 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800447
448 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700449 total = cs_mem_malloc(total_size);
Edward Williamsonf1e49752014-12-14 20:45:19 -0500450 if (total == NULL)
451 {
452 //malloc failed
453 handle->errnum = CS_ERR_MEM;
454 return 0;
455 }
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700456 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800457
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800458 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800459 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800460 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800461
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700462 // relative branches need to know the address & size of current insn
463 mci.address = offset;
464
465 if (handle->detail) {
466 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700467 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700468 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700469 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700470 }
471
472 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700473 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700474 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800475#ifdef CAPSTONE_DIET
476 // zero out mnemonic & op_str
477 mci.flat_insn->mnemonic[0] = '\0';
478 mci.flat_insn->op_str[0] = '\0';
479#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700480
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800481 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482 if (r) {
483 SStream ss;
484 SStream_Init(&ss);
485
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700486 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800487
488 // map internal instruction opcode to public insn ID
489 handle->insn_id(handle, insn_cache, mci.Opcode);
490
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800491 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800492
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700493 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800494
danghvu0d1aad12014-10-01 23:12:18 -0500495 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800496 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800497 // encounter a broken instruction
498
499 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800500 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800501 cs_mem_free(insn_cache->detail);
502 }
503
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800504 // if there is no request to skip data, or remaining data is too small,
505 // then bail out
506 if (!handle->skipdata || handle->skipdata_size > size)
507 break;
508
509 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800510 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800511 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800512 if (skipdata_bytes > size)
513 // remaining data is not enough
514 break;
515
516 if (!skipdata_bytes)
517 // user requested not to skip data, so bail out
518 break;
519 } else
520 skipdata_bytes = handle->skipdata_size;
521
522 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700523 insn_cache->id = 0; // invalid ID for this "data" instruction
524 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800525 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700526 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
527 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
528 sizeof(insn_cache->mnemonic) - 1);
529 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
530 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800531
danghvu0d1aad12014-10-01 23:12:18 -0500532 next_offset = skipdata_bytes;
533 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800534
danghvu0d1aad12014-10-01 23:12:18 -0500535 // one more instruction entering the cache
536 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800537
danghvu0d1aad12014-10-01 23:12:18 -0500538 // one more instruction disassembled
539 c++;
540 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800541 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500542 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800543
danghvu0d1aad12014-10-01 23:12:18 -0500544 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800545 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500546 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500547 total_size += (sizeof(cs_insn) * cache_size);
548 tmp = cs_mem_realloc(total, total_size);
549 if (tmp == NULL) { // insufficient memory
550 if (handle->detail) {
551 insn_cache = (cs_insn *)total;
552 for (i = 0; i < c; i++, insn_cache++)
553 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800554 }
555
danghvu0d1aad12014-10-01 23:12:18 -0500556 cs_mem_free(total);
557 *insn = NULL;
558 handle->errnum = CS_ERR_MEM;
559 return 0;
560 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800561
danghvu0d1aad12014-10-01 23:12:18 -0500562 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800563 // continue to fill in the cache after the last instruction
564 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800565
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800566 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500567 f = 0;
568 } else
569 insn_cache++;
570
571 buffer += next_offset;
572 size -= next_offset;
573 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800574 }
575
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800576 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800577 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800578 cs_mem_free(total);
579 total = NULL;
580 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800581 // total did not fully use the last cache, so downsize it
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800582 void *tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800583 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800584 // free all detail pointers
585 if (handle->detail) {
586 insn_cache = (cs_insn *)total;
587 for (i = 0; i < c; i++, insn_cache++)
588 cs_mem_free(insn_cache->detail);
589 }
590
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800591 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800592 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800593
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800594 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800595 return 0;
596 }
597
598 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800599 }
600
601 *insn = total;
602
603 return c;
604}
605
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800606CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800607CAPSTONE_DEPRECATED
608size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
609{
610 return cs_disasm(ud, buffer, size, offset, count, insn);
611}
612
613CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800614void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800615{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800616 size_t i;
617
618 // free all detail pointers
619 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800620 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800621
622 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800623 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800624}
625
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800626CAPSTONE_EXPORT
627cs_insn *cs_malloc(csh ud)
628{
629 cs_insn *insn;
630 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
631
632 insn = cs_mem_malloc(sizeof(cs_insn));
633 if (!insn) {
634 // insufficient memory
635 handle->errnum = CS_ERR_MEM;
636 return NULL;
637 } else {
638 if (handle->detail) {
639 // allocate memory for @detail pointer
640 insn->detail = cs_mem_malloc(sizeof(cs_detail));
641 if (insn->detail == NULL) { // insufficient memory
642 cs_mem_free(insn);
643 handle->errnum = CS_ERR_MEM;
644 return NULL;
645 }
646 } else
647 insn->detail = NULL;
648 }
649
650 return insn;
651}
652
hlide993f3622014-10-05 18:14:40 +0200653// iterator for instruction "single-stepping"
654CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800655bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
656 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200657{
658 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200659 uint16_t insn_size;
660 MCInst mci;
661 bool r;
662
663 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800664 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800665 return false;
hlide993f3622014-10-05 18:14:40 +0200666 }
667
668 handle->errnum = CS_ERR_OK;
669
hlide993f3622014-10-05 18:14:40 +0200670 MCInst_Init(&mci);
671 mci.csh = handle;
672
673 // relative branches need to know the address & size of current insn
674 mci.address = *address;
675
676 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800677 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200678 mci.flat_insn->address = *address;
679#ifdef CAPSTONE_DIET
680 // zero out mnemonic & op_str
681 mci.flat_insn->mnemonic[0] = '\0';
682 mci.flat_insn->op_str[0] = '\0';
683#endif
684
685 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800686 if (r) {
hlide993f3622014-10-05 18:14:40 +0200687 SStream ss;
688 SStream_Init(&ss);
689
690 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800691
692 // map internal instruction opcode to public insn ID
693 handle->insn_id(handle, insn, mci.Opcode);
694
hlide993f3622014-10-05 18:14:40 +0200695 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800696
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800697 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800698
hlide993f3622014-10-05 18:14:40 +0200699 *code += insn_size;
700 *size -= insn_size;
701 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800702 } else { // encounter a broken instruction
703 size_t skipdata_bytes;
704
705 // if there is no request to skip data, or remaining data is too small,
706 // then bail out
707 if (!handle->skipdata || handle->skipdata_size > *size)
708 return false;
709
710 if (handle->skipdata_setup.callback) {
711 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
712 0, handle->skipdata_setup.user_data);
713 if (skipdata_bytes > *size)
714 // remaining data is not enough
715 return false;
716
717 if (!skipdata_bytes)
718 // user requested not to skip data, so bail out
719 return false;
720 } else
721 skipdata_bytes = handle->skipdata_size;
722
723 // we have to skip some amount of data, depending on arch & mode
724 insn->id = 0; // invalid ID for this "data" instruction
725 insn->address = *address;
726 insn->size = (uint16_t)skipdata_bytes;
727 memcpy(insn->bytes, *code, skipdata_bytes);
728 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
729 sizeof(insn->mnemonic) - 1);
730 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
731
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800732 *code += skipdata_bytes;
733 *size -= skipdata_bytes;
734 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200735 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800736
737 return true;
hlide993f3622014-10-05 18:14:40 +0200738}
739
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800740// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800741CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100742const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800743{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800744 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800745
746 if (!handle || handle->reg_name == NULL) {
747 return NULL;
748 }
749
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800750 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800751}
752
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800753CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100754const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800755{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800756 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800757
758 if (!handle || handle->insn_name == NULL) {
759 return NULL;
760 }
761
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800762 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800763}
764
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800765CAPSTONE_EXPORT
766const char *cs_group_name(csh ud, unsigned int group)
767{
768 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
769
770 if (!handle || handle->group_name == NULL) {
771 return NULL;
772 }
773
774 return handle->group_name(ud, group);
775}
776
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800777static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800778{
779 int i;
780
781 for (i = 0; i < max; i++) {
782 if (arr[i] == id)
783 return true;
784 }
785
786 return false;
787}
788
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800789CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200790bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800791{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800792 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800793 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800794 return false;
795
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100796 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200797
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800798 if (!handle->detail) {
799 handle->errnum = CS_ERR_DETAIL;
800 return false;
801 }
802
Giovanni Condello95657e02014-05-07 17:31:27 +0200803 if(!insn->id) {
804 handle->errnum = CS_ERR_SKIPDATA;
805 return false;
806 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200807
Giovanni Condello95657e02014-05-07 17:31:27 +0200808 if(!insn->detail) {
809 handle->errnum = CS_ERR_DETAIL;
810 return false;
811 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200812
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800813 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800814}
815
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800816CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200817bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800818{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800819 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800820 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800821 return false;
822
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100823 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200824
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800825 if (!handle->detail) {
826 handle->errnum = CS_ERR_DETAIL;
827 return false;
828 }
829
Giovanni Condello95657e02014-05-07 17:31:27 +0200830 if(!insn->id) {
831 handle->errnum = CS_ERR_SKIPDATA;
832 return false;
833 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200834
Giovanni Condello95657e02014-05-07 17:31:27 +0200835 if(!insn->detail) {
836 handle->errnum = CS_ERR_DETAIL;
837 return false;
838 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200839
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800840 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800841}
842
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800843CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200844bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800845{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800846 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800847 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800848 return false;
849
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100850 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200851
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800852 if (!handle->detail) {
853 handle->errnum = CS_ERR_DETAIL;
854 return false;
855 }
856
Giovanni Condello95657e02014-05-07 17:31:27 +0200857 if(!insn->id) {
858 handle->errnum = CS_ERR_SKIPDATA;
859 return false;
860 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200861
Giovanni Condello95657e02014-05-07 17:31:27 +0200862 if(!insn->detail) {
863 handle->errnum = CS_ERR_DETAIL;
864 return false;
865 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200866
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800867 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800868}
869
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800870CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200871int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800872{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800873 struct cs_struct *handle;
874 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800875 if (!ud)
876 return -1;
877
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100878 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200879
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800880 if (!handle->detail) {
881 handle->errnum = CS_ERR_DETAIL;
882 return -1;
883 }
884
Giovanni Condello95657e02014-05-07 17:31:27 +0200885 if(!insn->id) {
886 handle->errnum = CS_ERR_SKIPDATA;
887 return -1;
888 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200889
Giovanni Condello95657e02014-05-07 17:31:27 +0200890 if(!insn->detail) {
891 handle->errnum = CS_ERR_DETAIL;
892 return -1;
893 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200894
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800895 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800896
897 switch (handle->arch) {
898 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800899 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800900 return -1;
901 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800902 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800903 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800904 count++;
905 break;
906 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800907 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800908 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800909 count++;
910 break;
911 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800912 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800913 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800914 count++;
915 break;
916 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800917 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800918 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800919 count++;
920 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800921 case CS_ARCH_PPC:
922 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800923 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800924 count++;
925 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800926 case CS_ARCH_SPARC:
927 for (i = 0; i < insn->detail->sparc.op_count; i++)
928 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
929 count++;
930 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800931 case CS_ARCH_SYSZ:
932 for (i = 0; i < insn->detail->sysz.op_count; i++)
933 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
934 count++;
935 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800936 case CS_ARCH_XCORE:
937 for (i = 0; i < insn->detail->xcore.op_count; i++)
938 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
939 count++;
940 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800941 }
942
943 return count;
944}
945
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800946CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200947int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800948 unsigned int post)
949{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800950 struct cs_struct *handle;
951 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800952 if (!ud)
953 return -1;
954
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100955 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200956
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800957 if (!handle->detail) {
958 handle->errnum = CS_ERR_DETAIL;
959 return -1;
960 }
961
Giovanni Condello95657e02014-05-07 17:31:27 +0200962 if(!insn->id) {
963 handle->errnum = CS_ERR_SKIPDATA;
964 return -1;
965 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200966
Giovanni Condello95657e02014-05-07 17:31:27 +0200967 if(!insn->detail) {
968 handle->errnum = CS_ERR_DETAIL;
969 return -1;
970 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200971
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800972 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800973
974 switch (handle->arch) {
975 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800976 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800977 return -1;
978 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800979 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800980 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800981 count++;
982 if (count == post)
983 return i;
984 }
985 break;
986 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800987 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800988 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800989 count++;
990 if (count == post)
991 return i;
992 }
993 break;
994 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800995 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800996 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800997 count++;
998 if (count == post)
999 return i;
1000 }
1001 break;
1002 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001003 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001004 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001005 count++;
1006 if (count == post)
1007 return i;
1008 }
1009 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001010 case CS_ARCH_PPC:
1011 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001012 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001013 count++;
1014 if (count == post)
1015 return i;
1016 }
1017 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001018 case CS_ARCH_SPARC:
1019 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1020 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1021 count++;
1022 if (count == post)
1023 return i;
1024 }
1025 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001026 case CS_ARCH_SYSZ:
1027 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1028 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1029 count++;
1030 if (count == post)
1031 return i;
1032 }
1033 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001034 case CS_ARCH_XCORE:
1035 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1036 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1037 count++;
1038 if (count == post)
1039 return i;
1040 }
1041 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001042 }
1043
1044 return -1;
1045}