Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HashCalc.h « Common « UI « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80a556537a9fc22cb2505435487da61f4cbf11c6 (plain)
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
// HashCalc.h

#ifndef __HASH_CALC_H
#define __HASH_CALC_H

#include "../../../Common/UTFConvert.h"
#include "../../../Common/Wildcard.h"

#include "../../Common/CreateCoder.h"
#include "../../Common/MethodProps.h"

#include "DirItem.h"
#include "IFileExtractCallback.h"

const unsigned k_HashCalc_DigestSize_Max = 64;
const unsigned k_HashCalc_ExtraSize = 8;
const unsigned k_HashCalc_NumGroups = 4;

enum
{
  k_HashCalc_Index_Current,
  k_HashCalc_Index_DataSum,
  k_HashCalc_Index_NamesSum,
  k_HashCalc_Index_StreamsSum
};

struct CHasherState
{
  CMyComPtr<IHasher> Hasher;
  AString Name;
  UInt32 DigestSize;
  UInt64 NumSums[k_HashCalc_NumGroups];
  Byte Digests[k_HashCalc_NumGroups][k_HashCalc_DigestSize_Max + k_HashCalc_ExtraSize];

  void InitDigestGroup(unsigned groupIndex)
  {
    NumSums[groupIndex] = 0;
    memset(Digests[groupIndex], 0, sizeof(Digests[groupIndex]));
  }

  const Byte *GetExtraData_for_Group(unsigned groupIndex) const
  {
    return Digests[groupIndex] + k_HashCalc_DigestSize_Max;
  }

  unsigned GetNumExtraBytes_for_Group(unsigned groupIndex) const
  {
    const Byte *p = GetExtraData_for_Group(groupIndex);
    // we use little-endian to read extra bytes
    for (unsigned i = k_HashCalc_ExtraSize; i != 0; i--)
      if (p[i - 1] != 0)
        return i;
    return 0;
  }

  void AddDigest(unsigned groupIndex, const Byte *data);

  void WriteToString(unsigned digestIndex, char *s) const;
};



struct IHashCalc
{
  virtual void InitForNewFile() = 0;
  virtual void Update(const void *data, UInt32 size) = 0;
  virtual void SetSize(UInt64 size) = 0;
  virtual void Final(bool isDir, bool isAltStream, const UString &path) = 0;
};

struct CHashBundle: public IHashCalc
{
  CObjectVector<CHasherState> Hashers;

  UInt64 NumDirs;
  UInt64 NumFiles;
  UInt64 NumAltStreams;
  UInt64 FilesSize;
  UInt64 AltStreamsSize;
  UInt64 NumErrors;

  UInt64 CurSize;

  UString MainName;
  UString FirstFileName;

  HRESULT SetMethods(DECL_EXTERNAL_CODECS_LOC_VARS const UStringVector &methods);
  
  // void Init() {}
  CHashBundle()
  {
    NumDirs = NumFiles = NumAltStreams = FilesSize = AltStreamsSize = NumErrors = 0;
  }

  virtual ~CHashBundle() {};

  void InitForNewFile();
  void Update(const void *data, UInt32 size);
  void SetSize(UInt64 size);
  void Final(bool isDir, bool isAltStream, const UString &path);
};

#define INTERFACE_IHashCallbackUI(x) \
  INTERFACE_IDirItemsCallback(x) \
  virtual HRESULT StartScanning() x; \
  virtual HRESULT FinishScanning(const CDirItemsStat &st) x; \
  virtual HRESULT SetNumFiles(UInt64 numFiles) x; \
  virtual HRESULT SetTotal(UInt64 size) x; \
  virtual HRESULT SetCompleted(const UInt64 *completeValue) x; \
  virtual HRESULT CheckBreak() x; \
  virtual HRESULT BeforeFirstFile(const CHashBundle &hb) x; \
  virtual HRESULT GetStream(const wchar_t *name, bool isFolder) x; \
  virtual HRESULT OpenFileError(const FString &path, DWORD systemError) x; \
  virtual HRESULT SetOperationResult(UInt64 fileSize, const CHashBundle &hb, bool showHash) x; \
  virtual HRESULT AfterLastFile(CHashBundle &hb) x; \

struct IHashCallbackUI: public IDirItemsCallback
{
  INTERFACE_IHashCallbackUI(=0)
};


struct CHashOptionsLocal
{
  CBoolPair HashMode_Zero;
  CBoolPair HashMode_Tag;
  CBoolPair HashMode_Dirs;
  CBoolPair HashMode_OnlyHash;
  
  void Init_HashOptionsLocal()
  {
    HashMode_Zero.Init();
    HashMode_Tag.Init();
    HashMode_Dirs.Init();
    HashMode_OnlyHash.Init();
    // HashMode_Dirs = true; // for debug
  }

  CHashOptionsLocal()
  {
    Init_HashOptionsLocal();
  }
  
  bool ParseFlagCharOption(wchar_t c, bool val)
  {
    c = MyCharLower_Ascii(c);
         if (c == 'z') HashMode_Zero.SetVal_as_Defined(val);
    else if (c == 't') HashMode_Tag.SetVal_as_Defined(val);
    else if (c == 'd') HashMode_Dirs.SetVal_as_Defined(val);
    else if (c == 'h') HashMode_OnlyHash.SetVal_as_Defined(val);
    else return false;
    return true;
  }

  bool ParseString(const UString &s)
  {
    for (unsigned i = 0; i < s.Len();)
    {
      const wchar_t c = s[i++];
      bool val = true;
      if (i < s.Len())
      {
        const wchar_t next  = s[i];
        if (next == '-')
        {
          val = false;
          i++;
        }
      }
      if (!ParseFlagCharOption(c, val))
        return false;
    }
    return true;
  }
};


struct CHashOptions
  // : public CHashOptionsLocal
{
  UStringVector Methods;
  // UString HashFilePath;

  bool PreserveATime;
  bool OpenShareForWrite;
  bool StdInMode;
  bool AltStreamsMode;
  CBoolPair SymLinks;

  NWildcard::ECensorPathMode PathMode;

  CHashOptions():
      PreserveATime(false),
      OpenShareForWrite(false),
      StdInMode(false),
      AltStreamsMode(false),
      PathMode(NWildcard::k_RelatPath) {};
};


HRESULT HashCalc(
    DECL_EXTERNAL_CODECS_LOC_VARS
    const NWildcard::CCensor &censor,
    const CHashOptions &options,
    AString &errorInfo,
    IHashCallbackUI *callback);



#ifndef _SFX

namespace NHash {

struct CHashPair
{
  CByteBuffer Hash;
  char Mode;
  bool IsBSD;
  bool Size_from_Arc_Defined;
  bool Size_from_Disk_Defined;
  AString Method;
  AString Name;
  
  AString FullLine;
  AString HashString;
  // unsigned HashLengthInBits;

  // AString MethodName;
  UInt64 Size_from_Arc;
  UInt64 Size_from_Disk;

  bool IsDir() const;

  void Get_UString_Path(UString &path) const
  {
    path.Empty();
    if (!ConvertUTF8ToUnicode(Name, path))
      return;
  }

  bool ParseCksum(const char *s);
  bool Parse(const char *s);

  bool IsSupportedMode() const
  {
    return Mode != 'U' && Mode != '^';
  }

  CHashPair():
      Mode(0)
      , IsBSD(false)
      , Size_from_Arc_Defined(false)
      , Size_from_Disk_Defined(false)
      // , HashLengthInBits(0)
      , Size_from_Arc(0)
      , Size_from_Disk(0)
    {}
};


class CHandler:
  public IInArchive,
  public IArchiveGetRawProps,
  // public IGetArchiveHashHandler,
  public IOutArchive,
  public ISetProperties,
  public CMyUnknownImp
{
  bool _isArc;
  UInt64 _phySize;
  CObjectVector<CHashPair> HashPairs;
  UString _nameExtenstion;
  // UString _method_fromName;
  AString _pgpMethod;
  bool _is_CksumMode;
  bool _is_PgpMethod;
  bool _is_ZeroMode;
  bool _are_there_Tags;
  bool _are_there_Dirs;
  bool _hashSize_Defined;
  unsigned _hashSize;

  bool _crcSize_WasSet;
  UInt32 _crcSize;
  UStringVector _methods;

  void ClearVars();

  void InitProps()
  {
    _crcSize_WasSet = false;
    _crcSize = 4;
    _methods.Clear();
    _options.Init_HashOptionsLocal();
  }

  CHashOptionsLocal _options;

  bool CanUpdate() const
  {
    if (!_isArc || _is_PgpMethod || _is_CksumMode)
      return false;
    return true;

  }

  HRESULT SetProperty(const wchar_t *nameSpec, const PROPVARIANT &value);

public:

  CHandler();

  MY_UNKNOWN_IMP4(
      IInArchive,
      IArchiveGetRawProps,
      IOutArchive,
      ISetProperties
      /*, IGetArchiveHashHandler */
      )
  INTERFACE_IInArchive(;)
  INTERFACE_IOutArchive(;)
  INTERFACE_IArchiveGetRawProps(;)
  // STDMETHOD(GetArchiveHashHandler)(CHandler **handler);
  STDMETHOD(SetProperties)(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps);
};

}

void Codecs_AddHashArcHandler(CCodecs *codecs);

#endif


#endif