blob: 95d992432e6f2c8f0ff0b72d5a4cd5147af5d39b [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080011#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +080013#define INSN_CACHE_SIZE 64
14
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080015cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080016cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
17void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080018
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080019extern void ARM_enable(void);
20extern void AArch64_enable(void);
21extern void Mips_enable(void);
22extern void X86_enable(void);
23extern void PPC_enable(void);
danghvu701b8502014-01-09 11:06:44 +070024
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080025static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080026{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080027 static bool initialized = false;
28
29 if (initialized)
30 return;
31
danghvub33bd2c2014-01-09 12:22:56 +070032#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080033 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070034#endif
35#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080036 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070037#endif
38#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080039 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070040#endif
41#ifdef CAPSTONE_HAS_X86
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080042 X86_enable();
danghvub33bd2c2014-01-09 12:22:56 +070043#endif
44#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070046#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080047
48 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070049}
50
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080051unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080052
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080053#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080054cs_malloc_t cs_mem_malloc = malloc;
55cs_calloc_t cs_mem_calloc = calloc;
56cs_realloc_t cs_mem_realloc = realloc;
57cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080058cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080059#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080060cs_malloc_t cs_mem_malloc = NULL;
61cs_calloc_t cs_mem_calloc = NULL;
62cs_realloc_t cs_mem_realloc = NULL;
63cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080064cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080065#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080066
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080067unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080068{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080069 if (major != NULL && minor != NULL) {
70 *major = CS_API_MAJOR;
71 *minor = CS_API_MINOR;
72 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080073
74 return (CS_API_MAJOR << 8) + CS_API_MINOR;
75}
76
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080077bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080078{
79 if (arch == CS_ARCH_ALL)
80 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080081 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
82 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083
84 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080085}
86
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087cs_err cs_errno(csh handle)
88{
89 if (!handle)
90 return CS_ERR_CSH;
91
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +080092 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080093
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080094 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080095}
96
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080097const char *cs_strerror(cs_err code)
98{
99 switch(code) {
100 default:
101 return "Unknown error code";
102 case CS_ERR_OK:
103 return "OK (CS_ERR_OK)";
104 case CS_ERR_MEM:
105 return "Out of memory (CS_ERR_MEM)";
106 case CS_ERR_ARCH:
107 return "Invalid architecture (CS_ERR_ARCH)";
108 case CS_ERR_HANDLE:
109 return "Invalid handle (CS_ERR_HANDLE)";
110 case CS_ERR_CSH:
111 return "Invalid csh (CS_ERR_CSH)";
112 case CS_ERR_MODE:
113 return "Invalid mode (CS_ERR_MODE)";
114 case CS_ERR_OPTION:
115 return "Invalid option (CS_ERR_OPTION)";
116 case CS_ERR_DETAIL:
117 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800118 case CS_ERR_MEMSETUP:
119 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800120 }
121}
122
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800123cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
124{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800125 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800126 // Error: before cs_open(), dynamic memory management must be initialized
127 // with cs_option(CS_OPT_MEM)
128 return CS_ERR_MEMSETUP;
129
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800130 archs_enable();
131
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800132 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800133 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800134
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800135 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800136 if (!ud) {
137 // memory insufficient
138 return CS_ERR_MEM;
139 }
140
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800141 ud->errnum = CS_ERR_OK;
142 ud->arch = arch;
143 ud->mode = mode;
144 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800145 // by default, do not break instruction into details
146 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147
Nguyen Anh Quynh53fc5c12014-01-21 15:26:02 +0800148 cs_err err = arch_init[ud->arch](ud);
149 if (err) {
150 cs_mem_free(ud);
151 *handle = 0;
152 return err;
153 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800154
155 *handle = (uintptr_t)ud;
156
157 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800158 } else {
159 *handle = 0;
160 return CS_ERR_ARCH;
161 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800162}
163
164cs_err cs_close(csh handle)
165{
166 if (!handle)
167 return CS_ERR_CSH;
168
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800169 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170
171 switch (ud->arch) {
172 case CS_ARCH_X86:
173 break;
174 case CS_ARCH_ARM:
175 case CS_ARCH_MIPS:
176 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800177 case CS_ARCH_PPC:
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800178 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800179 break;
180 default: // unsupported architecture
181 return CS_ERR_HANDLE;
182 }
183
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800184 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800185
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800186 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800187 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800188 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800189
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800190 return CS_ERR_OK;
191}
192
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800193#define MIN(x, y) ((x) < (y) ? (x) : (y))
194
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800195// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800196static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800197 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800198{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800199 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800200 // avoiding copy insn->detail
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800201 memcpy(insn, (uintptr_t)&mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800202
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800203 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
204 // copy from @regs_read until @arm
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800205 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800206 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
207 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800208 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
209 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800210 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800211 } else {
212 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800213 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800214 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800216 // fill the instruction bytes
217 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
218
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800219 // map internal instruction opcode to public insn ID
220 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800221 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800222
223 // alias instruction might have ID saved in OpcodePub
224 if (MCInst_getOpcodePub(mci))
225 insn->id = MCInst_getOpcodePub(mci);
226
227 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800228 if (postprinter)
229 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800230
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800232 // find first space or tab
233 char *sp = buffer;
234 for (sp = buffer; *sp; sp++)
235 if (*sp == ' '||*sp == '\t')
236 break;
237 if (*sp) {
238 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800239 // find the next non-space char
240 sp++;
241 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
242 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800243 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
244 } else
245 insn->op_str[0] = '\0';
246
247 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
248 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
249}
250
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800251cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800252{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800253 // cs_option() can be called with NULL handle just for CS_OPT_MEM
254 // This is supposed to be executed before all other APIs (even cs_open())
255 if (type == CS_OPT_MEM) {
256 cs_opt_mem *mem = (cs_opt_mem *)value;
257
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800258 cs_mem_malloc = mem->malloc;
259 cs_mem_calloc = mem->calloc;
260 cs_mem_realloc = mem->realloc;
261 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800262 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800263
264 return CS_ERR_OK;
265 }
266
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800267 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600268 if (!handle)
269 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800270
danghvu0b6ea042013-12-19 23:07:26 -0600271 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800272 handle->detail = value;
273 return CS_ERR_OK;
274 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800275
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800276 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800277}
278
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800279// get previous instruction, which can be in the cache, or in total buffer
280static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
281{
282 if (f == 0) {
283 if (total == NULL)
284 return NULL;
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800285 // get the trailing insn from total buffer, which is at
286 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800287 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800288 } else
289 return &cache[f - 1];
290}
291
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800292// dynamicly allocate memory to contain disasm insn
293// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800294size_t cs_disasm_ex(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 +0800295{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800296 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297 MCInst mci;
298 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800299 size_t c = 0;
300 unsigned int f = 0;
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800301 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800303 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304
305 if (!handle) {
306 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800307 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308 return 0;
309 }
310
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800311 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800312
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800313 // reset previous prefix for X86
314 handle->prev_prefix = 0;
315
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800316 memset(insn_cache, 0, sizeof(insn_cache));
317
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800318 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600319 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800320 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800321
322 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
323 if (r) {
324 SStream ss;
325 SStream_Init(&ss);
326
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800327 // relative branches need to know the address & size of current insn
328 mci.insn_size = insn_size;
329 mci.address = offset;
330
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800331 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800332 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800333 mci.flat_insn.address = offset;
334 mci.flat_insn.size = insn_size;
335 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800336 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800337 }
338
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800339 handle->printer(&mci, &ss, handle->printer_info);
340
Joxean114df0e2013-12-04 07:11:32 +0100341 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800342
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800343 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
344 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800346 if (f == ARR_SIZE(insn_cache)) {
347 // resize total to contain newly disasm insns
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800348 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800349 void *tmp = cs_mem_realloc(total, total_size);
350 if (tmp == NULL) { // insufficient memory
351 cs_mem_free(total);
352 handle->errnum = CS_ERR_MEM;
353 return 0;
354 }
355
356 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800357 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800358
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800359 // reset f back to 0
360 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800361 }
362
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800363 c++;
364 } else {
365 // combine this instruction with previous prefix instruction
366 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
367 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800368 }
369
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370 buffer += insn_size;
371 size -= insn_size;
372 offset += insn_size;
373
374 if (count > 0 && c == count)
375 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800376 } else {
377 // encounter a broken instruction
378 // XXX: TODO: JOXEAN continue here
379 break;
380 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381 }
382
383 if (f) {
384 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800385 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800386 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800387 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800388 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800389 return 0;
390 }
391
392 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800393 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800394
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395 }
396
397 *insn = total;
398
399 return c;
400}
401
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800402void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800403{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800404 size_t i;
405
406 // free all detail pointers
407 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800408 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800409
410 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800411 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412}
413
414// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100415const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800416{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800417 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418
419 if (!handle || handle->reg_name == NULL) {
420 return NULL;
421 }
422
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800423 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424}
425
pancakef0e4eed2013-12-11 22:14:42 +0100426const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800427{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800428 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429
430 if (!handle || handle->insn_name == NULL) {
431 return NULL;
432 }
433
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800434 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800435}
436
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800437static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438{
439 int i;
440
441 for (i = 0; i < max; i++) {
442 if (arr[i] == id)
443 return true;
444 }
445
446 return false;
447}
448
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800449bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800450{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800451 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800452 return false;
453
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800454 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800455 if (!handle->detail) {
456 handle->errnum = CS_ERR_DETAIL;
457 return false;
458 }
459
460 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800461}
462
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800463bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800464{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800465 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466 return false;
467
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800468 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800469 if (!handle->detail) {
470 handle->errnum = CS_ERR_DETAIL;
471 return false;
472 }
473
474 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800475}
476
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800477bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800478{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800479 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480 return false;
481
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800482 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800483 if (!handle->detail) {
484 handle->errnum = CS_ERR_DETAIL;
485 return false;
486 }
487
488 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800489}
490
491int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
492{
493 if (!ud)
494 return -1;
495
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800496 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800497 if (!handle->detail) {
498 handle->errnum = CS_ERR_DETAIL;
499 return -1;
500 }
501
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800502 unsigned int count = 0, i;
503
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800504 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800505
506 switch (handle->arch) {
507 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800508 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800509 return -1;
510 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800511 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800512 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513 count++;
514 break;
515 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800516 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800517 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800518 count++;
519 break;
520 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800521 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800522 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800523 count++;
524 break;
525 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800526 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800527 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800528 count++;
529 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800530 case CS_ARCH_PPC:
531 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800532 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800533 count++;
534 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800535 }
536
537 return count;
538}
539
540int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
541 unsigned int post)
542{
543 if (!ud)
544 return -1;
545
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800546 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800547 if (!handle->detail) {
548 handle->errnum = CS_ERR_DETAIL;
549 return -1;
550 }
551
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800552 unsigned int count = 0, i;
553
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800554 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800555
556 switch (handle->arch) {
557 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800558 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800559 return -1;
560 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800561 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800562 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800563 count++;
564 if (count == post)
565 return i;
566 }
567 break;
568 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800569 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800570 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800571 count++;
572 if (count == post)
573 return i;
574 }
575 break;
576 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800577 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800578 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 count++;
580 if (count == post)
581 return i;
582 }
583 break;
584 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800585 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800586 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800587 count++;
588 if (count == post)
589 return i;
590 }
591 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800592 case CS_ARCH_PPC:
593 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800594 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800595 count++;
596 if (count == post)
597 return i;
598 }
599 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800600 }
601
602 return -1;
603}