-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalUtil.h
executable file
·264 lines (223 loc) · 5.48 KB
/
localUtil.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
#ifndef LOCALUTIL
#define LOCALUTIL
#include <stdint.h>
#include <stdio.h>
#define DEFAULT_SEARCH_SIZE 0x80
#define PAGE_SIZE4K 0x1000
#define PAGE_MASK4K (PAGE_SIZE4K - 1)
// #ifdef __linux__
#ifndef PAGE_SIZE
#define PAGE_SIZE PAGE_SIZE4K
#endif
#ifndef PAGE_MASK
#define PAGE_MASK PAGE_MASK4K
#endif
// #endif
#define CASE_OVERLAP_R1_IN_R2(reg1, reg1sz, reg2, reg2sz) \
((reg1 >= reg2) && (reg1 < (reg2 + reg2sz)))
#define CASE_OVERLAP_R1_ENDS_R2(reg1, reg1sz, reg2, reg2sz) \
(((reg1 + reg1sz) > reg2) && ((reg1 + reg1sz) < (reg2 + reg2sz)))
// this case is if reg1 is less than reg2, and then gets to or passed it.
#define CASE_OVERLAP_R1_EATS_R2(reg1, reg1sz, reg2, reg2sz) \
((reg1 < reg2) && ((reg1 + reg1sz) > (reg2 + reg2sz)))
// first case, reg1 is inside of reg2
// second case, reg1 ends in reg2
// final case, reg1 encompasses reg2
#define OVERLAP_REGIONS(reg1, reg1sz, reg2, reg2sz) \
CASE_OVERLAP_R1_IN_R2(reg1, reg1sz, reg2, reg2sz) || \
CASE_OVERLAP_R1_ENDS_R2(reg1, reg1sz, reg2, reg2sz) || \
CASE_OVERLAP_R1_EATS_R2(reg1, reg1sz, reg2, reg2sz)
#define REGION_CONTAINS(reg, regSz, targ_addr) \
CASE_OVERLAP_R1_IN_R2(targ_addr, 0, reg, regSz)
#define UNUSED(x) (void)(x)
#define FINISH_IF(x) \
if (x) \
{ \
goto finish; \
}
#define SAFE_BAIL(x) \
if (x) \
{ \
goto fail; \
}
#define SAFE_CLOSESOCKET(x) \
if (x) \
{ \
closesocket(x); \
x = 0; \
}
#define SAFE_EXIT(x) \
if (x) \
{ \
exit(0); \
}
#define SAFE_FSCLOSE(x) \
if (x.is_open() == true) \
{ \
x.close(); \
}
#define SAFE_PEXIT(x, ...) \
if (x) \
{ \
printf(__VA_ARGS__); \
exit(0); \
}
#define SAFE_CONT(x) \
if (x) \
{ \
continue; \
}
#define SAFE_BREAK(x) \
if (x) \
{ \
break; \
}
#define SAFE_PBREAK(x, ...) \
if (x) \
{ \
printf(__VA_ARGS__); \
break; \
}
#define SAFE_PAIL(x, ...) \
if (x) \
{ \
printf(__VA_ARGS__); \
goto fail; \
}
#define SAFE_FAIL(x, ...) \
if (x) \
{ \
fprintf(stderr, "ERROR %s:%d ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
goto fail; \
}
#define SAFE_ERR(x) \
if (x) \
{ \
fprintf(stderr, "ERROR %s:%d \n", __FILE__, __LINE__); \
goto fail; \
}
#define SAFE_CLOSE(x) \
if (x > -1) \
{ \
close(x); \
x = -1; \
}
#define SAFE_VFREE(x, y) \
if (x != 0) \
{ \
VirtualFree(x, y, MEM_RELEASE); \
x = 0; \
}
#define SAFE_FCLOSE(x) \
if (x) \
{ \
fclose(x); \
x = 0; \
}
#define SAFE_HCLOSE(x) \
if ((x) != INVALID_HANDLE_VALUE) \
{ \
CloseHandle(x); \
x = INVALID_HANDLE_VALUE; \
}
#define SAFE_FREELIB(x) \
if ((x) != NULL) \
{ \
FreeLibrary(x); \
x = 0; \
}
#define SAFE_LFREE(x) \
if (x) \
{ \
LocalFree(x); \
x = 0; \
}
#define SAFE_FREE(x) \
if (x) \
{ \
free((void*)x); \
x = 0; \
}
#define SAFE_DEL(x) \
if (x) \
{ \
delete x; \
x = 0; \
}
#define SAFE_DELA(x) \
if (x) \
{ \
delete[] x; \
x = 0; \
}
#define SAFE_CLOSEDIR(x) \
if (x) \
{ \
closedir(x); \
x = 0; \
}
#define SAFE_REPOINT(x, y) \
if (x != 0) \
{ \
*x = y; \
}
#define BIT_PAD(x, TYPE_AUTO, PAD_TO) \
if (((size_t)x % PAD_TO) != 0) \
{ \
x = (TYPE_AUTO)((size_t)x + PAD_TO - ((size_t)x % PAD_TO)); \
}
#ifdef _WIN32
#define OSHANDLE HANDLE
#define OPENREAD(fname) CreateFileA(fname, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
// file to open
// open for reading
// share for reading
// default security
// existing file only
// overlapped operation
// no attr. template
#define OSSEEK(handle, SEEK_DIR) SetFilePointer(handle, 0, 0, SEEK_DIR)
#define OSFSZ(handle) GetFileSize(handle, NULL)
#define OSBA(newAlloc, SZ_ALLOC) \
newAlloc = VirtualAlloc(NULL, SZ_ALLOC, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)
#define SAFE_OSFCLOSE SAFE_HCLOSE
#define OSREAD(oshandle, INBUF, NBYTES, OUTCOUNT) ReadFile(oshandle, INBUF, NBYTES, &OUTCOUNT, NULL);
#else
#define OSHANDLE FILE*
#define OPENREAD(fname) fopen(fname, "r")
#define OSSEEK(handle, SEEK_DIR) fseek(handle, 0, SEEK_DIR)
#define OSFSZ(handle) ftell(handle)
#ifdef __METALKIT__
#define OSBA(newAlloc, SZ_ALLOC) \
newAlloc = memalign(PAGE_SIZE4K, SZ_ALLOC)
#else
#define OSBA(newAlloc, SZ_ALLOC) \
{ \
int TEMP_RESULT_PMA = 0; \
TEMP_RESULT_PMA = posix_memalign(&newAlloc, PAGE_SIZE4K, SZ_ALLOC); \
}
#endif // __METALKIT__
#define SAFE_OSFCLOSE SAFE_FCLOSE
#define OSREAD(oshandle, INBUF, NBYTES, OUTCOUNT) \
{ \
size_t TEMP_RESULT_FREAD = 0; \
TEMP_RESULT_FREAD = fread(INBUF, 1, NBYTES, oshandle); \
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
size_t rstrnlenu(const char* s, size_t maxlen);
size_t strnlenu(const char* s, size_t maxlen);
size_t rfindnn(const char* s, size_t maxlen);
int rstrncmp(const char* s1, const char* s2, size_t maxlen);
int block_grab(const char* fileTargName, void** allocBase, size_t* fSize);
unsigned long subint(const char* strbase, size_t strsize, int radix);
int recurse_op(int (*routine_on_file)(const char*, int, void**),
const char* path_dir, int count, void** vargs);
void hexdump(char* buf_in, size_t buf_sz);
#ifdef __cplusplus
}
#endif
#endif