Browse Source

Switch MHook to MinHook.

MinHook is maintained unlike MHook. It is also used in other popular
open source software.
Kacper Michajłow 2 years ago
parent
commit
323a2acca3

+ 3
- 0
.gitmodules View File

@@ -38,3 +38,6 @@
38 38
 [submodule "ZenLib"]
39 39
     path = src/thirdparty/ZenLib/library
40 40
     url = https://github.com/MediaArea/ZenLib.git
41
+[submodule "src/thirdparty/minhook/minhook"]
42
+    path = src/thirdparty/minhook/minhook
43
+    url = https://github.com/TsudaKageyu/minhook.git

+ 1
- 1
docs/Readme.txt View File

@@ -59,7 +59,7 @@ libdivide           zlib License                http://libdivide.com/
59 59
 Little CMS          MIT License                 http://www.littlecms.com/
60 60
 Logitech SDK        -                           -
61 61
 MediaInfoLib        Simplified BSD License      https://mediaarea.net/MediaInfo
62
-Mhook               MIT License                 https://github.com/martona/mhook
62
+MinHook             Simplified BSD License      https://github.com/TsudaKageyu/minhook
63 63
 MultiMon            CPOL                        http://www.codeproject.com/Articles/3690/
64 64
 NanoSVG             zlib License                https://github.com/memononen/nanosvg
65 65
 QuickTime SDK       -                           http://developer.apple.com/quicktime/

+ 28
- 28
mpc-hc.sln View File

@@ -1,6 +1,6 @@
1 1
 Microsoft Visual Studio Solution File, Format Version 12.00
2 2
 # Visual Studio 14
3
-VisualStudioVersion = 14.0.23107.0
3
+VisualStudioVersion = 14.0.25420.1
4 4
 MinimumVisualStudioVersion = 10.0.40219.1
5 5
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Apps", "Apps", "{A21F07E6-A891-479C-98EA-EDB58CE4EFAB}"
6 6
 EndProject
@@ -108,8 +108,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RARFileSource", "src\thirdp
108 108
 EndProject
109 109
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LAVFilters", "src\thirdparty\LAVFilters\LAVFilters.vcxproj", "{ED257874-E12E-4143-AF0A-0676DA3BB18C}"
110 110
 EndProject
111
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mhook", "src\thirdparty\mhook\mhook.vcxproj", "{F841363C-A630-4716-8941-CDDC1F32CFC3}"
112
-EndProject
113 111
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DoctorDump", "src\thirdparty\DoctorDump\DoctorDump.vcxproj", "{BECEAFF9-CDCA-45EC-A1CF-658FA51818E6}"
114 112
 EndProject
115 113
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrashReporterDialog", "src\mpc-hc\CrashReporterDialog\CrashReporterDialog.vcxproj", "{5DB958DD-8B25-45EF-8072-3356CAF8CCD7}"
@@ -130,6 +128,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BuildConfig", "build\BuildC
130 128
 EndProject
131 129
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tinyxml2", "src\thirdparty\tinyxml2\tinyxml2.vcxproj", "{E02F0C35-FB01-4059-90F9-9AC19DC22FBA}"
132 130
 EndProject
131
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "minhook", "src\thirdparty\minhook\minhook.vcxproj", "{303B855A-137D-45E9-AF6D-B7241C6E66D6}"
132
+EndProject
133 133
 Global
134 134
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
135 135
 		Debug Filter|Win32 = Debug Filter|Win32
@@ -1036,30 +1036,6 @@ Global
1036 1036
 		{ED257874-E12E-4143-AF0A-0676DA3BB18C}.Release|Win32.Build.0 = Release|Win32
1037 1037
 		{ED257874-E12E-4143-AF0A-0676DA3BB18C}.Release|x64.ActiveCfg = Release|x64
1038 1038
 		{ED257874-E12E-4143-AF0A-0676DA3BB18C}.Release|x64.Build.0 = Release|x64
1039
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Filter|Win32.ActiveCfg = Debug|Win32
1040
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Filter|Win32.Build.0 = Debug|Win32
1041
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Filter|x64.ActiveCfg = Debug|x64
1042
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Filter|x64.Build.0 = Debug|x64
1043
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Lite|Win32.ActiveCfg = Debug|Win32
1044
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Lite|Win32.Build.0 = Debug|Win32
1045
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Lite|x64.ActiveCfg = Debug|x64
1046
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug Lite|x64.Build.0 = Debug|x64
1047
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug|Win32.ActiveCfg = Debug|Win32
1048
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug|Win32.Build.0 = Debug|Win32
1049
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug|x64.ActiveCfg = Debug|x64
1050
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Debug|x64.Build.0 = Debug|x64
1051
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Filter|Win32.ActiveCfg = Release|Win32
1052
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Filter|Win32.Build.0 = Release|Win32
1053
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Filter|x64.ActiveCfg = Release|x64
1054
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Filter|x64.Build.0 = Release|x64
1055
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Lite|Win32.ActiveCfg = Release|Win32
1056
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Lite|Win32.Build.0 = Release|Win32
1057
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Lite|x64.ActiveCfg = Release|x64
1058
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release Lite|x64.Build.0 = Release|x64
1059
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release|Win32.ActiveCfg = Release|Win32
1060
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release|Win32.Build.0 = Release|Win32
1061
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release|x64.ActiveCfg = Release|x64
1062
-		{F841363C-A630-4716-8941-CDDC1F32CFC3}.Release|x64.Build.0 = Release|x64
1063 1039
 		{BECEAFF9-CDCA-45EC-A1CF-658FA51818E6}.Debug Filter|Win32.ActiveCfg = Release|Win32
1064 1040
 		{BECEAFF9-CDCA-45EC-A1CF-658FA51818E6}.Debug Filter|x64.ActiveCfg = Release|x64
1065 1041
 		{BECEAFF9-CDCA-45EC-A1CF-658FA51818E6}.Debug Lite|Win32.ActiveCfg = Release Lite|Win32
@@ -1260,6 +1236,30 @@ Global
1260 1236
 		{E02F0C35-FB01-4059-90F9-9AC19DC22FBA}.Release|Win32.Build.0 = Release|Win32
1261 1237
 		{E02F0C35-FB01-4059-90F9-9AC19DC22FBA}.Release|x64.ActiveCfg = Release|x64
1262 1238
 		{E02F0C35-FB01-4059-90F9-9AC19DC22FBA}.Release|x64.Build.0 = Release|x64
1239
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Filter|Win32.ActiveCfg = Debug|Win32
1240
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Filter|Win32.Build.0 = Debug|Win32
1241
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Filter|x64.ActiveCfg = Debug|x64
1242
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Filter|x64.Build.0 = Debug|x64
1243
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Lite|Win32.ActiveCfg = Debug|Win32
1244
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Lite|Win32.Build.0 = Debug|Win32
1245
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Lite|x64.ActiveCfg = Debug|x64
1246
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug Lite|x64.Build.0 = Debug|x64
1247
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug|Win32.ActiveCfg = Debug|Win32
1248
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug|Win32.Build.0 = Debug|Win32
1249
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug|x64.ActiveCfg = Debug|x64
1250
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Debug|x64.Build.0 = Debug|x64
1251
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Filter|Win32.ActiveCfg = Release|Win32
1252
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Filter|Win32.Build.0 = Release|Win32
1253
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Filter|x64.ActiveCfg = Release|x64
1254
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Filter|x64.Build.0 = Release|x64
1255
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Lite|Win32.ActiveCfg = Release|Win32
1256
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Lite|Win32.Build.0 = Release|Win32
1257
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Lite|x64.ActiveCfg = Release|x64
1258
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release Lite|x64.Build.0 = Release|x64
1259
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release|Win32.ActiveCfg = Release|Win32
1260
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release|Win32.Build.0 = Release|Win32
1261
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release|x64.ActiveCfg = Release|x64
1262
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6}.Release|x64.Build.0 = Release|x64
1263 1263
 	EndGlobalSection
1264 1264
 	GlobalSection(SolutionProperties) = preSolution
1265 1265
 		HideSolutionNode = FALSE
@@ -1315,7 +1315,6 @@ Global
1315 1315
 		{DA8461C4-7683-4360-9372-2A9E0F1795C2} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1316 1316
 		{2B7F22D7-1750-47C5-8709-1A3688B62499} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1317 1317
 		{ED257874-E12E-4143-AF0A-0676DA3BB18C} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1318
-		{F841363C-A630-4716-8941-CDDC1F32CFC3} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1319 1318
 		{BECEAFF9-CDCA-45EC-A1CF-658FA51818E6} = {B161892C-93C5-4F89-8C43-7E9BE5DD4AE4}
1320 1319
 		{5DB958DD-8B25-45EF-8072-3356CAF8CCD7} = {B161892C-93C5-4F89-8C43-7E9BE5DD4AE4}
1321 1320
 		{B161892C-93C5-4F89-8C43-7E9BE5DD4AE4} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
@@ -1326,5 +1325,6 @@ Global
1326 1325
 		{BB2B61AF-734A-4DAD-9326-07F4F9EA088F} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1327 1326
 		{981574AE-5A5E-4F27-BDF1-1B841E374CFF} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1328 1327
 		{E02F0C35-FB01-4059-90F9-9AC19DC22FBA} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1328
+		{303B855A-137D-45E9-AF6D-B7241C6E66D6} = {D9A0529B-9EC4-4D30-9E05-A5D533739D95}
1329 1329
 	EndGlobalSection
1330 1330
 EndGlobal

+ 2
- 7
src/DSUtil/MhookHelper.h View File

@@ -20,16 +20,11 @@
20 20
 
21 21
 #pragma once
22 22
 
23
-#include "mhook/mhook-lib/mhook.h"
23
+#include "minhook/minhook/include/MinHook.h"
24 24
 
25 25
 template <typename T>
26 26
 inline BOOL Mhook_SetHookEx(T** ppSystemFunction, PVOID pHookFunction)
27 27
 {
28
-    return Mhook_SetHook(reinterpret_cast<PVOID*>(ppSystemFunction), pHookFunction);
29
-}
28
+    return MH_CreateHook(*ppSystemFunction, pHookFunction, reinterpret_cast<LPVOID*>(ppSystemFunction)) == MH_OK;
30 29
 
31
-template <typename T>
32
-inline BOOL Mhook_UnhookEx(T** ppHookedFunction)
33
-{
34
-    return Mhook_Unhook(reinterpret_cast<PVOID*>(ppHookedFunction));
35 30
 }

+ 3
- 3
src/mpc-hc/mpc-hc.vcxproj View File

@@ -609,9 +609,6 @@
609 609
     <ProjectReference Include="..\thirdparty\MediaInfo\MediaInfoLib.vcxproj">
610 610
       <Project>{20e0f8d6-213c-460b-b361-9c725cb375c7}</Project>
611 611
     </ProjectReference>
612
-    <ProjectReference Include="..\thirdparty\mhook\mhook.vcxproj">
613
-      <Project>{f841363c-a630-4716-8941-cddc1f32cfc3}</Project>
614
-    </ProjectReference>
615 612
     <ProjectReference Include="..\thirdparty\RARFileSource\RARFileSource.vcxproj">
616 613
       <Project>{2b7f22d7-1750-47c5-8709-1a3688b62499}</Project>
617 614
     </ProjectReference>
@@ -688,6 +685,9 @@
688 685
       <LinkLibraryDependencies>false</LinkLibraryDependencies>
689 686
       <UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>
690 687
     </ProjectReference>
688
+    <ProjectReference Include="..\thirdparty\minhook\minhook.vcxproj">
689
+      <Project>{303B855A-137D-45E9-AF6D-B7241C6E66D6}</Project>
690
+    </ProjectReference>
691 691
   </ItemGroup>
692 692
   <ItemGroup>
693 693
     <Image Include="res\check_all.bmp" />

+ 4
- 0
src/mpc-hc/mplayerc.cpp View File

@@ -1469,6 +1469,8 @@ BOOL CMPlayerCApp::InitInstance()
1469 1469
     // At this point only main thread should be present, mhook is custom-hacked accordingly
1470 1470
     bool bHookingSuccessful = true;
1471 1471
 
1472
+    MH_Initialize();
1473
+
1472 1474
     bHookingSuccessful &= !!Mhook_SetHookEx(&Real_IsDebuggerPresent, Mine_IsDebuggerPresent);
1473 1475
 
1474 1476
     m_hNTDLL = LoadLibrary(_T("ntdll.dll"));
@@ -2026,6 +2028,8 @@ int CMPlayerCApp::ExitInstance()
2026 2028
 
2027 2029
     CMPCPngImage::CleanUp();
2028 2030
 
2031
+    MH_Uninitialize();
2032
+
2029 2033
     OleUninitialize();
2030 2034
 
2031 2035
     return CWinApp::ExitInstance();

+ 0
- 93
src/thirdparty/mhook/disasm-lib/cpu.c View File

@@ -1,93 +0,0 @@
1
-// Copyright (C) 2003, Matt Conover (mconover@gmail.com)
2
-#include "cpu.h"
3
-#include <assert.h>
4
-
5
-// NOTE: this assumes default scenarios (i.e., we assume CS/DS/ES/SS and flat
6
-// and all have a base of 0 and limit of 0xffffffff, we don't try to verify
7
-// that in the GDT)
8
-//
9
-// TODO: use inline assembly to get selector for segment
10
-// Segment = x86 segment register (SEG_ES = 0, SEG_CS = 1, ...)
11
-BYTE *GetAbsoluteAddressFromSegment(BYTE Segment, DWORD Offset)
12
-{
13
-	switch (Segment)
14
-	{
15
-		// Windows uses a flat address space (except FS for x86 and GS for x64)
16
-		case 0: // SEG_ES
17
-		case 1: // SEG_CS
18
-		case 2: // SEG_SS
19
-		case 3: // SEG_DS
20
-			return (BYTE *)(DWORD_PTR)Offset;
21
-		case 4: // SEG_FS
22
-		case 5: // SEG_GS
23
-			return (BYTE *)(DWORD_PTR)Offset;
24
-			// Note: we're really supposed to do this, but get_teb is not implemented
25
-			// in this bastardized version of the disassembler.
26
-			// return (BYTE *)get_teb() + Offset;
27
-		default:
28
-			assert(0);
29
-			return (BYTE *)(DWORD_PTR)Offset;
30
-	}
31
-}
32
-
33
-// This is an GDT/LDT selector (pGDT+Selector)
34
-BYTE *GetAbsoluteAddressFromSelector(WORD Selector, DWORD Offset)
35
-{
36
-	DESCRIPTOR_ENTRY Entry;
37
-	GATE_ENTRY *Gate;
38
-	ULONG_PTR Base;
39
-	
40
-	assert(Selector < 0x10000);
41
-	if (!GetThreadSelectorEntry(GetCurrentThread(), Selector, (LDT_ENTRY *)&Entry)) return NULL;
42
-	if (!Entry.Present) return NULL;
43
-	if (Entry.System)
44
-	{
45
-		Base = 0;
46
-#ifdef _WIN64
47
-		Base |= (ULONG_PTR)Entry.HighOffset64 << 32;
48
-#endif
49
-		Base |= Entry.BaseHi << 24;
50
-		Base |= Entry.BaseMid << 16;
51
-		Base |= Entry.BaseLow;
52
-	}
53
-	else
54
-	{
55
-		switch (Entry.Type)
56
-		{
57
-			case 1: // 16-bit TSS (available)
58
-			case 2: // LDT
59
-			case 3: // 16-bit TSS (busy)
60
-			case 9: // 32-bit TSS (available)
61
-			case 11: // 32-bit TSS (busy)
62
-				Base = 0;
63
-#ifdef _WIN64
64
-				Base |= (ULONG_PTR)Entry.HighOffset64 << 32;
65
-#endif
66
-				Base |= Entry.BaseHi << 24;
67
-				Base |= Entry.BaseMid << 16;
68
-				Base |= Entry.BaseLow;
69
-				break;
70
-
71
-			case 4: // 16-bit call gate
72
-			case 5: // task gate
73
-			case 6: // 16-bit interrupt gate
74
-			case 7: // 16-bit task gate
75
-			case 12: // 32-bit call gate
76
-			case 14: // 32-bit interrupt gate
77
-			case 15: // 32-bit trap gate
78
-				Gate = (GATE_ENTRY *)&Entry;
79
-#ifdef _WIN64
80
-				Base = ((ULONG_PTR)Gate->HighOffset64 << 32) | (Gate->HighOffset << 16) | Gate->LowOffset;
81
-#else
82
-				Base = (Gate->HighOffset << 16) | Gate->LowOffset;
83
-#endif
84
-				assert(!Offset); Offset = 0;
85
-				break;
86
-			default:
87
-				assert(0);
88
-				return NULL;
89
-		}
90
-	}
91
-	return (BYTE *)Base + Offset;
92
-}
93
-

+ 0
- 277
src/thirdparty/mhook/disasm-lib/cpu.h View File

@@ -1,277 +0,0 @@
1
-// Copyright (C) 2003, Matt Conover (mconover@gmail.com)
2
-#ifndef CPU_H
3
-#define CPU_H
4
-#ifdef __cplusplus
5
-extern "C" {
6
-#endif
7
-#pragma pack(push,1)
8
-
9
-#include <windows.h>
10
-#include "misc.h"
11
-
12
-////////////////////////////////////////////////////////
13
-// System descriptors
14
-////////////////////////////////////////////////////////
15
-
16
-#define GDT_NULL 0
17
-#define GDT_R0_CODE 0x08
18
-#define GDT_R0_DATA 0x10
19
-#define GDT_R3_CODE 0x18
20
-#define GDT_R3_DATA 0x20
21
-#define GDT_TSS 0x28
22
-#define GDT_PCR 0x30
23
-#define GDT_R3_TEB 0x38
24
-#define GDT_VDM 0x40
25
-#define GDT_LDT 0x48
26
-#define GDT_DOUBLEFAULT_TSS 0x50
27
-#define GDT_NMI_TSS 0x58
28
-
29
-// 16-bit GDT entries:
30
-// TODO: #define GDT_ABIOS_UNKNOWN   0x60  (22F30-32F2F)
31
-#define GDT_ABIOS_VIDEO 0x68
32
-#define GDT_ABIOS_GDT   0x70 // descriptor describing ABIOS GDT itself
33
-#define GDT_ABIOS_NTOS  0x78 // first 64K of NTOSKRNL
34
-#define GDT_ABIOS_CDA   0xE8 // common data area
35
-#define GDT_ABIOS_CODE  0xF0 // KiI386AbiosCall
36
-#define GDT_ABIOS_STACK 0xF8
37
-
38
-#define SELECTOR_RPL_MASK 0x03 // bits 0-1
39
-#define SELECTOR_LDT      0x04 // bit 2
40
-
41
-// for data selectors
42
-#define DATA_ACCESS_MASK       (1<<0)
43
-#define DATA_WRITE_ENABLE_MASK (1<<1)
44
-#define DATA_EXPAND_DOWN_MASK  (1<<2)
45
-
46
-// for code selectors
47
-#define CODE_ACCESS_MASK       (1<<0)
48
-#define CODE_READ_MASK         (1<<1)
49
-#define CODE_CONFORMING_MASK   (1<<2)
50
-#define CODE_FLAG              (1<<3)
51
-
52
-#define TASK_GATE      5
53
-#define INTERRUPT_GATE 6
54
-#define TRAP_GATE      7
55
-
56
-typedef struct _IDT_ENTRY
57
-{
58
-   USHORT LowOffset;
59
-   USHORT Selector;
60
-   UCHAR Ignored : 5;
61
-   UCHAR Zero : 3;
62
-   UCHAR Type : 3;
63
-   UCHAR Is32Bit : 1;
64
-   UCHAR Ignored2 : 1;
65
-   UCHAR DPL : 2;
66
-   UCHAR Present : 1;
67
-   USHORT HighOffset;
68
-#ifdef _WIN64
69
-   ULONG HighOffset64;
70
-   ULONG Reserved;
71
-#endif
72
-} IDT_ENTRY, TRAP_GATE_ENTRY;
73
-
74
-typedef struct _CALL_GATE_ENTRY
75
-{
76
-   USHORT LowOffset;
77
-   USHORT Selector;
78
-   UCHAR ParameterCount: 4;
79
-   UCHAR Ignored : 3;
80
-   UCHAR Type : 5;
81
-   UCHAR DPL : 2;
82
-   UCHAR Present : 1;
83
-   USHORT HighOffset;
84
-#ifdef _WIN64
85
-   ULONG HighOffset64;
86
-   ULONG Reserved;
87
-#endif
88
-} CALL_GATE_ENTRY;
89
-
90
-typedef struct _TASK_GATE_ENTRY
91
-{
92
-   USHORT Ignored;
93
-   USHORT Selector;
94
-   UCHAR Ignored2 : 5;
95
-   UCHAR Zero : 3;
96
-   UCHAR Type : 5;
97
-   UCHAR DPL : 2;
98
-   UCHAR Present : 1;
99
-   USHORT Ignored3;
100
-} TASK_GATE_ENTRY;
101
-
102
-typedef struct _DESCRIPTOR_ENTRY
103
-{
104
-    USHORT  LimitLow;
105
-    USHORT  BaseLow;
106
-    UCHAR   BaseMid;
107
-    UCHAR   Type : 4;        // 10EWA (code), E=ExpandDown, W=Writable, A=Accessed
108
-                             // 11CRA (data), C=Conforming, R=Readable, A=Accessed
109
-    UCHAR   System : 1;      // if 1 then it is a gate or LDT
110
-    UCHAR   DPL : 2;         // descriptor privilege level; 
111
-                             // for data selectors, MAX(CPL, RPL) must be <= DPL to access (or else GP# fault)
112
-                             // for non-conforming code selectors (without callgate), MAX(CPL, RPL) must be <= DPL to access (or else GP# fault)
113
-                             // for conforming code selectors, MAX(CPL, RPL) must be >= DPL (i.e., CPL 0-2 cannot access if DPL is 3)
114
-                             // for non-conforming code selectors (with call gate), DPL indicates lowest privilege allowed to access gate
115
-    UCHAR   Present : 1;
116
-    UCHAR   LimitHigh : 4;
117
-    UCHAR   Available: 1;    // aka AVL
118
-    UCHAR   Reserved : 1;
119
-    UCHAR   Is32Bit : 1;     // aka B flag
120
-    UCHAR   Granularity : 1; // aka G flag
121
-    UCHAR   BaseHi : 8;
122
-#ifdef _WIN64
123
-   ULONG HighOffset64;
124
-   ULONG Reserved2;
125
-#endif
126
-} DESCRIPTOR_ENTRY;
127
-
128
-typedef struct _GATE_ENTRY
129
-{
130
-   USHORT LowOffset;
131
-   UCHAR Skip;
132
-   UCHAR Type : 5;
133
-   UCHAR DPL : 2;
134
-   UCHAR Present : 1;
135
-   USHORT HighOffset;
136
-#ifdef _WIN64
137
-   ULONG HighOffset64;
138
-   ULONG Reserved;
139
-#endif
140
-} GATE_ENTRY;
141
-
142
-// TODO: update for X64
143
-typedef struct _PTE_ENTRY
144
-{
145
-    ULONG Present : 1;
146
-    ULONG Write : 1;
147
-    ULONG Owner : 1; // E.g., user mode or supervisor mode
148
-    ULONG WriteThrough : 1;
149
-    ULONG CacheDisable : 1;
150
-    ULONG Accessed : 1;
151
-    ULONG Dirty : 1;
152
-    ULONG PAT : 1;
153
-    ULONG Global : 1;
154
-    ULONG CopyOnWrite : 1;
155
-    ULONG Prototype : 1;
156
-    ULONG Transition : 1;
157
-    ULONG Address : 20;
158
-} PTE_ENTRY;
159
-
160
-// TODO: update for X64
161
-typedef struct _PDE_ENTRY
162
-{
163
-	ULONG Present : 1;
164
-	ULONG Write : 1;
165
-	ULONG Owner : 1;
166
-	ULONG WriteThrough : 1;
167
-	ULONG CacheDisable : 1;
168
-	ULONG Accessed : 1;
169
-	ULONG Reserved1 : 1;
170
-	ULONG PageSize : 1;
171
-	ULONG Global : 1;
172
-	ULONG Reserved : 3;
173
-	ULONG Address : 20;
174
-} PDE_ENTRY;
175
-
176
-// TODO: update for X64
177
-typedef struct _IO_ACCESS_MAP
178
-{
179
-    UCHAR DirectionMap[32];
180
-    UCHAR IoMap[8196];
181
-} IO_ACCESS_MAP;
182
-
183
-#define MIN_TSS_SIZE FIELD_OFFSET(TSS_ENTRY, IoMaps)
184
-// TODO: update for X64
185
-typedef struct _TSS_ENTRY
186
-{
187
-    USHORT  Backlink;
188
-    USHORT  Reserved0;
189
-    ULONG   Esp0;
190
-    USHORT  Ss0;
191
-    USHORT  Reserved1;
192
-    ULONG   NotUsed1[4];
193
-    ULONG   CR3;
194
-    ULONG   Eip;
195
-    ULONG   NotUsed2[9];
196
-    USHORT  Es;
197
-    USHORT  Reserved2;
198
-    USHORT  Cs;
199
-    USHORT  Reserved3;
200
-    USHORT  Ss;
201
-    USHORT  Reserved4;
202
-    USHORT  Ds;
203
-    USHORT  Reserved5;
204
-    USHORT  Fs;
205
-    USHORT  Reserved6;
206
-    USHORT  Gs;
207
-    USHORT  Reserved7;
208
-    USHORT  LDT;
209
-    USHORT  Reserved8;
210
-    USHORT  Flags;
211
-    USHORT  IoMapBase;
212
-    IO_ACCESS_MAP IoMaps[1];
213
-    UCHAR IntDirectionMap[32];
214
-} TSS_ENTRY;
215
-
216
-// TODO: update for X64
217
-typedef struct _TSS16_ENTRY
218
-{
219
-    USHORT  Backlink;
220
-    USHORT  Sp0;
221
-    USHORT  Ss0;
222
-    USHORT  Sp1;
223
-    USHORT  Ss1;
224
-    USHORT  Sp2;
225
-    USHORT  Ss3;
226
-    USHORT  Ip;
227
-    USHORT  Flags;
228
-    USHORT  Ax;
229
-    USHORT  Cx;
230
-    USHORT  Dx;
231
-    USHORT  Bx;
232
-    USHORT  Sp;
233
-    USHORT  Bp;
234
-    USHORT  Si;
235
-    USHORT  Di;
236
-    USHORT  Es;
237
-    USHORT  Cs;
238
-    USHORT  Ss;
239
-    USHORT  Ds;
240
-    USHORT  LDT;
241
-} TSS16_ENTRY;
242
-
243
-// TODO: update for X64
244
-typedef struct _GDT_ENTRY
245
-{
246
-    USHORT  LimitLow;
247
-    USHORT  BaseLow;
248
-    union {
249
-        struct {
250
-            UCHAR   BaseMid;
251
-            UCHAR   Flags1;
252
-            UCHAR   Flags2;
253
-            UCHAR   BaseHi;
254
-        } Bytes;
255
-        struct {
256
-            ULONG   BaseMid : 8;
257
-            ULONG   Type : 5;
258
-            ULONG   Dpl : 2;
259
-            ULONG   Pres : 1;
260
-            ULONG   LimitHi : 4;
261
-            ULONG   Sys : 1;
262
-            ULONG   Reserved_0 : 1;
263
-            ULONG   Default_Big : 1;
264
-            ULONG   Granularity : 1;
265
-            ULONG   BaseHi : 8;
266
-        } Bits;
267
-    } HighWord;
268
-} GDT_ENTRY;
269
-
270
-BYTE *GetAbsoluteAddressFromSegment(BYTE Segment, DWORD Offset);
271
-BYTE *GetAbsoluteAddressFromSelector(WORD Selector, DWORD Offset);
272
-
273
-#pragma pack(pop)
274
-#ifdef __cplusplus
275
-}
276
-#endif
277
-#endif // CPU_H

+ 0
- 122
src/thirdparty/mhook/disasm-lib/disasm.c View File

@@ -1,122 +0,0 @@
1
-// Copyright (C) 2004, Matt Conover (mconover@gmail.com)
2
-#undef NDEBUG
3
-#include <assert.h>
4
-#include <windows.h>
5
-#include "disasm.h"
6
-
7
-#ifdef NO_SANITY_CHECKS
8
-#define NDEBUG
9
-#undef assert
10
-#define assert(x)
11
-#endif
12
-
13
-//////////////////////////////////////////////////////////////////////
14
-// Global variables
15
-//////////////////////////////////////////////////////////////////////
16
-
17
-ARCHITECTURE_FORMAT SupportedArchitectures[] =
18
-{
19
-	{ ARCH_X86,	&X86 },
20
-	{ ARCH_X86_16, &X86 },
21
-	{ ARCH_X64,	&X86 },
22
-	{ ARCH_UNKNOWN, NULL }
23
-};
24
-
25
-typedef struct _DISASM_ARG_INFO
26
-{
27
-	INSTRUCTION *MatchedInstruction;
28
-	BOOL MatchPrefix;
29
-	U8 *Opcode;
30
-	U32 OpcodeLength;
31
-	INSTRUCTION_TYPE InstructionType;
32
-	U32 Count;
33
-} DISASM_ARG_INFO;
34
-
35
-//////////////////////////////////////////////////////////////////////
36
-// Function prototypes
37
-//////////////////////////////////////////////////////////////////////
38
-
39
-BOOL InitInstruction(INSTRUCTION *Instruction, DISASSEMBLER *Disassembler);
40
-struct _ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type);
41
-
42
-//////////////////////////////////////////////////////////////////////
43
-// Disassembler setup
44
-//////////////////////////////////////////////////////////////////////
45
-
46
-BOOL InitDisassembler(DISASSEMBLER *Disassembler, ARCHITECTURE_TYPE Architecture)
47
-{
48
-	ARCHITECTURE_FORMAT *ArchFormat;
49
-
50
-	memset(Disassembler, 0, sizeof(DISASSEMBLER));
51
-	Disassembler->Initialized = DISASSEMBLER_INITIALIZED;
52
-	
53
-	ArchFormat = GetArchitectureFormat(Architecture);
54
-	if (!ArchFormat) { assert(0); return FALSE; }
55
-	Disassembler->ArchType = ArchFormat->Type;
56
-	Disassembler->Functions = ArchFormat->Functions;
57
-	return TRUE;
58
-}
59
-
60
-void CloseDisassembler(DISASSEMBLER *Disassembler)
61
-{
62
-	memset(Disassembler, 0, sizeof(DISASSEMBLER));
63
-}
64
-
65
-//////////////////////////////////////////////////////////////////////
66
-// Instruction setup
67
-//////////////////////////////////////////////////////////////////////
68
-
69
-BOOL InitInstruction(INSTRUCTION *Instruction, DISASSEMBLER *Disassembler)
70
-{
71
-	memset(Instruction, 0, sizeof(INSTRUCTION));
72
-	Instruction->Initialized = INSTRUCTION_INITIALIZED;
73
-	Instruction->Disassembler = Disassembler;
74
-	memset(Instruction->String, ' ', MAX_OPCODE_DESCRIPTION-1);
75
-	Instruction->String[MAX_OPCODE_DESCRIPTION-1] = '\0';
76
-	return TRUE;
77
-}
78
-
79
-// If Decode = FALSE, only the following fields are valid:
80
-// Instruction->Length, Instruction->Address, Instruction->Prefixes, Instruction->PrefixCount,
81
-// Instruction->OpcodeBytes, Instruction->Instruction->OpcodeLength, Instruction->Groups,
82
-// Instruction->Type, Instruction->OperandCount
83
-//
84
-// If Disassemble = TRUE, then Instruction->String is valid (also requires Decode = TRUE)
85
-//
86
-// WARNING: This will overwrite the previously obtained instruction
87
-INSTRUCTION *GetInstruction(DISASSEMBLER *Disassembler, U64 VirtualAddress, U8 *Address, U32 Flags)
88
-{
89
-	if (Disassembler->Initialized != DISASSEMBLER_INITIALIZED) { assert(0); return NULL; }
90
-	assert(Address);
91
-	InitInstruction(&Disassembler->Instruction, Disassembler);
92
-	Disassembler->Instruction.Address = Address;	
93
-	Disassembler->Instruction.VirtualAddressDelta = VirtualAddress - (U64)Address;
94
-	if (!Disassembler->Functions->GetInstruction(&Disassembler->Instruction, Address, Flags))
95
-	{
96
-		assert(Disassembler->Instruction.Address == Address);
97
-		assert(Disassembler->Instruction.Length < MAX_INSTRUCTION_LENGTH);
98
-
99
-		// Save the address that failed, in case the lower-level disassembler didn't
100
-		Disassembler->Instruction.Address = Address;
101
-		Disassembler->Instruction.ErrorOccurred = TRUE;
102
-		return NULL;
103
-	}
104
-	return &Disassembler->Instruction;
105
-}
106
-
107
-///////////////////////////////////////////////////////////////////////////
108
-// Miscellaneous
109
-///////////////////////////////////////////////////////////////////////////
110
-
111
-static ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type)
112
-{
113
-	ARCHITECTURE_FORMAT *Format;
114
-	for (Format = SupportedArchitectures; Format->Type != ARCH_UNKNOWN; Format++)
115
-	{
116
-		if (Format->Type == Type) return Format;
117
-	}
118
-
119
-	assert(0);
120
-	return NULL;
121
-}
122
-

+ 0
- 578
src/thirdparty/mhook/disasm-lib/disasm.h View File

@@ -1,578 +0,0 @@
1
-// Copyright (C) 2004, Matt Conover (mconover@gmail.com)
2
-//
3
-// WARNING:
4
-// I wouldn't recommend changing any flags like OP_*, ITYPE_*, or *_MASK
5
-// aside from those marked as UNUSED. This is because the flags parts of
6
-// the flags are architecture independent and other are left to specific
7
-// architectures to define, so unless you understand the relationships
8
-// between them, I would leave them as is.
9
-
10
-#ifndef DISASM_H
11
-#define DISASM_H
12
-#ifdef __cplusplus
13
-extern "C" {
14
-#endif
15
-#include <windows.h>
16
-#include <stdio.h>
17
-#include "misc.h"
18
-
19
-typedef signed char S8;
20
-typedef unsigned char U8;
21
-typedef signed short S16;
22
-typedef unsigned short U16;
23
-typedef signed long S32;
24
-typedef unsigned long U32;
25
-typedef LONG64 S64;
26
-typedef ULONG64 U64;
27
-
28
-#ifdef SPEEDY
29
-// On Visual Studio 6, making the internal functions inline makes compiling take forever
30
-#define INTERNAL static _inline 
31
-#define INLINE _inline
32
-#else
33
-#define INTERNAL static
34
-#define INLINE
35
-#endif
36
-
37
-#define VALID_INSTRUCTION(i) ((i) && !((i)->ErrorOccurred))
38
-#define NEXT_INSTRUCTION(i) ((i)->Address + (i)->Length)
39
-#define DISASM_ARCH_TYPE(dis) ((dis)->ArchType)
40
-#define INS_ARCH_TYPE(ins) DISASM_ARCH_TYPE((ins)->Disassembler)
41
-
42
-// NOTE: these should be as big set to the maximum of the supported architectures
43
-#define MAX_PREFIX_LENGTH 15
44
-#define MAX_OPERAND_COUNT 3
45
-#define MAX_INSTRUCTION_LENGTH 25
46
-#define MAX_OPCODE_LENGTH 3
47
-#define MAX_OPCODE_DESCRIPTION 256
48
-
49
-/////////////////////////////////////////////////////////////////////
50
-// Code branch
51
-/////////////////////////////////////////////////////////////////////
52
-
53
-#define MAX_CODE_REFERENCE_COUNT 3
54
-
55
-typedef struct _CODE_BRANCH
56
-{
57
-	U64 Addresses[MAX_CODE_REFERENCE_COUNT]; // NULL if multiple to addresses
58
-	U32 Count;
59
-	U8 IsLoop : 1;
60
-	U8 IsCall : 1; // branch if false
61
-	U8 IsIndirect : 1; // call/jmp [Address]
62
-	U8 AddressOffset: 5;
63
-	struct _INSTRUCTION_OPERAND *Operand; // the operand containg the address
64
-} CODE_BRANCH;
65
-
66
-/////////////////////////////////////////////////////////////////////
67
-// Data references
68
-/////////////////////////////////////////////////////////////////////
69
-
70
-#define MAX_DATA_REFERENCE_COUNT 3
71
-
72
-typedef struct _DATA_REFERENCE
73
-{
74
-	U64 Addresses[MAX_DATA_REFERENCE_COUNT]; // NULL if multiple to addresses
75
-	U32 Count;
76
-	ULONG_PTR DataSize;
77
-	struct _INSTRUCTION_OPERAND *Operand; // the operand containg the address
78
-} DATA_REFERENCE;
79
-
80
-////////////////////////////////////////////////////////////////////
81
-// Instruction
82
-/////////////////////////////////////////////////////////////////////
83
-
84
-//
85
-// Instruction types (bits 0-7)
86
-// Instruction groups (bits 8-26)
87
-//
88
-#define ITYPE_EXEC_OFFSET     (1<<8)
89
-#define ITYPE_ARITH_OFFSET    (1<<9)
90
-#define ITYPE_LOGIC_OFFSET    (1<<10)
91
-#define ITYPE_STACK_OFFSET    (1<<11)
92
-#define ITYPE_TESTCOND_OFFSET (1<<12)
93
-#define ITYPE_LOAD_OFFSET     (1<<13)
94
-#define ITYPE_ARRAY_OFFSET    (1<<14)
95
-#define ITYPE_BIT_OFFSET      (1<<15)
96
-#define ITYPE_FLAG_OFFSET     (1<<16)
97
-#define ITYPE_FPU_OFFSET      (1<<17)
98
-#define ITYPE_TRAPS_OFFSET    (1<<18)
99
-#define ITYPE_SYSTEM_OFFSET   (1<<19)
100
-#define ITYPE_OTHER_OFFSET    (1<<20)
101
-#define ITYPE_UNUSED1_OFFSET  (1<<21)
102
-#define ITYPE_UNUSED2_OFFSET  (1<<22)
103
-#define ITYPE_UNUSED3_OFFSET  (1<<23)
104
-#define ITYPE_UNUSED4_OFFSET  (1<<24)
105
-#define ITYPE_UNUSED5_OFFSET  (1<<25)
106
-#define ITYPE_UNUSED6_OFFSET  (1<<26)
107
-#define ITYPE_EXT_UNUSED1     (1<<27)
108
-#define ITYPE_EXT_UNUSED2     (1<<28)
109
-#define ITYPE_EXT_UNUSED3     (1<<29)
110
-#define ITYPE_EXT_UNUSED4     (1<<30)
111
-#define ITYPE_EXT_UNUSED5     (1<<31)
112
-
113
-//
114
-// X86-specific flags (bits 27-31)
115
-//
116
-
117
-#define ITYPE_EXT_64     ITYPE_EXT_UNUSED1 // Use index 1 if in 64-bit mode and 0 otherwise
118
-#define ITYPE_EXT_MODRM  ITYPE_EXT_UNUSED2 // ModRM byte may extend the opcode
119
-#define ITYPE_EXT_SUFFIX ITYPE_EXT_UNUSED3 // byte after ModRM/SIB/displacement is the third opcode
120
-#define ITYPE_EXT_PREFIX ITYPE_EXT_UNUSED4 // prefix
121
-#define ITYPE_EXT_FPU    ITYPE_EXT_UNUSED5 // FPU instructions require special handling
122
-
123
-#define ITYPE_3DNOW_OFFSET ITYPE_UNUSED1_OFFSET
124
-#define ITYPE_MMX_OFFSET   ITYPE_UNUSED2_OFFSET
125
-#define ITYPE_SSE_OFFSET   ITYPE_UNUSED3_OFFSET
126
-#define ITYPE_SSE2_OFFSET  ITYPE_UNUSED4_OFFSET
127
-#define ITYPE_SSE3_OFFSET  ITYPE_UNUSED5_OFFSET
128
-
129
-//
130
-// Instruction types
131
-//
132
-
133
-#define ITYPE_TYPE_MASK  0x7FFFFFFF
134
-#define ITYPE_GROUP_MASK 0x7FFFFF00
135
-
136
-typedef enum _INSTRUCTION_TYPE
137
-{
138
-	// ITYPE_EXEC group
139
-	ITYPE_EXEC = ITYPE_EXEC_OFFSET,
140
-	ITYPE_BRANCH,
141
-	ITYPE_BRANCHCC, // conditional (not necessarily just flags)
142
-	ITYPE_CALL,
143
-	ITYPE_CALLCC, // conditional (not necessarily just flags)
144
-	ITYPE_RET,
145
-	ITYPE_LOOPCC,
146
-
147
-	// ITYPE_ARITH group
148
-	ITYPE_ARITH = ITYPE_ARITH_OFFSET,
149
-	ITYPE_XCHGADD,
150
-	ITYPE_ADD,
151
-	ITYPE_SUB,
152
-	ITYPE_MUL,
153
-	ITYPE_DIV,
154
-	ITYPE_INC,
155
-	ITYPE_DEC,
156
-	ITYPE_SHL,
157
-	ITYPE_SHR,
158
-	ITYPE_ROL,
159
-	ITYPE_ROR,
160
-
161
-	// ITYPE_LOGIC group
162
-	ITYPE_LOGIC=ITYPE_LOGIC_OFFSET,
163
-	ITYPE_AND,
164
-	ITYPE_OR,
165
-	ITYPE_XOR,
166
-	ITYPE_NOT,
167
-	ITYPE_NEG,
168
-
169
-	// ITYPE_STACK group
170
-	ITYPE_STACK=ITYPE_STACK_OFFSET,
171
-	ITYPE_PUSH,
172
-	ITYPE_POP,
173
-	ITYPE_PUSHA,
174
-	ITYPE_POPA,
175
-	ITYPE_PUSHF,
176
-	ITYPE_POPF,
177
-	ITYPE_ENTER,
178
-	ITYPE_LEAVE,
179
-
180
-	// ITYPE_TESTCOND group
181
-	ITYPE_TESTCOND=ITYPE_TESTCOND_OFFSET,
182
-		ITYPE_TEST,
183
-		ITYPE_CMP,
184
-
185
-	// ITYPE_LOAD group
186
-	ITYPE_LOAD=ITYPE_LOAD_OFFSET,
187
-		ITYPE_MOV,
188
-		ITYPE_MOVCC, // conditional
189
-		ITYPE_LEA,
190
-		ITYPE_XCHG,
191
-		ITYPE_XCHGCC, // conditional
192
-
193
-	// ITYPE_ARRAY group
194
-	ITYPE_ARRAY=ITYPE_ARRAY_OFFSET,
195
-		ITYPE_STRCMP,
196
-		ITYPE_STRLOAD,
197
-		ITYPE_STRMOV,
198
-		ITYPE_STRSTOR,
199
-		ITYPE_XLAT,
200
-
201
-	// ITYPE_BIT group
202
-	ITYPE_BIT=ITYPE_BIT_OFFSET,
203
-		ITYPE_BITTEST,
204
-		ITYPE_BITSET,
205
-		ITYPE_BITCLR,
206
-
207
-	// ITYPE_FLAG group
208
-	// PF = parify flag
209
-	// ZF = zero flag
210
-	// OF = overflow flag
211
-	// DF = direction flag
212
-	// SF = sign flag
213
-	ITYPE_FLAG=ITYPE_FLAG_OFFSET,
214
-		// clear
215
-		ITYPE_CLEARCF, 
216
-		ITYPE_CLEARZF,
217
-		ITYPE_CLEAROF,
218
-		ITYPE_CLEARDF,
219
-		ITYPE_CLEARSF,
220
-		ITYPE_CLEARPF,
221
-		// set
222
-		ITYPE_SETCF, 
223
-		ITYPE_SETZF,
224
-		ITYPE_SETOF,
225
-		ITYPE_SETDF,
226
-		ITYPE_SETSF,
227
-		ITYPE_SETPF,
228
-		// toggle
229
-		ITYPE_TOGCF, 
230
-		ITYPE_TOGZF,
231
-		ITYPE_TOGOF,
232
-		ITYPE_TOGDF,
233
-		ITYPE_TOGSF,
234
-		ITYPE_TOGPF,
235
-
236
-	// ITYPE_FPU group
237
-	ITYPE_FPU=ITYPE_FPU_OFFSET,
238
-		ITYPE_FADD,
239
-		ITYPE_FSUB,
240
-		ITYPE_FMUL,
241
-		ITYPE_FDIV,
242
-		ITYPE_FCOMP,
243
-		ITYPE_FEXCH,
244
-		ITYPE_FLOAD,
245
-		ITYPE_FLOADENV,
246
-		ITYPE_FSTORE,
247
-		ITYPE_FSTOREENV,
248
-		ITYPE_FSAVE,
249
-		ITYPE_FRESTORE,
250
-		ITYPE_FMOVCC,
251
-
252
-	ITYPE_UNUSED1=ITYPE_UNUSED1_OFFSET,
253
-	ITYPE_UNUSED2=ITYPE_UNUSED2_OFFSET,
254
-	ITYPE_UNUSED3=ITYPE_UNUSED3_OFFSET,
255
-
256
-	// ITYPE_MMX group
257
-	ITYPE_MMX=ITYPE_MMX_OFFSET,
258
-		ITYPE_MMX_MOV,
259
-		ITYPE_MMX_ADD,
260
-		ITYPE_MMX_SUB,
261
-		ITYPE_MMX_MUL,
262
-		ITYPE_MMX_DIV,
263
-		ITYPE_MMX_AND,
264
-		ITYPE_MMX_OR,
265
-		ITYPE_MMX_XOR,
266
-		ITYPE_MMX_CMP,
267
-
268
-	// ITYPE_SSE group
269
-	ITYPE_SSE=ITYPE_SSE_OFFSET,
270
-		ITYPE_SSE_MOV,
271
-		ITYPE_SSE_ADD,
272
-		ITYPE_SSE_SUB,
273
-		ITYPE_SSE_MUL,
274
-		ITYPE_SSE_DIV,
275
-		ITYPE_SSE_AND,
276
-		ITYPE_SSE_OR,
277
-		ITYPE_SSE_XOR,
278
-		ITYPE_SSE_CMP,
279
-		
280
-		// ITYPE_SSE2 group
281
-	ITYPE_SSE2=ITYPE_SSE2_OFFSET,
282
-		ITYPE_SSE2_MOV,
283
-		ITYPE_SSE2_ADD,
284
-		ITYPE_SSE2_SUB,
285
-		ITYPE_SSE2_MUL,
286
-		ITYPE_SSE2_DIV,
287
-		ITYPE_SSE2_AND,
288
-		ITYPE_SSE2_OR,
289
-		ITYPE_SSE2_XOR,
290
-		ITYPE_SSE2_CMP,
291
-
292
-	// ITYPE_SSE3 group
293
-	ITYPE_SSE3=ITYPE_SSE3_OFFSET,
294
-		ITYPE_SSE3_MOV,
295
-		ITYPE_SSE3_ADD,
296
-		ITYPE_SSE3_SUB,
297
-		ITYPE_SSE3_MUL,
298
-		ITYPE_SSE3_DIV,
299
-		ITYPE_SSE3_AND,
300
-		ITYPE_SSE3_OR,
301
-		ITYPE_SSE3_XOR,
302
-		ITYPE_SSE3_CMP,
303
-
304
-	// ITYPE_3DNOW group
305
-	ITYPE_3DNOW=ITYPE_3DNOW_OFFSET,
306
-		ITYPE_3DNOW_ADD,
307
-		ITYPE_3DNOW_SUB,
308
-		ITYPE_3DNOW_MUL,
309
-		ITYPE_3DNOW_DIV,
310
-		ITYPE_3DNOW_CMP,
311
-		ITYPE_3DNOW_XCHG,
312
-
313
-	// ITYPE_TRAP
314
-	ITYPE_TRAPS=ITYPE_TRAPS_OFFSET, 
315
-		ITYPE_TRAP, // generate trap
316
-		ITYPE_TRAPCC,  // conditional trap gen
317
-		ITYPE_TRAPRET,    // return from trap
318
-		ITYPE_BOUNDS,  // gen bounds trap
319
-		ITYPE_DEBUG,   // gen breakpoint trap
320
-		ITYPE_TRACE,   // gen single step trap
321
-		ITYPE_INVALID, // gen invalid instruction
322
-		ITYPE_OFLOW,   // gen overflow trap
323
-
324
-	// ITYPE_SYSTEM group
325
-	ITYPE_SYSTEM=ITYPE_SYSTEM_OFFSET,
326
-		ITYPE_HALT,    // halt machine
327
-		ITYPE_IN,      // input form port
328
-		ITYPE_OUT,     // output to port
329
-		ITYPE_CPUID,   // identify cpu
330
-		ITYPE_SETIF,   // allow interrupts
331
-		ITYPE_CLEARIF, // block interrupts
332
-		ITYPE_SYSCALL,
333
-		ITYPE_SYSCALLRET,
334
-
335
-	// ITYPE_OTHER group
336
-	ITYPE_OTHER = ITYPE_OTHER_OFFSET,
337
-		ITYPE_NOP,
338
-		ITYPE_BCDCONV, // convert to/from BCD
339
-		ITYPE_SZCONV   // convert size of operand
340
-} INSTRUCTION_TYPE;
341
-
342
-//
343
-// Operand flags
344
-//
345
-
346
-// Type = bits 0-6 (these are mutually exclusive -- bits 0-6 will always be a power of 2))
347
-#define OPTYPE_NONE    0x00
348
-#define OPTYPE_IMM    0x01 // immediate value
349
-#define OPTYPE_OFFSET 0x02 // relative offset
350
-#define OPTYPE_FLOAT  0x03 // floating point
351
-#define OPTYPE_BCD    0x04
352
-#define OPTYPE_STRING 0x05
353
-#define OPTYPE_SPECIAL 0x06
354
-#define OPTYPE_MASK   0x7F
355
-
356
-// Flags = bits 7-23 (these can be combinations)
357
-// These are used in the X86 opcode table
358
-#define OP_REG      (1<<7) // 0x80
359
-#define OP_SIGNED   (1<<8)
360
-#define OP_SYS      (1<<9) // parameter is an index into some system structure
361
-#define OP_CONDR    (1<<10)
362
-#define OP_CONDW    (1<<11)
363
-#define OP_UNUSED   (1<<12)
364
-#define OP_SRC      (1<<13) // operand is source operand
365
-#define OP_DST      (1<<14) // operand is destination operand
366
-#define OP_EXEC     (1<<15) // operand is executed
367
-
368
-#define OP_CONDE     OP_CONDR
369
-#define OP_COND_EXEC (OP_CONDE|OP_EXEC) // executed only if the pre-conditions are met
370
-#define OP_COND_SRC  (OP_CONDR|OP_SRC) // set only if pre-conditions are met
371
-#define OP_COND_DST  (OP_CONDW|OP_DST) // set only if pre-conditions are met
372
-#define OP_COND      (OP_CONDR|OP_CONDW)
373
-
374
-// Bits 16-31 are available for use outside of the opcode table, but they can only
375
-// be used in INSTRUCTION_OPERAND.Flags, they may conflit with the architecture specific
376
-// operands. For example, bits 16-31 are used in X86 for AMODE_* and OPTYPE_*
377
-#define OP_ADDRESS    (1<<16)
378
-#define OP_LOCAL      (1<<17)
379
-#define OP_PARAM      (1<<18)
380
-#define OP_GLOBAL     (1<<19)
381
-#define OP_FAR        (1<<20)
382
-#define OP_IPREL      (1<<21)
383
-
384
-//
385
-// X86-specific flags (bits 27-31)
386
-//
387
-#define OP_MSR      (OP_SYS|OP_UNUSED)
388
-
389
-//
390
-// Other architecture flags
391
-//
392
-#define OP_DELAY  OP_UNUSED // delayed instruction (e.g., delayed branch that executes after the next instruction)
393
-
394
-/////////////////////////////////////////////////////////////////////
395
-// Architectures
396
-/////////////////////////////////////////////////////////////////////
397
-
398
-typedef enum _ARCHITECTURE_TYPE
399
-{
400
-	ARCH_UNKNOWN=0,
401
-	
402
-	// x86-based
403
-	ARCH_X86,    // 32-bit x86
404
-	ARCH_X86_16, // 16-bit x86
405
-	ARCH_X64,    // AMD64 and Intel EMD64
406
-	
407
-	// everything else
408
-	ARCH_ALPHA,
409
-	ARCH_ARM,
410
-	ARCH_DOTNET,
411
-	ARCH_EFI,
412
-	ARCH_IA64,
413
-	ARCH_M68K,
414
-	ARCH_MIPS,
415
-	ARCH_PPC,
416
-	ARCH_SH3,
417
-	ARCH_SH4,
418
-	ARCH_SPARC,
419
-	ARCH_THUMB
420
-
421
-} ARCHITECTURE_TYPE;
422
-
423
-typedef BOOL (*INIT_INSTRUCTION)(struct _INSTRUCTION *Instruction);
424
-typedef void (*DUMP_INSTRUCTION)(struct _INSTRUCTION *Instruction, BOOL ShowBytes, BOOL Verbose);
425
-typedef BOOL (*GET_INSTRUCTION)(struct _INSTRUCTION *Instruction, U8 *Address, U32 Flags);
426
-typedef U8 *(*FIND_FUNCTION_BY_PROLOGUE)(struct _INSTRUCTION *Instruction, U8 *StartAddress, U8 *EndAddress, U32 Flags);
427
-
428
-typedef struct _ARCHITECTURE_FORMAT_FUNCTIONS
429
-{
430
-	INIT_INSTRUCTION InitInstruction;
431
-	DUMP_INSTRUCTION DumpInstruction;
432
-	GET_INSTRUCTION GetInstruction;
433
-	FIND_FUNCTION_BY_PROLOGUE FindFunctionByPrologue;
434
-} ARCHITECTURE_FORMAT_FUNCTIONS;
435
-
436
-typedef struct _ARCHITECTURE_FORMAT
437
-{
438
-	ARCHITECTURE_TYPE Type;
439
-	ARCHITECTURE_FORMAT_FUNCTIONS *Functions;
440
-} ARCHITECTURE_FORMAT;
441
-
442
-#define DISASSEMBLER_INITIALIZED 0x1234566F
443
-#define INSTRUCTION_INITIALIZED 0x1234567F
444
-
445
-#include "disasm_x86.h"
446
-
447
-typedef struct DECLSPEC_ALIGN(16) _S128
448
-{
449
-    U64 Low;
450
-    S64 High;
451
-} S128;
452
-typedef struct DECLSPEC_ALIGN(16) _U128
453
-{
454
-    U64 Low;
455
-    U64 High;
456
-} U128;
457
-
458
-typedef struct _INSTRUCTION_OPERAND
459
-{
460
-	U32 Flags;
461
-	U8 Type : 6;
462
-	U8 Unused : 2;
463
-	U16 Length;
464
-	
465
-
466
-	// If non-NULL, this indicates the target address of the instruction (e.g., a branch or
467
-	// a displacement with no base register). However, this address is only reliable if the
468
-	// image is mapped correctly (e.g., the executable is mapped as an image and fixups have
469
-	// been applied if it is not at its preferred image base).
470
-	//
471
-	// If disassembling a 16-bit DOS application, TargetAddress is in the context of 
472
-	// X86Instruction->Segment. For example, if TargetAddress is the address of a code branch, 
473
-	// it is in the CS segment (unless X86Instruction->HasSegmentOverridePrefix is set). If 
474
-	// TargetAddress is a data pointer, it is in the DS segment (unless 
475
-	// X86Instruction->HasSegmentOverridePrefix is set)
476
-	U64 TargetAddress;
477
-	U32 Register;
478
-
479
-	union
480
-	{
481
-		// All 8/16/32-bit operands are extended to 64-bits automatically
482
-		// If you want to downcast, check whether Flags & OP_SIGNED is set
483
-		// Like this:
484
-		// U32 GetOperand32(OPERAND *Operand)
485
-		// {
486
-		//	if (Operand->Flags & OP_SIGNED) return (S32)Operand->Value_S64;
487
-		//	else return (U32)Operand->Value_U64;
488
-		//}
489
-		U64 Value_U64;
490
-		S64 Value_S64;
491
-		U128 Value_U128;
492
-		U128 Float128;
493
-		U8 Float80[80];
494
-		U8 BCD[10];
495
-	};
496
-} INSTRUCTION_OPERAND;
497
-
498
-typedef struct _INSTRUCTION
499
-{
500
-	U32 Initialized;
501
-	struct _DISASSEMBLER *Disassembler;
502
-
503
-	char String[MAX_OPCODE_DESCRIPTION];
504
-	U8 StringIndex;
505
-	U64 VirtualAddressDelta;
506
-
507
-	U32 Groups; // ITYPE_EXEC, ITYPE_ARITH, etc. -- NOTE groups can be OR'd together
508
-	INSTRUCTION_TYPE Type; // ITYPE_ADD, ITYPE_RET, etc. -- NOTE there is only one possible type
509
-
510
-	U8 *Address;
511
-	U8 *OpcodeAddress;
512
-	U32 Length;
513
-
514
-	U8 Prefixes[MAX_PREFIX_LENGTH];
515
-	U32 PrefixCount;
516
-
517
-	U8 LastOpcode; // last byte of opcode
518
-	U8 OpcodeBytes[MAX_OPCODE_LENGTH];
519
-	U32 OpcodeLength; // excludes any operands and prefixes
520
-
521
-	INSTRUCTION_OPERAND Operands[MAX_OPERAND_COUNT];
522
-	U32 OperandCount;
523
-
524
-	X86_INSTRUCTION X86;
525
-
526
-	DATA_REFERENCE DataSrc;
527
-	DATA_REFERENCE DataDst;
528
-	CODE_BRANCH CodeBranch;
529
-
530
-	// Direction depends on which direction the stack grows
531
-	// For example, on x86 a push results in StackChange < 0 since the stack grows down
532
-	// This is only relevant if (Group & ITYPE_STACK) is true
533
-	//
534
-	// If Groups & ITYPE_STACK is set but StackChange = 0, it means that the change
535
-	// couldn't be determined (non-constant)
536
-	LONG StackChange;
537
-
538
-	// Used to assist in debugging
539
-	// If set, the current instruction is doing something that requires special handling
540
-	// For example, popf can cause tracing to be disabled
541
-
542
-	U8 StringAligned : 1; // internal only
543
-	U8 NeedsEmulation : 1; // instruction does something that re
544
-	U8 Repeat : 1; // instruction repeats until some condition is met (e.g., REP prefix on X86)
545
-	U8 ErrorOccurred : 1; // set if instruction is invalid
546
-	U8 AnomalyOccurred : 1; // set if instruction is anomalous
547
-	U8 LastInstruction : 1; // tells the iterator callback it is the last instruction
548
-	U8 CodeBlockFirst: 1;
549
-	U8 CodeBlockLast : 1;
550
-} INSTRUCTION;
551
-
552
-typedef struct _DISASSEMBLER
553
-{
554
-	U32 Initialized;
555
-	ARCHITECTURE_TYPE ArchType;
556
-	ARCHITECTURE_FORMAT_FUNCTIONS *Functions;
557
-	INSTRUCTION Instruction;
558
-	U32 Stage1Count; // GetInstruction called
559
-	U32 Stage2Count; // Opcode fully decoded
560
-	U32 Stage3CountNoDecode;   // made it through all checks when DISASM_DECODE is not set
561
-	U32 Stage3CountWithDecode; // made it through all checks when DISASM_DECODE is set
562
-} DISASSEMBLER;
563
-
564
-#define DISASM_DISASSEMBLE         (1<<1)
565
-#define DISASM_DECODE              (1<<2)
566
-#define DISASM_SUPPRESSERRORS      (1<<3)
567
-#define DISASM_SHOWFLAGS           (1<<4)
568
-#define DISASM_ALIGNOUTPUT         (1<<5)
569
-#define DISASM_DISASSEMBLE_MASK (DISASM_ALIGNOUTPUT|DISASM_SHOWBYTES|DISASM_DISASSEMBLE)
570
-
571
-BOOL InitDisassembler(DISASSEMBLER *Disassembler, ARCHITECTURE_TYPE Architecture);
572
-void CloseDisassembler(DISASSEMBLER *Disassembler);
573
-INSTRUCTION *GetInstruction(DISASSEMBLER *Disassembler, U64 VirtualAddress, U8 *Address, U32 Flags);
574
-
575
-#ifdef __cplusplus
576
-}
577
-#endif
578
-#endif // DISASM_H

+ 0
- 4662
src/thirdparty/mhook/disasm-lib/disasm_x86.c
File diff suppressed because it is too large
View File


+ 0
- 837
src/thirdparty/mhook/disasm-lib/disasm_x86.h View File

@@ -1,837 +0,0 @@
1
-// Copyright (C) 2004, Matt Conover (mconover@gmail.com)
2
-#ifndef X86_DISASM_H
3
-#define X86_DISASM_H
4
-#ifdef __cplusplus
5
-extern "C" {
6
-#endif
7
-
8
-// NOTE: the processor may actually accept less than this amount (officially 15)
9
-// #define AMD64_MAX_INSTRUCTION_LEN 15 // theoretical max 25=5+2+1+1+8+8
10
-#define AMD64_MAX_PREFIX_LENGTH 5 // 4 legacy + 1 rex
11
-#define AMD64_MAX_ADDRESS_LENGTH 18 // modrm + sib + 8 byte displacement + 8 byte immediate value
12
-
13
-// NOTE: the processor may actually accept less than this amount (officially 15)
14
-#define X86_MAX_INSTRUCTION_LEN 15 // theoretical 16=4+2+1+1+4+4
15
-#define X86_MAX_PREFIX_LENGTH 4
16
-#define X86_MAX_OPCODE_LENGTH 3 // third byte is either a suffix or prefix
17
-#define X86_MAX_ADDRESS_LENGTH 10 // modrm + sib + 4 byte displacement + 4 byte immediate value
18
-#define X86_MAX_OPERANDS 3
19
-
20
-#define X86_PREFIX(a) ((a)->MnemonicFlags == ITYPE_EXT_PREFIX)
21
-#define X86_SPECIAL_EXTENSION(a) ((a)->MnemonicFlags & (ITYPE_EXT_MODRM|ITYPE_EXT_FPU|ITYPE_EXT_SUFFIX|ITYPE_EXT_64))
22
-#define X86_EXTENDED_OPCODE(a) ((a)->Table)
23
-#define X86_INVALID(a) (!(a)->MnemonicFlags && !(a)->Table)
24
-#define X86_OPERAND_COUNT(a) ((a)->OperandFlags[0] ? ((a)->OperandFlags[1] ? ((a)->OperandFlags[2] ? 3 : 2) : 1) : 0)
25
-#define X86_GET_CATEGORY(p) ((p)->MnemonicFlags & ITYPE_GROUP_MASK)
26
-#define X86_GET_TYPE(p) ((p)->MnemonicFlags & ITYPE_TYPE_MASK)
27
-
28
-// Various instructions being specially decoded
29
-#define X86_TWO_BYTE_OPCODE 0x0f
30
-#define PREFIX_SEGMENT_OVERRIDE_ES 0x26
31
-#define PREFIX_SEGMENT_OVERRIDE_CS 0x2e
32
-#define PREFIX_BRANCH_NOT_TAKEN 0x2e // used only with conditional jumps
33
-#define PREFIX_SEGMENT_OVERRIDE_SS 0x36
34
-#define PREFIX_SEGMENT_OVERRIDE_DS 0x3e
35
-#define PREFIX_BRANCH_TAKEN 0x3e // used only with conditional jumps
36
-#define PREFIX_SEGMENT_OVERRIDE_FS 0x64
37
-#define PREFIX_SEGMENT_OVERRIDE_GS 0x65
38
-#define PREFIX_OPERAND_SIZE 0x66
39
-#define PREFIX_ADDRESS_SIZE 0x67
40
-#define PREFIX_LOCK 0xf0
41
-#define PREFIX_REPNE 0xf2
42
-#define PREFIX_REP 0xf3
43
-
44
-//////////////////////////////////////////////////////////////////
45
-// Implicit operand handling
46
-//////////////////////////////////////////////////////////////////
47
-
48
-#define X86_AMODE_MASK   0x00FF0000 // bits 16-23 (AMODE_*)
49
-#define X86_OPFLAGS_MASK 0x0000FF80 // bits 7-15 (OPTYPE_*)
50
-#define X86_OPTYPE_MASK  0xFF0000FF // bits 0-7 (OPTYPE_* below + OP_REG) and 24-31 (OPTYPE_* above)
51
-
52
-#define OPTYPE_0   0x01
53
-#define OPTYPE_1   0x02
54
-#define OPTYPE_FF  0x03
55
-//...
56
-#define OPTYPE_CS  0x10
57
-#define OPTYPE_DS  0x11
58
-#define OPTYPE_ES  0x12
59
-#define OPTYPE_FS  0x13
60
-#define OPTYPE_GS  0x14
61
-#define OPTYPE_SS  0x15
62
-#define OPTYPE_CR0 0x16
63
-#define OPTYPE_TSC 0x17 // time stamp counter
64
-//...
65
-#define OPTYPE_FLAGS  0x20
66
-#define OPTYPE_xFLAGS 0x21 // RFLAGS/EFLAGS (depending on operand size)
67
-#define OPTYPE_xCX_HI_xBX_LO 0x22 // represented by 2 registers CX:BX or ECX:EBX (depending on operand size)
68
-#define OPTYPE_xDX_HI_xAX_LO 0x23 // DX:AX or EDX:EAX (depending on operand size)
69
-#define OPTYPE_EDX_HI_EAX_LO 0x24 // DX:AX or EDX:EAX (depending on operand size)
70
-#define OPTYPE_EDX_ECX_EBX_EAX 0x25 // all registers are set
71
-//...
72
-#define OPTYPE_STx 0x30
73
-#define OPTYPE_ST0 0x31
74
-#define OPTYPE_ST1 0x32
75
-#define OPTYPE_FPU_STATUS  0x33
76
-#define OPTYPE_FPU_CONTROL 0x34
77
-#define OPTYPE_FPU_TAG 0x35
78
-#define OPTYPE_FLDZ   0x36 // 0
79
-#define OPTYPE_FLD1   0x37 // 1
80
-#define OPTYPE_FLDPI  0x38 // pi
81
-#define OPTYPE_FLDL2T 0x39 // lg 10
82
-#define OPTYPE_FLDL2E 0x3A // lg e
83
-#define OPTYPE_FLDLG2 0x3B // log_10 2
84
-#define OPTYPE_FLDLN2 0x3C // log_e 2
85
-//...
86
-#define OPTYPE_CS_MSR 0x40
87
-#define OPTYPE_EIP_MSR 0x41
88
-#define OPTYPE_ESP_MSR 0x42
89
-#define OPTYPE_KERNELBASE_MSR 0x43
90
-#define OPTYPE_FMASK_MSR 0x44
91
-#define OPTYPE_STAR_MSR 0x45
92
-#define OPTYPE_CSTAR_MSR 0x46 // 32-bit mode
93
-#define OPTYPE_LSTAR_MSR 0x47 // 64-bit mode
94
-
95
-
96
-// NOTE: OPTYPES >= 0x80 reserved for registers (OP_REG+XX)
97
-#define OPTYPE_REG_AL OP_REG+0x01
98
-#define OPTYPE_REG_CL OP_REG+0x02
99
-#define OPTYPE_REG_AH OP_REG+0x03
100
-#define OPTYPE_REG_AX OP_REG+0x04
101
-#define OPTYPE_REG_DX OP_REG+0x05
102
-#define OPTYPE_REG_ECX OP_REG+0x06
103
-#define OPTYPE_REG8 OP_REG+0x07
104
-
105
-// If address size is 2, use BP
106
-// If address size is 4, use EBP
107
-// If address size is 8, use RBP
108
-#define OPTYPE_REG_xBP OP_REG+0x08
109
-
110
-// If address size is 2, use BP
111
-// If address size is 4, use EBP
112
-// If address size is 8, use RBP
113
-#define OPTYPE_REG_xSP OP_REG+0x09
114
-
115
-// If operand size is 2, take 8-bit register
116
-// If operand size is 4, take 16-bit register
117
-// If operand size is 8, take 32-bit register
118
-#define OPTYPE_REG_xAX_SMALL OP_REG+0x0a
119
-
120
-// If operand size is 2, take 16-bit register
121
-// If operand size is 4, take 32-bit register
122
-// If operand size is 8, take 64-bit register
123
-#define OPTYPE_REG_xAX_BIG OP_REG+0x0b
124
-
125
-typedef enum _CPU_TYPE
126
-{
127
-	CPU_UNKNOWN=0,
128
-
129
-	///////////////////////////////////////
130
-	// 1st generation
131
-	///////////////////////////////////////
132
-	// 1978
133
-	//CPU_8086 = 1MB address limit, 16-bit registers
134
-	// 1982
135
-	//CPU_i186
136
-
137
-	///////////////////////////////////////
138
-	// 2nd generation
139
-	///////////////////////////////////////
140
-	// 1982
141
-	//CPU_I286 // 16MB limit, 16-bit registers, added protected mode
142
-	CPU_I287, // CPU_I286 + math coprocessor
143
-
144
-	///////////////////////////////////////
145
-	// 3rd generation
146
-	///////////////////////////////////////
147
-	// 1985
148
-	CPU_I386, // 32-bit registers, 4GB memory limit
149
-	// 1988
150
-	CPU_I387, // CPU_I386 + math coprocessor
151
-
152
-	///////////////////////////////////////
153
-	// 4th generation (1989)
154
-	///////////////////////////////////////
155
-	CPU_I486,
156
-
157
-	///////////////////////////////////////
158
-	// 5th generation
159
-	///////////////////////////////////////
160
-	// 1993
161
-	CPU_PENTIUM, // superscalar architecture
162
-	// 1997
163
-	//CPU_PENTIUM_MMX
164
-	
165
-	///////////////////////////////////////
166
-	// 6th generation (1995)
167
-	///////////////////////////////////////
168
-	CPU_PENTIUM_PRO, // P6 architecture, no MMX, out-of-order execution, speculative execution
169
-	//CPU_CYRIX_6X86,
170
-	//CPU_AMD_K5 // RISC processor
171
-	// 1997
172
-	CPU_PENTIUM2, // Pentium Pro architecture + MMX
173
-	//CPU_AMD_K6,
174
-	//CPU_CYRIX_6X86MX, // Cyrix 6x86 + MMX
175
-	// 1998
176
-	CPU_AMD_K6_2, // added 3DNow! (MMX)
177
-	// 1999
178
-	// CPU_AMD_K6_3 // added SSE
179
-
180
-	///////////////////////////////////////
181
-	// 7th generation
182
-	///////////////////////////////////////
183
-	// 1999
184
-	CPU_PENTIUM3, // introduced SSE
185
-	// CPU_AMD_K7 // aka Athlon
186
-	// 2000
187
-	CPU_PENTIUM4, // introduced SSE2 and hyperthreading
188
-
189
-	// 2004? 2005?
190
-	CPU_PRESCOTT, // introduced SSE3
191
-
192
-	///////////////////////////////////////
193
-	// 8th generation (X86-64)
194
-	// IA32 instruction set with 64-bit extensions, >4GB RAM
195
-	///////////////////////////////////////
196
-
197
-	// 2003
198
-	CPU_AMD64, // includes Athlon 64 and Opteron aka X86-64
199
-
200
-	// 2004?
201
-	//CPU_EMD64 // Intel's version of AMD64
202
-	CPU_IA64 // aka Itanium: new instruction set -- adds JMPE to IA32 mode to return to IA64 native code
203
-
204
-} CPU_TYPE;
205
-
206
-//////////////////////////////////////////////////////////////////
207
-// Conditions (these can be OR'd)
208
-//////////////////////////////////////////////////////////////////
209
-
210
-// Used for Flags.Preconditions
211
-#define COND_O   (1<<0)  // overflow (signed)
212
-#define COND_C   (1<<1)  // below (unsigned)
213
-#define COND_Z   (1<<2)  // equal (unsigned)
214
-#define COND_S   (1<<3)  // sign set (signed)
215
-#define COND_P   (1<<4)  // parity even
216
-#define COND_BE  (1<<5)  // CF or ZF is set (unsigned)
217
-#define COND_L   (1<<6)  // (SF && !OF) || (OF && !SF)
218
-#define COND_LE  (1<<7)  // ZF || (SF && !OF) || (OF && !SF) (signed)
219
-#define COND_NO  (1<<8)  // !O
220
-#define COND_NC  (1<<9)  // !C (not below, above or equal to)
221
-#define COND_NZ  (1<<10) // !Z (not equal)
222
-#define COND_NS  (1<<11) // !S
223
-#define COND_NP  (1<<12) // !P (parity odd)
224
-#define COND_NL  (1<<13) // (!SF && !OF) || (SF && OF)
225
-#define COND_G   (1<<14) // !ZF && ((!SF && !OF) || (SF && OF))
226
-#define COND_D   (1<<15) // DF
227
-#define COND_REG_xCX_BIG_Z  (1<<16) // CX/ECX/RCX (depending on address size) == 0
228
-#define COND_REG_xCX_BIG_NZ (1<<17) // CX/ECX/RCX (depending on address size) != 0
229
-#define COND_OP1_EQ_OP2 (1<<18)
230
-#define COND_OP1_EQ_OP3 (1<<19)
231
-#define COND_B   COND_C
232
-#define COND_NAE COND_C
233
-#define COND_E   COND_Z
234
-#define COND_NA  COND_BE
235
-#define COND_PE  COND_P
236
-#define COND_U   COND_P
237
-#define COND_NGE COND_L
238
-#define COND_NG  COND_LE
239
-#define COND_PO  COND_NP
240
-#define COND_NU  COND_NP
241
-#define COND_NE  COND_NZ
242
-#define COND_NB  COND_NC
243
-#define COND_AE  COND_NC
244
-#define COND_NE  COND_NZ
245
-#define COND_A   (COND_NC|COND_NZ)
246
-#define COND_NBE COND_A
247
-#define COND_GE COND_NL
248
-#define COND_NLE COND_G
249
-
250
-// Used for Opcode.FlagsChanged
251
-#define FLAG_CF_SET (1<<0)
252
-#define FLAG_DF_SET (1<<1)
253
-#define FLAG_IF_SET (1<<2)
254
-#define FLAG_SET_MASK (FLAG_CF_SET|FLAG_DF_SET|FLAG_IF_SET)
255
-
256
-#define FLAG_SF_CLR (1<<3)
257
-#define FLAG_ZF_CLR (1<<4)
258
-#define FLAG_AF_CLR (1<<5)
259
-#define FLAG_CF_CLR (1<<6)
260
-#define FLAG_DF_CLR (1<<7)
261
-#define FLAG_IF_CLR (1<<8)
262
-#define FLAG_OF_CLR (1<<9)
263
-#define FPU_C0_CLR (1<<19)
264
-#define FPU_C1_CLR (1<<20)
265
-#define FPU_C2_CLR (1<<21)
266
-#define FPU_C3_CLR (1<<22)
267
-#define FPU_ALL_CLR (FPU_C0_CLR|FPU_C1_CLR|FPU_C2_CLR|FPU_C3_CLR)
268
-#define FLAG_CLR_MASK (FLAG_SF_CLR|FLAG_ZF_CLR|FLAG_AF_CLR|FLAG_CF_CLR|FLAG_DF_CLR|FLAG_IF_CLR|FLAG_OF_CLR|FPU_ALL_CLR)
269
-
270
-#define FLAG_OF_MOD (1<<10)
271
-#define FLAG_SF_MOD (1<<11)
272
-#define FLAG_ZF_MOD (1<<12)
273
-#define FLAG_AF_MOD (1<<13)
274
-#define FLAG_PF_MOD (1<<14)
275
-#define FLAG_CF_MOD (1<<15)
276
-#define FLAG_DF_MOD (1<<16)
277
-#define FLAG_IF_MOD (1<<17)
278
-#define FLAG_ALL_MOD (FLAG_OF_MOD|FLAG_SF_MOD|FLAG_ZF_MOD|FLAG_AF_MOD|FLAG_PF_MOD|FLAG_CF_MOD|FLAG_DF_MOD|FLAG_IF_MOD)
279
-#define FLAG_COMMON_MOD (FLAG_OF_MOD|FLAG_SF_MOD|FLAG_ZF_MOD|FLAG_AF_MOD|FLAG_PF_MOD|FLAG_CF_MOD)
280
-#define FPU_C0_MOD (1<<23)
281
-#define FPU_C1_MOD (1<<24)
282
-#define FPU_C2_MOD (1<<25)
283
-#define FPU_C3_MOD (1<<26)
284
-#define FPU_ALL_MOD (FPU_C0_MOD|FPU_C1_MOD|FPU_C2_MOD|FPU_C3_MOD)
285
-#define FLAG_MOD_MASK (FLAG_ALL_MOD|FPU_ALL_MOD)
286
-
287
-#define FLAG_CF_TOG (1<<18)
288
-#define FLAG_TOG_MASK FLAG_CF_TOG
289
-
290
-// Used for Opcode.ResultsIfTrue and Opcode.ResultsIfFalse
291
-#define OP1_DST         (1<<0)
292
-#define OP2_DST         (1<<1)
293
-#define OP3_DST         (1<<2)
294
-#define OP1_SRC         (1<<3)
295
-#define OP2_SRC         (1<<4)
296
-#define OP3_SRC         (1<<5)
297
-#define FPU_STACK_INC   (1<<6)
298
-#define FPU_STACK_INC2  (1<<7)
299
-#define FPU_STACK_DEC   (1<<8)
300
-#define SERIALIZE_WRITE (1<<9)
301
-#define SERIALIZE_READ  (1<<10)
302
-#define xCX_DEC         (1<<11)
303
-#define xCX_REP_DEC     (1<<12)
304
-#define xDI_DEC         (1<<13)
305
-#define xDI_INC         (1<<14)
306
-#define xSI_DEC         (1<<15)
307
-#define xSI_INC         (1<<16)
308
-#define xDI_DECx        (1<<17)
309
-#define xDI_INCx        (1<<18)
310
-#define xSI_DECx        (1<<19)
311
-#define xSI_INCx        (1<<20)
312
-#define FPU_STACK_PUSH FPU_STACK_DEC
313
-#define FPU_STACK_POP  FPU_STACK_INC
314
-#define FPU_STACK_POP2 FPU_STACK_INC2
315
-#define SERIALIZE_ALL (SERIALIZE_WRITE|SERIALIZE_READ)
316
-
317
-#define X86_SEGMENT_OFFSET 0x00
318
-#define X86_TEST_OFFSET    0x10
319
-#define X86_CONTROL_OFFSET 0x20
320
-#define X86_DEBUG_OFFSET   0x30
321
-#define X86_FPU_OFFSET     0x40
322
-#define X86_MMX_OFFSET     0x50
323
-#define X86_XMM_OFFSET     0x60
324
-#define X86_8BIT_OFFSET    0x70
325
-#define X86_16BIT_OFFSET   0x80
326
-#define X86_32BIT_OFFSET   0x90
327
-#define AMD64_8BIT_OFFSET  0xA0
328
-#define AMD64_16BIT_OFFSET 0xB0
329
-#define AMD64_32BIT_OFFSET 0xC0
330
-#define AMD64_64BIT_OFFSET 0xD0
331
-
332
-typedef enum _X86_REGISTER
333
-{
334
-	// Segments
335
-	X86_SEG_ES = X86_SEGMENT_OFFSET,
336
-	X86_SEG_CS,
337
-	X86_SEG_SS,
338
-	X86_SEG_DS,
339
-	X86_SEG_FS,
340
-	X86_SEG_GS,
341
-
342
-	// Miscellaneous
343
-	X86_REG_FLAGS,
344
-	X86_REG_EFLAGS,
345
-	AMD64_REG_RFLAGS,
346
-	X86_REG_IP,
347
-	X86_REG_EIP,
348
-	AMD64_REG_RIP,
349
-
350
-	// Test registers
351
-	X86_REG_TR0 = X86_TEST_OFFSET,
352
-	X86_REG_TR1,
353
-	X86_REG_TR2,
354
-	X86_REG_TR3,
355
-	X86_REG_TR4,
356
-	X86_REG_TR5,
357
-	X86_REG_TR6,
358
-	X86_REG_TR7,
359
-	X86_REG_TR8,
360
-	X86_REG_TR9,
361
-	X86_REG_TR10,
362
-	X86_REG_TR11,
363
-	X86_REG_TR12,
364
-	X86_REG_TR13,
365
-	X86_REG_TR14,
366
-	X86_REG_TR15,
367
-
368
-	// Control registers
369
-	X86_REG_CR0=X86_CONTROL_OFFSET,
370
-	X86_REG_CR1,
371
-	X86_REG_CR2,
372
-	X86_REG_CR3,
373
-	X86_REG_CR4,
374
-	X86_REG_CR5,
375
-	X86_REG_CR6,
376
-	X86_REG_CR7,
377
-	X86_REG_CR8,
378
-	X86_REG_CR9,
379
-	X86_REG_CR10,
380
-	X86_REG_CR11,
381
-	X86_REG_CR12,
382
-	X86_REG_CR13,
383
-	X86_REG_CR14,
384
-	X86_REG_CR15,
385
-
386
-	// Debug registers
387
-	X86_REG_DR0=X86_DEBUG_OFFSET,
388
-	X86_REG_DR1,
389
-	X86_REG_DR2,
390
-	X86_REG_DR3,
391
-	X86_REG_DR4,
392
-	X86_REG_DR5,
393
-	X86_REG_DR6,
394
-	X86_REG_DR7,
395
-	X86_REG_DR8,
396
-	X86_REG_DR9,
397
-	X86_REG_DR10,
398
-	X86_REG_DR11,
399
-	X86_REG_DR12,
400
-	X86_REG_DR13,
401
-	X86_REG_DR14,
402
-	X86_REG_DR15,
403
-
404
-	// FPU registers
405
-	X86_REG_ST0=X86_FPU_OFFSET,
406
-	X86_REG_ST1,
407
-	X86_REG_ST2,
408
-	X86_REG_ST3,
409
-	X86_REG_ST4,
410
-	X86_REG_ST5,
411
-	X86_REG_ST6,
412
-	X86_REG_ST7,
413
-
414
-	// MMX registers
415
-	X86_REG_MM0=X86_MMX_OFFSET,
416
-	X86_REG_MM1,
417
-	X86_REG_MM2,
418
-	X86_REG_MM3,
419
-	X86_REG_MM4,
420
-	X86_REG_MM5,
421
-	X86_REG_MM6,
422
-	X86_REG_MM7,
423
-
424
-	// XMM registers
425
-	X86_REG_XMM0=X86_XMM_OFFSET,
426
-	X86_REG_XMM1,
427
-	X86_REG_XMM2,
428
-	X86_REG_XMM3,
429
-	X86_REG_XMM4,
430
-	X86_REG_XMM5,
431
-	X86_REG_XMM6,
432
-	X86_REG_XMM7,
433
-
434
-	// 8-bit registers
435
-	X86_REG_AL=X86_8BIT_OFFSET,
436
-	X86_REG_CL,
437
-	X86_REG_DL,
438
-	X86_REG_BL,
439
-	X86_REG_AH,
440
-	X86_REG_CH,
441
-	X86_REG_DH,
442
-	X86_REG_BH,
443
-
444
-	// 16-bit registers
445
-	X86_REG_AX=X86_16BIT_OFFSET,
446
-	X86_REG_CX,
447
-	X86_REG_DX,
448
-	X86_REG_BX,
449
-	X86_REG_SP,
450
-	X86_REG_BP,
451
-	X86_REG_SI,
452
-	X86_REG_DI,
453
-
454
-	// 32-bit registers
455
-	X86_REG_EAX=X86_32BIT_OFFSET,
456
-	X86_REG_ECX,
457
-	X86_REG_EDX,
458
-	X86_REG_EBX,
459
-	X86_REG_ESP,
460
-	X86_REG_EBP,
461
-	X86_REG_ESI,
462
-	X86_REG_EDI,
463
-
464
-	// AMD64 8-bit registers
465
-	AMD64_REG_AL=AMD64_8BIT_OFFSET,
466
-	AMD64_REG_CL,
467
-	AMD64_REG_DL,
468
-	AMD64_REG_BL,
469
-	AMD64_REG_SPL,
470
-	AMD64_REG_BPL,
471
-	AMD64_REG_SIL,
472
-	AMD64_REG_DIL,
473
-	AMD64_REG_R8B,
474
-	AMD64_REG_R9B,
475
-	AMD64_REG_R10B,
476
-	AMD64_REG_R11B,
477
-	AMD64_REG_R12B,
478
-	AMD64_REG_R13B,
479
-	AMD64_REG_R14B,
480
-	AMD64_REG_R15B,
481
-
482
-	// AMD64 16-bit registers
483
-	AMD64_REG_AX=AMD64_16BIT_OFFSET,
484
-	AMD64_REG_CX,
485
-	AMD64_REG_DX,
486
-	AMD64_REG_BX,
487
-	AMD64_REG_SP,
488
-	AMD64_REG_BP,
489
-	AMD64_REG_SI,
490
-	AMD64_REG_DI,
491
-	AMD64_REG_R8W,
492
-	AMD64_REG_R9W,
493
-	AMD64_REG_R10W,
494
-	AMD64_REG_R11W,
495
-	AMD64_REG_R12W,
496
-	AMD64_REG_R13W,
497
-	AMD64_REG_R14W,
498
-	AMD64_REG_R15W,
499
-
500
-	// AMD64 32-bit registers
501
-	AMD64_REG_EAX=AMD64_32BIT_OFFSET,
502
-	AMD64_REG_ECX,
503
-	AMD64_REG_EDX,
504
-	AMD64_REG_EBX,
505
-	AMD64_REG_ESP,
506
-	AMD64_REG_EBP,
507
-	AMD64_REG_ESI,
508
-	AMD64_REG_EDI,
509
-	AMD64_REG_R8D,
510
-	AMD64_REG_R9D,
511
-	AMD64_REG_R10D,
512
-	AMD64_REG_R11D,
513
-	AMD64_REG_R12D,
514
-	AMD64_REG_R13D,
515
-	AMD64_REG_R14D,
516
-	AMD64_REG_R15D,
517
-
518
-	// AMD64 64-bit registers
519
-	AMD64_REG_RAX=AMD64_64BIT_OFFSET,
520
-	AMD64_REG_RCX,
521
-	AMD64_REG_RDX,
522
-	AMD64_REG_RBX,
523
-	AMD64_REG_RSP,
524
-	AMD64_REG_RBP,
525
-	AMD64_REG_RSI,
526
-	AMD64_REG_RDI,
527
-	AMD64_REG_R8,
528
-	AMD64_REG_R9,
529
-	AMD64_REG_R10,
530
-	AMD64_REG_R11,
531
-	AMD64_REG_R12,
532
-	AMD64_REG_R13,
533
-	AMD64_REG_R14,
534
-	AMD64_REG_R15
535
-} X86_REGISTER;
536
-
537
-typedef enum _X86_TEST_REGISTER
538
-{
539
-	REG_TR0=0,
540
-	REG_TR1,
541
-	REG_TR2,
542
-	REG_TR3,
543
-	REG_TR4,
544
-	REG_TR5,
545
-	REG_TR6,
546
-	REG_TR7,
547
-	REG_TR8,
548
-	REG_TR9,
549
-	REG_TR10,
550
-	REG_TR11,
551
-	REG_TR12,
552
-	REG_TR13,
553
-	REG_TR14,
554
-	REG_TR15
555
-} X86_TEST_REGISTER;
556
-
557
-typedef enum _X86_CONTROL_REGISTER
558
-{
559
-	REG_CR0,
560
-	REG_CR1,
561
-	REG_CR2,
562
-	REG_CR3,
563
-	REG_CR4,
564
-	REG_CR5,
565
-	REG_CR6,
566
-	REG_CR7,
567
-	REG_CR8,
568
-	REG_CR9,
569
-	REG_CR10,
570
-	REG_CR11,
571
-	REG_CR12,
572
-	REG_CR13,
573
-	REG_CR14,
574
-	REG_CR15
575
-} X86_CONTROL_REGISTER;
576
-
577
-typedef enum _X86_DEBUG_REGISTER
578
-{
579
-	REG_DR0,
580
-	REG_DR1,
581
-	REG_DR2,
582
-	REG_DR3,
583
-	REG_DR4,
584
-	REG_DR5,
585
-	REG_DR6,
586
-	REG_DR7,
587
-	REG_DR8,
588
-	REG_DR9,
589
-	REG_DR10,
590
-	REG_DR11,
591
-	REG_DR12,
592
-	REG_DR13,
593
-	REG_DR14,
594
-	REG_DR15
595
-} X86_DEBUG_REGISTER;
596
-
597
-typedef enum _X86_MMX_REGISTER
598
-{
599
-	REG_MM0=0,
600
-	REG_MM1=1,
601
-	REG_MM2=2,
602
-	REG_MM3=3,
603
-	REG_MM4=4,
604
-	REG_MM5=5,
605
-	REG_MM6=6,
606
-	REG_MM7=7
607
-} X86_MMX_REGISTER;
608
-
609
-typedef enum _X86_SSE_REGISTER
610
-{
611
-	REG_XMM0=0,
612
-	REG_XMM1=1,
613
-	REG_XMM2=2,
614
-	REG_XMM3=3,
615
-	REG_XMM4=4,
616
-	REG_XMM5=5,
617
-	REG_XMM6=6,
618
-	REG_XMM7=7
619
-} X86_SSE_REGISTER;
620
-
621
-typedef enum _X86_FPU_REGISTER
622
-{
623
-	REG_ST0=0,
624
-	REG_ST1=1,
625
-	REG_ST2=2,
626
-	REG_ST3=3,
627
-	REG_ST4=4,
628
-	REG_ST5=5,
629
-	REG_ST6=6,
630
-	REG_ST7=7
631
-} X86_FPU_REGISTER;
632
-
633
-typedef enum _X86_8BIT_REGISTER
634
-{
635
-	REG_AL = 0,
636
-	REG_CL = 1,
637
-	REG_DL = 2,
638
-	REG_BL = 3,
639
-	REG_AH = 4,
640
-	REG_CH = 5,
641
-	REG_DH = 6,
642
-	REG_BH = 7
643
-} X86_8BIT_REGISTER;
644
-
645
-typedef enum _X86_16BIT_REGISTER
646
-{
647
-	REG_AX = 0,
648
-	REG_CX = 1,
649
-	REG_DX = 2,
650
-	REG_BX = 3,
651
-	REG_SP = 4,
652
-	REG_BP = 5,
653
-	REG_SI = 6,
654
-	REG_DI = 7
655
-} X86_16BIT_REGISTER;
656
-
657
-typedef enum _X86_32BIT_REGISTER
658
-{
659
-	REG_EAX = 0,
660
-	REG_ECX = 1,
661
-	REG_EDX = 2,
662
-	REG_EBX = 3,
663
-	REG_ESP = 4,
664
-	REG_EBP = 5,
665
-	REG_ESI = 6,
666
-	REG_EDI = 7
667
-} X86_32BIT_REGISTER;
668
-
669
-typedef enum _X86_SEGMENT
670
-{
671
-	SEG_ES = 0,
672
-	SEG_CS = 1,
673
-	SEG_SS = 2,
674
-	SEG_DS = 3,
675
-	SEG_FS = 4,
676
-	SEG_GS = 5,
677
-	SEG_MAX = 6
678
-} X86_SEGMENT;
679
-
680
-extern char *X86_Registers[];
681
-
682
-#pragma pack(push,1)
683
-typedef struct _MODRM
684
-{
685
-	U8 mod : 2;
686
-	U8 reg : 3;
687
-	U8 rm : 3;
688
-} MODRM;
689
-typedef struct _SIB
690
-{
691
-	U8 scale : 2;
692
-	U8 index : 3;
693
-	U8 base : 3;
694
-} SIB;
695
-typedef struct _REX
696
-{
697
-	U8 unused : 4; // bits 4,5,6,7
698
-	U8 w : 1; // bit 3
699
-	U8 r : 1; // bit 2
700
-	U8 x : 1; // bit 1
701
-	U8 b : 1; // bit 0
702
-} REX;
703
-typedef struct _REX_MODRM
704
-{
705
-	U8 reg : 4;
706
-	U8 rm : 4;
707
-} REX_MODRM;
708
-typedef struct _REX_SIB
709
-{
710
-	U8 index : 4;
711
-	U8 base : 4;
712
-} REX_SIB;
713
-#pragma pack(pop)
714
-
715
-//
716
-// Properties:
717
-// If an operand is OP_COND_EXEC, it means that it is executed only if the pre-conditions are met.
718
-//
719
-// If if an instruction has one or more OP_COND_DST operands, then the actions are determined by
720
-// whether the Opcode.Preconditions are met or not. If all the COND_* flags in Opcode.Preconditions 
721
-// are true, then the results are determined by ResultsIfTrue. If the preconditions are not met, then
722
-// the results are determined by ResultsIfFalse.
723
-//
724
-// If Preconditions == NOCOND, then results in ResultsIfTrue are unconditional and ResultsIfFalse
725
-// is ignored
726
-//
727
-typedef struct _X86_OPCODE
728
-{
729
-	struct _X86_OPCODE *Table;
730
-	CPU_TYPE CPU; // minimum CPU (starting with i386)
731
-	U32 MnemonicFlags;
732
-	char Mnemonic[X86_MAX_INSTRUCTION_LEN+1];
733
-	U32 OperandFlags[X86_MAX_OPERANDS];
734
-	U32 Preconditions;
735
-	U32 FlagsChanged; // changes in flags
736
-	U32 ResultsIfTrue; // results if Preconditions are met
737
-	U32 ResultsIfFalse; // results if Preconditions are not met
738
-} X86_OPCODE;
739
-
740
-typedef struct _X86_INSTRUCTION
741
-{
742
-	struct _INSTRUCTION *Instruction; // the generic instruction format representing this instruction
743
-
744
-	X86_OPCODE Opcode;
745
-
746
-	U8 sib_b;
747
-	U8 modrm_b;
748
-	MODRM modrm;
749
-	SIB sib;
750
-	U8 rex_b;
751
-	REX rex;
752
-	REX_MODRM rex_modrm;
753
-	REX_SIB rex_sib;
754
-
755
-	X86_SEGMENT DstSegment;
756
-	union
757
-	{
758
-		X86_SEGMENT Segment;
759
-		DWORD Selector;
760
-	};
761
-
762
-	// NOTE: these are for internal use, use Instruction->Operands[]
763
-	//
764
-	// If DstRegAddressing or SrcRegAddressing = TRUE then BaseRegister is the base register
765
-	// It is the operand represented by SIBOperand
766
-	//
767
-	// The operand indices of the destination operands is in DstOpIndex[0 to DstOpCount-1]
768
-	// The operand indices of the source operands is in SrcOpIndex[0 to SrcOpCount-1]
769
-	//
770
-	// These are used both for instructions like xadd/xchg (where both operands are source/destination)
771
-	// and to represent implicit registers (e.g., cmpxchg)
772
-
773
-	U8 SrcOpIndex[3];
774
-	U8 DstOpIndex[3];
775
-
776
-	// Addressing mode:
777
-	// If DstRegAddressing = TRUE, then these apply to DstReg
778
-	// If SrcRegAddressing = TRUE, then this applies to SrcReg[AddressIndex]
779
-	// If both are false, then SrcReg and DstReg are not addresses
780
-	X86_REGISTER BaseRegister;
781
-	X86_REGISTER IndexRegister;
782
-	
783
-	U8 Scale;
784
-	U8 HasDefault64Operand : 1;
785
-	U8 HasOperandSizePrefix : 1;
786
-	U8 HasAddressSizePrefix : 1;
787
-	U8 HasSegmentOverridePrefix : 1;
788
-	U8 HasLockPrefix : 1;
789
-	U8 HasRepeatWhileEqualPrefix : 1;
790
-	U8 HasRepeatWhileNotEqualPrefix : 1;
791
-	U8 HasBranchTakenPrefix : 1;
792
-	U8 HasBranchNotTakenPrefix : 1;
793
-	U8 HasDstAddressing : 1;
794
-	U8 HasSrcAddressing : 1; 
795
-	U8 HasModRM : 1;
796
-	U8 HasBaseRegister : 1;
797
-	U8 HasIndexRegister : 1;
798
-	U8 HasFullDisplacement : 1;
799
-	U8 HasDstSegment : 1; // used for ins/cmps/scas/movs/etc which have 2 segments
800
-	U8 DstAddressIndex : 2; // DstOpIndex[DstAddressIndex]
801
-	U8 SrcAddressIndex : 2; // SrcOpIndex[SrcAddressIndex]
802
-	U8 DstOpCount : 2;
803
-	U8 SrcOpCount : 2;
804
-	U8 OperandSize : 4;
805
-	U8 AddressSize : 4;
806
-	U8 Relative : 1;
807
-	U8 HasSelector : 1; // segment is actually a selector
808
-	U8 Group : 5;
809
-
810
-	S64 Displacement;
811
-
812
-} X86_INSTRUCTION;
813
-
814
-////////////////////////////////////////////////////////////////////////////////////
815
-// Exported functions
816
-////////////////////////////////////////////////////////////////////////////////////
817
-
818
-extern ARCHITECTURE_FORMAT_FUNCTIONS X86;
819
-
820
-// Instruction setup
821
-BOOL X86_InitInstruction(struct _INSTRUCTION *Instruction);
822
-void X86_CloseInstruction(struct _INSTRUCTION *Instruction);
823
-
824
-// Instruction translator
825
-BOOL X86_TranslateInstruction(struct _INSTRUCTION *Instruction, BOOL Verbose);
826
-
827
-// Instruction decoder
828
-BOOL X86_GetInstruction(struct _INSTRUCTION *Instruction, U8 *Address, DWORD Flags);
829
-
830
-// Function finding
831
-U8 *X86_FindFunctionByPrologue(struct _INSTRUCTION *Instruction, U8 *StartAddress, U8 *EndAddress, DWORD Flags);
832
-
833
-#ifdef __cplusplus
834
-}
835
-#endif
836
-#endif // X86_DISASM_H
837
-

+ 0
- 3654
src/thirdparty/mhook/disasm-lib/disasm_x86_tables.h
File diff suppressed because it is too large
View File


+ 0
- 185
src/thirdparty/mhook/disasm-lib/misc.c View File

@@ -1,185 +0,0 @@
1
-// Copyright (C) 2002, Matt Conover (mconover@gmail.com)
2
-#include "misc.h"
3
-
4
-BOOL IsHexChar(BYTE ch)
5
-{
6
-	switch (ch)
7
-	{
8
-		case '0': case '1': case '2': case '3': 
9
-		case '4': case '5': case '6': case '7': 
10
-		case '8': case '9': 
11
-		case 'A': case 'a': case 'B': case 'b':
12
-		case 'C': case 'c': case 'D': case 'd':
13
-		case 'E': case 'e': case 'F': case 'f':
14
-			return TRUE;
15
-		default:
16
-			return FALSE;
17
-	}
18
-}
19
-
20
-// NOTE: caller must free the buffer returned
21
-BYTE *HexToBinary(char *Input, DWORD InputLength, DWORD *OutputLength)
22
-{
23
-	DWORD i, j, ByteCount = 0;
24
-	char temp_byte[3];
25
-	BYTE *p, *ByteString = NULL;
26
-
27
-	if (!InputLength || !OutputLength) return NULL;
28
-	else *OutputLength = 0;
29
-
30
-	while (*Input && isspace(*Input)) { Input++; InputLength--; }
31
-	if (!*Input) return NULL;
32
-	if (Input[0] == '\"') { Input++; InputLength--; }
33
-	p = (BYTE *)strchr(Input, '\"');
34
-	if (p) InputLength--;
35
-
36
-	if (InputLength > 2 && Input[2] == ' ') // assume spaces
37
-	{
38
-		for (i = 0; i < InputLength; i += 3)
39
-		{
40
-			while (i < InputLength && isspace(Input[i])) i++; // skip over extra space, \r, and \n
41
-			if (i >= InputLength) break;
42
-
43
-			if (!IsHexChar(Input[i]))
44
-			{
45
-				//fprintf(stderr, "ERROR: invalid hex character at offset %lu (0x%04x)\n", i, i);
46
-				goto abort;
47
-			}
48
-
49
-			if (i+1 >= InputLength || !Input[i+1])
50
-			{
51
-				//fprintf(stderr, "ERROR: hex string terminates unexpectedly at offset %lu (0x%04x)\n", i+1, i+1);
52
-				goto abort;
53
-			}
54
-
55
-			if (i+2 < InputLength && Input[i+2] && !isspace(Input[i+2]))
56
-			{
57
-				//fprintf(stderr, "ERROR: Hex string is malformed at offset %lu (0x%04x)\n", i, i);
58
-				//fprintf(stderr, "Found '%c' (0x%02x) instead of space\n", Input[i+2], Input[i+2]);
59
-				goto abort;
60
-			}
61
-
62
-			ByteCount++;
63
-		}
64
-
65
-		if (!ByteCount)
66
-		{
67
-			//fprintf(stderr, "Error: no input (byte count = 0)\n");
68
-			goto abort;
69
-		}
70
-
71
-		ByteString = malloc(ByteCount+1);
72
-		if (!ByteString)
73
-		{
74
-			//fprintf(stderr, "ERROR: failed to allocate %lu bytes\n", ByteCount);
75
-			goto abort;
76
-		}
77
-			
78
-		memset(ByteString, 0, ByteCount+1);
79
-		for (i = 0, j = 0; j < ByteCount; i += 3, j++)
80
-		{			
81
-			while (isspace(Input[i])) i++; // skip over extra space, \r, and \n
82
-			temp_byte[0] = Input[i];
83
-			temp_byte[1] = Input[i+1];
84
-			temp_byte[2] = 0;
85
-			ByteString[j] = (BYTE)strtoul(temp_byte, NULL, 16);
86
-		}
87
-	}
88
-	else if (InputLength > 2 && Input[0] == '\\')
89
-	{
90
-		for (i = 0; i < InputLength; i += 2)
91
-		{
92
-			if (Input[i] != '\\' || (Input[i+1] != 'x' && Input[i+1] != '0'))
93
-			{
94
-				//fprintf(stderr, "ERROR: invalid hex character at offset %lu (0x%04x)\n", i, i);
95
-				goto abort;
96
-			}
97
-			i += 2;
98
-
99
-			if (!IsHexChar(Input[i]))
100
-			{
101
-				//fprintf(stderr, "ERROR: invalid hex character at offset %lu (0x%04x)\n", i, i);
102
-				goto abort;
103
-			}
104
-			if (i+1 >= InputLength || !Input[i+1])
105
-			{
106
-				//fprintf(stderr, "ERROR: hex string terminates unexpectedly at offset %lu (0x%04x)\n", i+1, i+1);
107
-				goto abort;
108
-			}
109
-
110
-			ByteCount++;
111
-		}
112
-
113
-		if (!ByteCount)
114
-		{
115
-			//fprintf(stderr, "Error: no input (byte count = 0)\n");
116
-			goto abort;
117
-		}
118
-
119
-		ByteString = malloc(ByteCount+1);
120
-		if (!ByteString)
121
-		{
122
-			//fprintf(stderr, "ERROR: failed to allocate %lu bytes\n", ByteCount);
123
-			goto abort;
124
-		}
125
-			
126
-		memset(ByteString, 0, ByteCount+1);
127
-		for (i = j = 0; j < ByteCount; i += 2, j++)
128
-		{
129
-			i += 2;
130
-			temp_byte[0] = Input[i];
131
-			temp_byte[1] = Input[i+1];
132
-			temp_byte[2] = 0;
133
-			ByteString[j] = (BYTE)strtoul(temp_byte, NULL, 16);
134
-		}
135
-	}
136
-	else // assume it is a hex string with no spaces with 2 bytes per character
137
-	{
138
-		for (i = 0; i < InputLength; i += 2)
139
-		{
140
-				if (!IsHexChar(Input[i]))
141
-			{
142
-				//fprintf(stderr, "ERROR: invalid hex character at offset %lu (0x%04x)\n", i, i);
143
-				goto abort;
144
-			}
145
-			if (i+1 >= InputLength || !Input[i+1])
146
-			{
147
-				//fprintf(stderr, "ERROR: hex string terminates unexpectedly at offset %lu (0x%04x)\n", i+1, i+1);
148
-				goto abort;
149
-			}
150
-
151
-			ByteCount++;
152
-		}
153
-
154
-		if (!ByteCount)
155
-		{
156
-			//fprintf(stderr, "Error: no input (byte count = 0)\n");
157
-			goto abort;
158
-		}
159
-
160
-		ByteString = malloc(ByteCount+1);
161
-		if (!ByteString)
162
-		{
163
-			//fprintf(stderr, "ERROR: failed to allocate %lu bytes\n", ByteCount);
164
-			goto abort;
165
-		}
166
-			
167
-		memset(ByteString, 0, ByteCount+1);
168
-		for (i = 0, j = 0; j < ByteCount; i += 2, j++)
169
-		{
170
-			temp_byte[0] = Input[i];
171
-			temp_byte[1] = Input[i+1];
172
-			temp_byte[2] = 0;
173
-			ByteString[j] = (BYTE)strtoul(temp_byte, NULL, 16);
174
-		}
175
-	}
176
-
177
-	*OutputLength = ByteCount;
178
-	return ByteString;
179
-
180
-abort:
181
-	if (OutputLength) *OutputLength = 0;
182
-	if (ByteString) free(ByteString);
183
-	return NULL;
184
-}
185
-

+ 0
- 52
src/thirdparty/mhook/disasm-lib/misc.h View File

@@ -1,52 +0,0 @@
1
-// Copyright (C) 2002, Matt Conover (mconover@gmail.com)
2
-#ifndef MISC_H
3
-#define MISC_H
4
-#ifdef __cplusplus
5
-extern "C" {
6
-#endif
7
-
8
-#include <windows.h>
9
-#include <stdio.h>
10
-#include <stdarg.h>
11
-#include <assert.h>
12
-
13
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
14
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
15
-
16
-// NOTE: start is inclusive, end is exclusive (as in start <= x < end)
17
-#define IS_IN_RANGE(x, s, e) \
18
-( \
19
-	((ULONG_PTR)(x) == (ULONG_PTR)(s) && (ULONG_PTR)(x) == (ULONG_PTR)(e)) || \
20
-	((ULONG_PTR)(x) >= (ULONG_PTR)(s) && (ULONG_PTR)(x) < (ULONG_PTR)(e)) \
21
-)
22
-
23
-#if _MSC_VER >= 1400
24
-#pragma warning(disable:4996)
25
-#endif
26
-
27
-#if defined(_WIN64)
28
-	#define VALID_ADDRESS_MAX 0x7FFEFFFFFFFFFFFF // Win64 specific
29
-	typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
30
-#else
31
-	#define VALID_ADDRESS_MAX 0x7FFEFFFF // Win32 specific
32
-	typedef unsigned long ULONG_PTR, *PULONG_PTR;
33
-#endif
34
-
35
-#ifndef DECLSPEC_ALIGN
36
-	#if (_MSC_VER >= 1300) && !defined(MIDL_PASS)
37
-		#define DECLSPEC_ALIGN(x) __declspec(align(x))
38
-	#else
39
-		#define DECLSPEC_ALIGN(x)
40
-	#endif
41
-#endif
42
-
43
-#define VALID_ADDRESS_MIN 0x10000    // Win32 specific
44
-#define IS_VALID_ADDRESS(a) IS_IN_RANGE(a, VALID_ADDRESS_MIN, VALID_ADDRESS_MAX+1)
45
-
46
-BOOL IsHexChar(BYTE ch);
47
-BYTE *HexToBinary(char *Input, DWORD InputLength, DWORD *OutputLength);
48
-
49
-#ifdef __cplusplus
50
-}
51
-#endif
52
-#endif // MISC_H

+ 0
- 813
src/thirdparty/mhook/mhook-lib/mhook.cpp View File

@@ -1,813 +0,0 @@
1
-//Copyright (c) 2007-2008, Marton Anka
2
-//
3
-//Permission is hereby granted, free of charge, to any person obtaining a 
4
-//copy of this software and associated documentation files (the "Software"), 
5
-//to deal in the Software without restriction, including without limitation 
6
-//the rights to use, copy, modify, merge, publish, distribute, sublicense, 
7
-//and/or sell copies of the Software, and to permit persons to whom the 
8
-//Software is furnished to do so, subject to the following conditions:
9
-//
10
-//The above copyright notice and this permission notice shall be included 
11
-//in all copies or substantial portions of the Software.
12
-//
13
-//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
14
-//OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
15
-//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
16
-//THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
17
-//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
18
-//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
19
-//IN THE SOFTWARE.
20
-
21
-#include <windows.h>
22
-#include <tlhelp32.h>
23
-#include <stdio.h>
24
-#include "mhook.h"
25
-#include "../disasm-lib/disasm.h"
26
-
27
-//=========================================================================
28
-#ifndef cntof
29
-#define cntof(a) (sizeof(a)/sizeof(a[0]))
30
-#endif
31
-
32
-//=========================================================================
33
-#ifndef GOOD_HANDLE
34
-#define GOOD_HANDLE(a) ((a!=INVALID_HANDLE_VALUE)&&(a!=NULL))
35
-#endif
36
-
37
-//=========================================================================
38
-#ifndef gle
39
-#define gle GetLastError
40
-#endif
41
-
42
-//=========================================================================
43
-#ifndef ODPRINTF
44
-
45
-#ifdef _DEBUG
46
-#define ODPRINTF(a) odprintf a
47
-#else
48
-#define ODPRINTF(a)
49
-#endif
50
-
51
-inline void __cdecl odprintf(PCSTR format, ...) {
52
-	va_list	args;
53
-	va_start(args, format);
54
-	int len = _vscprintf(format, args);
55
-	if (len > 0) {
56
-		len += (1 + 2);
57
-		PSTR buf = (PSTR) malloc(len);
58
-		if (buf) {
59
-			len = vsprintf_s(buf, len, format, args);
60
-			if (len > 0) {
61
-				while (len && isspace(buf[len-1])) len--;
62
-				buf[len++] = '\r';
63
-				buf[len++] = '\n';
64
-				buf[len] = 0;
65
-				OutputDebugStringA(buf);
66
-			}
67
-			free(buf);
68
-		}
69
-		va_end(args);
70
-	}
71
-}
72
-
73
-inline void __cdecl odprintf(PCWSTR format, ...) {
74
-	va_list	args;
75
-	va_start(args, format);
76
-	int len = _vscwprintf(format, args);
77
-	if (len > 0) {
78
-		len += (1 + 2);
79
-		PWSTR buf = (PWSTR) malloc(sizeof(WCHAR)*len);
80
-		if (buf) {
81
-			len = vswprintf_s(buf, len, format, args);
82
-			if (len > 0) {
83
-				while (len && iswspace(buf[len-1])) len--;
84
-				buf[len++] = L'\r';
85
-				buf[len++] = L'\n';
86
-				buf[len] = 0;
87
-				OutputDebugStringW(buf);
88
-			}
89
-			free(buf);
90
-		}
91
-		va_end(args);
92
-	}
93
-}
94
-
95
-#endif //#ifndef ODPRINTF
96
-
97
-//=========================================================================
98
-#define MHOOKS_MAX_CODE_BYTES	32
99
-#define MHOOKS_MAX_RIPS			 4
100
-
101
-//=========================================================================
102
-// The trampoline structure - stores every bit of info about a hook
103
-struct MHOOKS_TRAMPOLINE {
104
-	PBYTE	pSystemFunction;								// the original system function
105
-	DWORD	cbOverwrittenCode;								// number of bytes overwritten by the jump
106
-	PBYTE	pHookFunction;									// the hook function that we provide
107
-	BYTE	codeJumpToHookFunction[MHOOKS_MAX_CODE_BYTES];	// placeholder for code that jumps to the hook function
108
-	BYTE	codeTrampoline[MHOOKS_MAX_CODE_BYTES];			// placeholder for code that holds the first few
109
-															//   bytes from the system function and a jump to the remainder
110
-															//   in the original location
111
-	BYTE	codeUntouched[MHOOKS_MAX_CODE_BYTES];			// placeholder for unmodified original code
112
-															//   (we patch IP-relative addressing)
113
-};
114
-
115
-
116
-//=========================================================================
117
-// The patch data structures - store info about rip-relative instructions
118
-// during hook placement
119
-struct MHOOKS_RIPINFO
120
-{
121
-	DWORD	dwOffset;
122
-	S64		nDisplacement;
123
-};
124
-
125
-struct MHOOKS_PATCHDATA
126
-{
127
-	S64				nLimitUp;
128
-	S64				nLimitDown;
129
-	DWORD			nRipCnt;
130
-	MHOOKS_RIPINFO	rips[MHOOKS_MAX_RIPS];
131
-};
132
-
133
-//=========================================================================
134
-// Global vars
135
-static BOOL g_bVarsInitialized = FALSE;
136
-static CRITICAL_SECTION g_cs;
137
-static MHOOKS_TRAMPOLINE* g_pHooks[MHOOKS_MAX_SUPPORTED_HOOKS];
138
-static DWORD g_nHooksInUse = 0;
139
-static HANDLE* g_hThreadHandles = NULL;
140
-static DWORD g_nThreadHandles = 0;
141
-#define MHOOK_JMPSIZE 5
142
-
143
-//=========================================================================
144
-// Toolhelp defintions so the functions can be dynamically bound to
145
-typedef HANDLE (WINAPI * _CreateToolhelp32Snapshot)(
146
-	DWORD dwFlags,       
147
-	DWORD th32ProcessID  
148
-	);
149
-
150
-typedef BOOL (WINAPI * _Thread32First)(
151
-									   HANDLE hSnapshot,     
152
-									   LPTHREADENTRY32 lpte
153
-									   );
154
-
155
-typedef BOOL (WINAPI * _Thread32Next)(
156
-									  HANDLE hSnapshot,     
157
-									  LPTHREADENTRY32 lpte
158
-									  );
159
-
160
-//=========================================================================
161
-// Bring in the toolhelp functions from kernel32
162
-_CreateToolhelp32Snapshot fnCreateToolhelp32Snapshot = (_CreateToolhelp32Snapshot) GetProcAddress(GetModuleHandle(L"kernel32"), "CreateToolhelp32Snapshot");
163
-_Thread32First fnThread32First = (_Thread32First) GetProcAddress(GetModuleHandle(L"kernel32"), "Thread32First");
164
-_Thread32Next fnThread32Next = (_Thread32Next) GetProcAddress(GetModuleHandle(L"kernel32"), "Thread32Next");
165
-
166
-//=========================================================================
167
-static VOID EnterCritSec() {
168
-	if (!g_bVarsInitialized) {
169
-		InitializeCriticalSection(&g_cs);
170
-		ZeroMemory(g_pHooks, sizeof(g_pHooks));
171
-		g_bVarsInitialized = TRUE;
172
-	}
173
-	EnterCriticalSection(&g_cs);
174
-}
175
-
176
-//=========================================================================
177
-static VOID LeaveCritSec() {
178
-	LeaveCriticalSection(&g_cs);
179
-}
180
-
181
-//=========================================================================
182
-// Internal function:
183
-// 
184
-// Skip over jumps that lead to the real function. Gets around import
185
-// jump tables, etc.
186
-//=========================================================================
187
-static PBYTE SkipJumps(PBYTE pbCode) {
188
-#ifdef _M_IX86_X64
189
-	if (pbCode[0] == 0xff && pbCode[1] == 0x25) {
190
-#ifdef _M_IX86
191
-		// on x86 we have an absolute pointer...
192
-		PBYTE pbTarget = *(PBYTE *)&pbCode[2];
193
-		// ... that shows us an absolute pointer.
194
-		return SkipJumps(*(PBYTE *)pbTarget);
195
-#elif defined _M_X64
196
-		// on x64 we have a 32-bit offset...
197
-		INT32 lOffset = *(INT32 *)&pbCode[2];
198
-		// ... that shows us an absolute pointer
199
-		return SkipJumps(*(PBYTE*)(pbCode + 6 + lOffset));
200
-	} else if (pbCode[0] == 0x48 && pbCode[1] == 0xff && pbCode[2] == 0x25) {
201
-		// or we can have the same with a REX prefix
202
-		INT32 lOffset = *(INT32 *)&pbCode[3];
203
-		// ... that shows us an absolute pointer
204
-		return SkipJumps(*(PBYTE*)(pbCode + 7 + lOffset));
205
-#endif
206
-	} else if (pbCode[0] == 0xe9) {
207
-		// here the behavior is identical, we have...
208
-		// ...a 32-bit offset to the destination.
209
-		return SkipJumps(pbCode + 5 + *(INT32 *)&pbCode[1]);
210
-	} else if (pbCode[0] == 0xeb) {
211
-		// and finally an 8-bit offset to the destination
212
-		return SkipJumps(pbCode + 2 + *(CHAR *)&pbCode[1]);
213
-	}
214
-#else
215
-#error unsupported platform
216
-#endif
217
-	return pbCode;
218
-}
219
-
220
-//=========================================================================
221
-// Internal function: