blob: 05a5f1de354c41ac54065dcac5734f74d81851b9 [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);
danghvu701b8502014-01-09 11:06:44 +070036
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080038{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080039 static bool initialized = false;
40
41 if (initialized)
42 return;
43
danghvub33bd2c2014-01-09 12:22:56 +070044#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070046#endif
47#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080048 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070049#endif
50#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080051 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070052#endif
danghvub33bd2c2014-01-09 12:22:56 +070053#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080054 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070055#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080056#ifdef CAPSTONE_HAS_SPARC
57 Sparc_enable();
58#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080059#ifdef CAPSTONE_HAS_SYSZ
60 SystemZ_enable();
61#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080062#ifdef CAPSTONE_HAS_X86
63 X86_enable();
64#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080065
66 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070067}
68
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080069unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080070
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010071#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080072cs_malloc_t cs_mem_malloc = malloc;
73cs_calloc_t cs_mem_calloc = calloc;
74cs_realloc_t cs_mem_realloc = realloc;
75cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080076cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080077#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080078cs_malloc_t cs_mem_malloc = NULL;
79cs_calloc_t cs_mem_calloc = NULL;
80cs_realloc_t cs_mem_realloc = NULL;
81cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080082cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080083#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080084
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080085unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080086{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080087 archs_enable();
88
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080089 if (major != NULL && minor != NULL) {
90 *major = CS_API_MAJOR;
91 *minor = CS_API_MINOR;
92 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080093
94 return (CS_API_MAJOR << 8) + CS_API_MINOR;
95}
96
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080097bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080098{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080099 archs_enable();
100
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800101 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800102 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800103 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800104 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
105 (1 << CS_ARCH_SYSZ));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800106
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800107 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800108 return all_arch & (1 << query);
109
110 if (query == CS_SUPPORT_DIET) {
111#ifdef CAPSTONE_DIET
112 return true;
113#else
114 return false;
115#endif
116 }
117
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800118 if (query == CS_SUPPORT_X86_REDUCE) {
119#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800120 return true;
121#else
122 return false;
123#endif
124 }
125
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800126 // unsupported query
127 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800128}
129
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800130cs_err cs_errno(csh handle)
131{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800132 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800133 if (!handle)
134 return CS_ERR_CSH;
135
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100136 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800137
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800138 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800139}
140
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800141const char *cs_strerror(cs_err code)
142{
143 switch(code) {
144 default:
145 return "Unknown error code";
146 case CS_ERR_OK:
147 return "OK (CS_ERR_OK)";
148 case CS_ERR_MEM:
149 return "Out of memory (CS_ERR_MEM)";
150 case CS_ERR_ARCH:
151 return "Invalid architecture (CS_ERR_ARCH)";
152 case CS_ERR_HANDLE:
153 return "Invalid handle (CS_ERR_HANDLE)";
154 case CS_ERR_CSH:
155 return "Invalid csh (CS_ERR_CSH)";
156 case CS_ERR_MODE:
157 return "Invalid mode (CS_ERR_MODE)";
158 case CS_ERR_OPTION:
159 return "Invalid option (CS_ERR_OPTION)";
160 case CS_ERR_DETAIL:
161 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800162 case CS_ERR_MEMSETUP:
163 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800164 case CS_ERR_VERSION:
165 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800166 case CS_ERR_DIET:
167 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800168 case CS_ERR_SKIPDATA:
169 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800170 }
171}
172
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
174{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800175 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800176 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800177 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800178 // Error: before cs_open(), dynamic memory management must be initialized
179 // with cs_option(CS_OPT_MEM)
180 return CS_ERR_MEMSETUP;
181
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800182 archs_enable();
183
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800184 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800185 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800186 if (!ud) {
187 // memory insufficient
188 return CS_ERR_MEM;
189 }
190
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800191 ud->errnum = CS_ERR_OK;
192 ud->arch = arch;
193 ud->mode = mode;
194 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800195 // by default, do not break instruction into details
196 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800198 // default skipdata setup
199 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
200
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100201 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800202 if (err) {
203 cs_mem_free(ud);
204 *handle = 0;
205 return err;
206 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800207
208 *handle = (uintptr_t)ud;
209
210 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800211 } else {
212 *handle = 0;
213 return CS_ERR_ARCH;
214 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215}
216
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800217cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800218{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800219 struct cs_struct *ud;
220
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800221 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800222 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223 return CS_ERR_CSH;
224
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100225 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800226
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800227 if (ud->printer_info)
228 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800230 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800231
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800232 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800233 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800234 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800235
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800236 // invalidate this handle by ZERO out its value.
237 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800238 *handle = 0;
239
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240 return CS_ERR_OK;
241}
242
243// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800244static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800245 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800246{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800247#ifndef CAPSTONE_DIET
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800248 char *sp;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800249#endif
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800250
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800251 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800252 // avoiding copy insn->detail
253 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800254
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800255 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
256 // copy from @regs_read until @arm
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100257 memcpy(insn->detail, (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800258 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
259 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800260 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
261 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800262 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800263 } else {
264 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800265 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800266 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800268 // fill the instruction bytes
269 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
270
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800271 // map internal instruction opcode to public insn ID
272 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800273 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800274
275 // alias instruction might have ID saved in OpcodePub
276 if (MCInst_getOpcodePub(mci))
277 insn->id = MCInst_getOpcodePub(mci);
278
279 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800280 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800281 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800282
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800283#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800284 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800285 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100286 sp = buffer;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800287 for (sp = buffer; *sp; sp++) {
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800288 if (*sp == ' '||*sp == '\t')
289 break;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800290 if (*sp == '|')
291 *sp = ' ';
292 }
293
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800294 if (*sp) {
295 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800296 // find the next non-space char
297 sp++;
298 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
299 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
301 } else
302 insn->op_str[0] = '\0';
303
Nguyen Anh Quynh1dbc9592014-05-07 14:14:07 +0800304 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800306#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307}
308
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800309// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800310// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800311static uint8_t skipdata_size(cs_struct *handle)
312{
313 switch(handle->arch) {
314 default:
315 // should never reach
316 return -1;
317 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800318 // skip 2 bytes on Thumb mode.
319 if (handle->mode & CS_MODE_THUMB)
320 return 2;
321 // otherwise, skip 4 bytes
322 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800323 case CS_ARCH_ARM64:
324 case CS_ARCH_MIPS:
325 case CS_ARCH_PPC:
326 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800327 // skip 4 bytes
328 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800329 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800330 // SystemZ instruction's length can be 2, 4 or 6 bytes,
331 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800332 return 2;
333 case CS_ARCH_X86:
334 // X86 has no restriction on instruction alignment
335 return 1;
336 }
337}
338
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800339cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800340{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800341 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800342 archs_enable();
343
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800344 // cs_option() can be called with NULL handle just for CS_OPT_MEM
345 // This is supposed to be executed before all other APIs (even cs_open())
346 if (type == CS_OPT_MEM) {
347 cs_opt_mem *mem = (cs_opt_mem *)value;
348
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800349 cs_mem_malloc = mem->malloc;
350 cs_mem_calloc = mem->calloc;
351 cs_mem_realloc = mem->realloc;
352 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800353 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800354
355 return CS_ERR_OK;
356 }
357
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100358 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600359 if (!handle)
360 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800361
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800362 switch(type) {
363 default:
364 break;
365 case CS_OPT_DETAIL:
366 handle->detail = value;
367 return CS_ERR_OK;
368 case CS_OPT_SKIPDATA:
369 handle->skipdata = (value == CS_OPT_ON);
370 if (handle->skipdata) {
371 if (handle->skipdata_size == 0) {
372 // set the default skipdata size
373 handle->skipdata_size = skipdata_size(handle);
374 }
375 }
376 return CS_ERR_OK;
377 case CS_OPT_SKIPDATA_SETUP:
378 if (value)
379 handle->skipdata_setup = *((cs_opt_skipdata *)value);
380 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800381 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800382
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800383 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800384}
385
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800386// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800387static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
388{
389 char *p = opstr;
390 int len;
391 size_t i;
392
393 if (!size) {
394 opstr[0] = '\0';
395 return;
396 }
397
398 len = sprintf(p, "0x%02x", buffer[0]);
399 p+= len;
400
401 for(i = 1; i < size; i++) {
402 len = sprintf(p, ", 0x%02x", buffer[i]);
403 p+= len;
404 }
405}
406
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407// dynamicly allocate memory to contain disasm insn
408// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800409size_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 +0800410{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800411 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412 MCInst mci;
413 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800414 size_t c = 0;
415 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800416 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800417 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800418 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800419 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800420 void *tmp;
421 size_t skipdata_bytes;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800422 uint64_t offset_org;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800424 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800426 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800427 return 0;
428 }
429
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800430 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800432 memset(insn_cache, 0, sizeof(insn_cache));
433
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800434 // save the original offset for SKIPDATA
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800435 offset_org = offset;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800436
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800437 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600438 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800439 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800441 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442 if (r) {
443 SStream ss;
444 SStream_Init(&ss);
445
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800446 // relative branches need to know the address & size of current insn
447 mci.insn_size = insn_size;
448 mci.address = offset;
449
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800450 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800451 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800452 mci.flat_insn.address = offset;
453 mci.flat_insn.size = insn_size;
454 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800455 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800456 }
457
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800458 handle->printer(&mci, &ss, handle->printer_info);
459
Joxean114df0e2013-12-04 07:11:32 +0100460 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800461
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800462 f++;
463 if (f == ARR_SIZE(insn_cache)) {
464 // resize total to contain newly disasm insns
465 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
466 tmp = cs_mem_realloc(total, total_size);
467 if (tmp == NULL) { // insufficient memory
468 cs_mem_free(total);
469 handle->errnum = CS_ERR_MEM;
470 return 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471 }
472
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800473 total = tmp;
474 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
475
476 // reset f back to 0
477 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800478 }
479
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800480 c++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481 buffer += insn_size;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800482
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483 size -= insn_size;
484 offset += insn_size;
485
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800486 if (count > 0 && c == count)
487 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800488 } else {
489 // encounter a broken instruction
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800490 // if there is no request to skip data, or remaining data is too small,
491 // then bail out
492 if (!handle->skipdata || handle->skipdata_size > size)
493 break;
494
495 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh42288ac2014-04-14 14:53:13 +0800496 skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800497 handle->skipdata_setup.user_data);
498 if (skipdata_bytes > size)
499 // remaining data is not enough
500 break;
501
502 if (!skipdata_bytes)
503 // user requested not to skip data, so bail out
504 break;
505 } else
506 skipdata_bytes = handle->skipdata_size;
507
508 // we have to skip some amount of data, depending on arch & mode
509 insn_cache[f].id = 0; // invalid ID for this "data" instruction
510 insn_cache[f].address = offset;
511 insn_cache[f].size = skipdata_bytes;
512 memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
513 strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
514 sizeof(insn_cache[f].mnemonic) - 1);
515 skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
516 insn_cache[f].detail = NULL;
517
518 f++;
519 if (f == ARR_SIZE(insn_cache)) {
520 // resize total to contain newly disasm insns
521
522 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
523 tmp = cs_mem_realloc(total, total_size);
524 if (tmp == NULL) { // insufficient memory
525 cs_mem_free(total);
526 handle->errnum = CS_ERR_MEM;
527 return 0;
528 }
529
530 total = tmp;
531 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
532
533 // reset f back to 0
534 f = 0;
535 }
536
537 buffer += skipdata_bytes;
538 size -= skipdata_bytes;
539 offset += skipdata_bytes;
540 c++;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800541 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800542 }
543
544 if (f) {
545 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800546 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800547 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800548 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800549 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800550 return 0;
551 }
552
553 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800554 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800555
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800556 }
557
558 *insn = total;
559
560 return c;
561}
562
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800563void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800564{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800565 size_t i;
566
567 // free all detail pointers
568 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800569 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800570
571 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800572 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800573}
574
575// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100576const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800577{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800578 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579
580 if (!handle || handle->reg_name == NULL) {
581 return NULL;
582 }
583
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800584 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800585}
586
pancakef0e4eed2013-12-11 22:14:42 +0100587const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800589 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800590
591 if (!handle || handle->insn_name == NULL) {
592 return NULL;
593 }
594
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800595 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800596}
597
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800598static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800599{
600 int i;
601
602 for (i = 0; i < max; i++) {
603 if (arr[i] == id)
604 return true;
605 }
606
607 return false;
608}
609
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800610bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800611{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800612 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800613 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800614 return false;
615
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100616 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200617
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800618 if (!handle->detail) {
619 handle->errnum = CS_ERR_DETAIL;
620 return false;
621 }
622
Giovanni Condello95657e02014-05-07 17:31:27 +0200623 if(!insn->id) {
624 handle->errnum = CS_ERR_SKIPDATA;
625 return false;
626 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200627
Giovanni Condello95657e02014-05-07 17:31:27 +0200628 if(!insn->detail) {
629 handle->errnum = CS_ERR_DETAIL;
630 return false;
631 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200632
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800633 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634}
635
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800636bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800637{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800638 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800639 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800640 return false;
641
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100642 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200643
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800644 if (!handle->detail) {
645 handle->errnum = CS_ERR_DETAIL;
646 return false;
647 }
648
Giovanni Condello95657e02014-05-07 17:31:27 +0200649 if(!insn->id) {
650 handle->errnum = CS_ERR_SKIPDATA;
651 return false;
652 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200653
Giovanni Condello95657e02014-05-07 17:31:27 +0200654 if(!insn->detail) {
655 handle->errnum = CS_ERR_DETAIL;
656 return false;
657 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200658
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800659 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800660}
661
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800662bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800663{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800664 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800665 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800666 return false;
667
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100668 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200669
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800670 if (!handle->detail) {
671 handle->errnum = CS_ERR_DETAIL;
672 return false;
673 }
674
Giovanni Condello95657e02014-05-07 17:31:27 +0200675 if(!insn->id) {
676 handle->errnum = CS_ERR_SKIPDATA;
677 return false;
678 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200679
Giovanni Condello95657e02014-05-07 17:31:27 +0200680 if(!insn->detail) {
681 handle->errnum = CS_ERR_DETAIL;
682 return false;
683 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200684
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800685 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800686}
687
688int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
689{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800690 struct cs_struct *handle;
691 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800692 if (!ud)
693 return -1;
694
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100695 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200696
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800697 if (!handle->detail) {
698 handle->errnum = CS_ERR_DETAIL;
699 return -1;
700 }
701
Giovanni Condello95657e02014-05-07 17:31:27 +0200702 if(!insn->id) {
703 handle->errnum = CS_ERR_SKIPDATA;
704 return -1;
705 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200706
Giovanni Condello95657e02014-05-07 17:31:27 +0200707 if(!insn->detail) {
708 handle->errnum = CS_ERR_DETAIL;
709 return -1;
710 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200711
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800712 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800713
714 switch (handle->arch) {
715 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800716 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800717 return -1;
718 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800719 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800720 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800721 count++;
722 break;
723 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800724 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800725 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800726 count++;
727 break;
728 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800729 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800730 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800731 count++;
732 break;
733 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800734 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800735 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800736 count++;
737 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800738 case CS_ARCH_PPC:
739 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800740 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800741 count++;
742 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800743 case CS_ARCH_SPARC:
744 for (i = 0; i < insn->detail->sparc.op_count; i++)
745 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
746 count++;
747 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800748 case CS_ARCH_SYSZ:
749 for (i = 0; i < insn->detail->sysz.op_count; i++)
750 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
751 count++;
752 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800753 }
754
755 return count;
756}
757
758int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
759 unsigned int post)
760{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800761 struct cs_struct *handle;
762 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800763 if (!ud)
764 return -1;
765
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100766 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200767
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800768 if (!handle->detail) {
769 handle->errnum = CS_ERR_DETAIL;
770 return -1;
771 }
772
Giovanni Condello95657e02014-05-07 17:31:27 +0200773 if(!insn->id) {
774 handle->errnum = CS_ERR_SKIPDATA;
775 return -1;
776 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200777
Giovanni Condello95657e02014-05-07 17:31:27 +0200778 if(!insn->detail) {
779 handle->errnum = CS_ERR_DETAIL;
780 return -1;
781 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200782
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800783 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800784
785 switch (handle->arch) {
786 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800787 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800788 return -1;
789 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800790 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800791 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800792 count++;
793 if (count == post)
794 return i;
795 }
796 break;
797 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800798 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800799 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800800 count++;
801 if (count == post)
802 return i;
803 }
804 break;
805 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800806 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800807 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800808 count++;
809 if (count == post)
810 return i;
811 }
812 break;
813 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800814 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800815 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800816 count++;
817 if (count == post)
818 return i;
819 }
820 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800821 case CS_ARCH_PPC:
822 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800823 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800824 count++;
825 if (count == post)
826 return i;
827 }
828 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800829 case CS_ARCH_SPARC:
830 for (i = 0; i < insn->detail->sparc.op_count; i++) {
831 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
832 count++;
833 if (count == post)
834 return i;
835 }
836 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800837 case CS_ARCH_SYSZ:
838 for (i = 0; i < insn->detail->sysz.op_count; i++) {
839 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
840 count++;
841 if (count == post)
842 return i;
843 }
844 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800845 }
846
847 return -1;
848}