2016-05-26 03:25:48 +08:00
|
|
|
/***************************************************************************************************
|
|
|
|
|
2016-12-05 09:24:01 +08:00
|
|
|
Zyan Disassembler Library (Zydis)
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
Original Author : Florian Bernd
|
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
|
|
|
|
***************************************************************************************************/
|
|
|
|
|
2017-07-25 07:04:25 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Functions for formatting human-readable instructions.
|
|
|
|
*/
|
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
#ifndef ZYDIS_FORMATTER_H
|
|
|
|
#define ZYDIS_FORMATTER_H
|
|
|
|
|
2017-07-06 06:34:36 +08:00
|
|
|
#include <Zydis/DecoderTypes.h>
|
2016-05-26 03:25:48 +08:00
|
|
|
#include <Zydis/Defines.h>
|
|
|
|
#include <Zydis/Status.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|
|
|
|
/* Enums and types */
|
2016-11-21 21:55:17 +08:00
|
|
|
/* ============================================================================================== */
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
/**
|
2016-11-21 21:55:17 +08:00
|
|
|
* @brief Defines the @c ZydisFormatterStyle datatype.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
|
|
|
typedef uint8_t ZydisFormatterStyle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Values that represent formatter-styles.
|
|
|
|
*/
|
|
|
|
enum ZydisFormatterStyles
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @brief Generates intel-style disassembly.
|
|
|
|
*/
|
2017-09-22 05:50:44 +08:00
|
|
|
ZYDIS_FORMATTER_STYLE_INTEL,
|
|
|
|
/**
|
|
|
|
* @brief Maximum value of this enum.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_STYLE_MAX_VALUE = ZYDIS_FORMATTER_STYLE_INTEL
|
2016-05-26 03:25:48 +08:00
|
|
|
};
|
|
|
|
|
2016-11-26 20:08:37 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
2016-11-26 20:08:37 +08:00
|
|
|
* @brief Defines the @c ZydisFormatFlags datatype.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
2016-11-29 02:12:28 +08:00
|
|
|
typedef uint32_t ZydisFormatterFlags;
|
2016-05-26 03:25:48 +08:00
|
|
|
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Formats the instruction in uppercase instead of lowercase.
|
|
|
|
*/
|
2017-09-24 01:46:27 +08:00
|
|
|
#define ZYDIS_FMTFLAG_UPPERCASE 0x00000001 // (1 << 0)
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Forces the formatter to always print the segment register of memory-operands, instead
|
|
|
|
* of ommiting implicit DS/SS segments.
|
|
|
|
*/
|
2017-09-24 01:46:27 +08:00
|
|
|
#define ZYDIS_FMTFLAG_FORCE_SEGMENTS 0x00000002 // (1 << 1)
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Forces the formatter to always print the size of memory-operands.
|
|
|
|
*/
|
2017-09-24 01:46:27 +08:00
|
|
|
#define ZYDIS_FMTFLAG_FORCE_OPERANDSIZE 0x00000004 // (1 << 2)
|
2016-11-26 20:08:37 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
/**
|
2016-11-21 21:55:17 +08:00
|
|
|
* @brief Defines the @c ZydisFormatterAddressFormat datatype.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
|
|
|
typedef uint8_t ZydisFormatterAddressFormat;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Values that represent address-formats.
|
|
|
|
*/
|
2016-11-29 18:49:38 +08:00
|
|
|
enum ZydisFormatterAddressFormats
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Currently defaults to @c ZYDIS_FORMATTER_ADDR_ABSOLUTE.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_ADDR_DEFAULT,
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Displays absolute addresses instead of relative ones.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_ADDR_ABSOLUTE,
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Uses signed hexadecimal values to display relative addresses.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* "JMP 0x20"
|
|
|
|
* "JMP -0x20"
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_ADDR_RELATIVE_SIGNED,
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Uses unsigned hexadecimal values to display relative addresses.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* "JMP 0x20"
|
|
|
|
* "JMP 0xE0"
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_ADDR_RELATIVE_UNSIGNED,
|
2017-09-22 05:50:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Maximum value of this enum.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_ADDR_MAX_VALUE = ZYDIS_FORMATTER_ADDR_RELATIVE_UNSIGNED
|
2016-05-26 03:25:48 +08:00
|
|
|
};
|
|
|
|
|
2016-11-26 20:08:37 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
2016-11-21 21:55:17 +08:00
|
|
|
* @brief Defines the @c ZydisFormatterDisplacementFormat datatype.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
|
|
|
typedef uint8_t ZydisFormatterDisplacementFormat;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Values that represent displacement-formats.
|
|
|
|
*/
|
|
|
|
enum ZydisFormatterDisplacementFormats
|
|
|
|
{
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Currently defaults to @c ZYDIS_FORMATTER_DISP_HEX_SIGNED.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_DISP_DEFAULT,
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Formats displacements as signed hexadecimal values.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* "MOV EAX, DWORD PTR SS:[ESP+0x400]"
|
|
|
|
* "MOV EAX, DWORD PTR SS:[ESP-0x400]"
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_DISP_HEX_SIGNED,
|
|
|
|
/**
|
|
|
|
* @brief Formats displacements as unsigned hexadecimal values.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* "MOV EAX, DWORD PTR SS:[ESP+0x400]"
|
|
|
|
* "MOV EAX, DWORD PTR SS:[ESP+0xFFFFFC00]"
|
|
|
|
*/
|
2017-09-22 05:50:44 +08:00
|
|
|
ZYDIS_FORMATTER_DISP_HEX_UNSIGNED,
|
|
|
|
/**
|
|
|
|
* @brief Maximum value of this enum.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_DISP_MAX_VALUE = ZYDIS_FORMATTER_DISP_HEX_UNSIGNED
|
2016-05-26 03:25:48 +08:00
|
|
|
};
|
|
|
|
|
2016-11-26 20:08:37 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
2016-11-21 21:55:17 +08:00
|
|
|
* @brief Defines the @c ZydisFormatterImmediateFormat datatype.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
|
|
|
typedef uint8_t ZydisFormatterImmediateFormat;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Values that represent formatter immediate-formats.
|
|
|
|
*/
|
|
|
|
enum ZydisFormatterImmediateFormats
|
|
|
|
{
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Currently defaults to @c ZYDIS_FORMATTER_IMM_HEX_UNSIGNED.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_IMM_DEFAULT,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Automatically chooses the most suitable formatting-mode based on the operands
|
|
|
|
* @c ZydisOperandInfo.imm.isSigned attribute.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_IMM_HEX_AUTO,
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Formats immediates as signed hexadecimal values.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* "MOV EAX, 0x400"
|
|
|
|
* "MOV EAX, -0x400"
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_IMM_HEX_SIGNED,
|
|
|
|
/**
|
|
|
|
* @brief Formats immediates as unsigned hexadecimal values.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* "MOV EAX, 0x400"
|
|
|
|
* "MOV EAX, 0xFFFFFC00"
|
|
|
|
*/
|
2017-09-22 05:50:44 +08:00
|
|
|
ZYDIS_FORMATTER_IMM_HEX_UNSIGNED,
|
|
|
|
/**
|
|
|
|
* @brief Maximum value of this enum.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_IMM_MAX_VALUE = ZYDIS_FORMATTER_IMM_HEX_UNSIGNED
|
2016-05-26 03:25:48 +08:00
|
|
|
};
|
|
|
|
|
2016-11-26 20:08:37 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
2016-11-21 21:55:17 +08:00
|
|
|
* @brief Defines the @c ZydisFormatterHookType datatype.
|
|
|
|
*/
|
|
|
|
typedef uint8_t ZydisFormatterHookType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Values that represent formatter hook-types.
|
|
|
|
*/
|
|
|
|
enum ZydisFormatterHookTypes
|
|
|
|
{
|
|
|
|
/**
|
2016-11-26 20:08:37 +08:00
|
|
|
* @brief This function is called before the formatter starts formatting an instruction.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_PRE,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called before the formatter finished formatting an instruction.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_POST,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function refers to the main formatting function, that internally calls all
|
|
|
|
* other function except the ones that are hooked by @c ZYDIS_FORMATTER_HOOK_PRE and
|
|
|
|
* @c ZYDIS_FORMATTER_HOOK_POST.
|
|
|
|
*
|
|
|
|
* Replacing this function allows for complete custom formatting, but indirectly disables all
|
|
|
|
* other hooks except for @c ZYDIS_FORMATTER_HOOK_PRE and @c ZYDIS_FORMATTER_HOOK_POST.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_FORMAT_INSTRUCTION,
|
|
|
|
/**
|
|
|
|
* @brief This function is called to print the instruction prefixes.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_PREFIXES,
|
|
|
|
/**
|
|
|
|
* @brief This function is called to print the instruction mnemonic.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_MNEMONIC,
|
|
|
|
/**
|
|
|
|
* @brief This function is called to format an register operand.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_REG,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called to format an memory operand.
|
|
|
|
*
|
|
|
|
* Replacing this function might indirectly disable some specific calls to the
|
2016-11-28 18:14:47 +08:00
|
|
|
* @c ZYDIS_FORMATTER_PRINT_ADDRESS and @c ZYDIS_FORMATTER_HOOK_PRINT_DISPLACEMENT functions.
|
2016-11-26 20:08:37 +08:00
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_MEM,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called to format an pointer operand.
|
|
|
|
*/
|
2016-11-21 21:55:17 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_PTR,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called to format an immediate operand.
|
|
|
|
*
|
|
|
|
* Replacing this function might indirectly disable some specific calls to the
|
2016-11-28 18:14:47 +08:00
|
|
|
* @c ZYDIS_FORMATTER_PRINT_ADDRESS and @c ZYDIS_FORMATTER_HOOK_PRINT_IMMEDIATE functions.
|
2016-11-26 20:08:37 +08:00
|
|
|
*/
|
2016-11-27 01:41:58 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_IMM,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called right before formatting an memory operand to print the
|
|
|
|
* optional size-specifier.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_OPERANDSIZE,
|
2016-11-27 01:41:58 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called right before formatting an memory operand to print the
|
|
|
|
* optional segment-register.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_SEGMENT,
|
2016-11-26 20:08:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function is called right after formatting an operand to print the optional
|
2017-06-29 01:50:33 +08:00
|
|
|
* EVEX/MVEX operand-decorator.
|
2016-11-26 20:08:37 +08:00
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_DECORATOR,
|
2016-11-28 18:14:47 +08:00
|
|
|
/**
|
2016-11-26 20:08:37 +08:00
|
|
|
* @brief This function is called to print an absolute address.
|
|
|
|
*/
|
2016-11-28 18:14:47 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_ADDRESS,
|
|
|
|
/**
|
|
|
|
* @brief This function is called to print a memory displacement value.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_DISPLACEMENT,
|
|
|
|
/**
|
|
|
|
* @brief This function is called to print an immediate value.
|
|
|
|
*/
|
2017-09-22 05:50:44 +08:00
|
|
|
ZYDIS_FORMATTER_HOOK_PRINT_IMMEDIATE,
|
|
|
|
/**
|
|
|
|
* @brief Maximum value of this enum.
|
|
|
|
*/
|
|
|
|
ZYDIS_FORMATTER_HOOK_MAX_VALUE = ZYDIS_FORMATTER_HOOK_PRINT_IMMEDIATE
|
2016-11-21 21:55:17 +08:00
|
|
|
};
|
|
|
|
|
2016-11-26 20:08:37 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2017-07-05 01:02:11 +08:00
|
|
|
/**
|
|
|
|
* @brief Defines the @c ZydisDecoratorType datatype.
|
|
|
|
*/
|
|
|
|
typedef uint8_t ZydisDecoratorType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Values that represent decorator-types.
|
|
|
|
*/
|
|
|
|
enum ZydisDecoratorTypes
|
|
|
|
{
|
|
|
|
ZYDIS_DECORATOR_TYPE_INVALID,
|
|
|
|
ZYDIS_DECORATOR_TYPE_MASK,
|
|
|
|
ZYDIS_DECORATOR_TYPE_BROADCAST,
|
|
|
|
ZYDIS_DECORATOR_TYPE_ROUNDING_CONTROL,
|
|
|
|
ZYDIS_DECORATOR_TYPE_SAE,
|
|
|
|
ZYDIS_DECORATOR_TYPE_SWIZZLE,
|
|
|
|
ZYDIS_DECORATOR_TYPE_CONVERSION,
|
2017-09-22 05:50:44 +08:00
|
|
|
ZYDIS_DECORATOR_TYPE_EVICTION_HINT,
|
|
|
|
/**
|
|
|
|
* @brief Maximum value of this enum.
|
|
|
|
*/
|
|
|
|
ZYDIS_DECORATOR_TYPE_MAX_VALUE = ZYDIS_DECORATOR_TYPE_EVICTION_HINT
|
2017-07-05 01:02:11 +08:00
|
|
|
};
|
|
|
|
|
2017-07-03 23:36:03 +08:00
|
|
|
typedef struct ZydisFormatter_ ZydisFormatter;
|
2016-11-26 20:08:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Defines the @c ZydisFormatterNotifyFunc function pointer.
|
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2017-07-03 09:14:01 +08:00
|
|
|
* @param instruction A pointer to the @c ZydisDecodedInstruction struct.
|
2016-11-26 20:08:37 +08:00
|
|
|
*
|
|
|
|
* @return Returning a status code other than @c ZYDIS_STATUS_SUCCESS will immediately cause the
|
|
|
|
* formatting process to fail.
|
|
|
|
*
|
|
|
|
* This function type is used for the @c ZYDIS_FORMATTER_HOOK_PRE and
|
|
|
|
* @c ZYDIS_FORMATTER_HOOK_POST hook-types.
|
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
typedef ZydisStatus (*ZydisFormatterNotifyFunc)(const ZydisFormatter* formatter,
|
2017-07-03 09:14:01 +08:00
|
|
|
ZydisDecodedInstruction* instruction);
|
2016-11-26 20:08:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Defines the @c ZydisFormatterFormatFunc function pointer.
|
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2016-11-26 20:08:37 +08:00
|
|
|
* @param buffer A pointer to the string-buffer.
|
|
|
|
* @param bufferLen The length of the string-buffer.
|
2017-07-03 09:14:01 +08:00
|
|
|
* @param instruction A pointer to the @c ZydisDecodedInstruction struct.
|
2016-11-26 20:08:37 +08:00
|
|
|
*
|
|
|
|
* @return Returning a status code other than @c ZYDIS_STATUS_SUCCESS will immediately cause the
|
|
|
|
* formatting process to fail.
|
|
|
|
*
|
|
|
|
* After appending text to the @c buffer you MUST increase the buffer-pointer by the size of the
|
|
|
|
* number of chars written. Not increasing the buffer-pointer will cause unexpected behavior.
|
|
|
|
*
|
|
|
|
* This function type is used for the @c ZYDIS_FORMATTER_HOOK_FORMAT_INSTRUCTION,
|
|
|
|
* @c ZYDIS_FORMATTER_HOOK_PRINT_PREFIXES and @c ZYDIS_FORMATTER_HOOK_PRINT_MNEMONIC hook-types.
|
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
typedef ZydisStatus (*ZydisFormatterFormatFunc)(const ZydisFormatter* formatter,
|
2017-07-03 09:14:01 +08:00
|
|
|
char** buffer, size_t bufferLen, ZydisDecodedInstruction* instruction);
|
2016-11-26 20:08:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Defines the @c ZydisFormatterFormatOperandFunc function pointer.
|
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2016-11-26 20:08:37 +08:00
|
|
|
* @param buffer A pointer to the string-buffer.
|
|
|
|
* @param bufferLen The length of the string-buffer.
|
2017-07-03 09:14:01 +08:00
|
|
|
* @param instruction A pointer to the @c ZydisDecodedInstruction struct.
|
|
|
|
* @param operand A pointer to the @c ZydisDecodedOperand struct.
|
2016-11-26 20:08:37 +08:00
|
|
|
*
|
|
|
|
* @return Returning a status code other than @c ZYDIS_STATUS_SUCCESS will immediately cause the
|
|
|
|
* formatting process to fail.
|
|
|
|
*
|
|
|
|
* After appending text to the @c buffer you MUST increase the buffer-pointer by the size of the
|
|
|
|
* number of chars written.
|
|
|
|
*
|
|
|
|
* Returning @c ZYDIS_STATUS_SUCCESS in one of the @c ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_X hooks
|
|
|
|
* without increasing the buffer-pointer is valid and will cause the formatter to omit the current
|
|
|
|
* operand.
|
|
|
|
*
|
2016-11-27 01:41:58 +08:00
|
|
|
* Returning @c ZYDIS_STATUS_SUCCESS in @c ZYDIS_FORMATTER_HOOK_PRINT_OPERANDSIZE,
|
|
|
|
* @c ZYDIS_FORMATTER_HOOK_PRINT_SEGMENT or @c ZYDIS_FORMATTER_HOOK_PRINT_DECORATOR without
|
|
|
|
* increasing the buffer-pointer is valid and signals that the corresponding element should not be
|
|
|
|
* printed for the current operand.
|
2016-11-26 20:08:37 +08:00
|
|
|
*
|
|
|
|
* Not increasing the buffer-pointer for any other hook-type will cause unexpected behavior.
|
|
|
|
*
|
|
|
|
* This function type is used for the @c ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_REG,
|
|
|
|
* @c ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_MEM, @c ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_PTR,
|
2016-11-27 01:41:58 +08:00
|
|
|
* @c ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_IMM, @c ZYDIS_FORMATTER_HOOK_PRINT_OPERANDSIZE,
|
2016-11-28 18:14:47 +08:00
|
|
|
* @c ZYDIS_FORMATTER_HOOK_PRINT_SEGMENT, @c ZYDIS_FORMATTER_HOOK_PRINT_DECORATOR,
|
|
|
|
* @c ZYDIS_FORMATTER_HOOK_PRINT_DISPLACEMENT and @c ZYDIS_FORMATTER_HOOK_PRINT_IMMEDIATE
|
|
|
|
* hook-types.
|
2016-11-26 20:08:37 +08:00
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
typedef ZydisStatus (*ZydisFormatterFormatOperandFunc)(const ZydisFormatter* formatter,
|
2017-07-03 09:14:01 +08:00
|
|
|
char** buffer, size_t bufferLen, ZydisDecodedInstruction* instruction,
|
|
|
|
ZydisDecodedOperand* operand);
|
2016-11-26 20:08:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Defines the @c ZydisFormatterFormatAddressFunc function pointer.
|
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2016-11-26 20:08:37 +08:00
|
|
|
* @param buffer A pointer to the string-buffer.
|
|
|
|
* @param bufferLen The length of the string-buffer.
|
2017-07-03 09:14:01 +08:00
|
|
|
* @param instruction A pointer to the @c ZydisDecodedInstruction struct.
|
|
|
|
* @param operand A pointer to the @c ZydisDecodedOperand struct.
|
2016-11-26 20:08:37 +08:00
|
|
|
*
|
|
|
|
* @return Returning a status code other than @c ZYDIS_STATUS_SUCCESS will immediately cause the
|
|
|
|
* formatting process to fail.
|
|
|
|
*
|
|
|
|
* After appending text to the @c buffer you MUST increase the buffer-pointer by the size of the
|
|
|
|
* number of chars written.
|
|
|
|
* Not increasing the buffer-pointer will cause unexpected behavior.
|
|
|
|
*
|
|
|
|
* This function type is used for the @c ZYDIS_FORMATTER_HOOK_PRINT_ADDRESS hook-type.
|
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
typedef ZydisStatus (*ZydisFormatterFormatAddressFunc)(const ZydisFormatter* formatter,
|
2017-07-03 09:14:01 +08:00
|
|
|
char** buffer, size_t bufferLen, ZydisDecodedInstruction* instruction,
|
|
|
|
ZydisDecodedOperand* operand, uint64_t address);
|
2016-11-21 21:55:17 +08:00
|
|
|
|
2017-07-05 01:02:11 +08:00
|
|
|
/**
|
|
|
|
* @brief Defines the @c ZydisFormatterFormatDecoratorFunc function pointer.
|
|
|
|
*
|
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
|
|
|
* @param buffer A pointer to the string-buffer.
|
|
|
|
* @param bufferLen The length of the string-buffer.
|
|
|
|
* @param instruction A pointer to the @c ZydisDecodedInstruction struct.
|
|
|
|
* @param operand A pointer to the @c ZydisDecodedOperand struct.
|
|
|
|
* @param type The decorator type.
|
|
|
|
*
|
|
|
|
* @return Returning a status code other than @c ZYDIS_STATUS_SUCCESS will immediately cause the
|
|
|
|
* formatting process to fail.
|
|
|
|
*
|
|
|
|
* After appending text to the @c buffer you MUST increase the buffer-pointer by the size of the
|
|
|
|
* number of chars written.
|
|
|
|
*
|
|
|
|
* Returning @c ZYDIS_STATUS_SUCCESS without increasing the buffer-pointer is valid and will cause
|
|
|
|
* the formatter to omit the current decorator.
|
|
|
|
*
|
|
|
|
* This function type is used for the @c ZYDIS_FORMATTER_HOOK_PRINT_DECORATOR hook-type.
|
|
|
|
*/
|
|
|
|
typedef ZydisStatus (*ZydisFormatterFormatDecoratorFunc)(const ZydisFormatter* formatter,
|
|
|
|
char** buffer, size_t bufferLen, ZydisDecodedInstruction* instruction,
|
2017-07-15 09:39:48 +08:00
|
|
|
ZydisDecodedOperand* operand, ZydisDecoratorType type);
|
2017-07-05 01:02:11 +08:00
|
|
|
|
2016-11-21 21:55:17 +08:00
|
|
|
/**
|
2017-07-03 23:36:03 +08:00
|
|
|
* @brief Defines the @c ZydisFormatter struct.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
struct ZydisFormatter_
|
2016-05-26 03:25:48 +08:00
|
|
|
{
|
2016-11-29 02:12:28 +08:00
|
|
|
ZydisFormatterFlags flags;
|
2016-05-26 03:25:48 +08:00
|
|
|
ZydisFormatterAddressFormat addressFormat;
|
|
|
|
ZydisFormatterDisplacementFormat displacementFormat;
|
|
|
|
ZydisFormatterImmediateFormat immediateFormat;
|
2016-11-26 20:08:37 +08:00
|
|
|
ZydisFormatterNotifyFunc funcPre;
|
|
|
|
ZydisFormatterNotifyFunc funcPost;
|
|
|
|
ZydisFormatterFormatFunc funcFormatInstruction;
|
|
|
|
ZydisFormatterFormatFunc funcPrintPrefixes;
|
|
|
|
ZydisFormatterFormatFunc funcPrintMnemonic;
|
|
|
|
ZydisFormatterFormatOperandFunc funcFormatOperandReg;
|
|
|
|
ZydisFormatterFormatOperandFunc funcFormatOperandMem;
|
|
|
|
ZydisFormatterFormatOperandFunc funcFormatOperandPtr;
|
|
|
|
ZydisFormatterFormatOperandFunc funcFormatOperandImm;
|
|
|
|
ZydisFormatterFormatOperandFunc funcPrintOperandSize;
|
2016-11-27 01:41:58 +08:00
|
|
|
ZydisFormatterFormatOperandFunc funcPrintSegment;
|
2017-07-05 01:02:11 +08:00
|
|
|
ZydisFormatterFormatDecoratorFunc funcPrintDecorator;
|
2016-11-26 20:08:37 +08:00
|
|
|
ZydisFormatterFormatAddressFunc funcPrintAddress;
|
2016-11-28 18:14:47 +08:00
|
|
|
ZydisFormatterFormatOperandFunc funcPrintDisplacement;
|
2017-07-05 01:02:11 +08:00
|
|
|
ZydisFormatterFormatOperandFunc funcPrintImmediate;
|
2016-11-29 01:56:39 +08:00
|
|
|
};
|
2016-05-26 03:25:48 +08:00
|
|
|
|
2016-11-21 21:55:17 +08:00
|
|
|
/* ============================================================================================== */
|
2016-05-26 03:25:48 +08:00
|
|
|
/* Exported functions */
|
2016-11-21 21:55:17 +08:00
|
|
|
/* ============================================================================================== */
|
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
2017-07-03 23:36:03 +08:00
|
|
|
* @brief Initializes the given @c ZydisFormatter instance.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2016-11-26 20:08:37 +08:00
|
|
|
* @param style The formatter style.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
|
|
|
* @return A zydis status code.
|
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
ZYDIS_EXPORT ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle style);
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
/**
|
2017-07-03 23:36:03 +08:00
|
|
|
* @brief Initializes the given @c ZydisFormatter instance.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2016-11-26 20:08:37 +08:00
|
|
|
* @param style The formatter style.
|
|
|
|
* @param addressFormat The address format.
|
|
|
|
* @param displacementFormat The displacement format.
|
|
|
|
* @param immmediateFormat The immediate format.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
|
|
|
* @return A zydis status code.
|
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
ZYDIS_EXPORT ZydisStatus ZydisFormatterInitEx(ZydisFormatter* formatter, ZydisFormatterStyle style,
|
|
|
|
ZydisFormatterFlags flags, ZydisFormatterAddressFormat addressFormat,
|
|
|
|
ZydisFormatterDisplacementFormat displacementFormat,
|
2016-11-26 20:08:37 +08:00
|
|
|
ZydisFormatterImmediateFormat immmediateFormat);
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
/**
|
2017-07-05 01:02:11 +08:00
|
|
|
* @brief Replaces a formatter function with a custom callback and/or retrieves the currently
|
|
|
|
* used function.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2016-11-26 20:08:37 +08:00
|
|
|
* @param hook The formatter hook-type.
|
2017-07-05 01:02:11 +08:00
|
|
|
* @param callback A pointer to a variable that contains the pointer of the callback function
|
|
|
|
* and receives the pointer of the currently used function.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
|
|
|
* @return A zydis status code.
|
2017-07-05 01:02:11 +08:00
|
|
|
*
|
|
|
|
* Call this function with `callback` pointing to a `NULL` value to retrieve the currently used
|
|
|
|
* function without replacing it.
|
2016-05-26 03:25:48 +08:00
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
ZYDIS_EXPORT ZydisStatus ZydisFormatterSetHook(ZydisFormatter* formatter,
|
2016-11-26 20:08:37 +08:00
|
|
|
ZydisFormatterHookType hook, const void** callback);
|
2016-11-21 21:55:17 +08:00
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Formats the given instruction and writes it into the output buffer.
|
|
|
|
*
|
2017-07-03 23:36:03 +08:00
|
|
|
* @param formatter A pointer to the @c ZydisFormatter instance.
|
2017-07-03 09:14:01 +08:00
|
|
|
* @param instruction A pointer to the @c ZydisDecodedInstruction struct.
|
2017-01-11 18:20:24 +08:00
|
|
|
* @param buffer A pointer to the output buffer.
|
|
|
|
* @param bufferLen The length of the output buffer.
|
2016-05-26 03:25:48 +08:00
|
|
|
*
|
|
|
|
* @return A zydis status code.
|
|
|
|
*/
|
2017-07-03 23:36:03 +08:00
|
|
|
ZYDIS_EXPORT ZydisStatus ZydisFormatterFormatInstruction(const ZydisFormatter* formatter,
|
2017-07-03 09:14:01 +08:00
|
|
|
ZydisDecodedInstruction* instruction, char* buffer, size_t bufferLen);
|
2016-05-26 03:25:48 +08:00
|
|
|
|
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ZYDIS_FORMATTER_H */
|