-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.cpp
155 lines (121 loc) · 3.39 KB
/
patch.cpp
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
153
154
155
#include <patch.h>
#include <utility>
#include <vector>
#include <string>
#include <cstring>
#include <asm/asm.h>
#include <macro-assembler-x86.h>
#include <jit/jit_helpers.h>
#include <CDetour/detourhelpers.h>
std::vector<std::pair<size_t, size_t>> SplitString(const std::string &string, char character)
{
std::vector<std::pair<size_t, size_t>> slices;
size_t pos = string.find_first_not_of(character);
size_t next;
while ((next = string.find_first_of(character, pos)) != std::string::npos) {
slices.push_back(std::make_pair(pos, next - pos));
pos = string.find_first_not_of(character, next);
}
if (pos != std::string::npos)
slices.push_back(std::make_pair(pos, string.length() - pos));
return slices;
}
PatchMask::PatchMask(const std::string &mask_string)
{
auto slices = SplitString(mask_string, ' ');
for (size_t index = 0; index < slices.size(); index++) {
size_t pos = slices[index].first;
size_t len = slices[index].second;
if (len == 1) {
switch (mask_string[pos]) {
case 'i':
mask.push_back(PatchMask::EIGNORE);
continue;
case 'n':
mask.push_back(PatchMask::ENOP);
continue;
}
}
size_t len_parse = 0;
int shift = std::stoi(mask_string.substr(pos, len), &len_parse);
if (len_parse == len) {
mask.push_back(shift);
continue;
}
// parsing failed
mask.clear();
break;
}
}
size_t PatchMask::WritableSize() const
{
int lowest_shift = mask.size();
size_t index;
for (index = 0; index < mask.size(); index++) {
int entry = mask[index];
if (entry == PatchMask::EIGNORE || lowest_shift == 0)
break;
if (entry != PatchMask::ENOP)
if (entry < lowest_shift)
lowest_shift = entry;
lowest_shift--;
}
return index;
}
Patcher::Patcher(void* base, PatchMask mask)
: base(base), mask(mask)
{
if (!base || !mask.Valid())
return;
original = new char[mask.MaskSize()];
std::memcpy(original, base, mask.MaskSize());
}
Patcher::~Patcher()
{
if (Valid()) {
UnPatch();
delete[] original;
}
}
bool Patcher::ApplyMask()
{
if (!Valid() || !writable || patched)
return false;
for (int offset = mask.MaskSize() - 1; offset >= 0; offset--) {
int entry = mask[offset];
switch (entry) {
case PatchMask::EIGNORE:
continue;
case PatchMask::ENOP:
*((unsigned char*)base + offset) = 0x90;
continue;
default:
*((unsigned char*)base + offset + entry) = *((char*)base + offset);
*((unsigned char*)base + offset) = 0x90;
continue;
}
}
return true;
}
bool Patcher::Patch(sp::MacroAssembler &masm)
{
if (!Valid() || patched)
return false;
if (masm.length() > mask.WritableSize())
return false;
if (!writable) {
SetMemPatchable(base, mask.MaskSize());
writable = true;
}
ApplyMask();
masm.emitToExecutableMemory(base);
patched = true;
return true;
}
void Patcher::UnPatch()
{
if (!patched)
return;
std::memcpy(base, original, mask.MaskSize());
patched = false;
}