forked from gurnec/HashCheck
-
Notifications
You must be signed in to change notification settings - Fork 12
/
HashCheckCommon.c
511 lines (440 loc) · 15.9 KB
/
HashCheckCommon.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
* HashCheck Shell Extension
* Original work copyright (C) Kai Liu. All rights reserved.
* Modified work copyright (C) 2016 Christopher Gurnee. All rights reserved.
* Modified work copyright (C) 2021 Mounir IDRASSI. All rights reserved.
*
* Please refer to readme.txt for information about this source code.
* Please refer to license.txt for details about distribution and modification.
**/
#include <assert.h>
#include "globals.h"
#include "HashCheckCommon.h"
#include "GetHighMSB.h"
#include <Strsafe.h>
#define PROGRESS_BAR_STEPS 300
HANDLE __fastcall CreateThreadCRT( PVOID pThreadProc, PVOID pvParam )
{
if (!pThreadProc)
{
// If we have a NULL pThreadProc, then we are starting a worker thread,
// and there is some initialization that we need to take care of...
PCOMMONCONTEXT pcmnctx = pvParam;
pcmnctx->status = ACTIVE;
pcmnctx->cSentMsgs = 0;
pcmnctx->cHandledMsgs = 0;
pcmnctx->hWndPBTotal = GetDlgItem(pcmnctx->hWnd, IDC_PROG_TOTAL);
pcmnctx->hWndPBFile = GetDlgItem(pcmnctx->hWnd, IDC_PROG_FILE);
if (pcmnctx->hUnpauseEvent == NULL)
pcmnctx->hUnpauseEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
SendMessage(pcmnctx->hWndPBFile, PBM_SETRANGE, 0, MAKELPARAM(0, PROGRESS_BAR_STEPS));
pThreadProc = WorkerThreadStartup;
}
return((HANDLE)_beginthreadex(
NULL,
BASE_STACK_SIZE,
pThreadProc,
pvParam,
0,
NULL
));
}
HANDLE __fastcall OpenFileForReading( PCTSTR pszPath )
{
return(CreateFile(
pszPath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL
));
}
VOID __fastcall HCNormalizeString( PTSTR psz )
{
if (!psz) return;
while (*psz)
{
switch (*psz)
{
case TEXT('\r'):
*psz = TEXT('\n');
break;
case TEXT('\t'):
case TEXT('"'):
case TEXT('*'):
*psz = TEXT(' ');
break;
case TEXT('/'):
*psz = TEXT('\\');
break;
}
++psz;
}
}
VOID WINAPI SetControlText( HWND hWnd, UINT uCtrlID, UINT uStringID )
{
TCHAR szBuffer[MAX_STRINGMSG];
LoadString(g_hModThisDll, uStringID, szBuffer, countof(szBuffer));
SetDlgItemText(hWnd, uCtrlID, szBuffer);
}
VOID WINAPI EnableControl( HWND hWnd, UINT uCtrlID, BOOL bEnable )
{
HWND hWndControl = GetDlgItem(hWnd, uCtrlID);
ShowWindow(hWndControl, (bEnable) ? SW_SHOW : SW_HIDE);
EnableWindow(hWndControl, bEnable);
}
VOID WINAPI FormatFractionalResults( PTSTR pszFormat, PTSTR pszBuffer, UINT uPart, UINT uTotal )
{
// pszFormat must be at least MAX_STRINGRES TCHARs long
// pszBuffer must be at least MAX_STRINGMSG TCHARs long
if (*pszFormat == 0)
{
LoadString(
g_hModThisDll,
(uTotal == 1) ? IDS_HP_RESULT_FMT : IDS_HP_RESULTS_FMT,
pszFormat,
MAX_STRINGRES
);
}
if (*pszFormat != TEXT('!'))
StringCchPrintf(pszBuffer, MAX_STRINGMSG, pszFormat, uPart, uTotal);
else
StringCchPrintf(pszBuffer, MAX_STRINGMSG, pszFormat + 1, uTotal, uPart);
}
VOID WINAPI SetProgressBarPause( PCOMMONCONTEXT pcmnctx, WPARAM iState )
{
// For Windows Classic, we can change the color to indicate a pause
COLORREF clrProgress = (iState == PBST_NORMAL) ? CLR_DEFAULT : RGB(0xFF, 0x80, 0x00);
SendMessage(pcmnctx->hWndPBTotal, PBM_SETBARCOLOR, 0, clrProgress);
SendMessage(pcmnctx->hWndPBFile, PBM_SETBARCOLOR, 0, clrProgress);
// Toggle the marquee animation if applicable
if (pcmnctx->dwFlags & HCF_MARQUEE)
SendMessage(pcmnctx->hWndPBTotal, PBM_SETMARQUEE, iState == PBST_NORMAL, MARQUEE_INTERVAL);
if (g_uWinVer >= 0x0600)
{
// If this is Vista, we can also set the state
SendMessage(pcmnctx->hWndPBTotal, PBM_SETSTATE, iState, 0);
SendMessage(pcmnctx->hWndPBFile, PBM_SETSTATE, iState, 0);
// Vista's progress bar is buggy--if you pause it while it is animating,
// the color will not change (but it will stop animating), so it may
// be necessary to send another PBM_SETSTATE to get it right
if (iState == PBST_PAUSED)
SetTimer(pcmnctx->hWnd, TIMER_ID_PAUSE, 750, NULL);
}
}
VOID WINAPI WorkerThreadTogglePause( PCOMMONCONTEXT pcmnctx )
{
if (pcmnctx->status == ACTIVE)
{
ResetEvent(pcmnctx->hUnpauseEvent);
pcmnctx->status = PAUSED;
if (!(pcmnctx->dwFlags & HCF_EXIT_PENDING))
{
SetControlText(pcmnctx->hWnd, IDC_PAUSE, IDS_HC_RESUME);
SetProgressBarPause(pcmnctx, PBST_PAUSED);
}
}
else if (pcmnctx->status == PAUSED)
{
pcmnctx->status = ACTIVE;
if (!(pcmnctx->dwFlags & HCF_EXIT_PENDING))
{
SetControlText(pcmnctx->hWnd, IDC_PAUSE, IDS_HC_PAUSE);
SetProgressBarPause(pcmnctx, PBST_NORMAL);
}
SetEvent(pcmnctx->hUnpauseEvent);
}
}
VOID WINAPI WorkerThreadStop( PCOMMONCONTEXT pcmnctx )
{
if (pcmnctx->status == INACTIVE || pcmnctx->status == CLEANUP_COMPLETED)
return;
// If the thread is paused, unpause it
if (pcmnctx->status == PAUSED)
{
// Signal cancellation first, so that unpaused threads immediately exit
pcmnctx->status = CANCEL_REQUESTED;
SetEvent(pcmnctx->hUnpauseEvent);
}
else
{
// Signal cancellation
pcmnctx->status = CANCEL_REQUESTED;
}
// Disable the control buttons
if (! (pcmnctx->dwFlags & (HCF_EXIT_PENDING | HCF_RESTARTING)))
{
EnableWindow(GetDlgItem(pcmnctx->hWnd, IDC_PAUSE), FALSE);
EnableWindow(GetDlgItem(pcmnctx->hWnd, IDC_STOP), FALSE);
}
}
VOID WINAPI WorkerThreadCleanup( PCOMMONCONTEXT pcmnctx )
{
if (pcmnctx->status == CLEANUP_COMPLETED)
return;
// There are only two times this function gets called:
// Case 1: The worker thread has exited on its own, and this function
// was invoked in response to the thread's exit notification.
// Case 2: A forced abort was requested (app exit, settings change, etc.),
// where this is called right after calling WorkerThreadStop to signal the
// thread to exit.
if (pcmnctx->hThread != NULL)
{
if (pcmnctx->status != INACTIVE)
{
// Forced abort, where the thread has been told to stop but has not yet
// stopped. With the MS Concurrency runtime, there's no simple way to
// terminate errant threads; it's better to abort the process than to
// allow them to continue (maybe maxing out the CPU) in the background.
if (WaitForSingleObject(pcmnctx->hThread, 10000) == WAIT_TIMEOUT)
abort();
}
CloseHandle(pcmnctx->hThread);
pcmnctx->hThread = NULL;
}
// If we're done with the Unpause event and it's open, close it
if (!(pcmnctx->dwFlags & HCF_RESTARTING) && pcmnctx->hUnpauseEvent != NULL)
{
CloseHandle(pcmnctx->hUnpauseEvent);
pcmnctx->hUnpauseEvent = NULL;
}
pcmnctx->status = CLEANUP_COMPLETED;
if (! (pcmnctx->dwFlags & (HCF_EXIT_PENDING | HCF_RESTARTING)))
{
static const UINT16 arCtrls[] =
{
IDC_PROG_TOTAL,
IDC_PROG_FILE,
IDC_PAUSE,
IDC_STOP
};
UINT i;
for (i = 0; i < countof(arCtrls); ++i)
EnableControl(pcmnctx->hWnd, arCtrls[i], FALSE);
}
}
DWORD WINAPI WorkerThreadStartup( PCOMMONCONTEXT pcmnctx )
{
pcmnctx->pfnWorkerMain(pcmnctx);
pcmnctx->status = INACTIVE;
if (! (pcmnctx->dwFlags & (HCF_EXIT_PENDING | HCF_RESTARTING)))
PostMessage(pcmnctx->hWnd, HM_WORKERTHREAD_DONE, (WPARAM)pcmnctx, 0);
return(0);
}
// Post messages to update the progress bar. If there are multiple file-hashing threads,
// then only the thread currently operating on the largest file updates the progress bar.
__inline VOID UpdateProgressBar( HWND hWndPBFile, PCRITICAL_SECTION pCritSec,
PBOOL pbCurrentlyUpdating, volatile ULONGLONG* pcbCurrentMaxSize,
ULONGLONG cbFileSize, ULONGLONG cbFileRead, PUINT pLastProgress )
{
if (pCritSec) // if we're one among many file-hashing threads
{
// All the checks below outside of critical sections are innacurate; they're meant
// to quickly check if entering the critical section can be avoided, but they must
// be repeated inside the critical sections to avoid potential race conditions
if (*pbCurrentlyUpdating)
{
if (cbFileSize != *pcbCurrentMaxSize)
{
// Some other thread is now updating the progress bar
*pbCurrentlyUpdating = FALSE;
return;
}
UINT newProgress = (UINT) (PROGRESS_BAR_STEPS * cbFileRead / cbFileSize);
if (newProgress == *pLastProgress)
return;
EnterCriticalSection(pCritSec);
if (cbFileSize != *pcbCurrentMaxSize)
{
LeaveCriticalSection(pCritSec);
// Some other thread is now updating the progress bar
*pbCurrentlyUpdating = FALSE;
return;
}
// Special case for when this file has been completed:
if (cbFileRead == 0)
*pcbCurrentMaxSize = 0; // relinquish progress bar control to the next largest file
PostMessage(hWndPBFile, PBM_SETPOS, newProgress, 0);
LeaveCriticalSection(pCritSec);
*pLastProgress = newProgress;
}
else // if not *pbCurrentlyUpdating
{
if (cbFileSize > *pcbCurrentMaxSize) // if we should take over updating the progress bar
{
UINT newProgress = (UINT)(PROGRESS_BAR_STEPS * cbFileRead / cbFileSize);
EnterCriticalSection(pCritSec);
if (cbFileSize > *pcbCurrentMaxSize) // if we should definitely take over updating the progress bar
{
*pcbCurrentMaxSize = cbFileSize;
PostMessage(hWndPBFile, PBM_SETPOS, newProgress, 0);
}
LeaveCriticalSection(pCritSec);
*pbCurrentlyUpdating = TRUE;
*pLastProgress = newProgress;
}
}
}
else // if we're the only file-hashing thread
{
UINT newProgress = (UINT)(PROGRESS_BAR_STEPS * cbFileRead / cbFileSize);
if (newProgress != *pLastProgress)
{
PostMessage(hWndPBFile, PBM_SETPOS, newProgress, 0);
*pLastProgress = newProgress;
}
}
}
VOID WINAPI WorkerThreadHashFile( PCOMMONCONTEXT pcmnctx, PCTSTR pszPath,
PWHCTXEX pwhctx, PWHRESULTEX pwhres, PBYTE pbuffer,
PFILESIZE pFileSize, LPARAM lParam,
PCRITICAL_SECTION pUpdateCritSec, volatile ULONGLONG* pcbCurrentMaxSize
#ifdef _TIMED
, PDWORD pdwElapsed
#endif
)
{
HANDLE hFile;
// If the worker thread is working so fast that the UI cannot catch up,
// pause for a bit to let things settle down
while (pcmnctx->cSentMsgs > pcmnctx->cHandledMsgs + 50)
{
Sleep(50);
if (pcmnctx->status == PAUSED)
WaitForSingleObject(pcmnctx->hUnpauseEvent, INFINITE);
if (pcmnctx->status == CANCEL_REQUESTED)
return;
}
// This can happen if a user changes the hash selection in HashProp (if no
// new hashes were selected; we still want to run the throttling code above)
if (pwhctx->dwFlags == 0)
{
#ifdef _TIMED
if (pdwElapsed)
*pdwElapsed = 0;
#endif
return;
}
// Indicate that we want lower-case results (TODO: make this an option)
pwhctx->uCaseMode = WHFMT_LOWERCASE;
if ((hFile = OpenFileForReading(pszPath)) != INVALID_HANDLE_VALUE)
{
ULONGLONG cbFileSize, cbFileRead = 0;
DWORD cbBufferRead;
UINT lastProgress = 0;
UINT8 cInner = 0;
if (GetFileSizeEx(hFile, (PLARGE_INTEGER)&cbFileSize))
{
// The progress bar is updates only once every 4 buffer reads; if
// the file is small enough that it requires only one such cycle,
// then do not bother with updating the progress bar; this improves
// performance when working with large numbers of small files
BOOL bUpdateProgress = cbFileSize >= READ_BUFFER_SIZE * 4,
bCurrentlyUpdating = FALSE;
// If the caller provides a way to return the file size, then set
// the file size; send a SETSIZE notification only if it was "big"
if (pFileSize)
{
pFileSize->ui64 = cbFileSize;
StrFormatKBSize(cbFileSize, pFileSize->sz, countof(pFileSize->sz));
if (cbFileSize > READ_BUFFER_SIZE)
PostMessage(pcmnctx->hWnd, HM_WORKERTHREAD_SETSIZE, (WPARAM)pcmnctx, lParam != -1 ? lParam : (LPARAM)pFileSize);
}
#ifdef _TIMED
DWORD dwStarted;
if (pdwElapsed)
dwStarted = GetTickCount();
#endif
// Finally, read the file and calculate the checksum; the
// progress bar is updated only once every 4 buffer reads (512K)
WHInitEx(pwhctx);
do // Outer loop: keep going until the end
{
do // Inner loop: break every 4 cycles or if the end is reached
{
if (pcmnctx->status == PAUSED)
WaitForSingleObject(pcmnctx->hUnpauseEvent, INFINITE);
if (pcmnctx->status == CANCEL_REQUESTED)
{
CloseHandle(hFile);
return;
}
ReadFile(hFile, pbuffer, READ_BUFFER_SIZE, &cbBufferRead, NULL);
WHUpdateEx(pwhctx, pbuffer, cbBufferRead);
cbFileRead += cbBufferRead;
} while (cbBufferRead == READ_BUFFER_SIZE && (++cInner & 0x03));
if (bUpdateProgress)
UpdateProgressBar(pcmnctx->hWndPBFile, pUpdateCritSec, &bCurrentlyUpdating,
pcbCurrentMaxSize, cbFileSize, cbFileRead, &lastProgress);
} while (cbBufferRead == READ_BUFFER_SIZE);
WHFinishEx(pwhctx, pwhres);
#ifdef _TIMED
if (pdwElapsed)
*pdwElapsed = GetTickCount() - dwStarted;
#endif
// If we encountered a file read error
if (cbFileRead != cbFileSize)
// Clear the valid-results bits for the hashes we just calculated
// (they are set by WHFinishEx, but they're apparently *not* valid)
pwhres->dwFlags &= ~pwhctx->dwFlags;
if (bUpdateProgress)
UpdateProgressBar(pcmnctx->hWndPBFile, pUpdateCritSec, &bCurrentlyUpdating,
pcbCurrentMaxSize, cbFileSize, 0, &lastProgress);
}
CloseHandle(hFile);
}
}
__forceinline HANDLE WINAPI GetActCtx( HMODULE hModule, PCTSTR pszResourceName )
{
// Wraps away the silliness of CreateActCtx, including the fact that
// it will fail if you do not provide a valid path string even if you
// supply a valid module handle, which is quite silly since the path is
// just going to get translated into a module handle anyway by the API.
ACTCTX ctx;
TCHAR szModule[MAX_PATH << 1];
GetModuleFileName(hModule, szModule, countof(szModule));
ctx.cbSize = sizeof(ctx);
ctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
ctx.lpSource = szModule;
ctx.lpResourceName = pszResourceName;
ctx.hModule = hModule;
return(CreateActCtx(&ctx));
}
ULONG_PTR WINAPI ActivateManifest( BOOL bActivate )
{
if (!g_bActCtxCreated)
{
g_bActCtxCreated = TRUE;
g_hActCtx = GetActCtx(g_hModThisDll, MAKEINTRESOURCE(IDR_RT_MANIFEST));
}
if (g_hActCtx != INVALID_HANDLE_VALUE)
{
ULONG_PTR uCookie;
if (!bActivate)
return(1); // Just indicate that we have a good g_hActCtx
else if (ActivateActCtx(g_hActCtx, &uCookie))
return(uCookie);
}
// We can assume that zero is never a valid cookie value...
// * http://support.microsoft.com/kb/830033
// * http://blogs.msdn.com/jonwis/archive/2006/01/12/512405.aspx
return(0);
}
ULONG_PTR __fastcall HostAddRef( )
{
LPUNKNOWN pUnknown;
if (SHGetInstanceExplorer(&pUnknown) == S_OK)
return((ULONG_PTR)pUnknown);
else
return(0);
}
VOID __fastcall HostRelease( ULONG_PTR uCookie )
{
if (uCookie)
{
LPUNKNOWN pUnknown = (LPUNKNOWN)uCookie;
pUnknown->lpVtbl->Release(pUnknown);
}
}