| 1 |
Attribute VB_Name = "mCapStone" |
| 2 |
Option Explicit |
| 3 |
|
| 4 |
'Capstone Disassembly Engine bindings for VB6 |
| 5 |
'Contributed by FireEye FLARE Team |
| 6 |
'Author: David Zimmer <[email protected]>, <[email protected]> |
| 7 |
'License: Apache 2.0 |
| 8 |
'Copyright: FireEye 2017 |
| 9 |
|
| 10 |
'todo: cs_disasm_iter / skipdata |
| 11 |
|
| 12 |
'this is for my vb code and how much info it spits out in tostring methods.. |
| 13 |
Global Const DEBUG_DUMP = 0 |
| 14 |
|
| 15 |
'Architecture type |
| 16 |
Public Enum cs_arch |
| 17 |
CS_ARCH_ARM = 0 ' ARM architecture (including Thumb, Thumb-2) |
| 18 |
CS_ARCH_ARM64 ' ARM-64, also called AArch64 |
| 19 |
CS_ARCH_MIPS ' Mips architecture |
| 20 |
CS_ARCH_X86 ' X86 architecture (including x86 & x86-64) |
| 21 |
CS_ARCH_PPC ' PowerPC architecture |
| 22 |
CS_ARCH_SPARC ' Sparc architecture |
| 23 |
CS_ARCH_SYSZ ' SystemZ architecture |
| 24 |
CS_ARCH_XCORE ' XCore architecture |
| 25 |
CS_ARCH_MAX |
| 26 |
CS_ARCH_ALL = &HFFFF ' All architectures - for cs_support() |
| 27 |
End Enum |
| 28 |
|
| 29 |
Public Enum cs_mode |
| 30 |
CS_MODE_LITTLE_ENDIAN = 0 ' little-endian mode (default mode) |
| 31 |
CS_MODE_ARM = 0 ' 32-bit ARM |
| 32 |
CS_MODE_16 = 2 ' 16-bit mode (X86) |
| 33 |
CS_MODE_32 = 4 ' 32-bit mode (X86) |
| 34 |
CS_MODE_64 = 8 ' 64-bit mode (X86, PPC) |
| 35 |
CS_MODE_THUMB = 16 ' ARM's Thumb mode, including Thumb-2 |
| 36 |
CS_MODE_MCLASS = 32 ' ARM's Cortex-M series |
| 37 |
CS_MODE_V8 = 64 ' ARMv8 A32 encodings for ARM |
| 38 |
CS_MODE_MICRO = 16 ' MicroMips mode (MIPS) |
| 39 |
CS_MODE_MIPS3 = 32 ' Mips III ISA |
| 40 |
CS_MODE_MIPS32R6 = 64 ' Mips32r6 ISA |
| 41 |
CS_MODE_MIPSGP64 = 128 ' General Purpose Registers are 64-bit wide (MIPS) |
| 42 |
CS_MODE_V9 = 16 ' SparcV9 mode (Sparc) |
| 43 |
CS_MODE_BIG_ENDIAN = &H80000000 ' big-endian mode |
| 44 |
CS_MODE_MIPS32 = CS_MODE_32 ' Mips32 ISA (Mips) |
| 45 |
CS_MODE_MIPS64 = CS_MODE_64 ' Mips64 ISA (Mips) |
| 46 |
End Enum |
| 47 |
|
| 48 |
'Runtime option for the disassembled engine |
| 49 |
Public Enum cs_opt_type |
| 50 |
CS_OPT_SYNTAX = 1 ' Assembly output syntax |
| 51 |
CS_OPT_DETAIL ' Break down instruction structure into details |
| 52 |
CS_OPT_MODE ' Change engine's mode at run-time |
| 53 |
CS_OPT_MEM ' User-defined dynamic memory related functions |
| 54 |
CS_OPT_SKIPDATA ' Skip data when disassembling. Then engine is in SKIPDATA mode. |
| 55 |
CS_OPT_SKIPDATA_SETUP ' Setup user-defined function for SKIPDATA option |
| 56 |
End Enum |
| 57 |
|
| 58 |
|
| 59 |
'Runtime option value (associated with option type above) |
| 60 |
Public Enum cs_opt_value |
| 61 |
CS_OPT_OFF = 0 ' Turn OFF an option - default option of CS_OPT_DETAIL, CS_OPT_SKIPDATA. |
| 62 |
CS_OPT_ON = 3 ' Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA). |
| 63 |
CS_OPT_SYNTAX_DEFAULT = 0 ' Default asm syntax (CS_OPT_SYNTAX). |
| 64 |
CS_OPT_SYNTAX_INTEL ' X86 Intel asm syntax - default on X86 (CS_OPT_SYNTAX). |
| 65 |
CS_OPT_SYNTAX_ATT ' X86 ATT asm syntax (CS_OPT_SYNTAX). |
| 66 |
CS_OPT_SYNTAX_NOREGNAME ' Prints register name with only number (CS_OPT_SYNTAX) |
| 67 |
End Enum |
| 68 |
|
| 69 |
'Common instruction operand types - to be consistent across all architectures. |
| 70 |
Public Enum cs_op_type |
| 71 |
CS_OP_INVALID = 0 ' uninitialized/invalid operand. |
| 72 |
CS_OP_REG ' Register operand. |
| 73 |
CS_OP_IMM ' Immediate operand. |
| 74 |
CS_OP_MEM ' Memory operand. |
| 75 |
CS_OP_FP ' Floating-Point operand. |
| 76 |
End Enum |
| 77 |
|
| 78 |
'Common instruction groups - to be consistent across all architectures. |
| 79 |
Public Enum cs_group_type |
| 80 |
CS_GRP_INVALID = 0 ' uninitialized/invalid group. |
| 81 |
CS_GRP_JUMP ' all jump instructions (conditional+direct+indirect jumps) |
| 82 |
CS_GRP_CALL ' all call instructions |
| 83 |
CS_GRP_RET ' all return instructions |
| 84 |
CS_GRP_INT ' all interrupt instructions (int+syscall) |
| 85 |
CS_GRP_IRET ' all interrupt return instructions |
| 86 |
End Enum |
| 87 |
|
| 88 |
|
| 89 |
'NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON |
| 90 |
Public Type cs_detail |
| 91 |
regs_read(0 To 15) As Byte ' list of implicit registers read by this insn UNSIGNED |
| 92 |
regs_read_count As Byte ' number of implicit registers read by this insn UNSIGNED |
| 93 |
regs_write(0 To 19) As Byte ' list of implicit registers modified by this insn UNSIGNED |
| 94 |
regs_write_count As Byte ' number of implicit registers modified by this insn UNSIGNED |
| 95 |
groups(0 To 7) As Byte ' list of group this instruction belong to UNSIGNED |
| 96 |
groups_count As Byte ' number of groups this insn belongs to UNSIGNED |
| 97 |
End Type |
| 98 |
|
| 99 |
'typedef struct cs_detail { |
| 100 |
' uint8_t regs_read[16]; // list of implicit registers read by this insn |
| 101 |
' uint8_t regs_read_count; // number of implicit registers read by this insn |
| 102 |
' |
| 103 |
' uint8_t regs_write[20]; // list of implicit registers modified by this insn |
| 104 |
' uint8_t regs_write_count; // number of implicit registers modified by this insn |
| 105 |
' |
| 106 |
' uint8_t groups[8]; // list of group this instruction belong to |
| 107 |
' uint8_t groups_count; // number of groups this insn belongs to |
| 108 |
' |
| 109 |
' // Architecture-specific instruction info |
| 110 |
' union { |
| 111 |
' cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode |
| 112 |
' cs_arm64 arm64; // ARM64 architecture (aka AArch64) |
| 113 |
' cs_arm arm; // ARM architecture (including Thumb/Thumb2) |
| 114 |
' cs_mips mips; // MIPS architecture |
| 115 |
' cs_ppc ppc; // PowerPC architecture |
| 116 |
' cs_sparc sparc; // Sparc architecture |
| 117 |
' cs_sysz sysz; // SystemZ architecture |
| 118 |
' cs_xcore xcore; // XCore architecture |
| 119 |
' }; |
| 120 |
'} cs_detail; |
| 121 |
|
| 122 |
'Detail information of disassembled instruction |
| 123 |
Public Type cs_insn |
| 124 |
' Instruction ID (basically a numeric ID for the instruction mnemonic) |
| 125 |
' Find the instruction id in the '[ARCH]_insn' enum in the header file |
| 126 |
' of corresponding architecture, such as 'arm_insn' in arm.h for ARM, |
| 127 |
' 'x86_insn' in x86.h for X86, etc... |
| 128 |
' available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 129 |
' NOTE: in Skipdata mode, "data" instruction has 0 for this id field. UNSIGNED |
| 130 |
ID As Long ' |
| 131 |
align As Long 'not sure why it needs this..but it does.. |
| 132 |
address As Currency ' Address (EIP) of this instruction available even when CS_OPT_DETAIL = CS_OPT_OFF UNSIGNED |
| 133 |
size As Integer ' Size of this instruction available even when CS_OPT_DETAIL = CS_OPT_OFF UNSIGNED |
| 134 |
bytes(0 To 23) As Byte ' Machine bytes of this instruction, with number of bytes indicated by @size above available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 135 |
mnemonic(0 To 31) As Byte ' Ascii text of instruction mnemonic available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 136 |
op_str(0 To 159) As Byte ' Ascii text of instruction operands available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 137 |
|
| 138 |
' Pointer to cs_detail. |
| 139 |
' NOTE: detail pointer is only valid when both requirements below are met: |
| 140 |
' (1) CS_OP_DETAIL = CS_OPT_ON |
| 141 |
' (2) Engine is not in Skipdata mode (CS_OP_SKIPDATA option set to CS_OPT_ON) |
| 142 |
' NOTE 2: when in Skipdata mode, or when detail mode is OFF, even if this pointer |
| 143 |
' is not NULL, its content is still irrelevant. |
| 144 |
lpDetail As Long ' points to a cs_detail structure NOTE: only available when CS_OPT_DETAIL = CS_OPT_ON |
| 145 |
|
| 146 |
End Type |
| 147 |
|
| 148 |
'All type of errors encountered by Capstone API. |
| 149 |
'These are values returned by cs_errno() |
| 150 |
Public Enum cs_err |
| 151 |
CS_ERR_OK = 0 ' No error: everything was fine |
| 152 |
CS_ERR_MEM ' Out-Of-Memory error: cs_open(), cs_disasm(), cs_disasm_iter() |
| 153 |
CS_ERR_ARCH ' Unsupported architecture: cs_open() |
| 154 |
CS_ERR_HANDLE ' Invalid handle: cs_op_count(), cs_op_index() |
| 155 |
CS_ERR_CSH ' Invalid csh argument: cs_close(), cs_errno(), cs_option() |
| 156 |
CS_ERR_MODE ' Invalid/unsupported mode: cs_open() |
| 157 |
CS_ERR_OPTION ' Invalid/unsupported option: cs_option() |
| 158 |
CS_ERR_DETAIL ' Information is unavailable because detail option is OFF |
| 159 |
CS_ERR_MEMSETUP ' Dynamic memory management uninitialized (see CS_OPT_MEM) |
| 160 |
CS_ERR_VERSION ' Unsupported version (bindings) |
| 161 |
CS_ERR_DIET ' Access irrelevant data in "diet" engine |
| 162 |
CS_ERR_SKIPDATA ' Access irrelevant data for "data" instruction in SKIPDATA mode |
| 163 |
CS_ERR_X86_ATT ' X86 AT&T syntax is unsupported (opt-out at compile time) |
| 164 |
CS_ERR_X86_INTEL ' X86 Intel syntax is unsupported (opt-out at compile time) |
| 165 |
End Enum |
| 166 |
|
| 167 |
|
| 168 |
'/* |
| 169 |
' Return combined API version & major and minor version numbers. |
| 170 |
' |
| 171 |
' @major: major number of API version |
| 172 |
' @minor: minor number of API version |
| 173 |
' |
| 174 |
' @return hexical number as (major << 8 | minor), which encodes both |
| 175 |
' major & minor versions. |
| 176 |
' NOTE: This returned value can be compared with version number made |
| 177 |
' with macro CS_MAKE_VERSION |
| 178 |
' |
| 179 |
' For example, second API version would return 1 in @major, and 1 in @minor |
| 180 |
' The return value would be 0x0101 |
| 181 |
' |
| 182 |
' NOTE: if you only care about returned value, but not major and minor values, |
| 183 |
' set both @major & @minor arguments to NULL. |
| 184 |
'*/ |
| 185 |
'CAPSTONE_EXPORT |
| 186 |
'unsigned int cs_version(int *major, int *minor); |
| 187 |
Public Declare Function cs_version Lib "vbCapstone.dll" Alias "bs_version" (ByRef major As Long, ByRef minor As Long) As Long |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
' |
| 192 |
'/* |
| 193 |
' This API can be used to either ask for archs supported by this library, |
| 194 |
' or check to see if the library was compile with 'diet' option (or called |
| 195 |
' in 'diet' mode). |
| 196 |
' |
| 197 |
' To check if a particular arch is supported by this library, set @query to |
| 198 |
' arch mode (CS_ARCH_* value). |
| 199 |
' To verify if this library supports all the archs, use CS_ARCH_ALL. |
| 200 |
' |
| 201 |
' To check if this library is in 'diet' mode, set @query to CS_SUPPORT_DIET. |
| 202 |
' |
| 203 |
' @return True if this library supports the given arch, or in 'diet' mode. |
| 204 |
'*/ |
| 205 |
'CAPSTONE_EXPORT |
| 206 |
'bool cs_support(int query); |
| 207 |
Public Declare Function cs_support Lib "vbCapstone.dll" Alias "bs_support" (ByVal query As Long) As Long |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
'/* |
| 212 |
' Initialize CS handle: this must be done before any usage of CS. |
| 213 |
' |
| 214 |
' @arch: architecture type (CS_ARCH_*) |
| 215 |
' @mode: hardware mode. This is combined of CS_MODE_* |
| 216 |
' @handle: pointer to handle, which will be updated at return time |
| 217 |
' |
| 218 |
' @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum |
| 219 |
' for detailed error). |
| 220 |
'*/ |
| 221 |
'CAPSTONE_EXPORT |
| 222 |
'cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle); |
| 223 |
Public Declare Function cs_open Lib "vbCapstone.dll" Alias "bs_open" (ByVal arch As cs_arch, ByVal mode As cs_mode, ByRef hEngine As Long) As cs_err |
| 224 |
|
| 225 |
|
| 226 |
'/* |
| 227 |
' Close CS handle: MUST do to release the handle when it is not used anymore. |
| 228 |
' NOTE: this must be only called when there is no longer usage of Capstone, |
| 229 |
' not even access to cs_insn array. The reason is the this API releases some |
| 230 |
' cached memory, thus access to any Capstone API after cs_close() might crash |
| 231 |
' your application. |
| 232 |
' |
| 233 |
' In fact,this API invalidate @handle by ZERO out its value (i.e *handle = 0). |
| 234 |
' |
| 235 |
' @handle: pointer to a handle returned by cs_open() |
| 236 |
' |
| 237 |
' @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum |
| 238 |
' for detailed error). |
| 239 |
'*/ |
| 240 |
'CAPSTONE_EXPORT |
| 241 |
'cs_err cs_close(csh *handle); |
| 242 |
Public Declare Function cs_close Lib "vbCapstone.dll" Alias "bs_close" (ByRef hEngine As Long) As cs_err |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
'/* |
| 247 |
' Set option for disassembling engine at runtime |
| 248 |
' |
| 249 |
' @handle: handle returned by cs_open() |
| 250 |
' @type: type of option to be set |
| 251 |
' @value: option value corresponding with @type |
| 252 |
' |
| 253 |
' @return: CS_ERR_OK on success, or other value on failure. |
| 254 |
' Refer to cs_err enum for detailed error. |
| 255 |
' |
| 256 |
' NOTE: in the case of CS_OPT_MEM, handle's value can be anything, |
| 257 |
' so that cs_option(handle, CS_OPT_MEM, value) can (i.e must) be called |
| 258 |
' even before cs_open() |
| 259 |
'*/ |
| 260 |
'CAPSTONE_EXPORT |
| 261 |
'cs_err cs_option(csh handle, cs_opt_type type, size_t value); |
| 262 |
Public Declare Function cs_option Lib "vbCapstone.dll" Alias "bs_option" (ByVal hEngine As Long, ByVal typ As cs_opt_type, ByVal size As Long) As cs_err |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
'/* |
| 267 |
' Report the last error number when some API function fail. |
| 268 |
' Like glibc's errno, cs_errno might not retain its old value once accessed. |
| 269 |
' |
| 270 |
' @handle: handle returned by cs_open() |
| 271 |
' |
| 272 |
' @return: error code of cs_err enum type (CS_ERR_*, see above) |
| 273 |
'*/ |
| 274 |
'CAPSTONE_EXPORT |
| 275 |
'cs_err cs_errno(csh handle); |
| 276 |
Public Declare Function cs_errno Lib "vbCapstone.dll" Alias "bs_errno" (ByVal hEngine As Long) As cs_err |
| 277 |
|
| 278 |
' |
| 279 |
'/* |
| 280 |
' Return a string describing given error code. |
| 281 |
' |
| 282 |
' @code: error code (see CS_ERR_* above) |
| 283 |
' |
| 284 |
' @return: returns a pointer to a string that describes the error code |
| 285 |
' passed in the argument @code |
| 286 |
'*/ |
| 287 |
'CAPSTONE_EXPORT |
| 288 |
'const char *cs_strerror(cs_err code); |
| 289 |
Public Declare Function cs_strerror Lib "vbCapstone.dll" Alias "bs_strerror" (ByVal errCode As cs_err) As Long |
| 290 |
|
| 291 |
|
| 292 |
'/* |
| 293 |
' Disassemble binary code, given the code buffer, size, address and number |
| 294 |
' of instructions to be decoded. |
| 295 |
' This API dynamically allocate memory to contain disassembled instruction. |
| 296 |
' Resulting instructions will be put into @*insn |
| 297 |
' |
| 298 |
' NOTE 1: this API will automatically determine memory needed to contain |
| 299 |
' output disassembled instructions in @insn. |
| 300 |
' |
| 301 |
' NOTE 2: caller must free the allocated memory itself to avoid memory leaking. |
| 302 |
' |
| 303 |
' NOTE 3: for system with scarce memory to be dynamically allocated such as |
| 304 |
' OS kernel or firmware, the API cs_disasm_iter() might be a better choice than |
| 305 |
' cs_disasm(). The reason is that with cs_disasm(), based on limited available |
| 306 |
' memory, we have to calculate in advance how many instructions to be disassembled, |
| 307 |
' which complicates things. This is especially troublesome for the case @count=0, |
| 308 |
' when cs_disasm() runs uncontrollably (until either end of input buffer, or |
| 309 |
' when it encounters an invalid instruction). |
| 310 |
' |
| 311 |
' @handle: handle returned by cs_open() |
| 312 |
' @code: buffer containing raw binary code to be disassembled. |
| 313 |
' @code_size: size of the above code buffer. |
| 314 |
' @address: address of the first instruction in given raw code buffer. |
| 315 |
' @insn: array of instructions filled in by this API. |
| 316 |
' NOTE: @insn will be allocated by this function, and should be freed |
| 317 |
' with cs_free() API. |
| 318 |
' @count: number of instructions to be disassembled, or 0 to get all of them |
| 319 |
' |
| 320 |
' @return: the number of successfully disassembled instructions, |
| 321 |
' or 0 if this function failed to disassemble the given code |
| 322 |
' |
| 323 |
' On failure, call cs_errno() for error code. |
| 324 |
'*/ |
| 325 |
'CAPSTONE_EXPORT |
| 326 |
'size_t cs_disasm( |
| 327 |
' csh handle, |
| 328 |
' const uint8_t *code, |
| 329 |
' size_t code_size, |
| 330 |
' uint64_t address, |
| 331 |
' size_t count, |
| 332 |
' cs_insn **insn |
| 333 |
'); |
| 334 |
Public Declare Function cs_disasm Lib "vbCapstone.dll" Alias "bs_disasm" ( _ |
| 335 |
ByVal hEngine As Long, _ |
| 336 |
ByRef code As Byte, _ |
| 337 |
ByVal size As Long, _ |
| 338 |
ByVal address As Currency, _ |
| 339 |
ByVal count As Long, _ |
| 340 |
ByRef instAryPtr As Long _ |
| 341 |
) As Long |
| 342 |
|
| 343 |
'this proto also lets use byte() to get a dump easily.. |
| 344 |
Public Declare Sub getInstruction Lib "vbCapstone.dll" (ByVal hInstrAry As Long, ByVal index As Long, ByVal insPtr As Long, ByVal size As Long) |
| 345 |
|
| 346 |
|
| 347 |
'/* |
| 348 |
' Deprecated function - to be retired in the next version! |
| 349 |
' Use cs_disasm() instead of cs_disasm_ex() |
| 350 |
'*/ |
| 351 |
'CAPSTONE_EXPORT |
| 352 |
'CAPSTONE_DEPRECATED |
| 353 |
'size_t cs_disasm_ex(csh handle, |
| 354 |
' const uint8_t *code, size_t code_size, |
| 355 |
' uint64_t address, |
| 356 |
' size_t count, |
| 357 |
' cs_insn **insn); |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
'/* |
| 362 |
' Free memory allocated by cs_malloc() or cs_disasm() (argument @insn) |
| 363 |
' |
| 364 |
' @insn: pointer returned by @insn argument in cs_disasm() or cs_malloc() |
| 365 |
' @count: number of cs_insn structures returned by cs_disasm(), or 1 |
| 366 |
' to free memory allocated by cs_malloc(). |
| 367 |
'*/ |
| 368 |
'CAPSTONE_EXPORT |
| 369 |
'void cs_free(cs_insn *insn, size_t count); |
| 370 |
Public Declare Sub cs_free Lib "vbCapstone.dll" Alias "bs_free" (ByVal instr As Long, ByVal count As Long) |
| 371 |
|
| 372 |
|
| 373 |
' |
| 374 |
'/* |
| 375 |
' Allocate memory for 1 instruction to be used by cs_disasm_iter(). |
| 376 |
' |
| 377 |
' @handle: handle returned by cs_open() |
| 378 |
' |
| 379 |
' NOTE: when no longer in use, you can reclaim the memory allocated for |
| 380 |
' this instruction with cs_free(insn, 1) |
| 381 |
'*/ |
| 382 |
'CAPSTONE_EXPORT |
| 383 |
'cs_insn *cs_malloc(csh handle); |
| 384 |
Public Declare Function cs_malloc Lib "vbCapstone.dll" Alias "bs_malloc" (ByVal handle As Long) As Long |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
'/* |
| 389 |
' Fast API to disassemble binary code, given the code buffer, size, address |
| 390 |
' and number of instructions to be decoded. |
| 391 |
' This API puts the resulting instruction into a given cache in @insn. |
| 392 |
' See tests/test_iter.c for sample code demonstrating this API. |
| 393 |
' |
| 394 |
' NOTE 1: this API will update @code, @size & @address to point to the next |
| 395 |
' instruction in the input buffer. Therefore, it is convenient to use |
| 396 |
' cs_disasm_iter() inside a loop to quickly iterate all the instructions. |
| 397 |
' While decoding one instruction at a time can also be achieved with |
| 398 |
' cs_disasm(count=1), some benchmarks shown that cs_disasm_iter() can be 30% |
| 399 |
' faster on random input. |
| 400 |
' |
| 401 |
' NOTE 2: the cache in @insn can be created with cs_malloc() API. |
| 402 |
' |
| 403 |
' NOTE 3: for system with scarce memory to be dynamically allocated such as |
| 404 |
' OS kernel or firmware, this API is recommended over cs_disasm(), which |
| 405 |
' allocates memory based on the number of instructions to be disassembled. |
| 406 |
' The reason is that with cs_disasm(), based on limited available memory, |
| 407 |
' we have to calculate in advance how many instructions to be disassembled, |
| 408 |
' which complicates things. This is especially troublesome for the case |
| 409 |
' @count=0, when cs_disasm() runs uncontrollably (until either end of input |
| 410 |
' buffer, or when it encounters an invalid instruction). |
| 411 |
' |
| 412 |
' @handle: handle returned by cs_open() |
| 413 |
' @code: buffer containing raw binary code to be disassembled |
| 414 |
' @code_size: size of above code |
| 415 |
' @address: address of the first insn in given raw code buffer |
| 416 |
' @insn: pointer to instruction to be filled in by this API. |
| 417 |
' |
| 418 |
' @return: true if this API successfully decode 1 instruction, |
| 419 |
' or false otherwise. |
| 420 |
' |
| 421 |
' On failure, call cs_errno() for error code. |
| 422 |
'*/ |
| 423 |
'CAPSTONE_EXPORT |
| 424 |
'bool cs_disasm_iter(csh handle, const uint8_t **code, size_t *size, uint64_t *address, cs_insn *insn); |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
'/* |
| 429 |
' Return friendly name of register in a string. |
| 430 |
' Find the instruction id from header file of corresponding architecture (arm.h for ARM, |
| 431 |
' x86.h for X86, ...) |
| 432 |
' |
| 433 |
' WARN: when in 'diet' mode, this API is irrelevant because engine does not |
| 434 |
' store register name. |
| 435 |
' |
| 436 |
' @handle: handle returned by cs_open() |
| 437 |
' @reg_id: register id |
| 438 |
' |
| 439 |
' @return: string name of the register, or NULL if @reg_id is invalid. |
| 440 |
'*/ |
| 441 |
'CAPSTONE_EXPORT |
| 442 |
'const char *cs_reg_name(csh handle, unsigned int reg_id); |
| 443 |
Public Declare Function cs_reg_name Lib "vbCapstone.dll" Alias "bs_reg_name" (ByVal handle As Long, ByVal regID As Long) As Long |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
'/* |
| 449 |
' Return friendly name of an instruction in a string. |
| 450 |
' Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 451 |
' |
| 452 |
' WARN: when in 'diet' mode, this API is irrelevant because the engine does not |
| 453 |
' store instruction name. |
| 454 |
' |
| 455 |
' @handle: handle returned by cs_open() |
| 456 |
' @insn_id: instruction id |
| 457 |
' |
| 458 |
' @return: string name of the instruction, or NULL if @insn_id is invalid. |
| 459 |
'*/ |
| 460 |
'CAPSTONE_EXPORT |
| 461 |
'const char *cs_insn_name(csh handle, unsigned int insn_id); |
| 462 |
Public Declare Function cs_insn_name Lib "vbCapstone.dll" Alias "bs_insn_name" (ByVal handle As Long, ByVal insn_id As Long) As Long |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
'/* |
| 468 |
' Return friendly name of a group id (that an instruction can belong to) |
| 469 |
' Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 470 |
' |
| 471 |
' WARN: when in 'diet' mode, this API is irrelevant because the engine does not |
| 472 |
' store group name. |
| 473 |
' |
| 474 |
' @handle: handle returned by cs_open() |
| 475 |
' @group_id: group id |
| 476 |
' |
| 477 |
' @return: string name of the group, or NULL if @group_id is invalid. |
| 478 |
'*/ |
| 479 |
'CAPSTONE_EXPORT |
| 480 |
'const char *cs_group_name(csh handle, unsigned int group_id); |
| 481 |
Public Declare Function cs_group_name Lib "vbCapstone.dll" Alias "bs_group_name" (ByVal handle As Long, ByVal group_id As Long) As Long |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
'/* |
| 486 |
' Check if a disassembled instruction belong to a particular group. |
| 487 |
' Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 488 |
' Internally, this simply verifies if @group_id matches any member of insn->groups array. |
| 489 |
' |
| 490 |
' NOTE: this API is only valid when detail option is ON (which is OFF by default). |
| 491 |
' |
| 492 |
' WARN: when in 'diet' mode, this API is irrelevant because the engine does not |
| 493 |
' update @groups array. |
| 494 |
' |
| 495 |
' @handle: handle returned by cs_open() |
| 496 |
' @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter() |
| 497 |
' @group_id: group that you want to check if this instruction belong to. |
| 498 |
' |
| 499 |
' @return: true if this instruction indeed belongs to the given group, or false otherwise. |
| 500 |
'*/ |
| 501 |
'CAPSTONE_EXPORT |
| 502 |
'bool cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id); |
| 503 |
Public Declare Function cs_insn_group Lib "vbCapstone.dll" Alias "bs_insn_group" (ByVal handle As Long, ByVal instruction As Long, ByVal group_id As Long) As Long |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
'/* |
| 508 |
' Check if a disassembled instruction IMPLICITLY used a particular register. |
| 509 |
' Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 510 |
' Internally, this simply verifies if @reg_id matches any member of insn->regs_read array. |
| 511 |
' |
| 512 |
' NOTE: this API is only valid when detail option is ON (which is OFF by default) |
| 513 |
' |
| 514 |
' WARN: when in 'diet' mode, this API is irrelevant because the engine does not |
| 515 |
' update @regs_read array. |
| 516 |
' |
| 517 |
' @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter() |
| 518 |
' @reg_id: register that you want to check if this instruction used it. |
| 519 |
' |
| 520 |
' @return: true if this instruction indeed implicitly used the given register, or false otherwise. |
| 521 |
'*/ |
| 522 |
'CAPSTONE_EXPORT |
| 523 |
'bool cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id); |
| 524 |
Public Declare Function cs_reg_read Lib "vbCapstone.dll" Alias "bs_reg_read" (ByVal handle As Long, ByVal instruction As Long, ByVal reg_id As Long) As Long |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
'/* |
| 529 |
' Check if a disassembled instruction IMPLICITLY modified a particular register. |
| 530 |
' Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 531 |
' Internally, this simply verifies if @reg_id matches any member of insn->regs_write array. |
| 532 |
' |
| 533 |
' NOTE: this API is only valid when detail option is ON (which is OFF by default) |
| 534 |
' |
| 535 |
' WARN: when in 'diet' mode, this API is irrelevant because the engine does not |
| 536 |
' update @regs_write array. |
| 537 |
' |
| 538 |
' @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter() |
| 539 |
' @reg_id: register that you want to check if this instruction modified it. |
| 540 |
' |
| 541 |
' @return: true if this instruction indeed implicitly modified the given register, or false otherwise. |
| 542 |
'*/ |
| 543 |
'CAPSTONE_EXPORT |
| 544 |
'bool cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id); |
| 545 |
Public Declare Function cs_reg_write Lib "vbCapstone.dll" Alias "bs_reg_write" (ByVal handle As Long, ByVal instruction As Long, ByVal reg_id As Long) As Long |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
'/* |
| 550 |
' Count the number of operands of a given type. |
| 551 |
' Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 552 |
' |
| 553 |
' NOTE: this API is only valid when detail option is ON (which is OFF by default) |
| 554 |
' |
| 555 |
' @handle: handle returned by cs_open() |
| 556 |
' @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter() |
| 557 |
' @op_type: Operand type to be found. |
| 558 |
' |
| 559 |
' @return: number of operands of given type @op_type in instruction @insn, |
| 560 |
' or -1 on failure. |
| 561 |
'*/ |
| 562 |
'CAPSTONE_EXPORT |
| 563 |
'int cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type); |
| 564 |
Public Declare Function cs_op_count Lib "vbCapstone.dll" Alias "bs_op_count" (ByVal handle As Long, ByVal instruction As Long, ByVal op_type As Long) As Long |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
'/* |
| 569 |
' Retrieve the position of operand of given type in <arch>.operands[] array. |
| 570 |
' Later, the operand can be accessed using the returned position. |
| 571 |
' Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 572 |
' |
| 573 |
' NOTE: this API is only valid when detail option is ON (which is OFF by default) |
| 574 |
' |
| 575 |
' @handle: handle returned by cs_open() |
| 576 |
' @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter() |
| 577 |
' @op_type: Operand type to be found. |
| 578 |
' @position: position of the operand to be found. This must be in the range |
| 579 |
' [1, cs_op_count(handle, insn, op_type)] |
| 580 |
' |
| 581 |
' @return: index of operand of given type @op_type in <arch>.operands[] array |
| 582 |
' in instruction @insn, or -1 on failure. |
| 583 |
'*/ |
| 584 |
'CAPSTONE_EXPORT |
| 585 |
'int cs_op_index(csh handle, const cs_insn *insn, unsigned int op_type, unsigned int position); |
| 586 |
Public Declare Function cs_op_index Lib "vbCapstone.dll" Alias "bs_op_index" (ByVal handle As Long, ByVal instruction As Long, ByVal op_type As Long, ByVal position As Long) As Long |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long |
| 591 |
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long |
| 592 |
|
| 593 |
Function cstr2vb(lpStr As Long) As String |
| 594 |
|
| 595 |
Dim length As Long |
| 596 |
Dim buf() As Byte |
| 597 |
|
| 598 |
If lpStr = 0 Then Exit Function |
| 599 |
|
| 600 |
length = lstrlen(lpStr) |
| 601 |
If length < 1 Then Exit Function |
| 602 |
|
| 603 |
ReDim buf(1 To length) |
| 604 |
CopyMemory buf(1), ByVal lpStr, length |
| 605 |
|
| 606 |
cstr2vb = StrConv(buf, vbUnicode, &H409) |
| 607 |
|
| 608 |
End Function |
| 609 |
|
| 610 |
Function err2str(e As cs_err) As String |
| 611 |
Dim lpStr As Long |
| 612 |
lpStr = cs_strerror(e) |
| 613 |
err2str = cstr2vb(lpStr) |
| 614 |
End Function |
| 615 |
|
| 616 |
Function regName(hEngine As Long, regID As Long) As String |
| 617 |
Dim lpStr As Long |
| 618 |
lpStr = cs_reg_name(hEngine, regID) |
| 619 |
regName = cstr2vb(lpStr) |
| 620 |
If Len(regName) = 0 Or DEBUG_DUMP Then regName = regName & " (" & Hex(regID) & ")" |
| 621 |
End Function |
| 622 |
|
| 623 |
Function insnName(hEngine As Long, insnID As Long) As String |
| 624 |
Dim lpStr As Long |
| 625 |
lpStr = cs_insn_name(hEngine, insnID) |
| 626 |
insnName = cstr2vb(lpStr) |
| 627 |
If Len(insnName) = 0 Or DEBUG_DUMP Then insnName = insnName & " (" & Hex(insnID) & ")" |
| 628 |
End Function |
| 629 |
|
| 630 |
Function groupName(hEngine As Long, groupID As Long) As String |
| 631 |
Dim lpStr As Long |
| 632 |
lpStr = cs_group_name(hEngine, groupID) |
| 633 |
groupName = cstr2vb(lpStr) |
| 634 |
If Len(groupName) = 0 Or DEBUG_DUMP Then groupName = groupName & " (" & Hex(groupID) & ")" |
| 635 |
End Function |