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