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 "DirItem.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   AString 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 NumDirs;
46   UInt64 NumFiles;
47   UInt64 NumAltStreams;
48   UInt64 FilesSize;
49   UInt64 AltStreamsSize;
50   UInt64 NumErrors;
51 
52   UInt64 CurSize;
53 
54   UString MainName;
55   UString FirstFileName;
56 
57   HRESULT SetMethods(DECL_EXTERNAL_CODECS_LOC_VARS const UStringVector &methods);
58 
59   // void Init() {}
CHashBundleCHashBundle60   CHashBundle()
61   {
62     NumDirs = NumFiles = NumAltStreams = FilesSize = AltStreamsSize = NumErrors = 0;
63   }
64 
65   void InitForNewFile();
66   void Update(const void *data, UInt32 size);
67   void SetSize(UInt64 size);
68   void Final(bool isDir, bool isAltStream, const UString &path);
69 };
70 
71 #define INTERFACE_IHashCallbackUI(x) \
72   INTERFACE_IDirItemsCallback(x) \
73   virtual HRESULT StartScanning() x; \
74   virtual HRESULT FinishScanning(const CDirItemsStat &st) x; \
75   virtual HRESULT SetNumFiles(UInt64 numFiles) x; \
76   virtual HRESULT SetTotal(UInt64 size) x; \
77   virtual HRESULT SetCompleted(const UInt64 *completeValue) x; \
78   virtual HRESULT CheckBreak() x; \
79   virtual HRESULT BeforeFirstFile(const CHashBundle &hb) x; \
80   virtual HRESULT GetStream(const wchar_t *name, bool isFolder) x; \
81   virtual HRESULT OpenFileError(const FString &path, DWORD systemError) x; \
82   virtual HRESULT SetOperationResult(UInt64 fileSize, const CHashBundle &hb, bool showHash) x; \
83   virtual HRESULT AfterLastFile(CHashBundle &hb) x; \
84 
85 struct IHashCallbackUI: public IDirItemsCallback
86 {
87   INTERFACE_IHashCallbackUI(=0)
88 };
89 
90 struct CHashOptions
91 {
92   UStringVector Methods;
93   bool OpenShareForWrite;
94   bool StdInMode;
95   bool AltStreamsMode;
96   NWildcard::ECensorPathMode PathMode;
97 
CHashOptionsCHashOptions98   CHashOptions(): StdInMode(false), OpenShareForWrite(false), AltStreamsMode(false), PathMode(NWildcard::k_RelatPath) {};
99 };
100 
101 HRESULT HashCalc(
102     DECL_EXTERNAL_CODECS_LOC_VARS
103     const NWildcard::CCensor &censor,
104     const CHashOptions &options,
105     AString &errorInfo,
106     IHashCallbackUI *callback);
107 
108 void AddHashHexToString(char *dest, const Byte *data, UInt32 size);
109 
110 #endif
111