-
Notifications
You must be signed in to change notification settings - Fork 194
/
HashCheckOptions.c
430 lines (349 loc) · 12.7 KB
/
HashCheckOptions.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
/**
* HashCheck Shell Extension
* Original work copyright (C) Kai Liu. All rights reserved.
* Modified work copyright (C) 2014, 2016 Christopher Gurnee. All rights reserved.
* Modified work copyright (C) 2016 Tim Schlueter. 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 "HashCheckOptions.h"
#include "libs/WinHash.h"
#include "RegHelpers.h"
#include "libs/IsFontAvailable.h"
#include <Strsafe.h>
#define OPTIONS_KEYNAME TEXT("Software\\HashCheck")
typedef struct {
PHASHCHECKOPTIONS popt;
HFONT hFont;
BOOL bFontChanged;
} OPTIONSCONTEXT, *POPTIONSCONTEXT;
static const LOGFONT DEFAULT_FIXED_FONT =
{
-15, // lfHeight (expressed as -2 * PointSize)
0, // lfWidth
0, // lfEscapement
0, // lfOrientation
FW_REGULAR, // lfWeight
FALSE, // lfItalic
FALSE, // lfUnderline
FALSE, // lfStrikeOut
DEFAULT_CHARSET, // lfCharSet
OUT_DEFAULT_PRECIS, // lfOutPrecision
CLIP_DEFAULT_PRECIS, // lfClipPrecision
DEFAULT_QUALITY, // lfQuality
FIXED_PITCH | FF_DONTCARE, // lfPitchAndFamily
TEXT("Lucida Console") // lfFaceName[LF_FACESIZE]
};
/*============================================================================*\
Function declarations
\*============================================================================*/
// Dialog general
INT_PTR CALLBACK OptionsDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
VOID WINAPI OptionsDlgInit( HWND hWnd, POPTIONSCONTEXT poptctx );
// Dialog commands
VOID WINAPI ChangeFont( HWND hWnd, POPTIONSCONTEXT poptctx );
BOOL WINAPI SaveSettings( HWND hWnd, POPTIONSCONTEXT poptctx );
/*============================================================================*\
Public functions
\*============================================================================*/
VOID CALLBACK ShowOptions_RunDLLW( HWND hWnd, HINSTANCE hInstance,
PWSTR pszCmdLine, INT nCmdShow )
{
HASHCHECKOPTIONS opt;
OptionsDialog(hWnd, &opt);
}
VOID __fastcall OptionsDialog( HWND hWndOwner, PHASHCHECKOPTIONS popt )
{
// First, activate our manifest
ULONG_PTR uActCtxCookie = ActivateManifest(TRUE);
// Initialize the dialog's context
OPTIONSCONTEXT optctx = { popt, NULL, FALSE };
// Show the options dialog; returning the results to our caller is handled
// by the flags set in the caller's popt
DialogBoxParam(
g_hModThisDll,
MAKEINTRESOURCE(IDD_OPTIONS),
hWndOwner,
OptionsDlgProc,
(LPARAM)&optctx
);
// Release the font handle, if the dialog created one
if (optctx.hFont) DeleteObject(optctx.hFont);
// Clean up the manifest activation
DeactivateManifest(uActCtxCookie);
}
VOID __fastcall OptionsLoad( PHASHCHECKOPTIONS popt )
{
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, OPTIONS_KEYNAME, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
hKey = NULL;
if (popt->dwFlags & HCOF_FILTERINDEX)
{
if (!( hKey &&
RegGetDW(hKey, TEXT("FilterIndex"), &popt->dwFilterIndex) &&
popt->dwFilterIndex && popt->dwFilterIndex <= NUM_HASHES))
{
// Fall back to default (SHA-256)
popt->dwFilterIndex = DEFAULT_HASH_ALGORITHM;
}
}
if (popt->dwFlags & HCOF_MENUDISPLAY)
{
if (!( hKey &&
RegGetDW(hKey, TEXT("MenuDisplay"), &popt->dwMenuDisplay) &&
popt->dwMenuDisplay < 3 ))
{
// Fall back to default (always show)
popt->dwMenuDisplay = 0;
}
}
if (popt->dwFlags & HCOF_SAVEENCODING)
{
if (!( hKey &&
RegGetDW(hKey, TEXT("SaveEncoding"), &popt->dwSaveEncoding) &&
popt->dwSaveEncoding < 3 ))
{
// Fall back to default (UTF-8)
popt->dwSaveEncoding = 0;
}
}
if (popt->dwFlags & HCOF_CHECKSUMS)
{
if (!(hKey &&
RegGetDW(hKey, TEXT("Checksums"), &popt->dwChecksums) &&
popt->dwChecksums > 0 && popt->dwChecksums <= WHEX_ALL))
{
// Fall back to default (CRC-32, SHA-1, both SHA-2's)
popt->dwChecksums = DEFAULT_HASH_ALGORITHMS;
}
}
if (popt->dwFlags & HCOF_FONT)
{
DWORD dwType;
DWORD cbData = sizeof(LOGFONT);
if (!( hKey && ERROR_SUCCESS ==
RegQueryValueEx(hKey, TEXT("Font"), NULL, &dwType, (PBYTE)&popt->lfFont, &cbData) &&
dwType == REG_BINARY && cbData == sizeof(LOGFONT)) )
{
HDC hDC;
// Copy the default font (Lucida Console)
memcpy(&popt->lfFont, &DEFAULT_FIXED_FONT, sizeof(LOGFONT));
// Use Consolas if it is available
if (IsFontAvailable(TEXT("Consolas")))
{
popt->lfFont.lfHeight = -16;
popt->lfFont.lfQuality = CLEARTYPE_QUALITY;
SSStaticCpy(popt->lfFont.lfFaceName, TEXT("Consolas"));
}
// Convert our -2 * PointSize to device units
hDC = GetDC(NULL);
popt->lfFont.lfHeight = MulDiv(popt->lfFont.lfHeight, GetDeviceCaps(hDC, LOGPIXELSY), 144);
ReleaseDC(NULL, hDC);
}
}
if (hKey)
RegCloseKey(hKey);
}
VOID __fastcall OptionsSave( PHASHCHECKOPTIONS popt )
{
HKEY hKey;
// Avoid creating the key if nothing is being set
if (popt->dwFlags == 0)
return;
if (hKey = RegOpen(HKEY_CURRENT_USER, OPTIONS_KEYNAME, NULL, TRUE))
{
if (popt->dwFlags & HCOF_FILTERINDEX)
RegSetDW(hKey, TEXT("FilterIndex"), popt->dwFilterIndex);
if (popt->dwFlags & HCOF_MENUDISPLAY)
RegSetDW(hKey, TEXT("MenuDisplay"), popt->dwMenuDisplay);
if (popt->dwFlags & HCOF_SAVEENCODING)
RegSetDW(hKey, TEXT("SaveEncoding"), popt->dwSaveEncoding);
if (popt->dwFlags & HCOF_CHECKSUMS)
RegSetDW(hKey, TEXT("Checksums"), popt->dwChecksums);
if (popt->dwFlags & HCOF_FONT)
RegSetValueEx(hKey, TEXT("Font"), 0, REG_BINARY, (PBYTE)&popt->lfFont, sizeof(LOGFONT));
RegCloseKey(hKey);
}
}
/*============================================================================*\
Dialog general
\*============================================================================*/
INT_PTR CALLBACK OptionsDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch (uMsg)
{
case WM_INITDIALOG:
{
SetWindowLongPtr(hWnd, DWLP_USER, lParam);
OptionsDlgInit(hWnd, (POPTIONSCONTEXT)lParam);
return(TRUE);
}
case WM_ENDSESSION:
{
if (wParam == FALSE) // if TRUE, fall through to WM_CLOSE
break;
}
case WM_CLOSE:
{
goto end_dialog;
}
case WM_COMMAND:
{
POPTIONSCONTEXT poptctx = (POPTIONSCONTEXT)GetWindowLongPtr(hWnd, DWLP_USER);
switch (LOWORD(wParam))
{
case IDC_OPT_FONT_CHANGE:
ChangeFont(hWnd, poptctx);
return(TRUE);
case IDC_OK:
if (SaveSettings(hWnd, poptctx) == FALSE)
return(TRUE); // don't close on save errors
case IDC_CANCEL:
end_dialog: EndDialog(hWnd, 0);
return(TRUE);
}
break;
}
case WM_NOTIFY:
{
PNMLINK pLinkCtrl = (PNMLINK)lParam;
if (pLinkCtrl->hdr.idFrom == IDC_OPT_LINK)
switch (pLinkCtrl->hdr.code)
{
case NM_CLICK:
case NM_RETURN:
ShellExecute(hWnd, NULL, pLinkCtrl->item.szUrl, NULL, NULL, SW_SHOWNORMAL);
return(TRUE);
}
break;
}
}
return(FALSE);
}
VOID WINAPI OptionsDlgInit( HWND hWnd, POPTIONSCONTEXT poptctx )
{
// Load strings
{
static const UINT16 arStrMap[][2] =
{
{ IDC_OPT_CM, IDS_OPT_CM },
{ IDC_OPT_CM_ALWAYS, IDS_OPT_CM_ALWAYS },
{ IDC_OPT_CM_EXTENDED, IDS_OPT_CM_EXTENDED },
{ IDC_OPT_CM_NEVER, IDS_OPT_CM_NEVER },
{ IDC_OPT_ENCODING, IDS_OPT_ENCODING },
{ IDC_OPT_ENCODING_UTF8, IDS_OPT_ENCODING_UTF8 },
{ IDC_OPT_ENCODING_UTF16, IDS_OPT_ENCODING_UTF16 },
{ IDC_OPT_ENCODING_ANSI, IDS_OPT_ENCODING_ANSI },
{ IDC_OPT_CHK, IDS_OPT_CHK },
{ IDC_OPT_FONT, IDS_OPT_FONT },
{ IDC_OPT_FONT_CHANGE, IDS_OPT_FONT_CHANGE },
{ IDC_OK, IDS_OPT_OK },
{ IDC_CANCEL, IDS_OPT_CANCEL }
};
static const TCHAR szFormat[] = TEXT("%s (v") TEXT(HASHCHECK_VERSION_STR) TEXT("/") TEXT(ARCH_NAME_PART) TEXT(")");
TCHAR szBuffer[MAX_STRINGMSG], szTitle[MAX_STRINGMSG];
UINT i;
for (i = 0; i < countof(arStrMap); ++i)
SetControlText(hWnd, arStrMap[i][0], arStrMap[i][1]);
LoadString(g_hModThisDll, IDS_OPT_TITLE, szBuffer, countof(szBuffer));
StringCchPrintf(szTitle, countof(szTitle), szFormat, szBuffer);
SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)szTitle);
}
// Load options
{
poptctx->popt->dwFlags = HCOF_ALL;
OptionsLoad(poptctx->popt);
poptctx->popt->dwFlags = 0;
SendDlgItemMessage(hWnd, IDC_OPT_CM_FIRSTID + poptctx->popt->dwMenuDisplay,
BM_SETCHECK, BST_CHECKED, 0);
SendDlgItemMessage(hWnd, IDC_OPT_ENCODING_FIRSTID + poptctx->popt->dwSaveEncoding,
BM_SETCHECK, BST_CHECKED, 0);
#define HASH_OPT_SET_CHECKS_op(alg) \
if (poptctx->popt->dwChecksums & WHEX_CHECK##alg) \
SendDlgItemMessage(hWnd, IDC_OPT_CHK_##alg, BM_SETCHECK, BST_CHECKED, 0);
FOR_EACH_HASH(HASH_OPT_SET_CHECKS_op)
if (poptctx->hFont = CreateFontIndirect(&poptctx->popt->lfFont))
SendDlgItemMessage(hWnd, IDC_OPT_FONT_PREVIEW, WM_SETFONT, (WPARAM)poptctx->hFont, FALSE);
SetDlgItemText(hWnd, IDC_OPT_FONT_PREVIEW, poptctx->popt->lfFont.lfFaceName);
}
}
/*============================================================================*\
Dialog commands
\*============================================================================*/
VOID WINAPI ChangeFont( HWND hWnd, POPTIONSCONTEXT poptctx )
{
HFONT hFont;
CHOOSEFONT cf;
cf.lStructSize = sizeof(cf);
cf.hwndOwner = hWnd;
cf.lpLogFont = &poptctx->popt->lfFont;
cf.Flags = CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
if (ChooseFont(&cf) && (hFont = CreateFontIndirect(&poptctx->popt->lfFont)))
{
// Signal that a new font should be saved if "OK" is pressed
poptctx->bFontChanged = TRUE;
// Update the preview
SendDlgItemMessage(hWnd, IDC_OPT_FONT_PREVIEW, WM_SETFONT, (WPARAM)hFont, FALSE);
SetDlgItemText(hWnd, IDC_OPT_FONT_PREVIEW, poptctx->popt->lfFont.lfFaceName);
// Clean up
if (poptctx->hFont) DeleteObject(poptctx->hFont);
poptctx->hFont = hFont;
}
}
BOOL WINAPI SaveSettings( HWND hWnd, POPTIONSCONTEXT poptctx )
{
DWORD i;
// Get the user-selected value for dwChecksums
i = 0;
#define HASH_OPT_GET_CHECKS_op(alg) \
if (SendDlgItemMessage(hWnd, IDC_OPT_CHK_##alg, BM_GETCHECK, 0, 0) == BST_CHECKED) \
i |= WHEX_CHECK##alg;
FOR_EACH_HASH(HASH_OPT_GET_CHECKS_op)
// Ensure at least one is checked
if (i == 0)
{
TCHAR szError[MAX_STRINGMSG];
// "Please select at least one checksum"
LoadString(g_hModThisDll, IDS_OPT_CHK_ERROR, szError, countof(szError));
MessageBox(hWnd, szError, NULL, MB_ICONWARNING);
return(FALSE);
}
// If the value has changed, flag it for saving
if (i <= WHEX_ALL && poptctx->popt->dwChecksums != i)
{
poptctx->popt->dwFlags |= HCOF_CHECKSUMS;
poptctx->popt->dwChecksums = i;
}
// Get the user-selected value for dwMenuDisplay
for (i = 0; i < 3; ++i)
{
if (SendDlgItemMessage(hWnd, IDC_OPT_CM_FIRSTID + i, BM_GETCHECK, 0, 0) == BST_CHECKED)
break;
}
// If the value has changed, flag it for saving
if (i < 3 && poptctx->popt->dwMenuDisplay != i)
{
poptctx->popt->dwFlags |= HCOF_MENUDISPLAY;
poptctx->popt->dwMenuDisplay = i;
}
// Get the user-selected value for dwSaveEncoding
for (i = 0; i < 3; ++i)
{
if (SendDlgItemMessage(hWnd, IDC_OPT_ENCODING_FIRSTID + i, BM_GETCHECK, 0, 0) == BST_CHECKED)
break;
}
// If the value has changed, flag it for saving
if (i < 3 && poptctx->popt->dwSaveEncoding != i)
{
poptctx->popt->dwFlags |= HCOF_SAVEENCODING;
poptctx->popt->dwSaveEncoding = i;
}
// Flag the font for saving if necessary
if (poptctx->bFontChanged)
poptctx->popt->dwFlags |= HCOF_FONT;
OptionsSave(poptctx->popt);
return(TRUE);
}