-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeclPatch.h
279 lines (229 loc) · 6.58 KB
/
declPatch.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#if !defined(HEADER) || !defined(NAME)
#error "Must define HEADER and NAME!"
#endif
#include "util.h"
#define PATCH_BEGIN(file,csum) __PATCH_BEGIN(NAME,file,csum)
#define __PATCH_BEGIN(name,file,csum) _PATCH_BEGIN(name,file,csum)
#define PATCH_END() __PATCH_END(NAME)
#define __PATCH_END(name) _PATCH_END(name)
#define PATCH(offset,...) __PATCH(NAME,offset,__VA_ARGS__)
#define __PATCH(name,offset,...) _PATCH(name,offset,__VA_ARGS__)
#define PATCH_REL(offset,addr) __PATCH_REL(NAME,offset,addr)
#define __PATCH_REL(name,offset,addr) _PATCH_REL(name,offset,addr)
#define PATCH_VAR(offset,var) __PATCH_VAR(NAME,offset,var)
#define __PATCH_VAR(name,offset,var) _PATCH_VAR(name,offset,var)
#define PATCH_ORIG(offset,...) __PATCH_ORIG(NAME,offset,__VA_ARGS__)
#define __PATCH_ORIG(name,offset,...) _PATCH_ORIG(name,offset,__VA_ARGS__)
/*** PATCHES OBJECT ***/
#define _PATCH_BEGIN(name,file,csum) \
static struct { \
HMODULE lib; \
unsigned char *mem; \
} name = { NULL, NULL };
#define _PATCH_END(name)
#define _PATCH(name,offset,...)
#define _PATCH_REL(name,offset,addr)
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
/*** PATCH FUNCTIONS ***/
/*check csum/data*/
#define _PATCH_BEGIN(name,file,csum) \
int patch_##name##_check(void) \
{ \
const unsigned int want_checksum = (csum); \
unsigned int checksum;
#define _PATCH_END(name)
#define _PATCH(name,offset,...)
#define _PATCH_REL(name,offset,addr)
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...) \
const unsigned char name##_##offset##_orig[] = { __VA_ARGS__ };
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
#define _PATCH_BEGIN(name,file,csum) \
checksum = util_csum_library(name.lib); \
if (checksum != want_checksum) { \
LOG("CSUM mismatch for " #name "; expected %08x, got %08x", want_checksum, checksum); \
return -2; \
}
#define _PATCH_END(name) \
return 0; \
}
#define _PATCH(name,offset,...)
#define _PATCH_REL(name,offset,addr)
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...) \
if (memcmp(LIBABS(name.lib, offset), name##_##offset##_orig, sizeof(name##_##offset##_orig))) { \
LOG("Expected " #__VA_ARGS__ " at " #name "+%08x", offset); \
return -3; \
}
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
/*variables*/
#define _PATCH_BEGIN(name,file,csum) \
void patch_##name##_var(void) \
{
#define _PATCH_END(name) \
}
#define _PATCH(name,offset,...)
#define _PATCH_REL(name,offset,addr)
#define _PATCH_VAR(name,offset,var) \
(var) = LIBABS(name.lib, offset);
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
/*data*/
#define _PATCH_BEGIN(name,file,csum) \
void patch_##name##_data(void) \
{
#define _PATCH_END(name)
#define _PATCH(name,offset,...) \
unsigned char name##_##offset##_data[] = { __VA_ARGS__ };
#define _PATCH_REL(name,offset,addr) \
void *name##_##offset##_data = addr;
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
#define _PATCH_BEGIN(name,file,csum) \
size_t len = sizeof(void*)
#define _PATCH_END(name) \
; \
unsigned char *mem = malloc(len);
#define _PATCH(name,offset,...) \
+ sizeof(name##_##offset##_data) + sizeof(void*) + sizeof(unsigned int)
#define _PATCH_REL(name,offset,addr) \
_PATCH(name,offset,addr)
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
#define _PATCH_BEGIN(name,file,csum) \
*(void**)mem = NULL; mem += sizeof(void*);
#define _PATCH_END(name) \
name.mem = mem; \
}
#define _PATCH(name,offset,...) \
memcpy(mem, LIBABS(name.lib, offset), sizeof(name##_##offset##_data)); mem += sizeof(name##_##offset##_data); \
*(unsigned int*)mem = sizeof(name##_##offset##_data); mem += sizeof(unsigned int); \
*(void**)mem = LIBABS(name.lib, offset); mem += sizeof(void*); \
patch_mem(LIBABS(name.lib, offset), name##_##offset##_data, sizeof(name##_##offset##_data));
#define _PATCH_REL(name,offset,addr) \
memcpy(mem, LIBABS(name.lib, offset), sizeof(name##_##offset##_data)); mem += sizeof(name##_##offset##_data); \
*(unsigned int*)mem = sizeof(name##_##offset##_data); mem += sizeof(unsigned int); \
*(void**)mem = LIBABS(name.lib, offset); mem += sizeof(void*); \
patch_rel(LIBABS(name.lib, offset), name##_##offset##_data);
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
/*cleanup func*/
#define _PATCH_BEGIN(name,file,csum) \
void unpatch_##name(void) \
{ \
unsigned char *mem, *addr; \
if (!name.lib) return; \
mem = name.mem; \
/* Unpatch in reverse */ \
while ((addr = (*(void**)(mem -= sizeof(void*))))) { \
unsigned int len = *(unsigned int*)(mem -= sizeof(unsigned int)); \
mem -= len; \
patch_mem(addr, mem, len); \
}
#define _PATCH_END(name) \
free(mem); \
FreeLibrary(name.lib); \
name.lib = NULL; \
}
#define _PATCH(name,offset,...)
#define _PATCH_REL(name,offset,addr)
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
/*patchp func*/
#define _PATCH_BEGIN(name,file,csum) \
int patch_##name(void) \
{ \
int ret; \
if (name.lib) return 0; \
name.lib = LoadLibrary(file); \
if (!name.lib) return -1; \
if ((ret = patch_##name##_check())) { \
FreeLibrary(name.lib); \
name.lib = NULL; \
return ret; \
} \
patch_##name##_var(); \
patch_##name##_data();
#define _PATCH_END(name) \
return 0; \
}
#define _PATCH(name,offset,...)
#define _PATCH_REL(name,offset,addr)
#define _PATCH_VAR(name,offset,var)
#define _PATCH_ORIG(name,offset,...)
#include HEADER
#undef _PATCH_BEGIN
#undef _PATCH_END
#undef _PATCH
#undef _PATCH_REL
#undef _PATCH_VAR
#undef _PATCH_ORIG
/*** fin ***/
#undef PATCH_BEGIN
#undef PATCH_END
#undef PATCH
#undef PATCH_REL
#undef PATCH_VAR
#undef PATCH_ORIG
#undef __PATCH_BEGIN
#undef __PATCH_END
#undef __PATCH
#undef __PATCH_REL
#undef __PATCH_VAR
#undef __PATCH_ORIG
#undef HEADER
#undef NAME