forked from gynt/RuntimePatchingSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryFunctions.cpp
306 lines (237 loc) · 6.92 KB
/
MemoryFunctions.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "MemoryFunctions.h"
int luaReadByte(lua_State* L) {
if (lua_gettop(L) != 1) {
return luaL_error(L, "expected exactly 1 argument");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
lua_pushinteger(L, *((BYTE*)address));
return 1;
}
int luaReadSmallInteger(lua_State* L) {
if (lua_gettop(L) != 1) {
return luaL_error(L, "expected exactly 1 argument");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
lua_pushinteger(L, *((SHORT*)address));
return 1;
}
int luaReadInteger(lua_State* L) {
if (lua_gettop(L) != 1) {
return luaL_error(L, "expected exactly 1 argument");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
lua_pushinteger(L, *((int*)address));
return 1;
}
int luaReadString(lua_State* L) {
DWORD address = 0;
int maxLength = 0;
int length = 0;
bool wide = false;
if (lua_gettop(L) == 0) {
return luaL_error(L, "too few arguments passed to readString");
}
address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
if (lua_gettop(L) == 2) {
maxLength = lua_tointeger(L, 2);
}
if (lua_gettop(L) == 3) {
wide = lua_tointeger(L, 3) == 1;
}
if (wide || maxLength) {
return luaL_error(L, "sorry, maxlength and wide are not supported yet.");
}
std::string result((char*)address);
lua_pushstring(L, result.c_str());
return 1;
}
int luaReadBytes(lua_State* L) {
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected exactly 2 arguments");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
int size = lua_tointeger(L, 2);
lua_createtable(L, size, 0);
for (int i = 0; i < size; i++) {
unsigned char value = *((BYTE*)(address + i));
lua_pushinteger(L, (lua_Integer)i + 1);
lua_pushinteger(L, value);
lua_settable(L, -3); /* 3rd element from the stack top */
}
// we pass the table back;
return 1;
}
int luaWriteString(lua_State* L) {
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected exactly 2 arguments");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
std::string value = lua_tostring(L, 2);
#ifdef _DEBUG
if (!canWrite(address, value.size())) {
return luaL_error(L, ("cannot write " + std::to_string(1) + " string to location: " + std::to_string(address)).c_str());
}
#endif
memcpy((void*)address, value.c_str(), value.size());
return 0;
}
int luaWriteByte(lua_State* L) {
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected exactly 2 arguments");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
BYTE value = lua_tointeger(L, 2);
#ifdef _DEBUG
if (!canWrite(address, 1)) {
return luaL_error(L, ("cannot write " + std::to_string(1) + " bytes to location: " + std::to_string(address)).c_str());
}
#endif
* ((BYTE*)address) = value;
return 0;
}
int luaWriteSmallInteger(lua_State* L) {
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected exactly 2 arguments");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
SHORT value = lua_tointeger(L, 2);
#ifdef _DEBUG
if (!canWrite(address, 2)) {
return luaL_error(L, ("cannot write " + std::to_string(2) + " bytes to location: " + std::to_string(address)).c_str());
}
#endif
* ((SHORT*)address) = value;
return 0;
}
int luaWriteInteger(lua_State* L) {
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected exactly 2 arguments");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
int value = lua_tointeger(L, 2);
#ifdef _DEBUG
if (!canWrite(address, 4)) {
return luaL_error(L, ("cannot write " + std::to_string(4) + " bytes to location: " + std::to_string(address)).c_str());
}
#endif
* ((int*)address) = value;
return 0;
}
int luaWriteBytes(lua_State* L) {
if (lua_gettop(L) != 2) {
return luaL_error(L, "expected exactly 2 arguments");
}
DWORD address = lua_tointeger(L, 1);
if (address == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
if (!lua_istable(L, 2)) {
return luaL_error(L, "the second argument should be a table");
}
#ifdef _DEBUG
int length = lua_rawlen(L, 2);
if (!canWrite(address, length)) {
return luaL_error(L, ("cannot write " + std::to_string(length) + " bytes to location: " + std::to_string(address)).c_str());
}
#endif
int i = 0;
lua_pushvalue(L, 2); // push the table again; so that it is at -1
/* table is in the stack at index 't' */
lua_pushnil(L); /* first key */
while (lua_next(L, -2) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
if (!lua_isinteger(L, -1)) {
//lua_pushliteral(L, "The return value table must have integer values");
//lua_error(L);
return luaL_error(L, "The return value table must have integer values");
}
int value = lua_tointeger(L, -1);
if (value < 0) {
return luaL_error(L, "The values must all be positive");
}
*((BYTE*)(address + i)) = value;
i += 1;
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
//lua_next pops the key from the stack, if we reached the end, there is no key on the stack.
lua_pop(L, 1); //removes 'table'
return 0;
}
int luaMemCpy(lua_State* L) {
if (lua_gettop(L) != 3) {
return luaL_error(L, "expected exactly 3 arguments");
}
DWORD dst = lua_tointeger(L, 1);
if (dst == 0) {
return luaL_error(L, "argument 1 must be a valid address");
}
DWORD src = lua_tointeger(L, 2);
if (src == 0) {
return luaL_error(L, "argument 2 must be a valid address");
}
int size = lua_tointeger(L, 3);
if (size == 0) {
return luaL_error(L, "argument 3 must be a valid size higher than 0");
}
#ifdef _DEBUG
if (!canWrite(dst, size)) {
return luaL_error(L, ("cannot write " + std::to_string(size) + " bytes to location: " + std::to_string(dst)).c_str());
}
#endif
memcpy((void*)dst, (void*)src, size);
return 0;
}
std::set<std::string> stringSet;
int registerString(lua_State* L) {
if (lua_gettop(L) != 1) {
return luaL_error(L, "Wrong number of arguments passed");
}
std::string target = lua_tostring(L, 1);
std::pair<std::set<std::string>::iterator, bool> p = stringSet.insert(target);
std::set<std::string>::iterator it = p.first;
lua_pushinteger(L, (DWORD)p.first->c_str());
return 1;
}
int luaAllocate(lua_State* L) {
if (lua_gettop(L) != 1 && lua_gettop(L) != 2) {
return luaL_error(L, "Expected one or two arguments");
}
void* memory;
int size = lua_tonumber(L, 1);
if (lua_gettop(L) == 2 && lua_toboolean(L, 2)) {
memory = calloc(size, sizeof(BYTE));
}
else {
memory = malloc(size);
}
lua_pushinteger(L, (DWORD_PTR)memory);
return 1;
}