-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathpath_support.cpp
More file actions
968 lines (812 loc) · 33.5 KB
/
path_support.cpp
File metadata and controls
968 lines (812 loc) · 33.5 KB
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/***************************************************************************
Copyright 2004 Sebastian Ewert
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#include "resource.h"
#include "globals.h"
#include "CSyncQueue.h"
static VOID SetBasePath(lFILEINFO *fileList);
/*****************************************************************************
BOOL IsThisADirectory(CONST TCHAR szName[MAX_PATH_EX])
szName : (IN) string
Return Value:
returns TRUE if szName is a directory; FALSE otherwise
*****************************************************************************/
BOOL IsThisADirectory(CONST TCHAR szName[MAX_PATH_EX])
{
DWORD dwResult;
dwResult = GetFileAttributes(szName);
if(dwResult == 0xFFFFFFFF)
return FALSE;
else if(dwResult & FILE_ATTRIBUTE_DIRECTORY)
return TRUE;
else
return FALSE;
}
/*****************************************************************************
BOOL FileExists(CONST TCHAR szName[MAX_PATH_EX])
szName : (IN) string
Return Value:
returns TRUE if szName exists and is not a directory; FALSE otherwise
*****************************************************************************/
BOOL FileExists(CONST TCHAR szName[MAX_PATH_EX])
{
DWORD dwResult = GetFileAttributes(szName);
return (dwResult != INVALID_FILE_ATTRIBUTES && !(dwResult & FILE_ATTRIBUTE_DIRECTORY));
}
/*****************************************************************************
DWORD GetFileSizeQW(CONST TCHAR * szFilename, QWORD * qwSize)
szFilename : (IN) filename of the file whose filesize we want
qwSize : (OUT) filesize as QWORD
Return Value:
returns NOERROR if everything went fine. Otherwise GetLastError() is returned
Notes:
- gets size as QWORD from a string
- is also used to validate the string as a legal, openable file (f.e. if
szFilename is some blabla GetLastError() will return 2 => file not found)
*****************************************************************************/
VOID SetFileinfoAttributes(FILEINFO *pfileInfo)
{
BOOL bResult;
WIN32_FILE_ATTRIBUTE_DATA fileAttributeData;
ZeroMemory(&fileAttributeData, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
bResult = GetFileAttributesEx(pfileInfo->szFilename, GetFileExInfoStandard, &fileAttributeData);
if(!bResult) {
pfileInfo->dwError = GetLastError();
return;
}
pfileInfo->qwFilesize = MAKEQWORD(fileAttributeData.nFileSizeHigh, fileAttributeData.nFileSizeLow);
pfileInfo->ftModificationTime = fileAttributeData.ftLastWriteTime;
return;
}
/*****************************************************************************
BOOL GenerateNewFilename(TCHAR szFilenameNew[MAX_PATH_EX], CONST TCHAR szFilenameOld[MAX_PATH_EX],
DWORD dwCrc32, CONST TCHAR szFilenamePattern[MAX_PATH_EX])
szFilenameNew : (OUT) String receiving the new generated filename
(assumed to be of length MAX_LENGTH)
szFilenameOld : (IN) Input string from which the result is generated
dwCrc32 : (IN) CRC as DWORD, which is inserted into szFilenameOld
szFilenamePattern : (IN) specifies the way the new filename is created
Return Value:
returns TRUE
Notes:
- Parts szFilenameOld into path, filename and extension
- then the new filename is path\szFilenamePattern where the variables in szFilenamePattern
are replaced with filename, extension and CRC
*****************************************************************************/
BOOL GenerateNewFilename(TCHAR szFilenameNew[MAX_PATH_EX], CONST TCHAR szFilenameOld[MAX_PATH_EX],
CONST TCHAR *szHash, CONST TCHAR szFilenamePattern[MAX_PATH_EX])
{
size_t size_temp, stIndex;
//TCHAR szStringCrc[15];
TCHAR szPath[MAX_PATH_EX], szFilename[MAX_PATH_EX], szFileext[MAX_PATH_EX];
// fill the substrings for later use in the pattern string
SeparatePathFilenameExt(szFilenameOld, szPath, szFilename, szFileext);
//StringCchPrintf(szStringCrc, 15, TEXT("%08X"), dwCrc32 );
StringCchCopy(szFilenameNew, MAX_PATH_EX, szPath);
StringCchCat(szFilenameNew, MAX_PATH_EX, TEXT("\\"));
//no we parse the pattern string provided by the user and generate szFilenameNew
StringCchLength(szFilenamePattern, MAX_PATH_EX, & size_temp);
stIndex = 0;
while(stIndex < size_temp){
if(IsStringPrefix(TEXT("%FILENAME"), szFilenamePattern + stIndex)){
StringCchCat(szFilenameNew, MAX_PATH_EX, szFilename);
stIndex += 9; //length of "%FILENAME"
}
else if(IsStringPrefix(TEXT("%FILEEXT"), szFilenamePattern + stIndex)){
StringCchCat(szFilenameNew, MAX_PATH_EX, szFileext);
stIndex += 8;
// if empty extension and previous pos is dot remove it (invalid file name)
if(*szFileext == _T('\0')) {
int lastpos = lstrlen(szFilenameNew) - 1;
if(szFilenameNew[lastpos] == _T('.'))
szFilenameNew[lastpos] = _T('\0');
}
}
else if(IsStringPrefix(TEXT("%CRC"), szFilenamePattern + stIndex)){
StringCchCat(szFilenameNew, MAX_PATH_EX, szHash);
stIndex += 4;
}
else{
// if no replacement has to taken place we just copy the current char and
// proceed forward
StringCchCatN(szFilenameNew, MAX_PATH_EX, szFilenamePattern + stIndex, 1);
stIndex++;
}
}
return TRUE;
}
/*****************************************************************************
BOOL SeparatePathFilenameExt(CONST TCHAR szCompleteFilename[MAX_PATH_EX],
TCHAR szPath[MAX_PATH_EX], TCHAR szFilename[MAX_PATH_EX], TCHAR szFileext[MAX_PATH_EX])
szFilename : (IN) String to be seperated
szPath : (OUT) Path
szFilename : (OUT) Filename
szFileext : (OUT) File Extension
Return Value:
return TRUE
Notes:
- szFileext without the '.'
- szFilename without starting '\'
- szPath without trailing '\'
- looks for a '.' between the last character and last '\'. That is szFileext.
Rest is szFilename and szPath. If no '.' is found szFileext is empty
*****************************************************************************/
BOOL SeparatePathFilenameExt(CONST TCHAR szCompleteFilename[MAX_PATH_EX],
TCHAR szPath[MAX_PATH_EX], TCHAR szFilename[MAX_PATH_EX], TCHAR szFileext[MAX_PATH_EX])
{
size_t size_temp;
TCHAR szFilenameTemp[MAX_PATH_EX];
BOOL bExtensionFound;
StringCchLength(szCompleteFilename, MAX_PATH_EX, & size_temp);
StringCchCopy(szFilenameTemp, MAX_PATH_EX, szCompleteFilename);
StringCchCopy(szPath, MAX_PATH_EX, TEXT(""));
StringCchCopy(szFilename, MAX_PATH_EX, TEXT(""));
StringCchCopy(szFileext, MAX_PATH_EX, TEXT(""));
bExtensionFound = FALSE;
for(UINT iIndex = (UINT) size_temp; iIndex > 0; --iIndex){
if( !bExtensionFound && (szFilenameTemp[iIndex] == TEXT('.')) ){
StringCchCopy(szFileext, MAX_PATH_EX, szFilenameTemp + iIndex + 1);
szFilenameTemp[iIndex] = TEXT('\0');
bExtensionFound = TRUE;
}
if(szFilenameTemp[iIndex] == TEXT('\\') ){
StringCchCopy(szFilename, MAX_PATH_EX, szFilenameTemp + iIndex + 1);
szFilenameTemp[iIndex] = TEXT('\0');
StringCchCopy(szPath, MAX_PATH_EX, szFilenameTemp);
break;
}
}
return TRUE;
}
/*****************************************************************************
INT ReduceToPath(TCHAR szString[MAX_PATH_EX])
szString : (IN/OUT) assumed to be a string containing a filepath
Return Value:
returns the new length of the string
Notes:
- looks for a '\' in the string from back to front; if one is found the next
char is set to \0. Otherwise the whole string is set to TEXT("")
- used to set g_szBasePath
*****************************************************************************/
INT ReduceToPath(TCHAR szString[MAX_PATH_EX])
{
size_t stStringLength;
INT iStringLength;
StringCchLength(szString, MAX_PATH_EX, & stStringLength);
iStringLength = (INT) stStringLength;
while( (iStringLength > 0) && (szString[iStringLength] != TEXT('\\')) )
iStringLength--;
if(iStringLength != 0) // we want a \ at the end
iStringLength++;
szString[iStringLength] = TEXT('\0');
return iStringLength;
}
/*****************************************************************************
TCHAR * GetFilenameWithoutPathPointer(TCHAR szFilenameLong[MAX_PATH_EX])
szFilenameLong : (IN) a filename including path
Return Value:
returns a pointer to the filepart of the string
Notes:
- filepart if everything after the last '\' in the string. If no '\' is found,
we return the original pointer
*****************************************************************************/
CONST TCHAR * GetFilenameWithoutPathPointer(CONST TCHAR szFilenameLong[MAX_PATH_EX])
{
size_t size_temp;
INT iIndex;
StringCchLength(szFilenameLong, MAX_PATH_EX, &size_temp);
for(iIndex=((INT)size_temp - 1); iIndex >= 0; --iIndex){
if(szFilenameLong[iIndex] == TEXT('\\'))
break;
}
// if Backslash is found we want the character right beside it as the first character
// in the short string.
// if no backslash is found the last index is -1. In this case we also have to add +1 to get
// the original string as the short string
++iIndex;
return szFilenameLong + iIndex;
}
/*****************************************************************************
BOOL HasFileExtension(CONST TCHAR szFilename[MAX_PATH_EX], CONST TCHAR * szExtension)
szName : (IN) filename string
szExtension : (IN) extension we want to look for
Return Value:
returns TRUE if szFilename has the extension
*****************************************************************************/
BOOL HasFileExtension(CONST TCHAR szFilename[MAX_PATH_EX], CONST TCHAR *szExtension)
{
size_t stString;
size_t stExtension;
StringCchLength(szFilename, MAX_PATH_EX, & stString);
StringCchLength(szExtension, MAX_PATH_EX, & stExtension);
// we compare the last characters
if( (stString > stExtension) && (lstrcmpi(szFilename + stString - stExtension, szExtension) == 0) )
return TRUE;
else
return FALSE;
}
/*****************************************************************************
BOOL GetHashFromStream(TCHAR CONST *szFileName, BYTE * abResult, UINT uiHashType)
szFileName : (IN) string; assumed to be the szFilename member of the
FILEINFO struct
abResult : (IN, OUT) return value is TRUE if the bytes this parameter
points to are set to the HASH byte values
uiHashType : (IN) hash type to look for
Return Value:
returns TRUE if a HASH was found in the :<HASHNAME> stream. Otherwise FALSE
*****************************************************************************/
BOOL GetHashFromStream(TCHAR CONST *szFileName, BYTE * abResult, UINT uiHashType)
{
HANDLE hFile;
DWORD NumberOfBytesRead;
UINT hashLength = g_hash_type_infos[uiHashType].hash_length;
UINT hashLengthChars = hashLength * 2;
CString szStream(szFileName);
szStream.AppendFormat(TEXT(":%s"), g_hash_type_infos[uiHashType].hash_name);
std::vector<CHAR> charsHashAnsi(hashLengthChars);
std::vector<TCHAR> charsHash(hashLengthChars);
hFile = CreateFile(szStream, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN , 0);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if(!ReadFile(hFile, charsHashAnsi.data(), hashLengthChars, &NumberOfBytesRead, NULL)) {
CloseHandle(hFile);
return FALSE;
}
// since we read individual bytes, we have to transfer them into a TCHAR array (for HexToDword)
UnicodeFromAnsi(charsHash.data(), hashLengthChars, charsHashAnsi.data());
CloseHandle(hFile);
for (UINT i = 0; i < hashLength; i++)
{
*(abResult + i) = (BYTE)HexToDword(&charsHash[i * 2], 2);
}
return TRUE;
}
/*****************************************************************************
BOOL GetHashFromStreams(FILEINFO *fileInfo)
fileInfo : (IN, OUT) fileInfo to check for hashes in streams,
found hashes will be stored in the corresponding
hashinfo
Return Value:
returns TRUE if a HASH was found in any stream. Otherwise FALSE
*****************************************************************************/
BOOL GetHashFromStreams(FILEINFO *fileInfo)
{
BOOL found = FALSE;
WIN32_FIND_STREAM_DATA streamData;
HANDLE searchHandle = FindFirstStreamW(fileInfo->szFilename, FindStreamInfoStandard, &streamData, 0);
if (searchHandle != INVALID_HANDLE_VALUE)
{
// first stream is always ::$DATA, we can skip this
while(FindNextStreamW(searchHandle, &streamData))
{
CString streamname(streamData.cStreamName);
int start = 1;
streamname = streamname.Tokenize(TEXT(":"), start);
int uiHashType = -1;
for (int i = 0; i < NUM_HASH_TYPES; i++)
{
// check if the stream name corresponds to a hash type
if (streamname.Compare(g_hash_type_infos[i].hash_name) == 0 && (streamData.StreamSize.QuadPart >= g_hash_type_infos[i].hash_length * 2))
{
uiHashType = i;
break;
}
}
if (uiHashType >= 0)
{
BOOL ret = GetHashFromStream(fileInfo->szFilename, (BYTE*)(&fileInfo->hashInfo[uiHashType].f), uiHashType);
if (ret)
{
found = TRUE;
fileInfo->hashInfo[uiHashType].dwFound = HASH_FOUND_STREAM;
}
// need to reverse bytes for crc types (DWORDS are low byte before high byte)
if (uiHashType == HASH_TYPE_CRC32 || uiHashType == HASH_TYPE_CRC32C)
{
DWORD reversedCrc = 0;
BYTE *crc = (BYTE*)(&fileInfo->hashInfo[uiHashType].f);
for (int i = 0; i < 4; i++)
{
*((BYTE *)&reversedCrc + i) = crc[3 - i];
}
fileInfo->hashInfo[uiHashType].f.dwCrc32Found = reversedCrc; // f is union, no special case for crc32c needed
}
}
}
FindClose(searchHandle);
}
return found;
}
/*****************************************************************************
BOOL GetHashFromFilename(FILEINFO *fileInfo)
fileInfo : (IN/OUT) pointer to the FILEINFO struct to be checked
Return Value:
returns TRUE if a hash was found in the filename. Otherwise FALSE
Notes:
- walks through a filename from the rear to the front.
- checks for legal Hex characters in between delimiters, or anywhere,
depending on settings (via IsLegalHexSymbol)
*****************************************************************************/
BOOL GetHashFromFilename(FILEINFO *fileInfo)
{
size_t StringSize;
INT iIndex;
BOOL bFound;
TCHAR const *szFileWithoutPath;
TCHAR const *szHashStart;
UINT uiFoundHexSymbols;
int iHashIndex;
szFileWithoutPath = GetFilenameWithoutPathPointer(fileInfo->szFilename);
if(FAILED(StringCchLength(szFileWithoutPath, MAX_PATH_EX, &StringSize)))
return FALSE;
if(StringSize == 0)
return FALSE;
iIndex = (int)StringSize;
bFound = FALSE;
do{
--iIndex;
uiFoundHexSymbols = 0;
if(g_program_options.bAllowCrcAnywhere) {
if(IsLegalHexSymbol(szFileWithoutPath[iIndex]) )
{
if ((iIndex - 7) < 0)
break;
else {
int i;
for(i = 0; iIndex - i >= 0 && IsLegalHexSymbol(szFileWithoutPath[iIndex-i]); ++i)
uiFoundHexSymbols++;
iIndex -= i - 1;
}
}
} else {
if(IsValidCRCDelim(szFileWithoutPath[iIndex]) )
{
if ((iIndex - 9) < 0)
break;
else {
bool valid = false;
int i;
for(i = 1; iIndex - i >= 0; ++i) {
if(IsLegalHexSymbol(szFileWithoutPath[iIndex-i])) {
uiFoundHexSymbols++;
} else {
if(IsValidCRCDelim(szFileWithoutPath[iIndex-i]))
valid = true;
break;
}
}
if(!valid)
uiFoundHexSymbols = 0;
else
i -= 1; // leave delim, might be the start of the next possible match
iIndex -= i;
}
}
}
if(uiFoundHexSymbols) {
for(int i = 0; i < HASH_TYPE_SHA3_224; i++) {
// md5/ed2k have same size, but md5 will hit first
if(uiFoundHexSymbols == g_hash_type_infos[i].hash_length * 2) {
bFound = TRUE;
iHashIndex = i;
break;
}
}
}
}
while((iIndex > 0) && (!bFound));
if(!bFound)
return FALSE;
szHashStart = szFileWithoutPath + iIndex;
fileInfo->hashInfo[iHashIndex].dwFound = HASH_FOUND_FILENAME;
if(iHashIndex == HASH_TYPE_CRC32) {
fileInfo->hashInfo[HASH_TYPE_CRC32].f.dwCrc32Found = HexToDword(szHashStart, 8);
} else {
for(UINT uiIndex=0; uiIndex < g_hash_type_infos[iHashIndex].hash_length; ++uiIndex)
*((BYTE *)&fileInfo->hashInfo[iHashIndex].f + uiIndex) = (BYTE)HexToDword(szHashStart + uiIndex * 2, 2);
}
return TRUE;
}
/*****************************************************************************
VOID PostProcessList(CONST HWND arrHwnd[ID_NUM_WINDOWS],
SHOWRESULT_PARAMS * pshowresult_params,
lFILEINFO *fileList)
arrHwnd : (IN)
pshowresult_params : (IN/OUT) struct for ShowResult
fileList : (IN/OUT) pointer to the job structure that should be processed
Return Value:
returns nothing
Notes:
1.) disables buttons that should not be active while processing the list (if not already disabled)
2.) sets the lists g_szBasePath that specifies the directory that we use as a base
3.) calls ProcessDirectories to expand all directories and get the files inside
4.) if we have an .sfv file we process the .sfv file
5.) call ProcessFileProperties to get file properties like size etc...
6.) eventually sort the list (this seems pointless, will check in a later version)
7.) sets bDoCalculate[] of the job structure depending on in which
program mode we are and or if we want those by default. These values are passed by the caller to THREAD_CALC
*****************************************************************************/
VOID PostProcessList(CONST HWND arrHwnd[ID_NUM_WINDOWS],
SHOWRESULT_PARAMS * pshowresult_params,
lFILEINFO *fileList)
{
if(SyncQueue.bThreadDone) {
SetWindowText(arrHwnd[ID_EDIT_STATUS], TEXT("Getting Fileinfo..."));
EnableWindowsForThread(arrHwnd, FALSE);
ShowResult(arrHwnd, NULL, pshowresult_params);
}
MakePathsAbsolute(fileList);
// enter normal mode
fileList->uiRapidCrcMode = MODE_NORMAL;
if(!fileList->fInfos.empty()) {
UINT detectedMode = MODE_NORMAL;
if(fileList->uiCmdOpts == CMD_FORCE_BSD)
detectedMode = MODE_BSD;
else if (fileList->uiCmdOpts == CMD_NORMAL ||
fileList->uiCmdOpts == CMD_REPARENT ||
fileList->uiCmdOpts == CMD_ALLHASHES) {
detectedMode = DetermineHashType(fileList->fInfos.front().szFilename);
}
if(detectedMode != MODE_NORMAL) {
EnterHashMode(fileList, detectedMode);
} else {
SetBasePath(fileList);
ProcessDirectories(fileList, arrHwnd[ID_EDIT_STATUS]);
}
}
if(SyncQueue.bThreadDone) {
SetWindowText(arrHwnd[ID_EDIT_STATUS], TEXT("Getting File Sizes..."));
}
ProcessFileProperties(fileList);
if(fileList->uiRapidCrcMode != MODE_BSD) {
for(int i=0;i<NUM_HASH_TYPES;i++) {
if(fileList->uiRapidCrcMode == MODE_NORMAL)
fileList->bDoCalculate[i] = g_program_options.bCalcPerDefault[i] ? true : false;
else
fileList->bDoCalculate[i] |= (fileList->uiRapidCrcMode == i);
}
}
if (fileList->uiCmdOpts >= CMD_NTFS) {
fileList->bDoCalculate[fileList->uiCmdOpts - CMD_NTFS] = true;
}
else if (fileList->uiCmdOpts >= CMD_NAME) {
fileList->bDoCalculate[fileList->uiCmdOpts - CMD_NAME] = true;
}
else if (fileList->uiCmdOpts < CMD_NORMAL) {
fileList->bDoCalculate[fileList->uiCmdOpts] = true;
}
if(g_program_options.bSortList)
QuickSortList(fileList);
if(SyncQueue.bThreadDone) {
EnableWindowsForThread(arrHwnd, TRUE);
SetWindowText(arrHwnd[ID_EDIT_STATUS], TEXT("Getting Fileinfo done..."));
}
return;
}
/*****************************************************************************
VOID ProcessDirectories(lFILEINFO *fileList)
fileList : (IN/OUT) pointer to the job structure that should be processed
Return Value:
returns nothing
Notes:
- essentially gives some additional code to call ExpandDirectory correctly
*****************************************************************************/
VOID ProcessDirectories(lFILEINFO *fileList, CONST HWND hwndStatus, BOOL bOnlyHashFiles)
{
TCHAR szCurrentPath[MAX_PATH_EX];
TCHAR szStatusDisplay[MAX_PATH_EX];
// save org. path
GetCurrentDirectory(MAX_PATH_EX, szCurrentPath);
for(list<FILEINFO>::iterator it=fileList->fInfos.begin();it!=fileList->fInfos.end();) {
if(IsThisADirectory((*it).szFilename)) {
if(SyncQueue.bThreadDone) {
StringCchPrintf(szStatusDisplay, MAX_PATH_EX, TEXT("Getting Fileinfo... %s"), (*it).szFilename);
SetWindowText(hwndStatus, szStatusDisplay);
}
it = ExpandDirectory(&fileList->fInfos, it, bOnlyHashFiles);
} else {
// check to see if the current file-extension matches our exclude string
// if so, we remove it from the list
if(bOnlyHashFiles && !CheckHashFileMatch((*it).szFilename) ||
!bOnlyHashFiles && CheckExcludeStringMatch((*it).szFilename)) {
it = fileList->fInfos.erase(it);
}
else
it++;
}
}
// restore org. path
SetCurrentDirectory(szCurrentPath);
return;
}
/*****************************************************************************
list<FILEINFO>::iterator ExpandDirectory(list<FILEINFO> *fList,list<FILEINFO>::iterator it)
fList : (IN/OUT) pointer to the FILEINFO list we are working on
it : (IN) iterator to the directory item we want to expand
Return Value:
"it" is expanded; that means is replaced with the files
(or subdirectories) in that directory (if there are any). Because it is deleted we
return an iterator to the first item that replaced it, so that the calling function
knows where to go on in the list
Notes:
- expands the item at "it"
*****************************************************************************/
list<FILEINFO>::iterator ExpandDirectory(list<FILEINFO> *fList,list<FILEINFO>::iterator it, BOOL bOnlyHashFiles)
{
HANDLE hFileSearch;
WIN32_FIND_DATA findFileData;
FILEINFO fileinfoTmp = {0};
fileinfoTmp.parentList=(*it).parentList;
UINT insertCount=0;
TCHAR savedPath[MAX_PATH_EX];
StringCchCopy(savedPath,MAX_PATH_EX,(*it).szFilename);
it = fList->erase(it);
// start to find files and directories in this directory
if(savedPath[lstrlen(savedPath) - 1] == TEXT('\\'))
savedPath[lstrlen(savedPath) - 1] = TEXT('\0');
StringCchCat(savedPath,MAX_PATH_EX,TEXT("\\*"));
hFileSearch = FindFirstFile(savedPath, & findFileData);
savedPath[lstrlen(savedPath) - 2] = TEXT('\0');
if(hFileSearch == INVALID_HANDLE_VALUE){
return it;
}
do{
if( (lstrcmpi(findFileData.cFileName, TEXT(".")) != 0) && (lstrcmpi(findFileData.cFileName, TEXT("..")) != 0) ){
fileinfoTmp.szFilename.Format(TEXT("%s\\%s"),savedPath,findFileData.cFileName);
// now create a new item and increase the count of inserted items
// also check include/exclude here to avoid unnecessary fileinfo copies
if(IsThisADirectory(fileinfoTmp.szFilename) ||
bOnlyHashFiles && CheckHashFileMatch(fileinfoTmp.szFilename) ||
!bOnlyHashFiles && !CheckExcludeStringMatch(fileinfoTmp.szFilename)) {
fList->insert(it,fileinfoTmp);
insertCount++;
}
}
}while(FindNextFile(hFileSearch, & findFileData));
//we want to return an iterator to the first inserted item, so we hop back the number of inserted items
for(UINT i=0;i<insertCount;i++)
it--;
// stop the search
FindClose(hFileSearch);
return it;
}
/*****************************************************************************
VOID ProcessFileProperties(lFILEINFO *fileList)
fileList : (IN/OUT) pointer to the job structure whose files are to be processed
Return Value:
returns nothing
Notes:
- sets file information like filesize, CrcFromFilename, Long Pathname, szFilenameShort
*****************************************************************************/
VOID ProcessFileProperties(lFILEINFO *fileList)
{
size_t stString;
StringCchLength(fileList->g_szBasePath, MAX_PATH_EX, & stString);
fileList->qwFilesizeSum = 0;
for(list<FILEINFO>::iterator it=fileList->fInfos.begin();it!=fileList->fInfos.end();it++) {
LPCTSTR szFn = (*it).szFilename;
if(stString && !StrCmpN(szFn, fileList->g_szBasePath, stString)) {
(*it).szFilenameShort = szFn + stString;
} else
(*it).szFilenameShort = szFn + 4;
if(!IsApplDefError((*it).dwError)){
SetFileinfoAttributes(&(*it));
if ((*it).dwError == NO_ERROR){
fileList->qwFilesizeSum += (*it).qwFilesize;
if (fileList->uiRapidCrcMode == MODE_NORMAL)
{
if (!GetHashFromFilename(&(*it)))
{
GetHashFromStreams(&(*it));
}
}
}
}
}
return;
}
/*****************************************************************************
VOID MakePathsAbsolute(lFILEINFO *fileList)
fileList : (IN/OUT) pointer to the job structure whose files are to be processed
Return Value:
returns nothing
Notes:
- walks through the list and makes sure that all paths are absolute.
- it distinguishes between paths like c:\..., \... and ...
*****************************************************************************/
VOID MakePathsAbsolute(lFILEINFO *fileList)
{
TCHAR szFilenameTemp[MAX_PATH_EX];
TCHAR szFilenameTempExtended[MAX_PATH_EX];
TCHAR *szFilenameStart;
for(list<FILEINFO>::iterator it=fileList->fInfos.begin();it!=fileList->fInfos.end();it++) {
if((*it).szFilename.Left(4)==TEXT("\\\\?\\"))
continue;
(*it).szFilename.Replace(TEXT('/'), TEXT('\\'));
GetFullPathName((*it).szFilename, MAX_PATH_EX, szFilenameTemp, NULL);
szFilenameStart = szFilenameTemp;
StringCchCopy(szFilenameTempExtended, MAX_PATH_EX, TEXT("\\\\?\\"));
if(lstrlen(szFilenameTemp) && szFilenameTemp[0]== TEXT('\\') && szFilenameTemp[1]== TEXT('\\')) {
szFilenameStart += 2;
StringCchCat(szFilenameTempExtended, MAX_PATH_EX, TEXT("UNC\\"));
}
StringCchCat(szFilenameTempExtended, MAX_PATH_EX, szFilenameStart);
GetLongPathName(szFilenameTempExtended, szFilenameTempExtended, MAX_PATH_EX);
(*it).szFilename = szFilenameTempExtended;
}
return;
}
/*****************************************************************************
static VOID SetBasePath(lFILEINFO *fileList)
fileList : (IN/OUT) pointer to the job structure whose files are to be processed
Return Value:
returns nothing
Notes:
- sets fileLists g_szBasePath based on the first file
- if this is not a prefix (usually only via cmd line), we set g_szBasePath to \\?\
*****************************************************************************/
static VOID SetBasePath(lFILEINFO *fileList)
{
BOOL bIsPraefixForAll;
StringCchCopy(fileList->g_szBasePath, MAX_PATH_EX, fileList->fInfos.front().szFilename);
ReduceToPath(fileList->g_szBasePath);
GetLongPathName(fileList->g_szBasePath, fileList->g_szBasePath, MAX_PATH_EX);
bIsPraefixForAll = TRUE;
for(list<FILEINFO>::iterator it=fileList->fInfos.begin();it!=fileList->fInfos.end();it++) {
if(!IsStringPrefix(fileList->g_szBasePath, (*it).szFilename))
bIsPraefixForAll = FALSE;
}
if(!bIsPraefixForAll || !IsThisADirectory(fileList->g_szBasePath))
StringCchCopy(fileList->g_szBasePath, MAX_PATH_EX, TEXT("\\\\?\\"));
return;
}
/*****************************************************************************
UINT FindCommonPrefix(list<FILEINFO *> *fileInfoList)
fileInfoList : (IN/OUT) pointer to a list of FILEINFOs
Return Value:
returns the count of characters that are equal for all items' szFilename
Notes:
- iterates fileInfoList and checks for a common prefix
*****************************************************************************/
UINT FindCommonPrefix(list<FILEINFO *> *fileInfoList)
{
size_t countSameChars=MAXUINT;
FILEINFO* firstFileInfoPointer;
TCHAR *firstBasePathPointer;
bool sameBaseDir = true;
if(fileInfoList->empty())
return 0;
firstFileInfoPointer = fileInfoList->front();
firstBasePathPointer = firstFileInfoPointer->parentList->g_szBasePath;
for(list<FILEINFO*>::iterator it=fileInfoList->begin();it!=fileInfoList->end();it++) {
UINT i=0;
if(sameBaseDir && lstrcmp(firstBasePathPointer,(*it)->parentList->g_szBasePath))
sameBaseDir = false;
while(i<countSameChars && (*it)->szFilename[i]!=TEXT('\0') && firstFileInfoPointer->szFilename[i]!=TEXT('\0')) {
if((*it)->szFilename[i]!=firstFileInfoPointer->szFilename[i])
countSameChars = i;
i++;
}
if(countSameChars<3) return 0;
}
if(sameBaseDir) {
if(lstrlen(firstBasePathPointer) > 4)
StringCchLength(firstBasePathPointer,MAX_PATH_EX,&countSameChars);
else
countSameChars = 0;
}
while( (countSameChars > 0) && (fileInfoList->front()->szFilename[(int)countSameChars - 1] != TEXT('\\')) )
countSameChars--;
return (UINT)countSameChars;
}
/*****************************************************************************
UINT DetermineHashType(const CString &filename)
filename : (IN) filename in question
Return Value:
returns the type of hashfile this file is likely to be
Notes:
- does not examine the contents, only guesses from the filename
*****************************************************************************/
UINT DetermineHashType(const CString &filename)
{
TCHAR *szExtension;
szExtension = PathFindExtension(filename) + 1;
for(int i=0;i<NUM_HASH_TYPES;i++) {
if(lstrcmpi(g_hash_type_infos[i].hash_ext, szExtension)==0)
return MODE_SFV + i;
}
if(HasFileExtension(filename, TEXT(".bsdhash"))) {
return MODE_BSD;
}
if(!g_program_options.bHashtypeFromFilename)
return MODE_NORMAL;
CString filenameOnly = filename.Mid(filename.ReverseFind(TEXT('\\')) + 1);
CString ext;
int dot = filenameOnly.ReverseFind(TEXT('.'));
if(dot != -1) {
ext = filenameOnly.Mid(dot + 1);
filenameOnly = filenameOnly.Left(dot);
}
for(int i = 0; i < NUM_HASH_TYPES; i++) {
if(!filenameOnly.CompareNoCase(CString(g_hash_type_infos[i].hash_name) + TEXT("SUM")) ||
!filenameOnly.CompareNoCase(CString(g_hash_type_infos[i].hash_name) + TEXT("SUMS")) ||
!filenameOnly.CompareNoCase(CString(g_hash_type_infos[i].hash_name) + TEXT("CHECKSUM")) ||
!filenameOnly.CompareNoCase(CString(g_hash_type_infos[i].hash_name) + TEXT("CHECKSUMS")) ||
!filenameOnly.CompareNoCase(CString(g_hash_type_infos[i].hash_name) + TEXT("HASH")) ||
!filenameOnly.CompareNoCase(CString(g_hash_type_infos[i].hash_name) + TEXT("HASHES")))
{
if(ext.IsEmpty() || !ext.CompareNoCase(TEXT("txt")))
return i;
}
if(!filenameOnly.CompareNoCase(g_hash_type_infos[i].hash_name)) {
if(ext.IsEmpty() || !ext.CompareNoCase(TEXT("txt")))
return MODE_BSD;
}
}
if(filenameOnly.Find(TEXT("_hashes")) >= 0)
return MODE_BSD;
return MODE_NORMAL;
}
/*****************************************************************************
BOOL ConstructCompleteFilename(CString &filename, TCHAR *szBasePath, const TCHAR *szRelFilename)
filename : (OUT) complete filename
szBasePath : (IN) base path used to construct complete filenames
szRelFilename : (IN) (possibly) relative filename
Return Value:
FALSE if path was already absolute
Notes:
- constructs an absolute extended-length filename out of szBasePath and szRelFilename, removing
relative path elements
*****************************************************************************/
BOOL ConstructCompleteFilename(CString &filename, const TCHAR *szBasePath, const TCHAR *szRelFilename)
{
BOOL bMadeAbsolute = FALSE;
TCHAR szCanonicalizedName[MAX_PATH_EX];
if(!_tcsncmp(szRelFilename, TEXT("\\\\?"), 3)) {
// already extended-length
filename = szRelFilename;
} else if(PathIsRelative(szRelFilename)) {
// relative, make absolute with base path
filename.Format(TEXT("%s%s"), szBasePath, szRelFilename);
bMadeAbsolute = TRUE;
} else if(!_tcsncmp(szRelFilename, TEXT("\\\\"), 2)) {
// absolute unc, make extended-length with UNC
filename.Format(TEXT("\\\\?\\UNC\\%s"), szRelFilename + 2);
} else {
// absolute regular, make extended-length
filename.Format(TEXT("\\\\?\\%s"), szRelFilename);
}
// use GetFullPathName to do the canonicalization, PathCchCanonicalizeEx is only available on win8+
// the current directory problems of GetFullPathName do not apply here since we already have a full
// path and do not mess with the current directory at all
GetFullPathName(filename, MAX_PATH_EX, szCanonicalizedName, NULL);
filename = szCanonicalizedName;
return bMadeAbsolute;
}
BOOL RegularFromLongFilename(TCHAR szRegularFilename[MAX_PATH], const TCHAR *szLongFilename)
{
if(!_tcsncmp(szLongFilename, TEXT("\\\\?\\UNC\\"), 8)) {
szRegularFilename[0] = TEXT('\\');
StringCchCopy(szRegularFilename + 1, MAX_PATH_EX - 1, szLongFilename + 7);
return TRUE;
} else if(!_tcsncmp(szLongFilename, TEXT("\\\\?\\"), 4)) {
StringCchCopy(szRegularFilename, MAX_PATH_EX, szLongFilename + 4);
return TRUE;
} else {
StringCchCopy(szRegularFilename, MAX_PATH_EX, szLongFilename);
return FALSE;
}
}