From 4e189721b2691ed552f1098caea76b62bab26213 Mon Sep 17 00:00:00 2001 From: flobernd Date: Mon, 5 Feb 2018 23:49:50 +0100 Subject: [PATCH] Minor changes to the operand formatter hooks `ZYDIS_FORMATTER_HOOK_PRE_OPERAND` and `ZYDIS_FORMATTER_HOOK_POST_OPERAND` can be used to omit a specific operand, if you return a status-code different to `ZYDIS_STATUS_SUCCESS`. --- src/Formatter.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Formatter.c b/src/Formatter.c index 476de9c..9cdb076 100644 --- a/src/Formatter.c +++ b/src/Formatter.c @@ -88,13 +88,16 @@ static ZydisStatus ZydisFormatInstrIntel(const ZydisFormatter* formatter, ZydisS ZYDIS_CHECK(ZydisStringAppendC(string, ", ")); } + const ZydisUSize strLenPreOperand = string->length; if (formatter->funcPreOperand) { - formatter->funcPreOperand(formatter, string, instruction, &instruction->operands[i], - userData); + if (!ZYDIS_SUCCESS(formatter->funcPreOperand(formatter, string, instruction, + &instruction->operands[i], userData))) + { + goto SkipOperand; + } } - const ZydisUSize strLenPreOperand = string->length; switch (instruction->operands[i].type) { case ZYDIS_OPERAND_TYPE_REGISTER: @@ -125,9 +128,19 @@ static ZydisStatus ZydisFormatInstrIntel(const ZydisFormatter* formatter, ZydisS default: return ZYDIS_STATUS_INVALID_PARAMETER; } + + if (formatter->funcPostOperand) + { + if (!ZYDIS_SUCCESS(formatter->funcPostOperand(formatter, string, instruction, + &instruction->operands[i], userData))) + { + goto SkipOperand; + } + } if (strLenPreOperand == string->length) { +SkipOperand: // Omit whole operand, if the string did not change during the formatting-callback string->length = strLenRestore; @@ -140,12 +153,6 @@ static ZydisStatus ZydisFormatInstrIntel(const ZydisFormatter* formatter, ZydisS continue; } - if (formatter->funcPostOperand) - { - formatter->funcPostOperand(formatter, string, instruction, &instruction->operands[i], - userData); - } - if ((instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_EVEX) || (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_MVEX)) { @@ -902,6 +909,10 @@ ZydisStatus ZydisFormatterInit(ZydisFormatter* formatter, ZydisFormatterStyle st switch (style) { case ZYDIS_FORMATTER_STYLE_INTEL: + formatter->funcPreInstruction = ZYDIS_NULL; + formatter->funcPostInstruction = ZYDIS_NULL; + formatter->funcPreOperand = ZYDIS_NULL; + formatter->funcPostOperand = ZYDIS_NULL; formatter->funcFormatInstruction = &ZydisFormatInstrIntel; formatter->funcFormatOperandReg = &ZydisFormatOperandRegIntel; formatter->funcFormatOperandMem = &ZydisFormatOperandMemIntel;