blob: c2ed6bd88e5573807730c6e59a6e53d254c6078c [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 Quynhc34959b2014-01-22 09:46:42 +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
92 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
93
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]) {
133 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 Quynh06b3c052014-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
169 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
170
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
196static void fill_insn(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
201 memcpy(insn, &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
205 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
206 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
207 // then copy from @arm until end
208 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
209 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800210 } else {
211 insn->address = mci->address;
212 insn->size = mci->insn_size;
213 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800214
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800215 // fill the instruction bytes
216 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
217
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800218 // map internal instruction opcode to public insn ID
219 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800220 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800221
222 // alias instruction might have ID saved in OpcodePub
223 if (MCInst_getOpcodePub(mci))
224 insn->id = MCInst_getOpcodePub(mci);
225
226 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800227 if (postprinter)
228 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800229
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800230 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800231 // find first space or tab
232 char *sp = buffer;
233 for (sp = buffer; *sp; sp++)
234 if (*sp == ' '||*sp == '\t')
235 break;
236 if (*sp) {
237 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800238 // find the next non-space char
239 sp++;
240 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
241 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800242 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
243 } else
244 insn->op_str[0] = '\0';
245
246 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
247 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
248}
249
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800250cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800251{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800252 // cs_option() can be called with NULL handle just for CS_OPT_MEM
253 // This is supposed to be executed before all other APIs (even cs_open())
254 if (type == CS_OPT_MEM) {
255 cs_opt_mem *mem = (cs_opt_mem *)value;
256
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800257 cs_mem_malloc = mem->malloc;
258 cs_mem_calloc = mem->calloc;
259 cs_mem_realloc = mem->realloc;
260 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800261 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800262
263 return CS_ERR_OK;
264 }
265
danghvu2b192962013-12-19 22:40:28 -0600266 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
267 if (!handle)
268 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800269
danghvu0b6ea042013-12-19 23:07:26 -0600270 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800271 handle->detail = value;
272 return CS_ERR_OK;
273 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800274
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800275 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800276}
277
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800278// get previous instruction, which can be in the cache, or in total buffer
279static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
280{
281 if (f == 0) {
282 if (total == NULL)
283 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800284 // get the trailing insn from total buffer, which is at
285 // the end of the latest cache trunk
286 return (cs_insn *)(total + total_size - (sizeof(cs_insn) * INSN_CACHE_SIZE));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800287 } else
288 return &cache[f - 1];
289}
290
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800291// dynamicly allocate memory to contain disasm insn
292// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800293size_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 +0800294{
295 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
296 MCInst mci;
297 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800298 size_t c = 0;
299 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800300 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800302 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303
304 if (!handle) {
305 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800306 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307 return 0;
308 }
309
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800310 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800311
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800312 memset(insn_cache, 0, sizeof(insn_cache));
313
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800314 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600315 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800316 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317
318 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
319 if (r) {
320 SStream ss;
321 SStream_Init(&ss);
322
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800323 // relative branches need to know the address & size of current insn
324 mci.insn_size = insn_size;
325 mci.address = offset;
326
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800327 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800328 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800329 mci.flat_insn.address = offset;
330 mci.flat_insn.size = insn_size;
331 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800332 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800333 }
334
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800335 handle->printer(&mci, &ss, handle->printer_info);
336
Joxean114df0e2013-12-04 07:11:32 +0100337 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800338
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800339 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
340 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800341
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800342 if (f == ARR_SIZE(insn_cache)) {
343 // resize total to contain newly disasm insns
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800344 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800345 void *tmp = cs_mem_realloc(total, total_size);
346 if (tmp == NULL) { // insufficient memory
347 cs_mem_free(total);
348 handle->errnum = CS_ERR_MEM;
349 return 0;
350 }
351
352 total = tmp;
353 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
354 // reset f back to 0
355 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356 }
357
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800358 c++;
359 } else {
360 // combine this instruction with previous prefix instruction
361 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
362 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800363 }
364
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800365 buffer += insn_size;
366 size -= insn_size;
367 offset += insn_size;
368
369 if (count > 0 && c == count)
370 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800371 } else {
372 // encounter a broken instruction
373 // XXX: TODO: JOXEAN continue here
374 break;
375 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800376 }
377
378 if (f) {
379 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800380 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800382 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800383 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800384 return 0;
385 }
386
387 total = tmp;
388 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
389 }
390
391 *insn = total;
392
393 return c;
394}
395
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800396void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800397{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800398 size_t i;
399
400 // free all detail pointers
401 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800402 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800403
404 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800405 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800406}
407
408// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100409const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800410{
411 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
412
413 if (!handle || handle->reg_name == NULL) {
414 return NULL;
415 }
416
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800417 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418}
419
pancakef0e4eed2013-12-11 22:14:42 +0100420const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421{
422 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
423
424 if (!handle || handle->insn_name == NULL) {
425 return NULL;
426 }
427
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800428 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429}
430
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800431static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432{
433 int i;
434
435 for (i = 0; i < max; i++) {
436 if (arr[i] == id)
437 return true;
438 }
439
440 return false;
441}
442
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800443bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800444{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800445 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446 return false;
447
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800448 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800449 if (!handle->detail) {
450 handle->errnum = CS_ERR_DETAIL;
451 return false;
452 }
453
454 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455}
456
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800457bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800458{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800459 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460 return false;
461
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800462 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800463 if (!handle->detail) {
464 handle->errnum = CS_ERR_DETAIL;
465 return false;
466 }
467
468 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469}
470
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800471bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800472{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800473 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800474 return false;
475
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800476 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800477 if (!handle->detail) {
478 handle->errnum = CS_ERR_DETAIL;
479 return false;
480 }
481
482 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483}
484
485int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
486{
487 if (!ud)
488 return -1;
489
490 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800491 if (!handle->detail) {
492 handle->errnum = CS_ERR_DETAIL;
493 return -1;
494 }
495
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800496 unsigned int count = 0, i;
497
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800498 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800499
500 switch (handle->arch) {
501 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800502 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800503 return -1;
504 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800505 for (i = 0; i < insn->detail->arm.op_count; i++)
506 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800507 count++;
508 break;
509 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800510 for (i = 0; i < insn->detail->arm64.op_count; i++)
511 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800512 count++;
513 break;
514 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800515 for (i = 0; i < insn->detail->x86.op_count; i++)
516 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800517 count++;
518 break;
519 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800520 for (i = 0; i < insn->detail->mips.op_count; i++)
521 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522 count++;
523 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800524 case CS_ARCH_PPC:
525 for (i = 0; i < insn->detail->ppc.op_count; i++)
526 if (insn->detail->ppc.operands[i].type == op_type)
527 count++;
528 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800529 }
530
531 return count;
532}
533
534int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
535 unsigned int post)
536{
537 if (!ud)
538 return -1;
539
540 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800541 if (!handle->detail) {
542 handle->errnum = CS_ERR_DETAIL;
543 return -1;
544 }
545
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800546 unsigned int count = 0, i;
547
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800548 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800549
550 switch (handle->arch) {
551 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800552 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800553 return -1;
554 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800555 for (i = 0; i < insn->detail->arm.op_count; i++) {
556 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800557 count++;
558 if (count == post)
559 return i;
560 }
561 break;
562 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800563 for (i = 0; i < insn->detail->arm64.op_count; i++) {
564 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800565 count++;
566 if (count == post)
567 return i;
568 }
569 break;
570 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800571 for (i = 0; i < insn->detail->x86.op_count; i++) {
572 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800573 count++;
574 if (count == post)
575 return i;
576 }
577 break;
578 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800579 for (i = 0; i < insn->detail->mips.op_count; i++) {
580 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800581 count++;
582 if (count == post)
583 return i;
584 }
585 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800586 case CS_ARCH_PPC:
587 for (i = 0; i < insn->detail->ppc.op_count; i++) {
588 if (insn->detail->ppc.operands[i].type == op_type)
589 count++;
590 if (count == post)
591 return i;
592 }
593 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800594 }
595
596 return -1;
597}