Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
Axel 0vercl0k Souchet | 605faf1 | 2014-05-09 20:40:00 +0100 | [diff] [blame] | 3 | #if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64) |
| 4 | #pragma warning(disable:4996) |
| 5 | #endif |
reverser | bcf09f4 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 6 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 7 | #include <libkern/libkern.h> |
| 8 | #else |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 9 | #include <stddef.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
reverser | bcf09f4 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 12 | #endif |
| 13 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #include <capstone.h> |
| 16 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | #include "utils.h" |
Nguyen Anh Quynh | c740407 | 2014-01-05 11:35:47 +0800 | [diff] [blame] | 18 | #include "MCRegisterInfo.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 19 | |
tandasat | 1dd8987 | 2016-04-23 15:51:24 -0700 | [diff] [blame] | 20 | #if defined(_KERNEL_MODE) |
| 21 | #include "windows\winkernel_mm.h" |
| 22 | #endif |
| 23 | |
| 24 | #if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE) |
Nguyen Anh Quynh | 472a4a4 | 2014-03-06 09:13:04 +0800 | [diff] [blame] | 25 | #define INSN_CACHE_SIZE 32 |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 26 | #else |
Nguyen Anh Quynh | 5ee2b45 | 2014-03-07 08:40:35 +0800 | [diff] [blame] | 27 | // reduce stack variable size for kernel/firmware |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 28 | #define INSN_CACHE_SIZE 8 |
| 29 | #endif |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 30 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 31 | // default SKIPDATA mnemonic |
Nguyen Anh Quynh | c75a909 | 2014-04-10 10:26:49 +0800 | [diff] [blame] | 32 | #define SKIPDATA_MNEM ".byte" |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 33 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 34 | cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL }; |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 35 | cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL }; |
| 36 | void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL }; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 37 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 38 | extern void ARM_enable(void); |
| 39 | extern void AArch64_enable(void); |
| 40 | extern void Mips_enable(void); |
| 41 | extern void X86_enable(void); |
| 42 | extern void PPC_enable(void); |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 43 | extern void Sparc_enable(void); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 44 | extern void SystemZ_enable(void); |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 45 | extern void XCore_enable(void); |
danghvu | 701b850 | 2014-01-09 11:06:44 +0700 | [diff] [blame] | 46 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 47 | static void archs_enable(void) |
Nguyen Anh Quynh | 625b5bc | 2014-01-09 14:33:56 +0800 | [diff] [blame] | 48 | { |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 49 | static bool initialized = false; |
| 50 | |
| 51 | if (initialized) |
| 52 | return; |
| 53 | |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 54 | #ifdef CAPSTONE_HAS_ARM |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 55 | ARM_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 56 | #endif |
| 57 | #ifdef CAPSTONE_HAS_ARM64 |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 58 | AArch64_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 59 | #endif |
| 60 | #ifdef CAPSTONE_HAS_MIPS |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 61 | Mips_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 62 | #endif |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 63 | #ifdef CAPSTONE_HAS_POWERPC |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 64 | PPC_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 65 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 66 | #ifdef CAPSTONE_HAS_SPARC |
| 67 | Sparc_enable(); |
| 68 | #endif |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 69 | #ifdef CAPSTONE_HAS_SYSZ |
| 70 | SystemZ_enable(); |
| 71 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 72 | #ifdef CAPSTONE_HAS_X86 |
| 73 | X86_enable(); |
| 74 | #endif |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 75 | #ifdef CAPSTONE_HAS_XCORE |
| 76 | XCore_enable(); |
| 77 | #endif |
| 78 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 79 | |
| 80 | initialized = true; |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 81 | } |
| 82 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 83 | unsigned int all_arch = 0; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 84 | |
tandasat | 1dd8987 | 2016-04-23 15:51:24 -0700 | [diff] [blame] | 85 | #if defined(CAPSTONE_USE_SYS_DYN_MEM) |
| 86 | #if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE) |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 87 | cs_malloc_t cs_mem_malloc = malloc; |
| 88 | cs_calloc_t cs_mem_calloc = calloc; |
| 89 | cs_realloc_t cs_mem_realloc = realloc; |
| 90 | cs_free_t cs_mem_free = free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 91 | cs_vsnprintf_t cs_vsnprintf = vsnprintf; |
tandasat | 1dd8987 | 2016-04-23 15:51:24 -0700 | [diff] [blame] | 92 | #elif defined(_KERNEL_MODE) |
| 93 | cs_malloc_t cs_mem_malloc = cs_winkernel_malloc; |
| 94 | cs_calloc_t cs_mem_calloc = cs_winkernel_calloc; |
| 95 | cs_realloc_t cs_mem_realloc = cs_winkernel_realloc; |
| 96 | cs_free_t cs_mem_free = cs_winkernel_free; |
| 97 | cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 98 | #else |
Pb | b8741bd | 2015-11-10 23:02:26 +0100 | [diff] [blame] | 99 | extern void* kern_os_malloc(size_t size); |
| 100 | extern void kern_os_free(void* addr); |
| 101 | extern void* kern_os_realloc(void* addr, size_t nsize); |
| 102 | |
| 103 | static void* cs_kern_os_calloc(size_t num, size_t size) |
| 104 | { |
| 105 | return kern_os_malloc(num * size); // malloc bzeroes the buffer |
| 106 | } |
| 107 | |
| 108 | cs_malloc_t cs_mem_malloc = kern_os_malloc; |
| 109 | cs_calloc_t cs_mem_calloc = cs_kern_os_calloc; |
| 110 | cs_realloc_t cs_mem_realloc = kern_os_realloc; |
| 111 | cs_free_t cs_mem_free = kern_os_free; |
| 112 | cs_vsnprintf_t cs_vsnprintf = vsnprintf; |
| 113 | #endif |
| 114 | #else |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 115 | cs_malloc_t cs_mem_malloc = NULL; |
| 116 | cs_calloc_t cs_mem_calloc = NULL; |
| 117 | cs_realloc_t cs_mem_realloc = NULL; |
| 118 | cs_free_t cs_mem_free = NULL; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 119 | cs_vsnprintf_t cs_vsnprintf = NULL; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 120 | #endif |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 121 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 122 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 123 | unsigned int CAPSTONE_API cs_version(int *major, int *minor) |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 124 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 125 | archs_enable(); |
| 126 | |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 127 | if (major != NULL && minor != NULL) { |
| 128 | *major = CS_API_MAJOR; |
| 129 | *minor = CS_API_MINOR; |
| 130 | } |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 131 | |
| 132 | return (CS_API_MAJOR << 8) + CS_API_MINOR; |
| 133 | } |
| 134 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 135 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 136 | bool CAPSTONE_API cs_support(int query) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 137 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 138 | archs_enable(); |
| 139 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 140 | if (query == CS_ARCH_ALL) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 141 | return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 142 | (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) | |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 143 | (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) | |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 144 | (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE)); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 145 | |
Nguyen Anh Quynh | 48d5832 | 2014-02-27 13:54:28 +0800 | [diff] [blame] | 146 | if ((unsigned int)query < CS_ARCH_MAX) |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 147 | return all_arch & (1 << query); |
| 148 | |
| 149 | if (query == CS_SUPPORT_DIET) { |
| 150 | #ifdef CAPSTONE_DIET |
| 151 | return true; |
| 152 | #else |
| 153 | return false; |
| 154 | #endif |
| 155 | } |
| 156 | |
Nguyen Anh Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 157 | if (query == CS_SUPPORT_X86_REDUCE) { |
| 158 | #if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE) |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 159 | return true; |
| 160 | #else |
| 161 | return false; |
| 162 | #endif |
| 163 | } |
| 164 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 165 | // unsupported query |
| 166 | return false; |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 169 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 170 | cs_err CAPSTONE_API cs_errno(csh handle) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 171 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 172 | struct cs_struct *ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 173 | if (!handle) |
| 174 | return CS_ERR_CSH; |
| 175 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 176 | ud = (struct cs_struct *)(uintptr_t)handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 177 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 178 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 181 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 182 | const char * CAPSTONE_API cs_strerror(cs_err code) |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 183 | { |
| 184 | switch(code) { |
| 185 | default: |
| 186 | return "Unknown error code"; |
| 187 | case CS_ERR_OK: |
| 188 | return "OK (CS_ERR_OK)"; |
| 189 | case CS_ERR_MEM: |
| 190 | return "Out of memory (CS_ERR_MEM)"; |
| 191 | case CS_ERR_ARCH: |
| 192 | return "Invalid architecture (CS_ERR_ARCH)"; |
| 193 | case CS_ERR_HANDLE: |
| 194 | return "Invalid handle (CS_ERR_HANDLE)"; |
| 195 | case CS_ERR_CSH: |
| 196 | return "Invalid csh (CS_ERR_CSH)"; |
| 197 | case CS_ERR_MODE: |
| 198 | return "Invalid mode (CS_ERR_MODE)"; |
| 199 | case CS_ERR_OPTION: |
| 200 | return "Invalid option (CS_ERR_OPTION)"; |
| 201 | case CS_ERR_DETAIL: |
| 202 | return "Details are unavailable (CS_ERR_DETAIL)"; |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 203 | case CS_ERR_MEMSETUP: |
| 204 | return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)"; |
Nguyen Anh Quynh | 4f22028 | 2014-02-09 00:19:15 +0800 | [diff] [blame] | 205 | case CS_ERR_VERSION: |
| 206 | return "Different API version between core & binding (CS_ERR_VERSION)"; |
Nguyen Anh Quynh | f7cdbdf | 2014-02-24 16:47:36 +0800 | [diff] [blame] | 207 | case CS_ERR_DIET: |
| 208 | return "Information irrelevant in diet engine (CS_ERR_DIET)"; |
Nguyen Anh Quynh | 11ec881 | 2014-04-10 17:20:01 +0800 | [diff] [blame] | 209 | case CS_ERR_SKIPDATA: |
| 210 | return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)"; |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 214 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 215 | cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 216 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 217 | cs_err err; |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 218 | struct cs_struct *ud; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 219 | if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf) |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 220 | // Error: before cs_open(), dynamic memory management must be initialized |
| 221 | // with cs_option(CS_OPT_MEM) |
| 222 | return CS_ERR_MEMSETUP; |
| 223 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 224 | archs_enable(); |
| 225 | |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 226 | if (arch < CS_ARCH_MAX && arch_init[arch]) { |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 227 | ud = cs_mem_calloc(1, sizeof(*ud)); |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 228 | if (!ud) { |
| 229 | // memory insufficient |
| 230 | return CS_ERR_MEM; |
| 231 | } |
| 232 | |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 233 | ud->errnum = CS_ERR_OK; |
| 234 | ud->arch = arch; |
| 235 | ud->mode = mode; |
tandasat | b3d38cf | 2016-05-06 17:03:57 -0700 | [diff] [blame^] | 236 | ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0; |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 237 | // by default, do not break instruction into details |
| 238 | ud->detail = CS_OPT_OFF; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 239 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 240 | // default skipdata setup |
| 241 | ud->skipdata_setup.mnemonic = SKIPDATA_MNEM; |
| 242 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 243 | err = arch_init[ud->arch](ud); |
Nguyen Anh Quynh | 06b3c05 | 2014-01-21 15:26:02 +0800 | [diff] [blame] | 244 | if (err) { |
| 245 | cs_mem_free(ud); |
| 246 | *handle = 0; |
| 247 | return err; |
| 248 | } |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 249 | |
| 250 | *handle = (uintptr_t)ud; |
| 251 | |
| 252 | return CS_ERR_OK; |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 253 | } else { |
| 254 | *handle = 0; |
| 255 | return CS_ERR_ARCH; |
| 256 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 257 | } |
| 258 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 259 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 260 | cs_err CAPSTONE_API cs_close(csh *handle) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 261 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 262 | struct cs_struct *ud; |
| 263 | |
Nguyen Anh Quynh | ef3d04d | 2014-02-27 23:42:49 +0800 | [diff] [blame] | 264 | if (*handle == 0) |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 265 | // invalid handle |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 266 | return CS_ERR_CSH; |
| 267 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 268 | ud = (struct cs_struct *)(*handle); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 269 | |
Nguyen Anh Quynh | fbe10a5 | 2014-02-27 23:59:08 +0800 | [diff] [blame] | 270 | if (ud->printer_info) |
| 271 | cs_mem_free(ud->printer_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 272 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 273 | cs_mem_free(ud->insn_cache); |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 274 | |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 275 | memset(ud, 0, sizeof(*ud)); |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 276 | cs_mem_free(ud); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 277 | |
Nguyen Anh Quynh | c8e0785 | 2014-02-28 09:38:11 +0800 | [diff] [blame] | 278 | // invalidate this handle by ZERO out its value. |
| 279 | // this is to make sure it is unusable after cs_close() |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 280 | *handle = 0; |
| 281 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 282 | return CS_ERR_OK; |
| 283 | } |
| 284 | |
| 285 | // fill insn with mnemonic & operands info |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 286 | static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 287 | PostPrinter_t postprinter, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 288 | { |
Nguyen Anh Quynh | 8c2e2db | 2014-05-14 07:32:56 +0800 | [diff] [blame] | 289 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 290 | char *sp, *mnem; |
Nguyen Anh Quynh | 8c2e2db | 2014-05-14 07:32:56 +0800 | [diff] [blame] | 291 | #endif |
tandasat | a6609fe | 2016-05-04 05:54:28 -0700 | [diff] [blame] | 292 | uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 293 | |
Nguyen Anh Quynh | 6c0dd63 | 2014-10-30 20:34:22 +0800 | [diff] [blame] | 294 | // fill the instruction bytes. |
| 295 | // we might skip some redundant bytes in front in the case of X86 |
| 296 | memcpy(insn->bytes, code + insn->size - copy_size, copy_size); |
| 297 | insn->size = copy_size; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 298 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 299 | // alias instruction might have ID saved in OpcodePub |
| 300 | if (MCInst_getOpcodePub(mci)) |
| 301 | insn->id = MCInst_getOpcodePub(mci); |
| 302 | |
| 303 | // post printer handles some corner cases (hacky) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 304 | if (postprinter) |
Nguyen Anh Quynh | 6456481 | 2014-05-19 16:46:31 +0800 | [diff] [blame] | 305 | postprinter((csh)handle, insn, buffer, mci); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 306 | |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 307 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 308 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 309 | // find first space or tab |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 310 | sp = buffer; |
Nguyen Anh Quynh | 7566a2d | 2014-06-08 22:09:31 +0800 | [diff] [blame] | 311 | mnem = insn->mnemonic; |
Nguyen Anh Quynh | 25b7f76 | 2014-07-02 12:22:39 +0800 | [diff] [blame] | 312 | for (sp = buffer; *sp; sp++) { |
| 313 | if (*sp == ' '|| *sp == '\t') |
| 314 | break; |
| 315 | if (*sp == '|') // lock|rep prefix for x86 |
| 316 | *sp = ' '; |
| 317 | // copy to @mnemonic |
| 318 | *mnem = *sp; |
| 319 | mnem++; |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 320 | } |
| 321 | |
Nguyen Anh Quynh | 7566a2d | 2014-06-08 22:09:31 +0800 | [diff] [blame] | 322 | *mnem = '\0'; |
| 323 | |
| 324 | // copy @op_str |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 325 | if (*sp) { |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 326 | // find the next non-space char |
| 327 | sp++; |
| 328 | for (; ((*sp == ' ') || (*sp == '\t')); sp++); |
| 329 | strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 330 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 331 | } else |
| 332 | insn->op_str[0] = '\0'; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 333 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 334 | } |
| 335 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 336 | // how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)? |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 337 | // this very much depends on instruction alignment requirement of each arch. |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 338 | static uint8_t skipdata_size(cs_struct *handle) |
| 339 | { |
| 340 | switch(handle->arch) { |
| 341 | default: |
| 342 | // should never reach |
| 343 | return -1; |
| 344 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 345 | // skip 2 bytes on Thumb mode. |
| 346 | if (handle->mode & CS_MODE_THUMB) |
| 347 | return 2; |
| 348 | // otherwise, skip 4 bytes |
| 349 | return 4; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 350 | case CS_ARCH_ARM64: |
| 351 | case CS_ARCH_MIPS: |
| 352 | case CS_ARCH_PPC: |
| 353 | case CS_ARCH_SPARC: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 354 | // skip 4 bytes |
| 355 | return 4; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 356 | case CS_ARCH_SYSZ: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 357 | // SystemZ instruction's length can be 2, 4 or 6 bytes, |
| 358 | // so we just skip 2 bytes |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 359 | return 2; |
| 360 | case CS_ARCH_X86: |
| 361 | // X86 has no restriction on instruction alignment |
| 362 | return 1; |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 363 | case CS_ARCH_XCORE: |
| 364 | // XCore instruction's length can be 2 or 4 bytes, |
| 365 | // so we just skip 2 bytes |
| 366 | return 2; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 370 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 371 | cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value) |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 372 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 373 | struct cs_struct *handle; |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 374 | archs_enable(); |
| 375 | |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 376 | // cs_option() can be called with NULL handle just for CS_OPT_MEM |
| 377 | // This is supposed to be executed before all other APIs (even cs_open()) |
| 378 | if (type == CS_OPT_MEM) { |
| 379 | cs_opt_mem *mem = (cs_opt_mem *)value; |
| 380 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 381 | cs_mem_malloc = mem->malloc; |
| 382 | cs_mem_calloc = mem->calloc; |
| 383 | cs_mem_realloc = mem->realloc; |
| 384 | cs_mem_free = mem->free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 385 | cs_vsnprintf = mem->vsnprintf; |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 386 | |
| 387 | return CS_ERR_OK; |
| 388 | } |
| 389 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 390 | handle = (struct cs_struct *)(uintptr_t)ud; |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 391 | if (!handle) |
| 392 | return CS_ERR_CSH; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 393 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 394 | switch(type) { |
| 395 | default: |
| 396 | break; |
| 397 | case CS_OPT_DETAIL: |
Félix Cloutier | c141af9 | 2015-03-02 22:06:56 -0500 | [diff] [blame] | 398 | handle->detail = (cs_opt_value)value; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 399 | return CS_ERR_OK; |
| 400 | case CS_OPT_SKIPDATA: |
| 401 | handle->skipdata = (value == CS_OPT_ON); |
| 402 | if (handle->skipdata) { |
| 403 | if (handle->skipdata_size == 0) { |
| 404 | // set the default skipdata size |
| 405 | handle->skipdata_size = skipdata_size(handle); |
| 406 | } |
| 407 | } |
| 408 | return CS_ERR_OK; |
| 409 | case CS_OPT_SKIPDATA_SETUP: |
| 410 | if (value) |
| 411 | handle->skipdata_setup = *((cs_opt_skipdata *)value); |
| 412 | return CS_ERR_OK; |
Nguyen Anh Quynh | 7d02c92 | 2013-12-21 09:59:31 +0800 | [diff] [blame] | 413 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 414 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 415 | return arch_option[handle->arch](handle, type, value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 416 | } |
| 417 | |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 418 | // generate @op_str for data instruction of SKIPDATA |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 419 | static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size) |
| 420 | { |
| 421 | char *p = opstr; |
| 422 | int len; |
| 423 | size_t i; |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 424 | size_t available = sizeof(((cs_insn*)NULL)->op_str); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 425 | |
| 426 | if (!size) { |
| 427 | opstr[0] = '\0'; |
| 428 | return; |
| 429 | } |
| 430 | |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 431 | len = cs_snprintf(p, available, "0x%02x", buffer[0]); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 432 | p+= len; |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 433 | available -= len; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 434 | |
| 435 | for(i = 1; i < size; i++) { |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 436 | len = cs_snprintf(p, available, ", 0x%02x", buffer[i]); |
| 437 | if (len < 0) { |
| 438 | break; |
| 439 | } |
| 440 | if ((size_t)len > available - 1) { |
| 441 | break; |
| 442 | } |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 443 | p+= len; |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 444 | available -= len; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 448 | // dynamicly allocate memory to contain disasm insn |
| 449 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 450 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 451 | size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 452 | { |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 453 | struct cs_struct *handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 454 | MCInst mci; |
| 455 | uint16_t insn_size; |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 456 | size_t c = 0, i; |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 457 | unsigned int f = 0; // index of the next instruction in the cache |
| 458 | cs_insn *insn_cache; // cache contains disassembled instructions |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 459 | void *total = NULL; |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 460 | size_t total_size = 0; // total size of output buffer containing all insns |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 461 | bool r; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 462 | void *tmp; |
| 463 | size_t skipdata_bytes; |
Nguyen Anh Quynh | 523ca99 | 2014-10-01 10:46:37 +0800 | [diff] [blame] | 464 | uint64_t offset_org; // save all the original info of the buffer |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 465 | size_t size_org; |
| 466 | const uint8_t *buffer_org; |
Nguyen Anh Quynh | ac98ca0 | 2014-09-30 13:17:36 +0800 | [diff] [blame] | 467 | unsigned int cache_size = INSN_CACHE_SIZE; |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 468 | size_t next_offset; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 469 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 470 | handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 5848aaa | 2014-02-27 11:10:41 +0800 | [diff] [blame] | 471 | if (!handle) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 472 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 473 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 477 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 478 | |
Nguyen Anh Quynh | c2cb0e7 | 2016-04-27 14:43:10 +0800 | [diff] [blame] | 479 | // reset IT block of ARM structure |
Nguyen Anh Quynh | 0c86440 | 2016-04-27 14:47:05 +0800 | [diff] [blame] | 480 | if (handle->arch == CS_ARCH_ARM) |
| 481 | handle->ITBlock.size = 0; |
Nguyen Anh Quynh | c2cb0e7 | 2016-04-27 14:43:10 +0800 | [diff] [blame] | 482 | |
Nguyen Anh Quynh | 50eeba2 | 2014-09-30 13:28:02 +0800 | [diff] [blame] | 483 | #ifdef CAPSTONE_USE_SYS_DYN_MEM |
Nguyen Anh Quynh | 16f330c | 2014-10-02 10:09:59 +0800 | [diff] [blame] | 484 | if (count > 0 && count <= INSN_CACHE_SIZE) |
flyingsymbols | d91f964 | 2014-10-22 03:21:43 -0400 | [diff] [blame] | 485 | cache_size = (unsigned int) count; |
Nguyen Anh Quynh | 50eeba2 | 2014-09-30 13:28:02 +0800 | [diff] [blame] | 486 | #endif |
Nguyen Anh Quynh | ac98ca0 | 2014-09-30 13:17:36 +0800 | [diff] [blame] | 487 | |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 488 | // save the original offset for SKIPDATA |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 489 | buffer_org = buffer; |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 490 | offset_org = offset; |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 491 | size_org = size; |
Nguyen Anh Quynh | 523ca99 | 2014-10-01 10:46:37 +0800 | [diff] [blame] | 492 | |
| 493 | total_size = sizeof(cs_insn) * cache_size; |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 494 | total = cs_mem_malloc(total_size); |
Nguyen Anh Quynh | 611b0c5 | 2014-12-15 11:22:46 +0800 | [diff] [blame] | 495 | if (total == NULL) { |
| 496 | // insufficient memory |
Edward Williamson | f1e4975 | 2014-12-14 20:45:19 -0500 | [diff] [blame] | 497 | handle->errnum = CS_ERR_MEM; |
| 498 | return 0; |
| 499 | } |
Nguyen Anh Quynh | 611b0c5 | 2014-12-15 11:22:46 +0800 | [diff] [blame] | 500 | |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 501 | insn_cache = total; |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 502 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 503 | while (size > 0) { |
Nguyen Anh Quynh | 495295e | 2014-06-16 15:54:32 +0800 | [diff] [blame] | 504 | MCInst_Init(&mci); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 505 | mci.csh = handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 506 | |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 507 | // relative branches need to know the address & size of current insn |
| 508 | mci.address = offset; |
| 509 | |
| 510 | if (handle->detail) { |
| 511 | // allocate memory for @detail pointer |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 512 | insn_cache->detail = cs_mem_malloc(sizeof(cs_detail)); |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 513 | } else { |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 514 | insn_cache->detail = NULL; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 518 | mci.flat_insn = insn_cache; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 519 | mci.flat_insn->address = offset; |
Nguyen Anh Quynh | 0c07cc9 | 2014-08-27 22:31:54 +0800 | [diff] [blame] | 520 | #ifdef CAPSTONE_DIET |
| 521 | // zero out mnemonic & op_str |
| 522 | mci.flat_insn->mnemonic[0] = '\0'; |
| 523 | mci.flat_insn->op_str[0] = '\0'; |
| 524 | #endif |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 525 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 526 | r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 527 | if (r) { |
| 528 | SStream ss; |
| 529 | SStream_Init(&ss); |
| 530 | |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 531 | mci.flat_insn->size = insn_size; |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 532 | |
| 533 | // map internal instruction opcode to public insn ID |
| 534 | handle->insn_id(handle, insn_cache, mci.Opcode); |
| 535 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 536 | handle->printer(&mci, &ss, handle->printer_info); |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 537 | |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 538 | fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 539 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 540 | next_offset = insn_size; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 541 | } else { |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 542 | // encounter a broken instruction |
| 543 | |
| 544 | // free memory of @detail pointer |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 545 | if (handle->detail) { |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 546 | cs_mem_free(insn_cache->detail); |
| 547 | } |
| 548 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 549 | // if there is no request to skip data, or remaining data is too small, |
| 550 | // then bail out |
| 551 | if (!handle->skipdata || handle->skipdata_size > size) |
| 552 | break; |
| 553 | |
| 554 | if (handle->skipdata_setup.callback) { |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 555 | skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org, |
Nguyen Anh Quynh | 4b6b15f | 2014-08-26 15:57:04 +0800 | [diff] [blame] | 556 | (size_t)(offset - offset_org), handle->skipdata_setup.user_data); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 557 | if (skipdata_bytes > size) |
| 558 | // remaining data is not enough |
| 559 | break; |
| 560 | |
| 561 | if (!skipdata_bytes) |
| 562 | // user requested not to skip data, so bail out |
| 563 | break; |
| 564 | } else |
| 565 | skipdata_bytes = handle->skipdata_size; |
| 566 | |
| 567 | // we have to skip some amount of data, depending on arch & mode |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 568 | insn_cache->id = 0; // invalid ID for this "data" instruction |
| 569 | insn_cache->address = offset; |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 570 | insn_cache->size = (uint16_t)skipdata_bytes; |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 571 | memcpy(insn_cache->bytes, buffer, skipdata_bytes); |
| 572 | strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic, |
| 573 | sizeof(insn_cache->mnemonic) - 1); |
| 574 | skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes); |
| 575 | insn_cache->detail = NULL; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 576 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 577 | next_offset = skipdata_bytes; |
| 578 | } |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 579 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 580 | // one more instruction entering the cache |
| 581 | f++; |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 582 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 583 | // one more instruction disassembled |
| 584 | c++; |
| 585 | if (count > 0 && c == count) |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 586 | // already got requested number of instructions |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 587 | break; |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 588 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 589 | if (f == cache_size) { |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 590 | // full cache, so expand the cache to contain incoming insns |
danghvu | 2fb7c8e | 2014-10-02 07:38:24 -0500 | [diff] [blame] | 591 | cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 592 | total_size += (sizeof(cs_insn) * cache_size); |
| 593 | tmp = cs_mem_realloc(total, total_size); |
| 594 | if (tmp == NULL) { // insufficient memory |
| 595 | if (handle->detail) { |
| 596 | insn_cache = (cs_insn *)total; |
| 597 | for (i = 0; i < c; i++, insn_cache++) |
| 598 | cs_mem_free(insn_cache->detail); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 599 | } |
| 600 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 601 | cs_mem_free(total); |
| 602 | *insn = NULL; |
| 603 | handle->errnum = CS_ERR_MEM; |
| 604 | return 0; |
| 605 | } |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 606 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 607 | total = tmp; |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 608 | // continue to fill in the cache after the last instruction |
| 609 | insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 610 | |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 611 | // reset f back to 0, so we fill in the cache from begining |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 612 | f = 0; |
| 613 | } else |
| 614 | insn_cache++; |
| 615 | |
| 616 | buffer += next_offset; |
| 617 | size -= next_offset; |
| 618 | offset += next_offset; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 619 | } |
| 620 | |
Nguyen Anh Quynh | 27a4a08 | 2014-10-02 10:31:37 +0800 | [diff] [blame] | 621 | if (!c) { |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 622 | // we did not disassemble any instruction |
Nguyen Anh Quynh | 27a4a08 | 2014-10-02 10:31:37 +0800 | [diff] [blame] | 623 | cs_mem_free(total); |
| 624 | total = NULL; |
| 625 | } else if (f != cache_size) { |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 626 | // total did not fully use the last cache, so downsize it |
Tyler J. Stachecki | 30d1167 | 2015-10-23 20:59:20 -0400 | [diff] [blame] | 627 | tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 628 | if (tmp == NULL) { // insufficient memory |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 629 | // free all detail pointers |
| 630 | if (handle->detail) { |
| 631 | insn_cache = (cs_insn *)total; |
| 632 | for (i = 0; i < c; i++, insn_cache++) |
| 633 | cs_mem_free(insn_cache->detail); |
| 634 | } |
| 635 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 636 | cs_mem_free(total); |
Nguyen Anh Quynh | dab17fd | 2014-06-19 11:15:18 +0800 | [diff] [blame] | 637 | *insn = NULL; |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 638 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 639 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | total = tmp; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | *insn = total; |
| 647 | |
| 648 | return c; |
| 649 | } |
| 650 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 651 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 652 | CAPSTONE_DEPRECATED |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 653 | size_t CAPSTONE_API cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 654 | { |
| 655 | return cs_disasm(ud, buffer, size, offset, count, insn); |
| 656 | } |
| 657 | |
| 658 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 659 | void CAPSTONE_API cs_free(cs_insn *insn, size_t count) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 660 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 661 | size_t i; |
| 662 | |
| 663 | // free all detail pointers |
| 664 | for (i = 0; i < count; i++) |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 665 | cs_mem_free(insn[i].detail); |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 666 | |
| 667 | // then free pointer to cs_insn array |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 668 | cs_mem_free(insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 669 | } |
| 670 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 671 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 672 | cs_insn * CAPSTONE_API cs_malloc(csh ud) |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 673 | { |
| 674 | cs_insn *insn; |
| 675 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
| 676 | |
| 677 | insn = cs_mem_malloc(sizeof(cs_insn)); |
| 678 | if (!insn) { |
| 679 | // insufficient memory |
| 680 | handle->errnum = CS_ERR_MEM; |
| 681 | return NULL; |
| 682 | } else { |
| 683 | if (handle->detail) { |
| 684 | // allocate memory for @detail pointer |
| 685 | insn->detail = cs_mem_malloc(sizeof(cs_detail)); |
| 686 | if (insn->detail == NULL) { // insufficient memory |
| 687 | cs_mem_free(insn); |
| 688 | handle->errnum = CS_ERR_MEM; |
| 689 | return NULL; |
| 690 | } |
| 691 | } else |
| 692 | insn->detail = NULL; |
| 693 | } |
| 694 | |
| 695 | return insn; |
| 696 | } |
| 697 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 698 | // iterator for instruction "single-stepping" |
| 699 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 700 | bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size, |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 701 | uint64_t *address, cs_insn *insn) |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 702 | { |
| 703 | struct cs_struct *handle; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 704 | uint16_t insn_size; |
| 705 | MCInst mci; |
| 706 | bool r; |
| 707 | |
| 708 | handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 709 | if (!handle) { |
Nguyen Anh Quynh | 29ce6c3 | 2014-10-12 15:28:34 +0800 | [diff] [blame] | 710 | return false; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | handle->errnum = CS_ERR_OK; |
| 714 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 715 | MCInst_Init(&mci); |
| 716 | mci.csh = handle; |
| 717 | |
| 718 | // relative branches need to know the address & size of current insn |
| 719 | mci.address = *address; |
| 720 | |
| 721 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 722 | mci.flat_insn = insn; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 723 | mci.flat_insn->address = *address; |
| 724 | #ifdef CAPSTONE_DIET |
| 725 | // zero out mnemonic & op_str |
| 726 | mci.flat_insn->mnemonic[0] = '\0'; |
| 727 | mci.flat_insn->op_str[0] = '\0'; |
| 728 | #endif |
| 729 | |
| 730 | r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info); |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 731 | if (r) { |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 732 | SStream ss; |
| 733 | SStream_Init(&ss); |
| 734 | |
| 735 | mci.flat_insn->size = insn_size; |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 736 | |
| 737 | // map internal instruction opcode to public insn ID |
| 738 | handle->insn_id(handle, insn, mci.Opcode); |
| 739 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 740 | handle->printer(&mci, &ss, handle->printer_info); |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 741 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 742 | fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code); |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 743 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 744 | *code += insn_size; |
| 745 | *size -= insn_size; |
| 746 | *address += insn_size; |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 747 | } else { // encounter a broken instruction |
| 748 | size_t skipdata_bytes; |
| 749 | |
| 750 | // if there is no request to skip data, or remaining data is too small, |
| 751 | // then bail out |
| 752 | if (!handle->skipdata || handle->skipdata_size > *size) |
| 753 | return false; |
| 754 | |
| 755 | if (handle->skipdata_setup.callback) { |
| 756 | skipdata_bytes = handle->skipdata_setup.callback(*code, *size, |
| 757 | 0, handle->skipdata_setup.user_data); |
| 758 | if (skipdata_bytes > *size) |
| 759 | // remaining data is not enough |
| 760 | return false; |
| 761 | |
| 762 | if (!skipdata_bytes) |
| 763 | // user requested not to skip data, so bail out |
| 764 | return false; |
| 765 | } else |
| 766 | skipdata_bytes = handle->skipdata_size; |
| 767 | |
| 768 | // we have to skip some amount of data, depending on arch & mode |
| 769 | insn->id = 0; // invalid ID for this "data" instruction |
| 770 | insn->address = *address; |
| 771 | insn->size = (uint16_t)skipdata_bytes; |
| 772 | memcpy(insn->bytes, *code, skipdata_bytes); |
| 773 | strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic, |
| 774 | sizeof(insn->mnemonic) - 1); |
| 775 | skipdata_opstr(insn->op_str, *code, skipdata_bytes); |
| 776 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 777 | *code += skipdata_bytes; |
| 778 | *size -= skipdata_bytes; |
| 779 | *address += skipdata_bytes; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 780 | } |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 781 | |
| 782 | return true; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 785 | // return friendly name of regiser in a string |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 786 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 787 | const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 788 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 789 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 790 | |
| 791 | if (!handle || handle->reg_name == NULL) { |
| 792 | return NULL; |
| 793 | } |
| 794 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 795 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 796 | } |
| 797 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 798 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 799 | const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 800 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 801 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 802 | |
| 803 | if (!handle || handle->insn_name == NULL) { |
| 804 | return NULL; |
| 805 | } |
| 806 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 807 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 808 | } |
| 809 | |
Nguyen Anh Quynh | 650f96c | 2014-07-08 08:59:27 +0800 | [diff] [blame] | 810 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 811 | const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group) |
Nguyen Anh Quynh | 650f96c | 2014-07-08 08:59:27 +0800 | [diff] [blame] | 812 | { |
| 813 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
| 814 | |
| 815 | if (!handle || handle->group_name == NULL) { |
| 816 | return NULL; |
| 817 | } |
| 818 | |
| 819 | return handle->group_name(ud, group); |
| 820 | } |
| 821 | |
Nguyen Anh Quynh | 7008356 | 2013-12-20 22:02:20 +0800 | [diff] [blame] | 822 | static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 823 | { |
| 824 | int i; |
| 825 | |
| 826 | for (i = 0; i < max; i++) { |
| 827 | if (arr[i] == id) |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | return false; |
| 832 | } |
| 833 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 834 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 835 | bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 836 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 837 | struct cs_struct *handle; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 838 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 839 | return false; |
| 840 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 841 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 842 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 843 | if (!handle->detail) { |
| 844 | handle->errnum = CS_ERR_DETAIL; |
| 845 | return false; |
| 846 | } |
| 847 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 848 | if(!insn->id) { |
| 849 | handle->errnum = CS_ERR_SKIPDATA; |
| 850 | return false; |
| 851 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 852 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 853 | if(!insn->detail) { |
| 854 | handle->errnum = CS_ERR_DETAIL; |
| 855 | return false; |
| 856 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 857 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 858 | return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 859 | } |
| 860 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 861 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 862 | bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 863 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 864 | struct cs_struct *handle; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 865 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 866 | return false; |
| 867 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 868 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 869 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 870 | if (!handle->detail) { |
| 871 | handle->errnum = CS_ERR_DETAIL; |
| 872 | return false; |
| 873 | } |
| 874 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 875 | if(!insn->id) { |
| 876 | handle->errnum = CS_ERR_SKIPDATA; |
| 877 | return false; |
| 878 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 879 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 880 | if(!insn->detail) { |
| 881 | handle->errnum = CS_ERR_DETAIL; |
| 882 | return false; |
| 883 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 884 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 885 | return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 886 | } |
| 887 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 888 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 889 | bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 890 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 891 | struct cs_struct *handle; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 892 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 893 | return false; |
| 894 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 895 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 896 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 897 | if (!handle->detail) { |
| 898 | handle->errnum = CS_ERR_DETAIL; |
| 899 | return false; |
| 900 | } |
| 901 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 902 | if(!insn->id) { |
| 903 | handle->errnum = CS_ERR_SKIPDATA; |
| 904 | return false; |
| 905 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 906 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 907 | if(!insn->detail) { |
| 908 | handle->errnum = CS_ERR_DETAIL; |
| 909 | return false; |
| 910 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 911 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 912 | return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 913 | } |
| 914 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 915 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 916 | int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 917 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 918 | struct cs_struct *handle; |
| 919 | unsigned int count = 0, i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 920 | if (!ud) |
| 921 | return -1; |
| 922 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 923 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 924 | |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 925 | if (!handle->detail) { |
| 926 | handle->errnum = CS_ERR_DETAIL; |
| 927 | return -1; |
| 928 | } |
| 929 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 930 | if(!insn->id) { |
| 931 | handle->errnum = CS_ERR_SKIPDATA; |
| 932 | return -1; |
| 933 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 934 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 935 | if(!insn->detail) { |
| 936 | handle->errnum = CS_ERR_DETAIL; |
| 937 | return -1; |
| 938 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 939 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 940 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 941 | |
| 942 | switch (handle->arch) { |
| 943 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 944 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 945 | return -1; |
| 946 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 947 | for (i = 0; i < insn->detail->arm.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 948 | if (insn->detail->arm.operands[i].type == (arm_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 949 | count++; |
| 950 | break; |
| 951 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 952 | for (i = 0; i < insn->detail->arm64.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 953 | if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 954 | count++; |
| 955 | break; |
| 956 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 957 | for (i = 0; i < insn->detail->x86.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 958 | if (insn->detail->x86.operands[i].type == (x86_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 959 | count++; |
| 960 | break; |
| 961 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 962 | for (i = 0; i < insn->detail->mips.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 963 | if (insn->detail->mips.operands[i].type == (mips_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 964 | count++; |
| 965 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 966 | case CS_ARCH_PPC: |
| 967 | for (i = 0; i < insn->detail->ppc.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 968 | if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type) |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 969 | count++; |
| 970 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 971 | case CS_ARCH_SPARC: |
| 972 | for (i = 0; i < insn->detail->sparc.op_count; i++) |
| 973 | if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type) |
| 974 | count++; |
| 975 | break; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 976 | case CS_ARCH_SYSZ: |
| 977 | for (i = 0; i < insn->detail->sysz.op_count; i++) |
| 978 | if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type) |
| 979 | count++; |
| 980 | break; |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 981 | case CS_ARCH_XCORE: |
| 982 | for (i = 0; i < insn->detail->xcore.op_count; i++) |
| 983 | if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type) |
| 984 | count++; |
| 985 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | return count; |
| 989 | } |
| 990 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 991 | CAPSTONE_EXPORT |
tandasat | 760940f | 2016-03-31 18:14:43 -0700 | [diff] [blame] | 992 | int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 993 | unsigned int post) |
| 994 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 995 | struct cs_struct *handle; |
| 996 | unsigned int count = 0, i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 997 | if (!ud) |
| 998 | return -1; |
| 999 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 1000 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 1001 | |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 1002 | if (!handle->detail) { |
| 1003 | handle->errnum = CS_ERR_DETAIL; |
| 1004 | return -1; |
| 1005 | } |
| 1006 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 1007 | if(!insn->id) { |
| 1008 | handle->errnum = CS_ERR_SKIPDATA; |
| 1009 | return -1; |
| 1010 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 1011 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 1012 | if(!insn->detail) { |
| 1013 | handle->errnum = CS_ERR_DETAIL; |
| 1014 | return -1; |
| 1015 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 1016 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 1017 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1018 | |
| 1019 | switch (handle->arch) { |
| 1020 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 1021 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1022 | return -1; |
| 1023 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 1024 | for (i = 0; i < insn->detail->arm.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 1025 | if (insn->detail->arm.operands[i].type == (arm_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1026 | count++; |
| 1027 | if (count == post) |
| 1028 | return i; |
| 1029 | } |
| 1030 | break; |
| 1031 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 1032 | for (i = 0; i < insn->detail->arm64.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 1033 | if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1034 | count++; |
| 1035 | if (count == post) |
| 1036 | return i; |
| 1037 | } |
| 1038 | break; |
| 1039 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 1040 | for (i = 0; i < insn->detail->x86.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 1041 | if (insn->detail->x86.operands[i].type == (x86_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1042 | count++; |
| 1043 | if (count == post) |
| 1044 | return i; |
| 1045 | } |
| 1046 | break; |
| 1047 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 1048 | for (i = 0; i < insn->detail->mips.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 1049 | if (insn->detail->mips.operands[i].type == (mips_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1050 | count++; |
| 1051 | if (count == post) |
| 1052 | return i; |
| 1053 | } |
| 1054 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 1055 | case CS_ARCH_PPC: |
| 1056 | for (i = 0; i < insn->detail->ppc.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 1057 | if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type) |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 1058 | count++; |
| 1059 | if (count == post) |
| 1060 | return i; |
| 1061 | } |
| 1062 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 1063 | case CS_ARCH_SPARC: |
| 1064 | for (i = 0; i < insn->detail->sparc.op_count; i++) { |
| 1065 | if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type) |
| 1066 | count++; |
| 1067 | if (count == post) |
| 1068 | return i; |
| 1069 | } |
| 1070 | break; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 1071 | case CS_ARCH_SYSZ: |
| 1072 | for (i = 0; i < insn->detail->sysz.op_count; i++) { |
| 1073 | if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type) |
| 1074 | count++; |
| 1075 | if (count == post) |
| 1076 | return i; |
| 1077 | } |
| 1078 | break; |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 1079 | case CS_ARCH_XCORE: |
| 1080 | for (i = 0; i < insn->detail->xcore.op_count; i++) { |
| 1081 | if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type) |
| 1082 | count++; |
| 1083 | if (count == post) |
| 1084 | return i; |
| 1085 | } |
| 1086 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | return -1; |
| 1090 | } |