Added formatter properties to control padding of hexadecimal values

- Renamed `ZydisFormatterSetAttribute` to `ZydisFormatterSetProperty`
- Renamed some formatter enums
- Added `ZYDIS_FORMATTER_PROP_ADDR_PADDING`
- Added `ZYDIS_FORMATTER_PROP_DISP_PADDING`
- Added `ZYDIS_FORMATTER_PROP_IMM_PADDING`
This commit is contained in:
flobernd 2017-11-06 21:35:13 +01:00
parent a227bd4bbe
commit adbfb9cd66
7 changed files with 146 additions and 97 deletions

View File

@ -189,8 +189,8 @@ void disassembleBuffer(ZydisDecoder* decoder, uint8_t* data, size_t length, Zydi
{ {
ZydisFormatter formatter; ZydisFormatter formatter;
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL); ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
ZydisFormatterSetAttribute(&formatter, ZYDIS_FORMATTER_ATTRIB_FORCE_SEGMENTS, ZYDIS_TRUE); ZydisFormatterSetProperty(&formatter, ZYDIS_FORMATTER_PROP_FORCE_SEGMENTS, ZYDIS_TRUE);
ZydisFormatterSetAttribute(&formatter, ZYDIS_FORMATTER_ATTRIB_FORCE_OPERANDSIZE, ZYDIS_TRUE); ZydisFormatterSetProperty(&formatter, ZYDIS_FORMATTER_PROP_FORCE_OPERANDSIZE, ZYDIS_TRUE);
if (installHooks) if (installHooks)
{ {

View File

@ -45,7 +45,7 @@ typedef struct ZydisFuzzControlBlock_
ZydisAddressWidth addressWidth; ZydisAddressWidth addressWidth;
ZydisBool decoderMode[ZYDIS_DECODER_MODE_MAX_VALUE + 1]; ZydisBool decoderMode[ZYDIS_DECODER_MODE_MAX_VALUE + 1];
ZydisFormatterStyle formatterStyle; ZydisFormatterStyle formatterStyle;
uintptr_t formatterAttributes[ZYDIS_FORMATTER_ATTRIB_MAX_VALUE + 1]; uintptr_t formatterProperties[ZYDIS_FORMATTER_PROP_MAX_VALUE + 1];
} ZydisFuzzControlBlock; } ZydisFuzzControlBlock;
/* ============================================================================================== */ /* ============================================================================================== */
@ -90,10 +90,10 @@ int main()
fputs("Failed to initialize instruction-formatter\n", stderr); fputs("Failed to initialize instruction-formatter\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
for (ZydisFormatterAttribute attrib = 0; attrib <= ZYDIS_FORMATTER_ATTRIB_MAX_VALUE; ++attrib) for (ZydisFormatterProperty prop = 0; prop <= ZYDIS_FORMATTER_PROP_MAX_VALUE; ++prop)
{ {
if (!ZYDIS_SUCCESS(ZydisFormatterSetAttribute(&formatter, attrib, if (!ZYDIS_SUCCESS(ZydisFormatterSetProperty(&formatter, prop,
controlBlock.formatterAttributes[attrib]))) controlBlock.formatterProperties[prop])))
{ {
fputs("Failed to set formatter-attribute\n", stderr); fputs("Failed to set formatter-attribute\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -172,10 +172,10 @@ uint64_t processBuffer(const char* buffer, size_t length, ZydisBool minimalMode,
if (format) if (format)
{ {
if (!ZYDIS_SUCCESS(ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL)) || if (!ZYDIS_SUCCESS(ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL)) ||
!ZYDIS_SUCCESS(ZydisFormatterSetAttribute(&formatter, !ZYDIS_SUCCESS(ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_ATTRIB_FORCE_SEGMENTS, ZYDIS_TRUE)) || ZYDIS_FORMATTER_PROP_FORCE_SEGMENTS, ZYDIS_TRUE)) ||
!ZYDIS_SUCCESS(ZydisFormatterSetAttribute(&formatter, !ZYDIS_SUCCESS(ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_ATTRIB_FORCE_OPERANDSIZE, ZYDIS_TRUE))) ZYDIS_FORMATTER_PROP_FORCE_OPERANDSIZE, ZYDIS_TRUE)))
{ {
fputs("Failed to initialize instruction-formatter\n", stderr); fputs("Failed to initialize instruction-formatter\n", stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -73,14 +73,14 @@ enum ZydisFormatterStyles
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/** /**
* @brief Defines the @c ZydisFormatterAttribute datatype. * @brief Defines the @c ZydisFormatterProperty datatype.
*/ */
typedef uint8_t ZydisFormatterAttribute; typedef uint8_t ZydisFormatterProperty;
/** /**
* @brief Values that represent formatter-attributes. * @brief Values that represent formatter-properties.
*/ */
enum ZydisFormatterAttributes enum ZydisFormatterProperties
{ {
/** /**
* @brief Controls the letter-case. * @brief Controls the letter-case.
@ -89,7 +89,7 @@ enum ZydisFormatterAttributes
* *
* The default value is `ZYDIS_FALSE`. * The default value is `ZYDIS_FALSE`.
*/ */
ZYDIS_FORMATTER_ATTRIB_UPPERCASE, ZYDIS_FORMATTER_PROP_UPPERCASE,
/** /**
* @brief Controls the printing of segment prefixes. * @brief Controls the printing of segment prefixes.
* *
@ -98,7 +98,7 @@ enum ZydisFormatterAttributes
* *
* The default value is `ZYDIS_FALSE`. * The default value is `ZYDIS_FALSE`.
*/ */
ZYDIS_FORMATTER_ATTRIB_FORCE_SEGMENTS, ZYDIS_FORMATTER_PROP_FORCE_SEGMENTS,
/** /**
* @brief Controls the printing of operand-sizes. * @brief Controls the printing of operand-sizes.
* *
@ -107,29 +107,50 @@ enum ZydisFormatterAttributes
* *
* The default value is `ZYDIS_FALSE`. * The default value is `ZYDIS_FALSE`.
*/ */
ZYDIS_FORMATTER_ATTRIB_FORCE_OPERANDSIZE, ZYDIS_FORMATTER_PROP_FORCE_OPERANDSIZE,
/** /**
* @brief Controls the format of addresses. * @brief Controls the format of addresses.
* *
* The default value is `ZYDIS_FORMATTER_ADDR_ABSOLUTE`. * The default value is `ZYDIS_ADDR_FORMAT_ABSOLUTE`.
*/ */
ZYDIS_FORMATTER_ATTRIB_ADDR_FORMAT, ZYDIS_FORMATTER_PROP_ADDR_FORMAT,
/** /**
* @brief Controls the format of displacement values. * @brief Controls the format of displacement values.
* *
* The default value is `ZYDIS_FORMATTER_DISP_HEX_SIGNED`. * The default value is `ZYDIS_DISP_FORMAT_HEX_SIGNED`.
*/ */
ZYDIS_FORMATTER_ATTRIB_DISP_FORMAT, ZYDIS_FORMATTER_PROP_DISP_FORMAT,
/** /**
* @brief Controls the format of immediate values. * @brief Controls the format of immediate values.
* *
* The default value is `ZYDIS_FORMATTER_IMM_HEX_UNSIGNED`. * The default value is `ZYDIS_IMM_FORMAT_HEX_UNSIGNED`.
*/ */
ZYDIS_FORMATTER_ATTRIB_IMM_FORMAT, ZYDIS_FORMATTER_PROP_IMM_FORMAT,
/**
* @brief Controls the padding (minimum number of chars) of address values.
*
* The default value is `2`.
*/
ZYDIS_FORMATTER_PROP_ADDR_PADDING,
/**
* @brief Controls the padding (minimum number of chars) of displacement values.
*
* The default value is `2`.
*/
ZYDIS_FORMATTER_PROP_DISP_PADDING,
/**
* @brief Controls the padding (minimum number of chars) of immediate values.
*
* The default value is `2`.
*/
ZYDIS_FORMATTER_PROP_IMM_PADDING,
/** /**
* @brief Maximum value of this enum. * @brief Maximum value of this enum.
*/ */
ZYDIS_FORMATTER_ATTRIB_MAX_VALUE = ZYDIS_FORMATTER_ATTRIB_IMM_FORMAT ZYDIS_FORMATTER_PROP_MAX_VALUE = ZYDIS_FORMATTER_PROP_IMM_PADDING
}; };
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -139,12 +160,12 @@ enum ZydisFormatterAttributes
/** /**
* @brief Values that represent address-formats. * @brief Values that represent address-formats.
*/ */
enum ZydisFormatterAddressFormats enum ZydisAddressFormat
{ {
/** /**
* @brief Displays absolute addresses instead of relative ones. * @brief Displays absolute addresses instead of relative ones.
*/ */
ZYDIS_FORMATTER_ADDR_ABSOLUTE, ZYDIS_ADDR_FORMAT_ABSOLUTE,
/** /**
* @brief Uses signed hexadecimal values to display relative addresses. * @brief Uses signed hexadecimal values to display relative addresses.
* *
@ -152,7 +173,7 @@ enum ZydisFormatterAddressFormats
* "JMP 0x20" * "JMP 0x20"
* "JMP -0x20" * "JMP -0x20"
*/ */
ZYDIS_FORMATTER_ADDR_RELATIVE_SIGNED, ZYDIS_ADDR_FORMAT_RELATIVE_SIGNED,
/** /**
* @brief Uses unsigned hexadecimal values to display relative addresses. * @brief Uses unsigned hexadecimal values to display relative addresses.
* *
@ -160,11 +181,11 @@ enum ZydisFormatterAddressFormats
* "JMP 0x20" * "JMP 0x20"
* "JMP 0xE0" * "JMP 0xE0"
*/ */
ZYDIS_FORMATTER_ADDR_RELATIVE_UNSIGNED, ZYDIS_ADDR_FORMAT_RELATIVE_UNSIGNED,
/** /**
* @brief Maximum value of this enum. * @brief Maximum value of this enum.
*/ */
ZYDIS_FORMATTER_ADDR_MAX_VALUE = ZYDIS_FORMATTER_ADDR_RELATIVE_UNSIGNED ZYDIS_ADDR_FORMAT_MAX_VALUE = ZYDIS_ADDR_FORMAT_RELATIVE_UNSIGNED
}; };
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -174,7 +195,7 @@ enum ZydisFormatterAddressFormats
/** /**
* @brief Values that represent displacement-formats. * @brief Values that represent displacement-formats.
*/ */
enum ZydisFormatterDisplacementFormats enum ZydisDisplacementFormat
{ {
/** /**
* @brief Formats displacements as signed hexadecimal values. * @brief Formats displacements as signed hexadecimal values.
@ -183,7 +204,7 @@ enum ZydisFormatterDisplacementFormats
* "MOV EAX, DWORD PTR SS:[ESP+0x400]" * "MOV EAX, DWORD PTR SS:[ESP+0x400]"
* "MOV EAX, DWORD PTR SS:[ESP-0x400]" * "MOV EAX, DWORD PTR SS:[ESP-0x400]"
*/ */
ZYDIS_FORMATTER_DISP_HEX_SIGNED, ZYDIS_DISP_FORMAT_HEX_SIGNED,
/** /**
* @brief Formats displacements as unsigned hexadecimal values. * @brief Formats displacements as unsigned hexadecimal values.
* *
@ -191,11 +212,11 @@ enum ZydisFormatterDisplacementFormats
* "MOV EAX, DWORD PTR SS:[ESP+0x400]" * "MOV EAX, DWORD PTR SS:[ESP+0x400]"
* "MOV EAX, DWORD PTR SS:[ESP+0xFFFFFC00]" * "MOV EAX, DWORD PTR SS:[ESP+0xFFFFFC00]"
*/ */
ZYDIS_FORMATTER_DISP_HEX_UNSIGNED, ZYDIS_DISP_FORMAT_HEX_UNSIGNED,
/** /**
* @brief Maximum value of this enum. * @brief Maximum value of this enum.
*/ */
ZYDIS_FORMATTER_DISP_MAX_VALUE = ZYDIS_FORMATTER_DISP_HEX_UNSIGNED ZYDIS_DISP_FORMAT_MAX_VALUE = ZYDIS_DISP_FORMAT_HEX_UNSIGNED
}; };
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -205,13 +226,13 @@ enum ZydisFormatterDisplacementFormats
/** /**
* @brief Values that represent formatter immediate-formats. * @brief Values that represent formatter immediate-formats.
*/ */
enum ZydisFormatterImmediateFormats enum ZydisImmediateFormat
{ {
/** /**
* @brief Automatically chooses the most suitable formatting-mode based on the operands * @brief Automatically chooses the most suitable formatting-mode based on the operands
* @c ZydisOperandInfo.imm.isSigned attribute. * @c ZydisOperandInfo.imm.isSigned attribute.
*/ */
ZYDIS_FORMATTER_IMM_HEX_AUTO, ZYDIS_IMM_FORMAT_HEX_AUTO,
/** /**
* @brief Formats immediates as signed hexadecimal values. * @brief Formats immediates as signed hexadecimal values.
* *
@ -219,7 +240,7 @@ enum ZydisFormatterImmediateFormats
* "MOV EAX, 0x400" * "MOV EAX, 0x400"
* "MOV EAX, -0x400" * "MOV EAX, -0x400"
*/ */
ZYDIS_FORMATTER_IMM_HEX_SIGNED, ZYDIS_IMM_FORMAT_HEX_SIGNED,
/** /**
* @brief Formats immediates as unsigned hexadecimal values. * @brief Formats immediates as unsigned hexadecimal values.
* *
@ -227,11 +248,11 @@ enum ZydisFormatterImmediateFormats
* "MOV EAX, 0x400" * "MOV EAX, 0x400"
* "MOV EAX, 0xFFFFFC00" * "MOV EAX, 0xFFFFFC00"
*/ */
ZYDIS_FORMATTER_IMM_HEX_UNSIGNED, ZYDIS_IMM_FORMAT_HEX_UNSIGNED,
/** /**
* @brief Maximum value of this enum. * @brief Maximum value of this enum.
*/ */
ZYDIS_FORMATTER_IMM_MAX_VALUE = ZYDIS_FORMATTER_IMM_HEX_UNSIGNED ZYDIS_IMM_FORMAT_MAX_VALUE = ZYDIS_IMM_FORMAT_HEX_UNSIGNED
}; };
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -497,6 +518,9 @@ struct ZydisFormatter_
uint8_t addressFormat; uint8_t addressFormat;
uint8_t displacementFormat; uint8_t displacementFormat;
uint8_t immediateFormat; uint8_t immediateFormat;
uint8_t addressPadding;
uint8_t displacementPadding;
uint8_t immediatePadding;
ZydisFormatterNotifyFunc funcPre; ZydisFormatterNotifyFunc funcPre;
ZydisFormatterNotifyFunc funcPost; ZydisFormatterNotifyFunc funcPost;
ZydisFormatterFormatFunc funcFormatInstruction; ZydisFormatterFormatFunc funcFormatInstruction;
@ -534,13 +558,13 @@ ZYDIS_EXPORT ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisForm
* @brief Sets the value of the specified formatter `attribute`. * @brief Sets the value of the specified formatter `attribute`.
* *
* @param formatter A pointer to the @c ZydisFormatter instance. * @param formatter A pointer to the @c ZydisFormatter instance.
* @param attribute The id of the formatter-attribute. * @param property The id of the formatter-property.
* @param value The new value. * @param value The new value.
* *
* @return A zydis status code. * @return A zydis status code.
*/ */
ZYDIS_EXPORT ZydisStatus ZydisFormatterSetAttribute(ZydisFormatter* formatter, ZYDIS_EXPORT ZydisStatus ZydisFormatterSetProperty(ZydisFormatter* formatter,
ZydisFormatterAttribute attribute, uintptr_t value); ZydisFormatterProperty property, uintptr_t value);
/** /**
* @brief Replaces a formatter function with a custom callback and/or retrieves the currently * @brief Replaces a formatter function with a custom callback and/or retrieves the currently

View File

@ -156,7 +156,7 @@ static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* for
(operand->mem.index == ZYDIS_REGISTER_NONE) && (operand->mem.scale == 0)) (operand->mem.index == ZYDIS_REGISTER_NONE) && (operand->mem.scale == 0))
{ {
// EIP/RIP-relative or absolute-displacement address operand // EIP/RIP-relative or absolute-displacement address operand
if ((formatter->addressFormat == ZYDIS_FORMATTER_ADDR_ABSOLUTE) || if ((formatter->addressFormat == ZYDIS_ADDR_FORMAT_ABSOLUTE) ||
(operand->mem.base == ZYDIS_REGISTER_NONE)) (operand->mem.base == ZYDIS_REGISTER_NONE))
{ {
uint64_t address; uint64_t address;
@ -244,17 +244,17 @@ static ZydisStatus ZydisFormatterFormatOperandImmIntel(const ZydisFormatter* for
ZydisBool printSignedHEX = ZYDIS_FALSE; ZydisBool printSignedHEX = ZYDIS_FALSE;
switch (formatter->addressFormat) switch (formatter->addressFormat)
{ {
case ZYDIS_FORMATTER_ADDR_ABSOLUTE: case ZYDIS_ADDR_FORMAT_ABSOLUTE:
{ {
uint64_t address; uint64_t address;
ZYDIS_CHECK(ZydisCalcAbsoluteAddress(instruction, operand, &address)); ZYDIS_CHECK(ZydisCalcAbsoluteAddress(instruction, operand, &address));
return formatter->funcPrintAddress(formatter, buffer, bufferLen, instruction, operand, return formatter->funcPrintAddress(formatter, buffer, bufferLen, instruction, operand,
address, userData); address, userData);
} }
case ZYDIS_FORMATTER_ADDR_RELATIVE_SIGNED: case ZYDIS_ADDR_FORMAT_RELATIVE_SIGNED:
printSignedHEX = ZYDIS_TRUE; printSignedHEX = ZYDIS_TRUE;
break; break;
case ZYDIS_FORMATTER_ADDR_RELATIVE_UNSIGNED: case ZYDIS_ADDR_FORMAT_RELATIVE_UNSIGNED:
break; break;
default: default:
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
@ -262,10 +262,11 @@ static ZydisStatus ZydisFormatterFormatOperandImmIntel(const ZydisFormatter* for
if (printSignedHEX) if (printSignedHEX)
{ {
return ZydisPrintHexS( return ZydisPrintHexS(buffer, bufferLen, (int32_t)operand->imm.value.s,
buffer, bufferLen, (int32_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->addressPadding, ZYDIS_TRUE, ZYDIS_TRUE);
} }
return ZydisPrintHexU(buffer, bufferLen, operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); return ZydisPrintHexU(buffer, bufferLen, operand->imm.value.u, formatter->addressPadding,
ZYDIS_TRUE, ZYDIS_TRUE);
} }
// The immediate operand contains an actual ordinal value // The immediate operand contains an actual ordinal value
@ -315,13 +316,13 @@ static ZydisStatus ZydisFormatterPrintDisplacementIntel(const ZydisFormatter* fo
(operand->mem.index == ZYDIS_REGISTER_NONE)))) (operand->mem.index == ZYDIS_REGISTER_NONE))))
{ {
ZydisBool printSignedHEX = ZydisBool printSignedHEX =
(formatter->displacementFormat != ZYDIS_FORMATTER_DISP_HEX_UNSIGNED); (formatter->displacementFormat != ZYDIS_DISP_FORMAT_HEX_UNSIGNED);
if (printSignedHEX && (operand->mem.disp.value < 0) && ( if (printSignedHEX && (operand->mem.disp.value < 0) && (
(operand->mem.base != ZYDIS_REGISTER_NONE) || (operand->mem.base != ZYDIS_REGISTER_NONE) ||
(operand->mem.index != ZYDIS_REGISTER_NONE))) (operand->mem.index != ZYDIS_REGISTER_NONE)))
{ {
return ZydisPrintHexS( return ZydisPrintHexS(buffer, bufferLen, operand->mem.disp.value,
buffer, bufferLen, operand->mem.disp.value, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->displacementPadding, ZYDIS_TRUE, ZYDIS_TRUE);
} }
char* bufEnd = *buffer + bufferLen; char* bufEnd = *buffer + bufferLen;
if ((operand->mem.base != ZYDIS_REGISTER_NONE) || if ((operand->mem.base != ZYDIS_REGISTER_NONE) ||
@ -329,8 +330,8 @@ static ZydisStatus ZydisFormatterPrintDisplacementIntel(const ZydisFormatter* fo
{ {
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "+", ZYDIS_LETTER_CASE_DEFAULT)); ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "+", ZYDIS_LETTER_CASE_DEFAULT));
} }
return ZydisPrintHexU( return ZydisPrintHexU(buffer, bufEnd - *buffer, (uint64_t)operand->mem.disp.value,
buffer, bufEnd - *buffer, (uint64_t)operand->mem.disp.value, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->displacementPadding, ZYDIS_TRUE, ZYDIS_TRUE);
} }
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
@ -346,8 +347,8 @@ static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* forma
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
ZydisBool printSignedHEX = (formatter->immediateFormat == ZYDIS_FORMATTER_IMM_HEX_SIGNED); ZydisBool printSignedHEX = (formatter->immediateFormat == ZYDIS_IMM_FORMAT_HEX_SIGNED);
if (formatter->immediateFormat == ZYDIS_FORMATTER_IMM_HEX_AUTO) if (formatter->immediateFormat == ZYDIS_IMM_FORMAT_HEX_AUTO)
{ {
printSignedHEX = operand->imm.isSigned; printSignedHEX = operand->imm.isSigned;
} }
@ -357,17 +358,17 @@ static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* forma
switch (operand->size) switch (operand->size)
{ {
case 8: case 8:
return ZydisPrintHexS( return ZydisPrintHexS(buffer, bufferLen, (int8_t)operand->imm.value.s,
buffer, bufferLen, (int8_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
case 16: case 16:
return ZydisPrintHexS( return ZydisPrintHexS(buffer, bufferLen, (int16_t)operand->imm.value.s,
buffer, bufferLen, (int16_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
case 32: case 32:
return ZydisPrintHexS( return ZydisPrintHexS(buffer, bufferLen, (int32_t)operand->imm.value.s,
buffer, bufferLen, (int32_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
case 64: case 64:
return ZydisPrintHexS( return ZydisPrintHexS(buffer, bufferLen, operand->imm.value.s,
buffer, bufferLen, operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
default: default:
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
@ -375,17 +376,17 @@ static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* forma
switch (instruction->operandWidth) switch (instruction->operandWidth)
{ {
case 8: case 8:
return ZydisPrintHexU( return ZydisPrintHexU(buffer, bufferLen, (uint8_t)operand->imm.value.u,
buffer, bufferLen, (uint8_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
case 16: case 16:
return ZydisPrintHexU( return ZydisPrintHexU(buffer, bufferLen, (uint16_t)operand->imm.value.u,
buffer, bufferLen, (uint16_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
case 32: case 32:
return ZydisPrintHexU( return ZydisPrintHexU(buffer, bufferLen, (uint32_t)operand->imm.value.u,
buffer, bufferLen, (uint32_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
case 64: case 64:
return ZydisPrintHexU( return ZydisPrintHexU(buffer, bufferLen, operand->imm.value.u,
buffer, bufferLen, operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); formatter->immediatePadding, ZYDIS_TRUE, ZYDIS_TRUE);
default: default:
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
@ -894,12 +895,15 @@ ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle st
} }
memset(formatter, 0, sizeof(ZydisFormatter)); memset(formatter, 0, sizeof(ZydisFormatter));
formatter->letterCase = ZYDIS_LETTER_CASE_DEFAULT; formatter->letterCase = ZYDIS_LETTER_CASE_DEFAULT;
formatter->forceSegments = ZYDIS_FALSE; formatter->forceSegments = ZYDIS_FALSE;
formatter->forceOperandSize = ZYDIS_FALSE; formatter->forceOperandSize = ZYDIS_FALSE;
formatter->addressFormat = ZYDIS_FORMATTER_ADDR_ABSOLUTE; formatter->addressFormat = ZYDIS_ADDR_FORMAT_ABSOLUTE;
formatter->displacementFormat = ZYDIS_FORMATTER_DISP_HEX_SIGNED; formatter->displacementFormat = ZYDIS_DISP_FORMAT_HEX_SIGNED;
formatter->immediateFormat = ZYDIS_FORMATTER_IMM_HEX_UNSIGNED; formatter->immediateFormat = ZYDIS_IMM_FORMAT_HEX_UNSIGNED;
formatter->addressPadding = 2;
formatter->displacementPadding = 2;
formatter->immediatePadding = 2;
switch (style) switch (style)
{ {
@ -925,17 +929,17 @@ ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle st
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
ZydisStatus ZydisFormatterSetAttribute(ZydisFormatter* formatter, ZydisStatus ZydisFormatterSetProperty(ZydisFormatter* formatter,
ZydisFormatterAttribute attribute, uintptr_t value) ZydisFormatterProperty property, uintptr_t value)
{ {
if (!formatter) if (!formatter)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
switch (attribute) switch (property)
{ {
case ZYDIS_FORMATTER_ATTRIB_UPPERCASE: case ZYDIS_FORMATTER_PROP_UPPERCASE:
switch (value) switch (value)
{ {
case ZYDIS_FALSE: case ZYDIS_FALSE:
@ -948,7 +952,7 @@ ZydisStatus ZydisFormatterSetAttribute(ZydisFormatter* formatter,
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
break; break;
case ZYDIS_FORMATTER_ATTRIB_FORCE_SEGMENTS: case ZYDIS_FORMATTER_PROP_FORCE_SEGMENTS:
switch (value) switch (value)
{ {
case ZYDIS_FALSE: case ZYDIS_FALSE:
@ -961,7 +965,7 @@ ZydisStatus ZydisFormatterSetAttribute(ZydisFormatter* formatter,
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
break; break;
case ZYDIS_FORMATTER_ATTRIB_FORCE_OPERANDSIZE: case ZYDIS_FORMATTER_PROP_FORCE_OPERANDSIZE:
switch (value) switch (value)
{ {
case ZYDIS_FALSE: case ZYDIS_FALSE:
@ -974,27 +978,48 @@ ZydisStatus ZydisFormatterSetAttribute(ZydisFormatter* formatter,
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
break; break;
case ZYDIS_FORMATTER_ATTRIB_ADDR_FORMAT: case ZYDIS_FORMATTER_PROP_ADDR_FORMAT:
if (value > ZYDIS_FORMATTER_ADDR_MAX_VALUE) if (value > ZYDIS_ADDR_FORMAT_MAX_VALUE)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
formatter->addressFormat = (uint8_t)value; formatter->addressFormat = (uint8_t)value;
break; break;
case ZYDIS_FORMATTER_ATTRIB_DISP_FORMAT: case ZYDIS_FORMATTER_PROP_DISP_FORMAT:
if (value > ZYDIS_FORMATTER_DISP_MAX_VALUE) if (value > ZYDIS_DISP_FORMAT_MAX_VALUE)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
formatter->displacementFormat = (uint8_t)value; formatter->displacementFormat = (uint8_t)value;
break; break;
case ZYDIS_FORMATTER_ATTRIB_IMM_FORMAT: case ZYDIS_FORMATTER_PROP_IMM_FORMAT:
if (value > ZYDIS_FORMATTER_IMM_MAX_VALUE) if (value > ZYDIS_IMM_FORMAT_MAX_VALUE)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
formatter->immediateFormat = (uint8_t)value; formatter->immediateFormat = (uint8_t)value;
break; break;
case ZYDIS_FORMATTER_PROP_ADDR_PADDING:
if (value > 20)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
formatter->addressPadding = (uint8_t)value;
break;
case ZYDIS_FORMATTER_PROP_DISP_PADDING:
if (value > 20)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
formatter->displacementPadding = (uint8_t)value;
break;
case ZYDIS_FORMATTER_PROP_IMM_PADDING:
if (value > 20)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
formatter->immediatePadding = (uint8_t)value;
break;
default: default:
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }

View File

@ -68,10 +68,10 @@ int main(int argc, char** argv)
ZydisFormatter formatter; ZydisFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL)) || if (!ZYDIS_SUCCESS(ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL)) ||
!ZYDIS_SUCCESS(ZydisFormatterSetAttribute(&formatter, !ZYDIS_SUCCESS(ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_ATTRIB_FORCE_SEGMENTS, ZYDIS_TRUE)) || ZYDIS_FORMATTER_PROP_FORCE_SEGMENTS, ZYDIS_TRUE)) ||
!ZYDIS_SUCCESS(ZydisFormatterSetAttribute(&formatter, !ZYDIS_SUCCESS(ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_ATTRIB_FORCE_OPERANDSIZE, ZYDIS_TRUE))) ZYDIS_FORMATTER_PROP_FORCE_OPERANDSIZE, ZYDIS_TRUE)))
{ {
fputs("Failed to initialized instruction-formatter\n", stderr); fputs("Failed to initialized instruction-formatter\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -549,10 +549,10 @@ void printInstruction(ZydisDecodedInstruction* instruction)
ZydisStatus status; ZydisStatus status;
ZydisFormatter formatter; ZydisFormatter formatter;
if (!ZYDIS_SUCCESS((status = ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL))) || if (!ZYDIS_SUCCESS((status = ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL))) ||
!ZYDIS_SUCCESS((status = ZydisFormatterSetAttribute(&formatter, !ZYDIS_SUCCESS((status = ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_ATTRIB_FORCE_SEGMENTS, ZYDIS_TRUE))) || ZYDIS_FORMATTER_PROP_FORCE_SEGMENTS, ZYDIS_TRUE))) ||
!ZYDIS_SUCCESS((status = ZydisFormatterSetAttribute(&formatter, !ZYDIS_SUCCESS((status = ZydisFormatterSetProperty(&formatter,
ZYDIS_FORMATTER_ATTRIB_FORCE_OPERANDSIZE, ZYDIS_TRUE)))) ZYDIS_FORMATTER_PROP_FORCE_OPERANDSIZE, ZYDIS_TRUE))))
{ {
fputs("Failed to initialize instruction-formatter\n", stderr); fputs("Failed to initialize instruction-formatter\n", stderr);
exit(status); exit(status);