-
Notifications
You must be signed in to change notification settings - Fork 2
/
PointerHandler.cpp
364 lines (319 loc) · 9.58 KB
/
PointerHandler.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "stdafx.h"
#include <fstream>
#include <string>
#include "PointerHandler.h"
#include "Pointer.h"
#include "AtlasLogger.h"
#include "GenericVariable.h"
#include "AtlasCore.h"
#include "AtlasTypes.h"
using namespace std;
PointerHandler::PointerHandler(VariableMap* Map)
{
this->Map = Map;
}
bool PointerHandler::CreatePointer(std::string& PtrId, std::string& AddressType,
__int64 Offsetting, unsigned int Size, unsigned int HeaderSize)
{
CustomPointer* Ptr = (CustomPointer*)Map->GetVar(PtrId)->GetData();
if(Ptr != NULL) // Already initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has already been allocated", PtrId.c_str());
return false;
}
Ptr = new CustomPointer;
if(!Ptr->Init(Offsetting, Size, HeaderSize))
{
Logger.ReportError(CurrentLine, "Invalid size parameter for CREATEPTR");
return false;
}
if(!Ptr->SetAddressType(AddressType))
{
Logger.ReportError(CurrentLine, "Invalid address type for CREATEPTR");
return false;
}
Map->SetVarData(PtrId, Ptr, P_CUSTOMPOINTER);
return true;
}
unsigned int PointerHandler::GetPtrSize(string& PtrId)
{
CustomPointer* Ptr = (CustomPointer*)Map->GetVar(PtrId)->GetData();
if(Ptr == NULL) // Uninitialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with CREATEPTR", PtrId.c_str());
return -1;
}
return Ptr->GetSize();
}
unsigned int PointerHandler::GetPtrAddress(std::string& PtrId, unsigned int ScriptPos,
unsigned int& Size)
{
CustomPointer* Ptr = (CustomPointer*)Map->GetVar(PtrId)->GetData();
if(Ptr == NULL) // Uninitialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with CREATEPTR", PtrId.c_str());
return -1;
}
unsigned int Address = Ptr->GetAddress(ScriptPos);
Size = Ptr->GetSize();
return Address;
}
bool PointerHandler::CreatePointerList(std::string& ListId, const char* Filename,
std::string& PtrId)
{
PointerList* List = (PointerList*)Map->GetVar(ListId)->GetData();
if(List != NULL) // Already initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has already been allocated", ListId.c_str());
return false;
}
CustomPointer* Ptr = (CustomPointer*)Map->GetVar(PtrId)->GetData();
if(Ptr == NULL)
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with CREATEPTR", PtrId.c_str());
return false;
}
List = new PointerList;
bool Success = List->Create(Filename, *Ptr);
Map->SetVarData(ListId, List, P_POINTERLIST);
return Success;
}
unsigned int PointerHandler::GetListAddress(std::string& ListId, unsigned int ScriptPos, unsigned int& Size, unsigned int& WritePos)
{
PointerList* List = (PointerList*)Map->GetVar(ListId)->GetData();
if(List == NULL) // Not initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with PTRLIST", ListId.c_str());
return -1;
}
return List->GetAddress(ScriptPos, Size, WritePos);
}
bool PointerHandler::CreatePointerTable(std::string& TblId, unsigned int Start,
unsigned int Increment, std::string& PtrId)
{
PointerTable* Tbl = (PointerTable*)Map->GetVar(TblId)->GetData();
if(Tbl != NULL) // Already allocated
{
Logger.ReportError(CurrentLine, "Identifier %s has already been allocated", TblId.c_str());
return false;
}
CustomPointer* Ptr = (CustomPointer*)Map->GetVar(PtrId)->GetData();
if(Ptr == NULL)
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with CREATEPTR", PtrId.c_str());
return false;
}
Tbl = new PointerTable;
Tbl->Create(Increment, Start, *Ptr);
Map->SetVarData(TblId, Tbl, P_POINTERTABLE);
return true;
}
bool PointerHandler::CreateEmbPointerTable(string& TblId, unsigned int Start, unsigned int PtrCount, string& PtrId)
{
EmbPointerTable* Tbl = (EmbPointerTable*)Map->GetVar(TblId)->GetData();
if(Tbl != NULL) // Already allocated
{
Logger.ReportError(CurrentLine, "Identifier %s has already been allocated", TblId.c_str());
return false;
}
CustomPointer* Ptr = (CustomPointer*)Map->GetVar(PtrId)->GetData();
if(Ptr == NULL)
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with CREATEPTR", PtrId.c_str());
return false;
}
Tbl = new EmbPointerTable;
Tbl->Create(Start, PtrCount, *Ptr);
Map->SetVarData(TblId, Tbl, P_EMBPOINTERTABLE);
return true;
}
unsigned int PointerHandler::GetEmbTableAddress(string& TblId, unsigned int ScriptPos,
unsigned int& Size, unsigned int& WritePos)
{
EmbPointerTable* Tbl = (EmbPointerTable*)Map->GetVar(TblId)->GetData();
if(Tbl == NULL) // Not initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with EMBPTRTBL", TblId.c_str());
return -1;
}
return Tbl->GetAddress(ScriptPos, Size, WritePos);
}
unsigned int PointerHandler::GetEmbTableAddress(string& TblId, unsigned int ScriptPos, unsigned int PtrNum,
unsigned int& Size, unsigned int& WritePos)
{
EmbPointerTable* Tbl = (EmbPointerTable*)Map->GetVar(TblId)->GetData();
if(Tbl == NULL) // Not initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with EMBPTRTBL", TblId.c_str());
return -1;
}
return Tbl->GetAddress(ScriptPos, PtrNum, Size, WritePos);
}
unsigned int PointerHandler::GetTableAddress(std::string& TblId, unsigned int ScriptPos,
unsigned int& Size, unsigned int& WritePos)
{
PointerTable* Tbl = (PointerTable*)Map->GetVar(TblId)->GetData();
if(Tbl == NULL) // Not initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with PTRTBL", TblId.c_str());
return -1;
}
return Tbl->GetAddress(ScriptPos, Size, WritePos);
}
unsigned int PointerHandler::GetTableAddress(std::string& TblId, unsigned int ScriptPos, unsigned int PtrNum,
unsigned int& Size, unsigned int& WritePos)
{
PointerTable* Tbl = (PointerTable*)Map->GetVar(TblId)->GetData();
if(Tbl == NULL) // Not initialized
{
Logger.ReportError(CurrentLine, "Identifier %s has not been initialized with PTRTBL", TblId.c_str());
return -1;
}
return Tbl->GetAddress(ScriptPos, PtrNum, Size, WritePos);
}
PointerList::PointerList()
{
Location = 0;
}
PointerList::~PointerList()
{
}
bool PointerList::Create(const char* Filename, CustomPointer& CustPointer)
{
ifstream Input(Filename);
if(!Input.is_open())
{
// File Error
return false;
}
string Line;
size_t FirstPos = 0;
bool bRet = true;
unsigned int Res = 0;
unsigned int CurLine = 1;
while(!Input.eof())
{
getline(Input, Line);
FirstPos = Line.find_first_not_of(" \t", 0);
if(FirstPos == string::npos) // Whitespace line
continue;
if(Line.length() > FirstPos+1)
if(Line[FirstPos] == '/' && Line[FirstPos] == '/') // Comment
continue;
// Trim trailing whitespace
size_t Last;
for(Last = Line.length() - 1; Last > 0; Last--)
if(Line[Last] != ' ' && Line[Last] != '\t')
break;
if(Last < Line.length())
Line.erase(Last+1);
if(Line[FirstPos] == '$')
{
FirstPos++;
if(string::npos == Line.find_first_not_of("0123456789ABCDEF", FirstPos))
Res = strtoul(Line.substr(FirstPos, Line.length() - FirstPos).c_str(), NULL, 16);
else
{
Logger.ReportError(CurLine, "Error parsing %s in %s", Line.c_str(), Filename);
bRet = false;
}
}
else
{
if(string::npos == Line.find_first_not_of("0123456789", FirstPos))
Res = strtoul(Line.substr(FirstPos, Line.length() - FirstPos).c_str(), NULL, 10);
else
{
Logger.ReportError(CurLine, "Error parsing %s in %s", Line.c_str(), Filename);
bRet = false;
}
}
LocationList.push_back(Res);
CurLine++;
}
Input.close();
LocationIt = LocationList.begin();
Location = 0;
Pointer = CustPointer;
return bRet;
}
unsigned int PointerList::GetAddress(unsigned int TextPosition, unsigned int& Size, unsigned int& WritePos)
{
if(Location < LocationList.size())
{
Size = Pointer.GetSize();
WritePos = *LocationIt;
LocationIt++;
Location++;
return Pointer.GetAddress(TextPosition);
}
else
return -1;
}
PointerTable::PointerTable()
{
Increment = 0;
CurOffset = 0;
}
PointerTable::~PointerTable()
{
}
bool PointerTable::Create(unsigned int Inc, unsigned int StartOffset, CustomPointer& CustPointer)
{
Increment = Inc;
CurOffset = StartOffset;
Pointer = CustPointer;
TableStart = StartOffset;
return true;
}
unsigned int PointerTable::GetAddress(unsigned int TextPosition, unsigned int& Size, unsigned int& WritePos)
{
Size = Pointer.GetSize();
WritePos = CurOffset;
CurOffset += Increment;
return Pointer.GetAddress(TextPosition);
}
unsigned int PointerTable::GetAddress(unsigned int TextPosition, unsigned int PtrNum,
unsigned int& Size, unsigned int& WritePos)
{
Size = Pointer.GetSize();
WritePos = TableStart + (PtrNum * Increment);
CurOffset = WritePos + Increment;
return Pointer.GetAddress(TextPosition);
}
EmbPointerTable::EmbPointerTable()
{
TableStart = 0;
CurPointer = 0;
PtrCount = 0;
}
EmbPointerTable::~EmbPointerTable()
{
}
bool EmbPointerTable::Create(unsigned int StartOffset, unsigned int PointerCount, CustomPointer& CustPointer)
{
TableStart = StartOffset;
CurPointer = 0;
PtrCount = PointerCount;
Pointer = CustPointer;
return true;
}
unsigned int EmbPointerTable::GetAddress(unsigned int TextPosition, unsigned int &Size, unsigned int &WritePos)
{
if(CurPointer >= PtrCount)
return -1; // Out of bounds
Size = Pointer.GetSize();
WritePos = TableStart + CurPointer*(Size/8);
CurPointer++;
return Pointer.GetAddress(TextPosition);
}
unsigned int EmbPointerTable::GetAddress(unsigned int TextPosition, unsigned int PtrNum,
unsigned int &Size, unsigned int &WritePos)
{
if(PtrNum >= PtrCount)
return -1; // Out of bounds
Size = Pointer.GetSize();
WritePos = TableStart + PtrNum * (Size/8);
CurPointer = PtrNum+1;
return Pointer.GetAddress(TextPosition);
}