diff --git a/include/Zydis/CommonTypes.h b/include/Zydis/CommonTypes.h index 96157c6..0de9ec4 100644 --- a/include/Zydis/CommonTypes.h +++ b/include/Zydis/CommonTypes.h @@ -2,7 +2,7 @@ Zyan Disassembler Library (Zydis) - Original Author : Florian Bernd + 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 @@ -32,18 +32,47 @@ #ifndef ZYDIS_COMMONTYPES_H #define ZYDIS_COMMONTYPES_H +#include + /* ============================================================================================== */ /* Integral types */ /* ============================================================================================== */ -/** - * uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t - */ -#include +// Fixed width integer types. +#if defined(ZYDIS_WINKERNEL) +# if !defined(ZYDIS_MSVC) +# error "Windows kernel drivers are only supported with MSVC" +# endif + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; + typedef unsigned __int64 uint64_t; + typedef __int8 int8_t; + typedef __int16 int16_t; + typedef __int32 int32_t; + typedef __int64 int64_t; +# define UINT8_MAX (255) +# define UINT16_MAX (65535U) +# define UINT32_MAX (4294967295UL) +# define UINT64_MAX (18446744073709551615ULL) +# define INT8_MAX (127) +# define INT8_MIN (-128) +# define INT16_MAX (32767) +# define INT16_MIN (-32767-1) +# define INT32_MIN (-2147483647L-1) +# define INT32_MAX (2147483647L) +# define INT64_MIN (-9223372036854775807LL-1) +# define INT64_MAX (9223372036854775807LL) +# define PRIX8 "hhX" +# define PRIX16 "hX" +# define PRIX32 "X" +# define PRIX64 "llX" +#else +# include +# include +#endif -/** - * size_t, ptrdiff_t - */ +// size_t, ptrdiff_t #include #ifdef __cplusplus diff --git a/include/Zydis/Defines.h b/include/Zydis/Defines.h index e5774bc..1f34fa0 100644 --- a/include/Zydis/Defines.h +++ b/include/Zydis/Defines.h @@ -32,8 +32,6 @@ #ifndef ZYDIS_DEFINES_H #define ZYDIS_DEFINES_H -#include -#include #include /* ============================================================================================== */ @@ -123,7 +121,12 @@ /* Debugging and optimization macros */ /* ============================================================================================== */ -#define ZYDIS_ASSERT(condition) assert(condition) +#if defined(ZYDIS_WINKERNEL) +# define ZYDIS_ASSERT(condition) +#else +# include +# define ZYDIS_ASSERT(condition) assert(condition) +#endif #if defined(ZYDIS_RELEASE) # if defined(ZYDIS_GNUC) @@ -137,7 +140,10 @@ # else # define ZYDIS_UNREACHABLE # endif +#elif defined(ZYDIS_WINKERNEL) +# define ZYDIS_UNREACHABLE #else +# include # define ZYDIS_UNREACHABLE { assert(0); abort(); } #endif diff --git a/include/Zydis/Encoder.h b/include/Zydis/Encoder.h index 5c23102..7c395d8 100644 --- a/include/Zydis/Encoder.h +++ b/include/Zydis/Encoder.h @@ -34,6 +34,8 @@ #include #include +#include +#include #include #ifdef ZYDIS_ENABLE_FEATURE_DECODER # include diff --git a/include/Zydis/Utils.h b/include/Zydis/Utils.h index 6a9d208..146c0e4 100644 --- a/include/Zydis/Utils.h +++ b/include/Zydis/Utils.h @@ -32,7 +32,6 @@ #ifndef ZYDIS_UTILS_H #define ZYDIS_UTILS_H -#include #include #include #include diff --git a/src/Encoder.c b/src/Encoder.c index 454a79f..50a864c 100644 --- a/src/Encoder.c +++ b/src/Encoder.c @@ -29,7 +29,6 @@ #include #include -#include /* ============================================================================================== */ /* Internal context and table types */ diff --git a/src/Formatter.c b/src/Formatter.c index 061febb..ffa8cad 100644 --- a/src/Formatter.c +++ b/src/Formatter.c @@ -28,9 +28,14 @@ #include #include #include -#include #include #include +#include + +#if defined(ZYDIS_WINKERNEL) +# include +# include +#endif /* ============================================================================================== */ /* String formatting */ @@ -68,6 +73,24 @@ enum ZydisStringBufferAppendModes /* Internal functions */ /* ---------------------------------------------------------------------------------------------- */ +#if defined(ZYDIS_WINKERNEL) +static int ZydisVSNPrintF(char* s, size_t n, const char* format, va_list arg) +{ + size_t bytesRemaining; + NTSTATUS ret = RtlStringCchVPrintfExA( + s, n, NULL, &bytesRemaining, 0, format, arg + ); + + if (!NT_SUCCESS(ret)) return -1; + return (int)(n - bytesRemaining); +} +#else +static int ZydisVSNPrintF(char* s, size_t n, const char* format, va_list arg) +{ + return vsnprintf(s, n, format, arg); +} +#endif + /** * @brief Appends the @c text to the given @c buffer and increases the string-buffer pointer by * the number of chars written. @@ -137,7 +160,7 @@ static ZydisStatus ZydisStringBufferAppendFormat(char** buffer, size_t bufferLen va_list arglist; va_start(arglist, format); - int w = vsnprintf(*buffer, bufferLen, format, arglist); + int w = ZydisVSNPrintF(*buffer, bufferLen, format, arglist); if ((w < 0) || ((size_t)w >= bufferLen)) { va_end(arglist); diff --git a/src/Utils.c b/src/Utils.c index 270c78d..98d5235 100644 --- a/src/Utils.c +++ b/src/Utils.c @@ -24,7 +24,6 @@ ***************************************************************************************************/ -#include #include /* ============================================================================================== */ diff --git a/tools/ZydisDisasm.c b/tools/ZydisDisasm.c index 9ffd5a9..f1b8b70 100644 --- a/tools/ZydisDisasm.c +++ b/tools/ZydisDisasm.c @@ -95,7 +95,7 @@ int main(int argc, char** argv) // TODO: Remove // DEBUG CODE START -#if 1 +#if 0 for (size_t i = 0; i < instruction.length; ++i) { printf("%02X ", *(readBuf + readOffs + i));