-
Notifications
You must be signed in to change notification settings - Fork 11
/
patch.c
149 lines (110 loc) · 3.53 KB
/
patch.c
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
/*
* ff7_opengl - Complete OpenGL replacement of the Direct3D renderer used in
* the original ports of Final Fantasy VII and Final Fantasy VIII for the PC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
/*
* patch.c - helper functions for runtime code patching
*/
#include <windows.h>
#include "types.h"
uint replace_counter = 0;
uint replaced_functions[512 * 3];
uint replace_function(uint offset, void *func)
{
uint dummy;
VirtualProtect((void *)offset, 5, PAGE_EXECUTE_READWRITE, &dummy);
replaced_functions[replace_counter++] = *(unsigned char *)offset;
replaced_functions[replace_counter++] = *(uint *)(offset + 1);
replaced_functions[replace_counter++] = offset;
*(unsigned char *)offset = 0xE9;
*(uint *)(offset + 1) = ((uint)func - offset) - 5;
return replace_counter - 3;
}
void unreplace_function(uint func)
{
uint offset = replaced_functions[func + 2];
uint dummy;
VirtualProtect((void *)offset, 5, PAGE_EXECUTE_READWRITE, &dummy);
*(uint *)(offset + 1) = replaced_functions[func + 1];
*(unsigned char *)offset = replaced_functions[func];
}
void unreplace_functions()
{
while(replace_counter > 0)
{
uint offset = replaced_functions[--replace_counter];
uint dummy;
VirtualProtect((void *)offset, 5, PAGE_EXECUTE_READWRITE, &dummy);
*(uint *)(offset + 1) = replaced_functions[--replace_counter];
*(unsigned char *)offset = replaced_functions[--replace_counter];
}
}
void replace_call(uint offset, void *func)
{
uint dummy;
VirtualProtect((void *)offset, 5, PAGE_EXECUTE_READWRITE, &dummy);
*(uint *)(offset + 1) = ((uint)func - offset) - 5;
}
uint get_relative_call(uint base, uint offset)
{
return base + *((uint *)(base + offset + 1)) + offset + 5;
}
uint get_absolute_value(uint base, uint offset)
{
return *((uint *)(base + offset));
}
void patch_code_byte(uint offset, unsigned char r)
{
uint dummy;
VirtualProtect((void *)offset, sizeof(r), PAGE_EXECUTE_READWRITE, &dummy);
*(unsigned char *)offset = r;
}
void patch_code_word(uint offset, word r)
{
uint dummy;
VirtualProtect((void *)offset, sizeof(r), PAGE_EXECUTE_READWRITE, &dummy);
*(word *)offset = r;
}
void patch_code_uint(uint offset, uint r)
{
uint dummy;
VirtualProtect((void *)offset, sizeof(r), PAGE_EXECUTE_READWRITE, &dummy);
*(uint *)offset = r;
}
void patch_code_float(uint offset, float r)
{
uint dummy;
VirtualProtect((void *)offset, sizeof(r), PAGE_EXECUTE_READWRITE, &dummy);
*(float *)offset = r;
}
void patch_code_double(uint offset, double r)
{
uint dummy;
VirtualProtect((void *)offset, sizeof(r), PAGE_EXECUTE_READWRITE, &dummy);
*(double *)offset = r;
}
void memcpy_code(uint offset, void *data, uint size)
{
uint dummy;
VirtualProtect((void *)offset, size, PAGE_EXECUTE_READWRITE, &dummy);
memcpy((void *)offset, data, size);
}
void memset_code(uint offset, uint val, uint size)
{
uint dummy;
VirtualProtect((void *)offset, size, PAGE_EXECUTE_READWRITE, &dummy);
memset((void *)offset, val, size);
}