Zydis  v2.0
Formatter.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 
3  Zyan Disassembler Library (Zydis)
4 
5  Original Author : Florian Bernd
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
32 #ifndef ZYDIS_FORMATTER_H
33 #define ZYDIS_FORMATTER_H
34 
35 #include <Zycore/Defines.h>
36 #include <Zydis/DecoderTypes.h>
37 #include <Zydis/Status.h>
38 #include <Zydis/String.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* ============================================================================================== */
45 /* Enums and types */
46 /* ============================================================================================== */
47 
48 /* ---------------------------------------------------------------------------------------------- */
49 /* Formatter style */
50 /* ---------------------------------------------------------------------------------------------- */
51 
56 {
66 
76 
77 /* ---------------------------------------------------------------------------------------------- */
78 /* Attributes */
79 /* ---------------------------------------------------------------------------------------------- */
80 
85 {
120  //ZYDIS_FORMATTER_PROP_DETAILED_PREFIXES, // TODO: implement
121 
140 
179 
189 
190 /* ---------------------------------------------------------------------------------------------- */
191 /* Address format */
192 /* ---------------------------------------------------------------------------------------------- */
193 
198 {
239 
249 
250 /* ---------------------------------------------------------------------------------------------- */
251 /* Displacement format */
252 /* ---------------------------------------------------------------------------------------------- */
253 
258 {
275 
285 
286 /* ---------------------------------------------------------------------------------------------- */
287 /* Immediate format */
288 /* ---------------------------------------------------------------------------------------------- */
289 
294 {
316 
326 
327 /* ---------------------------------------------------------------------------------------------- */
328 /* Hook type */
329 /* ---------------------------------------------------------------------------------------------- */
330 
335 {
352 
384 
405 
419 
429 
430 /* ---------------------------------------------------------------------------------------------- */
431 /* Decorator type */
432 /* ---------------------------------------------------------------------------------------------- */
433 
438 {
439  ZYDIS_DECORATOR_TYPE_INVALID,
468 
478 
479 /* ---------------------------------------------------------------------------------------------- */
480 /* Formatter context */
481 /* ---------------------------------------------------------------------------------------------- */
482 
483 typedef struct ZydisFormatter_ ZydisFormatter;
484 
489 {
501  ZyanU64 address;
505  void* user_data;
507 
508 /* ---------------------------------------------------------------------------------------------- */
509 /* Callback definitions */
510 /* ---------------------------------------------------------------------------------------------- */
511 
544 typedef ZyanStatus (*ZydisFormatterFunc)(const ZydisFormatter* formatter,
545  ZydisString* string, ZydisFormatterContext* context);
546 
561 typedef ZyanStatus (*ZydisFormatterRegisterFunc)(const ZydisFormatter* formatter,
562  ZydisString* string, ZydisFormatterContext* instruction, ZydisRegister reg);
563 
578 typedef ZyanStatus (*ZydisFormatterAddressFunc)(const ZydisFormatter* formatter,
579  ZydisString* string, ZydisFormatterContext* context, ZyanU64 address);
580 
595 typedef ZyanStatus (*ZydisFormatterDecoratorFunc)(const ZydisFormatter* formatter,
596  ZydisString* string, ZydisFormatterContext* context, ZydisDecoratorType decorator);
597 
598 /* ---------------------------------------------------------------------------------------------- */
599 /* Formatter struct */
600 /* ---------------------------------------------------------------------------------------------- */
601 
609 {
610  ZydisLetterCase letter_case;
611  ZyanBool force_memory_segment;
612  ZyanBool force_memory_size;
613  ZyanU8 format_address;
614  ZyanU8 format_disp;
615  ZyanU8 format_imm;
616  ZyanBool hex_uppercase;
617  ZydisString* hex_prefix;
618  ZydisString hex_prefix_data;
619  ZydisString* hex_suffix;
620  ZydisString hex_suffix_data;
621  ZyanU8 hex_padding_address;
622  ZyanU8 hex_padding_disp;
623  ZyanU8 hex_padding_imm;
624  ZydisFormatterFunc func_pre_instruction;
625  ZydisFormatterFunc func_post_instruction;
626  ZydisFormatterFunc func_pre_operand;
627  ZydisFormatterFunc func_post_operand;
628  ZydisFormatterFunc func_format_instruction;
629  ZydisFormatterFunc func_format_operand_reg;
630  ZydisFormatterFunc func_format_operand_mem;
631  ZydisFormatterFunc func_format_operand_ptr;
632  ZydisFormatterFunc func_format_operand_imm;
633  ZydisFormatterFunc func_print_mnemonic;
634  ZydisFormatterRegisterFunc func_print_register;
635  ZydisFormatterAddressFunc func_print_address;
636  ZydisFormatterFunc func_print_disp;
637  ZydisFormatterFunc func_print_imm;
638  ZydisFormatterFunc func_print_mem_size;
639  ZydisFormatterFunc func_print_prefixes;
640  ZydisFormatterDecoratorFunc func_print_decorator;
641 };
642 
643 /* ---------------------------------------------------------------------------------------------- */
644 
645 /* ============================================================================================== */
646 /* Exported functions */
647 /* ============================================================================================== */
648 
657 ZYDIS_EXPORT ZyanStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle style);
658 
668 ZYDIS_EXPORT ZyanStatus ZydisFormatterSetProperty(ZydisFormatter* formatter,
669  ZydisFormatterProperty property, ZyanUPointer value);
670 
685 ZYDIS_EXPORT ZyanStatus ZydisFormatterSetHook(ZydisFormatter* formatter,
686  ZydisFormatterHookType hook, const void** callback);
687 
699 ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatInstruction(const ZydisFormatter* formatter,
700  const ZydisDecodedInstruction* instruction, char* buffer, ZyanUSize length, ZyanU64 address);
701 
715 ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatInstructionEx(const ZydisFormatter* formatter,
716  const ZydisDecodedInstruction* instruction, char* buffer, ZyanUSize length, ZyanU64 address,
717  void* user_data);
718 
734 ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatOperand(const ZydisFormatter* formatter,
735  const ZydisDecodedInstruction* instruction, ZyanU8 index, char* buffer, ZyanUSize length,
736  ZyanU64 address);
737 
755 ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatOperandEx(const ZydisFormatter* formatter,
756  const ZydisDecodedInstruction* instruction, ZyanU8 index, char* buffer, ZyanUSize length,
757  ZyanU64 address, void* user_data);
758 
759 /* ============================================================================================== */
760 
761 #ifdef __cplusplus
762 }
763 #endif
764 
765 #endif /* ZYDIS_FORMATTER_H */
This function is invoked before the formatter formats an instruction.
Definition: Formatter.h:339
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:476
The register-swizzle decorator.
Definition: Formatter.h:459
ZyanStatus(* ZydisFormatterAddressFunc)(const ZydisFormatter *formatter, ZydisString *string, ZydisFormatterContext *context, ZyanU64 address)
Defines the ZydisFormatterAddressFunc function pointer.
Definition: Formatter.h:578
enum ZydisImmediateFormat_ ZydisImmediateFormat
Defines the ZydisImmediateFormat enum.
Defines the ZydisFormatterContext struct.
Definition: Formatter.h:488
This function is invoked to format a memory operand.
Definition: Formatter.h:372
enum ZydisFormatterStyle_ ZydisFormatterStyle
Defines the ZydisFormatterStyle enum.
ZydisFormatterHookType_
Defines the ZydisFormatterHookType enum.
Definition: Formatter.h:334
This function is invoked to print a memory displacement value.
Definition: Formatter.h:400
ZyanStatus(* ZydisFormatterFunc)(const ZydisFormatter *formatter, ZydisString *string, ZydisFormatterContext *context)
Defines the ZydisFormatterFunc function pointer.
Definition: Formatter.h:544
This function is invoked to print the instruction mnemonic.
Definition: Formatter.h:388
This function is invoked to print the size of a memory operand.
Definition: Formatter.h:409
ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatInstruction(const ZydisFormatter *formatter, const ZydisDecodedInstruction *instruction, char *buffer, ZyanUSize length, ZyanU64 address)
Formats the given instruction and writes it into the output buffer.
enum ZydisFormatterHookType_ ZydisFormatterHookType
Defines the ZydisFormatterHookType enum.
ZYDIS_EXPORT ZyanStatus ZydisFormatterSetHook(ZydisFormatter *formatter, ZydisFormatterHookType hook, const void **callback)
Replaces a formatter function with a custom callback and/or retrieves the currently used function...
enum ZydisDisplacementFormat_ ZydisDisplacementFormat
Defines the ZydisDisplacementFormat enum.
Maximum value of this enum.
Definition: Formatter.h:243
The embedded-mask decorator.
Definition: Formatter.h:443
ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatOperandEx(const ZydisFormatter *formatter, const ZydisDecodedInstruction *instruction, ZyanU8 index, char *buffer, ZyanUSize length, ZyanU64 address, void *user_data)
Formats the given operand and writes it into the output buffer.
ZydisFormatterStyle_
Defines the ZydisFormatterStyle enum.
Definition: Formatter.h:55
Maximum value of this enum.
Definition: Formatter.h:320
Generates MASM-style disassembly that is directly accepted as input for the MASM assembler.
Definition: Formatter.h:65
Controls the padding (minimum number of chars) of hexadecimal address values.
Definition: Formatter.h:166
enum ZydisAddressFormat_ ZydisAddressFormat
Defines the ZydisAddressFormat enum.
The eviction-hint decorator.
Definition: Formatter.h:467
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:247
const ZydisDecodedInstruction * instruction
A pointer to the ZydisDecodedInstruction struct.
Definition: Formatter.h:493
Uses signed hexadecimal values to display relative addresses that are accepted as direct input by mos...
Definition: Formatter.h:238
Controls the format of displacement values.
Definition: Formatter.h:133
ZyanStatus(* ZydisFormatterDecoratorFunc)(const ZydisFormatter *formatter, ZydisString *string, ZydisFormatterContext *context, ZydisDecoratorType decorator)
Defines the ZydisFormatterDecoratorFunc function pointer.
Definition: Formatter.h:595
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:283
Formats immediates as signed hexadecimal values.
Definition: Formatter.h:307
Maximum value of this enum.
Definition: Formatter.h:279
This function is invoked to format a register operand.
Definition: Formatter.h:364
ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatInstructionEx(const ZydisFormatter *formatter, const ZydisDecodedInstruction *instruction, char *buffer, ZyanUSize length, ZyanU64 address, void *user_data)
Formats the given instruction and writes it into the output buffer.
Formats immediates as unsigned hexadecimal values.
Definition: Formatter.h:315
Maximum value of this enum.
Definition: Formatter.h:472
enum ZydisDecoratorTypes_ ZydisDecoratorType
Defines the ZydisDecoratorType enum.
Maximum value of this enum.
Definition: Formatter.h:70
Defines the basic ZydisDecodedInstruction and ZydisDecodedOperand structs.
Controls the padding (minimum number of chars) of hexadecimal displacement values.
Definition: Formatter.h:172
void * user_data
A pointer to user-defined data.
Definition: Formatter.h:505
This function is invoked to format a pointer operand.
Definition: Formatter.h:376
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:187
Automatically chooses the most suitable formatting-mode based on the operands ZydisOperandInfo.imm.is_signed attribute.
Definition: Formatter.h:299
The broadcast decorator.
Definition: Formatter.h:447
ZydisImmediateFormat_
Defines the ZydisImmediateFormat enum.
Definition: Formatter.h:293
ZYDIS_EXPORT ZyanStatus ZydisFormatterFormatOperand(const ZydisFormatter *formatter, const ZydisDecodedInstruction *instruction, ZyanU8 index, char *buffer, ZyanUSize length, ZyanU64 address)
Formats the given operand and writes it into the output buffer.
ZydisAddressFormat_
Defines the ZydisAddressFormat enum.
Definition: Formatter.h:197
This function is invoked to print a register.
Definition: Formatter.h:392
This function is invoked to print an immediate value.
Definition: Formatter.h:404
Sets the suffix for hexadecimal values.
Definition: Formatter.h:160
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:324
This function is invoked to print an absolute address.
Definition: Formatter.h:396
ZyanU64 address
The runtime address of the instruction.
Definition: Formatter.h:501
Controls the padding (minimum number of chars) of hexadecimal immediate values.
Definition: Formatter.h:178
This function is invoked to print the instruction prefixes.
Definition: Formatter.h:413
Controls the printing of instruction prefixes.
Definition: Formatter.h:127
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:74
Uses unsigned hexadecimal values to display relative addresses.
Definition: Formatter.h:216
Defines the ZydisDecodedOperand struct.
Definition: DecoderTypes.h:91
Controls the letter-case of hexadecimal values.
Definition: Formatter.h:148
Displays absolute addresses instead of relative ones.
Definition: Formatter.h:205
ZydisFormatterProperty_
Defines the ZydisFormatterProperty enum.
Definition: Formatter.h:84
struct ZydisFormatterContext_ ZydisFormatterContext
Defines the ZydisFormatterContext struct.
Controls the printing of segment prefixes.
Definition: Formatter.h:102
ZYDIS_EXPORT ZyanStatus ZydisFormatterSetProperty(ZydisFormatter *formatter, ZydisFormatterProperty property, ZyanUPointer value)
Sets the value of the specified formatter attribute.
Defines the ZydisString struct.
Definition: String.h:53
This function is invoked before the formatter formats an operand.
Definition: Formatter.h:347
This function is invoked to format an immediate operand.
Definition: Formatter.h:383
This function refers to the main formatting function.
Definition: Formatter.h:360
This function is invoked after formatting an operand to print a EVEX/MVEX decorator.
Definition: Formatter.h:418
Maximum value of this enum.
Definition: Formatter.h:423
Uses signed hexadecimal values to display relative addresses.
Definition: Formatter.h:227
Defines the ZydisDecodedInstruction struct.
Definition: DecoderTypes.h:771
Controls the printing of memory-operand sizes.
Definition: Formatter.h:111
Status code definitions and check macros.
The minimum number of bits required to represent all values of this enum.
Definition: Formatter.h:427
ZydisDecoratorTypes_
Defines the ZydisDecoratorType enum.
Definition: Formatter.h:437
ZydisDisplacementFormat_
Defines the ZydisDisplacementFormat enum.
Definition: Formatter.h:257
Controls the letter-case.
Definition: Formatter.h:93
ZyanStatus(* ZydisFormatterRegisterFunc)(const ZydisFormatter *formatter, ZydisString *string, ZydisFormatterContext *instruction, ZydisRegister reg)
Defines the ZydisFormatterRegisterFunc function pointer.
Definition: Formatter.h:561
Formats displacements as unsigned hexadecimal values.
Definition: Formatter.h:274
const ZydisDecodedOperand * operand
A pointer to the ZydisDecodedOperand struct.
Definition: Formatter.h:497
Maximum value of this enum.
Definition: Formatter.h:183
Formats displacements as signed hexadecimal values.
Definition: Formatter.h:266
Generates intel-style disassembly.
Definition: Formatter.h:60
This function is invoked after the formatter formatted an instruction.
Definition: Formatter.h:343
The rounding-control decorator.
Definition: Formatter.h:451
Defines the ZydisFormatter struct.
Definition: Formatter.h:608
Sets the prefix for hexadecimal values.
Definition: Formatter.h:154
The suppress-all-exceptions decorator.
Definition: Formatter.h:455
enum ZydisFormatterProperty_ ZydisFormatterProperty
Defines the ZydisFormatterProperty enum.
Controls the format of immediate values.
Definition: Formatter.h:139
ZYDIS_EXPORT ZyanStatus ZydisFormatterInit(ZydisFormatter *formatter, ZydisFormatterStyle style)
Initializes the given ZydisFormatter instance.
The conversion decorator.
Definition: Formatter.h:463
This function is invoked after the formatter formatted an operand.
Definition: Formatter.h:351