File: hash.hpp

package info (click to toggle)
unrar-nonfree 1%3A7.1.8-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 1,952 kB
  • sloc: cpp: 26,394; makefile: 712; sh: 11
file content (75 lines) | stat: -rw-r--r-- 1,828 bytes parent folder | download | duplicates (2)
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
#ifndef _RAR_DATAHASH_
#define _RAR_DATAHASH_

enum HASH_TYPE {HASH_NONE,HASH_RAR14,HASH_CRC32,HASH_BLAKE2};

struct HashValue
{
  void Init(HASH_TYPE Type);

  // Use the const member, so types on both sides of "==" match.
  // Otherwise clang -std=c++20 issues "ambiguity is between a regular call
  // to this operator and a call with the argument order reversed" warning.
  bool operator == (const HashValue &cmp) const;

  // Not actually used now. Const member for same reason as operator == above.
  // Can be removed after switching to C++20, which automatically provides "!="
  // if operator == is defined.
  bool operator != (const HashValue &cmp) const {return !(*this==cmp);}

  HASH_TYPE Type;
  union
  {
    uint CRC32;
    byte Digest[SHA256_DIGEST_SIZE];
  };
};


#ifdef RAR_SMP
class ThreadPool;
class DataHash;
#endif


class DataHash
{
  public:
    struct CRC32ThreadData
    {
      void *Data;
      size_t DataSize;
      uint DataCRC;
    };
  private:
    void UpdateCRC32MT(const void *Data,size_t DataSize);
    uint BitReverse32(uint N);
    uint gfMulCRC(uint A, uint B);
    uint gfExpCRC(uint N);

    // Speed gain seems to vanish above 8 CRC32 threads.
    static const uint CRC32_POOL_THREADS=8;
    // Thread pool must allow at least BLAKE2_THREADS_NUMBER threads.
    static const uint HASH_POOL_THREADS=Max(BLAKE2_THREADS_NUMBER,CRC32_POOL_THREADS);

    HASH_TYPE HashType;
    uint CurCRC32;
    blake2sp_state *blake2ctx;

#ifdef RAR_SMP
    ThreadPool *ThPool;

    uint MaxThreads;
#endif
  public:
    DataHash();
    ~DataHash();
    void Init(HASH_TYPE Type,uint MaxThreads);
    void Update(const void *Data,size_t DataSize);
    void Result(HashValue *Result);
    uint GetCRC32();
    bool Cmp(HashValue *CmpValue,byte *Key);
    HASH_TYPE Type() {return HashType;}
};

#endif