-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.c
438 lines (378 loc) · 11.4 KB
/
utils.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//? Marius Negrutiu ([email protected]) :: 2014/02/02
#include "main.h"
#include "utils.h"
int _fltused = 0;
MEMORY_STATS g_MemStats = { 0 };
//++ UtilsInitialize
VOID UtilsInitialize()
{
TRACE( _T( " UtilsInitialize()\n" ) );
}
//++ UtilsDestroy
VOID UtilsDestroy()
{
TRACE( _T( " MyAlloc: %u calls, %I64u bytes\n" ), g_MemStats.AllocCalls, g_MemStats.AllocBytes );
TRACE( _T( " MyFree: %u calls, %I64u bytes\n" ), g_MemStats.FreeCalls, g_MemStats.FreeBytes );
TRACE( _T( " UtilsDestroy()\n" ) );
}
//++ TraceImpl
#if defined (TRACE_ENABLED)
VOID TraceImpl( __in LPCTSTR pszFormat, ... )
{
DWORD err = ERROR_SUCCESS;
if ( pszFormat && *pszFormat ) {
TCHAR szStr[1024];
int iLen1, iLen2;
va_list args;
iLen1 = wnsprintf( szStr, (int)ARRAYSIZE( szStr ), _T( "[xfer.th%04x] " ), GetCurrentThreadId() );
va_start( args, pszFormat );
iLen2 = wvnsprintf( szStr + iLen1, (int)ARRAYSIZE( szStr ) - iLen1, pszFormat, args );
if ( iLen2 > 0 ) {
if ( iLen1 + iLen2 < ARRAYSIZE( szStr ) )
szStr[iLen1 + iLen2] = 0; /// The string is not guaranteed to be null terminated
} else {
szStr[ARRAYSIZE( szStr ) - 1] = 0;
}
OutputDebugString( szStr );
va_end( args );
}
}
#endif
//++ TraceCallImpl
#if defined (TRACE_ENABLED)
VOID TraceCallImpl( __in stack_t **ppStackTop, __in_opt LPCTSTR pszPrefix )
{
int iStrLen = 4096;
LPTSTR pszStr = MyAllocStr( iStrLen );
assert( pszStr );
if (pszStr) {
stack_t *p;
LPTSTR psz = pszStr;
int i;
pszStr[0] = _T( '\0' );
if (pszPrefix && *pszPrefix)
i = wnsprintf( psz, iStrLen, _T( "%s" ), pszPrefix ), psz += i, iStrLen -= i;
if (ppStackTop) {
for (p = *ppStackTop; p; p = p->next) {
i = wnsprintf( psz, iStrLen, _T( " %s" ), p->text ), psz += i, iStrLen -= i;
if (CompareString( 0, NORM_IGNORECASE, p->text, -1, _T( "/END" ), -1 ) == CSTR_EQUAL)
break;
}
}
if (pszStr[0] != _T( '\0' )) {
i = wnsprintf( psz, iStrLen, _T( "\n" ) ), psz += i, iStrLen -= i;
OutputDebugString( pszStr );
}
MyFree( pszStr );
}
}
#endif
//++ GetLocalFileTime
BOOL GetLocalFileTime( _Out_ LPFILETIME lpFT )
{
if ( lpFT ) {
SYSTEMTIME st;
GetLocalTime( &st );
return SystemTimeToFileTime( &st, lpFT );
}
return FALSE;
}
//++ AllocErrorStr
VOID AllocErrorStr( _In_ DWORD dwErrCode, _Out_ TCHAR **ppszErrText )
{
if ( ppszErrText ) {
DWORD dwLen;
TCHAR szTextError[512];
HMODULE hModule = NULL;
DWORD dwExtraFlags = 0;
if ( dwErrCode >= INTERNET_ERROR_BASE && dwErrCode <= INTERNET_ERROR_LAST ) {
hModule = GetModuleHandle( _T( "wininet.dll" ) );
dwExtraFlags = FORMAT_MESSAGE_FROM_HMODULE;
} else {
dwExtraFlags = FORMAT_MESSAGE_FROM_SYSTEM;
}
szTextError[0] = 0;
dwLen = FormatMessage( FORMAT_MESSAGE_IGNORE_INSERTS | dwExtraFlags, hModule, dwErrCode, 0, szTextError, ARRAYSIZE( szTextError ), NULL );
if ( dwLen > 0 ) {
StrTrim( szTextError, _T( ". \r\n" ) );
MyStrDup( *ppszErrText, szTextError );
}
}
}
/// ULONLONG <-> double conversion groups
/// ULLONG_MAX == 18446744073709551615
ULONGLONG ULONGLONG_groups[19] = { 1000000000000000000, 100000000000000000, 10000000000000000, 1000000000000000, 100000000000000, 10000000000000, 1000000000000, 100000000000, 10000000000, 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 };
double double_groups[19] = { 1000000000000000000.0F, 100000000000000000.0F, 10000000000000000.0F, 1000000000000000.0F, 100000000000000.0F, 10000000000000.0F, 1000000000000.0F, 100000000000.0F, 10000000000.0F, 1000000000.0F, 100000000.0F, 10000000.0F, 1000000.0F, 100000.0F, 10000.0F, 1000.0F, 100.0F, 10.0F, 1.0F };
//++ MyUlonglongToDouble
double MyUlonglongToDouble( __in ULONGLONG ull )
{
double d = 0.0F;
int i;
for (i = 0; i < 19; i++) {
while (ull >= ULONGLONG_groups[i]) {
d += double_groups[i];
ull -= ULONGLONG_groups[i];
}
}
return d;
}
//++ MyDoubleToUlonglong
ULONGLONG MyDoubleToUlonglong( __in double d )
{
ULONGLONG ull = 0;
int i;
for (i = 0; i < 19; i++) {
while (d >= double_groups[i]) {
ull += ULONGLONG_groups[i];
d -= double_groups[i];
}
}
return ull;
}
//++ MyDiv64
ULONGLONG MyDiv64( __in ULONGLONG iNumerator, __in ULONGLONG iDenominator )
{
/* ULONGLONG iQuotient = 0;
if (iDenominator != 0) {
while (iNumerator >= iDenominator) {
iNumerator -= iDenominator;
iQuotient++;
}
}
return iQuotient;*/
if (iDenominator == 0)
iDenominator = 1;
return MyDoubleToUlonglong( MyUlonglongToDouble( iNumerator ) / MyUlonglongToDouble( iDenominator ) );
}
//++ MyMulDiv64
ULONGLONG MyMulDiv64( __in ULONGLONG iNumber, __in ULONGLONG iNumerator, __in ULONGLONG iDenominator )
{
/// iNumber*(iNumerator/iDenominator)
return MyDoubleToUlonglong( MyUlonglongToDouble( iNumber ) * (MyUlonglongToDouble( iNumerator ) / MyUlonglongToDouble( iDenominator )) );
}
//++ MyTimeDiff
ULONG MyTimeDiff( __in PFILETIME pEndTime, __in PFILETIME pStartTime )
{
/// NOTE: Large integer operations are not available because we're not linking to CRT
if (pStartTime && pEndTime) {
ULONGLONG iDiff = MyDiv64( ((PULARGE_INTEGER)pEndTime)->QuadPart - ((PULARGE_INTEGER)pStartTime)->QuadPart, 10000 );
if (iDiff < UINT_MAX)
return (ULONG)iDiff;
else
return UINT_MAX; /// ~49 days
}
return 0;
}
//++ ReadVersionInfoString
DWORD ReadVersionInfoString(
__in_opt LPCTSTR szFile,
__in LPCTSTR szStringName,
__out LPTSTR szStringValue,
__in UINT iStringValueLen
)
{
DWORD err = ERROR_SUCCESS;
// Validate parameters
if (szStringName && *szStringName && szStringValue && (iStringValueLen > 0)) {
TCHAR szExeFile[MAX_PATH];
DWORD dwVerInfoSize;
szStringValue[0] = 0;
if (szFile && *szFile) {
lstrcpyn( szExeFile, szFile, ARRAYSIZE( szExeFile ) );
} else {
GetModuleFileName( NULL, szExeFile, ARRAYSIZE( szExeFile ) ); /// Current executable
}
dwVerInfoSize = GetFileVersionInfoSize( szExeFile, NULL );
if (dwVerInfoSize > 0) {
HANDLE hMem = GlobalAlloc( GMEM_MOVEABLE, dwVerInfoSize );
if (hMem) {
LPBYTE pMem = GlobalLock( hMem );
if (pMem) {
if (GetFileVersionInfo( szExeFile, 0, dwVerInfoSize, pMem )) {
typedef struct _LANGANDCODEPAGE { WORD wLanguage; WORD wCodePage; } LANGANDCODEPAGE;
LANGANDCODEPAGE *pCodePage;
UINT iCodePageSize = sizeof( *pCodePage );
/// Code page
if (VerQueryValue( pMem, _T( "\\VarFileInfo\\Translation" ), (LPVOID*)&pCodePage, &iCodePageSize )) {
TCHAR szTemp[255];
LPCTSTR szValue = NULL;
UINT iValueLen = 0;
/// Read version string
wnsprintf( szTemp, ARRAYSIZE( szTemp ), _T( "\\StringFileInfo\\%04x%04x\\%s" ), pCodePage->wLanguage, pCodePage->wCodePage, szStringName );
if (VerQueryValue( pMem, szTemp, (LPVOID*)&szValue, &iValueLen )) {
/// Output
if (*szValue) {
lstrcpyn( szStringValue, szValue, iStringValueLen );
if (iValueLen > iStringValueLen) {
/// The output buffer is not large enough
/// We'll return the truncated string, and ERROR_BUFFER_OVERFLOW error code
err = ERROR_BUFFER_OVERFLOW;
}
} else {
err = ERROR_NOT_FOUND;
}
} else {
err = ERROR_NOT_FOUND;
}
} else {
err = ERROR_NOT_FOUND;
}
} else {
err = GetLastError();
}
GlobalUnlock( hMem );
} else {
err = GetLastError();
}
GlobalFree( hMem );
} else {
err = GetLastError();
}
} else {
err = GetLastError();
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ BinaryToString
ULONG BinaryToString(
__in LPVOID pData, __in ULONG iDataSize,
__out LPTSTR pszStr, __in ULONG iStrLen
)
{
ULONG iLen = 0;
if (pszStr && iStrLen) {
pszStr[0] = _T( '\0' );
if (pData) {
ULONG i, n;
CHAR ch;
for (i = 0, n = (ULONG)__min( iDataSize, iStrLen - 1 ); i < n; i++) {
ch = ((PCH)pData)[i];
if ((ch >= 32 /*&& ch < 127*/) || ch == '\r' || ch == '\n') {
pszStr[i] = ch;
} else {
pszStr[i] = _T( '.' );
}
}
pszStr[i] = _T( '\0' );
iLen += i; /// Not including NULL terminator
}
}
return iLen;
}
//++ RegReadDWORD
DWORD RegReadDWORD( __in HKEY hRoot, __in LPCTSTR pszKey, __in LPCTSTR pszValue, __out LPDWORD pdwValue )
{
DWORD err = ERROR_SUCCESS;
if (hRoot && pszKey && *pszKey && pszValue && pdwValue) {
HKEY hKey;
*pdwValue = 0; /// Init
err = RegOpenKeyEx( hRoot, pszKey, 0, KEY_READ | KEY_WOW64_64KEY, &hKey );
if (err == ERROR_SUCCESS) {
DWORD dwType, dwSize = sizeof( *pdwValue );
err = RegQueryValueEx( hKey, pszValue, NULL, &dwType, (LPBYTE)pdwValue, &dwSize );
if (err == ERROR_SUCCESS) {
if (dwType == REG_DWORD) {
/// Done
} else {
err = ERROR_INVALID_DATATYPE;
}
}
RegCloseKey( hKey );
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ RegWriteDWORD
DWORD RegWriteDWORD( __in HKEY hRoot, __in LPCTSTR pszKey, __in LPCTSTR pszValue, __in DWORD dwValue )
{
DWORD err = ERROR_SUCCESS;
if (hRoot && pszKey && *pszKey && pszValue) {
HKEY hKey;
err = RegOpenKeyEx( hRoot, pszKey, 0, KEY_WRITE | KEY_WOW64_64KEY, &hKey );
if (err == ERROR_SUCCESS) {
err = RegSetValueEx( hKey, pszValue, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof( dwValue ) );
RegCloseKey( hKey );
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ MyStrToInt64
BOOL MyStrToInt64( _In_ LPCTSTR pszStr, _Out_ PUINT64 piNum )
{
BOOL bRet = FALSE;
int ch, i;
UINT64 n;
if (pszStr && piNum) {
for (*piNum = 0;;) {
ch = *(pszStr++) - _T( '0' );
if (ch < 0 || ch > 9)
break;
/// *piNum *= 10;
for (i = 0, n = *piNum; i < 9; i++)
*piNum += n;
*piNum += ch;
}
bRet = TRUE;
}
return bRet;
}
//++ MyCreateDirectory
ULONG MyCreateDirectory( _In_ LPCTSTR pszPath, _In_ BOOLEAN bHasFilename )
{
ULONG e = ERROR_SUCCESS;
LPTSTR pszBuf;
if (!pszPath || !*pszPath)
return ERROR_INVALID_PARAMETER;
// Work with a copy of the string
MyStrDup( pszBuf, pszPath );
if (pszBuf) {
TCHAR *psz, ch;
ULONG len = lstrlen( pszBuf );
// Strip trailing backslashes
for (psz = pszBuf + len - 1; (psz > pszBuf) && (*psz == _T( '\\' ) || *psz == _T( '/' )); psz--)
*psz = _T( '\0' ), len--;
if (bHasFilename) {
// Strip filename
for (psz = pszBuf + len - 1; (psz > pszBuf) && (*psz != _T( '\\' )) && (*psz != _T( '/' )); psz--)
*psz = _T( '\0' ), len--;
// Strip trailing backslashes
for (psz = pszBuf + len - 1; (psz > pszBuf) && (*psz == _T( '\\' ) || *psz == _T( '/' )); psz--)
*psz = _T( '\0' ), len--;
}
psz = pszBuf;
// Skip UNC server name (\\?\UNC\SERVER)
if (CompareString( CP_ACP, NORM_IGNORECASE, psz, 8, _T( "\\\\?\\UNC\\" ), -1 ) == CSTR_EQUAL) {
psz += 8;
for (; *psz != _T( '\\' ) && *psz != _T( '\0' ); psz++);
}
// Skip \\?\ prefix
if (CompareString( CP_ACP, NORM_IGNORECASE, psz, 4, _T( "\\\\?\\" ), -1 ) == CSTR_EQUAL)
psz += 4;
// Skip network server name (\\SERVER)
if (CompareString( CP_ACP, NORM_IGNORECASE, psz, 2, _T( "\\\\" ), -1 ) == CSTR_EQUAL) {
psz += 2;
for (; *psz != _T( '\\' ) && *psz != _T( '\0' ); psz++);
}
// Create intermediate directories
for (; (e == ERROR_SUCCESS) && *psz; ) {
for (; *psz == _T( '\\' ); psz++);
for (; *psz != _T( '\\' ) && *psz != _T( '\0' ); psz++);
ch = *psz, *psz = _T( '\0' );
e = CreateDirectory( pszBuf, NULL ) ? ERROR_SUCCESS : GetLastError();
if (e == ERROR_ALREADY_EXISTS || e == ERROR_ACCESS_DENIED)
e = ERROR_SUCCESS;
*psz = ch;
}
MyFree( pszBuf );
} else {
e = ERROR_OUTOFMEMORY;
}
return e;
}