zydis/tools/ZydisDisasm.c

107 lines
4.0 KiB
C
Raw Normal View History

2016-05-26 03:25:48 +08:00
/***************************************************************************************************
Zyan Disassembler Engine (Zydis)
Original Author : Florian Bernd, Joel Höner
* 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.
***************************************************************************************************/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <Zydis/Zydis.h>
/* ============================================================================================== */
/* Entry point */
/* ============================================================================================== */
int main(int argc, char** argv)
{
2017-01-11 19:18:26 +08:00
if (argc < 1 || argc > 2)
2016-05-26 03:25:48 +08:00
{
2017-01-11 19:18:26 +08:00
fprintf(stderr, "Usage: %s [input file]\n", (argc > 0 ? argv[0] : "ZydisDisasm"));
2016-05-26 03:25:48 +08:00
return EXIT_FAILURE;
}
2017-01-11 19:18:26 +08:00
FILE* file = argc >= 2 ? fopen(argv[1], "rb") : stdin;
2016-05-26 03:25:48 +08:00
if (!file)
{
fprintf(stderr, "Can not open file: %s\n", strerror(errno));
return EXIT_FAILURE;
}
ZydisInstructionFormatter formatter;
if (!ZYDIS_SUCCESS(ZydisFormatterInitInstructionFormatterEx(&formatter,
ZYDIS_FORMATTER_STYLE_INTEL, ZYDIS_FMTFLAG_FORCE_SEGMENTS | ZYDIS_FMTFLAG_FORCE_OPERANDSIZE,
ZYDIS_FORMATTER_ADDR_ABSOLUTE, ZYDIS_FORMATTER_DISP_DEFAULT, ZYDIS_FORMATTER_IMM_DEFAULT)))
2016-05-26 03:25:48 +08:00
{
fputs("Failed to initialized instruction-formatter\n", stderr);
return EXIT_FAILURE;
}
ZydisInstructionDecoder decoder;
2017-01-11 19:18:26 +08:00
if (!ZYDIS_SUCCESS(ZydisDecoderInitInstructionDecoder(&decoder, ZYDIS_DISASSEMBLER_MODE_64BIT)))
2016-05-26 03:25:48 +08:00
{
fputs("Failed to initialize instruction-decoder\n", stderr);
return EXIT_FAILURE;
}
2017-01-11 19:18:26 +08:00
uint8_t readBuf[ZYDIS_MAX_INSTRUCTION_LENGTH];
size_t numBytesRead;
do
2016-05-26 03:25:48 +08:00
{
2017-01-11 19:18:26 +08:00
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)
2016-05-26 03:25:48 +08:00
{
2017-01-11 19:18:26 +08:00
if (!ZYDIS_SUCCESS(status))
{
++decoder.instructionPointer;
++readOffs;
printf("db %02X\n", info.data[0]);
continue;
}
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);
2016-05-26 03:25:48 +08:00
}
2017-01-11 19:18:26 +08:00
} while (numBytesRead == sizeof(readBuf));
2016-05-26 03:25:48 +08:00
2017-01-11 19:18:26 +08:00
return 0;
2016-05-26 03:25:48 +08:00
}
/* ============================================================================================== */