unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

hash.cpp
Go to the documentation of this file.
1#include "rar.hpp"
2
4{
6
7 // Zero length data CRC32 is 0. It is important to set it when creating
8 // headers with no following data like directories or symlinks.
10 CRC32=0;
11 if (Type==HASH_BLAKE2)
12 {
13 // dd0e891776933f43c7d032b08a917e25741f8aa9a12c12e1cac8801500f2ca4f
14 // is BLAKE2sp hash of empty data. We init the structure to this value,
15 // so if we create a file or service header with no following data like
16 // "file copy" or "symlink", we set the checksum to proper value avoiding
17 // additional header type or size checks when extracting.
18 static byte EmptyHash[32]={
19 0xdd, 0x0e, 0x89, 0x17, 0x76, 0x93, 0x3f, 0x43,
20 0xc7, 0xd0, 0x32, 0xb0, 0x8a, 0x91, 0x7e, 0x25,
21 0x74, 0x1f, 0x8a, 0xa9, 0xa1, 0x2c, 0x12, 0xe1,
22 0xca, 0xc8, 0x80, 0x15, 0x00, 0xf2, 0xca, 0x4f
23 };
24 memcpy(Digest,EmptyHash,sizeof(Digest));
25 }
26}
27
28
30{
31 if (Type==HASH_NONE || cmp.Type==HASH_NONE)
32 return true;
33 if (Type==HASH_RAR14 && cmp.Type==HASH_RAR14 ||
35 return CRC32==cmp.CRC32;
36 if (Type==HASH_BLAKE2 && cmp.Type==HASH_BLAKE2)
37 return memcmp(Digest,cmp.Digest,sizeof(Digest))==0;
38 return false;
39}
40
41
43{
44 blake2ctx=NULL;
46#ifdef RAR_SMP
47 ThPool=NULL;
48 MaxThreads=0;
49#endif
50}
51
52
54{
55#ifdef RAR_SMP
56 delete ThPool;
57#endif
58 cleandata(&CurCRC32, sizeof(CurCRC32));
59 if (blake2ctx!=NULL)
60 {
62 delete blake2ctx;
63 }
64}
65
66
67void DataHash::Init(HASH_TYPE Type,uint MaxThreads)
68{
69 if (blake2ctx==NULL)
72 if (Type==HASH_RAR14)
73 CurCRC32=0;
74 if (Type==HASH_CRC32)
75 CurCRC32=0xffffffff; // Initial CRC32 value.
76 if (Type==HASH_BLAKE2)
78#ifdef RAR_SMP
79 DataHash::MaxThreads=Min(MaxThreads,MaxHashThreads);
80#endif
81}
82
83
84void DataHash::Update(const void *Data,size_t DataSize)
85{
86#ifndef SFX_MODULE
88 CurCRC32=Checksum14((ushort)CurCRC32,Data,DataSize);
89#endif
91 CurCRC32=CRC32(CurCRC32,Data,DataSize);
92
94 {
95#ifdef RAR_SMP
96 if (MaxThreads>1 && ThPool==NULL)
97 ThPool=new ThreadPool(BLAKE2_THREADS_NUMBER);
98 blake2ctx->ThPool=ThPool;
99 blake2ctx->MaxThreads=MaxThreads;
100#endif
101 blake2sp_update( blake2ctx, (byte *)Data, DataSize);
102 }
103}
104
105
107{
108 Result->Type=HashType;
109 if (HashType==HASH_RAR14)
110 Result->CRC32=CurCRC32;
111 if (HashType==HASH_CRC32)
112 Result->CRC32=CurCRC32^0xffffffff;
114 {
115 // Preserve the original context, so we can continue hashing if necessary.
117 blake2sp_final(&res,Result->Digest);
118 }
119}
120
121
123{
124 return HashType==HASH_CRC32 ? CurCRC32^0xffffffff : 0;
125}
126
127
128bool DataHash::Cmp(HashValue *CmpValue,byte *Key)
129{
130 HashValue Final;
131 Result(&Final);
132 if (Key!=NULL)
133 ConvertHashToMAC(&Final,Key);
134 return Final==*CmpValue;
135}
#define BLAKE2_THREADS_NUMBER
Definition: blake2s.hpp:6
void blake2sp_final(blake2sp_state *S, byte *digest)
Definition: blake2sp.cpp:131
void blake2sp_update(blake2sp_state *S, const byte *in, size_t inlen)
Definition: blake2sp.cpp:67
void blake2sp_init(blake2sp_state *S)
Definition: blake2sp.cpp:16
void Init(HASH_TYPE Type, uint MaxThreads)
Definition: hash.cpp:67
DataHash()
Definition: hash.cpp:42
uint CurCRC32
Definition: hash.hpp:31
void Update(const void *Data, size_t DataSize)
Definition: hash.cpp:84
~DataHash()
Definition: hash.cpp:53
HASH_TYPE HashType
Definition: hash.hpp:30
void Result(HashValue *Result)
Definition: hash.cpp:106
HASH_TYPE Type()
Definition: hash.hpp:49
bool Cmp(HashValue *CmpValue, byte *Key)
Definition: hash.cpp:128
uint GetCRC32()
Definition: hash.cpp:122
blake2sp_state * blake2ctx
Definition: hash.hpp:32
uint CRC32(uint StartCRC, const void *Addr, size_t Size)
Definition: crc.cpp:62
ushort Checksum14(ushort StartCRC, const void *Addr, size_t Size)
Definition: crc.cpp:100
void ConvertHashToMAC(HashValue *Value, byte *Key)
Definition: crypt5.cpp:192
HASH_TYPE
Definition: hash.hpp:4
@ HASH_NONE
Definition: hash.hpp:4
@ HASH_RAR14
Definition: hash.hpp:4
@ HASH_CRC32
Definition: hash.hpp:4
@ HASH_BLAKE2
Definition: hash.hpp:4
#define Min(x, y)
Definition: rardefs.hpp:4
unsigned int uint
Definition: rartypes.hpp:8
uint16_t ushort
Definition: rartypes.hpp:7
void cleandata(void *data, size_t size)
Definition: secpassword.cpp:80
byte Digest[SHA256_DIGEST_SIZE]
Definition: hash.hpp:16
void Init(HASH_TYPE Type)
Definition: hash.cpp:3
HASH_TYPE Type
Definition: hash.hpp:12
bool operator==(const HashValue &cmp)
Definition: hash.cpp:29
uint CRC32
Definition: hash.hpp:15