1 // CrcReg.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../C/7zCrc.h"
6 #include "../../C/CpuArch.h"
7 
8 #include "../Common/MyCom.h"
9 
10 #include "../7zip/Common/RegisterCodec.h"
11 
12 EXTERN_C_BEGIN
13 
14 // UInt32 Z7_FASTCALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table);
15 
16 extern CRC_FUNC g_CrcUpdate;
17 // extern CRC_FUNC g_CrcUpdateT4;
18 extern CRC_FUNC g_CrcUpdateT8;
19 extern CRC_FUNC g_CrcUpdateT0_32;
20 extern CRC_FUNC g_CrcUpdateT0_64;
21 
22 EXTERN_C_END
23 
24 Z7_CLASS_IMP_COM_2(
25   CCrcHasher
26   , IHasher
27   , ICompressSetCoderProperties
28 )
29   UInt32 _crc;
30   CRC_FUNC _updateFunc;
31 
32   Z7_CLASS_NO_COPY(CCrcHasher)
33 
34   bool SetFunctions(UInt32 tSize);
35 public:
36   Byte _mtDummy[1 << 7];  // it's public to eliminate clang warning: unused private field
37 
CCrcHasher()38   CCrcHasher(): _crc(CRC_INIT_VAL) { SetFunctions(0); }
39 };
40 
41 bool CCrcHasher::SetFunctions(UInt32 tSize)
42 {
43   CRC_FUNC f = NULL;
44        if (tSize ==  0) f = g_CrcUpdate;
45   // else if (tSize ==  1) f = CrcUpdateT1;
46   // else if (tSize ==  4) f = g_CrcUpdateT4;
47   else if (tSize ==  8) f = g_CrcUpdateT8;
48   else if (tSize == 32) f = g_CrcUpdateT0_32;
49   else if (tSize == 64) f = g_CrcUpdateT0_64;
50 
51   if (!f)
52   {
53     _updateFunc = g_CrcUpdate;
54     return false;
55   }
56   _updateFunc = f;
57   return true;
58 }
59 
60 Z7_COM7F_IMF(CCrcHasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps))
61 {
62   for (UInt32 i = 0; i < numProps; i++)
63   {
64     if (propIDs[i] == NCoderPropID::kDefaultProp)
65     {
66       const PROPVARIANT &prop = coderProps[i];
67       if (prop.vt != VT_UI4)
68         return E_INVALIDARG;
69       if (!SetFunctions(prop.ulVal))
70         return E_NOTIMPL;
71     }
72   }
73   return S_OK;
74 }
75 
76 Z7_COM7F_IMF2(void, CCrcHasher::Init())
77 {
78   _crc = CRC_INIT_VAL;
79 }
80 
81 Z7_COM7F_IMF2(void, CCrcHasher::Update(const void *data, UInt32 size))
82 {
83   _crc = _updateFunc(_crc, data, size, g_CrcTable);
84 }
85 
86 Z7_COM7F_IMF2(void, CCrcHasher::Final(Byte *digest))
87 {
88   const UInt32 val = CRC_GET_DIGEST(_crc);
89   SetUi32(digest, val)
90 }
91 
92 REGISTER_HASHER(CCrcHasher, 0x1, "CRC32", 4)
93