├── src ├── hook.cpp ├── thread.h ├── buffer.cpp ├── thread.cpp ├── trampoline.cpp ├── buffer.h ├── hook.h ├── trampoline.h ├── export.cpp ├── HDE32 │ ├── hde32.h │ ├── table32.h │ └── hde32.c ├── HDE64 │ ├── include │ │ └── hde64.h │ └── src │ │ ├── table64.h │ │ └── hde64.c └── pstdint.h ├── dll_resources ├── MinHook.rc ├── resource.h ├── MinHook.def ├── dllmain.cpp └── MinHook.h ├── .gitignore ├── README.md ├── COPYING.txt └── include └── MinHook.h /src/hook.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/src/hook.cpp -------------------------------------------------------------------------------- /src/thread.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/src/thread.h -------------------------------------------------------------------------------- /src/buffer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/src/buffer.cpp -------------------------------------------------------------------------------- /src/thread.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/src/thread.cpp -------------------------------------------------------------------------------- /src/trampoline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/src/trampoline.cpp -------------------------------------------------------------------------------- /dll_resources/MinHook.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/dll_resources/MinHook.rc -------------------------------------------------------------------------------- /dll_resources/resource.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgeHahn/DPIMangler/HEAD/dll_resources/resource.h -------------------------------------------------------------------------------- /dll_resources/MinHook.def: -------------------------------------------------------------------------------- 1 | EXPORTS 2 | MH_Initialize 3 | MH_Uninitialize 4 | 5 | MH_CreateHook 6 | MH_RemoveHook 7 | MH_EnableHook 8 | MH_DisableHook 9 | MH_QueueEnableHook 10 | MH_QueueDisableHook 11 | MH_ApplyQueued 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #OS junk files 2 | [Tt]humbs.db 3 | *.DS_Store 4 | 5 | #Visual Studio files 6 | *.[Oo]bj 7 | *.user 8 | *.aps 9 | *.pch 10 | *.vspscc 11 | *.vssscc 12 | *_i.c 13 | *_p.c 14 | *.ncb 15 | *.suo 16 | *.tlb 17 | *.tlh 18 | *.bak 19 | *.[Cc]ache 20 | *.ilk 21 | *.log 22 | *.sbr 23 | *.sdf 24 | *.opensdf 25 | *.unsuccessfulbuild 26 | ipch/ 27 | obj/ 28 | [Ll]ib 29 | [Bb]in 30 | [Dd]ebug*/ 31 | [Rr]elease*/ 32 | Ankh.NoLoad 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DPIMangler 2 | 3 | Hooks SetProcessDPIAware and blocks future calls from reaching Windows, forcefully enabling DPI scaling for many applications. 4 | 5 | Suitable to be injected into a process at initialization time or forcefully added to a process by adding to the import table. 6 | 7 | Probably works with: 8 | * Cadence OrCAD 16.6 9 | * Dashlane 10 | * Seafile 11 | * SQL Server Management Studio 12 | 13 | Probably doesn't work with: 14 | * Photoshop 15 | * Viber 16 | * RuneScape 17 | 18 | Please file an issue with the result for any applications you try this with 19 | 20 | **Note: Before using this, make sure the application manefest does not set DpiAware to true.** 21 | 22 | See [this blog post](http://www.genericmaker.com/2014/05/force-dpi-scaling-on-windows.html) for more information. 23 | -------------------------------------------------------------------------------- /src/buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - Minimalistic API Hook Library 3 | * Copyright (C) 2009 Tsuda Kageyu. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #pragma once 30 | 31 | namespace MinHook 32 | { 33 | void InitializeBuffer(); 34 | void UninitializeBuffer(); 35 | void* AllocateCodeBuffer(void* const pOrigin, size_t size); 36 | void* AllocateDataBuffer(void* const pOrigin, size_t size); 37 | void FreeBuffer(void* const pBuffer); 38 | void RollbackBuffer(); 39 | void CommitBuffer(); 40 | } 41 | -------------------------------------------------------------------------------- /src/hook.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - Minimalistic API Hook Library 3 | * Copyright (C) 2009 Tsuda Kageyu. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include "MinHook.h" 32 | 33 | namespace MinHook 34 | { 35 | MH_STATUS Initialize(); 36 | MH_STATUS Uninitialize(); 37 | MH_STATUS CreateHook(void* pTarget, void* const pDetour, void** ppOriginal); 38 | MH_STATUS RemoveHook(void* pTarget); 39 | MH_STATUS EnableHook(void* pTarget); 40 | MH_STATUS DisableHook(void* pTarget); 41 | MH_STATUS QueueEnableHook(void* pTarget); 42 | MH_STATUS QueueDisableHook(void* pTarget); 43 | MH_STATUS ApplyQueued(); 44 | } 45 | -------------------------------------------------------------------------------- /src/trampoline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - Minimalistic API Hook Library 3 | * Copyright (C) 2009 Tsuda Kageyu. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | 33 | namespace MinHook 34 | { 35 | struct TEMP_ADDR 36 | { 37 | uintptr_t address; 38 | size_t position; 39 | size_t pc; 40 | }; 41 | 42 | struct CREATE_TREMPOLINE_T 43 | { 44 | void* pTarget; 45 | void* pTrampoline; 46 | bool patchAbove; 47 | std::vector trampoline; 48 | std::vector tempAddr; 49 | #if defined _M_X64 50 | void* pTable; 51 | std::vector table; 52 | #endif 53 | std::vector oldIPs; 54 | std::vector newIPs; 55 | }; 56 | 57 | bool CreateTrampolineFunction(CREATE_TREMPOLINE_T& ct); 58 | bool ResolveTemporaryAddresses(CREATE_TREMPOLINE_T& ct); 59 | } -------------------------------------------------------------------------------- /dll_resources/dllmain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MinHook.h" 3 | 4 | bool initialized = false; 5 | 6 | typedef int (WINAPI *SETPROCESSDPIAWARE)(VOID); 7 | typedef int (WINAPI *SETPROCESSDPIAWARENESS)(VOID); // TODO: For W8.1 users, hook SetProcessDpiAwareness and nuke it too 8 | 9 | // Pointer for calling original SETPROCESSDPIAWARE. 10 | SETPROCESSDPIAWARE fpSetProcessDPIAware = NULL; 11 | 12 | // New function which overrides SetProcessDPIAware. 13 | int WINAPI NukeSetProcessDPIAware() 14 | { 15 | return 0; 16 | } 17 | 18 | BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 19 | { 20 | switch (ul_reason_for_call) 21 | { 22 | case DLL_PROCESS_ATTACH: 23 | if (initialized == true) 24 | break; 25 | 26 | initialized = true; 27 | 28 | //MessageBoxW(NULL, L"Hooking", L"DPIMangler", MB_OK); 29 | 30 | // Initialize MinHook. 31 | if (MH_Initialize() != MH_OK) 32 | { 33 | MessageBoxW(NULL, L"Initialization error", L"DPIMangler", MB_OK); 34 | return 1; 35 | } 36 | 37 | // Create a hook for SetProcessDPIAware, in disabled state. 38 | if (MH_CreateHook(&SetProcessDPIAware, &NukeSetProcessDPIAware, 39 | reinterpret_cast(&fpSetProcessDPIAware)) != MH_OK) 40 | { 41 | MessageBoxW(NULL, L"Error creating hook", L"DPIMangler", MB_OK); 42 | return 1; 43 | } 44 | 45 | // Enable the hook for SetProcessDPIAware. 46 | if (MH_EnableHook(&SetProcessDPIAware) != MH_OK) 47 | { 48 | MessageBoxW(NULL, L"Error enabling hook", L"DPIMangler", MB_OK); 49 | return 1; 50 | } 51 | 52 | //MessageBoxW(NULL, L"Hooked...", L"DPIManger", MB_OK); 53 | 54 | break; 55 | case DLL_PROCESS_DETACH: 56 | 57 | // Disable the hook for SetProcessDPIAware. 58 | if (MH_DisableHook(&SetProcessDPIAware) != MH_OK) 59 | { 60 | MessageBoxW(NULL, L"Error disabling hook", L"DPIMangler", MB_OK); 61 | return 1; 62 | } 63 | 64 | // Uninitialize MinHook. 65 | if (MH_Uninitialize() != MH_OK) 66 | { 67 | MessageBoxW(NULL, L"Error uninitializing minhook", L"DPIMangler", MB_OK); 68 | return 1; 69 | } 70 | break; 71 | } 72 | 73 | return TRUE; 74 | } 75 | -------------------------------------------------------------------------------- /src/export.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - Minimalistic API Hook Library 3 | * Copyright (C) 2009 Tsuda Kageyu. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #include 30 | #include "MinHook.h" 31 | #include "hook.h" 32 | 33 | using namespace MinHook; 34 | 35 | MH_STATUS WINAPI MH_Initialize() 36 | { 37 | return Initialize(); 38 | } 39 | 40 | MH_STATUS WINAPI MH_Uninitialize() 41 | { 42 | return Uninitialize(); 43 | } 44 | 45 | MH_STATUS WINAPI MH_CreateHook(void* pTarget, void* const pDetour, void** ppOriginal) 46 | { 47 | return CreateHook(pTarget, pDetour, ppOriginal); 48 | } 49 | 50 | MH_STATUS WINAPI MH_RemoveHook(void* pTarget) 51 | { 52 | return RemoveHook(pTarget); 53 | } 54 | 55 | MH_STATUS WINAPI MH_EnableHook(void* pTarget) 56 | { 57 | return EnableHook(pTarget); 58 | } 59 | 60 | MH_STATUS WINAPI MH_DisableHook(void* pTarget) 61 | { 62 | return DisableHook(pTarget); 63 | } 64 | 65 | MH_STATUS WINAPI MH_QueueEnableHook(void* pTarget) 66 | { 67 | return QueueEnableHook(pTarget); 68 | } 69 | 70 | MH_STATUS WINAPI MH_QueueDisableHook(void* pTarget) 71 | { 72 | return QueueDisableHook(pTarget); 73 | } 74 | 75 | MH_STATUS WINAPI MH_ApplyQueued() 76 | { 77 | return ApplyQueued(); 78 | } 79 | -------------------------------------------------------------------------------- /src/HDE32/hde32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 32 3 | * Copyright (c) 2006-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | * hde32.h: C/C++ header file 7 | * 8 | */ 9 | 10 | #ifndef _HDE32_H_ 11 | #define _HDE32_H_ 12 | 13 | /* stdint.h - C99 standard header 14 | * http://en.wikipedia.org/wiki/stdint.h 15 | * 16 | * if your compiler doesn't contain "stdint.h" header (for 17 | * example, Microsoft Visual C++), you can download file: 18 | * http://www.azillionmonkeys.com/qed/pstdint.h 19 | * and change next line to: 20 | * #include "pstdint.h" 21 | */ 22 | /* #include */ 23 | #include "../pstdint.h" 24 | 25 | #define F_MODRM 0x00000001 26 | #define F_SIB 0x00000002 27 | #define F_IMM8 0x00000004 28 | #define F_IMM16 0x00000008 29 | #define F_IMM32 0x00000010 30 | #define F_DISP8 0x00000020 31 | #define F_DISP16 0x00000040 32 | #define F_DISP32 0x00000080 33 | #define F_RELATIVE 0x00000100 34 | #define F_2IMM16 0x00000800 35 | #define F_ERROR 0x00001000 36 | #define F_ERROR_OPCODE 0x00002000 37 | #define F_ERROR_LENGTH 0x00004000 38 | #define F_ERROR_LOCK 0x00008000 39 | #define F_ERROR_OPERAND 0x00010000 40 | #define F_PREFIX_REPNZ 0x01000000 41 | #define F_PREFIX_REPX 0x02000000 42 | #define F_PREFIX_REP 0x03000000 43 | #define F_PREFIX_66 0x04000000 44 | #define F_PREFIX_67 0x08000000 45 | #define F_PREFIX_LOCK 0x10000000 46 | #define F_PREFIX_SEG 0x20000000 47 | #define F_PREFIX_ANY 0x3f000000 48 | 49 | #define PREFIX_SEGMENT_CS 0x2e 50 | #define PREFIX_SEGMENT_SS 0x36 51 | #define PREFIX_SEGMENT_DS 0x3e 52 | #define PREFIX_SEGMENT_ES 0x26 53 | #define PREFIX_SEGMENT_FS 0x64 54 | #define PREFIX_SEGMENT_GS 0x65 55 | #define PREFIX_LOCK 0xf0 56 | #define PREFIX_REPNZ 0xf2 57 | #define PREFIX_REPX 0xf3 58 | #define PREFIX_OPERAND_SIZE 0x66 59 | #define PREFIX_ADDRESS_SIZE 0x67 60 | 61 | #pragma pack(push,1) 62 | 63 | typedef struct { 64 | uint8_t len; 65 | uint8_t p_rep; 66 | uint8_t p_lock; 67 | uint8_t p_seg; 68 | uint8_t p_66; 69 | uint8_t p_67; 70 | uint8_t opcode; 71 | uint8_t opcode2; 72 | uint8_t modrm; 73 | uint8_t modrm_mod; 74 | uint8_t modrm_reg; 75 | uint8_t modrm_rm; 76 | uint8_t sib; 77 | uint8_t sib_scale; 78 | uint8_t sib_index; 79 | uint8_t sib_base; 80 | union { 81 | uint8_t imm8; 82 | uint16_t imm16; 83 | uint32_t imm32; 84 | } imm; 85 | union { 86 | uint8_t disp8; 87 | uint16_t disp16; 88 | uint32_t disp32; 89 | } disp; 90 | uint32_t flags; 91 | } hde32s; 92 | 93 | #pragma pack(pop) 94 | 95 | #ifdef __cplusplus 96 | extern "C" { 97 | #endif 98 | 99 | /* __cdecl */ 100 | unsigned int hde32_disasm(const void *code, hde32s *hs); 101 | 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | 106 | #endif /* _HDE32_H_ */ 107 | -------------------------------------------------------------------------------- /src/HDE64/include/hde64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 64 3 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | * hde64.h: C/C++ header file 7 | * 8 | */ 9 | 10 | #ifndef _HDE64_H_ 11 | #define _HDE64_H_ 12 | 13 | /* stdint.h - C99 standard header 14 | * http://en.wikipedia.org/wiki/stdint.h 15 | * 16 | * if your compiler doesn't contain "stdint.h" header (for 17 | * example, Microsoft Visual C++), you can download file: 18 | * http://www.azillionmonkeys.com/qed/pstdint.h 19 | * and change next line to: 20 | * #include "pstdint.h" 21 | */ 22 | /* #include */ 23 | #include "../../pstdint.h" 24 | 25 | #define F_MODRM 0x00000001 26 | #define F_SIB 0x00000002 27 | #define F_IMM8 0x00000004 28 | #define F_IMM16 0x00000008 29 | #define F_IMM32 0x00000010 30 | #define F_IMM64 0x00000020 31 | #define F_DISP8 0x00000040 32 | #define F_DISP16 0x00000080 33 | #define F_DISP32 0x00000100 34 | #define F_RELATIVE 0x00000200 35 | #define F_ERROR 0x00001000 36 | #define F_ERROR_OPCODE 0x00002000 37 | #define F_ERROR_LENGTH 0x00004000 38 | #define F_ERROR_LOCK 0x00008000 39 | #define F_ERROR_OPERAND 0x00010000 40 | #define F_PREFIX_REPNZ 0x01000000 41 | #define F_PREFIX_REPX 0x02000000 42 | #define F_PREFIX_REP 0x03000000 43 | #define F_PREFIX_66 0x04000000 44 | #define F_PREFIX_67 0x08000000 45 | #define F_PREFIX_LOCK 0x10000000 46 | #define F_PREFIX_SEG 0x20000000 47 | #define F_PREFIX_REX 0x40000000 48 | #define F_PREFIX_ANY 0x7f000000 49 | 50 | #define PREFIX_SEGMENT_CS 0x2e 51 | #define PREFIX_SEGMENT_SS 0x36 52 | #define PREFIX_SEGMENT_DS 0x3e 53 | #define PREFIX_SEGMENT_ES 0x26 54 | #define PREFIX_SEGMENT_FS 0x64 55 | #define PREFIX_SEGMENT_GS 0x65 56 | #define PREFIX_LOCK 0xf0 57 | #define PREFIX_REPNZ 0xf2 58 | #define PREFIX_REPX 0xf3 59 | #define PREFIX_OPERAND_SIZE 0x66 60 | #define PREFIX_ADDRESS_SIZE 0x67 61 | 62 | #pragma pack(push,1) 63 | 64 | typedef struct { 65 | uint8_t len; 66 | uint8_t p_rep; 67 | uint8_t p_lock; 68 | uint8_t p_seg; 69 | uint8_t p_66; 70 | uint8_t p_67; 71 | uint8_t rex; 72 | uint8_t rex_w; 73 | uint8_t rex_r; 74 | uint8_t rex_x; 75 | uint8_t rex_b; 76 | uint8_t opcode; 77 | uint8_t opcode2; 78 | uint8_t modrm; 79 | uint8_t modrm_mod; 80 | uint8_t modrm_reg; 81 | uint8_t modrm_rm; 82 | uint8_t sib; 83 | uint8_t sib_scale; 84 | uint8_t sib_index; 85 | uint8_t sib_base; 86 | union { 87 | uint8_t imm8; 88 | uint16_t imm16; 89 | uint32_t imm32; 90 | uint64_t imm64; 91 | } imm; 92 | union { 93 | uint8_t disp8; 94 | uint16_t disp16; 95 | uint32_t disp32; 96 | } disp; 97 | uint32_t flags; 98 | } hde64s; 99 | 100 | #pragma pack(pop) 101 | 102 | #ifdef __cplusplus 103 | extern "C" { 104 | #endif 105 | 106 | /* __cdecl */ 107 | unsigned int hde64_disasm(const void *code, hde64s *hs); 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | 113 | #endif /* _HDE64_H_ */ 114 | -------------------------------------------------------------------------------- /src/HDE32/table32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 32 C 3 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | */ 7 | 8 | #define C_NONE 0x00 9 | #define C_MODRM 0x01 10 | #define C_IMM8 0x02 11 | #define C_IMM16 0x04 12 | #define C_IMM_P66 0x10 13 | #define C_REL8 0x20 14 | #define C_REL32 0x40 15 | #define C_GROUP 0x80 16 | #define C_ERROR 0xff 17 | 18 | #define PRE_ANY 0x00 19 | #define PRE_NONE 0x01 20 | #define PRE_F2 0x02 21 | #define PRE_F3 0x04 22 | #define PRE_66 0x08 23 | #define PRE_67 0x10 24 | #define PRE_LOCK 0x20 25 | #define PRE_SEG 0x40 26 | #define PRE_ALL 0xff 27 | 28 | #define DELTA_OPCODES 0x4a 29 | #define DELTA_FPU_REG 0xf1 30 | #define DELTA_FPU_MODRM 0xf8 31 | #define DELTA_PREFIXES 0x130 32 | #define DELTA_OP_LOCK_OK 0x1a1 33 | #define DELTA_OP2_LOCK_OK 0x1b9 34 | #define DELTA_OP_ONLY_MEM 0x1cb 35 | #define DELTA_OP2_ONLY_MEM 0x1da 36 | 37 | unsigned char hde32_table[] = { 38 | 0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3, 39 | 0xa8,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xac,0xaa,0xb2,0xaa,0x9f,0x9f, 40 | 0x9f,0x9f,0xb5,0xa3,0xa3,0xa4,0xaa,0xaa,0xba,0xaa,0x96,0xaa,0xa8,0xaa,0xc3, 41 | 0xc3,0x96,0x96,0xb7,0xae,0xd6,0xbd,0xa3,0xc5,0xa3,0xa3,0x9f,0xc3,0x9c,0xaa, 42 | 0xaa,0xac,0xaa,0xbf,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0x90, 43 | 0x82,0x7d,0x97,0x59,0x59,0x59,0x59,0x59,0x7f,0x59,0x59,0x60,0x7d,0x7f,0x7f, 44 | 0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x9a,0x88,0x7d, 45 | 0x59,0x50,0x50,0x50,0x50,0x59,0x59,0x59,0x59,0x61,0x94,0x61,0x9e,0x59,0x59, 46 | 0x85,0x59,0x92,0xa3,0x60,0x60,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59, 47 | 0x59,0x59,0x9f,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xcc,0x01,0xbc,0x03,0xf0, 48 | 0x10,0x10,0x10,0x10,0x50,0x50,0x50,0x50,0x14,0x20,0x20,0x20,0x20,0x01,0x01, 49 | 0x01,0x01,0xc4,0x02,0x10,0x00,0x00,0x00,0x00,0x01,0x01,0xc0,0xc2,0x10,0x11, 50 | 0x02,0x03,0x11,0x03,0x03,0x04,0x00,0x00,0x14,0x00,0x02,0x00,0x00,0xc6,0xc8, 51 | 0x02,0x02,0x02,0x02,0x00,0x00,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xca, 52 | 0x01,0x01,0x01,0x00,0x06,0x00,0x04,0x00,0xc0,0xc2,0x01,0x01,0x03,0x01,0xff, 53 | 0xff,0x01,0x00,0x03,0xc4,0xc4,0xc6,0x03,0x01,0x01,0x01,0xff,0x03,0x03,0x03, 54 | 0xc8,0x40,0x00,0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00, 55 | 0x00,0x00,0x00,0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00, 56 | 0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 57 | 0x00,0xff,0xff,0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 58 | 0x7f,0x00,0x00,0xff,0x4a,0x4a,0x4a,0x4a,0x4b,0x52,0x4a,0x4a,0x4a,0x4a,0x4f, 59 | 0x4c,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x55,0x45,0x40,0x4a,0x4a,0x4a, 60 | 0x45,0x59,0x4d,0x46,0x4a,0x5d,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a, 61 | 0x4a,0x4a,0x4a,0x4a,0x4a,0x61,0x63,0x67,0x4e,0x4a,0x4a,0x6b,0x6d,0x4a,0x4a, 62 | 0x45,0x6d,0x4a,0x4a,0x44,0x45,0x4a,0x4a,0x00,0x00,0x00,0x02,0x0d,0x06,0x06, 63 | 0x06,0x06,0x0e,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x00,0x06,0x06,0x02,0x06, 64 | 0x00,0x0a,0x0a,0x07,0x07,0x06,0x02,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, 65 | 0x04,0x04,0x00,0x00,0x00,0x0e,0x05,0x06,0x06,0x06,0x01,0x06,0x00,0x00,0x08, 66 | 0x00,0x10,0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01, 67 | 0x86,0x00,0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba, 68 | 0xf8,0xbb,0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00, 69 | 0xc4,0xff,0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00, 70 | 0x13,0x09,0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07, 71 | 0xb2,0xff,0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf, 72 | 0xe7,0x08,0x00,0xf0,0x02,0x00 73 | }; 74 | -------------------------------------------------------------------------------- /src/HDE64/src/table64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 64 C 3 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | */ 7 | 8 | #define C_NONE 0x00 9 | #define C_MODRM 0x01 10 | #define C_IMM8 0x02 11 | #define C_IMM16 0x04 12 | #define C_IMM_P66 0x10 13 | #define C_REL8 0x20 14 | #define C_REL32 0x40 15 | #define C_GROUP 0x80 16 | #define C_ERROR 0xff 17 | 18 | #define PRE_ANY 0x00 19 | #define PRE_NONE 0x01 20 | #define PRE_F2 0x02 21 | #define PRE_F3 0x04 22 | #define PRE_66 0x08 23 | #define PRE_67 0x10 24 | #define PRE_LOCK 0x20 25 | #define PRE_SEG 0x40 26 | #define PRE_ALL 0xff 27 | 28 | #define DELTA_OPCODES 0x4a 29 | #define DELTA_FPU_REG 0xfd 30 | #define DELTA_FPU_MODRM 0x104 31 | #define DELTA_PREFIXES 0x13c 32 | #define DELTA_OP_LOCK_OK 0x1ae 33 | #define DELTA_OP2_LOCK_OK 0x1c6 34 | #define DELTA_OP_ONLY_MEM 0x1d8 35 | #define DELTA_OP2_ONLY_MEM 0x1e7 36 | 37 | unsigned char hde64_table[] = { 38 | 0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5, 39 | 0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1, 40 | 0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea, 41 | 0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0, 42 | 0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab, 43 | 0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92, 44 | 0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90, 45 | 0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b, 46 | 0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b, 47 | 0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc, 48 | 0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20, 49 | 0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff, 50 | 0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00, 51 | 0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01, 52 | 0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10, 53 | 0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00, 54 | 0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00, 55 | 0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00, 56 | 0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00, 57 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff, 58 | 0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00, 59 | 0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40, 60 | 0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43, 61 | 0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, 62 | 0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40, 63 | 0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06, 64 | 0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07, 65 | 0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, 66 | 0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10, 67 | 0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00, 68 | 0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb, 69 | 0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff, 70 | 0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09, 71 | 0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff, 72 | 0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08, 73 | 0x00,0xf0,0x02,0x00 74 | }; 75 | -------------------------------------------------------------------------------- /COPYING.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - The Minimalistic API Hooking Library for x64/x86 3 | * Copyright (c) 2009 Tsuda Kageyu. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. The name of the author may not be used to endorse or promote products 16 | * derived from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | ================================================================================ 31 | Portions of this software are Copyright (c) 2008-2009, Vyacheslav Patkov. 32 | ================================================================================ 33 | /* 34 | * Hacker Disassembler Engine 32 C 35 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 36 | * All rights reserved. 37 | * 38 | */ 39 | 40 | /* 41 | * Hacker Disassembler Engine 64 C 42 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 43 | * All rights reserved. 44 | * 45 | */ 46 | 47 | ================================================================================ 48 | Portions of this software are Copyright (c) 2005-2007 Paul Hsieh. 49 | ================================================================================ 50 | /* A portable stdint.h 51 | **************************************************************************** 52 | * BSD License: 53 | **************************************************************************** 54 | * 55 | * Copyright (c) 2005-2007 Paul Hsieh 56 | * All rights reserved. 57 | * 58 | * Redistribution and use in source and binary forms, with or without 59 | * modification, are permitted provided that the following conditions 60 | * are met: 61 | * 62 | * 1. Redistributions of source code must retain the above copyright 63 | * notice, this list of conditions and the following disclaimer. 64 | * 2. Redistributions in binary form must reproduce the above copyright 65 | * notice, this list of conditions and the following disclaimer in the 66 | * documentation and/or other materials provided with the distribution. 67 | * 3. The name of the author may not be used to endorse or promote products 68 | * derived from this software without specific prior written permission. 69 | * 70 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 71 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 72 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 73 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 74 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 75 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 76 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 77 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 78 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 79 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 80 | -------------------------------------------------------------------------------- /include/MinHook.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - Minimalistic API Hook Library 3 | * Copyright (C) 2009 Tsuda Kageyu. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | 33 | // MinHook Error Codes. 34 | typedef enum MH_STATUS 35 | { 36 | // Unknown error. Should not be returned. 37 | MH_UNKNOWN = -1, 38 | 39 | // Successful. 40 | MH_OK = 0, 41 | 42 | // MinHook is already initialized. 43 | MH_ERROR_ALREADY_INITIALIZED, 44 | 45 | // MinHook is not initialized yet, or already uninitialized. 46 | MH_ERROR_NOT_INITIALIZED, 47 | 48 | // The hook for the specified target function is already created. 49 | MH_ERROR_ALREADY_CREATED, 50 | 51 | // The hook for the specified target function is not created yet. 52 | MH_ERROR_NOT_CREATED, 53 | 54 | // The hook for the specified target function is already enabled. 55 | MH_ERROR_ENABLED, 56 | 57 | // The hook for the specified target function is not enabled yet, or already disabled. 58 | MH_ERROR_DISABLED, 59 | 60 | // The specified pointer is invalid. It points the address of non-allocated and/or non-executable region. 61 | MH_ERROR_NOT_EXECUTABLE, 62 | 63 | // The specified target function cannot be hooked. 64 | MH_ERROR_UNSUPPORTED_FUNCTION, 65 | 66 | // Failed to allocate memory. 67 | MH_ERROR_MEMORY_ALLOC, 68 | 69 | // Failed to change the memory protection. 70 | MH_ERROR_MEMORY_PROTECT 71 | } 72 | MH_STATUS; 73 | 74 | // Can be passed as a parameter to MH_EnableHook, MH_DisableHook, MH_QueueEnableHook or MH_QueueDisableHook. 75 | #define MH_ALL_HOOKS NULL 76 | 77 | #if defined __cplusplus 78 | extern "C" { 79 | #endif 80 | 81 | // Initialize the MinHook library. 82 | MH_STATUS WINAPI MH_Initialize(); 83 | 84 | // Uninitialize the MinHook library. 85 | MH_STATUS WINAPI MH_Uninitialize(); 86 | 87 | // Creates the Hook for the specified target function, in disabled state. 88 | // Parameters: 89 | // pTarget [in] A pointer to the target function, which will be overridden by the detour function. 90 | // pDetour [in] A pointer to the detour function, which will override the target function. 91 | // ppOriginal [out] A pointer to the trampoline function, which will be used to call the original target function. 92 | MH_STATUS WINAPI MH_CreateHook(void* pTarget, void* const pDetour, void** ppOriginal); 93 | 94 | // Removes the already created hook. 95 | // Parameters: 96 | // pTarget [in] A pointer to the target function. 97 | MH_STATUS WINAPI MH_RemoveHook(void* pTarget); 98 | 99 | // Enables the already created hook. 100 | // Parameters: 101 | // pTarget [in] A pointer to the target function. 102 | // If this parameter is MH_ALL_HOOKS, all created hooks are enabled in one go. 103 | MH_STATUS WINAPI MH_EnableHook(void* pTarget); 104 | 105 | // Disables the already created hook. 106 | // Parameters: 107 | // pTarget [in] A pointer to the target function. 108 | // If this parameter is MH_ALL_HOOKS, all created hooks are disabled in one go. 109 | MH_STATUS WINAPI MH_DisableHook(void* pTarget); 110 | 111 | // Queues to enable the already created hook. 112 | // Parameters: 113 | // pTarget [in] A pointer to the target function. 114 | // If this parameter is MH_ALL_HOOKS, all created hooks are queued to be enabled. 115 | MH_STATUS WINAPI MH_QueueEnableHook(void* pTarget); 116 | 117 | // Queues to disable the already created hook. 118 | // Parameters: 119 | // pTarget [in] A pointer to the target function. 120 | // If this parameter is MH_ALL_HOOKS, all created hooks are queued to be disabled. 121 | MH_STATUS WINAPI MH_QueueDisableHook(void* pTarget); 122 | 123 | // Applies all queued changes in one go. 124 | MH_STATUS WINAPI MH_ApplyQueued(); 125 | 126 | #if defined __cplusplus 127 | } 128 | #endif 129 | 130 | -------------------------------------------------------------------------------- /dll_resources/MinHook.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MinHook - Minimalistic API Hook Library 3 | * Copyright (C) 2009 Tsuda Kageyu. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | 33 | // MinHook Error Codes. 34 | typedef enum MH_STATUS 35 | { 36 | // Unknown error. Should not be returned. 37 | MH_UNKNOWN = -1, 38 | 39 | // Successful. 40 | MH_OK = 0, 41 | 42 | // MinHook is already initialized. 43 | MH_ERROR_ALREADY_INITIALIZED, 44 | 45 | // MinHook is not initialized yet, or already uninitialized. 46 | MH_ERROR_NOT_INITIALIZED, 47 | 48 | // The hook for the specified target function is already created. 49 | MH_ERROR_ALREADY_CREATED, 50 | 51 | // The hook for the specified target function is not created yet. 52 | MH_ERROR_NOT_CREATED, 53 | 54 | // The hook for the specified target function is already enabled. 55 | MH_ERROR_ENABLED, 56 | 57 | // The hook for the specified target function is not enabled yet, or already disabled. 58 | MH_ERROR_DISABLED, 59 | 60 | // The specified pointer is invalid. It points the address of non-allocated and/or non-executable region. 61 | MH_ERROR_NOT_EXECUTABLE, 62 | 63 | // The specified target function cannot be hooked. 64 | MH_ERROR_UNSUPPORTED_FUNCTION, 65 | 66 | // Failed to allocate memory. 67 | MH_ERROR_MEMORY_ALLOC, 68 | 69 | // Failed to change the memory protection. 70 | MH_ERROR_MEMORY_PROTECT 71 | } 72 | MH_STATUS; 73 | 74 | // Can be passed as a parameter to MH_EnableHook, MH_DisableHook, MH_QueueEnableHook or MH_QueueDisableHook. 75 | #define MH_ALL_HOOKS NULL 76 | 77 | #if defined __cplusplus 78 | extern "C" { 79 | #endif 80 | 81 | // Initialize the MinHook library. 82 | MH_STATUS WINAPI MH_Initialize(); 83 | 84 | // Uninitialize the MinHook library. 85 | MH_STATUS WINAPI MH_Uninitialize(); 86 | 87 | // Creates the Hook for the specified target function, in disabled state. 88 | // Parameters: 89 | // pTarget [in] A pointer to the target function, which will be overridden by the detour function. 90 | // pDetour [in] A pointer to the detour function, which will override the target function. 91 | // ppOriginal [out] A pointer to the trampoline function, which will be used to call the original target function. 92 | MH_STATUS WINAPI MH_CreateHook(void* pTarget, void* const pDetour, void** ppOriginal); 93 | 94 | // Removes the already created hook. 95 | // Parameters: 96 | // pTarget [in] A pointer to the target function. 97 | MH_STATUS WINAPI MH_RemoveHook(void* pTarget); 98 | 99 | // Enables the already created hook. 100 | // Parameters: 101 | // pTarget [in] A pointer to the target function. 102 | // If this parameter is MH_ALL_HOOKS, all created hooks are enabled in one go. 103 | MH_STATUS WINAPI MH_EnableHook(void* pTarget); 104 | 105 | // Disables the already created hook. 106 | // Parameters: 107 | // pTarget [in] A pointer to the target function. 108 | // If this parameter is MH_ALL_HOOKS, all created hooks are disabled in one go. 109 | MH_STATUS WINAPI MH_DisableHook(void* pTarget); 110 | 111 | // Queues to enable the already created hook. 112 | // Parameters: 113 | // pTarget [in] A pointer to the target function. 114 | // If this parameter is MH_ALL_HOOKS, all created hooks are queued to be enabled. 115 | MH_STATUS WINAPI MH_QueueEnableHook(void* pTarget); 116 | 117 | // Queues to disable the already created hook. 118 | // Parameters: 119 | // pTarget [in] A pointer to the target function. 120 | // If this parameter is MH_ALL_HOOKS, all created hooks are queued to be disabled. 121 | MH_STATUS WINAPI MH_QueueDisableHook(void* pTarget); 122 | 123 | // Applies all queued changes in one go. 124 | MH_STATUS WINAPI MH_ApplyQueued(); 125 | 126 | #if defined __cplusplus 127 | } 128 | #endif 129 | 130 | -------------------------------------------------------------------------------- /src/HDE32/hde32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 32 C 3 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | */ 7 | 8 | /* #include */ 9 | #include 10 | 11 | #include "hde32.h" 12 | #include "table32.h" 13 | 14 | unsigned int hde32_disasm(const void *code, hde32s *hs) 15 | { 16 | uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0; 17 | uint8_t *ht = hde32_table, m_mod, m_reg, m_rm, disp_size = 0; 18 | 19 | memset(hs,0,sizeof(hde32s)); 20 | 21 | for (x = 16; x; x--) 22 | switch (c = *p++) { 23 | case 0xf3: 24 | hs->p_rep = c; 25 | pref |= PRE_F3; 26 | break; 27 | case 0xf2: 28 | hs->p_rep = c; 29 | pref |= PRE_F2; 30 | break; 31 | case 0xf0: 32 | hs->p_lock = c; 33 | pref |= PRE_LOCK; 34 | break; 35 | case 0x26: case 0x2e: case 0x36: 36 | case 0x3e: case 0x64: case 0x65: 37 | hs->p_seg = c; 38 | pref |= PRE_SEG; 39 | break; 40 | case 0x66: 41 | hs->p_66 = c; 42 | pref |= PRE_66; 43 | break; 44 | case 0x67: 45 | hs->p_67 = c; 46 | pref |= PRE_67; 47 | break; 48 | default: 49 | goto pref_done; 50 | } 51 | pref_done: 52 | 53 | hs->flags = (uint32_t)pref << 23; 54 | 55 | if (!pref) 56 | pref |= PRE_NONE; 57 | 58 | if ((hs->opcode = c) == 0x0f) { 59 | hs->opcode2 = c = *p++; 60 | ht += DELTA_OPCODES; 61 | } else if (c >= 0xa0 && c <= 0xa3) { 62 | if (pref & PRE_67) 63 | pref |= PRE_66; 64 | else 65 | pref &= ~PRE_66; 66 | } 67 | 68 | opcode = c; 69 | cflags = ht[ht[opcode / 4] + (opcode % 4)]; 70 | 71 | if (cflags == C_ERROR) { 72 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 73 | cflags = 0; 74 | if ((opcode & -3) == 0x24) 75 | cflags++; 76 | } 77 | 78 | x = 0; 79 | if (cflags & C_GROUP) { 80 | uint16_t t; 81 | t = *(uint16_t *)(ht + (cflags & 0x7f)); 82 | cflags = (uint8_t)t; 83 | x = (uint8_t)(t >> 8); 84 | } 85 | 86 | if (hs->opcode2) { 87 | ht = hde32_table + DELTA_PREFIXES; 88 | if (ht[ht[opcode / 4] + (opcode % 4)] & pref) 89 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 90 | } 91 | 92 | if (cflags & C_MODRM) { 93 | hs->flags |= F_MODRM; 94 | hs->modrm = c = *p++; 95 | hs->modrm_mod = m_mod = c >> 6; 96 | hs->modrm_rm = m_rm = c & 7; 97 | hs->modrm_reg = m_reg = (c & 0x3f) >> 3; 98 | 99 | if (x && ((x << m_reg) & 0x80)) 100 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 101 | 102 | if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { 103 | uint8_t t = opcode - 0xd9; 104 | if (m_mod == 3) { 105 | ht = hde32_table + DELTA_FPU_MODRM + t*8; 106 | t = ht[m_reg] << m_rm; 107 | } else { 108 | ht = hde32_table + DELTA_FPU_REG; 109 | t = ht[t] << m_reg; 110 | } 111 | if (t & 0x80) 112 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 113 | } 114 | 115 | if (pref & PRE_LOCK) { 116 | if (m_mod == 3) { 117 | hs->flags |= F_ERROR | F_ERROR_LOCK; 118 | } else { 119 | uint8_t *table_end, op = opcode; 120 | if (hs->opcode2) { 121 | ht = hde32_table + DELTA_OP2_LOCK_OK; 122 | table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; 123 | } else { 124 | ht = hde32_table + DELTA_OP_LOCK_OK; 125 | table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; 126 | op &= -2; 127 | } 128 | for (; ht != table_end; ht++) 129 | if (*ht++ == op) { 130 | if (!((*ht << m_reg) & 0x80)) 131 | goto no_lock_error; 132 | else 133 | break; 134 | } 135 | hs->flags |= F_ERROR | F_ERROR_LOCK; 136 | no_lock_error: 137 | ; 138 | } 139 | } 140 | 141 | if (hs->opcode2) { 142 | switch (opcode) { 143 | case 0x20: case 0x22: 144 | m_mod = 3; 145 | if (m_reg > 4 || m_reg == 1) 146 | goto error_operand; 147 | else 148 | goto no_error_operand; 149 | case 0x21: case 0x23: 150 | m_mod = 3; 151 | if (m_reg == 4 || m_reg == 5) 152 | goto error_operand; 153 | else 154 | goto no_error_operand; 155 | } 156 | } else { 157 | switch (opcode) { 158 | case 0x8c: 159 | if (m_reg > 5) 160 | goto error_operand; 161 | else 162 | goto no_error_operand; 163 | case 0x8e: 164 | if (m_reg == 1 || m_reg > 5) 165 | goto error_operand; 166 | else 167 | goto no_error_operand; 168 | } 169 | } 170 | 171 | if (m_mod == 3) { 172 | uint8_t *table_end; 173 | if (hs->opcode2) { 174 | ht = hde32_table + DELTA_OP2_ONLY_MEM; 175 | table_end = ht + sizeof(hde32_table) - DELTA_OP2_ONLY_MEM; 176 | } else { 177 | ht = hde32_table + DELTA_OP_ONLY_MEM; 178 | table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; 179 | } 180 | for (; ht != table_end; ht += 2) 181 | if (*ht++ == opcode) { 182 | if (*ht++ & pref && !((*ht << m_reg) & 0x80)) 183 | goto error_operand; 184 | else 185 | break; 186 | } 187 | goto no_error_operand; 188 | } else if (hs->opcode2) { 189 | switch (opcode) { 190 | case 0x50: case 0xd7: case 0xf7: 191 | if (pref & (PRE_NONE | PRE_66)) 192 | goto error_operand; 193 | break; 194 | case 0xd6: 195 | if (pref & (PRE_F2 | PRE_F3)) 196 | goto error_operand; 197 | break; 198 | case 0xc5: 199 | goto error_operand; 200 | } 201 | goto no_error_operand; 202 | } else 203 | goto no_error_operand; 204 | 205 | error_operand: 206 | hs->flags |= F_ERROR | F_ERROR_OPERAND; 207 | no_error_operand: 208 | 209 | c = *p++; 210 | if (m_reg <= 1) { 211 | if (opcode == 0xf6) 212 | cflags |= C_IMM8; 213 | else if (opcode == 0xf7) 214 | cflags |= C_IMM_P66; 215 | } 216 | 217 | switch (m_mod) { 218 | case 0: 219 | if (pref & PRE_67) { 220 | if (m_rm == 6) 221 | disp_size = 2; 222 | } else 223 | if (m_rm == 5) 224 | disp_size = 4; 225 | break; 226 | case 1: 227 | disp_size = 1; 228 | break; 229 | case 2: 230 | disp_size = 2; 231 | if (!(pref & PRE_67)) 232 | disp_size <<= 1; 233 | } 234 | 235 | if (m_mod != 3 && m_rm == 4 && !(pref & PRE_67)) { 236 | hs->flags |= F_SIB; 237 | p++; 238 | hs->sib = c; 239 | hs->sib_scale = c >> 6; 240 | hs->sib_index = (c & 0x3f) >> 3; 241 | if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) 242 | disp_size = 4; 243 | } 244 | 245 | p--; 246 | switch (disp_size) { 247 | case 1: 248 | hs->flags |= F_DISP8; 249 | hs->disp.disp8 = *p; 250 | break; 251 | case 2: 252 | hs->flags |= F_DISP16; 253 | hs->disp.disp16 = *(uint16_t *)p; 254 | break; 255 | case 4: 256 | hs->flags |= F_DISP32; 257 | hs->disp.disp32 = *(uint32_t *)p; 258 | } 259 | p += disp_size; 260 | } else if (pref & PRE_LOCK) 261 | hs->flags |= F_ERROR | F_ERROR_LOCK; 262 | 263 | if (cflags & C_IMM_P66) { 264 | if (cflags & C_REL32) { 265 | if (pref & PRE_66) { 266 | hs->flags |= F_IMM16 | F_RELATIVE; 267 | hs->imm.imm16 = *(uint16_t *)p; 268 | p += 2; 269 | goto disasm_done; 270 | } 271 | goto rel32_ok; 272 | } 273 | if (pref & PRE_66) { 274 | hs->flags |= F_IMM16; 275 | hs->imm.imm16 = *(uint16_t *)p; 276 | p += 2; 277 | } else { 278 | hs->flags |= F_IMM32; 279 | hs->imm.imm32 = *(uint32_t *)p; 280 | p += 4; 281 | } 282 | } 283 | 284 | if (cflags & C_IMM16) { 285 | if (hs->flags & F_IMM32) { 286 | hs->flags |= F_IMM16; 287 | hs->disp.disp16 = *(uint16_t *)p; 288 | } else if (hs->flags & F_IMM16) { 289 | hs->flags |= F_2IMM16; 290 | hs->disp.disp16 = *(uint16_t *)p; 291 | } else { 292 | hs->flags |= F_IMM16; 293 | hs->imm.imm16 = *(uint16_t *)p; 294 | } 295 | p += 2; 296 | } 297 | if (cflags & C_IMM8) { 298 | hs->flags |= F_IMM8; 299 | hs->imm.imm8 = *p++; 300 | } 301 | 302 | if (cflags & C_REL32) { 303 | rel32_ok: 304 | hs->flags |= F_IMM32 | F_RELATIVE; 305 | hs->imm.imm32 = *(uint32_t *)p; 306 | p += 4; 307 | } else if (cflags & C_REL8) { 308 | hs->flags |= F_IMM8 | F_RELATIVE; 309 | hs->imm.imm8 = *p++; 310 | } 311 | 312 | disasm_done: 313 | 314 | if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) { 315 | hs->flags |= F_ERROR | F_ERROR_LENGTH; 316 | hs->len = 15; 317 | } 318 | 319 | return (unsigned int)hs->len; 320 | } 321 | -------------------------------------------------------------------------------- /src/HDE64/src/hde64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Hacker Disassembler Engine 64 C 3 | * Copyright (c) 2008-2009, Vyacheslav Patkov. 4 | * All rights reserved. 5 | * 6 | */ 7 | 8 | /* #include */ 9 | #include 10 | 11 | #include "../include/hde64.h" 12 | #include "table64.h" 13 | 14 | unsigned int hde64_disasm(const void *code, hde64s *hs) 15 | { 16 | uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0; 17 | uint8_t *ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0; 18 | uint8_t op64 = 0; 19 | 20 | memset(hs,0,sizeof(hde64s)); 21 | 22 | for (x = 16; x; x--) 23 | switch (c = *p++) { 24 | case 0xf3: 25 | hs->p_rep = c; 26 | pref |= PRE_F3; 27 | break; 28 | case 0xf2: 29 | hs->p_rep = c; 30 | pref |= PRE_F2; 31 | break; 32 | case 0xf0: 33 | hs->p_lock = c; 34 | pref |= PRE_LOCK; 35 | break; 36 | case 0x26: case 0x2e: case 0x36: 37 | case 0x3e: case 0x64: case 0x65: 38 | hs->p_seg = c; 39 | pref |= PRE_SEG; 40 | break; 41 | case 0x66: 42 | hs->p_66 = c; 43 | pref |= PRE_66; 44 | break; 45 | case 0x67: 46 | hs->p_67 = c; 47 | pref |= PRE_67; 48 | break; 49 | default: 50 | goto pref_done; 51 | } 52 | pref_done: 53 | 54 | hs->flags = (uint32_t)pref << 23; 55 | 56 | if (!pref) 57 | pref |= PRE_NONE; 58 | 59 | if ((c & 0xf0) == 0x40) { 60 | hs->flags |= F_PREFIX_REX; 61 | if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8) 62 | op64++; 63 | hs->rex_r = (c & 7) >> 2; 64 | hs->rex_x = (c & 3) >> 1; 65 | hs->rex_b = c & 1; 66 | if (((c = *p++) & 0xf0) == 0x40) { 67 | opcode = c; 68 | goto error_opcode; 69 | } 70 | } 71 | 72 | if ((hs->opcode = c) == 0x0f) { 73 | hs->opcode2 = c = *p++; 74 | ht += DELTA_OPCODES; 75 | } else if (c >= 0xa0 && c <= 0xa3) { 76 | op64++; 77 | if (pref & PRE_67) 78 | pref |= PRE_66; 79 | else 80 | pref &= ~PRE_66; 81 | } 82 | 83 | opcode = c; 84 | cflags = ht[ht[opcode / 4] + (opcode % 4)]; 85 | 86 | if (cflags == C_ERROR) { 87 | error_opcode: 88 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 89 | cflags = 0; 90 | if ((opcode & -3) == 0x24) 91 | cflags++; 92 | } 93 | 94 | x = 0; 95 | if (cflags & C_GROUP) { 96 | uint16_t t; 97 | t = *(uint16_t *)(ht + (cflags & 0x7f)); 98 | cflags = (uint8_t)t; 99 | x = (uint8_t)(t >> 8); 100 | } 101 | 102 | if (hs->opcode2) { 103 | ht = hde64_table + DELTA_PREFIXES; 104 | if (ht[ht[opcode / 4] + (opcode % 4)] & pref) 105 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 106 | } 107 | 108 | if (cflags & C_MODRM) { 109 | hs->flags |= F_MODRM; 110 | hs->modrm = c = *p++; 111 | hs->modrm_mod = m_mod = c >> 6; 112 | hs->modrm_rm = m_rm = c & 7; 113 | hs->modrm_reg = m_reg = (c & 0x3f) >> 3; 114 | 115 | if (x && ((x << m_reg) & 0x80)) 116 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 117 | 118 | if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { 119 | uint8_t t = opcode - 0xd9; 120 | if (m_mod == 3) { 121 | ht = hde64_table + DELTA_FPU_MODRM + t*8; 122 | t = ht[m_reg] << m_rm; 123 | } else { 124 | ht = hde64_table + DELTA_FPU_REG; 125 | t = ht[t] << m_reg; 126 | } 127 | if (t & 0x80) 128 | hs->flags |= F_ERROR | F_ERROR_OPCODE; 129 | } 130 | 131 | if (pref & PRE_LOCK) { 132 | if (m_mod == 3) { 133 | hs->flags |= F_ERROR | F_ERROR_LOCK; 134 | } else { 135 | uint8_t *table_end, op = opcode; 136 | if (hs->opcode2) { 137 | ht = hde64_table + DELTA_OP2_LOCK_OK; 138 | table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; 139 | } else { 140 | ht = hde64_table + DELTA_OP_LOCK_OK; 141 | table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; 142 | op &= -2; 143 | } 144 | for (; ht != table_end; ht++) 145 | if (*ht++ == op) { 146 | if (!((*ht << m_reg) & 0x80)) 147 | goto no_lock_error; 148 | else 149 | break; 150 | } 151 | hs->flags |= F_ERROR | F_ERROR_LOCK; 152 | no_lock_error: 153 | ; 154 | } 155 | } 156 | 157 | if (hs->opcode2) { 158 | switch (opcode) { 159 | case 0x20: case 0x22: 160 | m_mod = 3; 161 | if (m_reg > 4 || m_reg == 1) 162 | goto error_operand; 163 | else 164 | goto no_error_operand; 165 | case 0x21: case 0x23: 166 | m_mod = 3; 167 | if (m_reg == 4 || m_reg == 5) 168 | goto error_operand; 169 | else 170 | goto no_error_operand; 171 | } 172 | } else { 173 | switch (opcode) { 174 | case 0x8c: 175 | if (m_reg > 5) 176 | goto error_operand; 177 | else 178 | goto no_error_operand; 179 | case 0x8e: 180 | if (m_reg == 1 || m_reg > 5) 181 | goto error_operand; 182 | else 183 | goto no_error_operand; 184 | } 185 | } 186 | 187 | if (m_mod == 3) { 188 | uint8_t *table_end; 189 | if (hs->opcode2) { 190 | ht = hde64_table + DELTA_OP2_ONLY_MEM; 191 | table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM; 192 | } else { 193 | ht = hde64_table + DELTA_OP_ONLY_MEM; 194 | table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; 195 | } 196 | for (; ht != table_end; ht += 2) 197 | if (*ht++ == opcode) { 198 | if (*ht++ & pref && !((*ht << m_reg) & 0x80)) 199 | goto error_operand; 200 | else 201 | break; 202 | } 203 | goto no_error_operand; 204 | } else if (hs->opcode2) { 205 | switch (opcode) { 206 | case 0x50: case 0xd7: case 0xf7: 207 | if (pref & (PRE_NONE | PRE_66)) 208 | goto error_operand; 209 | break; 210 | case 0xd6: 211 | if (pref & (PRE_F2 | PRE_F3)) 212 | goto error_operand; 213 | break; 214 | case 0xc5: 215 | goto error_operand; 216 | } 217 | goto no_error_operand; 218 | } else 219 | goto no_error_operand; 220 | 221 | error_operand: 222 | hs->flags |= F_ERROR | F_ERROR_OPERAND; 223 | no_error_operand: 224 | 225 | c = *p++; 226 | if (m_reg <= 1) { 227 | if (opcode == 0xf6) 228 | cflags |= C_IMM8; 229 | else if (opcode == 0xf7) 230 | cflags |= C_IMM_P66; 231 | } 232 | 233 | switch (m_mod) { 234 | case 0: 235 | if (pref & PRE_67) { 236 | if (m_rm == 6) 237 | disp_size = 2; 238 | } else 239 | if (m_rm == 5) 240 | disp_size = 4; 241 | break; 242 | case 1: 243 | disp_size = 1; 244 | break; 245 | case 2: 246 | disp_size = 2; 247 | if (!(pref & PRE_67)) 248 | disp_size <<= 1; 249 | } 250 | 251 | if (m_mod != 3 && m_rm == 4) { 252 | hs->flags |= F_SIB; 253 | p++; 254 | hs->sib = c; 255 | hs->sib_scale = c >> 6; 256 | hs->sib_index = (c & 0x3f) >> 3; 257 | if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) 258 | disp_size = 4; 259 | } 260 | 261 | p--; 262 | switch (disp_size) { 263 | case 1: 264 | hs->flags |= F_DISP8; 265 | hs->disp.disp8 = *p; 266 | break; 267 | case 2: 268 | hs->flags |= F_DISP16; 269 | hs->disp.disp16 = *(uint16_t *)p; 270 | break; 271 | case 4: 272 | hs->flags |= F_DISP32; 273 | hs->disp.disp32 = *(uint32_t *)p; 274 | } 275 | p += disp_size; 276 | } else if (pref & PRE_LOCK) 277 | hs->flags |= F_ERROR | F_ERROR_LOCK; 278 | 279 | if (cflags & C_IMM_P66) { 280 | if (cflags & C_REL32) { 281 | if (pref & PRE_66) { 282 | hs->flags |= F_IMM16 | F_RELATIVE; 283 | hs->imm.imm16 = *(uint16_t *)p; 284 | p += 2; 285 | goto disasm_done; 286 | } 287 | goto rel32_ok; 288 | } 289 | if (op64) { 290 | hs->flags |= F_IMM64; 291 | hs->imm.imm64 = *(uint64_t *)p; 292 | p += 8; 293 | } else if (!(pref & PRE_66)) { 294 | hs->flags |= F_IMM32; 295 | hs->imm.imm32 = *(uint32_t *)p; 296 | p += 4; 297 | } else 298 | goto imm16_ok; 299 | } 300 | 301 | 302 | if (cflags & C_IMM16) { 303 | imm16_ok: 304 | hs->flags |= F_IMM16; 305 | hs->imm.imm16 = *(uint16_t *)p; 306 | p += 2; 307 | } 308 | if (cflags & C_IMM8) { 309 | hs->flags |= F_IMM8; 310 | hs->imm.imm8 = *p++; 311 | } 312 | 313 | if (cflags & C_REL32) { 314 | rel32_ok: 315 | hs->flags |= F_IMM32 | F_RELATIVE; 316 | hs->imm.imm32 = *(uint32_t *)p; 317 | p += 4; 318 | } else if (cflags & C_REL8) { 319 | hs->flags |= F_IMM8 | F_RELATIVE; 320 | hs->imm.imm8 = *p++; 321 | } 322 | 323 | disasm_done: 324 | 325 | if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) { 326 | hs->flags |= F_ERROR | F_ERROR_LENGTH; 327 | hs->len = 15; 328 | } 329 | 330 | return (unsigned int)hs->len; 331 | } 332 | -------------------------------------------------------------------------------- /src/pstdint.h: -------------------------------------------------------------------------------- 1 | /* A portable stdint.h 2 | **************************************************************************** 3 | * BSD License: 4 | **************************************************************************** 5 | * 6 | * Copyright (c) 2005-2007 Paul Hsieh 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. The name of the author may not be used to endorse or promote products 19 | * derived from this software without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | **************************************************************************** 33 | * 34 | * Version 0.1.11 35 | * 36 | * The ANSI C standard committee, for the C99 standard, specified the 37 | * inclusion of a new standard include file called stdint.h. This is 38 | * a very useful and long desired include file which contains several 39 | * very precise definitions for integer scalar types that is 40 | * critically important for making portable several classes of 41 | * applications including cryptography, hashing, variable length 42 | * integer libraries and so on. But for most developers its likely 43 | * useful just for programming sanity. 44 | * 45 | * The problem is that most compiler vendors have decided not to 46 | * implement the C99 standard, and the next C++ language standard 47 | * (which has a lot more mindshare these days) will be a long time in 48 | * coming and its unknown whether or not it will include stdint.h or 49 | * how much adoption it will have. Either way, it will be a long time 50 | * before all compilers come with a stdint.h and it also does nothing 51 | * for the extremely large number of compilers available today which 52 | * do not include this file, or anything comparable to it. 53 | * 54 | * So that's what this file is all about. Its an attempt to build a 55 | * single universal include file that works on as many platforms as 56 | * possible to deliver what stdint.h is supposed to. A few things 57 | * that should be noted about this file: 58 | * 59 | * 1) It is not guaranteed to be portable and/or present an identical 60 | * interface on all platforms. The extreme variability of the 61 | * ANSI C standard makes this an impossibility right from the 62 | * very get go. Its really only meant to be useful for the vast 63 | * majority of platforms that possess the capability of 64 | * implementing usefully and precisely defined, standard sized 65 | * integer scalars. Systems which are not intrinsically 2s 66 | * complement may produce invalid constants. 67 | * 68 | * 2) There is an unavoidable use of non-reserved symbols. 69 | * 70 | * 3) Other standard include files are invoked. 71 | * 72 | * 4) This file may come in conflict with future platforms that do 73 | * include stdint.h. The hope is that one or the other can be 74 | * used with no real difference. 75 | * 76 | * 5) In the current verison, if your platform can't represent 77 | * int32_t, int16_t and int8_t, it just dumps out with a compiler 78 | * error. 79 | * 80 | * 6) 64 bit integers may or may not be defined. Test for their 81 | * presence with the test: #ifdef INT64_MAX or #ifdef UINT64_MAX. 82 | * Note that this is different from the C99 specification which 83 | * requires the existence of 64 bit support in the compiler. If 84 | * this is not defined for your platform, yet it is capable of 85 | * dealing with 64 bits then it is because this file has not yet 86 | * been extended to cover all of your system's capabilities. 87 | * 88 | * 7) (u)intptr_t may or may not be defined. Test for its presence 89 | * with the test: #ifdef PTRDIFF_MAX. If this is not defined 90 | * for your platform, then it is because this file has not yet 91 | * been extended to cover all of your system's capabilities, not 92 | * because its optional. 93 | * 94 | * 8) The following might not been defined even if your platform is 95 | * capable of defining it: 96 | * 97 | * WCHAR_MIN 98 | * WCHAR_MAX 99 | * (u)int64_t 100 | * PTRDIFF_MIN 101 | * PTRDIFF_MAX 102 | * (u)intptr_t 103 | * 104 | * 9) The following have not been defined: 105 | * 106 | * WINT_MIN 107 | * WINT_MAX 108 | * 109 | * 10) The criteria for defining (u)int_least(*)_t isn't clear, 110 | * except for systems which don't have a type that precisely 111 | * defined 8, 16, or 32 bit types (which this include file does 112 | * not support anyways). Default definitions have been given. 113 | * 114 | * 11) The criteria for defining (u)int_fast(*)_t isn't something I 115 | * would trust to any particular compiler vendor or the ANSI C 116 | * committee. It is well known that "compatible systems" are 117 | * commonly created that have very different performance 118 | * characteristics from the systems they are compatible with, 119 | * especially those whose vendors make both the compiler and the 120 | * system. Default definitions have been given, but its strongly 121 | * recommended that users never use these definitions for any 122 | * reason (they do *NOT* deliver any serious guarantee of 123 | * improved performance -- not in this file, nor any vendor's 124 | * stdint.h). 125 | * 126 | * 12) The following macros: 127 | * 128 | * PRINTF_INTMAX_MODIFIER 129 | * PRINTF_INT64_MODIFIER 130 | * PRINTF_INT32_MODIFIER 131 | * PRINTF_INT16_MODIFIER 132 | * PRINTF_LEAST64_MODIFIER 133 | * PRINTF_LEAST32_MODIFIER 134 | * PRINTF_LEAST16_MODIFIER 135 | * PRINTF_INTPTR_MODIFIER 136 | * 137 | * are strings which have been defined as the modifiers required 138 | * for the "d", "u" and "x" printf formats to correctly output 139 | * (u)intmax_t, (u)int64_t, (u)int32_t, (u)int16_t, (u)least64_t, 140 | * (u)least32_t, (u)least16_t and (u)intptr_t types respectively. 141 | * PRINTF_INTPTR_MODIFIER is not defined for some systems which 142 | * provide their own stdint.h. PRINTF_INT64_MODIFIER is not 143 | * defined if INT64_MAX is not defined. These are an extension 144 | * beyond what C99 specifies must be in stdint.h. 145 | * 146 | * In addition, the following macros are defined: 147 | * 148 | * PRINTF_INTMAX_HEX_WIDTH 149 | * PRINTF_INT64_HEX_WIDTH 150 | * PRINTF_INT32_HEX_WIDTH 151 | * PRINTF_INT16_HEX_WIDTH 152 | * PRINTF_INT8_HEX_WIDTH 153 | * PRINTF_INTMAX_DEC_WIDTH 154 | * PRINTF_INT64_DEC_WIDTH 155 | * PRINTF_INT32_DEC_WIDTH 156 | * PRINTF_INT16_DEC_WIDTH 157 | * PRINTF_INT8_DEC_WIDTH 158 | * 159 | * Which specifies the maximum number of characters required to 160 | * print the number of that type in either hexadecimal or decimal. 161 | * These are an extension beyond what C99 specifies must be in 162 | * stdint.h. 163 | * 164 | * Compilers tested (all with 0 warnings at their highest respective 165 | * settings): Borland Turbo C 2.0, WATCOM C/C++ 11.0 (16 bits and 32 166 | * bits), Microsoft Visual C++ 6.0 (32 bit), Microsoft Visual Studio 167 | * .net (VC7), Intel C++ 4.0, GNU gcc v3.3.3 168 | * 169 | * This file should be considered a work in progress. Suggestions for 170 | * improvements, especially those which increase coverage are strongly 171 | * encouraged. 172 | * 173 | * Acknowledgements 174 | * 175 | * The following people have made significant contributions to the 176 | * development and testing of this file: 177 | * 178 | * Chris Howie 179 | * John Steele Scott 180 | * Dave Thorup 181 | * 182 | */ 183 | 184 | #include 185 | #include 186 | #include 187 | 188 | /* 189 | * For gcc with _STDINT_H, fill in the PRINTF_INT*_MODIFIER macros, and 190 | * do nothing else. On the Mac OS X version of gcc this is _STDINT_H_. 191 | */ 192 | 193 | #if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined (__WATCOMC__) && (defined (_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_)) )) && !defined (_PSTDINT_H_INCLUDED) 194 | #include 195 | #define _PSTDINT_H_INCLUDED 196 | # ifndef PRINTF_INT64_MODIFIER 197 | # define PRINTF_INT64_MODIFIER "ll" 198 | # endif 199 | # ifndef PRINTF_INT32_MODIFIER 200 | # define PRINTF_INT32_MODIFIER "l" 201 | # endif 202 | # ifndef PRINTF_INT16_MODIFIER 203 | # define PRINTF_INT16_MODIFIER "h" 204 | # endif 205 | # ifndef PRINTF_INTMAX_MODIFIER 206 | # define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER 207 | # endif 208 | # ifndef PRINTF_INT64_HEX_WIDTH 209 | # define PRINTF_INT64_HEX_WIDTH "16" 210 | # endif 211 | # ifndef PRINTF_INT32_HEX_WIDTH 212 | # define PRINTF_INT32_HEX_WIDTH "8" 213 | # endif 214 | # ifndef PRINTF_INT16_HEX_WIDTH 215 | # define PRINTF_INT16_HEX_WIDTH "4" 216 | # endif 217 | # ifndef PRINTF_INT8_HEX_WIDTH 218 | # define PRINTF_INT8_HEX_WIDTH "2" 219 | # endif 220 | # ifndef PRINTF_INT64_DEC_WIDTH 221 | # define PRINTF_INT64_DEC_WIDTH "20" 222 | # endif 223 | # ifndef PRINTF_INT32_DEC_WIDTH 224 | # define PRINTF_INT32_DEC_WIDTH "10" 225 | # endif 226 | # ifndef PRINTF_INT16_DEC_WIDTH 227 | # define PRINTF_INT16_DEC_WIDTH "5" 228 | # endif 229 | # ifndef PRINTF_INT8_DEC_WIDTH 230 | # define PRINTF_INT8_DEC_WIDTH "3" 231 | # endif 232 | # ifndef PRINTF_INTMAX_HEX_WIDTH 233 | # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH 234 | # endif 235 | # ifndef PRINTF_INTMAX_DEC_WIDTH 236 | # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH 237 | # endif 238 | 239 | /* 240 | * Something really weird is going on with Open Watcom. Just pull some of 241 | * these duplicated definitions from Open Watcom's stdint.h file for now. 242 | */ 243 | 244 | # if defined (__WATCOMC__) && __WATCOMC__ >= 1250 245 | # if !defined (INT64_C) 246 | # define INT64_C(x) (x + (INT64_MAX - INT64_MAX)) 247 | # endif 248 | # if !defined (UINT64_C) 249 | # define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX)) 250 | # endif 251 | # if !defined (INT32_C) 252 | # define INT32_C(x) (x + (INT32_MAX - INT32_MAX)) 253 | # endif 254 | # if !defined (UINT32_C) 255 | # define UINT32_C(x) (x + (UINT32_MAX - UINT32_MAX)) 256 | # endif 257 | # if !defined (INT16_C) 258 | # define INT16_C(x) (x) 259 | # endif 260 | # if !defined (UINT16_C) 261 | # define UINT16_C(x) (x) 262 | # endif 263 | # if !defined (INT8_C) 264 | # define INT8_C(x) (x) 265 | # endif 266 | # if !defined (UINT8_C) 267 | # define UINT8_C(x) (x) 268 | # endif 269 | # if !defined (UINT64_MAX) 270 | # define UINT64_MAX 18446744073709551615ULL 271 | # endif 272 | # if !defined (INT64_MAX) 273 | # define INT64_MAX 9223372036854775807LL 274 | # endif 275 | # if !defined (UINT32_MAX) 276 | # define UINT32_MAX 4294967295UL 277 | # endif 278 | # if !defined (INT32_MAX) 279 | # define INT32_MAX 2147483647L 280 | # endif 281 | # if !defined (INTMAX_MAX) 282 | # define INTMAX_MAX INT64_MAX 283 | # endif 284 | # if !defined (INTMAX_MIN) 285 | # define INTMAX_MIN INT64_MIN 286 | # endif 287 | # endif 288 | #endif 289 | 290 | #ifndef _PSTDINT_H_INCLUDED 291 | #define _PSTDINT_H_INCLUDED 292 | 293 | #ifndef SIZE_MAX 294 | # define SIZE_MAX (~(size_t)0) 295 | #endif 296 | 297 | /* 298 | * Deduce the type assignments from limits.h under the assumption that 299 | * integer sizes in bits are powers of 2, and follow the ANSI 300 | * definitions. 301 | */ 302 | 303 | #ifndef UINT8_MAX 304 | # define UINT8_MAX 0xff 305 | #endif 306 | #ifndef uint8_t 307 | # if (UCHAR_MAX == UINT8_MAX) || defined (S_SPLINT_S) 308 | typedef unsigned char uint8_t; 309 | # define UINT8_C(v) ((uint8_t) v) 310 | # else 311 | # error "Platform not supported" 312 | # endif 313 | #endif 314 | 315 | #ifndef INT8_MAX 316 | # define INT8_MAX 0x7f 317 | #endif 318 | #ifndef INT8_MIN 319 | # define INT8_MIN INT8_C(0x80) 320 | #endif 321 | #ifndef int8_t 322 | # if (SCHAR_MAX == INT8_MAX) || defined (S_SPLINT_S) 323 | typedef signed char int8_t; 324 | # define INT8_C(v) ((int8_t) v) 325 | # else 326 | # error "Platform not supported" 327 | # endif 328 | #endif 329 | 330 | #ifndef UINT16_MAX 331 | # define UINT16_MAX 0xffff 332 | #endif 333 | #ifndef uint16_t 334 | #if (UINT_MAX == UINT16_MAX) || defined (S_SPLINT_S) 335 | typedef unsigned int uint16_t; 336 | # ifndef PRINTF_INT16_MODIFIER 337 | # define PRINTF_INT16_MODIFIER "" 338 | # endif 339 | # define UINT16_C(v) ((uint16_t) (v)) 340 | #elif (USHRT_MAX == UINT16_MAX) 341 | typedef unsigned short uint16_t; 342 | # define UINT16_C(v) ((uint16_t) (v)) 343 | # ifndef PRINTF_INT16_MODIFIER 344 | # define PRINTF_INT16_MODIFIER "h" 345 | # endif 346 | #else 347 | #error "Platform not supported" 348 | #endif 349 | #endif 350 | 351 | #ifndef INT16_MAX 352 | # define INT16_MAX 0x7fff 353 | #endif 354 | #ifndef INT16_MIN 355 | # define INT16_MIN INT16_C(0x8000) 356 | #endif 357 | #ifndef int16_t 358 | #if (INT_MAX == INT16_MAX) || defined (S_SPLINT_S) 359 | typedef signed int int16_t; 360 | # define INT16_C(v) ((int16_t) (v)) 361 | # ifndef PRINTF_INT16_MODIFIER 362 | # define PRINTF_INT16_MODIFIER "" 363 | # endif 364 | #elif (SHRT_MAX == INT16_MAX) 365 | typedef signed short int16_t; 366 | # define INT16_C(v) ((int16_t) (v)) 367 | # ifndef PRINTF_INT16_MODIFIER 368 | # define PRINTF_INT16_MODIFIER "h" 369 | # endif 370 | #else 371 | #error "Platform not supported" 372 | #endif 373 | #endif 374 | 375 | #ifndef UINT32_MAX 376 | # define UINT32_MAX (0xffffffffUL) 377 | #endif 378 | #ifndef uint32_t 379 | #if (ULONG_MAX == UINT32_MAX) || defined (S_SPLINT_S) 380 | typedef unsigned long uint32_t; 381 | # define UINT32_C(v) v ## UL 382 | # ifndef PRINTF_INT32_MODIFIER 383 | # define PRINTF_INT32_MODIFIER "l" 384 | # endif 385 | #elif (UINT_MAX == UINT32_MAX) 386 | typedef unsigned int uint32_t; 387 | # ifndef PRINTF_INT32_MODIFIER 388 | # define PRINTF_INT32_MODIFIER "" 389 | # endif 390 | # define UINT32_C(v) v ## U 391 | #elif (USHRT_MAX == UINT32_MAX) 392 | typedef unsigned short uint32_t; 393 | # define UINT32_C(v) ((unsigned short) (v)) 394 | # ifndef PRINTF_INT32_MODIFIER 395 | # define PRINTF_INT32_MODIFIER "" 396 | # endif 397 | #else 398 | #error "Platform not supported" 399 | #endif 400 | #endif 401 | 402 | #ifndef INT32_MAX 403 | # define INT32_MAX (0x7fffffffL) 404 | #endif 405 | #ifndef INT32_MIN 406 | # define INT32_MIN INT32_C(0x80000000) 407 | #endif 408 | #ifndef int32_t 409 | #if (LONG_MAX == INT32_MAX) || defined (S_SPLINT_S) 410 | typedef signed long int32_t; 411 | # define INT32_C(v) v ## L 412 | # ifndef PRINTF_INT32_MODIFIER 413 | # define PRINTF_INT32_MODIFIER "l" 414 | # endif 415 | #elif (INT_MAX == INT32_MAX) 416 | typedef signed int int32_t; 417 | # define INT32_C(v) v 418 | # ifndef PRINTF_INT32_MODIFIER 419 | # define PRINTF_INT32_MODIFIER "" 420 | # endif 421 | #elif (SHRT_MAX == INT32_MAX) 422 | typedef signed short int32_t; 423 | # define INT32_C(v) ((short) (v)) 424 | # ifndef PRINTF_INT32_MODIFIER 425 | # define PRINTF_INT32_MODIFIER "" 426 | # endif 427 | #else 428 | #error "Platform not supported" 429 | #endif 430 | #endif 431 | 432 | /* 433 | * The macro stdint_int64_defined is temporarily used to record 434 | * whether or not 64 integer support is available. It must be 435 | * defined for any 64 integer extensions for new platforms that are 436 | * added. 437 | */ 438 | 439 | #undef stdint_int64_defined 440 | #if (defined(__STDC__) && defined(__STDC_VERSION__)) || defined (S_SPLINT_S) 441 | # if (__STDC__ && __STDC_VERSION >= 199901L) || defined (S_SPLINT_S) 442 | # define stdint_int64_defined 443 | typedef long long int64_t; 444 | typedef unsigned long long uint64_t; 445 | # define UINT64_C(v) v ## ULL 446 | # define INT64_C(v) v ## LL 447 | # ifndef PRINTF_INT64_MODIFIER 448 | # define PRINTF_INT64_MODIFIER "ll" 449 | # endif 450 | # endif 451 | #endif 452 | 453 | #if !defined (stdint_int64_defined) 454 | # if defined(__GNUC__) 455 | # define stdint_int64_defined 456 | __extension__ typedef long long int64_t; 457 | __extension__ typedef unsigned long long uint64_t; 458 | # define UINT64_C(v) v ## ULL 459 | # define INT64_C(v) v ## LL 460 | # ifndef PRINTF_INT64_MODIFIER 461 | # define PRINTF_INT64_MODIFIER "ll" 462 | # endif 463 | # elif defined(__MWERKS__) || defined (__SUNPRO_C) || defined (__SUNPRO_CC) || defined (__APPLE_CC__) || defined (_LONG_LONG) || defined (_CRAYC) || defined (S_SPLINT_S) 464 | # define stdint_int64_defined 465 | typedef long long int64_t; 466 | typedef unsigned long long uint64_t; 467 | # define UINT64_C(v) v ## ULL 468 | # define INT64_C(v) v ## LL 469 | # ifndef PRINTF_INT64_MODIFIER 470 | # define PRINTF_INT64_MODIFIER "ll" 471 | # endif 472 | # elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined (__BORLANDC__) && __BORLANDC__ > 0x460) || defined (__alpha) || defined (__DECC) 473 | # define stdint_int64_defined 474 | typedef __int64 int64_t; 475 | typedef unsigned __int64 uint64_t; 476 | # define UINT64_C(v) v ## UI64 477 | # define INT64_C(v) v ## I64 478 | # ifndef PRINTF_INT64_MODIFIER 479 | # define PRINTF_INT64_MODIFIER "I64" 480 | # endif 481 | # endif 482 | #endif 483 | 484 | #if !defined (LONG_LONG_MAX) && defined (INT64_C) 485 | # define LONG_LONG_MAX INT64_C (9223372036854775807) 486 | #endif 487 | #ifndef ULONG_LONG_MAX 488 | # define ULONG_LONG_MAX UINT64_C (18446744073709551615) 489 | #endif 490 | 491 | #if !defined (INT64_MAX) && defined (INT64_C) 492 | # define INT64_MAX INT64_C (9223372036854775807) 493 | #endif 494 | #if !defined (INT64_MIN) && defined (INT64_C) 495 | # define INT64_MIN INT64_C (-9223372036854775808) 496 | #endif 497 | #if !defined (UINT64_MAX) && defined (INT64_C) 498 | # define UINT64_MAX UINT64_C (18446744073709551615) 499 | #endif 500 | 501 | /* 502 | * Width of hexadecimal for number field. 503 | */ 504 | 505 | #ifndef PRINTF_INT64_HEX_WIDTH 506 | # define PRINTF_INT64_HEX_WIDTH "16" 507 | #endif 508 | #ifndef PRINTF_INT32_HEX_WIDTH 509 | # define PRINTF_INT32_HEX_WIDTH "8" 510 | #endif 511 | #ifndef PRINTF_INT16_HEX_WIDTH 512 | # define PRINTF_INT16_HEX_WIDTH "4" 513 | #endif 514 | #ifndef PRINTF_INT8_HEX_WIDTH 515 | # define PRINTF_INT8_HEX_WIDTH "2" 516 | #endif 517 | 518 | #ifndef PRINTF_INT64_DEC_WIDTH 519 | # define PRINTF_INT64_DEC_WIDTH "20" 520 | #endif 521 | #ifndef PRINTF_INT32_DEC_WIDTH 522 | # define PRINTF_INT32_DEC_WIDTH "10" 523 | #endif 524 | #ifndef PRINTF_INT16_DEC_WIDTH 525 | # define PRINTF_INT16_DEC_WIDTH "5" 526 | #endif 527 | #ifndef PRINTF_INT8_DEC_WIDTH 528 | # define PRINTF_INT8_DEC_WIDTH "3" 529 | #endif 530 | 531 | /* 532 | * Ok, lets not worry about 128 bit integers for now. Moore's law says 533 | * we don't need to worry about that until about 2040 at which point 534 | * we'll have bigger things to worry about. 535 | */ 536 | 537 | #ifdef stdint_int64_defined 538 | typedef int64_t intmax_t; 539 | typedef uint64_t uintmax_t; 540 | # define INTMAX_MAX INT64_MAX 541 | # define INTMAX_MIN INT64_MIN 542 | # define UINTMAX_MAX UINT64_MAX 543 | # define UINTMAX_C(v) UINT64_C(v) 544 | # define INTMAX_C(v) INT64_C(v) 545 | # ifndef PRINTF_INTMAX_MODIFIER 546 | # define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER 547 | # endif 548 | # ifndef PRINTF_INTMAX_HEX_WIDTH 549 | # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH 550 | # endif 551 | # ifndef PRINTF_INTMAX_DEC_WIDTH 552 | # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH 553 | # endif 554 | #else 555 | typedef int32_t intmax_t; 556 | typedef uint32_t uintmax_t; 557 | # define INTMAX_MAX INT32_MAX 558 | # define UINTMAX_MAX UINT32_MAX 559 | # define UINTMAX_C(v) UINT32_C(v) 560 | # define INTMAX_C(v) INT32_C(v) 561 | # ifndef PRINTF_INTMAX_MODIFIER 562 | # define PRINTF_INTMAX_MODIFIER PRINTF_INT32_MODIFIER 563 | # endif 564 | # ifndef PRINTF_INTMAX_HEX_WIDTH 565 | # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT32_HEX_WIDTH 566 | # endif 567 | # ifndef PRINTF_INTMAX_DEC_WIDTH 568 | # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT32_DEC_WIDTH 569 | # endif 570 | #endif 571 | 572 | /* 573 | * Because this file currently only supports platforms which have 574 | * precise powers of 2 as bit sizes for the default integers, the 575 | * least definitions are all trivial. Its possible that a future 576 | * version of this file could have different definitions. 577 | */ 578 | 579 | #ifndef stdint_least_defined 580 | typedef int8_t int_least8_t; 581 | typedef uint8_t uint_least8_t; 582 | typedef int16_t int_least16_t; 583 | typedef uint16_t uint_least16_t; 584 | typedef int32_t int_least32_t; 585 | typedef uint32_t uint_least32_t; 586 | # define PRINTF_LEAST32_MODIFIER PRINTF_INT32_MODIFIER 587 | # define PRINTF_LEAST16_MODIFIER PRINTF_INT16_MODIFIER 588 | # define UINT_LEAST8_MAX UINT8_MAX 589 | # define INT_LEAST8_MAX INT8_MAX 590 | # define UINT_LEAST16_MAX UINT16_MAX 591 | # define INT_LEAST16_MAX INT16_MAX 592 | # define UINT_LEAST32_MAX UINT32_MAX 593 | # define INT_LEAST32_MAX INT32_MAX 594 | # define INT_LEAST8_MIN INT8_MIN 595 | # define INT_LEAST16_MIN INT16_MIN 596 | # define INT_LEAST32_MIN INT32_MIN 597 | # ifdef stdint_int64_defined 598 | typedef int64_t int_least64_t; 599 | typedef uint64_t uint_least64_t; 600 | # define PRINTF_LEAST64_MODIFIER PRINTF_INT64_MODIFIER 601 | # define UINT_LEAST64_MAX UINT64_MAX 602 | # define INT_LEAST64_MAX INT64_MAX 603 | # define INT_LEAST64_MIN INT64_MIN 604 | # endif 605 | #endif 606 | #undef stdint_least_defined 607 | 608 | /* 609 | * The ANSI C committee pretending to know or specify anything about 610 | * performance is the epitome of misguided arrogance. The mandate of 611 | * this file is to *ONLY* ever support that absolute minimum 612 | * definition of the fast integer types, for compatibility purposes. 613 | * No extensions, and no attempt to suggest what may or may not be a 614 | * faster integer type will ever be made in this file. Developers are 615 | * warned to stay away from these types when using this or any other 616 | * stdint.h. 617 | */ 618 | 619 | typedef int_least8_t int_fast8_t; 620 | typedef uint_least8_t uint_fast8_t; 621 | typedef int_least16_t int_fast16_t; 622 | typedef uint_least16_t uint_fast16_t; 623 | typedef int_least32_t int_fast32_t; 624 | typedef uint_least32_t uint_fast32_t; 625 | #define UINT_FAST8_MAX UINT_LEAST8_MAX 626 | #define INT_FAST8_MAX INT_LEAST8_MAX 627 | #define UINT_FAST16_MAX UINT_LEAST16_MAX 628 | #define INT_FAST16_MAX INT_LEAST16_MAX 629 | #define UINT_FAST32_MAX UINT_LEAST32_MAX 630 | #define INT_FAST32_MAX INT_LEAST32_MAX 631 | #define INT_FAST8_MIN INT_LEAST8_MIN 632 | #define INT_FAST16_MIN INT_LEAST16_MIN 633 | #define INT_FAST32_MIN INT_LEAST32_MIN 634 | #ifdef stdint_int64_defined 635 | typedef int_least64_t int_fast64_t; 636 | typedef uint_least64_t uint_fast64_t; 637 | # define UINT_FAST64_MAX UINT_LEAST64_MAX 638 | # define INT_FAST64_MAX INT_LEAST64_MAX 639 | # define INT_FAST64_MIN INT_LEAST64_MIN 640 | #endif 641 | 642 | #undef stdint_int64_defined 643 | 644 | /* 645 | * Whatever piecemeal, per compiler thing we can do about the wchar_t 646 | * type limits. 647 | */ 648 | 649 | #if defined(__WATCOMC__) || defined(_MSC_VER) || defined (__GNUC__) 650 | # include 651 | # ifndef WCHAR_MIN 652 | # define WCHAR_MIN 0 653 | # endif 654 | # ifndef WCHAR_MAX 655 | # define WCHAR_MAX ((wchar_t)-1) 656 | # endif 657 | #endif 658 | 659 | /* 660 | * Whatever piecemeal, per compiler/platform thing we can do about the 661 | * (u)intptr_t types and limits. 662 | */ 663 | 664 | #if defined (_MSC_VER) && defined (_UINTPTR_T_DEFINED) 665 | # define STDINT_H_UINTPTR_T_DEFINED 666 | #endif 667 | 668 | #ifndef STDINT_H_UINTPTR_T_DEFINED 669 | # if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (_WIN64) 670 | # define stdint_intptr_bits 64 671 | # elif defined (__WATCOMC__) || defined (__TURBOC__) 672 | # if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__) 673 | # define stdint_intptr_bits 16 674 | # else 675 | # define stdint_intptr_bits 32 676 | # endif 677 | # elif defined (__i386__) || defined (_WIN32) || defined (WIN32) 678 | # define stdint_intptr_bits 32 679 | # elif defined (__INTEL_COMPILER) 680 | /* TODO -- what will Intel do about x86-64? */ 681 | # endif 682 | 683 | # ifdef stdint_intptr_bits 684 | # define stdint_intptr_glue3_i(a,b,c) a##b##c 685 | # define stdint_intptr_glue3(a,b,c) stdint_intptr_glue3_i(a,b,c) 686 | # ifndef PRINTF_INTPTR_MODIFIER 687 | # define PRINTF_INTPTR_MODIFIER stdint_intptr_glue3(PRINTF_INT,stdint_intptr_bits,_MODIFIER) 688 | # endif 689 | # ifndef PTRDIFF_MAX 690 | # define PTRDIFF_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX) 691 | # endif 692 | # ifndef PTRDIFF_MIN 693 | # define PTRDIFF_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN) 694 | # endif 695 | # ifndef UINTPTR_MAX 696 | # define UINTPTR_MAX stdint_intptr_glue3(UINT,stdint_intptr_bits,_MAX) 697 | # endif 698 | # ifndef INTPTR_MAX 699 | # define INTPTR_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX) 700 | # endif 701 | # ifndef INTPTR_MIN 702 | # define INTPTR_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN) 703 | # endif 704 | # ifndef INTPTR_C 705 | # define INTPTR_C(x) stdint_intptr_glue3(INT,stdint_intptr_bits,_C)(x) 706 | # endif 707 | # ifndef UINTPTR_C 708 | # define UINTPTR_C(x) stdint_intptr_glue3(UINT,stdint_intptr_bits,_C)(x) 709 | # endif 710 | typedef stdint_intptr_glue3(uint,stdint_intptr_bits,_t) uintptr_t; 711 | typedef stdint_intptr_glue3( int,stdint_intptr_bits,_t) intptr_t; 712 | # else 713 | /* TODO -- This following is likely wrong for some platforms, and does 714 | nothing for the definition of uintptr_t. */ 715 | typedef ptrdiff_t intptr_t; 716 | # endif 717 | # define STDINT_H_UINTPTR_T_DEFINED 718 | #endif 719 | 720 | /* 721 | * Assumes sig_atomic_t is signed and we have a 2s complement machine. 722 | */ 723 | 724 | #ifndef SIG_ATOMIC_MAX 725 | # define SIG_ATOMIC_MAX ((((sig_atomic_t) 1) << (sizeof (sig_atomic_t)*CHAR_BIT-1)) - 1) 726 | #endif 727 | 728 | #endif 729 | 730 | #if defined (__TEST_PSTDINT_FOR_CORRECTNESS) 731 | 732 | /* 733 | * Please compile with the maximum warning settings to make sure macros are not 734 | * defined more than once. 735 | */ 736 | 737 | #include 738 | #include 739 | #include 740 | 741 | #define glue3_aux(x,y,z) x ## y ## z 742 | #define glue3(x,y,z) glue3_aux(x,y,z) 743 | 744 | #define DECLU(bits) glue3(uint,bits,_t) glue3(u,bits,=) glue3(UINT,bits,_C) (0); 745 | #define DECLI(bits) glue3(int,bits,_t) glue3(i,bits,=) glue3(INT,bits,_C) (0); 746 | 747 | #define DECL(us,bits) glue3(DECL,us,) (bits) 748 | 749 | #define TESTUMAX(bits) glue3(u,bits,=) glue3(~,u,bits); if (glue3(UINT,bits,_MAX) glue3(!=,u,bits)) printf ("Something wrong with UINT%d_MAX\n", bits) 750 | 751 | int main () { 752 | DECL(I,8) 753 | DECL(U,8) 754 | DECL(I,16) 755 | DECL(U,16) 756 | DECL(I,32) 757 | DECL(U,32) 758 | #ifdef INT64_MAX 759 | DECL(I,64) 760 | DECL(U,64) 761 | #endif 762 | intmax_t imax = INTMAX_C(0); 763 | uintmax_t umax = UINTMAX_C(0); 764 | char str0[256], str1[256]; 765 | 766 | sprintf (str0, "%d %x\n", 0, ~0); 767 | 768 | sprintf (str1, "%d %x\n", i8, ~0); 769 | if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1); 770 | sprintf (str1, "%u %x\n", u8, ~0); 771 | if (0 != strcmp (str0, str1)) printf ("Something wrong with u8 : %s\n", str1); 772 | sprintf (str1, "%d %x\n", i16, ~0); 773 | if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1); 774 | sprintf (str1, "%u %x\n", u16, ~0); 775 | if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1); 776 | sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0); 777 | if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1); 778 | sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0); 779 | if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1); 780 | #ifdef INT64_MAX 781 | sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0); 782 | if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1); 783 | #endif 784 | sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0); 785 | if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1); 786 | sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0); 787 | if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1); 788 | 789 | TESTUMAX(8); 790 | TESTUMAX(16); 791 | TESTUMAX(32); 792 | #ifdef INT64_MAX 793 | TESTUMAX(64); 794 | #endif 795 | 796 | return EXIT_SUCCESS; 797 | } 798 | 799 | #endif 800 | --------------------------------------------------------------------------------