xref: /aosp_15_r20/external/lzma/CPP/Common/CrcReg.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
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 EXTERN_C_END
15 
16 Z7_CLASS_IMP_COM_2(
17   CCrcHasher
18   , IHasher
19   , ICompressSetCoderProperties
20 )
21   UInt32 _crc;
22   Z7_CRC_UPDATE_FUNC _updateFunc;
23 
24   Z7_CLASS_NO_COPY(CCrcHasher)
25 
26   bool SetFunctions(UInt32 tSize);
27 public:
28   Byte _mtDummy[1 << 7];  // it's public to eliminate clang warning: unused private field
29 
30   CCrcHasher(): _crc(CRC_INIT_VAL) { SetFunctions(0); }
31 };
32 
33 bool CCrcHasher::SetFunctions(UInt32 tSize)
34 {
35   const Z7_CRC_UPDATE_FUNC f = z7_GetFunc_CrcUpdate(tSize);
36   if (!f)
37   {
38     _updateFunc = CrcUpdate;
39     return false;
40   }
41   _updateFunc = f;
42   return true;
43 }
44 
45 Z7_COM7F_IMF(CCrcHasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps))
46 {
47   for (UInt32 i = 0; i < numProps; i++)
48   {
49     if (propIDs[i] == NCoderPropID::kDefaultProp)
50     {
51       const PROPVARIANT &prop = coderProps[i];
52       if (prop.vt != VT_UI4)
53         return E_INVALIDARG;
54       if (!SetFunctions(prop.ulVal))
55         return E_NOTIMPL;
56     }
57   }
58   return S_OK;
59 }
60 
61 Z7_COM7F_IMF2(void, CCrcHasher::Init())
62 {
63   _crc = CRC_INIT_VAL;
64 }
65 
66 Z7_COM7F_IMF2(void, CCrcHasher::Update(const void *data, UInt32 size))
67 {
68   _crc = _updateFunc(_crc, data, size);
69 }
70 
71 Z7_COM7F_IMF2(void, CCrcHasher::Final(Byte *digest))
72 {
73   const UInt32 val = CRC_GET_DIGEST(_crc);
74   SetUi32(digest, val)
75 }
76 
77 REGISTER_HASHER(CCrcHasher, 0x1, "CRC32", 4)
78