forked from Satanic-Spirit/Tickrate-Enabler
-
Notifications
You must be signed in to change notification settings - Fork 9
/
basicbinpatch.h
152 lines (135 loc) · 4.35 KB
/
basicbinpatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* vim: set ts=4 :
* =============================================================================
* BasicBinPatch
* Copyright (C) 2012 Michael "ProdigySim" Busby
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the author(s) give you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the author(s) grant
* this exception to all derivative works. The author(s) define further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#ifndef _BASICBINPATCH_H_
#define _BASICBINPATCH_H_
#include "codepatch/icodepatch.h"
#include "memutils.h"
// Basic binary patch that can be calculated and cached ahead of time
// Only needing to store the "restore" and "replacement" bytes in order for infinite patch/unpatch cycles
class BasicBinPatch : public ICodePatch
{
public:
// target: ptr to code to be patched
// replacement: ptr to byte array containing patch (replacement) bytes
// length: length of patch
// patchTimeCopy: Should we copy the original code back out every time we Patch, or is during construction enough?
BasicBinPatch(BYTE * target, const BYTE * replacement, size_t length, bool patchTimeCopy=false)
: m_length(length), m_pTarget(target), m_pReplacement(NULL), m_pOriginal(NULL),
m_bFirstTime(true), m_bPatched(false), m_bPatchTimeCopy(patchTimeCopy)
{
m_pOriginal = new BYTE[length+length];
m_pReplacement = m_pOriginal + length;
memcpy(m_pReplacement, replacement, length);
if(!patchTimeCopy)
{
memcpy(m_pOriginal, target, length);
}
}
~BasicBinPatch()
{
delete m_pOriginal;
}
void Patch()
{
if(m_bPatched) return;
if(m_bFirstTime)
{
g_MemUtils.SetMemPatchable(m_pTarget, m_length);
m_bFirstTime = false;
}
if(m_bPatchTimeCopy)
{
memcpy(m_pOriginal, m_pTarget, m_length);
}
memcpy(m_pTarget, m_pReplacement, m_length);
m_bPatched = true;
}
void Unpatch()
{
if(!m_bPatched) return;
memcpy(m_pTarget, m_pOriginal, m_length);
}
private:
size_t m_length;
BYTE * m_pTarget;
BYTE * m_pReplacement;
BYTE * m_pOriginal;
bool m_bFirstTime;
bool m_bPatched;
bool m_bPatchTimeCopy;
};
// Basic binary patch that can be calculated and cached ahead of time, and size is known at compile time
// Only needing to store the "restore" and "replacement" bytes in order for infinite patch/unpatch cycles
template <size_t LENGTH>
class BasicStaticBinPatch : public ICodePatch
{
public:
// target: ptr to code to be patched
// replacement: ptr to byte array containing patch (replacement) bytes
// patchTimeCopy: Should we copy the original code back out every time we Patch, or is during construction enough?
BasicStaticBinPatch(BYTE * target, const BYTE * replacement, bool patchTimeCopy=false)
: m_pTarget(target), m_bFirstTime(true), m_bPatchTimeCopy(patchTimeCopy), m_bPatched(false)
{
memcpy(m_replacement, replacement, LENGTH);
if(!patchTimeCopy)
{
memcpy(m_original, target, LENGTH);
}
}
void Patch()
{
if(m_bPatched) return;
if(m_bFirstTime)
{
g_MemUtils.SetMemPatchable(m_pTarget, LENGTH);
m_bFirstTime = false;
}
if(m_bPatchTimeCopy)
{
memcpy(m_original, m_pTarget, LENGTH);
}
memcpy(m_pTarget, m_replacement, LENGTH);
m_bPatched = true;
}
void Unpatch()
{
if(!m_bPatched) return;
memcpy(m_pTarget, m_original, LENGTH);
}
private:
BYTE * m_pTarget;
BYTE m_replacement[LENGTH];
BYTE m_original[LENGTH];
bool m_bFirstTime;
bool m_bPatchTimeCopy;
bool m_bPatched;
};
#endif