forked from gurnec/HashCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashCalc.c
606 lines (500 loc) · 16.6 KB
/
HashCalc.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/**
* 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 "UnicodeHelpers.h"
#define SAVE_FILTERS TEXT("CRC-32 (*.sfv)\0*.sfv\0") \
TEXT("MD4 (*.md4)\0*.md4\0") \
TEXT("MD5 (*.md5)\0*.md5\0") \
TEXT("SHA-1 (*.sha1)\0*.sha1\0")\
TEXT("SHA-256 (*.sha256)\0*.sha256\0")
static const PCTSTR SAVE_EXTS[] =
{
NULL,
TEXT(".sfv"),
TEXT(".md4"),
TEXT(".md5"),
TEXT(".sha1"),
TEXT(".sha256")
};
static const TCHAR SAVE_DEFAULT_NAME[] = TEXT("checksums");
// Due to the stupidity of the x64 compiler, the code emitted for the non-inline
// function is not as efficient as it is on x86
#ifdef _M_IX86
#undef SSChainNCpy2
#define SSChainNCpy2 SSChainNCpy2F
#endif
/*============================================================================*\
Function declarations
\*============================================================================*/
// Path processing
VOID WINAPI HashCalcWalkDirectory( PHASHCALCCONTEXT phcctx, PTSTR pszPath, UINT cchPath );
__forceinline BOOL WINAPI IsSpecialDirectoryName( PCTSTR pszPath );
__forceinline BOOL WINAPI IsDoubleSlashPath( PCTSTR pszPath );
// Save helpers
__forceinline VOID WINAPI HashCalcSetSavePrefix( PHASHCALCCONTEXT phcctx, PTSTR pszSave );
/*============================================================================*\
Path processing
\*============================================================================*/
VOID WINAPI HashCalcPrepare( PHASHCALCCONTEXT phcctx )
{
PTSTR pszPrev = NULL;
PTSTR pszCurrent, pszCurrentEnd;
UINT cbCurrent, cchCurrent;
SLReset(phcctx->hListRaw);
while (pszCurrent = SLGetDataAndStepEx(phcctx->hListRaw, &cbCurrent))
{
pszCurrentEnd = BYTEADD(pszCurrent, cbCurrent);
cchCurrent = cbCurrent / sizeof(TCHAR) - 1;
// Get rid of the trailing slash if there is one
if (cchCurrent && *(pszCurrentEnd - 1) == TEXT('\\'))
{
*(--pszCurrentEnd) = 0;
--cchCurrent;
cbCurrent -= sizeof(TCHAR);
}
if (pszPrev == NULL)
{
// Initialize the cchPrefix (and cchMax) for the first time; since
// we have stripped away the trailing slash (if there was one), we
// are guaranteed that cchPrefix < cchCurrent for the first run,
// and that cchPrefix < cchPrev for all other iterations
PTSTR pszTail = StrRChr(pszCurrent, pszCurrentEnd, TEXT('\\'));
if (pszTail)
phcctx->cchPrefix = (UINT)(pszTail - pszCurrent) + 1;
else
phcctx->cchPrefix = 0;
// For "\\" paths, we cannot cut off any of the first two slashes
if (phcctx->cchPrefix == 2 && IsDoubleSlashPath(pszCurrent))
phcctx->cchPrefix = 0;
phcctx->cchMax = cchCurrent;
}
else
{
// Or, just update cchPrefix
UINT i, j = 0, k = 0;
for (i = 0; i < cchCurrent && i < phcctx->cchPrefix; ++i)
{
if (pszCurrent[i] != pszPrev[i])
break;
if (pszCurrent[i] == TEXT('\\'))
{
j = i + 1;
++k;
}
}
// For "\\" paths, we cannot cut off any of the first two slashes
if (cchCurrent >= 2 && IsDoubleSlashPath(pszCurrent) && k < 3)
phcctx->cchPrefix = 0;
else
phcctx->cchPrefix = j;
}
if (cchCurrent && phcctx->hList)
{
// Finally, we can do the actual work that's needed!
if (GetFileAttributes(pszCurrent) & FILE_ATTRIBUTE_DIRECTORY)
{
if (cchCurrent < MAX_PATH_BUFFER - 2)
{
memcpy(phcctx->scratch.sz, pszCurrent, cbCurrent);
HashCalcWalkDirectory(phcctx, phcctx->scratch.sz, cchCurrent);
}
}
else
{
PHASHCALCITEM pItem = SLAddItem(phcctx->hList, NULL, sizeof(HASHCALCITEM) + cbCurrent);
if (pItem)
{
pItem->bValid = FALSE;
pItem->cchPath = cchCurrent;
memcpy(pItem->szPath, pszCurrent, cbCurrent);
if (phcctx->cchMax < cchCurrent)
phcctx->cchMax = cchCurrent;
++phcctx->cTotal;
}
}
}
if (phcctx->status == CANCEL_REQUESTED)
return;
pszPrev = pszCurrent;
}
}
VOID WINAPI HashCalcWalkDirectory( PHASHCALCCONTEXT phcctx, PTSTR pszPath, UINT cchPath )
{
HANDLE hFind;
WIN32_FIND_DATA finddata;
PTSTR pszPathAppend = pszPath + cchPath;
*pszPathAppend = TEXT('\\');
SSCpy2Ch(++pszPathAppend, TEXT('*'), 0);
if ((hFind = FindFirstFile(pszPath, &finddata)) == INVALID_HANDLE_VALUE)
return;
do
{
// Add 1 to the length since we are also going to count the slash that
// was added at the end of the directory
UINT cchLeaf = (UINT)SSLen(finddata.cFileName) + 1;
UINT cchNew = cchPath + cchLeaf;
if (phcctx->status == CANCEL_REQUESTED)
break;
if ( (!(finddata.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE)) &&
(cchNew < MAX_PATH_BUFFER - 2) )
{
SSChainNCpy(pszPathAppend, finddata.cFileName, cchLeaf);
if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// Directory: Recurse
if (!IsSpecialDirectoryName(finddata.cFileName))
HashCalcWalkDirectory(phcctx, pszPath, cchNew);
}
else
{
// File: Add to the list
UINT cbPathBuffer = (cchNew + 1) * sizeof(TCHAR);
PHASHCALCITEM pItem = SLAddItem(phcctx->hList, NULL, sizeof(HASHCALCITEM) + cbPathBuffer);
if (pItem)
{
pItem->bValid = FALSE;
pItem->cchPath = cchNew;
memcpy(pItem->szPath, pszPath, cbPathBuffer);
if (phcctx->cchMax < cchNew)
phcctx->cchMax = cchNew;
++phcctx->cTotal;
}
}
}
} while (FindNextFile(hFind, &finddata));
FindClose(hFind);
}
BOOL WINAPI IsSpecialDirectoryName( PCTSTR pszPath )
{
// TRUE if name is "." or ".."
#ifdef UNICODE
return(
(*((UPDWORD)pszPath) == WCHARS2DWORD(L'.', 0)) ||
(*((UPDWORD)pszPath) == WCHARS2DWORD(L'.', L'.') && pszPath[2] == 0)
);
#else
return(
(*((UPWORD)pszPath) == CHARS2WORD('.', 0)) ||
(*((UPWORD)pszPath) == CHARS2WORD('.', '.') && pszPath[2] == 0)
);
#endif
}
BOOL WINAPI IsDoubleSlashPath( PCTSTR pszPath )
{
// TRUE if string starts with "\\"
#ifdef UNICODE
return(*((UPDWORD)pszPath) == WCHARS2DWORD(L'\\', L'\\'));
#else
return(*((UPWORD)pszPath) == CHARS2WORD('\\', '\\'));
#endif
}
/*============================================================================*\
Save dialog
\*============================================================================*/
VOID WINAPI HashCalcInitSave( PHASHCALCCONTEXT phcctx )
{
HWND hWnd = phcctx->hWnd;
// We can use the extended portion of the scratch buffer for the file name
PTSTR pszFile = (PTSTR)phcctx->scratch.ext;
// Default result value
phcctx->hFileOut = INVALID_HANDLE_VALUE;
// Load settings
phcctx->opt.dwFlags = HCOF_FILTERINDEX | HCOF_SAVEENCODING;
OptionsLoad(&phcctx->opt);
// Initialize the struct for the first time, if needed
if (phcctx->ofn.lStructSize == 0)
{
phcctx->ofn.lStructSize = sizeof(phcctx->ofn);
phcctx->ofn.hwndOwner = hWnd;
phcctx->ofn.lpstrFilter = SAVE_FILTERS;
phcctx->ofn.nFilterIndex = phcctx->opt.dwFilterIndex;
phcctx->ofn.lpstrFile = pszFile;
phcctx->ofn.nMaxFile = MAX_PATH_BUFFER + 10;
phcctx->ofn.Flags = OFN_DONTADDTORECENT | OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
phcctx->ofn.lpstrDefExt = TEXT("");
// Set the initial file name
{
PTSTR pszOrigPath;
SLReset(phcctx->hListRaw);
pszOrigPath = SLGetDataAndStep(phcctx->hListRaw);
if (SLCheck(phcctx->hListRaw))
{
// Multiple items were selected in Explorer
SSChainNCpy2(
pszFile,
pszOrigPath, phcctx->cchPrefix,
SAVE_DEFAULT_NAME, countof(SAVE_DEFAULT_NAME)
);
}
else
{
// Only one item was selected in Explorer (may be a single
// file or a directory containing multiple files)
SSChainCpyCat(pszFile, pszOrigPath, SAVE_EXTS[phcctx->ofn.nFilterIndex]);
}
}
}
// We should also do a sanity check to make sure that the filter index
// is set to a valid value since we depend on that to determine the format
if ( GetSaveFileName(&phcctx->ofn) &&
phcctx->ofn.nFilterIndex &&
phcctx->ofn.nFilterIndex <= 5 )
{
BOOL bSuccess = FALSE;
// Save the filter in the user's preferences
if (phcctx->opt.dwFilterIndex != phcctx->ofn.nFilterIndex)
{
phcctx->opt.dwFilterIndex = phcctx->ofn.nFilterIndex;
phcctx->opt.dwFlags = HCOF_FILTERINDEX;
OptionsSave(&phcctx->opt);
}
// Extension fixup: Correct the extension to match the selected
// type, but only if the extension was one of the 5 in the list
if (phcctx->ofn.nFileExtension)
{
PTSTR pszExt = pszFile + phcctx->ofn.nFileExtension - 1;
if ( StrCmpI(pszExt, TEXT(".sfv")) == 0 ||
StrCmpI(pszExt, TEXT(".md4")) == 0 ||
StrCmpI(pszExt, TEXT(".md5")) == 0 ||
StrCmpI(pszExt, TEXT(".sha1")) == 0||
StrCmpI(pszExt, TEXT(".sha256")) == 0 )
{
if (StrCmpI(pszExt, SAVE_EXTS[phcctx->ofn.nFilterIndex]))
SSCpy(pszExt, SAVE_EXTS[phcctx->ofn.nFilterIndex]);
}
}
// Adjust the file paths for the output path, if necessary
HashCalcSetSavePrefix(phcctx, pszFile);
// Open the file for output
phcctx->hFileOut = CreateFile(
pszFile,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL
);
if (phcctx->hFileOut != INVALID_HANDLE_VALUE)
{
// The actual format will be set when HashCalcWriteResult is called
phcctx->szFormat[0] = 0;
if (phcctx->opt.dwSaveEncoding == 1)
{
// Write the BOM for UTF-16LE
WCHAR BOM = 0xFEFF;
DWORD cbWritten;
WriteFile(phcctx->hFileOut, &BOM, sizeof(WCHAR), &cbWritten, NULL);
}
}
else
{
TCHAR szMessage[MAX_STRINGMSG];
LoadString(g_hModThisDll, IDS_HC_SAVE_ERROR, szMessage, countof(szMessage));
MessageBox(hWnd, szMessage, NULL, MB_OK | MB_ICONERROR);
}
}
}
BOOL WINAPI HashCalcWriteResult( PHASHCALCCONTEXT phcctx, PHASHCALCITEM pItem )
{
PCTSTR pszHash;
PVOID pvLine;
INT cchLine, cbLine; // Length of line, in TCHARs or BYTEs, EXCLUDING the terminator
if (!pItem->bValid)
return(FALSE);
// Set szFormat is necessary
if (phcctx->szFormat[0] == 0)
{
// Did I ever mention that I hate SFV?
// The reason we tracked cchMax was because of this idiotic format
if (phcctx->ofn.nFilterIndex == 1)
{
wnsprintf(
phcctx->szFormat,
countof(phcctx->szFormat),
TEXT("%%-%ds %%s\r\n"),
phcctx->cchMax - phcctx->cchAdjusted
);
}
else
{
SSStaticCpy(phcctx->szFormat, TEXT("%s *%s\r\n"));
}
}
// Translate the filter index to a hash
switch (phcctx->ofn.nFilterIndex)
{
case 1: pszHash = pItem->results.szHexCRC32; break;
case 2: pszHash = pItem->results.szHexMD4; break;
case 3: pszHash = pItem->results.szHexMD5; break;
case 4: pszHash = pItem->results.szHexSHA1; break;
case 5: pszHash = pItem->results.szHexSHA256;break;
default: return(FALSE);
}
// Format the line
#define HashCalcFormat(a, b) wnsprintf(phcctx->scratch.sz, MAX_PATH_BUFFER, phcctx->szFormat, a, b)
cchLine = (phcctx->ofn.nFilterIndex == 1) ?
HashCalcFormat(pItem->szPath + phcctx->cchAdjusted, pszHash) : // SFV
HashCalcFormat(pszHash, pItem->szPath + phcctx->cchAdjusted); // everything else
#undef HashCalcFormat
if (cchLine > 0)
{
// Convert to the correct encoding
switch (phcctx->opt.dwSaveEncoding)
{
case 0:
{
// UTF-8
#ifdef UNICODE
cbLine = WStrToUTF8(phcctx->scratch.szW, phcctx->scratch.szA, countof(phcctx->scratch.szA)) - 1;
#else
AStrToWStr(phcctx->scratch.szA, phcctx->scratch.szW, countof(phcctx->scratch.szW));
cbLine = WStrToUTF8(phcctx->scratch.szW, phcctx->scratch.szA, countof(phcctx->scratch.szA)) - 1;
#endif
pvLine = phcctx->scratch.szA;
break;
}
case 1:
{
// UTF-16
#ifndef UNICODE
cchLine = AStrToWStr(phcctx->scratch.szA, phcctx->scratch.szW, countof(phcctx->scratch.szW)) - 1;
#endif
cbLine = cchLine * sizeof(WCHAR);
pvLine = phcctx->scratch.szW;
break;
}
case 2:
{
// ANSI
#ifdef UNICODE
cbLine = WStrToAStr(phcctx->scratch.szW, phcctx->scratch.szA, countof(phcctx->scratch.szA)) - 1;
#else
cbLine = cchLine;
#endif
pvLine = phcctx->scratch.szA;
break;
}
default: return(FALSE);
}
if (cbLine > 0)
{
INT cbWritten;
WriteFile(phcctx->hFileOut, pvLine, cbLine, &cbWritten, NULL);
if (cbLine != cbWritten) return(FALSE);
}
else return(FALSE);
}
else return(FALSE);
return(TRUE);
}
VOID WINAPI HashCalcSetSavePrefix( PHASHCALCCONTEXT phcctx, PTSTR pszSave )
{
// We have to be careful here about case sensitivity since we are now
// working with a user-provided path instead of a system-provided path...
// We want to build new paths without resorting to using "..", as that is
// ugly, fragile (often more so than absolute paths), and not to mention,
// complicated to calculate. This means that relative paths will be used
// only for paths within the same line of ancestry.
BOOL bMultiSel;
PTSTR pszOrig;
PTSTR pszTail;
// First, grab one of the original paths to work with
SLReset(phcctx->hListRaw);
pszOrig = SLGetDataAndStep(phcctx->hListRaw);
bMultiSel = SLCheck(phcctx->hListRaw);
// Unfortunately, we also have to contend with the possibility that one of
// these paths may be in short name format (e.g., if the user navigates to
// %TEMP% on a NT 5.x system)
{
// The scratch buffer's sz members are large enough for us
PTSTR pszOrigLong = (PTSTR)phcctx->scratch.szW;
PTSTR pszSaveLong = (PTSTR)phcctx->scratch.szA;
// Copy original path to scratch and terminate
pszTail = SSChainNCpy(pszOrigLong, pszOrig, phcctx->cchPrefix);
pszTail[0] = 0;
// Copy output path to scratch and terminate
pszTail = SSChainNCpy(pszSaveLong, pszSave, phcctx->ofn.nFileOffset);
pszTail[0] = 0;
// Normalize both paths to LFN
GetLongPathName(pszOrigLong, pszOrigLong, MAX_PATH_BUFFER);
GetLongPathName(pszSaveLong, pszSaveLong, MAX_PATH_BUFFER);
// We will only handle the case where they are the same, to prevent our
// re-prefixing from messing up the base behavior; it is not worth the
// trouble to account for LFN for all cases--just let it fall through
// to an absolute path.
if (StrCmpNI(pszOrigLong, pszSaveLong, MAX_PATH_BUFFER) == 0)
{
phcctx->cchAdjusted = phcctx->cchPrefix;
return;
}
}
if (pszTail = StrRChr(pszSave, NULL, TEXT('\\')))
{
phcctx->cchAdjusted = (UINT)(pszTail - pszSave) + 1;
if (phcctx->cchAdjusted <= phcctx->cchPrefix)
{
if (StrCmpNI(pszOrig, pszSave, phcctx->cchAdjusted) == 0)
{
// If the ouput prefix is the same as or a parent of the input
// prefix...
if (!(IsDoubleSlashPath(pszSave) && phcctx->cchAdjusted < 3))
return;
}
}
else if (!bMultiSel)
{
// We will make an exception for the case where the user selects
// a single directory from the Shell and then saves the output in
// that directory...
BOOL bEqual;
*pszTail = 0;
bEqual = StrCmpNI(pszOrig, pszSave, phcctx->cchAdjusted) == 0;
*pszTail = TEXT('\\');
if (bEqual) return;
}
}
// If we have reached this point, we need to use an absolute path
if ( pszSave[1] == TEXT(':') && phcctx->cchPrefix > 2 &&
StrCmpNI(pszOrig, pszSave, 2) == 0 )
{
// Omit drive letter
phcctx->cchAdjusted = 2;
}
else
{
// Full absolute path
phcctx->cchAdjusted = 0;
}
}
/*============================================================================*\
Progress bar
\*============================================================================*/
VOID WINAPI HashCalcTogglePrep( PHASHCALCCONTEXT phcctx, BOOL bState )
{
DWORD dwStyle = (DWORD)GetWindowLongPtr(phcctx->hWndPBTotal, GWL_STYLE);
if (bState)
{
dwStyle &= ~PBS_SMOOTH;
dwStyle |= PBS_MARQUEE;
phcctx->dwFlags |= HCF_MARQUEE;
}
else
{
dwStyle |= PBS_SMOOTH;
dwStyle &= ~PBS_MARQUEE;
phcctx->dwFlags &= ~HCF_MARQUEE;
}
SetWindowLongPtr(phcctx->hWndPBTotal, GWL_STYLE, dwStyle);
SendMessage(phcctx->hWndPBTotal, PBM_SETMARQUEE, bState, MARQUEE_INTERVAL);
if (!bState)
SendMessage(phcctx->hWndPBTotal, PBM_SETRANGE32, 0, phcctx->cTotal);
}