forked from gurnec/HashCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashSave.c
323 lines (257 loc) · 8.3 KB
/
HashSave.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
/**
* HashCheck Shell Extension
* Original work copyright (C) Kai Liu. All rights reserved.
* Modified work copyright (C) 2014 Christopher Gurnee. 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 "globals.h"
#include "HashCheckCommon.h"
#include "HashCalc.h"
#include "SetAppID.h"
// Control structures, from HashCalc.h
#define HASHSAVESCRATCH HASHCALCSCRATCH
#define PHASHSAVESCRATCH PHASHCALCSCRATCH
#define HASHSAVECONTEXT HASHCALCCONTEXT
#define PHASHSAVECONTEXT PHASHCALCCONTEXT
#define HASHSAVEITEM HASHCALCITEM
#define PHASHSAVEITEM PHASHCALCITEM
/*============================================================================*\
Function declarations
\*============================================================================*/
// Entry points / main functions
DWORD WINAPI HashSaveThread( PHASHSAVECONTEXT phsctx );
// Worker thread
VOID __fastcall HashSaveWorkerMain( PHASHSAVECONTEXT phsctx );
// Dialog general
INT_PTR CALLBACK HashSaveDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
VOID WINAPI HashSaveDlgInit( PHASHSAVECONTEXT phsctx );
/*============================================================================*\
Entry points / main functions
\*============================================================================*/
VOID WINAPI HashSaveStart( HWND hWndOwner, HSIMPLELIST hListRaw )
{
// Explorer will be blocking as long as this function is running, so we
// want to return as quickly as possible and leave the work up to the
// thread that we are going to spawn
PHASHSAVECONTEXT phsctx = SLSetContextSize(hListRaw, sizeof(HASHSAVECONTEXT));
if (phsctx)
{
HANDLE hThread;
phsctx->hWnd = hWndOwner;
phsctx->hListRaw = hListRaw;
InterlockedIncrement(&g_cRefThisDll);
SLAddRef(hListRaw);
if (hThread = CreateThreadCRT(HashSaveThread, phsctx))
{
CloseHandle(hThread);
return;
}
// If the thread creation was successful, the thread will be
// responsible for decrementing the ref count
SLRelease(hListRaw);
InterlockedDecrement(&g_cRefThisDll);
}
}
DWORD WINAPI HashSaveThread( PHASHSAVECONTEXT phsctx )
{
// First, activate our manifest and AddRef our host
ULONG_PTR uActCtxCookie = ActivateManifest(TRUE);
ULONG_PTR uHostCookie = HostAddRef();
// Calling HashCalcPrepare with a NULL hList will cause it to calculate
// and set cchPrefix, but it will not copy the data or walk the directories
// (we will leave that for the worker thread to do); the reason we do a
// limited scan now is so that we can show the file dialog (which requires
// cchPrefix for the automatic name generation) as soon as possible
phsctx->status = INACTIVE;
phsctx->hList = NULL;
HashCalcPrepare(phsctx);
// Get a file name from the user
ZeroMemory(&phsctx->ofn, sizeof(phsctx->ofn));
HashCalcInitSave(phsctx);
if (phsctx->hFileOut != INVALID_HANDLE_VALUE)
{
if (phsctx->hList = SLCreateEx(TRUE))
{
DialogBoxParam(
g_hModThisDll,
MAKEINTRESOURCE(IDD_HASHSAVE),
NULL,
HashSaveDlgProc,
(LPARAM)phsctx
);
SLRelease(phsctx->hList);
}
CloseHandle(phsctx->hFileOut);
}
// This must be the last thing that we free, since this is what supports
// our context!
SLRelease(phsctx->hListRaw);
// Clean up the manifest activation and release our host
DeactivateManifest(uActCtxCookie);
HostRelease(uHostCookie);
InterlockedDecrement(&g_cRefThisDll);
return(0);
}
/*============================================================================*\
Worker thread
\*============================================================================*/
VOID __fastcall HashSaveWorkerMain( PHASHSAVECONTEXT phsctx )
{
// Note that ALL message communication to and from the main window MUST
// be asynchronous, or else there may be a deadlock.
PHASHSAVEITEM pItem;
// Prep: expand directories, max path, etc. (prefix was set by earlier call)
PostMessage(phsctx->hWnd, HM_WORKERTHREAD_TOGGLEPREP, (WPARAM)phsctx, TRUE);
HashCalcPrepare(phsctx);
PostMessage(phsctx->hWnd, HM_WORKERTHREAD_TOGGLEPREP, (WPARAM)phsctx, FALSE);
// Indicate which hash type we are after
// WHEX_CHECKCRC32 = 0x01 = 1 << 0
// WHEX_CHECKMD4 = 0x02 = 1 << 1
// WHEX_CHECKMD5 = 0x04 = 1 << 2
// WHEX_CHECKSHA1 = 0x08 = 1 << 3
// WHEX_CHECKSHA256= 0x10 = 1 << 4
phsctx->whctx.flags = 1 << (phsctx->ofn.nFilterIndex - 1);
while (pItem = SLGetDataAndStep(phsctx->hList))
{
// Get the hash
WorkerThreadHashFile(
(PCOMMONCONTEXT)phsctx,
pItem->szPath,
&pItem->bValid,
&phsctx->whctx,
&pItem->results,
NULL
);
if (phsctx->status == CANCEL_REQUESTED)
return;
// Write the data
HashCalcWriteResult(phsctx, pItem);
// Update the UI
++phsctx->cSentMsgs;
PostMessage(phsctx->hWnd, HM_WORKERTHREAD_UPDATE, (WPARAM)phsctx, (LPARAM)pItem);
}
}
/*============================================================================*\
Dialog general
\*============================================================================*/
INT_PTR CALLBACK HashSaveDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
PHASHSAVECONTEXT phsctx;
switch (uMsg)
{
case WM_INITDIALOG:
{
phsctx = (PHASHSAVECONTEXT)lParam;
// Associate the window with the context and vice-versa
phsctx->hWnd = hWnd;
SetWindowLongPtr(hWnd, DWLP_USER, (LONG_PTR)phsctx);
SetAppIDForWindow(hWnd, TRUE);
HashSaveDlgInit(phsctx);
phsctx->ex.pfnWorkerMain = HashSaveWorkerMain;
phsctx->hThread = CreateThreadCRT(NULL, phsctx);
if (!phsctx->hThread || WaitForSingleObject(phsctx->hThread, 1000) != WAIT_TIMEOUT)
{
WorkerThreadCleanup((PCOMMONCONTEXT)phsctx);
EndDialog(hWnd, 0);
}
return(TRUE);
}
case WM_DESTROY:
{
SetAppIDForWindow(hWnd, FALSE);
break;
}
case WM_ENDSESSION:
case WM_CLOSE:
{
phsctx = (PHASHSAVECONTEXT)GetWindowLongPtr(hWnd, DWLP_USER);
goto cleanup_and_exit;
}
case WM_COMMAND:
{
phsctx = (PHASHSAVECONTEXT)GetWindowLongPtr(hWnd, DWLP_USER);
switch (LOWORD(wParam))
{
case IDC_PAUSE:
{
WorkerThreadTogglePause((PCOMMONCONTEXT)phsctx);
return(TRUE);
}
case IDC_CANCEL:
{
cleanup_and_exit:
phsctx->dwFlags |= HCF_EXIT_PENDING;
WorkerThreadStop((PCOMMONCONTEXT)phsctx);
WorkerThreadCleanup((PCOMMONCONTEXT)phsctx);
EndDialog(hWnd, 0);
break;
}
}
break;
}
case WM_TIMER:
{
// Vista: Workaround to fix their buggy progress bar
KillTimer(hWnd, TIMER_ID_PAUSE);
phsctx = (PHASHSAVECONTEXT)GetWindowLongPtr(hWnd, DWLP_USER);
if (phsctx->status == PAUSED)
SetProgressBarPause((PCOMMONCONTEXT)phsctx, PBST_PAUSED);
return(TRUE);
}
case HM_WORKERTHREAD_DONE:
{
phsctx = (PHASHSAVECONTEXT)wParam;
WorkerThreadCleanup((PCOMMONCONTEXT)phsctx);
EndDialog(hWnd, 0);
return(TRUE);
}
case HM_WORKERTHREAD_UPDATE:
{
phsctx = (PHASHSAVECONTEXT)wParam;
++phsctx->cHandledMsgs;
SendMessage(phsctx->hWndPBTotal, PBM_SETPOS, phsctx->cHandledMsgs, 0);
return(TRUE);
}
case HM_WORKERTHREAD_TOGGLEPREP:
{
HashCalcTogglePrep((PHASHSAVECONTEXT)wParam, (BOOL)lParam);
return(TRUE);
}
}
return(FALSE);
}
VOID WINAPI HashSaveDlgInit( PHASHSAVECONTEXT phsctx )
{
HWND hWnd = phsctx->hWnd;
// Load strings
{
SetControlText(hWnd, IDC_PAUSE, IDS_HS_PAUSE);
SetControlText(hWnd, IDC_CANCEL, IDS_HS_CANCEL);
}
// Set the window icon and title
{
PTSTR pszFileName = phsctx->ofn.lpstrFile + phsctx->ofn.nFileOffset;
TCHAR szFormat[MAX_STRINGRES];
LoadString(g_hModThisDll, IDS_HS_TITLE_FMT, szFormat, countof(szFormat));
wnsprintf(phsctx->scratch.sz, countof(phsctx->scratch.sz), szFormat, pszFileName);
SendMessage(
hWnd,
WM_SETTEXT,
0,
(LPARAM)phsctx->scratch.sz
);
SendMessage(
hWnd,
WM_SETICON,
ICON_BIG, // No need to explicitly set the small icon
(LPARAM)LoadIcon(g_hModThisDll, MAKEINTRESOURCE(IDI_FILETYPE))
);
}
// Initialize miscellaneous stuff
{
phsctx->dwFlags = 0;
phsctx->cTotal = 0;
}
}