forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
340 lines (314 loc) · 9.87 KB
/
util.c
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
// util.c : implementation of various utility functions.
//
// (c) Ulf Frisk, 2018-2019
// Author: Ulf Frisk, [email protected]
//
#include "util.h"
#include <math.h>
/*
* Calculate the number of digits of an integer number.
*/
DWORD Util_GetNumDigits(_In_ DWORD dwNumber)
{
return (DWORD)max(1, floor(log10(dwNumber) + 1));
}
QWORD Util_GetNumericA(_In_ LPSTR sz)
{
if((strlen(sz) > 1) && (sz[0] == '0') && ((sz[1] == 'x') || (sz[1] == 'X'))) {
return strtoull(sz, NULL, 16); // Hex (starts with 0x)
} else {
return strtoull(sz, NULL, 10); // Not Hex -> try Decimal
}
}
QWORD Util_GetNumericW(_In_ LPWSTR wsz)
{
if((wcslen(wsz) > 1) && (wsz[0] == '0') && ((wsz[1] == 'x') || (wsz[1] == 'X'))) {
return wcstoull(wsz, NULL, 16); // Hex (starts with 0x)
} else {
return wcstoull(wsz, NULL, 10); // Not Hex -> try Decimal
}
}
#define Util_2HexChar(x) (((((x) & 0xf) <= 9) ? '0' : ('a' - 10)) + ((x) & 0xf))
_Success_(return)
BOOL Util_FillHexAscii(_In_opt_ PBYTE pb, _In_ DWORD cb, _In_ DWORD cbInitialOffset, _Out_opt_ LPSTR sz, _Inout_ PDWORD pcsz)
{
DWORD i, j, o = 0, iMod, cRows;
// checks
if((cbInitialOffset > cb) || (cbInitialOffset > 0x1000) || (cbInitialOffset & 0xf)) { return FALSE; }
cRows = (cb + 0xf) >> 4;
if(!sz) {
*pcsz = 1 + cRows * 76;
return TRUE;
}
if(!pb || (*pcsz <= cRows * 76)) { return FALSE; }
// fill buffer with bytes
for(i = cbInitialOffset; i < cb + ((cb % 16) ? (16 - cb % 16) : 0); i++)
{
// address
if(0 == i % 16) {
iMod = i % 0x10000;
sz[o++] = Util_2HexChar(iMod >> 12);
sz[o++] = Util_2HexChar(iMod >> 8);
sz[o++] = Util_2HexChar(iMod >> 4);
sz[o++] = Util_2HexChar(iMod);
sz[o++] = ' ';
sz[o++] = ' ';
sz[o++] = ' ';
sz[o++] = ' ';
} else if(0 == i % 8) {
sz[o++] = ' ';
}
// hex
if(i < cb) {
sz[o++] = Util_2HexChar(pb[i] >> 4);
sz[o++] = Util_2HexChar(pb[i]);
sz[o++] = ' ';
} else {
sz[o++] = ' ';
sz[o++] = ' ';
sz[o++] = ' ';
}
// ascii
if(15 == i % 16) {
sz[o++] = ' ';
sz[o++] = ' ';
for(j = i - 15; j <= i; j++) {
if(j >= cb) {
sz[o++] = ' ';
} else {
sz[o++] = UTIL_PRINTASCII[pb[j]];
}
}
sz[o++] = '\n';
}
}
sz[o] = 0;
*pcsz = o;
return TRUE;
}
VOID Util_PrintHexAscii(_In_ PBYTE pb, _In_ DWORD cb, _In_ DWORD cbInitialOffset)
{
DWORD szMax;
LPSTR sz;
if(cb > 0x10000) {
vmmprintf("Large output. Only displaying first 65kB.\n");
cb = 0x10000 - cbInitialOffset;
}
Util_FillHexAscii(pb, cb, cbInitialOffset, NULL, &szMax);
if(!(sz = LocalAlloc(0, szMax))) { return; }
Util_FillHexAscii(pb, cb, cbInitialOffset, sz, &szMax);
vmmprintf("%s", sz);
LocalFree(sz);
}
VOID Util_AsciiFileNameFix(_In_ LPSTR sz, _In_ CHAR chDefault)
{
DWORD i = 0;
while(sz[i]) {
if(UTIL_ASCIIFILENAME_ALLOW[sz[i]] == '0') { sz[i] = chDefault; }
i++;
}
}
DWORD Util_PathFileNameFixA(_Out_writes_(MAX_PATH) LPWSTR wszOut, _In_ LPCSTR sz, _In_opt_ DWORD csz)
{
WCHAR ch;
DWORD i = 0, iMax = (csz ? min(csz, MAX_PATH - 1) : (MAX_PATH - 1));
while((ch = sz[i]) && (i < iMax)) {
wszOut[i] = ((ch < 128) && (UTIL_ASCIIFILENAME_ALLOW[ch] == '0')) ? '_' : ch;
i++;
}
wszOut[i] = 0;
return i;
}
DWORD Util_PathFileNameFixW(_Out_writes_(MAX_PATH) LPWSTR wszOut, _In_ LPCWSTR wsz, _In_opt_ DWORD cwsz)
{
WCHAR ch;
DWORD i = 0, iMax = (cwsz ? min(cwsz, MAX_PATH - 1) : (MAX_PATH - 1));
while((ch = wsz[i]) && (i < iMax)) {
wszOut[i] = ((ch < 128) && (UTIL_ASCIIFILENAME_ALLOW[ch] == '0')) ? '_' : ch;
i++;
}
wszOut[i] = 0;
return i;
}
DWORD Util_PathFileNameFix_Registry(_Out_writes_(MAX_PATH) LPWSTR wszOut, _In_opt_ LPCSTR sz, _In_opt_ LPCWSTR wsz, _In_opt_ DWORD cwsz, _In_opt_ DWORD iSuffix, _In_ BOOL fUpper)
{
WCHAR ch;
DWORD i = 0, iMax = (cwsz ? min(cwsz, MAX_PATH - 1) : (MAX_PATH - 1));
if(sz || wsz) {
while((ch = (sz ? sz[i] : wsz[i])) && (i < iMax)) {
if(fUpper && ch >= 'a' && ch <= 'z') {
ch += 'A' - 'a';
} else if(ch < 128) {
ch = (UTIL_ASCIIFILENAME_ALLOW[ch] == '0') ? '_' : ch;
}
wszOut[i] = ch;
i++;
}
}
if(iSuffix && (iSuffix < 10) && (i < MAX_PATH - 3)) {
wszOut[i++] = '-';
wszOut[i++] = '0' + (WCHAR)iSuffix;
}
wszOut[i] = 0;
return i;
}
QWORD Util_PathGetBaseFromW(_In_ LPWSTR wsz)
{
if((wcslen(wsz) < 15) || (wsz[0] != '0') || (wsz[1] != 'x')) { return (ULONG64)-1; }
return wcstoull(wsz, NULL, 16);
}
LPWSTR Util_PathSplitNextW(_In_ LPWSTR wsz)
{
WCHAR ch;
DWORD i = 0;
while(TRUE) {
ch = wsz[i++];
if(ch == '\0') {
return wsz + i - 1;
}
if(ch == '\\') {
return wsz + i;
}
}
}
LPWSTR Util_PathFileSplitW(_In_ LPWSTR wsz, _Out_writes_(MAX_PATH) LPWSTR wszPath)
{
DWORD i, iBackSlash = -1;
WCHAR ch = -1;
for(i = 0; ch && i < MAX_PATH; i++) {
ch = wsz[i];
wszPath[i] = ch;
if(ch == '\\') {
iBackSlash = i;
}
}
wszPath[MAX_PATH - 1] = 0;
if(iBackSlash == -1) { return NULL; }
wszPath[iBackSlash] = 0;
return wszPath + iBackSlash + 1;
}
LPWSTR Util_PathSplit2_ExWCHAR(_In_ LPWSTR wsz, _Out_writes_(cwsz1) LPWSTR wsz1, _In_ DWORD cwsz1)
{
WCHAR wch;
DWORD i = 0;
while((wch = wsz[i]) && (wch != '\\') && (i < cwsz1 - 1)) {
wsz1[i++] = wch;
}
wsz1[i] = 0;
return wsz[i] ? &wsz[i + 1] : L"";
}
int Util_wcsstrncmp(_In_ LPSTR sz, _In_ LPWSTR wsz, _In_opt_ DWORD cMax)
{
DWORD i;
cMax = cMax ? cMax : (DWORD)-1;
for(i = 0; i < cMax; i++) {
if(sz[i] != wsz[i]) { return 1; }
if(!sz[i]) { return 0; }
}
return 0;
}
VOID Util_GetPathDll(_Out_writes_(MAX_PATH) PCHAR szPath, _In_opt_ HMODULE hModule)
{
SIZE_T i;
GetModuleFileNameA(hModule, szPath, MAX_PATH - 4);
for(i = strlen(szPath) - 1; i > 0; i--) {
if(szPath[i] == '/' || szPath[i] == '\\') {
szPath[i + 1] = '\0';
return;
}
}
}
#define UTIL_NTSTATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define UTIL_NTSTATUS_END_OF_FILE ((NTSTATUS)0xC0000011L)
NTSTATUS Util_VfsReadFile_FromPBYTE(_In_ PBYTE pbFile, _In_ QWORD cbFile, _Out_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbRead, _In_ QWORD cbOffset)
{
if(cbOffset > cbFile) { return UTIL_NTSTATUS_END_OF_FILE; }
*pcbRead = (DWORD)min(cb, cbFile - cbOffset);
memcpy(pb, pbFile + cbOffset, *pcbRead);
return *pcbRead ? UTIL_NTSTATUS_SUCCESS : UTIL_NTSTATUS_END_OF_FILE;
}
NTSTATUS Util_VfsReadFile_FromNumber(_In_ QWORD qwValue, _Out_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbRead, _In_ QWORD cbOffset)
{
BYTE pbBuffer[32];
DWORD cbBuffer;
cbBuffer = snprintf(pbBuffer, 32, "%lli", qwValue);
return Util_VfsReadFile_FromPBYTE(pbBuffer, cbBuffer, pb, cb, pcbRead, cbOffset);
}
NTSTATUS Util_VfsReadFile_FromQWORD(_In_ QWORD qwValue, _Out_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbRead, _In_ QWORD cbOffset, _In_ BOOL fPrefix)
{
BYTE pbBuffer[32];
DWORD cbBuffer;
cbBuffer = snprintf(pbBuffer, 32, (fPrefix ? "0x%016llx" : "%016llx"), qwValue);
return Util_VfsReadFile_FromPBYTE(pbBuffer, cbBuffer, pb, cb, pcbRead, cbOffset);
}
NTSTATUS Util_VfsReadFile_FromDWORD(_In_ DWORD dwValue, _Out_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbRead, _In_ QWORD cbOffset, _In_ BOOL fPrefix)
{
BYTE pbBuffer[32];
DWORD cbBuffer;
cbBuffer = snprintf(pbBuffer, 32, (fPrefix ? "0x%08x" : "%08x"), dwValue);
return Util_VfsReadFile_FromPBYTE(pbBuffer, cbBuffer, pb, cb, pcbRead, cbOffset);
}
NTSTATUS Util_VfsReadFile_FromBOOL(_In_ BOOL fValue, _Out_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbRead, _In_ QWORD cbOffset)
{
BYTE pbBuffer[1];
pbBuffer[0] = fValue ? '1' : '0';
return Util_VfsReadFile_FromPBYTE(pbBuffer, 1, pb, cb, pcbRead, cbOffset);
}
NTSTATUS Util_VfsWriteFile_BOOL(_Inout_ PBOOL pfTarget, _In_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbWrite, _In_ QWORD cbOffset)
{
CHAR ch;
if((cb > 0) && (cbOffset == 0)) {
ch = *(PCHAR)pb;
*pfTarget = (ch == 0 || ch == '0') ? FALSE : TRUE;
}
*pcbWrite = cb;
return UTIL_NTSTATUS_SUCCESS;
}
NTSTATUS Util_VfsWriteFile_DWORD(_Inout_ PDWORD pdwTarget, _In_ PBYTE pb, _In_ DWORD cb, _Out_ PDWORD pcbWrite, _In_ QWORD cbOffset, _In_ DWORD dwMinAllow)
{
DWORD dw;
BYTE pbBuffer[9];
if(cbOffset < 8) {
snprintf(pbBuffer, 9, "%08x", *pdwTarget);
cb = (DWORD)min(8 - cbOffset, cb);
memcpy(pbBuffer + cbOffset, pb, cb);
pbBuffer[8] = 0;
dw = strtoul(pbBuffer, NULL, 16);
dw = max(dw, dwMinAllow);
*pdwTarget = dw;
}
*pcbWrite = cb;
return UTIL_NTSTATUS_SUCCESS;
}
LPSTR Util_StrDupA(_In_opt_ LPSTR sz)
{
SIZE_T cch;
LPSTR szDup;
if(!sz) { return NULL; }
cch = 1 + strlen(sz);
szDup = LocalAlloc(0, cch);
if(szDup) {
memcpy(szDup, sz, cch);
}
return szDup;
}
VOID Util_FileTime2String(_In_ PFILETIME pFileTime, _Out_writes_(MAX_PATH) LPSTR szTime)
{
SYSTEMTIME SystemTime;
if(!*(PQWORD)pFileTime) {
strcpy_s(szTime, MAX_PATH, " ***");
return;
}
FileTimeToSystemTime(pFileTime, &SystemTime);
sprintf_s(
szTime,
MAX_PATH,
"%04i-%02i-%02i %02i:%02i:%02i UTC",
SystemTime.wYear,
SystemTime.wMonth,
SystemTime.wDay,
SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond
);
}