Fixed build for tools

This commit is contained in:
Joel Höner 2017-01-11 12:18:26 +01:00
parent 9804bf1d3e
commit 4d0caac923
3 changed files with 68 additions and 46 deletions

View File

@ -94,6 +94,7 @@ if (ZYDIS_BUILD_EXAMPLES)
"examples/FormatHelper.h")
target_link_libraries("FormatterHooks" "Zydis")
set_target_properties ("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif ()
# Tools
@ -101,8 +102,10 @@ if (ZYDIS_BUILD_TOOLS)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
add_executable("ZydisFuzzIn" "tools/ZydisFuzzIn.c")
target_link_libraries("ZydisFuzzIn" "Zydis")
set_target_properties("ZydisFuzzIn" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif ()

View File

@ -38,26 +38,19 @@
int main(int argc, char** argv)
{
if (argc != 2)
if (argc < 1 || argc > 2)
{
fprintf(stderr, "Usage: %s <input file>\n", (argc > 0 ? argv[0] : "ZydisDisasm"));
fprintf(stderr, "Usage: %s [input file]\n", (argc > 0 ? argv[0] : "ZydisDisasm"));
return EXIT_FAILURE;
}
FILE* file = fopen(argv[1], "rb");
FILE* file = argc >= 2 ? fopen(argv[1], "rb") : stdin;
if (!file)
{
fprintf(stderr, "Can not open file: %s\n", strerror(errno));
return EXIT_FAILURE;
}
ZydisFileInput input;
if (!ZYDIS_SUCCESS(ZydisInputInitFileInput(&input, file)))
{
fputs("Failed to initialize file-input\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter,
ZYDIS_FORMATTER_STYLE_INTEL, ZYDIS_FMTFLAG_FORCE_SEGMENTS | ZYDIS_FMTFLAG_FORCE_OPERANDSIZE,
@ -68,26 +61,46 @@ int main(int argc, char** argv)
}
ZydisInstructionDecoder decoder;
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoderEx(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT,
(ZydisCustomInput*)&input, ZYDIS_DECODER_FLAG_SKIP_DATA)))
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoder(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT)))
{
fputs("Failed to initialize instruction-decoder\n", stderr);
return EXIT_FAILURE;
}
char buffer[256];
ZydisInstructionInfo info;
while (ZYDIS_SUCCESS(ZydisDecoderDecodeNextInstruction(&decoder, &info)))
uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH];
size_t numBytesRead;
do
{
if (info.instrFlags & ZYDIS_INSTRFLAG_ERROR_MASK)
numBytesRead = fread(readBuf, 1, sizeof(readBuf), file);
ZydisInstructionInfo info;
ZydisStatus status;
size_t readOffs = 0;
while ((status = ZydisDecoderDecodeInstruction(
&decoder, readBuf + readOffs, numBytesRead - readOffs, &info
)) != ZYDIS_STATUS_NO_MORE_DATA)
{
printf("db %02X\n", info.data[0]);
continue;
}
if (!ZYDIS_SUCCESS(status))
{
++decoder.instructionPointer;
++readOffs;
printf("db %02X\n", info.data[0]);
continue;
}
ZydisFormatterFormatInstruction(&formatter, &info, buffer, sizeof(buffer));
puts(buffer);
}
char printBuffer[256];
ZydisFormatterFormatInstruction(&formatter, &info, printBuffer, sizeof(printBuffer));
puts(printBuffer);
readOffs += info.length;
}
if (readOffs < sizeof(readBuf))
{
memmove(readBuf, readBuf + readOffs, sizeof(readBuf) - readOffs);
}
} while (numBytesRead == sizeof(readBuf));
return 0;
}
/* ============================================================================================== */

View File

@ -40,14 +40,12 @@
#include <Zydis/Zydis.h>
typedef struct ZydisFuzzControlBlock_ {
ZydisDisassemblerMode disasMode;
ZydisDecoderFlags decoderFlags;
ZydisDisassemblerMode disasMode;
ZydisFormatterStyle formatterStyle;
ZydisFormatterFlags formatterFlags;
ZydisFormatterAddressFormat formatterAddrFormat;
ZydisFormatterDisplacementFormat formatterDispFormat;
ZydisFormatterImmediateFormat formatterImmFormat;
uint8_t bufSize;
} ZydisFuzzControlBlock;
/* ============================================================================================== */
@ -63,45 +61,53 @@ int main()
return EXIT_FAILURE;
}
ZydisFileInput input;
if (!ZYDIS_SUCCESS(ZydisInputInitFileInput(&input, stdin)))
{
fputs("failed to initialize file-input\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter,
controlBlock.formatterStyle, controlBlock.formatterFlags, controlBlock.formatterAddrFormat,
controlBlock.formatterDispFormat, controlBlock.formatterImmFormat)))
{
fputs("failed to initialized instruction-formatter\n", stderr);
fputs("failed to initialize instruction-formatter\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionDecoder decoder;
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoderEx(&decoder, controlBlock.disasMode,
(ZydisCustomInput*)&input, controlBlock.decoderFlags)))
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoder(&decoder, controlBlock.disasMode)))
{
fputs("Failed to initialize instruction-decoder\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionInfo info;
char *outBuf = malloc(controlBlock.bufSize);
while (ZYDIS_SUCCESS(ZydisDecoderDecodeNextInstruction(&decoder, &info)))
uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH];
size_t numBytesRead;
do
{
if (info.instrFlags & ZYDIS_INSTRFLAG_ERROR_MASK)
numBytesRead = fread(readBuf, 1, sizeof(readBuf), stdin);
ZydisInstructionInfo info;
ZydisStatus status;
size_t readOffs = 0;
while ((status = ZydisDecoderDecodeInstruction(
&decoder, readBuf + readOffs, numBytesRead - readOffs, &info
)) != ZYDIS_STATUS_NO_MORE_DATA)
{
printf("db %02X\n", info.data[0]);
continue;
if (!ZYDIS_SUCCESS(status))
{
++decoder.instructionPointer;
++readOffs;
continue;
}
char printBuffer[256];
ZydisFormatterFormatInstruction(&formatter, &info, printBuffer, sizeof(printBuffer));
readOffs += info.length;
}
ZydisFormatterFormatInstruction(&formatter, &info, outBuf, controlBlock.bufSize);
puts(outBuf);
}
if (readOffs < sizeof(readBuf))
{
memmove(readBuf, readBuf + readOffs, sizeof(readBuf) - readOffs);
}
} while (numBytesRead == sizeof(readBuf));
free(outBuf);
return 0;
}