blob: 587c1b83482b3fcb95e874cdd5f0a6224e623846 [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 // map internal instruction opcode to public insn ID
Nguyen Anh Quynh07e84a22014-06-08 19:27:22 +0800268 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800269
270 // alias instruction might have ID saved in OpcodePub
271 if (MCInst_getOpcodePub(mci))
272 insn->id = MCInst_getOpcodePub(mci);
273
274 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800275 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800276 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800277
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800278#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800279 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800280 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100281 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800282 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800283 for (sp = buffer; *sp; sp++) {
284 if (*sp == ' '|| *sp == '\t')
285 break;
286 if (*sp == '|') // lock|rep prefix for x86
287 *sp = ' ';
288 // copy to @mnemonic
289 *mnem = *sp;
290 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800291 }
292
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800293 *mnem = '\0';
294
295 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800296 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800297 // find the next non-space char
298 sp++;
299 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
300 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
302 } else
303 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800304#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305}
306
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800307// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800308// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800309static uint8_t skipdata_size(cs_struct *handle)
310{
311 switch(handle->arch) {
312 default:
313 // should never reach
314 return -1;
315 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800316 // skip 2 bytes on Thumb mode.
317 if (handle->mode & CS_MODE_THUMB)
318 return 2;
319 // otherwise, skip 4 bytes
320 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800321 case CS_ARCH_ARM64:
322 case CS_ARCH_MIPS:
323 case CS_ARCH_PPC:
324 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800325 // skip 4 bytes
326 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800327 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800328 // SystemZ instruction's length can be 2, 4 or 6 bytes,
329 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800330 return 2;
331 case CS_ARCH_X86:
332 // X86 has no restriction on instruction alignment
333 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800334 case CS_ARCH_XCORE:
335 // XCore instruction's length can be 2 or 4 bytes,
336 // so we just skip 2 bytes
337 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800338 }
339}
340
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800341CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800342cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800343{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800344 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800345 archs_enable();
346
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800347 // cs_option() can be called with NULL handle just for CS_OPT_MEM
348 // This is supposed to be executed before all other APIs (even cs_open())
349 if (type == CS_OPT_MEM) {
350 cs_opt_mem *mem = (cs_opt_mem *)value;
351
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800352 cs_mem_malloc = mem->malloc;
353 cs_mem_calloc = mem->calloc;
354 cs_mem_realloc = mem->realloc;
355 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800356 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800357
358 return CS_ERR_OK;
359 }
360
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100361 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600362 if (!handle)
363 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800364
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800365 switch(type) {
366 default:
367 break;
368 case CS_OPT_DETAIL:
369 handle->detail = value;
370 return CS_ERR_OK;
371 case CS_OPT_SKIPDATA:
372 handle->skipdata = (value == CS_OPT_ON);
373 if (handle->skipdata) {
374 if (handle->skipdata_size == 0) {
375 // set the default skipdata size
376 handle->skipdata_size = skipdata_size(handle);
377 }
378 }
379 return CS_ERR_OK;
380 case CS_OPT_SKIPDATA_SETUP:
381 if (value)
382 handle->skipdata_setup = *((cs_opt_skipdata *)value);
383 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800384 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800385
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800386 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800387}
388
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800389// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800390static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
391{
392 char *p = opstr;
393 int len;
394 size_t i;
395
396 if (!size) {
397 opstr[0] = '\0';
398 return;
399 }
400
401 len = sprintf(p, "0x%02x", buffer[0]);
402 p+= len;
403
404 for(i = 1; i < size; i++) {
405 len = sprintf(p, ", 0x%02x", buffer[i]);
406 p+= len;
407 }
408}
409
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800410// dynamicly allocate memory to contain disasm insn
411// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800412CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800413size_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 +0800414{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800415 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800416 MCInst mci;
417 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800418 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800419 unsigned int f = 0; // index of the next instruction in the cache
420 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800422 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800423 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800424 void *tmp;
425 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800426 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800427 size_t size_org;
428 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800429 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500430 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800432 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800433 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800434 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800435 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 return 0;
437 }
438
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800439 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800441#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800442 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400443 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800444#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800445
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800446 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800447 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800448 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800449 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800450
451 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700452 total = cs_mem_malloc(total_size);
453 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800454
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800456 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800457 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800458
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700459 // relative branches need to know the address & size of current insn
460 mci.address = offset;
461
462 if (handle->detail) {
463 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700464 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700465 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700466 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700467 }
468
469 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700470 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700471 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800472#ifdef CAPSTONE_DIET
473 // zero out mnemonic & op_str
474 mci.flat_insn->mnemonic[0] = '\0';
475 mci.flat_insn->op_str[0] = '\0';
476#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700477
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800478 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479 if (r) {
480 SStream ss;
481 SStream_Init(&ss);
482
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700483 mci.flat_insn->size = insn_size;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800484 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700485 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800486
danghvu0d1aad12014-10-01 23:12:18 -0500487 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800488 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800489 // encounter a broken instruction
490
491 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800492 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800493 cs_mem_free(insn_cache->detail);
494 }
495
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800496 // if there is no request to skip data, or remaining data is too small,
497 // then bail out
498 if (!handle->skipdata || handle->skipdata_size > size)
499 break;
500
501 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800502 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800503 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800504 if (skipdata_bytes > size)
505 // remaining data is not enough
506 break;
507
508 if (!skipdata_bytes)
509 // user requested not to skip data, so bail out
510 break;
511 } else
512 skipdata_bytes = handle->skipdata_size;
513
514 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700515 insn_cache->id = 0; // invalid ID for this "data" instruction
516 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800517 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700518 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
519 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
520 sizeof(insn_cache->mnemonic) - 1);
521 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
522 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800523
danghvu0d1aad12014-10-01 23:12:18 -0500524 next_offset = skipdata_bytes;
525 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800526
danghvu0d1aad12014-10-01 23:12:18 -0500527 // one more instruction entering the cache
528 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800529
danghvu0d1aad12014-10-01 23:12:18 -0500530 // one more instruction disassembled
531 c++;
532 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800533 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500534 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800535
danghvu0d1aad12014-10-01 23:12:18 -0500536 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800537 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500538 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500539 total_size += (sizeof(cs_insn) * cache_size);
540 tmp = cs_mem_realloc(total, total_size);
541 if (tmp == NULL) { // insufficient memory
542 if (handle->detail) {
543 insn_cache = (cs_insn *)total;
544 for (i = 0; i < c; i++, insn_cache++)
545 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800546 }
547
danghvu0d1aad12014-10-01 23:12:18 -0500548 cs_mem_free(total);
549 *insn = NULL;
550 handle->errnum = CS_ERR_MEM;
551 return 0;
552 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800553
danghvu0d1aad12014-10-01 23:12:18 -0500554 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800555 // continue to fill in the cache after the last instruction
556 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800557
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800558 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500559 f = 0;
560 } else
561 insn_cache++;
562
563 buffer += next_offset;
564 size -= next_offset;
565 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800566 }
567
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800568 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800569 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800570 cs_mem_free(total);
571 total = NULL;
572 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800573 // total did not fully use the last cache, so downsize it
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800574 void *tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800575 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800576 // free all detail pointers
577 if (handle->detail) {
578 insn_cache = (cs_insn *)total;
579 for (i = 0; i < c; i++, insn_cache++)
580 cs_mem_free(insn_cache->detail);
581 }
582
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800583 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800584 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800585
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800586 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800587 return 0;
588 }
589
590 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800591 }
592
593 *insn = total;
594
595 return c;
596}
597
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800598CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800599CAPSTONE_DEPRECATED
600size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
601{
602 return cs_disasm(ud, buffer, size, offset, count, insn);
603}
604
605CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800606void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800607{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800608 size_t i;
609
610 // free all detail pointers
611 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800612 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800613
614 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800615 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800616}
617
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800618CAPSTONE_EXPORT
619cs_insn *cs_malloc(csh ud)
620{
621 cs_insn *insn;
622 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
623
624 insn = cs_mem_malloc(sizeof(cs_insn));
625 if (!insn) {
626 // insufficient memory
627 handle->errnum = CS_ERR_MEM;
628 return NULL;
629 } else {
630 if (handle->detail) {
631 // allocate memory for @detail pointer
632 insn->detail = cs_mem_malloc(sizeof(cs_detail));
633 if (insn->detail == NULL) { // insufficient memory
634 cs_mem_free(insn);
635 handle->errnum = CS_ERR_MEM;
636 return NULL;
637 }
638 } else
639 insn->detail = NULL;
640 }
641
642 return insn;
643}
644
hlide993f3622014-10-05 18:14:40 +0200645// iterator for instruction "single-stepping"
646CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800647bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
648 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200649{
650 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200651 uint16_t insn_size;
652 MCInst mci;
653 bool r;
654
655 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800656 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800657 return false;
hlide993f3622014-10-05 18:14:40 +0200658 }
659
660 handle->errnum = CS_ERR_OK;
661
hlide993f3622014-10-05 18:14:40 +0200662 MCInst_Init(&mci);
663 mci.csh = handle;
664
665 // relative branches need to know the address & size of current insn
666 mci.address = *address;
667
668 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800669 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200670 mci.flat_insn->address = *address;
671#ifdef CAPSTONE_DIET
672 // zero out mnemonic & op_str
673 mci.flat_insn->mnemonic[0] = '\0';
674 mci.flat_insn->op_str[0] = '\0';
675#endif
676
677 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800678 if (r) {
hlide993f3622014-10-05 18:14:40 +0200679 SStream ss;
680 SStream_Init(&ss);
681
682 mci.flat_insn->size = insn_size;
683 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800684 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
hlide993f3622014-10-05 18:14:40 +0200685 *code += insn_size;
686 *size -= insn_size;
687 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800688 } else { // encounter a broken instruction
689 size_t skipdata_bytes;
690
691 // if there is no request to skip data, or remaining data is too small,
692 // then bail out
693 if (!handle->skipdata || handle->skipdata_size > *size)
694 return false;
695
696 if (handle->skipdata_setup.callback) {
697 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
698 0, handle->skipdata_setup.user_data);
699 if (skipdata_bytes > *size)
700 // remaining data is not enough
701 return false;
702
703 if (!skipdata_bytes)
704 // user requested not to skip data, so bail out
705 return false;
706 } else
707 skipdata_bytes = handle->skipdata_size;
708
709 // we have to skip some amount of data, depending on arch & mode
710 insn->id = 0; // invalid ID for this "data" instruction
711 insn->address = *address;
712 insn->size = (uint16_t)skipdata_bytes;
713 memcpy(insn->bytes, *code, skipdata_bytes);
714 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
715 sizeof(insn->mnemonic) - 1);
716 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
717
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800718 *code += skipdata_bytes;
719 *size -= skipdata_bytes;
720 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200721 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800722
723 return true;
hlide993f3622014-10-05 18:14:40 +0200724}
725
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800726// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800727CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100728const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800729{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800730 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800731
732 if (!handle || handle->reg_name == NULL) {
733 return NULL;
734 }
735
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800736 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800737}
738
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800739CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100740const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800741{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800742 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800743
744 if (!handle || handle->insn_name == NULL) {
745 return NULL;
746 }
747
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800748 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800749}
750
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800751CAPSTONE_EXPORT
752const char *cs_group_name(csh ud, unsigned int group)
753{
754 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
755
756 if (!handle || handle->group_name == NULL) {
757 return NULL;
758 }
759
760 return handle->group_name(ud, group);
761}
762
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800763static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800764{
765 int i;
766
767 for (i = 0; i < max; i++) {
768 if (arr[i] == id)
769 return true;
770 }
771
772 return false;
773}
774
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800775CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200776bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800777{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800778 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800779 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800780 return false;
781
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100782 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200783
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800784 if (!handle->detail) {
785 handle->errnum = CS_ERR_DETAIL;
786 return false;
787 }
788
Giovanni Condello95657e02014-05-07 17:31:27 +0200789 if(!insn->id) {
790 handle->errnum = CS_ERR_SKIPDATA;
791 return false;
792 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200793
Giovanni Condello95657e02014-05-07 17:31:27 +0200794 if(!insn->detail) {
795 handle->errnum = CS_ERR_DETAIL;
796 return false;
797 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200798
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800799 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800800}
801
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800802CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200803bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800804{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800805 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800806 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800807 return false;
808
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100809 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200810
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800811 if (!handle->detail) {
812 handle->errnum = CS_ERR_DETAIL;
813 return false;
814 }
815
Giovanni Condello95657e02014-05-07 17:31:27 +0200816 if(!insn->id) {
817 handle->errnum = CS_ERR_SKIPDATA;
818 return false;
819 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200820
Giovanni Condello95657e02014-05-07 17:31:27 +0200821 if(!insn->detail) {
822 handle->errnum = CS_ERR_DETAIL;
823 return false;
824 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200825
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800826 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800827}
828
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800829CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200830bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800831{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800832 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800833 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800834 return false;
835
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100836 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200837
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800838 if (!handle->detail) {
839 handle->errnum = CS_ERR_DETAIL;
840 return false;
841 }
842
Giovanni Condello95657e02014-05-07 17:31:27 +0200843 if(!insn->id) {
844 handle->errnum = CS_ERR_SKIPDATA;
845 return false;
846 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200847
Giovanni Condello95657e02014-05-07 17:31:27 +0200848 if(!insn->detail) {
849 handle->errnum = CS_ERR_DETAIL;
850 return false;
851 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200852
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800853 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800854}
855
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800856CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200857int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800858{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800859 struct cs_struct *handle;
860 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800861 if (!ud)
862 return -1;
863
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100864 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200865
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800866 if (!handle->detail) {
867 handle->errnum = CS_ERR_DETAIL;
868 return -1;
869 }
870
Giovanni Condello95657e02014-05-07 17:31:27 +0200871 if(!insn->id) {
872 handle->errnum = CS_ERR_SKIPDATA;
873 return -1;
874 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200875
Giovanni Condello95657e02014-05-07 17:31:27 +0200876 if(!insn->detail) {
877 handle->errnum = CS_ERR_DETAIL;
878 return -1;
879 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200880
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800881 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800882
883 switch (handle->arch) {
884 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800885 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800886 return -1;
887 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800888 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800889 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800890 count++;
891 break;
892 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800893 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800894 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800895 count++;
896 break;
897 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800898 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800899 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800900 count++;
901 break;
902 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800903 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800904 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800905 count++;
906 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800907 case CS_ARCH_PPC:
908 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800909 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800910 count++;
911 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800912 case CS_ARCH_SPARC:
913 for (i = 0; i < insn->detail->sparc.op_count; i++)
914 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
915 count++;
916 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800917 case CS_ARCH_SYSZ:
918 for (i = 0; i < insn->detail->sysz.op_count; i++)
919 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
920 count++;
921 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800922 case CS_ARCH_XCORE:
923 for (i = 0; i < insn->detail->xcore.op_count; i++)
924 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
925 count++;
926 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800927 }
928
929 return count;
930}
931
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800932CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200933int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800934 unsigned int post)
935{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800936 struct cs_struct *handle;
937 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800938 if (!ud)
939 return -1;
940
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100941 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200942
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800943 if (!handle->detail) {
944 handle->errnum = CS_ERR_DETAIL;
945 return -1;
946 }
947
Giovanni Condello95657e02014-05-07 17:31:27 +0200948 if(!insn->id) {
949 handle->errnum = CS_ERR_SKIPDATA;
950 return -1;
951 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200952
Giovanni Condello95657e02014-05-07 17:31:27 +0200953 if(!insn->detail) {
954 handle->errnum = CS_ERR_DETAIL;
955 return -1;
956 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200957
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800958 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959
960 switch (handle->arch) {
961 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800962 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963 return -1;
964 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800965 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800966 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800967 count++;
968 if (count == post)
969 return i;
970 }
971 break;
972 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800973 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800974 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975 count++;
976 if (count == post)
977 return i;
978 }
979 break;
980 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800981 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800982 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800983 count++;
984 if (count == post)
985 return i;
986 }
987 break;
988 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800989 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800990 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800991 count++;
992 if (count == post)
993 return i;
994 }
995 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800996 case CS_ARCH_PPC:
997 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800998 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800999 count++;
1000 if (count == post)
1001 return i;
1002 }
1003 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001004 case CS_ARCH_SPARC:
1005 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1006 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1007 count++;
1008 if (count == post)
1009 return i;
1010 }
1011 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001012 case CS_ARCH_SYSZ:
1013 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1014 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1015 count++;
1016 if (count == post)
1017 return i;
1018 }
1019 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001020 case CS_ARCH_XCORE:
1021 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1022 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1023 count++;
1024 if (count == post)
1025 return i;
1026 }
1027 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001028 }
1029
1030 return -1;
1031}