blob: 250e1055e9f02f2432628dfd01265c2545978326 [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 Quynh42c6b1a2013-12-30 00:15:25 +080013cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080014cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
15void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080016
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080017extern void ARM_enable(void);
18extern void AArch64_enable(void);
19extern void Mips_enable(void);
20extern void X86_enable(void);
21extern void PPC_enable(void);
danghvu701b8502014-01-09 11:06:44 +070022
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080023static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080024{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080025 static bool initialized = false;
26
27 if (initialized)
28 return;
29
danghvub33bd2c2014-01-09 12:22:56 +070030#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080031 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070032#endif
33#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070035#endif
36#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070038#endif
39#ifdef CAPSTONE_HAS_X86
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080040 X86_enable();
danghvub33bd2c2014-01-09 12:22:56 +070041#endif
42#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080043 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070044#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045
46 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070047}
48
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080049unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080050
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080051#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080052cs_malloc_t cs_mem_malloc = malloc;
53cs_calloc_t cs_mem_calloc = calloc;
54cs_realloc_t cs_mem_realloc = realloc;
55cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080056cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080057#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080058cs_malloc_t cs_mem_malloc = NULL;
59cs_calloc_t cs_mem_calloc = NULL;
60cs_realloc_t cs_mem_realloc = NULL;
61cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080062cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080063#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080064
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080065unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080066{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080067 if (major != NULL && minor != NULL) {
68 *major = CS_API_MAJOR;
69 *minor = CS_API_MINOR;
70 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080071
72 return (CS_API_MAJOR << 8) + CS_API_MINOR;
73}
74
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080075bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080076{
77 if (arch == CS_ARCH_ALL)
78 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080079 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
80 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080081
82 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080083}
84
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080085cs_err cs_errno(csh handle)
86{
87 if (!handle)
88 return CS_ERR_CSH;
89
90 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
91
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080092 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080093}
94
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080095const char *cs_strerror(cs_err code)
96{
97 switch(code) {
98 default:
99 return "Unknown error code";
100 case CS_ERR_OK:
101 return "OK (CS_ERR_OK)";
102 case CS_ERR_MEM:
103 return "Out of memory (CS_ERR_MEM)";
104 case CS_ERR_ARCH:
105 return "Invalid architecture (CS_ERR_ARCH)";
106 case CS_ERR_HANDLE:
107 return "Invalid handle (CS_ERR_HANDLE)";
108 case CS_ERR_CSH:
109 return "Invalid csh (CS_ERR_CSH)";
110 case CS_ERR_MODE:
111 return "Invalid mode (CS_ERR_MODE)";
112 case CS_ERR_OPTION:
113 return "Invalid option (CS_ERR_OPTION)";
114 case CS_ERR_DETAIL:
115 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800116 case CS_ERR_MEMSETUP:
117 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800118 }
119}
120
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800121cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
122{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800123 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800124 // Error: before cs_open(), dynamic memory management must be initialized
125 // with cs_option(CS_OPT_MEM)
126 return CS_ERR_MEMSETUP;
127
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800128 archs_enable();
129
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800130 if (arch < CS_ARCH_MAX && arch_init[arch]) {
131 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800132
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800133 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800134 if (!ud) {
135 // memory insufficient
136 return CS_ERR_MEM;
137 }
138
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800139 ud->errnum = CS_ERR_OK;
140 ud->arch = arch;
141 ud->mode = mode;
142 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800143 // by default, do not break instruction into details
144 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800145
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800146 cs_err err = arch_init[ud->arch](ud);
147 if (err) {
148 cs_mem_free(ud);
149 *handle = 0;
150 return err;
151 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800152
153 *handle = (uintptr_t)ud;
154
155 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800156 } else {
157 *handle = 0;
158 return CS_ERR_ARCH;
159 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800160}
161
162cs_err cs_close(csh handle)
163{
164 if (!handle)
165 return CS_ERR_CSH;
166
167 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
168
169 switch (ud->arch) {
170 case CS_ARCH_X86:
171 break;
172 case CS_ARCH_ARM:
173 case CS_ARCH_MIPS:
174 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800175 case CS_ARCH_PPC:
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800176 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800177 break;
178 default: // unsupported architecture
179 return CS_ERR_HANDLE;
180 }
181
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800182 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800183
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800184 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800185 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800186 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800187
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800188 return CS_ERR_OK;
189}
190
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800191#define MIN(x, y) ((x) < (y) ? (x) : (y))
192
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193// fill insn with mnemonic & operands info
194static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800195 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800196{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800197 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800198 // avoiding copy insn->detail
199 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800201 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
202 // copy from @regs_read until @arm
203 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
204 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
205 // then copy from @arm until end
206 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
207 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800208 } else {
209 insn->address = mci->address;
210 insn->size = mci->insn_size;
211 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800213 // fill the instruction bytes
214 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
215
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800216 // map internal instruction opcode to public insn ID
217 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800218 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800219
220 // alias instruction might have ID saved in OpcodePub
221 if (MCInst_getOpcodePub(mci))
222 insn->id = MCInst_getOpcodePub(mci);
223
224 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800225 if (postprinter)
226 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800227
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800229 // find first space or tab
230 char *sp = buffer;
231 for (sp = buffer; *sp; sp++)
232 if (*sp == ' '||*sp == '\t')
233 break;
234 if (*sp) {
235 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800236 // find the next non-space char
237 sp++;
238 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
239 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
241 } else
242 insn->op_str[0] = '\0';
243
244 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
245 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
246}
247
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800248cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800249{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800250 // cs_option() can be called with NULL handle just for CS_OPT_MEM
251 // This is supposed to be executed before all other APIs (even cs_open())
252 if (type == CS_OPT_MEM) {
253 cs_opt_mem *mem = (cs_opt_mem *)value;
254
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800255 cs_mem_malloc = mem->malloc;
256 cs_mem_calloc = mem->calloc;
257 cs_mem_realloc = mem->realloc;
258 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800259 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800260
261 return CS_ERR_OK;
262 }
263
danghvu2b192962013-12-19 22:40:28 -0600264 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
265 if (!handle)
266 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800267
danghvu0b6ea042013-12-19 23:07:26 -0600268 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800269 handle->detail = value;
270 return CS_ERR_OK;
271 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800272
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800273 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800274}
275
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800276// get previous instruction, which can be in the cache, or in total buffer
277static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
278{
279 if (f == 0) {
280 if (total == NULL)
281 return NULL;
282 // get the trailing insn from total buffer
283 return (cs_insn *)(total + total_size - sizeof(cs_insn));
284 } else
285 return &cache[f - 1];
286}
287
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288// dynamicly allocate memory to contain disasm insn
289// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800290size_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 +0800291{
292 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
293 MCInst mci;
294 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800295 size_t c = 0;
296 unsigned int f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297 cs_insn insn_cache[64];
298 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800299 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300
301 if (!handle) {
302 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800303 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304 return 0;
305 }
306
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800307 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800309 memset(insn_cache, 0, sizeof(insn_cache));
310
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800311 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600312 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800313 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800314
315 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
316 if (r) {
317 SStream ss;
318 SStream_Init(&ss);
319
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800320 // relative branches need to know the address & size of current insn
321 mci.insn_size = insn_size;
322 mci.address = offset;
323
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800324 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800325 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800326 mci.flat_insn.address = offset;
327 mci.flat_insn.size = insn_size;
328 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800329 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800330 }
331
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800332 handle->printer(&mci, &ss, handle->printer_info);
333
Joxean114df0e2013-12-04 07:11:32 +0100334 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800335
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800336 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
337 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800338
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800339 if (f == ARR_SIZE(insn_cache)) {
340 // resize total to contain newly disasm insns
341 total_size += sizeof(insn_cache);
342 void *tmp = cs_mem_realloc(total, total_size);
343 if (tmp == NULL) { // insufficient memory
344 cs_mem_free(total);
345 handle->errnum = CS_ERR_MEM;
346 return 0;
347 }
348
349 total = tmp;
350 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
351 // reset f back to 0
352 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353 }
354
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800355 c++;
356 } else {
357 // combine this instruction with previous prefix instruction
358 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
359 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800360 }
361
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800362 buffer += insn_size;
363 size -= insn_size;
364 offset += insn_size;
365
366 if (count > 0 && c == count)
367 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800368 } else {
369 // encounter a broken instruction
370 // XXX: TODO: JOXEAN continue here
371 break;
372 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800373 }
374
375 if (f) {
376 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800377 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800378 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800379 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800380 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381 return 0;
382 }
383
384 total = tmp;
385 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
386 }
387
388 *insn = total;
389
390 return c;
391}
392
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800393void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800395 size_t i;
396
397 // free all detail pointers
398 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800399 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800400
401 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800402 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800403}
404
405// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100406const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407{
408 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
409
410 if (!handle || handle->reg_name == NULL) {
411 return NULL;
412 }
413
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800414 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800415}
416
pancakef0e4eed2013-12-11 22:14:42 +0100417const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418{
419 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
420
421 if (!handle || handle->insn_name == NULL) {
422 return NULL;
423 }
424
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800425 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800426}
427
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800428static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429{
430 int i;
431
432 for (i = 0; i < max; i++) {
433 if (arr[i] == id)
434 return true;
435 }
436
437 return false;
438}
439
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800440bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800441{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800442 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800443 return false;
444
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800445 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800446 if (!handle->detail) {
447 handle->errnum = CS_ERR_DETAIL;
448 return false;
449 }
450
451 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800452}
453
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800454bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800456 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800457 return false;
458
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800459 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800460 if (!handle->detail) {
461 handle->errnum = CS_ERR_DETAIL;
462 return false;
463 }
464
465 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466}
467
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800468bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800470 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471 return false;
472
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800473 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800474 if (!handle->detail) {
475 handle->errnum = CS_ERR_DETAIL;
476 return false;
477 }
478
479 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480}
481
482int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
483{
484 if (!ud)
485 return -1;
486
487 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800488 if (!handle->detail) {
489 handle->errnum = CS_ERR_DETAIL;
490 return -1;
491 }
492
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800493 unsigned int count = 0, i;
494
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800495 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800496
497 switch (handle->arch) {
498 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800499 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800500 return -1;
501 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800502 for (i = 0; i < insn->detail->arm.op_count; i++)
503 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800504 count++;
505 break;
506 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800507 for (i = 0; i < insn->detail->arm64.op_count; i++)
508 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800509 count++;
510 break;
511 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800512 for (i = 0; i < insn->detail->x86.op_count; i++)
513 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800514 count++;
515 break;
516 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800517 for (i = 0; i < insn->detail->mips.op_count; i++)
518 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519 count++;
520 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800521 case CS_ARCH_PPC:
522 for (i = 0; i < insn->detail->ppc.op_count; i++)
523 if (insn->detail->ppc.operands[i].type == op_type)
524 count++;
525 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800526 }
527
528 return count;
529}
530
531int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
532 unsigned int post)
533{
534 if (!ud)
535 return -1;
536
537 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800538 if (!handle->detail) {
539 handle->errnum = CS_ERR_DETAIL;
540 return -1;
541 }
542
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800543 unsigned int count = 0, i;
544
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800545 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800546
547 switch (handle->arch) {
548 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800549 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800550 return -1;
551 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800552 for (i = 0; i < insn->detail->arm.op_count; i++) {
553 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800554 count++;
555 if (count == post)
556 return i;
557 }
558 break;
559 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800560 for (i = 0; i < insn->detail->arm64.op_count; i++) {
561 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800562 count++;
563 if (count == post)
564 return i;
565 }
566 break;
567 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800568 for (i = 0; i < insn->detail->x86.op_count; i++) {
569 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800570 count++;
571 if (count == post)
572 return i;
573 }
574 break;
575 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800576 for (i = 0; i < insn->detail->mips.op_count; i++) {
577 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800578 count++;
579 if (count == post)
580 return i;
581 }
582 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800583 case CS_ARCH_PPC:
584 for (i = 0; i < insn->detail->ppc.op_count; i++) {
585 if (insn->detail->ppc.operands[i].type == op_type)
586 count++;
587 if (count == post)
588 return i;
589 }
590 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800591 }
592
593 return -1;
594}