1 module capstone.capstone;
2 
3 import core.stdc.config;
4 import core.stdc.stdio;
5 import core.stdc.stdarg;
6 
7 import capstone.arm64;
8 import capstone.arm;
9 import capstone.evm;
10 import capstone.m680x;
11 import capstone.m68k;
12 import capstone.mips;
13 import capstone.ppc;
14 import capstone.sparc;
15 import capstone.systemz;
16 import capstone.tms320c64x;
17 import capstone.x86;
18 import capstone.xcore;
19 
20 extern (C):
21 
22 /* Capstone Disassembly Engine */
23 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2016 */
24 
25 // defined(CAPSTONE_STATIC)
26 
27 // defined(CAPSTONE_STATIC)
28 
29 // Capstone API version
30 enum CS_API_MAJOR = 4;
31 enum CS_API_MINOR = 0;
32 
33 // Version for bleeding edge code of the Github's "next" branch.
34 // Use this if you want the absolutely latest development code.
35 // This version number will be bumped up whenever we have a new major change.
36 enum CS_NEXT_VERSION = 4;
37 
38 // Capstone package version
39 enum CS_VERSION_MAJOR = CS_API_MAJOR;
40 enum CS_VERSION_MINOR = CS_API_MINOR;
41 enum CS_VERSION_EXTRA = 0;
42 
43 /// Macro to create combined version which can be compared to
44 /// result of cs_version() API.
45 extern (D) auto CS_MAKE_VERSION(T0, T1)(auto ref T0 major, auto ref T1 minor)
46 {
47     return (major << 8) + minor;
48 }
49 
50 /// Maximum size of an instruction mnemonic string.
51 enum CS_MNEMONIC_SIZE = 32;
52 
53 // Handle using with all API
54 alias csh = c_ulong;
55 
56 /// Architecture type
57 enum cs_arch
58 {
59     CS_ARCH_ARM = 0, ///< ARM architecture (including Thumb, Thumb-2)
60     CS_ARCH_ARM64 = 1, ///< ARM-64, also called AArch64
61     CS_ARCH_MIPS = 2, ///< Mips architecture
62     CS_ARCH_X86 = 3, ///< X86 architecture (including x86 & x86-64)
63     CS_ARCH_PPC = 4, ///< PowerPC architecture
64     CS_ARCH_SPARC = 5, ///< Sparc architecture
65     CS_ARCH_SYSZ = 6, ///< SystemZ architecture
66     CS_ARCH_XCORE = 7, ///< XCore architecture
67     CS_ARCH_M68K = 8, ///< 68K architecture
68     CS_ARCH_TMS320C64X = 9, ///< TMS320C64x architecture
69     CS_ARCH_M680X = 10, ///< 680X architecture
70     CS_ARCH_EVM = 11, ///< Ethereum architecture
71     CS_ARCH_MAX = 12,
72     CS_ARCH_ALL = 0xFFFF // All architectures - for cs_support()
73 }
74 
75 // Support value to verify diet mode of the engine.
76 // If cs_support(CS_SUPPORT_DIET) return True, the engine was compiled
77 // in diet mode.
78 enum CS_SUPPORT_DIET = cs_arch.CS_ARCH_ALL + 1;
79 
80 // Support value to verify X86 reduce mode of the engine.
81 // If cs_support(CS_SUPPORT_X86_REDUCE) return True, the engine was compiled
82 // in X86 reduce mode.
83 enum CS_SUPPORT_X86_REDUCE = cs_arch.CS_ARCH_ALL + 2;
84 
85 /// Mode type
86 enum cs_mode
87 {
88     CS_MODE_LITTLE_ENDIAN = 0, ///< little-endian mode (default mode)
89     CS_MODE_ARM = 0, ///< 32-bit ARM
90     CS_MODE_16 = 1 << 1, ///< 16-bit mode (X86)
91     CS_MODE_32 = 1 << 2, ///< 32-bit mode (X86)
92     CS_MODE_64 = 1 << 3, ///< 64-bit mode (X86, PPC)
93     CS_MODE_THUMB = 1 << 4, ///< ARM's Thumb mode, including Thumb-2
94     CS_MODE_MCLASS = 1 << 5, ///< ARM's Cortex-M series
95     CS_MODE_V8 = 1 << 6, ///< ARMv8 A32 encodings for ARM
96     CS_MODE_MICRO = 1 << 4, ///< MicroMips mode (MIPS)
97     CS_MODE_MIPS3 = 1 << 5, ///< Mips III ISA
98     CS_MODE_MIPS32R6 = 1 << 6, ///< Mips32r6 ISA
99     CS_MODE_MIPS2 = 1 << 7, ///< Mips II ISA
100     CS_MODE_V9 = 1 << 4, ///< SparcV9 mode (Sparc)
101     CS_MODE_QPX = 1 << 4, ///< Quad Processing eXtensions mode (PPC)
102     CS_MODE_M68K_000 = 1 << 1, ///< M68K 68000 mode
103     CS_MODE_M68K_010 = 1 << 2, ///< M68K 68010 mode
104     CS_MODE_M68K_020 = 1 << 3, ///< M68K 68020 mode
105     CS_MODE_M68K_030 = 1 << 4, ///< M68K 68030 mode
106     CS_MODE_M68K_040 = 1 << 5, ///< M68K 68040 mode
107     CS_MODE_M68K_060 = 1 << 6, ///< M68K 68060 mode
108     CS_MODE_BIG_ENDIAN = 1 << 31, ///< big-endian mode
109     CS_MODE_MIPS32 = CS_MODE_32, ///< Mips32 ISA (Mips)
110     CS_MODE_MIPS64 = CS_MODE_64, ///< Mips64 ISA (Mips)
111     CS_MODE_M680X_6301 = 1 << 1, ///< M680X Hitachi 6301,6303 mode
112     CS_MODE_M680X_6309 = 1 << 2, ///< M680X Hitachi 6309 mode
113     CS_MODE_M680X_6800 = 1 << 3, ///< M680X Motorola 6800,6802 mode
114     CS_MODE_M680X_6801 = 1 << 4, ///< M680X Motorola 6801,6803 mode
115     CS_MODE_M680X_6805 = 1 << 5, ///< M680X Motorola/Freescale 6805 mode
116     CS_MODE_M680X_6808 = 1 << 6, ///< M680X Motorola/Freescale/NXP 68HC08 mode
117     CS_MODE_M680X_6809 = 1 << 7, ///< M680X Motorola 6809 mode
118     CS_MODE_M680X_6811 = 1 << 8, ///< M680X Motorola/Freescale/NXP 68HC11 mode
119     CS_MODE_M680X_CPU12 = 1 << 9, ///< M680X Motorola/Freescale/NXP CPU12
120     ///< used on M68HC12/HCS12
121     CS_MODE_M680X_HCS08 = 1 << 10 ///< M680X Freescale/NXP HCS08 mode
122 }
123 
124 alias cs_malloc_t = void* function (size_t size);
125 alias cs_calloc_t = void* function (size_t nmemb, size_t size);
126 alias cs_realloc_t = void* function (void* ptr, size_t size);
127 alias cs_free_t = void function (void* ptr);
128 alias cs_vsnprintf_t = int function (char* str, size_t size, const(char)* format, va_list ap);
129 
130 /// User-defined dynamic memory related functions: malloc/calloc/realloc/free/vsnprintf()
131 /// By default, Capstone uses system's malloc(), calloc(), realloc(), free() & vsnprintf().
132 struct cs_opt_mem
133 {
134     cs_malloc_t malloc;
135     cs_calloc_t calloc;
136     cs_realloc_t realloc;
137     cs_free_t free;
138     cs_vsnprintf_t vsnprintf;
139 }
140 
141 /// Customize mnemonic for instructions with alternative name.
142 /// To reset existing customized instruction to its default mnemonic,
143 /// call cs_option(CS_OPT_MNEMONIC) again with the same @id and NULL value
144 /// for @mnemonic.
145 struct cs_opt_mnem
146 {
147     /// ID of instruction to be customized.
148     uint id;
149     /// Customized instruction mnemonic.
150     const(char)* mnemonic;
151 }
152 
153 /// Runtime option for the disassembled engine
154 enum cs_opt_type
155 {
156     CS_OPT_INVALID = 0, ///< No option specified
157     CS_OPT_SYNTAX = 1, ///< Assembly output syntax
158     CS_OPT_DETAIL = 2, ///< Break down instruction structure into details
159     CS_OPT_MODE = 3, ///< Change engine's mode at run-time
160     CS_OPT_MEM = 4, ///< User-defined dynamic memory related functions
161     CS_OPT_SKIPDATA = 5, ///< Skip data when disassembling. Then engine is in SKIPDATA mode.
162     CS_OPT_SKIPDATA_SETUP = 6, ///< Setup user-defined function for SKIPDATA option
163     CS_OPT_MNEMONIC = 7, ///< Customize instruction mnemonic
164     CS_OPT_UNSIGNED = 8 ///< print immediate operands in unsigned form
165 }
166 
167 /// Runtime option value (associated with option type above)
168 enum cs_opt_value
169 {
170     CS_OPT_OFF = 0, ///< Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
171     CS_OPT_ON = 3, ///< Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
172     CS_OPT_SYNTAX_DEFAULT = 0, ///< Default asm syntax (CS_OPT_SYNTAX).
173     CS_OPT_SYNTAX_INTEL = 1, ///< X86 Intel asm syntax - default on X86 (CS_OPT_SYNTAX).
174     CS_OPT_SYNTAX_ATT = 2, ///< X86 ATT asm syntax (CS_OPT_SYNTAX).
175     CS_OPT_SYNTAX_NOREGNAME = 3, ///< Prints register name with only number (CS_OPT_SYNTAX)
176     CS_OPT_SYNTAX_MASM = 4 ///< X86 Intel Masm syntax (CS_OPT_SYNTAX).
177 }
178 
179 /// Common instruction operand types - to be consistent across all architectures.
180 enum cs_op_type
181 {
182     CS_OP_INVALID = 0, ///< uninitialized/invalid operand.
183     CS_OP_REG = 1, ///< Register operand.
184     CS_OP_IMM = 2, ///< Immediate operand.
185     CS_OP_MEM = 3, ///< Memory operand.
186     CS_OP_FP = 4 ///< Floating-Point operand.
187 }
188 
189 /// Common instruction operand access types - to be consistent across all architectures.
190 /// It is possible to combine access types, for example: CS_AC_READ | CS_AC_WRITE
191 enum cs_ac_type
192 {
193     CS_AC_INVALID = 0, ///< Uninitialized/invalid access type.
194     CS_AC_READ = 1 << 0, ///< Operand read from memory or register.
195     CS_AC_WRITE = 1 << 1 ///< Operand write to memory or register.
196 }
197 
198 /// Common instruction groups - to be consistent across all architectures.
199 enum cs_group_type
200 {
201     CS_GRP_INVALID = 0, ///< uninitialized/invalid group.
202     CS_GRP_JUMP = 1, ///< all jump instructions (conditional+direct+indirect jumps)
203     CS_GRP_CALL = 2, ///< all call instructions
204     CS_GRP_RET = 3, ///< all return instructions
205     CS_GRP_INT = 4, ///< all interrupt instructions (int+syscall)
206     CS_GRP_IRET = 5, ///< all interrupt return instructions
207     CS_GRP_PRIVILEGE = 6, ///< all privileged instructions
208     CS_GRP_BRANCH_RELATIVE = 7 ///< all relative branching instructions
209 }
210 
211 /**
212  User-defined callback function for SKIPDATA option.
213  See tests/test_skipdata.c for sample code demonstrating this API.
214 
215  @code: the input buffer containing code to be disassembled.
216         This is the same buffer passed to cs_disasm().
217  @code_size: size (in bytes) of the above @code buffer.
218  @offset: the position of the currently-examining byte in the input
219       buffer @code mentioned above.
220  @user_data: user-data passed to cs_option() via @user_data field in
221       cs_opt_skipdata struct below.
222 
223  @return: return number of bytes to skip, or 0 to immediately stop disassembling.
224 */
225 alias cs_skipdata_cb_t = c_ulong function (const(ubyte)* code, size_t code_size, size_t offset, void* user_data);
226 
227 /// User-customized setup for SKIPDATA option
228 struct cs_opt_skipdata
229 {
230     /// Capstone considers data to skip as special "instructions".
231     /// User can specify the string for this instruction's "mnemonic" here.
232     /// By default (if @mnemonic is NULL), Capstone use ".byte".
233     const(char)* mnemonic;
234 
235     /// User-defined callback function to be called when Capstone hits data.
236     /// If the returned value from this callback is positive (>0), Capstone
237     /// will skip exactly that number of bytes & continue. Otherwise, if
238     /// the callback returns 0, Capstone stops disassembling and returns
239     /// immediately from cs_disasm()
240     /// NOTE: if this callback pointer is NULL, Capstone would skip a number
241     /// of bytes depending on architectures, as following:
242     /// Arm:     2 bytes (Thumb mode) or 4 bytes.
243     /// Arm64:   4 bytes.
244     /// Mips:    4 bytes.
245     /// M680x:   1 byte.
246     /// PowerPC: 4 bytes.
247     /// Sparc:   4 bytes.
248     /// SystemZ: 2 bytes.
249     /// X86:     1 bytes.
250     /// XCore:   2 bytes.
251     /// EVM:     1 bytes.
252     cs_skipdata_cb_t callback; // default value is NULL
253 
254     /// User-defined data to be passed to @callback function pointer.
255     void* user_data;
256 }
257 
258 /// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON
259 /// Initialized as memset(., 0, offsetof(cs_detail, ARCH)+sizeof(cs_ARCH))
260 /// by ARCH_getInstruction in arch/ARCH/ARCHDisassembler.c
261 /// if cs_detail changes, in particular if a field is added after the union,
262 /// then update arch/ARCH/ARCHDisassembler.c accordingly
263 struct cs_detail
264 {
265     ushort[12] regs_read; ///< list of implicit registers read by this insn
266     ubyte regs_read_count; ///< number of implicit registers read by this insn
267 
268     ushort[20] regs_write; ///< list of implicit registers modified by this insn
269     ubyte regs_write_count; ///< number of implicit registers modified by this insn
270 
271     ubyte[8] groups; ///< list of group this instruction belong to
272     ubyte groups_count; ///< number of groups this insn belongs to
273 
274     /// Architecture-specific instruction info
275     union
276     {
277         cs_x86 x86; ///< X86 architecture, including 16-bit, 32-bit & 64-bit mode
278         cs_arm64 arm64; ///< ARM64 architecture (aka AArch64)
279         cs_arm arm; ///< ARM architecture (including Thumb/Thumb2)
280         cs_m68k m68k; ///< M68K architecture
281         cs_mips mips; ///< MIPS architecture
282         cs_ppc ppc; ///< PowerPC architecture
283         cs_sparc sparc; ///< Sparc architecture
284         cs_sysz sysz; ///< SystemZ architecture
285         cs_xcore xcore; ///< XCore architecture
286         cs_tms320c64x tms320c64x; ///< TMS320C64x architecture
287         cs_m680x m680x; ///< M680X architecture
288         cs_evm evm; ///< Ethereum architecture
289     }
290 }
291 
292 /// Detail information of disassembled instruction
293 struct cs_insn
294 {
295     /// Instruction ID (basically a numeric ID for the instruction mnemonic)
296     /// Find the instruction id in the '[ARCH]_insn' enum in the header file
297     /// of corresponding architecture, such as 'arm_insn' in arm.h for ARM,
298     /// 'x86_insn' in x86.h for X86, etc...
299     /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
300     /// NOTE: in Skipdata mode, "data" instruction has 0 for this id field.
301     uint id;
302 
303     /// Address (EIP) of this instruction
304     /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
305     ulong address;
306 
307     /// Size of this instruction
308     /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
309     ushort size;
310 
311     /// Machine bytes of this instruction, with number of bytes indicated by @size above
312     /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
313     ubyte[16] bytes;
314 
315     /// Ascii text of instruction mnemonic
316     /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
317     char[CS_MNEMONIC_SIZE] mnemonic;
318 
319     /// Ascii text of instruction operands
320     /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
321     char[160] op_str;
322 
323     /// Pointer to cs_detail.
324     /// NOTE: detail pointer is only valid when both requirements below are met:
325     /// (1) CS_OP_DETAIL = CS_OPT_ON
326     /// (2) Engine is not in Skipdata mode (CS_OP_SKIPDATA option set to CS_OPT_ON)
327     ///
328     /// NOTE 2: when in Skipdata mode, or when detail mode is OFF, even if this pointer
329     ///     is not NULL, its content is still irrelevant.
330     cs_detail* detail;
331 }
332 
333 /// Calculate the offset of a disassembled instruction in its buffer, given its position
334 /// in its array of disassembled insn
335 /// NOTE: this macro works with position (>=1), not index
336 extern (D) auto CS_INSN_OFFSET(T0, T1)(auto ref T0 insns, auto ref T1 post)
337 {
338     return insns[post - 1].address - insns[0].address;
339 }
340 
341 /// All type of errors encountered by Capstone API.
342 /// These are values returned by cs_errno()
343 enum cs_err
344 {
345     CS_ERR_OK = 0, ///< No error: everything was fine
346     CS_ERR_MEM = 1, ///< Out-Of-Memory error: cs_open(), cs_disasm(), cs_disasm_iter()
347     CS_ERR_ARCH = 2, ///< Unsupported architecture: cs_open()
348     CS_ERR_HANDLE = 3, ///< Invalid handle: cs_op_count(), cs_op_index()
349     CS_ERR_CSH = 4, ///< Invalid csh argument: cs_close(), cs_errno(), cs_option()
350     CS_ERR_MODE = 5, ///< Invalid/unsupported mode: cs_open()
351     CS_ERR_OPTION = 6, ///< Invalid/unsupported option: cs_option()
352     CS_ERR_DETAIL = 7, ///< Information is unavailable because detail option is OFF
353     CS_ERR_MEMSETUP = 8, ///< Dynamic memory management uninitialized (see CS_OPT_MEM)
354     CS_ERR_VERSION = 9, ///< Unsupported version (bindings)
355     CS_ERR_DIET = 10, ///< Access irrelevant data in "diet" engine
356     CS_ERR_SKIPDATA = 11, ///< Access irrelevant data for "data" instruction in SKIPDATA mode
357     CS_ERR_X86_ATT = 12, ///< X86 AT&T syntax is unsupported (opt-out at compile time)
358     CS_ERR_X86_INTEL = 13, ///< X86 Intel syntax is unsupported (opt-out at compile time)
359     CS_ERR_X86_MASM = 14 ///< X86 Intel syntax is unsupported (opt-out at compile time)
360 }
361 
362 /**
363  Return combined API version & major and minor version numbers.
364 
365  @major: major number of API version
366  @minor: minor number of API version
367 
368  @return hexical number as (major << 8 | minor), which encodes both
369 	 major & minor versions.
370 	 NOTE: This returned value can be compared with version number made
371 	 with macro CS_MAKE_VERSION
372 
373  For example, second API version would return 1 in @major, and 1 in @minor
374  The return value would be 0x0101
375 
376  NOTE: if you only care about returned value, but not major and minor values,
377  set both @major & @minor arguments to NULL.
378 */
379 uint cs_version (int* major, int* minor);
380 
381 /**
382  This API can be used to either ask for archs supported by this library,
383  or check to see if the library was compile with 'diet' option (or called
384  in 'diet' mode).
385 
386  To check if a particular arch is supported by this library, set @query to
387  arch mode (CS_ARCH_* value).
388  To verify if this library supports all the archs, use CS_ARCH_ALL.
389 
390  To check if this library is in 'diet' mode, set @query to CS_SUPPORT_DIET.
391 
392  @return True if this library supports the given arch, or in 'diet' mode.
393 */
394 bool cs_support (int query);
395 
396 /**
397  Initialize CS handle: this must be done before any usage of CS.
398 
399  @arch: architecture type (CS_ARCH_*)
400  @mode: hardware mode. This is combined of CS_MODE_*
401  @handle: pointer to handle, which will be updated at return time
402 
403  @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
404  for detailed error).
405 */
406 cs_err cs_open (cs_arch arch, cs_mode mode, csh* handle);
407 
408 /**
409  Close CS handle: MUST do to release the handle when it is not used anymore.
410  NOTE: this must be only called when there is no longer usage of Capstone,
411  not even access to cs_insn array. The reason is the this API releases some
412  cached memory, thus access to any Capstone API after cs_close() might crash
413  your application.
414 
415  In fact,this API invalidate @handle by ZERO out its value (i.e *handle = 0).
416 
417  @handle: pointer to a handle returned by cs_open()
418 
419  @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
420  for detailed error).
421 */
422 cs_err cs_close (csh* handle);
423 
424 /**
425  Set option for disassembling engine at runtime
426 
427  @handle: handle returned by cs_open()
428  @type: type of option to be set
429  @value: option value corresponding with @type
430 
431  @return: CS_ERR_OK on success, or other value on failure.
432  Refer to cs_err enum for detailed error.
433 
434  NOTE: in the case of CS_OPT_MEM, handle's value can be anything,
435  so that cs_option(handle, CS_OPT_MEM, value) can (i.e must) be called
436  even before cs_open()
437 */
438 cs_err cs_option (csh handle, cs_opt_type type, size_t value);
439 
440 /**
441  Report the last error number when some API function fail.
442  Like glibc's errno, cs_errno might not retain its old value once accessed.
443 
444  @handle: handle returned by cs_open()
445 
446  @return: error code of cs_err enum type (CS_ERR_*, see above)
447 */
448 cs_err cs_errno (csh handle);
449 
450 /**
451  Return a string describing given error code.
452 
453  @code: error code (see CS_ERR_* above)
454 
455  @return: returns a pointer to a string that describes the error code
456 	passed in the argument @code
457 */
458 const(char)* cs_strerror (cs_err code);
459 
460 /**
461  Disassemble binary code, given the code buffer, size, address and number
462  of instructions to be decoded.
463  This API dynamically allocate memory to contain disassembled instruction.
464  Resulting instructions will be put into @*insn
465 
466  NOTE 1: this API will automatically determine memory needed to contain
467  output disassembled instructions in @insn.
468 
469  NOTE 2: caller must free the allocated memory itself to avoid memory leaking.
470 
471  NOTE 3: for system with scarce memory to be dynamically allocated such as
472  OS kernel or firmware, the API cs_disasm_iter() might be a better choice than
473  cs_disasm(). The reason is that with cs_disasm(), based on limited available
474  memory, we have to calculate in advance how many instructions to be disassembled,
475  which complicates things. This is especially troublesome for the case @count=0,
476  when cs_disasm() runs uncontrollably (until either end of input buffer, or
477  when it encounters an invalid instruction).
478 
479  @handle: handle returned by cs_open()
480  @code: buffer containing raw binary code to be disassembled.
481  @code_size: size of the above code buffer.
482  @address: address of the first instruction in given raw code buffer.
483  @insn: array of instructions filled in by this API.
484 	   NOTE: @insn will be allocated by this function, and should be freed
485 	   with cs_free() API.
486  @count: number of instructions to be disassembled, or 0 to get all of them
487 
488  @return: the number of successfully disassembled instructions,
489  or 0 if this function failed to disassemble the given code
490 
491  On failure, call cs_errno() for error code.
492 */
493 size_t cs_disasm (
494     csh handle,
495     const(ubyte)* code,
496     size_t code_size,
497     ulong address,
498     size_t count,
499     cs_insn** insn);
500 
501 /**
502   Deprecated function - to be retired in the next version!
503   Use cs_disasm() instead of cs_disasm_ex()
504 */
505 size_t cs_disasm_ex (
506     csh handle,
507     const(ubyte)* code,
508     size_t code_size,
509     ulong address,
510     size_t count,
511     cs_insn** insn);
512 
513 /**
514  Free memory allocated by cs_malloc() or cs_disasm() (argument @insn)
515 
516  @insn: pointer returned by @insn argument in cs_disasm() or cs_malloc()
517  @count: number of cs_insn structures returned by cs_disasm(), or 1
518      to free memory allocated by cs_malloc().
519 */
520 void cs_free (cs_insn* insn, size_t count);
521 
522 /**
523  Allocate memory for 1 instruction to be used by cs_disasm_iter().
524 
525  @handle: handle returned by cs_open()
526 
527  NOTE: when no longer in use, you can reclaim the memory allocated for
528  this instruction with cs_free(insn, 1)
529 */
530 cs_insn* cs_malloc (csh handle);
531 
532 /**
533  Fast API to disassemble binary code, given the code buffer, size, address
534  and number of instructions to be decoded.
535  This API puts the resulting instruction into a given cache in @insn.
536  See tests/test_iter.c for sample code demonstrating this API.
537 
538  NOTE 1: this API will update @code, @size & @address to point to the next
539  instruction in the input buffer. Therefore, it is convenient to use
540  cs_disasm_iter() inside a loop to quickly iterate all the instructions.
541  While decoding one instruction at a time can also be achieved with
542  cs_disasm(count=1), some benchmarks shown that cs_disasm_iter() can be 30%
543  faster on random input.
544 
545  NOTE 2: the cache in @insn can be created with cs_malloc() API.
546 
547  NOTE 3: for system with scarce memory to be dynamically allocated such as
548  OS kernel or firmware, this API is recommended over cs_disasm(), which
549  allocates memory based on the number of instructions to be disassembled.
550  The reason is that with cs_disasm(), based on limited available memory,
551  we have to calculate in advance how many instructions to be disassembled,
552  which complicates things. This is especially troublesome for the case
553  @count=0, when cs_disasm() runs uncontrollably (until either end of input
554  buffer, or when it encounters an invalid instruction).
555 
556  @handle: handle returned by cs_open()
557  @code: buffer containing raw binary code to be disassembled
558  @size: size of above code
559  @address: address of the first insn in given raw code buffer
560  @insn: pointer to instruction to be filled in by this API.
561 
562  @return: true if this API successfully decode 1 instruction,
563  or false otherwise.
564 
565  On failure, call cs_errno() for error code.
566 */
567 bool cs_disasm_iter (
568     csh handle,
569     const(ubyte*)* code,
570     size_t* size,
571     ulong* address,
572     cs_insn* insn);
573 
574 /**
575  Return friendly name of register in a string.
576  Find the instruction id from header file of corresponding architecture (arm.h for ARM,
577  x86.h for X86, ...)
578 
579  WARN: when in 'diet' mode, this API is irrelevant because engine does not
580  store register name.
581 
582  @handle: handle returned by cs_open()
583  @reg_id: register id
584 
585  @return: string name of the register, or NULL if @reg_id is invalid.
586 */
587 const(char)* cs_reg_name (csh handle, uint reg_id);
588 
589 /**
590  Return friendly name of an instruction in a string.
591  Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
592 
593  WARN: when in 'diet' mode, this API is irrelevant because the engine does not
594  store instruction name.
595 
596  @handle: handle returned by cs_open()
597  @insn_id: instruction id
598 
599  @return: string name of the instruction, or NULL if @insn_id is invalid.
600 */
601 const(char)* cs_insn_name (csh handle, uint insn_id);
602 
603 /**
604  Return friendly name of a group id (that an instruction can belong to)
605  Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
606 
607  WARN: when in 'diet' mode, this API is irrelevant because the engine does not
608  store group name.
609 
610  @handle: handle returned by cs_open()
611  @group_id: group id
612 
613  @return: string name of the group, or NULL if @group_id is invalid.
614 */
615 const(char)* cs_group_name (csh handle, uint group_id);
616 
617 /**
618  Check if a disassembled instruction belong to a particular group.
619  Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
620  Internally, this simply verifies if @group_id matches any member of insn->groups array.
621 
622  NOTE: this API is only valid when detail option is ON (which is OFF by default).
623 
624  WARN: when in 'diet' mode, this API is irrelevant because the engine does not
625  update @groups array.
626 
627  @handle: handle returned by cs_open()
628  @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
629  @group_id: group that you want to check if this instruction belong to.
630 
631  @return: true if this instruction indeed belongs to the given group, or false otherwise.
632 */
633 bool cs_insn_group (csh handle, const(cs_insn)* insn, uint group_id);
634 
635 /**
636  Check if a disassembled instruction IMPLICITLY used a particular register.
637  Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
638  Internally, this simply verifies if @reg_id matches any member of insn->regs_read array.
639 
640  NOTE: this API is only valid when detail option is ON (which is OFF by default)
641 
642  WARN: when in 'diet' mode, this API is irrelevant because the engine does not
643  update @regs_read array.
644 
645  @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
646  @reg_id: register that you want to check if this instruction used it.
647 
648  @return: true if this instruction indeed implicitly used the given register, or false otherwise.
649 */
650 bool cs_reg_read (csh handle, const(cs_insn)* insn, uint reg_id);
651 
652 /**
653  Check if a disassembled instruction IMPLICITLY modified a particular register.
654  Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
655  Internally, this simply verifies if @reg_id matches any member of insn->regs_write array.
656 
657  NOTE: this API is only valid when detail option is ON (which is OFF by default)
658 
659  WARN: when in 'diet' mode, this API is irrelevant because the engine does not
660  update @regs_write array.
661 
662  @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
663  @reg_id: register that you want to check if this instruction modified it.
664 
665  @return: true if this instruction indeed implicitly modified the given register, or false otherwise.
666 */
667 bool cs_reg_write (csh handle, const(cs_insn)* insn, uint reg_id);
668 
669 /**
670  Count the number of operands of a given type.
671  Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
672 
673  NOTE: this API is only valid when detail option is ON (which is OFF by default)
674 
675  @handle: handle returned by cs_open()
676  @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
677  @op_type: Operand type to be found.
678 
679  @return: number of operands of given type @op_type in instruction @insn,
680  or -1 on failure.
681 */
682 int cs_op_count (csh handle, const(cs_insn)* insn, uint op_type);
683 
684 /**
685  Retrieve the position of operand of given type in <arch>.operands[] array.
686  Later, the operand can be accessed using the returned position.
687  Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
688 
689  NOTE: this API is only valid when detail option is ON (which is OFF by default)
690 
691  @handle: handle returned by cs_open()
692  @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
693  @op_type: Operand type to be found.
694  @position: position of the operand to be found. This must be in the range
695 			[1, cs_op_count(handle, insn, op_type)]
696 
697  @return: index of operand of given type @op_type in <arch>.operands[] array
698  in instruction @insn, or -1 on failure.
699 */
700 int cs_op_index (csh handle, const(cs_insn)* insn, uint op_type, uint position);
701 
702 /// Type of array to keep the list of registers
703 alias cs_regs = ushort[64];
704 
705 /**
706  Retrieve all the registers accessed by an instruction, either explicitly or
707  implicitly.
708 
709  WARN: when in 'diet' mode, this API is irrelevant because engine does not
710  store registers.
711 
712  @handle: handle returned by cs_open()
713  @insn: disassembled instruction structure returned from cs_disasm() or cs_disasm_iter()
714  @regs_read: on return, this array contains all registers read by instruction.
715  @regs_read_count: number of registers kept inside @regs_read array.
716  @regs_write: on return, this array contains all registers written by instruction.
717  @regs_write_count: number of registers kept inside @regs_write array.
718 
719  @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
720  for detailed error).
721 */
722 cs_err cs_regs_access (
723     csh handle,
724     const(cs_insn)* insn,
725     cs_regs regs_read,
726     ubyte* regs_read_count,
727     cs_regs regs_write,
728     ubyte* regs_write_count);
729