{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpFuncs.cpp
More file actions
352 lines (303 loc) · 8.02 KB
/
Copy pathHelpFuncs.cpp
File metadata and controls
352 lines (303 loc) · 8.02 KB
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
#define RET_NO_ERRORS ErrorReturn_t (RET_SUCCESS)
#define CATCH_CONS(data, message, code) \
catch (ExceptionHandler& e) \
CONS_EXCEPTION (expn_, message, code, e)
#define MIN(a, b) (((a) > (b)) ? (b) : (a))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
class StackData_t;
bool isNum (std::string* str);
bool IsString (std::string* str);
bool IsArgValue (const int8_t& flag);
int8_t _GetString (FILE* f, std::string* str, int8_t del);
std::string GetAsmNumString (int val, const char* operand = "dword");
void PushStackValueJit (stack<StackData_t>* st, int dump_start, JitCompiler_t* comp, exception_data* expn);
bool isNum (std::string* str)
{
bool first = true;
STL_LOOP (i, *str)
{
if (first && (*i != '-' && *i != '+') && !isdigit (*i))
return false;
else
{
first = false;
continue;
}
if (!first && !isdigit (*i))
return false;
}
return true;
}
bool IsString (std::string* str)
{
return (*(str->begin ()) == '"' && *(str->rbegin ()) == '"');
}
struct VarDescriptor_t
{
int64_t num;
int16_t die;
int64_t typeCode;
bool pointer;
};
struct VarData_t
{
void* var;
int64_t code;
size_t size;
VarData_t (void* var_, int64_t code_, size_t size_) :
var (var_),
code (code_),
size (size_)
{}
VarData_t (const VarData_t& that) :
var (that.var),
code (that.code),
size (that.size)
{}
VarData_t& operator = (const VarData_t& that)
{
var = that.var;
code = that.code;
size = that.size;
return *this;
}
VarData_t () :
var (nullptr),
code (0),
size (0)
{}
void Free ()
{
if (size > 0) var = new int8_t [size];
}
void Delete ()
{
if (var) delete [] reinterpret_cast<int8_t*> (var);
var = nullptr;
}
~VarData_t ()
{
Delete ();
}
};
bool IsArgValue (const int8_t& flag)
{
if (flag != ARG_NUM &&
flag != ARG_VAR &&
flag != ARG_REG &&
flag != ARG_STR)
return false;
return true;
}
struct ErrorReturn_t
{
int retVal;
std::string errorText;
ErrorReturn_t () :
retVal (),
errorText ()
{}
ErrorReturn_t (int val) :
retVal (val),
errorText ()
{}
ErrorReturn_t (int val, const char* text) :
retVal (val),
errorText (text)
{}
ErrorReturn_t (int val, std::string text) :
retVal (val),
errorText (text)
{}
};
int8_t _GetString (FILE* f, std::string* str, int8_t del)
{
int8_t c = ' ';
bool str_ = false;
bool str_found = false;
while (c == ' ') c = static_cast<int8_t> (fgetc (f));
int iteration = 0;
while (c != '\n' &&
c != EOF &&
(str_ || c != ' ') &&
c != del && (!str_found || str))
{
if (c == '"' && iteration == 0) {str_ = true; str_found = true;}
else if (c == '"') str_ = false;
if (str_ && c == '\\')
{
switch (c = static_cast<int8_t> (fgetc (f)))
{
case 'n':
*str += '\n';
break;
case 'r':
*str += '\r';
break;
case '\\':
*str += '\\';
break;
case '"':
*str += '"';
str_ = true;
break;
default:
*str += '\\';
*str += c;
break;
}
}
else
{
*str += c;
}
c = static_cast<int8_t> (fgetc (f));
iteration++;
}
return c;
}
struct ExpectedArg_t
{
std::vector<int8_t> expFlag1;
std::vector<int8_t> expFlag2;
ExpectedArg_t (int n_flag1, int n_flag2, ...) :
expFlag1 (),
expFlag2 ()
{
va_list arg; va_start (arg, n_flag2);
for (int i = 0; i < n_flag1; i++) expFlag1.push_back (int8_t(va_arg (arg, int)));
for (int i = 0; i < n_flag2; i++) expFlag2.push_back (int8_t(va_arg (arg, int)));
va_end (arg);
if (n_flag1 == 0) expFlag1.push_back (ARG_NULL);
if (n_flag2 == 0) expFlag2.push_back (ARG_NULL);
}
~ExpectedArg_t ()
{ }
};
template<class T> typename std::vector<T>::const_iterator find (const std::vector<T>& vec, const T& data)
{
STL_LOOP (i, vec) if (*i == data) return i;
return vec.end ();
}
struct StackData_t
{
int64_t data;
size_t size;
template <typename T>
StackData_t (T data_ = 0, size_t size_ = 0) :
data (static_cast <int64_t> (data_)),
size (size_)
{}
bool operator! ()
{
return (data == 0);
}
template <typename T>
operator T ()
{
return reinterpret_cast <T> (data);
}
friend std::ostream& operator<< (std::ostream& output, const StackData_t& s)
{
output << s.data;
return output;
}
};
std::string GetAsmNumString (int val, const char* operand /* = "dword" */)
{
char val_[MAX_PATH] = "";
itoa (val, val_, 16);
std::string val_str;
val_str += std::string (operand) + " (0" + val_ + "h)";
return val_str;
}
void PushStackValueJit (stack<StackData_t>* st, int dump_start, JitCompiler_t* comp, exception_data* expn)
{
int size = st->size() - dump_start;
if (size <= 0)
return;
if (size == 1)
{
comp->ParameterPush ((*st)[dump_start + size - 1].data);
}
else
if ((st->size() - dump_start) == 2)
{
comp->ParameterPush ((*st)[dump_start + size - 1].data,
(*st)[dump_start + size - 2].data);
}
else
if ((st->size() - dump_start) == 3)
{
comp->ParameterPush ((*st)[dump_start + size - 1].data,
(*st)[dump_start + size - 2].data,
(*st)[dump_start + size - 3].data);
}
else
if ((st->size() - dump_start) == 4)
{
comp->ParameterPush ((*st)[dump_start + size - 1].data,
(*st)[dump_start + size - 2].data,
(*st)[dump_start + size - 3].data,
(*st)[dump_start + size - 4].data);
}
else
{
comp->ParameterPush ((*st)[dump_start + size - 1].data,
(*st)[dump_start + size - 2].data,
(*st)[dump_start + size - 3].data,
(*st)[dump_start + size - 4].data);
for (stack<StackData_t>::iterator i (st, & (*st)[dump_start + size - 4]);
i > stack<StackData_t>::iterator (st, & (*st)[dump_start]);
i--)
{
switch (i->size)
{
case sizeof (int8_t):
comp->push<int8_t> (static_cast<int8_t> (i->data));
break;
case sizeof (int16_t):
comp->push<int16_t> (static_cast<int16_t> (i->data));
break;
case sizeof (int32_t):
comp->push<int32_t> (static_cast<int32_t> (i->data));
break;
case sizeof (int64_t):
comp->push<int64_t> (static_cast<int64_t> (i->data));
break;
default:
NAT_EXCEPTION (expn, "Failed to emit \"push\", invalid operand size", ERROR_INVALID_OP_SIZE_SWITCH)
}
}
}
}
struct MemberVarDescriptor_t
{
int64_t num;
int64_t offset;
int64_t typeCode;
};
struct FuncStructPair_t
{
int64_t func_;
int64_t struct_;
};
struct CallInfo_t
{
int retLine;
int var;
CallInfo_t (int retLine_ = 0, int var_ = 0) :
retLine (retLine_),
var (var_)
{}
friend std::ostream& operator<< (std::ostream& output, const CallInfo_t& s)
{
output << s.retLine;
return output;
}
};
struct JmpPatchData_t
{
int32_t first, second;
bool offset;
};
You can’t perform that action at this time.
