1 // HashCalc.h
2 
3 #ifndef __HASH_CALC_H
4 #define __HASH_CALC_H
5 
6 #include "../../../Common/Wildcard.h"
7 
8 #include "../../Common/CreateCoder.h"
9 #include "../../Common/MethodProps.h"
10 
11 #include "Property.h"
12 
13 const unsigned k_HashCalc_DigestSize_Max = 64;
14 
15 const unsigned k_HashCalc_NumGroups = 4;
16 
17 enum
18 {
19   k_HashCalc_Index_Current,
20   k_HashCalc_Index_DataSum,
21   k_HashCalc_Index_NamesSum,
22   k_HashCalc_Index_StreamsSum
23 };
24 
25 struct CHasherState
26 {
27   CMyComPtr<IHasher> Hasher;
28   UString Name;
29   UInt32 DigestSize;
30   Byte Digests[k_HashCalc_NumGroups][k_HashCalc_DigestSize_Max];
31 };
32 
33 struct IHashCalc
34 {
35   virtual void InitForNewFile() = 0;
36   virtual void Update(const void *data, UInt32 size) = 0;
37   virtual void SetSize(UInt64 size) = 0;
38   virtual void Final(bool isDir, bool isAltStream, const UString &path) = 0;
39 };
40 
41 struct CHashBundle: public IHashCalc
42 {
43   CObjectVector<CHasherState> Hashers;
44 
45   UInt64 NumFiles;
46   UInt64 NumDirs;
47   UInt64 NumAltStreams;
48   UInt64 FilesSize;
49   UInt64 AltStreamsSize;
50   UInt64 NumErrors;
51 
52   UInt64 CurSize;
53 
54   HRESULT SetMethods(DECL_EXTERNAL_CODECS_LOC_VARS const UStringVector &methods);
55 
InitCHashBundle56   void Init()
57   {
58     NumFiles = NumDirs = NumAltStreams = FilesSize = AltStreamsSize = NumErrors = 0;
59   }
60 
61   void InitForNewFile();
62   void Update(const void *data, UInt32 size);
63   void SetSize(UInt64 size);
64   void Final(bool isDir, bool isAltStream, const UString &path);
65 };
66 
67 #define INTERFACE_IHashCallbackUI(x) \
68   virtual HRESULT StartScanning() x; \
69   virtual HRESULT ScanProgress(UInt64 numFolders, UInt64 numFiles, UInt64 totalSize, const wchar_t *path, bool isDir) x; \
70   virtual HRESULT CanNotFindError(const wchar_t *name, DWORD systemError) x; \
71   virtual HRESULT FinishScanning() x; \
72   virtual HRESULT SetNumFiles(UInt64 numFiles) x; \
73   virtual HRESULT SetTotal(UInt64 size) x; \
74   virtual HRESULT SetCompleted(const UInt64 *completeValue) x; \
75   virtual HRESULT CheckBreak() x; \
76   virtual HRESULT BeforeFirstFile(const CHashBundle &hb) x; \
77   virtual HRESULT GetStream(const wchar_t *name, bool isFolder) x; \
78   virtual HRESULT OpenFileError(const wchar_t *name, DWORD systemError) x; \
79   virtual HRESULT SetOperationResult(UInt64 fileSize, const CHashBundle &hb, bool showHash) x; \
80   virtual HRESULT AfterLastFile(const CHashBundle &hb) x; \
81 
82 struct IHashCallbackUI
83 {
84   INTERFACE_IHashCallbackUI(=0)
85 };
86 
87 struct CHashOptions
88 {
89   UStringVector Methods;
90   bool OpenShareForWrite;
91   bool StdInMode;
92   bool AltStreamsMode;
93   NWildcard::ECensorPathMode PathMode;
94 
CHashOptionsCHashOptions95   CHashOptions(): StdInMode(false), OpenShareForWrite(false), AltStreamsMode(false), PathMode(NWildcard::k_RelatPath) {};
96 };
97 
98 HRESULT HashCalc(
99     DECL_EXTERNAL_CODECS_LOC_VARS
100     const NWildcard::CCensor &censor,
101     const CHashOptions &options,
102     UString &errorInfo,
103     IHashCallbackUI *callback);
104 
105 void AddHashHexToString(char *dest, const Byte *data, UInt32 size);
106 
107 #endif
108