mirror of https://github.com/x64dbg/zydis
Minor improvements to the instruction-formatter
- Added zero-termination for the output string of `ZydisFormatterFormatInstruction` / `ZydisFormatterFormatInstructionEx` in error cases - Renamed some internal functions
This commit is contained in:
parent
1d6c296c5f
commit
043b1f5a5a
136
src/Formatter.c
136
src/Formatter.c
|
@ -29,15 +29,37 @@
|
||||||
#include <Zydis/Utils.h>
|
#include <Zydis/Utils.h>
|
||||||
|
|
||||||
/* ============================================================================================== */
|
/* ============================================================================================== */
|
||||||
/* Instruction formatter */
|
/* Internal functions */
|
||||||
/* ============================================================================================== */
|
/* ============================================================================================== */
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
/* General */
|
||||||
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
static ZydisStatus ZydisFormatInstruction(const ZydisFormatter* formatter, const
|
||||||
|
ZydisDecodedInstruction* instruction, ZydisString* string, void* userData)
|
||||||
|
{
|
||||||
|
if (formatter->funcPre)
|
||||||
|
{
|
||||||
|
ZYDIS_CHECK(formatter->funcPre(formatter, string, instruction, userData));
|
||||||
|
}
|
||||||
|
|
||||||
|
ZYDIS_CHECK(formatter->funcFormatInstruction(formatter, string, instruction, userData));
|
||||||
|
|
||||||
|
if (formatter->funcPost)
|
||||||
|
{
|
||||||
|
return formatter->funcPost(formatter, string, instruction, userData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ZYDIS_STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
/* Intel style */
|
/* Intel style */
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintPrefixesIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintPrefixesIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction, void* userData)
|
const ZydisDecodedInstruction* instruction, void* userData)
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -81,8 +103,8 @@ static ZydisStatus ZydisFormatterPrintPrefixesIntel(const ZydisFormatter* format
|
||||||
return ZYDIS_STATUS_SUCCESS;
|
return ZYDIS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintMnemonicIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintMnemonicIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction, void* userData)
|
const ZydisDecodedInstruction* instruction, void* userData)
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -108,9 +130,8 @@ static ZydisStatus ZydisFormatterPrintMnemonicIntel(const ZydisFormatter* format
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterFormatOperandRegIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisFormatOperandRegIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -132,9 +153,8 @@ static ZydisStatus ZydisFormatterFormatOperandRegIntel(const ZydisFormatter* for
|
||||||
return ZydisStringAppendExStatic(string, reg, formatter->letterCase);
|
return ZydisStringAppendExStatic(string, reg, formatter->letterCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisFormatOperandMemIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
if (!formatter || !instruction || !operand)
|
if (!formatter || !instruction || !operand)
|
||||||
{
|
{
|
||||||
|
@ -202,9 +222,8 @@ static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* for
|
||||||
return ZydisStringAppendC(string, "]");
|
return ZydisStringAppendC(string, "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterFormatOperandPtrIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisFormatOperandPtrIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -220,9 +239,8 @@ static ZydisStatus ZydisFormatterFormatOperandPtrIntel(const ZydisFormatter* for
|
||||||
formatter->hexUppercase, formatter->hexPrefix, formatter->hexSuffix);
|
formatter->hexUppercase, formatter->hexPrefix, formatter->hexSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterFormatOperandImmIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisFormatOperandImmIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
if (!formatter || !instruction || !operand)
|
if (!formatter || !instruction || !operand)
|
||||||
{
|
{
|
||||||
|
@ -268,9 +286,9 @@ static ZydisStatus ZydisFormatterFormatOperandImmIntel(const ZydisFormatter* for
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintAddressIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintAddressIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand,
|
||||||
const ZydisDecodedOperand* operand, ZydisU64 address, void* userData)
|
ZydisU64 address, void* userData)
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -295,9 +313,8 @@ static ZydisStatus ZydisFormatterPrintAddressIntel(const ZydisFormatter* formatt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintDisplacementIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintDisplacementIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -332,9 +349,8 @@ static ZydisStatus ZydisFormatterPrintDisplacementIntel(const ZydisFormatter* fo
|
||||||
return ZYDIS_STATUS_SUCCESS;
|
return ZYDIS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintImmediateIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -398,9 +414,8 @@ static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* forma
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintOperandSizeIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintOperandSizeIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -506,9 +521,8 @@ static ZydisStatus ZydisFormatterPrintOperandSizeIntel(const ZydisFormatter* for
|
||||||
return ZYDIS_STATUS_SUCCESS;
|
return ZYDIS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintSegmentIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintSegmentIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand, void* userData)
|
||||||
const ZydisDecodedOperand* operand, void* userData)
|
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -551,9 +565,9 @@ static ZydisStatus ZydisFormatterPrintSegmentIntel(const ZydisFormatter* formatt
|
||||||
return ZYDIS_STATUS_SUCCESS;
|
return ZYDIS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterPrintDecoratorIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisPrintDecoratorIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction,
|
const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operand,
|
||||||
const ZydisDecodedOperand* operand, ZydisDecoratorType type, void* userData)
|
ZydisDecoratorType type, void* userData)
|
||||||
{
|
{
|
||||||
ZYDIS_UNUSED_PARAMETER(userData);
|
ZYDIS_UNUSED_PARAMETER(userData);
|
||||||
|
|
||||||
|
@ -733,8 +747,8 @@ static ZydisStatus ZydisFormatterPrintDecoratorIntel(const ZydisFormatter* forma
|
||||||
return ZYDIS_STATUS_SUCCESS;
|
return ZYDIS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZydisStatus ZydisFormatterFormatInstrIntel(const ZydisFormatter* formatter,
|
static ZydisStatus ZydisFormatInstrIntel(const ZydisFormatter* formatter, ZydisString* string,
|
||||||
ZydisString* string, const ZydisDecodedInstruction* instruction, void* userData)
|
const ZydisDecodedInstruction* instruction, void* userData)
|
||||||
{
|
{
|
||||||
if (!formatter || !string || !instruction)
|
if (!formatter || !string || !instruction)
|
||||||
{
|
{
|
||||||
|
@ -845,8 +859,10 @@ static ZydisStatus ZydisFormatterFormatInstrIntel(const ZydisFormatter* formatte
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/* ============================================================================================== */
|
||||||
/* Exported functions */
|
/* Exported functions */
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
/* ============================================================================================== */
|
||||||
|
|
||||||
ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle style)
|
ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle style)
|
||||||
{
|
{
|
||||||
|
@ -874,19 +890,19 @@ ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle st
|
||||||
switch (style)
|
switch (style)
|
||||||
{
|
{
|
||||||
case ZYDIS_FORMATTER_STYLE_INTEL:
|
case ZYDIS_FORMATTER_STYLE_INTEL:
|
||||||
formatter->funcFormatInstruction = &ZydisFormatterFormatInstrIntel;
|
formatter->funcFormatInstruction = &ZydisFormatInstrIntel;
|
||||||
formatter->funcPrintPrefixes = &ZydisFormatterPrintPrefixesIntel;
|
formatter->funcPrintPrefixes = &ZydisPrintPrefixesIntel;
|
||||||
formatter->funcPrintMnemonic = &ZydisFormatterPrintMnemonicIntel;
|
formatter->funcPrintMnemonic = &ZydisPrintMnemonicIntel;
|
||||||
formatter->funcFormatOperandReg = &ZydisFormatterFormatOperandRegIntel;
|
formatter->funcFormatOperandReg = &ZydisFormatOperandRegIntel;
|
||||||
formatter->funcFormatOperandMem = &ZydisFormatterFormatOperandMemIntel;
|
formatter->funcFormatOperandMem = &ZydisFormatOperandMemIntel;
|
||||||
formatter->funcFormatOperandPtr = &ZydisFormatterFormatOperandPtrIntel;
|
formatter->funcFormatOperandPtr = &ZydisFormatOperandPtrIntel;
|
||||||
formatter->funcFormatOperandImm = &ZydisFormatterFormatOperandImmIntel;
|
formatter->funcFormatOperandImm = &ZydisFormatOperandImmIntel;
|
||||||
formatter->funcPrintOperandSize = &ZydisFormatterPrintOperandSizeIntel;
|
formatter->funcPrintOperandSize = &ZydisPrintOperandSizeIntel;
|
||||||
formatter->funcPrintSegment = &ZydisFormatterPrintSegmentIntel;
|
formatter->funcPrintSegment = &ZydisPrintSegmentIntel;
|
||||||
formatter->funcPrintDecorator = &ZydisFormatterPrintDecoratorIntel;
|
formatter->funcPrintDecorator = &ZydisPrintDecoratorIntel;
|
||||||
formatter->funcPrintAddress = &ZydisFormatterPrintAddressIntel;
|
formatter->funcPrintAddress = &ZydisPrintAddressIntel;
|
||||||
formatter->funcPrintDisplacement = &ZydisFormatterPrintDisplacementIntel;
|
formatter->funcPrintDisplacement = &ZydisPrintDisplacementIntel;
|
||||||
formatter->funcPrintImmediate = &ZydisFormatterPrintImmediateIntel;
|
formatter->funcPrintImmediate = &ZydisPrintImmediateIntel;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return ZYDIS_STATUS_INVALID_PARAMETER;
|
return ZYDIS_STATUS_INVALID_PARAMETER;
|
||||||
|
@ -1120,23 +1136,13 @@ ZydisStatus ZydisFormatterFormatInstructionEx(const ZydisFormatter* formatter,
|
||||||
ZydisString string;
|
ZydisString string;
|
||||||
string.buffer = buffer;
|
string.buffer = buffer;
|
||||||
string.length = 0;
|
string.length = 0;
|
||||||
string.capacity = bufferLen;
|
string.capacity = bufferLen - 1;
|
||||||
|
|
||||||
if (formatter->funcPre)
|
const ZydisStatus status = ZydisFormatInstruction(formatter, instruction, &string, userData);
|
||||||
{
|
|
||||||
ZYDIS_CHECK(formatter->funcPre(formatter, &string, instruction, userData));
|
|
||||||
}
|
|
||||||
ZYDIS_CHECK(formatter->funcFormatInstruction(formatter, &string, instruction, userData));
|
|
||||||
if (formatter->funcPost)
|
|
||||||
{
|
|
||||||
return formatter->funcPost(formatter, &string, instruction, userData);
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[string.length] = 0; // TODO: Should we add 0-termination in error case?
|
buffer[string.length] = 0;
|
||||||
|
|
||||||
return ZYDIS_STATUS_SUCCESS;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
/* ============================================================================================== */
|
/* ============================================================================================== */
|
||||||
|
|
Loading…
Reference in New Issue