+++ /dev/null
-/* \r
- * MinHook - The Minimalistic API Hooking Library for x64/x86\r
- * Copyright (c) 2009 Tsuda Kageyu. \r
- * All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-================================================================================\r
-Portions of this software are Copyright (c) 2008-2009, Vyacheslav Patkov.\r
-================================================================================\r
-/*\r
- * Hacker Disassembler Engine 32 C\r
- * Copyright (c) 2008-2009, Vyacheslav Patkov.\r
- * All rights reserved.\r
- *\r
- */\r
-\r
-/*\r
- * Hacker Disassembler Engine 64 C\r
- * Copyright (c) 2008-2009, Vyacheslav Patkov.\r
- * All rights reserved.\r
- *\r
- */\r
-\r
-================================================================================\r
-Portions of this software are Copyright (c) 2005-2007 Paul Hsieh.\r
-================================================================================\r
-/* A portable stdint.h\r
- ****************************************************************************\r
- * BSD License:\r
- ****************************************************************************\r
- *\r
- * Copyright (c) 2005-2007 Paul Hsieh\r
- * All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#pragma once\r
-\r
-#include <Windows.h>\r
-\r
-// MinHook Error Codes.\r
-typedef enum MH_STATUS\r
-{\r
- // Unknown error. Should not be returned.\r
- MH_UNKNOWN = -1,\r
-\r
- // Successful.\r
- MH_OK = 0,\r
-\r
- \r
- // MinHook is already initialized. \r
- MH_ERROR_ALREADY_INITIALIZED,\r
-\r
- // MinHook is not initialized yet, or already uninitialized.\r
- MH_ERROR_NOT_INITIALIZED,\r
-\r
- // The hook for the specified target function is already created. \r
- MH_ERROR_ALREADY_CREATED,\r
-\r
- // The hook for the specified target function is not created yet. \r
- MH_ERROR_NOT_CREATED,\r
-\r
- // The hook for the specified target function is already enabled.\r
- MH_ERROR_ENABLED,\r
-\r
- // The hook for the specified target function is not enabled yet, or already disabled.\r
- MH_ERROR_DISABLED,\r
- \r
- // The specified pointer is invalid. It points the address of non-allocated and/or non-executable region.\r
- MH_ERROR_NOT_EXECUTABLE,\r
-\r
- // The specified target function cannot be hooked. \r
- MH_ERROR_UNSUPPORTED_FUNCTION,\r
- \r
- // Failed to allocate memory.\r
- MH_ERROR_MEMORY_ALLOC,\r
-\r
- // Failed to change the memory protection.\r
- MH_ERROR_MEMORY_PROTECT\r
-}\r
-MH_STATUS;\r
-\r
-#if defined __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
- // Initialize the MinHook library.\r
- MH_STATUS WINAPI MH_Initialize();\r
-\r
- // Uninitialize the MinHook library.\r
- MH_STATUS WINAPI MH_Uninitialize();\r
-\r
- // Creates the Hook for the specified target function, in disabled state.\r
- // Parameters:\r
- // pTarget [in] A pointer to the target function, which will be overridden by the detour function.\r
- // pDetour [in] A pointer to the detour function, which will override the target function.\r
- // ppOriginal [out] A pointer to the trampoline function, which will be used to call the original target function. \r
- MH_STATUS WINAPI MH_CreateHook(void* pTarget, void* const pDetour, void** ppOriginal);\r
-\r
- // Enables the already created hook.\r
- // Parameters:\r
- // pTarget [in] A pointer to the target function.\r
- MH_STATUS WINAPI MH_EnableHook(void* pTarget);\r
-\r
- // Disables the already created hook.\r
- // Parameters:\r
- // pTarget [in] A pointer to the target function.\r
- MH_STATUS WINAPI MH_DisableHook(void* pTarget);\r
-\r
-#if defined __cplusplus\r
-}\r
-#endif\r
-\r
+++ /dev/null
-<?xml version="1.0" encoding="shift_jis"?>\r
-<VisualStudioProject\r
- ProjectType="Visual C++"\r
- Version="9.00"\r
- Name="libMinHook"\r
- ProjectGUID="{65021938-D251-46FA-BC3D-85C385D4C06D}"\r
- RootNamespace="libMinHook"\r
- Keyword="Win32Proj"\r
- TargetFrameworkVersion="196613"\r
- >\r
- <Platforms>\r
- <Platform\r
- Name="Win32"\r
- />\r
- <Platform\r
- Name="x64"\r
- />\r
- </Platforms>\r
- <ToolFiles>\r
- </ToolFiles>\r
- <Configurations>\r
- <Configuration\r
- Name="Debug|Win32"\r
- OutputDirectory="$(SolutionDir)$(ConfigurationName)"\r
- IntermediateDirectory="$(ConfigurationName)"\r
- ConfigurationType="4"\r
- CharacterSet="1"\r
- >\r
- <Tool\r
- Name="VCPreBuildEventTool"\r
- />\r
- <Tool\r
- Name="VCCustomBuildTool"\r
- />\r
- <Tool\r
- Name="VCXMLDataGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCWebServiceProxyGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCMIDLTool"\r
- />\r
- <Tool\r
- Name="VCCLCompilerTool"\r
- Optimization="0"\r
- PreprocessorDefinitions="WIN32;_DEBUG;_LIB"\r
- MinimalRebuild="true"\r
- BasicRuntimeChecks="3"\r
- RuntimeLibrary="3"\r
- UsePrecompiledHeader="0"\r
- WarningLevel="3"\r
- DebugInformationFormat="4"\r
- />\r
- <Tool\r
- Name="VCManagedResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCPreLinkEventTool"\r
- />\r
- <Tool\r
- Name="VCLibrarianTool"\r
- OutputFile="$(OutDir)\$(ProjectName).x86.lib"\r
- />\r
- <Tool\r
- Name="VCALinkTool"\r
- />\r
- <Tool\r
- Name="VCXDCMakeTool"\r
- />\r
- <Tool\r
- Name="VCBscMakeTool"\r
- />\r
- <Tool\r
- Name="VCFxCopTool"\r
- />\r
- <Tool\r
- Name="VCPostBuildEventTool"\r
- />\r
- </Configuration>\r
- <Configuration\r
- Name="Debug|x64"\r
- OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"\r
- IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"\r
- ConfigurationType="4"\r
- CharacterSet="1"\r
- >\r
- <Tool\r
- Name="VCPreBuildEventTool"\r
- />\r
- <Tool\r
- Name="VCCustomBuildTool"\r
- />\r
- <Tool\r
- Name="VCXMLDataGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCWebServiceProxyGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCMIDLTool"\r
- TargetEnvironment="3"\r
- />\r
- <Tool\r
- Name="VCCLCompilerTool"\r
- Optimization="0"\r
- PreprocessorDefinitions="WIN32;_DEBUG;_LIB"\r
- MinimalRebuild="true"\r
- BasicRuntimeChecks="3"\r
- RuntimeLibrary="3"\r
- UsePrecompiledHeader="0"\r
- WarningLevel="3"\r
- DebugInformationFormat="3"\r
- />\r
- <Tool\r
- Name="VCManagedResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCPreLinkEventTool"\r
- />\r
- <Tool\r
- Name="VCLibrarianTool"\r
- OutputFile="$(OutDir)\$(ProjectName).x64.lib"\r
- />\r
- <Tool\r
- Name="VCALinkTool"\r
- />\r
- <Tool\r
- Name="VCXDCMakeTool"\r
- />\r
- <Tool\r
- Name="VCBscMakeTool"\r
- />\r
- <Tool\r
- Name="VCFxCopTool"\r
- />\r
- <Tool\r
- Name="VCPostBuildEventTool"\r
- />\r
- </Configuration>\r
- <Configuration\r
- Name="Release|Win32"\r
- OutputDirectory="$(SolutionDir)$(ConfigurationName)"\r
- IntermediateDirectory="$(ConfigurationName)"\r
- ConfigurationType="4"\r
- CharacterSet="1"\r
- WholeProgramOptimization="1"\r
- >\r
- <Tool\r
- Name="VCPreBuildEventTool"\r
- />\r
- <Tool\r
- Name="VCCustomBuildTool"\r
- />\r
- <Tool\r
- Name="VCXMLDataGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCWebServiceProxyGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCMIDLTool"\r
- />\r
- <Tool\r
- Name="VCCLCompilerTool"\r
- Optimization="2"\r
- EnableIntrinsicFunctions="true"\r
- PreprocessorDefinitions="WIN32;NDEBUG;_LIB"\r
- RuntimeLibrary="2"\r
- EnableFunctionLevelLinking="true"\r
- UsePrecompiledHeader="0"\r
- WarningLevel="3"\r
- DebugInformationFormat="3"\r
- />\r
- <Tool\r
- Name="VCManagedResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCPreLinkEventTool"\r
- />\r
- <Tool\r
- Name="VCLibrarianTool"\r
- OutputFile="$(OutDir)\$(ProjectName).x86.lib"\r
- />\r
- <Tool\r
- Name="VCALinkTool"\r
- />\r
- <Tool\r
- Name="VCXDCMakeTool"\r
- />\r
- <Tool\r
- Name="VCBscMakeTool"\r
- />\r
- <Tool\r
- Name="VCFxCopTool"\r
- />\r
- <Tool\r
- Name="VCPostBuildEventTool"\r
- />\r
- </Configuration>\r
- <Configuration\r
- Name="Release|x64"\r
- OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"\r
- IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"\r
- ConfigurationType="4"\r
- CharacterSet="1"\r
- WholeProgramOptimization="1"\r
- >\r
- <Tool\r
- Name="VCPreBuildEventTool"\r
- />\r
- <Tool\r
- Name="VCCustomBuildTool"\r
- />\r
- <Tool\r
- Name="VCXMLDataGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCWebServiceProxyGeneratorTool"\r
- />\r
- <Tool\r
- Name="VCMIDLTool"\r
- TargetEnvironment="3"\r
- />\r
- <Tool\r
- Name="VCCLCompilerTool"\r
- Optimization="2"\r
- EnableIntrinsicFunctions="true"\r
- PreprocessorDefinitions="WIN32;NDEBUG;_LIB"\r
- RuntimeLibrary="2"\r
- EnableFunctionLevelLinking="true"\r
- UsePrecompiledHeader="0"\r
- WarningLevel="3"\r
- DebugInformationFormat="3"\r
- />\r
- <Tool\r
- Name="VCManagedResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCResourceCompilerTool"\r
- />\r
- <Tool\r
- Name="VCPreLinkEventTool"\r
- />\r
- <Tool\r
- Name="VCLibrarianTool"\r
- OutputFile="$(OutDir)\$(ProjectName).x64.lib"\r
- />\r
- <Tool\r
- Name="VCALinkTool"\r
- />\r
- <Tool\r
- Name="VCXDCMakeTool"\r
- />\r
- <Tool\r
- Name="VCBscMakeTool"\r
- />\r
- <Tool\r
- Name="VCFxCopTool"\r
- />\r
- <Tool\r
- Name="VCPostBuildEventTool"\r
- />\r
- </Configuration>\r
- </Configurations>\r
- <References>\r
- </References>\r
- <Files>\r
- <Filter\r
- Name="src"\r
- >\r
- <Filter\r
- Name="HDE32"\r
- >\r
- <File\r
- RelativePath=".\src\HDE32\hde32.c"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\HDE32\hde32.h"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\HDE32\table32.h"\r
- >\r
- </File>\r
- </Filter>\r
- <Filter\r
- Name="HDE64"\r
- >\r
- <File\r
- RelativePath=".\src\HDE64\src\hde64.c"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\HDE64\include\hde64.h"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\HDE64\src\table64.h"\r
- >\r
- </File>\r
- </Filter>\r
- <Filter\r
- Name="Source Files"\r
- >\r
- <File\r
- RelativePath=".\src\buffer.cpp"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\export.cpp"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\hook.cpp"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\thread.cpp"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\trampoline.cpp"\r
- >\r
- </File>\r
- </Filter>\r
- <Filter\r
- Name="Header Files"\r
- >\r
- <File\r
- RelativePath=".\src\buffer.h"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\hook.h"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\pstdint.h"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\thread.h"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\src\trampoline.h"\r
- >\r
- </File>\r
- </Filter>\r
- </Filter>\r
- <File\r
- RelativePath=".\COPYING.txt"\r
- >\r
- </File>\r
- <File\r
- RelativePath=".\MinHook.h"\r
- >\r
- </File>\r
- </Files>\r
- <Globals>\r
- </Globals>\r
-</VisualStudioProject>\r
+++ /dev/null
-/*
- * Hacker Disassembler Engine 32 C
- * Copyright (c) 2008-2009, Vyacheslav Patkov.
- * All rights reserved.
- *
- */
-
-/* #include <stdint.h> */
-#include <string.h>
-
-#include "hde32.h"
-#include "table32.h"
-
-unsigned int hde32_disasm(const void *code, hde32s *hs)
-{
- uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0;
- uint8_t *ht = hde32_table, m_mod, m_reg, m_rm, disp_size = 0;
-
- memset(hs,0,sizeof(hde32s));
-
- for (x = 16; x; x--)
- switch (c = *p++) {
- case 0xf3:
- hs->p_rep = c;
- pref |= PRE_F3;
- break;
- case 0xf2:
- hs->p_rep = c;
- pref |= PRE_F2;
- break;
- case 0xf0:
- hs->p_lock = c;
- pref |= PRE_LOCK;
- break;
- case 0x26: case 0x2e: case 0x36:
- case 0x3e: case 0x64: case 0x65:
- hs->p_seg = c;
- pref |= PRE_SEG;
- break;
- case 0x66:
- hs->p_66 = c;
- pref |= PRE_66;
- break;
- case 0x67:
- hs->p_67 = c;
- pref |= PRE_67;
- break;
- default:
- goto pref_done;
- }
- pref_done:
-
- hs->flags = (uint32_t)pref << 23;
-
- if (!pref)
- pref |= PRE_NONE;
-
- if ((hs->opcode = c) == 0x0f) {
- hs->opcode2 = c = *p++;
- ht += DELTA_OPCODES;
- } else if (c >= 0xa0 && c <= 0xa3) {
- if (pref & PRE_67)
- pref |= PRE_66;
- else
- pref &= ~PRE_66;
- }
-
- opcode = c;
- cflags = ht[ht[opcode / 4] + (opcode % 4)];
-
- if (cflags == C_ERROR) {
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
- cflags = 0;
- if ((opcode & -3) == 0x24)
- cflags++;
- }
-
- x = 0;
- if (cflags & C_GROUP) {
- uint16_t t;
- t = *(uint16_t *)(ht + (cflags & 0x7f));
- cflags = (uint8_t)t;
- x = (uint8_t)(t >> 8);
- }
-
- if (hs->opcode2) {
- ht = hde32_table + DELTA_PREFIXES;
- if (ht[ht[opcode / 4] + (opcode % 4)] & pref)
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
- }
-
- if (cflags & C_MODRM) {
- hs->flags |= F_MODRM;
- hs->modrm = c = *p++;
- hs->modrm_mod = m_mod = c >> 6;
- hs->modrm_rm = m_rm = c & 7;
- hs->modrm_reg = m_reg = (c & 0x3f) >> 3;
-
- if (x && ((x << m_reg) & 0x80))
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
-
- if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) {
- uint8_t t = opcode - 0xd9;
- if (m_mod == 3) {
- ht = hde32_table + DELTA_FPU_MODRM + t*8;
- t = ht[m_reg] << m_rm;
- } else {
- ht = hde32_table + DELTA_FPU_REG;
- t = ht[t] << m_reg;
- }
- if (t & 0x80)
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
- }
-
- if (pref & PRE_LOCK) {
- if (m_mod == 3) {
- hs->flags |= F_ERROR | F_ERROR_LOCK;
- } else {
- uint8_t *table_end, op = opcode;
- if (hs->opcode2) {
- ht = hde32_table + DELTA_OP2_LOCK_OK;
- table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK;
- } else {
- ht = hde32_table + DELTA_OP_LOCK_OK;
- table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK;
- op &= -2;
- }
- for (; ht != table_end; ht++)
- if (*ht++ == op) {
- if (!((*ht << m_reg) & 0x80))
- goto no_lock_error;
- else
- break;
- }
- hs->flags |= F_ERROR | F_ERROR_LOCK;
- no_lock_error:
- ;
- }
- }
-
- if (hs->opcode2) {
- switch (opcode) {
- case 0x20: case 0x22:
- m_mod = 3;
- if (m_reg > 4 || m_reg == 1)
- goto error_operand;
- else
- goto no_error_operand;
- case 0x21: case 0x23:
- m_mod = 3;
- if (m_reg == 4 || m_reg == 5)
- goto error_operand;
- else
- goto no_error_operand;
- }
- } else {
- switch (opcode) {
- case 0x8c:
- if (m_reg > 5)
- goto error_operand;
- else
- goto no_error_operand;
- case 0x8e:
- if (m_reg == 1 || m_reg > 5)
- goto error_operand;
- else
- goto no_error_operand;
- }
- }
-
- if (m_mod == 3) {
- uint8_t *table_end;
- if (hs->opcode2) {
- ht = hde32_table + DELTA_OP2_ONLY_MEM;
- table_end = ht + sizeof(hde32_table) - DELTA_OP2_ONLY_MEM;
- } else {
- ht = hde32_table + DELTA_OP_ONLY_MEM;
- table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM;
- }
- for (; ht != table_end; ht += 2)
- if (*ht++ == opcode) {
- if (*ht++ & pref && !((*ht << m_reg) & 0x80))
- goto error_operand;
- else
- break;
- }
- goto no_error_operand;
- } else if (hs->opcode2) {
- switch (opcode) {
- case 0x50: case 0xd7: case 0xf7:
- if (pref & (PRE_NONE | PRE_66))
- goto error_operand;
- break;
- case 0xd6:
- if (pref & (PRE_F2 | PRE_F3))
- goto error_operand;
- break;
- case 0xc5:
- goto error_operand;
- }
- goto no_error_operand;
- } else
- goto no_error_operand;
-
- error_operand:
- hs->flags |= F_ERROR | F_ERROR_OPERAND;
- no_error_operand:
-
- c = *p++;
- if (m_reg <= 1) {
- if (opcode == 0xf6)
- cflags |= C_IMM8;
- else if (opcode == 0xf7)
- cflags |= C_IMM_P66;
- }
-
- switch (m_mod) {
- case 0:
- if (pref & PRE_67) {
- if (m_rm == 6)
- disp_size = 2;
- } else
- if (m_rm == 5)
- disp_size = 4;
- break;
- case 1:
- disp_size = 1;
- break;
- case 2:
- disp_size = 2;
- if (!(pref & PRE_67))
- disp_size <<= 1;
- }
-
- if (m_mod != 3 && m_rm == 4 && !(pref & PRE_67)) {
- hs->flags |= F_SIB;
- p++;
- hs->sib = c;
- hs->sib_scale = c >> 6;
- hs->sib_index = (c & 0x3f) >> 3;
- if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1))
- disp_size = 4;
- }
-
- p--;
- switch (disp_size) {
- case 1:
- hs->flags |= F_DISP8;
- hs->disp.disp8 = *p;
- break;
- case 2:
- hs->flags |= F_DISP16;
- hs->disp.disp16 = *(uint16_t *)p;
- break;
- case 4:
- hs->flags |= F_DISP32;
- hs->disp.disp32 = *(uint32_t *)p;
- }
- p += disp_size;
- } else if (pref & PRE_LOCK)
- hs->flags |= F_ERROR | F_ERROR_LOCK;
-
- if (cflags & C_IMM_P66) {
- if (cflags & C_REL32) {
- if (pref & PRE_66) {
- hs->flags |= F_IMM16 | F_RELATIVE;
- hs->imm.imm16 = *(uint16_t *)p;
- p += 2;
- goto disasm_done;
- }
- goto rel32_ok;
- }
- if (pref & PRE_66) {
- hs->flags |= F_IMM16;
- hs->imm.imm16 = *(uint16_t *)p;
- p += 2;
- } else {
- hs->flags |= F_IMM32;
- hs->imm.imm32 = *(uint32_t *)p;
- p += 4;
- }
- }
-
- if (cflags & C_IMM16) {
- if (hs->flags & F_IMM32) {
- hs->flags |= F_IMM16;
- hs->disp.disp16 = *(uint16_t *)p;
- } else if (hs->flags & F_IMM16) {
- hs->flags |= F_2IMM16;
- hs->disp.disp16 = *(uint16_t *)p;
- } else {
- hs->flags |= F_IMM16;
- hs->imm.imm16 = *(uint16_t *)p;
- }
- p += 2;
- }
- if (cflags & C_IMM8) {
- hs->flags |= F_IMM8;
- hs->imm.imm8 = *p++;
- }
-
- if (cflags & C_REL32) {
- rel32_ok:
- hs->flags |= F_IMM32 | F_RELATIVE;
- hs->imm.imm32 = *(uint32_t *)p;
- p += 4;
- } else if (cflags & C_REL8) {
- hs->flags |= F_IMM8 | F_RELATIVE;
- hs->imm.imm8 = *p++;
- }
-
- disasm_done:
-
- if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) {
- hs->flags |= F_ERROR | F_ERROR_LENGTH;
- hs->len = 15;
- }
-
- return (unsigned int)hs->len;
-}
+++ /dev/null
-/*
- * Hacker Disassembler Engine 32
- * Copyright (c) 2006-2009, Vyacheslav Patkov.
- * All rights reserved.
- *
- * hde32.h: C/C++ header file
- *
- */
-
-#ifndef _HDE32_H_
-#define _HDE32_H_
-
-/* stdint.h - C99 standard header
- * http://en.wikipedia.org/wiki/stdint.h
- *
- * if your compiler doesn't contain "stdint.h" header (for
- * example, Microsoft Visual C++), you can download file:
- * http://www.azillionmonkeys.com/qed/pstdint.h
- * and change next line to:
- * #include "pstdint.h"
- */
-/* #include <stdint.h> */
-#include "../pstdint.h"
-
-#define F_MODRM 0x00000001
-#define F_SIB 0x00000002
-#define F_IMM8 0x00000004
-#define F_IMM16 0x00000008
-#define F_IMM32 0x00000010
-#define F_DISP8 0x00000020
-#define F_DISP16 0x00000040
-#define F_DISP32 0x00000080
-#define F_RELATIVE 0x00000100
-#define F_2IMM16 0x00000800
-#define F_ERROR 0x00001000
-#define F_ERROR_OPCODE 0x00002000
-#define F_ERROR_LENGTH 0x00004000
-#define F_ERROR_LOCK 0x00008000
-#define F_ERROR_OPERAND 0x00010000
-#define F_PREFIX_REPNZ 0x01000000
-#define F_PREFIX_REPX 0x02000000
-#define F_PREFIX_REP 0x03000000
-#define F_PREFIX_66 0x04000000
-#define F_PREFIX_67 0x08000000
-#define F_PREFIX_LOCK 0x10000000
-#define F_PREFIX_SEG 0x20000000
-#define F_PREFIX_ANY 0x3f000000
-
-#define PREFIX_SEGMENT_CS 0x2e
-#define PREFIX_SEGMENT_SS 0x36
-#define PREFIX_SEGMENT_DS 0x3e
-#define PREFIX_SEGMENT_ES 0x26
-#define PREFIX_SEGMENT_FS 0x64
-#define PREFIX_SEGMENT_GS 0x65
-#define PREFIX_LOCK 0xf0
-#define PREFIX_REPNZ 0xf2
-#define PREFIX_REPX 0xf3
-#define PREFIX_OPERAND_SIZE 0x66
-#define PREFIX_ADDRESS_SIZE 0x67
-
-#pragma pack(push,1)
-
-typedef struct {
- uint8_t len;
- uint8_t p_rep;
- uint8_t p_lock;
- uint8_t p_seg;
- uint8_t p_66;
- uint8_t p_67;
- uint8_t opcode;
- uint8_t opcode2;
- uint8_t modrm;
- uint8_t modrm_mod;
- uint8_t modrm_reg;
- uint8_t modrm_rm;
- uint8_t sib;
- uint8_t sib_scale;
- uint8_t sib_index;
- uint8_t sib_base;
- union {
- uint8_t imm8;
- uint16_t imm16;
- uint32_t imm32;
- } imm;
- union {
- uint8_t disp8;
- uint16_t disp16;
- uint32_t disp32;
- } disp;
- uint32_t flags;
-} hde32s;
-
-#pragma pack(pop)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* __cdecl */
-unsigned int hde32_disasm(const void *code, hde32s *hs);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _HDE32_H_ */
+++ /dev/null
-/*
- * Hacker Disassembler Engine 32 C
- * Copyright (c) 2008-2009, Vyacheslav Patkov.
- * All rights reserved.
- *
- */
-
-#define C_NONE 0x00
-#define C_MODRM 0x01
-#define C_IMM8 0x02
-#define C_IMM16 0x04
-#define C_IMM_P66 0x10
-#define C_REL8 0x20
-#define C_REL32 0x40
-#define C_GROUP 0x80
-#define C_ERROR 0xff
-
-#define PRE_ANY 0x00
-#define PRE_NONE 0x01
-#define PRE_F2 0x02
-#define PRE_F3 0x04
-#define PRE_66 0x08
-#define PRE_67 0x10
-#define PRE_LOCK 0x20
-#define PRE_SEG 0x40
-#define PRE_ALL 0xff
-
-#define DELTA_OPCODES 0x4a
-#define DELTA_FPU_REG 0xf1
-#define DELTA_FPU_MODRM 0xf8
-#define DELTA_PREFIXES 0x130
-#define DELTA_OP_LOCK_OK 0x1a1
-#define DELTA_OP2_LOCK_OK 0x1b9
-#define DELTA_OP_ONLY_MEM 0x1cb
-#define DELTA_OP2_ONLY_MEM 0x1da
-
-unsigned char hde32_table[] = {
- 0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,
- 0xa8,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xac,0xaa,0xb2,0xaa,0x9f,0x9f,
- 0x9f,0x9f,0xb5,0xa3,0xa3,0xa4,0xaa,0xaa,0xba,0xaa,0x96,0xaa,0xa8,0xaa,0xc3,
- 0xc3,0x96,0x96,0xb7,0xae,0xd6,0xbd,0xa3,0xc5,0xa3,0xa3,0x9f,0xc3,0x9c,0xaa,
- 0xaa,0xac,0xaa,0xbf,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0x90,
- 0x82,0x7d,0x97,0x59,0x59,0x59,0x59,0x59,0x7f,0x59,0x59,0x60,0x7d,0x7f,0x7f,
- 0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x9a,0x88,0x7d,
- 0x59,0x50,0x50,0x50,0x50,0x59,0x59,0x59,0x59,0x61,0x94,0x61,0x9e,0x59,0x59,
- 0x85,0x59,0x92,0xa3,0x60,0x60,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,
- 0x59,0x59,0x9f,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xcc,0x01,0xbc,0x03,0xf0,
- 0x10,0x10,0x10,0x10,0x50,0x50,0x50,0x50,0x14,0x20,0x20,0x20,0x20,0x01,0x01,
- 0x01,0x01,0xc4,0x02,0x10,0x00,0x00,0x00,0x00,0x01,0x01,0xc0,0xc2,0x10,0x11,
- 0x02,0x03,0x11,0x03,0x03,0x04,0x00,0x00,0x14,0x00,0x02,0x00,0x00,0xc6,0xc8,
- 0x02,0x02,0x02,0x02,0x00,0x00,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xca,
- 0x01,0x01,0x01,0x00,0x06,0x00,0x04,0x00,0xc0,0xc2,0x01,0x01,0x03,0x01,0xff,
- 0xff,0x01,0x00,0x03,0xc4,0xc4,0xc6,0x03,0x01,0x01,0x01,0xff,0x03,0x03,0x03,
- 0xc8,0x40,0x00,0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,
- 0x00,0x00,0x00,0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,
- 0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0xff,0xff,0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x7f,0x00,0x00,0xff,0x4a,0x4a,0x4a,0x4a,0x4b,0x52,0x4a,0x4a,0x4a,0x4a,0x4f,
- 0x4c,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x55,0x45,0x40,0x4a,0x4a,0x4a,
- 0x45,0x59,0x4d,0x46,0x4a,0x5d,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,
- 0x4a,0x4a,0x4a,0x4a,0x4a,0x61,0x63,0x67,0x4e,0x4a,0x4a,0x6b,0x6d,0x4a,0x4a,
- 0x45,0x6d,0x4a,0x4a,0x44,0x45,0x4a,0x4a,0x00,0x00,0x00,0x02,0x0d,0x06,0x06,
- 0x06,0x06,0x0e,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x00,0x06,0x06,0x02,0x06,
- 0x00,0x0a,0x0a,0x07,0x07,0x06,0x02,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04,
- 0x04,0x04,0x00,0x00,0x00,0x0e,0x05,0x06,0x06,0x06,0x01,0x06,0x00,0x00,0x08,
- 0x00,0x10,0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,
- 0x86,0x00,0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,
- 0xf8,0xbb,0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,
- 0xc4,0xff,0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,
- 0x13,0x09,0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,
- 0xb2,0xff,0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,
- 0xe7,0x08,0x00,0xf0,0x02,0x00
-};
+++ /dev/null
-/*
- * Hacker Disassembler Engine 64
- * Copyright (c) 2008-2009, Vyacheslav Patkov.
- * All rights reserved.
- *
- * hde64.h: C/C++ header file
- *
- */
-
-#ifndef _HDE64_H_
-#define _HDE64_H_
-
-/* stdint.h - C99 standard header
- * http://en.wikipedia.org/wiki/stdint.h
- *
- * if your compiler doesn't contain "stdint.h" header (for
- * example, Microsoft Visual C++), you can download file:
- * http://www.azillionmonkeys.com/qed/pstdint.h
- * and change next line to:
- * #include "pstdint.h"
- */
-/* #include <stdint.h> */
-#include "../../pstdint.h"
-
-#define F_MODRM 0x00000001
-#define F_SIB 0x00000002
-#define F_IMM8 0x00000004
-#define F_IMM16 0x00000008
-#define F_IMM32 0x00000010
-#define F_IMM64 0x00000020
-#define F_DISP8 0x00000040
-#define F_DISP16 0x00000080
-#define F_DISP32 0x00000100
-#define F_RELATIVE 0x00000200
-#define F_ERROR 0x00001000
-#define F_ERROR_OPCODE 0x00002000
-#define F_ERROR_LENGTH 0x00004000
-#define F_ERROR_LOCK 0x00008000
-#define F_ERROR_OPERAND 0x00010000
-#define F_PREFIX_REPNZ 0x01000000
-#define F_PREFIX_REPX 0x02000000
-#define F_PREFIX_REP 0x03000000
-#define F_PREFIX_66 0x04000000
-#define F_PREFIX_67 0x08000000
-#define F_PREFIX_LOCK 0x10000000
-#define F_PREFIX_SEG 0x20000000
-#define F_PREFIX_REX 0x40000000
-#define F_PREFIX_ANY 0x7f000000
-
-#define PREFIX_SEGMENT_CS 0x2e
-#define PREFIX_SEGMENT_SS 0x36
-#define PREFIX_SEGMENT_DS 0x3e
-#define PREFIX_SEGMENT_ES 0x26
-#define PREFIX_SEGMENT_FS 0x64
-#define PREFIX_SEGMENT_GS 0x65
-#define PREFIX_LOCK 0xf0
-#define PREFIX_REPNZ 0xf2
-#define PREFIX_REPX 0xf3
-#define PREFIX_OPERAND_SIZE 0x66
-#define PREFIX_ADDRESS_SIZE 0x67
-
-#pragma pack(push,1)
-
-typedef struct {
- uint8_t len;
- uint8_t p_rep;
- uint8_t p_lock;
- uint8_t p_seg;
- uint8_t p_66;
- uint8_t p_67;
- uint8_t rex;
- uint8_t rex_w;
- uint8_t rex_r;
- uint8_t rex_x;
- uint8_t rex_b;
- uint8_t opcode;
- uint8_t opcode2;
- uint8_t modrm;
- uint8_t modrm_mod;
- uint8_t modrm_reg;
- uint8_t modrm_rm;
- uint8_t sib;
- uint8_t sib_scale;
- uint8_t sib_index;
- uint8_t sib_base;
- union {
- uint8_t imm8;
- uint16_t imm16;
- uint32_t imm32;
- uint64_t imm64;
- } imm;
- union {
- uint8_t disp8;
- uint16_t disp16;
- uint32_t disp32;
- } disp;
- uint32_t flags;
-} hde64s;
-
-#pragma pack(pop)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* __cdecl */
-unsigned int hde64_disasm(const void *code, hde64s *hs);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _HDE64_H_ */
+++ /dev/null
-/*
- * Hacker Disassembler Engine 64 C
- * Copyright (c) 2008-2009, Vyacheslav Patkov.
- * All rights reserved.
- *
- */
-
-/* #include <stdint.h> */
-#include <string.h>
-
-#include "../include/hde64.h"
-#include "table64.h"
-
-unsigned int hde64_disasm(const void *code, hde64s *hs)
-{
- uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0;
- uint8_t *ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0;
- uint8_t op64 = 0;
-
- memset(hs,0,sizeof(hde64s));
-
- for (x = 16; x; x--)
- switch (c = *p++) {
- case 0xf3:
- hs->p_rep = c;
- pref |= PRE_F3;
- break;
- case 0xf2:
- hs->p_rep = c;
- pref |= PRE_F2;
- break;
- case 0xf0:
- hs->p_lock = c;
- pref |= PRE_LOCK;
- break;
- case 0x26: case 0x2e: case 0x36:
- case 0x3e: case 0x64: case 0x65:
- hs->p_seg = c;
- pref |= PRE_SEG;
- break;
- case 0x66:
- hs->p_66 = c;
- pref |= PRE_66;
- break;
- case 0x67:
- hs->p_67 = c;
- pref |= PRE_67;
- break;
- default:
- goto pref_done;
- }
- pref_done:
-
- hs->flags = (uint32_t)pref << 23;
-
- if (!pref)
- pref |= PRE_NONE;
-
- if ((c & 0xf0) == 0x40) {
- hs->flags |= F_PREFIX_REX;
- if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8)
- op64++;
- hs->rex_r = (c & 7) >> 2;
- hs->rex_x = (c & 3) >> 1;
- hs->rex_b = c & 1;
- if (((c = *p++) & 0xf0) == 0x40) {
- opcode = c;
- goto error_opcode;
- }
- }
-
- if ((hs->opcode = c) == 0x0f) {
- hs->opcode2 = c = *p++;
- ht += DELTA_OPCODES;
- } else if (c >= 0xa0 && c <= 0xa3) {
- op64++;
- if (pref & PRE_67)
- pref |= PRE_66;
- else
- pref &= ~PRE_66;
- }
-
- opcode = c;
- cflags = ht[ht[opcode / 4] + (opcode % 4)];
-
- if (cflags == C_ERROR) {
- error_opcode:
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
- cflags = 0;
- if ((opcode & -3) == 0x24)
- cflags++;
- }
-
- x = 0;
- if (cflags & C_GROUP) {
- uint16_t t;
- t = *(uint16_t *)(ht + (cflags & 0x7f));
- cflags = (uint8_t)t;
- x = (uint8_t)(t >> 8);
- }
-
- if (hs->opcode2) {
- ht = hde64_table + DELTA_PREFIXES;
- if (ht[ht[opcode / 4] + (opcode % 4)] & pref)
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
- }
-
- if (cflags & C_MODRM) {
- hs->flags |= F_MODRM;
- hs->modrm = c = *p++;
- hs->modrm_mod = m_mod = c >> 6;
- hs->modrm_rm = m_rm = c & 7;
- hs->modrm_reg = m_reg = (c & 0x3f) >> 3;
-
- if (x && ((x << m_reg) & 0x80))
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
-
- if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) {
- uint8_t t = opcode - 0xd9;
- if (m_mod == 3) {
- ht = hde64_table + DELTA_FPU_MODRM + t*8;
- t = ht[m_reg] << m_rm;
- } else {
- ht = hde64_table + DELTA_FPU_REG;
- t = ht[t] << m_reg;
- }
- if (t & 0x80)
- hs->flags |= F_ERROR | F_ERROR_OPCODE;
- }
-
- if (pref & PRE_LOCK) {
- if (m_mod == 3) {
- hs->flags |= F_ERROR | F_ERROR_LOCK;
- } else {
- uint8_t *table_end, op = opcode;
- if (hs->opcode2) {
- ht = hde64_table + DELTA_OP2_LOCK_OK;
- table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK;
- } else {
- ht = hde64_table + DELTA_OP_LOCK_OK;
- table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK;
- op &= -2;
- }
- for (; ht != table_end; ht++)
- if (*ht++ == op) {
- if (!((*ht << m_reg) & 0x80))
- goto no_lock_error;
- else
- break;
- }
- hs->flags |= F_ERROR | F_ERROR_LOCK;
- no_lock_error:
- ;
- }
- }
-
- if (hs->opcode2) {
- switch (opcode) {
- case 0x20: case 0x22:
- m_mod = 3;
- if (m_reg > 4 || m_reg == 1)
- goto error_operand;
- else
- goto no_error_operand;
- case 0x21: case 0x23:
- m_mod = 3;
- if (m_reg == 4 || m_reg == 5)
- goto error_operand;
- else
- goto no_error_operand;
- }
- } else {
- switch (opcode) {
- case 0x8c:
- if (m_reg > 5)
- goto error_operand;
- else
- goto no_error_operand;
- case 0x8e:
- if (m_reg == 1 || m_reg > 5)
- goto error_operand;
- else
- goto no_error_operand;
- }
- }
-
- if (m_mod == 3) {
- uint8_t *table_end;
- if (hs->opcode2) {
- ht = hde64_table + DELTA_OP2_ONLY_MEM;
- table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM;
- } else {
- ht = hde64_table + DELTA_OP_ONLY_MEM;
- table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM;
- }
- for (; ht != table_end; ht += 2)
- if (*ht++ == opcode) {
- if (*ht++ & pref && !((*ht << m_reg) & 0x80))
- goto error_operand;
- else
- break;
- }
- goto no_error_operand;
- } else if (hs->opcode2) {
- switch (opcode) {
- case 0x50: case 0xd7: case 0xf7:
- if (pref & (PRE_NONE | PRE_66))
- goto error_operand;
- break;
- case 0xd6:
- if (pref & (PRE_F2 | PRE_F3))
- goto error_operand;
- break;
- case 0xc5:
- goto error_operand;
- }
- goto no_error_operand;
- } else
- goto no_error_operand;
-
- error_operand:
- hs->flags |= F_ERROR | F_ERROR_OPERAND;
- no_error_operand:
-
- c = *p++;
- if (m_reg <= 1) {
- if (opcode == 0xf6)
- cflags |= C_IMM8;
- else if (opcode == 0xf7)
- cflags |= C_IMM_P66;
- }
-
- switch (m_mod) {
- case 0:
- if (pref & PRE_67) {
- if (m_rm == 6)
- disp_size = 2;
- } else
- if (m_rm == 5)
- disp_size = 4;
- break;
- case 1:
- disp_size = 1;
- break;
- case 2:
- disp_size = 2;
- if (!(pref & PRE_67))
- disp_size <<= 1;
- }
-
- if (m_mod != 3 && m_rm == 4) {
- hs->flags |= F_SIB;
- p++;
- hs->sib = c;
- hs->sib_scale = c >> 6;
- hs->sib_index = (c & 0x3f) >> 3;
- if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1))
- disp_size = 4;
- }
-
- p--;
- switch (disp_size) {
- case 1:
- hs->flags |= F_DISP8;
- hs->disp.disp8 = *p;
- break;
- case 2:
- hs->flags |= F_DISP16;
- hs->disp.disp16 = *(uint16_t *)p;
- break;
- case 4:
- hs->flags |= F_DISP32;
- hs->disp.disp32 = *(uint32_t *)p;
- }
- p += disp_size;
- } else if (pref & PRE_LOCK)
- hs->flags |= F_ERROR | F_ERROR_LOCK;
-
- if (cflags & C_IMM_P66) {
- if (cflags & C_REL32) {
- if (pref & PRE_66) {
- hs->flags |= F_IMM16 | F_RELATIVE;
- hs->imm.imm16 = *(uint16_t *)p;
- p += 2;
- goto disasm_done;
- }
- goto rel32_ok;
- }
- if (op64) {
- hs->flags |= F_IMM64;
- hs->imm.imm64 = *(uint64_t *)p;
- p += 8;
- } else if (!(pref & PRE_66)) {
- hs->flags |= F_IMM32;
- hs->imm.imm32 = *(uint32_t *)p;
- p += 4;
- } else
- goto imm16_ok;
- }
-
-
- if (cflags & C_IMM16) {
- imm16_ok:
- hs->flags |= F_IMM16;
- hs->imm.imm16 = *(uint16_t *)p;
- p += 2;
- }
- if (cflags & C_IMM8) {
- hs->flags |= F_IMM8;
- hs->imm.imm8 = *p++;
- }
-
- if (cflags & C_REL32) {
- rel32_ok:
- hs->flags |= F_IMM32 | F_RELATIVE;
- hs->imm.imm32 = *(uint32_t *)p;
- p += 4;
- } else if (cflags & C_REL8) {
- hs->flags |= F_IMM8 | F_RELATIVE;
- hs->imm.imm8 = *p++;
- }
-
- disasm_done:
-
- if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) {
- hs->flags |= F_ERROR | F_ERROR_LENGTH;
- hs->len = 15;
- }
-
- return (unsigned int)hs->len;
-}
+++ /dev/null
-/*
- * Hacker Disassembler Engine 64 C
- * Copyright (c) 2008-2009, Vyacheslav Patkov.
- * All rights reserved.
- *
- */
-
-#define C_NONE 0x00
-#define C_MODRM 0x01
-#define C_IMM8 0x02
-#define C_IMM16 0x04
-#define C_IMM_P66 0x10
-#define C_REL8 0x20
-#define C_REL32 0x40
-#define C_GROUP 0x80
-#define C_ERROR 0xff
-
-#define PRE_ANY 0x00
-#define PRE_NONE 0x01
-#define PRE_F2 0x02
-#define PRE_F3 0x04
-#define PRE_66 0x08
-#define PRE_67 0x10
-#define PRE_LOCK 0x20
-#define PRE_SEG 0x40
-#define PRE_ALL 0xff
-
-#define DELTA_OPCODES 0x4a
-#define DELTA_FPU_REG 0xfd
-#define DELTA_FPU_MODRM 0x104
-#define DELTA_PREFIXES 0x13c
-#define DELTA_OP_LOCK_OK 0x1ae
-#define DELTA_OP2_LOCK_OK 0x1c6
-#define DELTA_OP_ONLY_MEM 0x1d8
-#define DELTA_OP2_ONLY_MEM 0x1e7
-
-unsigned char hde64_table[] = {
- 0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5,
- 0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1,
- 0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea,
- 0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0,
- 0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab,
- 0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92,
- 0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90,
- 0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b,
- 0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,
- 0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc,
- 0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20,
- 0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff,
- 0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00,
- 0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01,
- 0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10,
- 0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00,
- 0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00,
- 0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00,
- 0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
- 0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,
- 0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40,
- 0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43,
- 0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
- 0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40,
- 0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06,
- 0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07,
- 0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04,
- 0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10,
- 0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00,
- 0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb,
- 0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff,
- 0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09,
- 0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff,
- 0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08,
- 0x00,0xf0,0x02,0x00
-};
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <cassert>\r
-#include <vector>\r
-#include <algorithm>\r
-#include <boost/foreach.hpp>\r
-#include <Windows.h>\r
-\r
-#include "buffer.h"\r
-\r
-namespace MinHook { namespace\r
-{\r
- struct MEMORY_BLOCK\r
- {\r
- void* pAddress;\r
- DWORD protect;\r
- size_t usedSize;\r
- size_t fixedSize;\r
- };\r
-\r
- template <typename T>\r
- bool operator <(const MEMORY_BLOCK& lhs, const T& rhs);\r
- template <typename T>\r
- bool operator <(const T& lhs, const MEMORY_BLOCK& rhs);\r
- bool operator <(const MEMORY_BLOCK& lhs, const MEMORY_BLOCK& rhs);\r
-\r
- void* AllocateBuffer(void* const pOrigin, DWORD protect, size_t size);\r
- MEMORY_BLOCK* GetMemoryBlock(void* const pOrigin, DWORD protect, size_t capacity);\r
-\r
- const size_t BlockSize = 0x10000;\r
-\r
-#if defined _M_X64\r
- intptr_t gMinAddress;\r
- intptr_t gMaxAddress;\r
-#endif\r
- std::vector<MEMORY_BLOCK> gMemoryBlocks;\r
-}}\r
-\r
-namespace MinHook \r
-{\r
- void InitializeBuffer()\r
- {\r
-#if defined _M_X64\r
- SYSTEM_INFO si;\r
- GetSystemInfo(&si);\r
- \r
- gMinAddress = reinterpret_cast<intptr_t>(si.lpMinimumApplicationAddress);\r
- gMaxAddress = reinterpret_cast<intptr_t>(si.lpMaximumApplicationAddress);\r
-#endif\r
- }\r
-\r
- void UninitializeBuffer()\r
- {\r
- BOOST_FOREACH (MEMORY_BLOCK& block, gMemoryBlocks)\r
- {\r
- VirtualFree(block.pAddress, 0, MEM_RELEASE);\r
- }\r
-\r
- std::vector<MEMORY_BLOCK> v;\r
- gMemoryBlocks.swap(v);\r
- }\r
-\r
- void* AllocateCodeBuffer(void* const pOrigin, size_t size)\r
- {\r
- assert(("AllocateBuffer", (size > 0)));\r
-\r
- return AllocateBuffer(pOrigin, PAGE_EXECUTE_READ, size);\r
- }\r
-\r
- void* AllocateDataBuffer(void* const pOrigin, size_t size)\r
- {\r
- assert(("AllocateBuffer", (size > 0)));\r
-\r
- return AllocateBuffer(pOrigin, PAGE_READONLY, size);\r
- }\r
-\r
- void RollbackBuffer()\r
- {\r
- BOOST_FOREACH (MEMORY_BLOCK& block, gMemoryBlocks)\r
- {\r
- block.usedSize = block.fixedSize;\r
- }\r
- }\r
-\r
- void CommitBuffer()\r
- {\r
- BOOST_FOREACH (MEMORY_BLOCK& block, gMemoryBlocks)\r
- {\r
- if (block.usedSize == block.fixedSize)\r
- {\r
- continue;\r
- }\r
-\r
- void* pBuffer = reinterpret_cast<char*>(block.pAddress) + block.fixedSize;\r
- size_t size = block.usedSize - block.fixedSize;\r
- DWORD op;\r
- VirtualProtect(pBuffer, size, block.protect, &op);\r
- }\r
- }\r
-}\r
-\r
-namespace MinHook { namespace\r
-{\r
- void* AllocateBuffer(void* const pOrigin, DWORD protect, size_t size)\r
- {\r
- assert(("AllocateBuffer", (protect == PAGE_EXECUTE_READ || protect == PAGE_READONLY)));\r
- assert(("AllocateBuffer", (size > 0)));\r
-\r
- // \83A\83\89\83C\83\81\83\93\83g\8b«\8aE\82É\90Ø\82è\8fã\82°\r
- size = (size + TYPE_ALIGNMENT(void*) - 1) & ~(TYPE_ALIGNMENT(void*) - 1);\r
-\r
- MEMORY_BLOCK* pBlock = GetMemoryBlock(pOrigin, protect, size);\r
- if (pBlock == NULL)\r
- {\r
- return NULL;\r
- }\r
-\r
- void* pBuffer = reinterpret_cast<char*>(pBlock->pAddress) + pBlock->usedSize;\r
- if (VirtualAlloc(pBuffer, size, MEM_COMMIT, pBlock->protect) == NULL)\r
- {\r
- return NULL;\r
- }\r
-\r
- DWORD oldProtect;\r
- // PAGE_EXECUTE_READ -> PAGE_EXECUTE_READWRITE, PAGE_READONLY -> PAGE_READWRITE\r
- if (!VirtualProtect(pBuffer, size, (pBlock->protect << 1), &oldProtect))\r
- {\r
- return NULL;\r
- }\r
-\r
- pBlock->usedSize += size;\r
- return pBuffer;\r
- }\r
-\r
- MEMORY_BLOCK* GetMemoryBlock(void* const pOrigin, DWORD protect, size_t capacity)\r
- {\r
- assert(("GetMemoryBlock", (protect == PAGE_EXECUTE_READ || protect == PAGE_READONLY)));\r
- assert(("GetMemoryBlock", (capacity > 0)));\r
-\r
- typedef std::vector<MEMORY_BLOCK>::iterator mb_iter;\r
-\r
-#if defined _M_X64\r
- intptr_t minAddr = gMinAddress;\r
- intptr_t maxAddr = gMaxAddress; \r
- if (pOrigin != NULL)\r
- {\r
- // pOrigin \81} 512MB \82Ì\94Í\88Í \r
- minAddr = std::max<intptr_t>(minAddr, reinterpret_cast<intptr_t>(pOrigin) - 0x20000000);\r
- maxAddr = std::min<intptr_t>(maxAddr, reinterpret_cast<intptr_t>(pOrigin) + 0x20000000);\r
- }\r
-#endif\r
-\r
- // \82·\82Å\82É\93o\98^\8dÏ\82Ý\82Ì\97Ì\88æ\82Ì\92\86\82©\82ç\8eg\97p\89Â\94\\82È\82à\82Ì\82ª\8c©\82Â\82©\82ê\82Î\81A\82»\82ê\82ð\95Ô\82·\r
- MEMORY_BLOCK* pBlock = NULL;\r
- {\r
- mb_iter ib = gMemoryBlocks.begin();\r
- mb_iter ie = gMemoryBlocks.end();\r
-#if defined _M_X64\r
- if (pOrigin != NULL)\r
- {\r
- // \8c\9f\8dõ\91O\82É\83A\83h\83\8c\83X\94Í\88Í\82Å\8di\82è\8d\9e\82Ý\r
- ib = std::lower_bound(ib, ie, minAddr);\r
- ie = std::lower_bound(ib, ie, maxAddr);\r
- }\r
-#endif\r
- for (mb_iter i = ib; i != ie; ++i)\r
- {\r
- if (i->protect == protect && i->usedSize + capacity <= BlockSize)\r
- {\r
- return &(*i);\r
- }\r
- }\r
- }\r
-\r
- // \8c©\82Â\82©\82ç\82È\82¯\82ê\82Î\81A\90V\82½\82È\83A\83h\83\8c\83X\97Ì\88æ\82ð\8am\95Û\r
- void* pAlloc = NULL;\r
-#if defined _M_X64\r
- if (pOrigin != NULL)\r
- {\r
- // \8c\9f\8dõ\94Í\88Í\82Ì\92\86\90S\82©\82ç\8aO\91¤\82Ö\8bó\82«\97Ì\88æ\82ð\92T\82µ\82Ä\82¢\82\r
- intptr_t min = minAddr / BlockSize;\r
- intptr_t max = maxAddr / BlockSize;\r
- int rel = 0;\r
- MEMORY_BASIC_INFORMATION mi = { 0 };\r
- for (int i = 0; i < (max - min + 1); ++i)\r
- {\r
- rel = -rel + (i & 1);\r
- void* pQuery = reinterpret_cast<void*>(((min + max) / 2 + rel) * BlockSize);\r
- VirtualQuery(pQuery, &mi, sizeof(mi));\r
- if (mi.State == MEM_FREE)\r
- {\r
- pAlloc = VirtualAlloc(pQuery, BlockSize, MEM_RESERVE, protect);\r
- if (pAlloc != NULL)\r
- {\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- else\r
-#endif // X86\83\82\81[\83h\82Å\82Í\81A\83A\83h\83\8c\83X\82Í\96â\91è\82É\82È\82ç\82È\82¢\r
- {\r
- pAlloc = VirtualAlloc(NULL, BlockSize, MEM_RESERVE, protect);\r
- }\r
-\r
- if (pAlloc != NULL)\r
- {\r
- MEMORY_BLOCK block = { 0 };\r
- block.pAddress = pAlloc;\r
- block.protect = protect;\r
-\r
-#if defined _M_X64\r
- mb_iter i = std::lower_bound(gMemoryBlocks.begin(), gMemoryBlocks.end(), pAlloc);\r
-#elif defined _M_IX86\r
- mb_iter i = gMemoryBlocks.begin();\r
-#endif\r
- i = gMemoryBlocks.insert(i, block);\r
-\r
- return &(*i);\r
- }\r
-\r
- return NULL;\r
- }\r
-\r
- template <typename T>\r
- bool operator <(const MEMORY_BLOCK& lhs, const T& rhs) \r
- { \r
- return lhs.pAddress < reinterpret_cast<void*>(rhs); \r
- }\r
-\r
- template <typename T>\r
- bool operator <(const T& lhs, const MEMORY_BLOCK& rhs) \r
- { \r
- return reinterpret_cast<void*>(lhs) < rhs.pAddress; \r
- }\r
-\r
- bool operator <(const MEMORY_BLOCK& lhs, const MEMORY_BLOCK& rhs)\r
- { \r
- return lhs.pAddress < rhs.pAddress;\r
- }\r
-}}
\ No newline at end of file
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#pragma once\r
-\r
-namespace MinHook\r
-{ \r
- void InitializeBuffer();\r
- void UninitializeBuffer();\r
- void* AllocateCodeBuffer(void* const pOrigin, size_t size);\r
- void* AllocateDataBuffer(void* const pOrigin, size_t size);\r
- void RollbackBuffer();\r
- void CommitBuffer();\r
-}\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <Windows.h>\r
-#include "../MinHook.h"\r
-#include "hook.h"\r
-\r
-using namespace MinHook;\r
-\r
-MH_STATUS WINAPI MH_Initialize()\r
-{\r
- return Initialize();\r
-}\r
-\r
-MH_STATUS WINAPI MH_Uninitialize()\r
-{\r
- return Uninitialize();\r
-}\r
-\r
-MH_STATUS WINAPI MH_CreateHook(void* pTarget, void* const pDetour, void** ppOriginal)\r
-{\r
- return CreateHook(pTarget, pDetour, ppOriginal);\r
-}\r
-\r
-MH_STATUS WINAPI MH_EnableHook(void* pTarget)\r
-{\r
- return EnableHook(pTarget);\r
-}\r
-\r
-MH_STATUS WINAPI MH_DisableHook(void* pTarget)\r
-{\r
- return DisableHook(pTarget);\r
-}\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <cassert>\r
-#include <vector>\r
-#include <algorithm>\r
-#include <functional>\r
-#include <boost/scope_exit.hpp>\r
-#include <boost/foreach.hpp>\r
-#include <Windows.h>\r
-#include "pstdint.h"\r
-\r
-#include "../MinHook.h"\r
-#include "hook.h"\r
-#include "buffer.h"\r
-#include "trampoline.h"\r
-#include "thread.h"\r
-\r
-namespace MinHook { namespace\r
-{\r
- struct HOOK_ENTRY\r
- {\r
- void* pTarget;\r
- void* pDetour;\r
-#if defined _M_X64\r
- void* pRelay;\r
-#endif\r
- void* pTrampoline;\r
- void* pBackup;\r
- bool isEnabled;\r
- std::vector<uintptr_t> oldIPs;\r
- std::vector<uintptr_t> newIPs;\r
- };\r
-\r
- // \96½\97ß\8f\91\82«\8d\9e\82Ý\97p\8d\\91¢\91Ì\r
-#pragma pack(push, 1)\r
- struct JMP_REL\r
- {\r
- uint8_t opcode;\r
- uint32_t operand;\r
- };\r
-\r
- struct JMP_ABS\r
- {\r
- uint16_t opcode;\r
- uint32_t operand;\r
- };\r
-#pragma pack(pop)\r
-\r
- HOOK_ENTRY* FindHook(void* const pTarget);\r
- bool IsExecutableAddress(void* pAddress);\r
- void WriteRelativeJump(void* pFrom, void* const pTo);\r
- void WriteAbsoluteJump(void* pFrom, void* const pTo, void* pTable);\r
-\r
- template <typename T>\r
- bool operator <(const HOOK_ENTRY& lhs, const T& rhs) ;\r
- template <typename T>\r
- bool operator <(const T& lhs, const HOOK_ENTRY& rhs) ;\r
- bool operator <(const HOOK_ENTRY& lhs, const HOOK_ENTRY& rhs);\r
-\r
- CriticalSection gCS;\r
- std::vector<HOOK_ENTRY> gHooks;\r
- bool gIsInitialized = false;\r
-}}\r
-\r
-namespace MinHook\r
-{\r
- MH_STATUS Initialize()\r
- {\r
- CriticalSection::ScopedLock lock(gCS);\r
-\r
- if (gIsInitialized)\r
- {\r
- return MH_ERROR_ALREADY_INITIALIZED;\r
- }\r
-\r
- // \93à\95\94\8aÖ\90\94\83o\83b\83t\83@\82Ì\8f\89\8aú\89»\r
- InitializeBuffer();\r
-\r
- gIsInitialized = true;\r
- return MH_OK;\r
- }\r
-\r
- MH_STATUS Uninitialize()\r
- {\r
- CriticalSection::ScopedLock lock(gCS);\r
-\r
- if (!gIsInitialized)\r
- {\r
- return MH_ERROR_NOT_INITIALIZED;\r
- }\r
-\r
- // \82·\82×\82Ä\82Ì\83t\83b\83N\82ð\89ð\8f\9c\r
- BOOST_FOREACH (const HOOK_ENTRY& hook, gHooks)\r
- {\r
- if (!hook.isEnabled)\r
- {\r
- continue;\r
- }\r
- \r
- MH_STATUS status = DisableHook(hook.pTarget);\r
- if (status != MH_OK)\r
- {\r
- return status;\r
- }\r
- }\r
-\r
- std::vector<HOOK_ENTRY> v;\r
- gHooks.swap(v);\r
-\r
- // \93à\95\94\8aÖ\90\94\83o\83b\83t\83@\82Ì\8aJ\95ú\r
- UninitializeBuffer();\r
-\r
- gIsInitialized = false;\r
- return MH_OK;\r
- }\r
-\r
- MH_STATUS CreateHook(void* pTarget, void* const pDetour, void** ppOriginal)\r
- {\r
- CriticalSection::ScopedLock lock(gCS);\r
-\r
- if (!gIsInitialized)\r
- {\r
- return MH_ERROR_NOT_INITIALIZED;\r
- }\r
-\r
- HOOK_ENTRY *pHook = FindHook(pTarget);\r
- if (pHook != NULL)\r
- {\r
- return MH_ERROR_ALREADY_CREATED;\r
- }\r
-\r
- if (!IsExecutableAddress(pTarget) || !IsExecutableAddress(pDetour))\r
- {\r
- return MH_ERROR_NOT_EXECUTABLE;\r
- }\r
-\r
- if (pHook == NULL)\r
- {\r
- bool committed = false;\r
- BOOST_SCOPE_EXIT((&committed))\r
- {\r
- if (!committed)\r
- {\r
- RollbackBuffer();\r
- }\r
- }\r
- BOOST_SCOPE_EXIT_END;\r
-\r
- // \83g\83\89\83\93\83|\83\8a\83\93\8aÖ\90\94\82ð\8dì\90¬\82·\82é\r
- CREATE_TREMPOLINE_T ct = { 0 };\r
- ct.pTarget = pTarget;\r
- if (!CreateTrampolineFunction(ct))\r
- {\r
- return MH_ERROR_UNSUPPORTED_FUNCTION;\r
- }\r
-\r
- void* pTrampoline = AllocateCodeBuffer(pTarget, ct.trampoline.size());\r
- if (pTrampoline == NULL)\r
- {\r
- return MH_ERROR_MEMORY_ALLOC;\r
- }\r
-#if defined _M_X64\r
- void* pTable = AllocateDataBuffer(pTrampoline, (ct.table.size() + 1) * sizeof(uintptr_t));\r
- if (pTable == NULL)\r
- {\r
- return MH_ERROR_MEMORY_ALLOC;\r
- }\r
-#endif\r
-\r
- ct.pTrampoline = pTrampoline;\r
-#if defined _M_X64\r
- ct.pTable = pTable;\r
-#endif\r
- if (!ResolveTemporaryAddresses(ct))\r
- {\r
- return MH_ERROR_UNSUPPORTED_FUNCTION;\r
- }\r
-\r
- memcpy(pTrampoline, &ct.trampoline[ 0 ], ct.trampoline.size());\r
-#if defined _M_X64\r
- if (ct.table.size() != 0)\r
- {\r
- memcpy(pTable, &ct.table[ 0 ], ct.table.size() * sizeof(uintptr_t));\r
- }\r
-#endif\r
-\r
- // \83^\81[\83Q\83b\83g\8aÖ\90\94\82Ì\83o\83b\83N\83A\83b\83v\82ð\82Æ\82é\r
- void* pBackup = AllocateDataBuffer(NULL, sizeof(JMP_REL));\r
- if (pBackup == NULL)\r
- {\r
- return MH_ERROR_MEMORY_ALLOC;\r
- }\r
-\r
- memcpy(pBackup, pTarget, sizeof(JMP_REL));\r
-\r
- // \92\86\8cp\8aÖ\90\94\82ð\8dì\90¬\82·\82é\r
-#if defined _M_X64\r
- void* pRelay = AllocateCodeBuffer(pTarget, sizeof(JMP_ABS));\r
- if (pRelay == NULL)\r
- {\r
- return MH_ERROR_MEMORY_ALLOC;\r
- }\r
-\r
- WriteAbsoluteJump(pRelay, pDetour, reinterpret_cast<uintptr_t*>(pTable) + ct.table.size());\r
-#endif\r
- CommitBuffer();\r
- committed = true;\r
-\r
- // \83t\83b\83N\8fî\95ñ\82Ì\93o\98^\r
- HOOK_ENTRY hook = { 0 };\r
- hook.pTarget = pTarget;\r
- hook.pDetour = pDetour;\r
-#if defined _M_X64\r
- hook.pRelay = pRelay;\r
-#endif\r
- hook.pTrampoline = pTrampoline;\r
- hook.pBackup = pBackup;\r
- hook.isEnabled = false;\r
- hook.oldIPs = ct.oldIPs;\r
- hook.newIPs = ct.newIPs;\r
-\r
- std::vector<HOOK_ENTRY>::iterator i = std::lower_bound(gHooks.begin(), gHooks.end(), hook);\r
- i = gHooks.insert(i, hook);\r
- pHook = &(*i);\r
-\r
- }\r
-\r
- // OUT\88ø\90\94\82Ì\8f\88\97\9d\r
- *ppOriginal = pHook->pTrampoline;\r
- \r
- return MH_OK;\r
- }\r
-\r
- MH_STATUS EnableHook(void* pTarget)\r
- {\r
- CriticalSection::ScopedLock lock(gCS);\r
-\r
- if (!gIsInitialized)\r
- {\r
- return MH_ERROR_NOT_INITIALIZED;\r
- }\r
-\r
- HOOK_ENTRY *pHook = FindHook(pTarget);\r
- if (pHook == NULL)\r
- {\r
- return MH_ERROR_NOT_CREATED;\r
- }\r
-\r
- if (pHook->isEnabled)\r
- {\r
- return MH_ERROR_ENABLED;\r
- }\r
-\r
- // \83^\81[\83Q\83b\83g\8aÖ\90\94\82Ì\96`\93ª\82É\81A\92\86\8cp\8aÖ\90\94\82Ü\82½\82Í\83t\83b\83N\8aÖ\90\94\82Ö\82Ì\83W\83\83\83\93\83v\82ð\8f\91\82«\8d\9e\82Þ\r
- {\r
- ScopedThreadExclusive tex(pHook->oldIPs, pHook->newIPs);\r
-\r
- DWORD oldProtect;\r
- if (!VirtualProtect(pHook->pTarget, sizeof(JMP_REL), PAGE_EXECUTE_READWRITE, &oldProtect))\r
- {\r
- return MH_ERROR_MEMORY_PROTECT;\r
- }\r
-\r
-#if defined _M_X64\r
- WriteRelativeJump(pHook->pTarget, pHook->pRelay);\r
-#elif defined _M_IX86\r
- WriteRelativeJump(pHook->pTarget, pHook->pDetour);\r
-#endif\r
- VirtualProtect(pHook->pTarget, sizeof(JMP_REL), oldProtect, &oldProtect);\r
- }\r
-\r
- pHook->isEnabled = true;\r
-\r
- return MH_OK;\r
- }\r
-\r
- MH_STATUS DisableHook(void* pTarget)\r
- {\r
- CriticalSection::ScopedLock lock(gCS);\r
-\r
- if (!gIsInitialized)\r
- {\r
- return MH_ERROR_NOT_INITIALIZED;\r
- }\r
-\r
- HOOK_ENTRY *pHook = FindHook(pTarget);\r
- if (pHook == NULL)\r
- {\r
- return MH_ERROR_NOT_CREATED;\r
- }\r
-\r
- if (!pHook->isEnabled)\r
- {\r
- return MH_ERROR_DISABLED;\r
- }\r
-\r
- // \83^\81[\83Q\83b\83g\8aÖ\90\94\82Ì\96`\93ª\82ð\8f\91\82«\96ß\82·\82¾\82¯\81B\91¼\82Í\8dÄ\97\98\97p\82Ì\82½\82ß\8ec\82µ\82Ä\82¨\82\r
- {\r
- ScopedThreadExclusive tex(pHook->oldIPs, pHook->newIPs);\r
-\r
- DWORD oldProtect;\r
- if (!VirtualProtect(pHook->pTarget, sizeof(JMP_REL), PAGE_EXECUTE_READWRITE, &oldProtect))\r
- {\r
- return MH_ERROR_MEMORY_PROTECT;\r
- }\r
-\r
- memcpy(pHook->pTarget, pHook->pBackup, sizeof(JMP_REL));\r
-\r
- VirtualProtect(pHook->pTarget, sizeof(JMP_REL), oldProtect, &oldProtect);\r
- }\r
-\r
- pHook->isEnabled = false;\r
-\r
- return MH_OK;\r
- }\r
-}\r
-namespace MinHook { namespace\r
-{\r
- HOOK_ENTRY* FindHook(void* const pTarget)\r
- {\r
- std::vector<HOOK_ENTRY>::iterator i \r
- = std::lower_bound(gHooks.begin(), gHooks.end(), pTarget);\r
- if (i != gHooks.end() && i->pTarget == pTarget)\r
- {\r
- return &(*i);\r
- }\r
-\r
- return NULL;\r
- }\r
-\r
- bool IsExecutableAddress(void* pAddress)\r
- {\r
- static const DWORD PageExecuteMask \r
- = (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY); \r
-\r
- // \96¢\8a\84\82è\93\96\82Ä\82â\8eÀ\8ds\95s\89Â\94\\82È\97Ì\88æ\82ð\83`\83F\83b\83N\r
- MEMORY_BASIC_INFORMATION mi = { 0 };\r
- VirtualQuery(pAddress, &mi, sizeof(mi));\r
-\r
- return ((mi.Protect & PageExecuteMask) != 0);\r
- }\r
-\r
- void WriteRelativeJump(void* pFrom, void* const pTo)\r
- {\r
- JMP_REL jmp;\r
- jmp.opcode = 0xE9;\r
- jmp.operand = static_cast<uint32_t>(reinterpret_cast<char*>(pTo) - (reinterpret_cast<char*>(pFrom) + sizeof(jmp)));\r
-\r
- memcpy(pFrom, &jmp, sizeof(jmp));\r
- }\r
-\r
- void WriteAbsoluteJump(void* pFrom, void* const pTo, void* pTable)\r
- {\r
- JMP_ABS jmp;\r
- jmp.opcode = 0x25FF;\r
- jmp.operand = static_cast<uint32_t>(reinterpret_cast<char*>(pTable) - (reinterpret_cast<char*>(pFrom) + sizeof(jmp)));\r
-\r
- memcpy(pFrom, &jmp, sizeof(jmp));\r
- memcpy(pTable, &pTo, sizeof(pTo));\r
- }\r
-\r
- template <typename T>\r
- bool operator <(const HOOK_ENTRY& lhs, const T& rhs) \r
- { \r
- return lhs.pTarget < reinterpret_cast<void*>(rhs); \r
- }\r
-\r
- template <typename T>\r
- bool operator <(const T& lhs, const HOOK_ENTRY& rhs) \r
- { \r
- return reinterpret_cast<void*>(lhs) < rhs.pTarget; \r
- }\r
-\r
- bool operator <(const HOOK_ENTRY& lhs, const HOOK_ENTRY& rhs)\r
- { \r
- return lhs.pTarget < rhs.pTarget;\r
- }\r
-}}\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#pragma once\r
-\r
-#include "../MinHook.h"\r
-\r
-namespace MinHook\r
-{\r
- MH_STATUS Initialize();\r
- MH_STATUS Uninitialize();\r
- MH_STATUS CreateHook(void* pTarget, void* const pDetour, void** ppOriginal);\r
- MH_STATUS EnableHook(void* pTarget);\r
- MH_STATUS DisableHook(void* pTarget);\r
-}\r
+++ /dev/null
-/* A portable stdint.h\r
- ****************************************************************************\r
- * BSD License:\r
- ****************************************************************************\r
- *\r
- * Copyright (c) 2005-2007 Paul Hsieh\r
- * All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- ****************************************************************************\r
- *\r
- * Version 0.1.11\r
- *\r
- * The ANSI C standard committee, for the C99 standard, specified the\r
- * inclusion of a new standard include file called stdint.h. This is\r
- * a very useful and long desired include file which contains several\r
- * very precise definitions for integer scalar types that is\r
- * critically important for making portable several classes of\r
- * applications including cryptography, hashing, variable length\r
- * integer libraries and so on. But for most developers its likely\r
- * useful just for programming sanity.\r
- *\r
- * The problem is that most compiler vendors have decided not to\r
- * implement the C99 standard, and the next C++ language standard\r
- * (which has a lot more mindshare these days) will be a long time in\r
- * coming and its unknown whether or not it will include stdint.h or\r
- * how much adoption it will have. Either way, it will be a long time\r
- * before all compilers come with a stdint.h and it also does nothing\r
- * for the extremely large number of compilers available today which\r
- * do not include this file, or anything comparable to it.\r
- *\r
- * So that's what this file is all about. Its an attempt to build a\r
- * single universal include file that works on as many platforms as\r
- * possible to deliver what stdint.h is supposed to. A few things\r
- * that should be noted about this file:\r
- *\r
- * 1) It is not guaranteed to be portable and/or present an identical\r
- * interface on all platforms. The extreme variability of the\r
- * ANSI C standard makes this an impossibility right from the\r
- * very get go. Its really only meant to be useful for the vast\r
- * majority of platforms that possess the capability of\r
- * implementing usefully and precisely defined, standard sized\r
- * integer scalars. Systems which are not intrinsically 2s\r
- * complement may produce invalid constants.\r
- *\r
- * 2) There is an unavoidable use of non-reserved symbols.\r
- *\r
- * 3) Other standard include files are invoked.\r
- *\r
- * 4) This file may come in conflict with future platforms that do\r
- * include stdint.h. The hope is that one or the other can be\r
- * used with no real difference.\r
- *\r
- * 5) In the current verison, if your platform can't represent\r
- * int32_t, int16_t and int8_t, it just dumps out with a compiler\r
- * error.\r
- *\r
- * 6) 64 bit integers may or may not be defined. Test for their\r
- * presence with the test: #ifdef INT64_MAX or #ifdef UINT64_MAX.\r
- * Note that this is different from the C99 specification which\r
- * requires the existence of 64 bit support in the compiler. If\r
- * this is not defined for your platform, yet it is capable of\r
- * dealing with 64 bits then it is because this file has not yet\r
- * been extended to cover all of your system's capabilities.\r
- *\r
- * 7) (u)intptr_t may or may not be defined. Test for its presence\r
- * with the test: #ifdef PTRDIFF_MAX. If this is not defined\r
- * for your platform, then it is because this file has not yet\r
- * been extended to cover all of your system's capabilities, not\r
- * because its optional.\r
- *\r
- * 8) The following might not been defined even if your platform is\r
- * capable of defining it:\r
- *\r
- * WCHAR_MIN\r
- * WCHAR_MAX\r
- * (u)int64_t\r
- * PTRDIFF_MIN\r
- * PTRDIFF_MAX\r
- * (u)intptr_t\r
- *\r
- * 9) The following have not been defined:\r
- *\r
- * WINT_MIN\r
- * WINT_MAX\r
- *\r
- * 10) The criteria for defining (u)int_least(*)_t isn't clear,\r
- * except for systems which don't have a type that precisely\r
- * defined 8, 16, or 32 bit types (which this include file does\r
- * not support anyways). Default definitions have been given.\r
- *\r
- * 11) The criteria for defining (u)int_fast(*)_t isn't something I\r
- * would trust to any particular compiler vendor or the ANSI C\r
- * committee. It is well known that "compatible systems" are\r
- * commonly created that have very different performance\r
- * characteristics from the systems they are compatible with,\r
- * especially those whose vendors make both the compiler and the\r
- * system. Default definitions have been given, but its strongly\r
- * recommended that users never use these definitions for any\r
- * reason (they do *NOT* deliver any serious guarantee of\r
- * improved performance -- not in this file, nor any vendor's\r
- * stdint.h).\r
- *\r
- * 12) The following macros:\r
- *\r
- * PRINTF_INTMAX_MODIFIER\r
- * PRINTF_INT64_MODIFIER\r
- * PRINTF_INT32_MODIFIER\r
- * PRINTF_INT16_MODIFIER\r
- * PRINTF_LEAST64_MODIFIER\r
- * PRINTF_LEAST32_MODIFIER\r
- * PRINTF_LEAST16_MODIFIER\r
- * PRINTF_INTPTR_MODIFIER\r
- *\r
- * are strings which have been defined as the modifiers required\r
- * for the "d", "u" and "x" printf formats to correctly output\r
- * (u)intmax_t, (u)int64_t, (u)int32_t, (u)int16_t, (u)least64_t,\r
- * (u)least32_t, (u)least16_t and (u)intptr_t types respectively.\r
- * PRINTF_INTPTR_MODIFIER is not defined for some systems which\r
- * provide their own stdint.h. PRINTF_INT64_MODIFIER is not\r
- * defined if INT64_MAX is not defined. These are an extension\r
- * beyond what C99 specifies must be in stdint.h.\r
- *\r
- * In addition, the following macros are defined:\r
- *\r
- * PRINTF_INTMAX_HEX_WIDTH\r
- * PRINTF_INT64_HEX_WIDTH\r
- * PRINTF_INT32_HEX_WIDTH\r
- * PRINTF_INT16_HEX_WIDTH\r
- * PRINTF_INT8_HEX_WIDTH\r
- * PRINTF_INTMAX_DEC_WIDTH\r
- * PRINTF_INT64_DEC_WIDTH\r
- * PRINTF_INT32_DEC_WIDTH\r
- * PRINTF_INT16_DEC_WIDTH\r
- * PRINTF_INT8_DEC_WIDTH\r
- *\r
- * Which specifies the maximum number of characters required to\r
- * print the number of that type in either hexadecimal or decimal.\r
- * These are an extension beyond what C99 specifies must be in\r
- * stdint.h.\r
- *\r
- * Compilers tested (all with 0 warnings at their highest respective\r
- * settings): Borland Turbo C 2.0, WATCOM C/C++ 11.0 (16 bits and 32\r
- * bits), Microsoft Visual C++ 6.0 (32 bit), Microsoft Visual Studio\r
- * .net (VC7), Intel C++ 4.0, GNU gcc v3.3.3\r
- *\r
- * This file should be considered a work in progress. Suggestions for\r
- * improvements, especially those which increase coverage are strongly\r
- * encouraged.\r
- *\r
- * Acknowledgements\r
- *\r
- * The following people have made significant contributions to the\r
- * development and testing of this file:\r
- *\r
- * Chris Howie\r
- * John Steele Scott\r
- * Dave Thorup\r
- *\r
- */\r
-\r
-#include <stddef.h>\r
-#include <limits.h>\r
-#include <signal.h>\r
-\r
-/*\r
- * For gcc with _STDINT_H, fill in the PRINTF_INT*_MODIFIER macros, and\r
- * do nothing else. On the Mac OS X version of gcc this is _STDINT_H_.\r
- */\r
-\r
-#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)\r
-#include <stdint.h>\r
-#define _PSTDINT_H_INCLUDED\r
-# ifndef PRINTF_INT64_MODIFIER\r
-# define PRINTF_INT64_MODIFIER "ll"\r
-# endif\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER "l"\r
-# endif\r
-# ifndef PRINTF_INT16_MODIFIER\r
-# define PRINTF_INT16_MODIFIER "h"\r
-# endif\r
-# ifndef PRINTF_INTMAX_MODIFIER\r
-# define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER\r
-# endif\r
-# ifndef PRINTF_INT64_HEX_WIDTH\r
-# define PRINTF_INT64_HEX_WIDTH "16"\r
-# endif\r
-# ifndef PRINTF_INT32_HEX_WIDTH\r
-# define PRINTF_INT32_HEX_WIDTH "8"\r
-# endif\r
-# ifndef PRINTF_INT16_HEX_WIDTH\r
-# define PRINTF_INT16_HEX_WIDTH "4"\r
-# endif\r
-# ifndef PRINTF_INT8_HEX_WIDTH\r
-# define PRINTF_INT8_HEX_WIDTH "2"\r
-# endif\r
-# ifndef PRINTF_INT64_DEC_WIDTH\r
-# define PRINTF_INT64_DEC_WIDTH "20"\r
-# endif\r
-# ifndef PRINTF_INT32_DEC_WIDTH\r
-# define PRINTF_INT32_DEC_WIDTH "10"\r
-# endif\r
-# ifndef PRINTF_INT16_DEC_WIDTH\r
-# define PRINTF_INT16_DEC_WIDTH "5"\r
-# endif\r
-# ifndef PRINTF_INT8_DEC_WIDTH\r
-# define PRINTF_INT8_DEC_WIDTH "3"\r
-# endif\r
-# ifndef PRINTF_INTMAX_HEX_WIDTH\r
-# define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH\r
-# endif\r
-# ifndef PRINTF_INTMAX_DEC_WIDTH\r
-# define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH\r
-# endif\r
-\r
-/*\r
- * Something really weird is going on with Open Watcom. Just pull some of\r
- * these duplicated definitions from Open Watcom's stdint.h file for now.\r
- */\r
-\r
-# if defined (__WATCOMC__) && __WATCOMC__ >= 1250\r
-# if !defined (INT64_C)\r
-# define INT64_C(x) (x + (INT64_MAX - INT64_MAX))\r
-# endif\r
-# if !defined (UINT64_C)\r
-# define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX))\r
-# endif\r
-# if !defined (INT32_C)\r
-# define INT32_C(x) (x + (INT32_MAX - INT32_MAX))\r
-# endif\r
-# if !defined (UINT32_C)\r
-# define UINT32_C(x) (x + (UINT32_MAX - UINT32_MAX))\r
-# endif\r
-# if !defined (INT16_C)\r
-# define INT16_C(x) (x)\r
-# endif\r
-# if !defined (UINT16_C)\r
-# define UINT16_C(x) (x)\r
-# endif\r
-# if !defined (INT8_C)\r
-# define INT8_C(x) (x)\r
-# endif\r
-# if !defined (UINT8_C)\r
-# define UINT8_C(x) (x)\r
-# endif\r
-# if !defined (UINT64_MAX)\r
-# define UINT64_MAX 18446744073709551615ULL\r
-# endif\r
-# if !defined (INT64_MAX)\r
-# define INT64_MAX 9223372036854775807LL\r
-# endif\r
-# if !defined (UINT32_MAX)\r
-# define UINT32_MAX 4294967295UL\r
-# endif\r
-# if !defined (INT32_MAX)\r
-# define INT32_MAX 2147483647L\r
-# endif\r
-# if !defined (INTMAX_MAX)\r
-# define INTMAX_MAX INT64_MAX\r
-# endif\r
-# if !defined (INTMAX_MIN)\r
-# define INTMAX_MIN INT64_MIN\r
-# endif\r
-# endif\r
-#endif\r
-\r
-#ifndef _PSTDINT_H_INCLUDED\r
-#define _PSTDINT_H_INCLUDED\r
-\r
-#ifndef SIZE_MAX\r
-# define SIZE_MAX (~(size_t)0)\r
-#endif\r
-\r
-/*\r
- * Deduce the type assignments from limits.h under the assumption that\r
- * integer sizes in bits are powers of 2, and follow the ANSI\r
- * definitions.\r
- */\r
-\r
-#ifndef UINT8_MAX\r
-# define UINT8_MAX 0xff\r
-#endif\r
-#ifndef uint8_t\r
-# if (UCHAR_MAX == UINT8_MAX) || defined (S_SPLINT_S)\r
- typedef unsigned char uint8_t;\r
-# define UINT8_C(v) ((uint8_t) v)\r
-# else\r
-# error "Platform not supported"\r
-# endif\r
-#endif\r
-\r
-#ifndef INT8_MAX\r
-# define INT8_MAX 0x7f\r
-#endif\r
-#ifndef INT8_MIN\r
-# define INT8_MIN INT8_C(0x80)\r
-#endif\r
-#ifndef int8_t\r
-# if (SCHAR_MAX == INT8_MAX) || defined (S_SPLINT_S)\r
- typedef signed char int8_t;\r
-# define INT8_C(v) ((int8_t) v)\r
-# else\r
-# error "Platform not supported"\r
-# endif\r
-#endif\r
-\r
-#ifndef UINT16_MAX\r
-# define UINT16_MAX 0xffff\r
-#endif\r
-#ifndef uint16_t\r
-#if (UINT_MAX == UINT16_MAX) || defined (S_SPLINT_S)\r
- typedef unsigned int uint16_t;\r
-# ifndef PRINTF_INT16_MODIFIER\r
-# define PRINTF_INT16_MODIFIER ""\r
-# endif\r
-# define UINT16_C(v) ((uint16_t) (v))\r
-#elif (USHRT_MAX == UINT16_MAX)\r
- typedef unsigned short uint16_t;\r
-# define UINT16_C(v) ((uint16_t) (v))\r
-# ifndef PRINTF_INT16_MODIFIER\r
-# define PRINTF_INT16_MODIFIER "h"\r
-# endif\r
-#else\r
-#error "Platform not supported"\r
-#endif\r
-#endif\r
-\r
-#ifndef INT16_MAX\r
-# define INT16_MAX 0x7fff\r
-#endif\r
-#ifndef INT16_MIN\r
-# define INT16_MIN INT16_C(0x8000)\r
-#endif\r
-#ifndef int16_t\r
-#if (INT_MAX == INT16_MAX) || defined (S_SPLINT_S)\r
- typedef signed int int16_t;\r
-# define INT16_C(v) ((int16_t) (v))\r
-# ifndef PRINTF_INT16_MODIFIER\r
-# define PRINTF_INT16_MODIFIER ""\r
-# endif\r
-#elif (SHRT_MAX == INT16_MAX)\r
- typedef signed short int16_t;\r
-# define INT16_C(v) ((int16_t) (v))\r
-# ifndef PRINTF_INT16_MODIFIER\r
-# define PRINTF_INT16_MODIFIER "h"\r
-# endif\r
-#else\r
-#error "Platform not supported"\r
-#endif\r
-#endif\r
-\r
-#ifndef UINT32_MAX\r
-# define UINT32_MAX (0xffffffffUL)\r
-#endif\r
-#ifndef uint32_t\r
-#if (ULONG_MAX == UINT32_MAX) || defined (S_SPLINT_S)\r
- typedef unsigned long uint32_t;\r
-# define UINT32_C(v) v ## UL\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER "l"\r
-# endif\r
-#elif (UINT_MAX == UINT32_MAX)\r
- typedef unsigned int uint32_t;\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER ""\r
-# endif\r
-# define UINT32_C(v) v ## U\r
-#elif (USHRT_MAX == UINT32_MAX)\r
- typedef unsigned short uint32_t;\r
-# define UINT32_C(v) ((unsigned short) (v))\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER ""\r
-# endif\r
-#else\r
-#error "Platform not supported"\r
-#endif\r
-#endif\r
-\r
-#ifndef INT32_MAX\r
-# define INT32_MAX (0x7fffffffL)\r
-#endif\r
-#ifndef INT32_MIN\r
-# define INT32_MIN INT32_C(0x80000000)\r
-#endif\r
-#ifndef int32_t\r
-#if (LONG_MAX == INT32_MAX) || defined (S_SPLINT_S)\r
- typedef signed long int32_t;\r
-# define INT32_C(v) v ## L\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER "l"\r
-# endif\r
-#elif (INT_MAX == INT32_MAX)\r
- typedef signed int int32_t;\r
-# define INT32_C(v) v\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER ""\r
-# endif\r
-#elif (SHRT_MAX == INT32_MAX)\r
- typedef signed short int32_t;\r
-# define INT32_C(v) ((short) (v))\r
-# ifndef PRINTF_INT32_MODIFIER\r
-# define PRINTF_INT32_MODIFIER ""\r
-# endif\r
-#else\r
-#error "Platform not supported"\r
-#endif\r
-#endif\r
-\r
-/*\r
- * The macro stdint_int64_defined is temporarily used to record\r
- * whether or not 64 integer support is available. It must be\r
- * defined for any 64 integer extensions for new platforms that are\r
- * added.\r
- */\r
-\r
-#undef stdint_int64_defined\r
-#if (defined(__STDC__) && defined(__STDC_VERSION__)) || defined (S_SPLINT_S)\r
-# if (__STDC__ && __STDC_VERSION >= 199901L) || defined (S_SPLINT_S)\r
-# define stdint_int64_defined\r
- typedef long long int64_t;\r
- typedef unsigned long long uint64_t;\r
-# define UINT64_C(v) v ## ULL\r
-# define INT64_C(v) v ## LL\r
-# ifndef PRINTF_INT64_MODIFIER\r
-# define PRINTF_INT64_MODIFIER "ll"\r
-# endif\r
-# endif\r
-#endif\r
-\r
-#if !defined (stdint_int64_defined)\r
-# if defined(__GNUC__)\r
-# define stdint_int64_defined\r
- __extension__ typedef long long int64_t;\r
- __extension__ typedef unsigned long long uint64_t;\r
-# define UINT64_C(v) v ## ULL\r
-# define INT64_C(v) v ## LL\r
-# ifndef PRINTF_INT64_MODIFIER\r
-# define PRINTF_INT64_MODIFIER "ll"\r
-# endif\r
-# elif defined(__MWERKS__) || defined (__SUNPRO_C) || defined (__SUNPRO_CC) || defined (__APPLE_CC__) || defined (_LONG_LONG) || defined (_CRAYC) || defined (S_SPLINT_S)\r
-# define stdint_int64_defined\r
- typedef long long int64_t;\r
- typedef unsigned long long uint64_t;\r
-# define UINT64_C(v) v ## ULL\r
-# define INT64_C(v) v ## LL\r
-# ifndef PRINTF_INT64_MODIFIER\r
-# define PRINTF_INT64_MODIFIER "ll"\r
-# endif\r
-# elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined (__BORLANDC__) && __BORLANDC__ > 0x460) || defined (__alpha) || defined (__DECC)\r
-# define stdint_int64_defined\r
- typedef __int64 int64_t;\r
- typedef unsigned __int64 uint64_t;\r
-# define UINT64_C(v) v ## UI64\r
-# define INT64_C(v) v ## I64\r
-# ifndef PRINTF_INT64_MODIFIER\r
-# define PRINTF_INT64_MODIFIER "I64"\r
-# endif\r
-# endif\r
-#endif\r
-\r
-#if !defined (LONG_LONG_MAX) && defined (INT64_C)\r
-# define LONG_LONG_MAX INT64_C (9223372036854775807)\r
-#endif\r
-#ifndef ULONG_LONG_MAX\r
-# define ULONG_LONG_MAX UINT64_C (18446744073709551615)\r
-#endif\r
-\r
-#if !defined (INT64_MAX) && defined (INT64_C)\r
-# define INT64_MAX INT64_C (9223372036854775807)\r
-#endif\r
-#if !defined (INT64_MIN) && defined (INT64_C)\r
-# define INT64_MIN INT64_C (-9223372036854775808)\r
-#endif\r
-#if !defined (UINT64_MAX) && defined (INT64_C)\r
-# define UINT64_MAX UINT64_C (18446744073709551615)\r
-#endif\r
-\r
-/*\r
- * Width of hexadecimal for number field.\r
- */\r
-\r
-#ifndef PRINTF_INT64_HEX_WIDTH\r
-# define PRINTF_INT64_HEX_WIDTH "16"\r
-#endif\r
-#ifndef PRINTF_INT32_HEX_WIDTH\r
-# define PRINTF_INT32_HEX_WIDTH "8"\r
-#endif\r
-#ifndef PRINTF_INT16_HEX_WIDTH\r
-# define PRINTF_INT16_HEX_WIDTH "4"\r
-#endif\r
-#ifndef PRINTF_INT8_HEX_WIDTH\r
-# define PRINTF_INT8_HEX_WIDTH "2"\r
-#endif\r
-\r
-#ifndef PRINTF_INT64_DEC_WIDTH\r
-# define PRINTF_INT64_DEC_WIDTH "20"\r
-#endif\r
-#ifndef PRINTF_INT32_DEC_WIDTH\r
-# define PRINTF_INT32_DEC_WIDTH "10"\r
-#endif\r
-#ifndef PRINTF_INT16_DEC_WIDTH\r
-# define PRINTF_INT16_DEC_WIDTH "5"\r
-#endif\r
-#ifndef PRINTF_INT8_DEC_WIDTH\r
-# define PRINTF_INT8_DEC_WIDTH "3"\r
-#endif\r
-\r
-/*\r
- * Ok, lets not worry about 128 bit integers for now. Moore's law says\r
- * we don't need to worry about that until about 2040 at which point\r
- * we'll have bigger things to worry about.\r
- */\r
-\r
-#ifdef stdint_int64_defined\r
- typedef int64_t intmax_t;\r
- typedef uint64_t uintmax_t;\r
-# define INTMAX_MAX INT64_MAX\r
-# define INTMAX_MIN INT64_MIN\r
-# define UINTMAX_MAX UINT64_MAX\r
-# define UINTMAX_C(v) UINT64_C(v)\r
-# define INTMAX_C(v) INT64_C(v)\r
-# ifndef PRINTF_INTMAX_MODIFIER\r
-# define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER\r
-# endif\r
-# ifndef PRINTF_INTMAX_HEX_WIDTH\r
-# define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH\r
-# endif\r
-# ifndef PRINTF_INTMAX_DEC_WIDTH\r
-# define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH\r
-# endif\r
-#else\r
- typedef int32_t intmax_t;\r
- typedef uint32_t uintmax_t;\r
-# define INTMAX_MAX INT32_MAX\r
-# define UINTMAX_MAX UINT32_MAX\r
-# define UINTMAX_C(v) UINT32_C(v)\r
-# define INTMAX_C(v) INT32_C(v)\r
-# ifndef PRINTF_INTMAX_MODIFIER\r
-# define PRINTF_INTMAX_MODIFIER PRINTF_INT32_MODIFIER\r
-# endif\r
-# ifndef PRINTF_INTMAX_HEX_WIDTH\r
-# define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT32_HEX_WIDTH\r
-# endif\r
-# ifndef PRINTF_INTMAX_DEC_WIDTH\r
-# define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT32_DEC_WIDTH\r
-# endif\r
-#endif\r
-\r
-/*\r
- * Because this file currently only supports platforms which have\r
- * precise powers of 2 as bit sizes for the default integers, the\r
- * least definitions are all trivial. Its possible that a future\r
- * version of this file could have different definitions.\r
- */\r
-\r
-#ifndef stdint_least_defined\r
- typedef int8_t int_least8_t;\r
- typedef uint8_t uint_least8_t;\r
- typedef int16_t int_least16_t;\r
- typedef uint16_t uint_least16_t;\r
- typedef int32_t int_least32_t;\r
- typedef uint32_t uint_least32_t;\r
-# define PRINTF_LEAST32_MODIFIER PRINTF_INT32_MODIFIER\r
-# define PRINTF_LEAST16_MODIFIER PRINTF_INT16_MODIFIER\r
-# define UINT_LEAST8_MAX UINT8_MAX\r
-# define INT_LEAST8_MAX INT8_MAX\r
-# define UINT_LEAST16_MAX UINT16_MAX\r
-# define INT_LEAST16_MAX INT16_MAX\r
-# define UINT_LEAST32_MAX UINT32_MAX\r
-# define INT_LEAST32_MAX INT32_MAX\r
-# define INT_LEAST8_MIN INT8_MIN\r
-# define INT_LEAST16_MIN INT16_MIN\r
-# define INT_LEAST32_MIN INT32_MIN\r
-# ifdef stdint_int64_defined\r
- typedef int64_t int_least64_t;\r
- typedef uint64_t uint_least64_t;\r
-# define PRINTF_LEAST64_MODIFIER PRINTF_INT64_MODIFIER\r
-# define UINT_LEAST64_MAX UINT64_MAX\r
-# define INT_LEAST64_MAX INT64_MAX\r
-# define INT_LEAST64_MIN INT64_MIN\r
-# endif\r
-#endif\r
-#undef stdint_least_defined\r
-\r
-/*\r
- * The ANSI C committee pretending to know or specify anything about\r
- * performance is the epitome of misguided arrogance. The mandate of\r
- * this file is to *ONLY* ever support that absolute minimum\r
- * definition of the fast integer types, for compatibility purposes.\r
- * No extensions, and no attempt to suggest what may or may not be a\r
- * faster integer type will ever be made in this file. Developers are\r
- * warned to stay away from these types when using this or any other\r
- * stdint.h.\r
- */\r
-\r
-typedef int_least8_t int_fast8_t;\r
-typedef uint_least8_t uint_fast8_t;\r
-typedef int_least16_t int_fast16_t;\r
-typedef uint_least16_t uint_fast16_t;\r
-typedef int_least32_t int_fast32_t;\r
-typedef uint_least32_t uint_fast32_t;\r
-#define UINT_FAST8_MAX UINT_LEAST8_MAX\r
-#define INT_FAST8_MAX INT_LEAST8_MAX\r
-#define UINT_FAST16_MAX UINT_LEAST16_MAX\r
-#define INT_FAST16_MAX INT_LEAST16_MAX\r
-#define UINT_FAST32_MAX UINT_LEAST32_MAX\r
-#define INT_FAST32_MAX INT_LEAST32_MAX\r
-#define INT_FAST8_MIN INT_LEAST8_MIN\r
-#define INT_FAST16_MIN INT_LEAST16_MIN\r
-#define INT_FAST32_MIN INT_LEAST32_MIN\r
-#ifdef stdint_int64_defined\r
- typedef int_least64_t int_fast64_t;\r
- typedef uint_least64_t uint_fast64_t;\r
-# define UINT_FAST64_MAX UINT_LEAST64_MAX\r
-# define INT_FAST64_MAX INT_LEAST64_MAX\r
-# define INT_FAST64_MIN INT_LEAST64_MIN\r
-#endif\r
-\r
-#undef stdint_int64_defined\r
-\r
-/*\r
- * Whatever piecemeal, per compiler thing we can do about the wchar_t\r
- * type limits.\r
- */\r
-\r
-#if defined(__WATCOMC__) || defined(_MSC_VER) || defined (__GNUC__)\r
-# include <wchar.h>\r
-# ifndef WCHAR_MIN\r
-# define WCHAR_MIN 0\r
-# endif\r
-# ifndef WCHAR_MAX\r
-# define WCHAR_MAX ((wchar_t)-1)\r
-# endif\r
-#endif\r
-\r
-/*\r
- * Whatever piecemeal, per compiler/platform thing we can do about the\r
- * (u)intptr_t types and limits.\r
- */\r
-\r
-#if defined (_MSC_VER) && defined (_UINTPTR_T_DEFINED)\r
-# define STDINT_H_UINTPTR_T_DEFINED\r
-#endif\r
-\r
-#ifndef STDINT_H_UINTPTR_T_DEFINED\r
-# if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (_WIN64)\r
-# define stdint_intptr_bits 64\r
-# elif defined (__WATCOMC__) || defined (__TURBOC__)\r
-# if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)\r
-# define stdint_intptr_bits 16\r
-# else\r
-# define stdint_intptr_bits 32\r
-# endif\r
-# elif defined (__i386__) || defined (_WIN32) || defined (WIN32)\r
-# define stdint_intptr_bits 32\r
-# elif defined (__INTEL_COMPILER)\r
-/* TODO -- what will Intel do about x86-64? */\r
-# endif\r
-\r
-# ifdef stdint_intptr_bits\r
-# define stdint_intptr_glue3_i(a,b,c) a##b##c\r
-# define stdint_intptr_glue3(a,b,c) stdint_intptr_glue3_i(a,b,c)\r
-# ifndef PRINTF_INTPTR_MODIFIER\r
-# define PRINTF_INTPTR_MODIFIER stdint_intptr_glue3(PRINTF_INT,stdint_intptr_bits,_MODIFIER)\r
-# endif\r
-# ifndef PTRDIFF_MAX\r
-# define PTRDIFF_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX)\r
-# endif\r
-# ifndef PTRDIFF_MIN\r
-# define PTRDIFF_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN)\r
-# endif\r
-# ifndef UINTPTR_MAX\r
-# define UINTPTR_MAX stdint_intptr_glue3(UINT,stdint_intptr_bits,_MAX)\r
-# endif\r
-# ifndef INTPTR_MAX\r
-# define INTPTR_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX)\r
-# endif\r
-# ifndef INTPTR_MIN\r
-# define INTPTR_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN)\r
-# endif\r
-# ifndef INTPTR_C\r
-# define INTPTR_C(x) stdint_intptr_glue3(INT,stdint_intptr_bits,_C)(x)\r
-# endif\r
-# ifndef UINTPTR_C\r
-# define UINTPTR_C(x) stdint_intptr_glue3(UINT,stdint_intptr_bits,_C)(x)\r
-# endif\r
- typedef stdint_intptr_glue3(uint,stdint_intptr_bits,_t) uintptr_t;\r
- typedef stdint_intptr_glue3( int,stdint_intptr_bits,_t) intptr_t;\r
-# else\r
-/* TODO -- This following is likely wrong for some platforms, and does\r
- nothing for the definition of uintptr_t. */\r
- typedef ptrdiff_t intptr_t;\r
-# endif\r
-# define STDINT_H_UINTPTR_T_DEFINED\r
-#endif\r
-\r
-/*\r
- * Assumes sig_atomic_t is signed and we have a 2s complement machine.\r
- */\r
-\r
-#ifndef SIG_ATOMIC_MAX\r
-# define SIG_ATOMIC_MAX ((((sig_atomic_t) 1) << (sizeof (sig_atomic_t)*CHAR_BIT-1)) - 1)\r
-#endif\r
-\r
-#endif\r
-\r
-#if defined (__TEST_PSTDINT_FOR_CORRECTNESS)\r
-\r
-/* \r
- * Please compile with the maximum warning settings to make sure macros are not\r
- * defined more than once.\r
- */\r
- \r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include <string.h>\r
- \r
-#define glue3_aux(x,y,z) x ## y ## z\r
-#define glue3(x,y,z) glue3_aux(x,y,z)\r
-\r
-#define DECLU(bits) glue3(uint,bits,_t) glue3(u,bits,=) glue3(UINT,bits,_C) (0);\r
-#define DECLI(bits) glue3(int,bits,_t) glue3(i,bits,=) glue3(INT,bits,_C) (0);\r
-\r
-#define DECL(us,bits) glue3(DECL,us,) (bits)\r
-\r
-#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)\r
- \r
-int main () {\r
- DECL(I,8)\r
- DECL(U,8)\r
- DECL(I,16)\r
- DECL(U,16)\r
- DECL(I,32)\r
- DECL(U,32)\r
-#ifdef INT64_MAX\r
- DECL(I,64)\r
- DECL(U,64)\r
-#endif\r
- intmax_t imax = INTMAX_C(0);\r
- uintmax_t umax = UINTMAX_C(0);\r
- char str0[256], str1[256];\r
-\r
- sprintf (str0, "%d %x\n", 0, ~0);\r
- \r
- sprintf (str1, "%d %x\n", i8, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1);\r
- sprintf (str1, "%u %x\n", u8, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with u8 : %s\n", str1);\r
- sprintf (str1, "%d %x\n", i16, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1);\r
- sprintf (str1, "%u %x\n", u16, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1); \r
- sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1);\r
- sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1);\r
-#ifdef INT64_MAX \r
- sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1);\r
-#endif\r
- sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1);\r
- sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0);\r
- if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1); \r
- \r
- TESTUMAX(8);\r
- TESTUMAX(16);\r
- TESTUMAX(32);\r
-#ifdef INT64_MAX\r
- TESTUMAX(64);\r
-#endif\r
-\r
- return EXIT_SUCCESS;\r
-}\r
-\r
-#endif\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <cassert>\r
-#include <vector>\r
-#include <algorithm>\r
-#include <boost/foreach.hpp>\r
-#include <windows.h>\r
-#include <TlHelp32.h>\r
-\r
-#include "thread.h"\r
-\r
-namespace MinHook { namespace\r
-{\r
- // \8e©\93®\93I\82ÉCloseHandle\82³\82ê\82éWindows\83n\83\93\83h\83\8b\r
- class ScopedHandle : boost::noncopyable\r
- {\r
- private:\r
- HANDLE handle_;\r
- public:\r
- ScopedHandle(HANDLE handle)\r
- : handle_(handle)\r
- {\r
- }\r
-\r
- ~ScopedHandle()\r
- {\r
- CloseHandle(handle_);\r
- }\r
-\r
- operator HANDLE() const\r
- {\r
- return handle_;\r
- }\r
- };\r
-\r
-}}\r
-\r
-// CriticalSection, CriticalSection::ScopedLock \82Ì\8eÀ\91\95\r
-namespace MinHook\r
-{\r
- CriticalSection::CriticalSection()\r
- {\r
- InitializeCriticalSection(&cs_);\r
- }\r
-\r
- CriticalSection::~CriticalSection()\r
- {\r
- DeleteCriticalSection(&cs_);\r
- }\r
-\r
- void CriticalSection::enter()\r
- {\r
- EnterCriticalSection(&cs_);\r
- }\r
-\r
- void CriticalSection::leave()\r
- {\r
- LeaveCriticalSection(&cs_);\r
- }\r
-\r
- CriticalSection::ScopedLock::ScopedLock(CriticalSection& cs)\r
- : cs_(cs)\r
- {\r
- cs_.enter();\r
- }\r
-\r
- CriticalSection::ScopedLock::~ScopedLock()\r
- {\r
- cs_.leave();\r
- }\r
-}\r
-\r
-// ScopedThreadExclusive \82Ì\8eÀ\91\95\r
-namespace MinHook\r
-{\r
- ScopedThreadExclusive::ScopedThreadExclusive(const std::vector<uintptr_t>& oldIPs, const std::vector<uintptr_t>& newIPs)\r
- {\r
- assert(("ScopedThreadExclusive::ctor", (oldIPs.size() == newIPs.size())));\r
-\r
- GetThreads(threads_);\r
- Freeze(threads_, oldIPs, newIPs);\r
- }\r
-\r
- ScopedThreadExclusive::~ScopedThreadExclusive()\r
- {\r
- Unfreeze(threads_);\r
- }\r
-\r
- void ScopedThreadExclusive::GetThreads(std::vector<DWORD>& threads)\r
- {\r
- ScopedHandle hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);\r
- if (hSnapshot == INVALID_HANDLE_VALUE)\r
- {\r
- return;\r
- }\r
- \r
- THREADENTRY32 te = { sizeof(te) };\r
- if (Thread32First(hSnapshot, &te))\r
- {\r
- do \r
- {\r
- if (te.th32OwnerProcessID == GetCurrentProcessId()\r
- && te.th32ThreadID != GetCurrentThreadId())\r
- {\r
- threads.push_back(te.th32ThreadID);\r
- }\r
- } \r
- while (Thread32Next(hSnapshot, &te));\r
- }\r
- }\r
-\r
- void ScopedThreadExclusive::Freeze(\r
- const std::vector<DWORD>& threads, const std::vector<uintptr_t>& oldIPs, const std::vector<uintptr_t>& newIPs)\r
- {\r
- assert(("ScopedThreadExclusive::freeze", (oldIPs.size() == newIPs.size())));\r
-\r
- static const DWORD ThreadAccess \r
- = THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION | THREAD_SET_CONTEXT;\r
- \r
- BOOST_FOREACH (const DWORD& tid, threads)\r
- {\r
- ScopedHandle hThread = OpenThread(ThreadAccess, FALSE, tid);\r
- SuspendThread(hThread);\r
-\r
- // \8f\91\82«\8a·\82¦\94Í\88Í\93à\82Å\83X\83\8c\83b\83h\82ª\92â\8e~\82µ\82½\8fê\8d\87\82Í\81A\83g\83\89\83\93\83|\83\8a\83\93\8aÖ\90\94\82É\90§\8cä\82ð\88Ú\82·\r
- CONTEXT c = { 0 };\r
- c.ContextFlags = CONTEXT_CONTROL;\r
- if (!GetThreadContext(hThread, &c))\r
- {\r
- return;\r
- }\r
-\r
-#if defined _M_X64\r
- DWORD64& ip = c.Rip;\r
-#elif defined _M_IX86\r
- DWORD& ip = c.Eip;\r
-#endif\r
- for (size_t i = 0; i < oldIPs.size(); ++i)\r
- {\r
- if (ip == oldIPs[ i ])\r
- {\r
- ip = newIPs[ i ];\r
- break;\r
- }\r
- }\r
-\r
- SetThreadContext(hThread, &c);\r
- }\r
- }\r
-\r
- void ScopedThreadExclusive::Unfreeze(const std::vector<DWORD>& threads)\r
- {\r
- BOOST_FOREACH (const DWORD& tid, threads)\r
- {\r
- ScopedHandle hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, tid);\r
- ResumeThread(hThread);\r
- }\r
- }\r
-}\r
-\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#pragma once\r
-\r
-#include <vector>\r
-#include <boost/utility.hpp>\r
-#include <windows.h>\r
-\r
-#include "trampoline.h"\r
-\r
-namespace MinHook\r
-{\r
- // ScopedLock \95t\82«\83N\83\8a\83e\83B\83J\83\8b\83Z\83N\83V\83\87\83\93\r
- class CriticalSection : boost::noncopyable\r
- {\r
- public:\r
- class ScopedLock : boost::noncopyable\r
- {\r
- private:\r
- CriticalSection& cs_;\r
- public:\r
- ScopedLock(CriticalSection& cs);\r
- ~ScopedLock();\r
- };\r
-\r
- private:\r
- CRITICAL_SECTION cs_;\r
- public:\r
- CriticalSection();\r
- ~CriticalSection();\r
- void enter();\r
- void leave();\r
- };\r
-\r
- // \93¯\88ê\83v\83\8d\83Z\83X\93à\82Ì\91¼\82Ì\83X\83\8c\83b\83h\82ð\82·\82×\82Ä\92â\8e~\r
- class ScopedThreadExclusive\r
- {\r
- private:\r
- std::vector<DWORD> threads_;\r
- public:\r
- ScopedThreadExclusive(const std::vector<uintptr_t>& oldIPs, const std::vector<uintptr_t>& newIPs);\r
- ~ScopedThreadExclusive();\r
- private:\r
- static void GetThreads(std::vector<DWORD>& threads);\r
- static void Freeze(\r
- const std::vector<DWORD>& threads, const std::vector<uintptr_t>& oldIPs, const std::vector<uintptr_t>& newIPs);\r
- static void Unfreeze(const std::vector<DWORD>& threads);\r
- };\r
-}\r
-\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <cassert>\r
-#include <vector>\r
-#include <boost/foreach.hpp>\r
-#include "pstdint.h"\r
-\r
-#if defined _M_X64\r
-#include "hde64/include/hde64.h"\r
-#elif defined _M_IX86\r
-#include "hde32/hde32.h"\r
-#endif\r
-\r
-#include "trampoline.h"\r
-\r
-namespace MinHook { namespace\r
-{\r
-#if defined _M_X64\r
- typedef hde64s hde_t;\r
- inline unsigned int hde_disasm(const void* code, hde_t* hs) { return hde64_disasm(code, hs); }\r
-#elif defined _M_IX86\r
- typedef hde32s hde_t;\r
- inline unsigned int hde_disasm(const void* code, hde_t* hs) { return hde32_disasm(code, hs); }\r
-#endif\r
-\r
- // \96½\97ß\8f\91\82«\8d\9e\82Ý\97p\8d\\91¢\91Ì\r
-#pragma pack(push, 1)\r
- struct JMP_REL\r
- {\r
- uint8_t opcode;\r
- uint32_t operand;\r
- };\r
- typedef JMP_REL CALL_REL;\r
-\r
- struct JMP_ABS\r
- {\r
- uint16_t opcode;\r
- uint32_t operand;\r
- };\r
- typedef JMP_ABS CALL_ABS, JCC_REL;\r
-\r
- // \8aÔ\90Ú\90â\91ÎNEAR Jcc\82É\91\8a\93\96\82·\82é\83\8d\83W\83b\83N\r
- struct JCC_ABS\r
- {\r
- uint8_t opcode; // 7* 02 J** +4 \r
- uint8_t dummy0;\r
- uint8_t dummy1; // EB 06 JMP +8\r
- uint8_t dummy2;\r
- uint16_t dummy3; // FF25 xxxxxxxx JMP [RIP+xxxxxxxx]\r
- uint32_t operand;\r
- };\r
-#pragma pack(pop)\r
-\r
- uintptr_t GetRelativeBranchDestination(uint8_t* pInst, const hde_t& hs);\r
- inline bool IsInternalJump(void* pTarget, uintptr_t dest);\r
- template <typename T>\r
- void AppendTempAddress(uintptr_t address, size_t pos, const T& inst, CREATE_TREMPOLINE_T& ct);\r
-#if defined _M_X64\r
- void AppendRipRelativeAddress(uint8_t* pInst, size_t pos, const hde_t& hs, CREATE_TREMPOLINE_T& ct);\r
-#endif\r
- inline void SetJccOpcode(const hde_t& hs, JCC_REL& inst);\r
- inline void SetJccOpcode(const hde_t& hs, JCC_ABS& inst);\r
-}}\r
-\r
-namespace MinHook\r
-{\r
- bool CreateTrampolineFunction(CREATE_TREMPOLINE_T& ct)\r
- {\r
- assert(("CreateTrampolineFunction", ct.pTarget != NULL));\r
-\r
-#if defined _M_X64\r
- CALL_ABS call = { 0x15FF, 0x00000000 };\r
- JMP_ABS jmp = { 0x25FF, 0x00000000 };\r
- JCC_ABS jcc = { 0x70, 0x02, 0xEB, 0x06, 0x25FF, 0x00000000 };\r
-#elif defined _M_IX86\r
- CALL_REL call = { 0xE8, 0x00000000 };\r
- JMP_REL jmp = { 0xE9, 0x00000000 };\r
- JCC_REL jcc = { 0x800F, 0x00000000 };\r
-#endif\r
-\r
- size_t oldPos = 0;\r
- size_t newPos = 0;\r
- uintptr_t jmpDest = 0; // \8aÖ\90\94\93à\83W\83\83\83\93\83v\82Ì\94ò\82Ñ\90æ\83A\83h\83\8c\83X\81i\95ª\8aò\92\86\94»\92è\82É\8eg\97p\81j\r
- bool finished = false; // \8aÖ\90\94\8fI\97¹\83t\83\89\83O\r
- while (!finished)\r
- {\r
- uint8_t *pInst = reinterpret_cast<uint8_t*>(ct.pTarget) + oldPos;\r
- hde_t hs;\r
- hde_disasm(pInst, &hs);\r
- if ((hs.flags & F_ERROR) == F_ERROR)\r
- {\r
- return false;\r
- }\r
-\r
- void* pCopySrc = pInst;\r
- size_t copySize = hs.len;\r
-\r
- if (pInst - reinterpret_cast<uint8_t*>(ct.pTarget) >= sizeof(JMP_REL))\r
- {\r
- // \83^\81[\83Q\83b\83g\8aÖ\90\94\82Ö\82Ì\83W\83\83\83\93\83v\82ð\8f\91\82«\8d\9e\82Ý\81A\8aÖ\90\94\82ð\8fI\97¹\r
- AppendTempAddress(reinterpret_cast<uintptr_t>(pInst), newPos, jmp, ct);\r
-\r
- pCopySrc = &jmp;\r
- copySize = sizeof(jmp);\r
-\r
- finished = true;\r
- }\r
-#if defined _M_X64\r
- // RIP\91\8a\91Î\83A\83h\83\8c\83b\83V\83\93\83O\82ð\8eg\97p\82µ\82Ä\82¢\82é\96½\97ß (ModR/M = 00???101B)\r
- else if ((hs.modrm & 0xC7) == 0x05)\r
- {\r
- // RIP\91\8a\91Î\83A\83h\83\8c\83X\82Ì\82Ý\8f\91\82«\8a·\82¦\r
- AppendRipRelativeAddress(pInst, newPos, hs, ct);\r
-\r
- // JMP (FF /4)\82È\82ç\8aÖ\90\94\82ð\8fI\97¹\r
- if (hs.opcode == 0xFF && hs.modrm_reg == 4) \r
- {\r
- finished = true;\r
- }\r
- }\r
-#endif\r
- // \91\8a\91Î\92¼\90ÚCALL\r
- else if (hs.opcode == 0xE8)\r
- {\r
- AppendTempAddress(GetRelativeBranchDestination(pInst, hs), newPos, call, ct);\r
- pCopySrc = &call;\r
- copySize = sizeof(call);\r
- }\r
- // \91\8a\91Î\92¼\90ÚJMP (EB or E9)\r
- else if ((hs.opcode & 0xFD) == 0xE9)\r
- {\r
- uintptr_t dest = GetRelativeBranchDestination(pInst, hs);\r
-\r
- // \8aÖ\90\94\93à\82Ö\82Ì\83W\83\83\83\93\83v\82Í\82»\82Ì\82Ü\82Ü\83R\83s\81[\81i\83W\83\83\83\93\83v\92\86\82Í\96½\97ß\92·\82ª\95Ï\82í\82é\82æ\82¤\82È\91\80\8dì\82Í\95s\89Â\81j\r
- if (IsInternalJump(ct.pTarget, dest))\r
- {\r
- jmpDest = std::max<uintptr_t>(jmpDest, dest);\r
- }\r
- else\r
- {\r
- AppendTempAddress(dest, newPos, jmp, ct);\r
- pCopySrc = &jmp;\r
- copySize = sizeof(jmp);\r
-\r
- // \95ª\8aò\92\86\82Å\82È\82¯\82ê\82Î\8aÖ\90\94\82ð\8fI\97¹\r
- finished = (reinterpret_cast<uintptr_t>(pInst) >= jmpDest);\r
- }\r
- }\r
- // \91\8a\91Î\92¼\90ÚJcc\r
- else if ((hs.opcode & 0xF0) == 0x70 || hs.opcode == 0xE3 || (hs.opcode2 & 0xF0) == 0x80)\r
- {\r
- uintptr_t dest = GetRelativeBranchDestination(pInst, hs);\r
-\r
- // \8aÖ\90\94\93à\82Ö\82Ì\83W\83\83\83\93\83v\82Í\82»\82Ì\82Ü\82Ü\83R\83s\81[\81i\95ª\8aò\92\86\82Í\96½\97ß\92·\82ª\95Ï\82í\82é\82æ\82¤\82È\91\80\8dì\82Í\95s\89Â\81j\r
- if (IsInternalJump(ct.pTarget, dest))\r
- {\r
- jmpDest = std::max<uintptr_t>(jmpDest, dest);\r
- }\r
- else if (hs.opcode == 0xE3) // \8aÖ\90\94\8aO\82Ö\82ÌJCXZ, JECXZ \82É\82Í\91Î\89\9e\82µ\82È\82¢ \r
- {\r
- return false;\r
- }\r
- else\r
- {\r
- AppendTempAddress(dest, newPos, jcc, ct);\r
- SetJccOpcode(hs, jcc);\r
- pCopySrc = &jcc;\r
- copySize = sizeof(jcc);\r
- }\r
- }\r
- // RET (C2 or C3)\r
- else if ((hs.opcode & 0xFE) == 0xC2)\r
- {\r
- // \95ª\8aò\92\86\82Å\82È\82¯\82ê\82Î\83g\83\89\83\93\83|\83\8a\83\93\8aÖ\90\94\82ð\8fI\97¹\r
- finished = (reinterpret_cast<uintptr_t>(pInst) >= jmpDest);\r
- }\r
-\r
- // \95ª\8aò\92\86\82Í\96½\97ß\92·\82ª\95Ï\82í\82é\82æ\82¤\82È\91\80\8dì\82Í\95s\89Â\r
- if (reinterpret_cast<uintptr_t>(pInst) < jmpDest && copySize != hs.len)\r
- {\r
- return false;\r
- }\r
-\r
- ct.trampoline.resize(newPos + copySize);\r
- memcpy(&ct.trampoline[ newPos ], pCopySrc, copySize);\r
-\r
- ct.oldIPs.push_back(oldPos);\r
- oldPos += hs.len;\r
- ct.newIPs.push_back(newPos);\r
- newPos += copySize;\r
- }\r
-\r
- return true;\r
- }\r
-\r
- bool ResolveTemporaryAddresses(CREATE_TREMPOLINE_T& ct)\r
- {\r
- assert(("ResolveTemporaryAddresses", ct.pTrampoline != NULL));\r
-#if defined _M_X64\r
- assert(("ResolveTemporaryAddresses", (ct.table.empty() || ct.pTable != NULL)));\r
-#endif\r
-\r
-#if defined _M_X64\r
- uintptr_t* pt = reinterpret_cast<uintptr_t*>(ct.pTable); \r
-#endif\r
- BOOST_FOREACH (const TEMP_ADDR& ta, ct.tempAddr)\r
- {\r
- if (ta.position > ct.trampoline.size() - sizeof(uint32_t))\r
- {\r
- return false;\r
- }\r
-\r
- uintptr_t addr;\r
-#if defined _M_X64\r
- if (ta.address < 0x10000) // 0x10000\96¢\96\9e\82Í\83e\81[\83u\83\8b\82Ì\83C\83\93\83f\83b\83N\83X\81A0x10000\88È\8fã\82ÍRIP\91\8a\91Î\83A\83h\83\8c\83X\r
- {\r
- addr = reinterpret_cast<uintptr_t>(pt++);\r
- }\r
- else\r
-#endif\r
- {\r
- addr = ta.address;\r
- }\r
-\r
- *reinterpret_cast<uint32_t*>(&ct.trampoline[ ta.position ]) \r
- = static_cast<uint32_t>(addr - (reinterpret_cast<uintptr_t>(ct.pTrampoline) + ta.pc));\r
- }\r
-\r
- for (size_t i = 0; i < ct.oldIPs.size(); ++i)\r
- {\r
- ct.oldIPs[ i ] += reinterpret_cast<uintptr_t>(ct.pTarget);\r
- ct.newIPs[ i ] += reinterpret_cast<uintptr_t>(ct.pTrampoline);\r
- }\r
-\r
- return true;\r
- }\r
-}\r
-\r
-namespace MinHook { namespace\r
-{\r
- inline uintptr_t GetRelativeBranchDestination(uint8_t* pInst, const hde_t& hs)\r
- {\r
- return reinterpret_cast<uintptr_t>(pInst) + hs.len + static_cast<int32_t>(hs.imm.imm32);\r
- }\r
- \r
- inline bool IsInternalJump(void* pTarget, uintptr_t dest)\r
- {\r
- uintptr_t pt = reinterpret_cast<uintptr_t>(pTarget);\r
- return (pt <= dest && dest <= pt + sizeof(JMP_REL));\r
- }\r
-\r
- template <typename T>\r
- void AppendTempAddress(uintptr_t address, size_t pos, const T& inst, CREATE_TREMPOLINE_T& ct)\r
- {\r
- TEMP_ADDR ta;\r
-#if defined _M_X64\r
- ta.address = ct.table.size();\r
- ct.table.push_back(address);\r
-#elif defined _M_IX86\r
- ta.address = address;\r
-#endif\r
- ta.position = pos + (reinterpret_cast<uintptr_t>(&inst.operand) - reinterpret_cast<uintptr_t>(&inst));\r
- ta.pc = pos + sizeof(inst);\r
-\r
- ct.tempAddr.push_back(ta);\r
- }\r
-\r
-#if defined _M_X64\r
- void AppendRipRelativeAddress(uint8_t* pInst, size_t pos, const hde_t& hs, CREATE_TREMPOLINE_T& ct)\r
- {\r
- TEMP_ADDR ta;\r
- ta.address = reinterpret_cast<uintptr_t>(pInst) + hs.len + static_cast<int32_t>(hs.disp.disp32);\r
- ta.position = pos + hs.len - ((hs.flags & 0x3C) >> 2) - 4; // pos + \96½\97ß\92· - \91¦\92l\83T\83C\83Y - 4\r
- ta.pc = pos + hs.len;\r
-\r
- ct.tempAddr.push_back(ta);\r
- }\r
-#endif\r
-\r
- inline void SetJccOpcode(const hde_t& hs, JCC_REL& inst)\r
- {\r
- uint8_t n = ((hs.opcode != 0x0F ? hs.opcode : hs.opcode2) & 0x0F);\r
- inst.opcode = 0x800F | (n << 8);\r
- }\r
-\r
- inline void SetJccOpcode(const hde_t& hs, JCC_ABS& inst)\r
- {\r
- uint8_t n = ((hs.opcode != 0x0F ? hs.opcode : hs.opcode2) & 0x0F);\r
- inst.opcode = 0x70 | n;\r
- }\r
-}}\r
-\r
-\r
+++ /dev/null
-/* \r
- * MinHook - Minimalistic API Hook Library \r
- * Copyright (C) 2009 Tsuda Kageyu. All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#pragma once\r
-\r
-#include <vector>\r
-\r
-namespace MinHook\r
-{\r
- struct TEMP_ADDR\r
- {\r
- uintptr_t address;\r
- size_t position;\r
- size_t pc;\r
- };\r
-\r
- struct CREATE_TREMPOLINE_T\r
- {\r
- void* pTarget; \r
- void* pTrampoline; \r
- std::vector<char> trampoline;\r
- std::vector<TEMP_ADDR> tempAddr;\r
-#if defined _M_X64\r
- void* pTable;\r
- std::vector<uintptr_t> table;\r
-#endif\r
- std::vector<uintptr_t> oldIPs;\r
- std::vector<uintptr_t> newIPs;\r
- };\r
-\r
- bool CreateTrampolineFunction(CREATE_TREMPOLINE_T& ct);\r
- bool ResolveTemporaryAddresses(CREATE_TREMPOLINE_T& ct);\r
-}
\ No newline at end of file