1370b324cSopenharmony_ci// CrcReg.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "../../C/7zCrc.h"
6370b324cSopenharmony_ci#include "../../C/CpuArch.h"
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#include "../Common/MyCom.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#include "../7zip/Common/RegisterCodec.h"
11370b324cSopenharmony_ci
12370b324cSopenharmony_ciEXTERN_C_BEGIN
13370b324cSopenharmony_ci
14370b324cSopenharmony_ci// UInt32 Z7_FASTCALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table);
15370b324cSopenharmony_ci
16370b324cSopenharmony_ciextern CRC_FUNC g_CrcUpdate;
17370b324cSopenharmony_ci// extern CRC_FUNC g_CrcUpdateT4;
18370b324cSopenharmony_ciextern CRC_FUNC g_CrcUpdateT8;
19370b324cSopenharmony_ciextern CRC_FUNC g_CrcUpdateT0_32;
20370b324cSopenharmony_ciextern CRC_FUNC g_CrcUpdateT0_64;
21370b324cSopenharmony_ci
22370b324cSopenharmony_ciEXTERN_C_END
23370b324cSopenharmony_ci
24370b324cSopenharmony_ciZ7_CLASS_IMP_COM_2(
25370b324cSopenharmony_ci  CCrcHasher
26370b324cSopenharmony_ci  , IHasher
27370b324cSopenharmony_ci  , ICompressSetCoderProperties
28370b324cSopenharmony_ci)
29370b324cSopenharmony_ci  UInt32 _crc;
30370b324cSopenharmony_ci  CRC_FUNC _updateFunc;
31370b324cSopenharmony_ci
32370b324cSopenharmony_ci  Z7_CLASS_NO_COPY(CCrcHasher)
33370b324cSopenharmony_ci
34370b324cSopenharmony_ci  bool SetFunctions(UInt32 tSize);
35370b324cSopenharmony_cipublic:
36370b324cSopenharmony_ci  Byte _mtDummy[1 << 7];  // it's public to eliminate clang warning: unused private field
37370b324cSopenharmony_ci
38370b324cSopenharmony_ci  CCrcHasher(): _crc(CRC_INIT_VAL) { SetFunctions(0); }
39370b324cSopenharmony_ci};
40370b324cSopenharmony_ci
41370b324cSopenharmony_cibool CCrcHasher::SetFunctions(UInt32 tSize)
42370b324cSopenharmony_ci{
43370b324cSopenharmony_ci  CRC_FUNC f = NULL;
44370b324cSopenharmony_ci       if (tSize ==  0) f = g_CrcUpdate;
45370b324cSopenharmony_ci  // else if (tSize ==  1) f = CrcUpdateT1;
46370b324cSopenharmony_ci  // else if (tSize ==  4) f = g_CrcUpdateT4;
47370b324cSopenharmony_ci  else if (tSize ==  8) f = g_CrcUpdateT8;
48370b324cSopenharmony_ci  else if (tSize == 32) f = g_CrcUpdateT0_32;
49370b324cSopenharmony_ci  else if (tSize == 64) f = g_CrcUpdateT0_64;
50370b324cSopenharmony_ci
51370b324cSopenharmony_ci  if (!f)
52370b324cSopenharmony_ci  {
53370b324cSopenharmony_ci    _updateFunc = g_CrcUpdate;
54370b324cSopenharmony_ci    return false;
55370b324cSopenharmony_ci  }
56370b324cSopenharmony_ci  _updateFunc = f;
57370b324cSopenharmony_ci  return true;
58370b324cSopenharmony_ci}
59370b324cSopenharmony_ci
60370b324cSopenharmony_ciZ7_COM7F_IMF(CCrcHasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps))
61370b324cSopenharmony_ci{
62370b324cSopenharmony_ci  for (UInt32 i = 0; i < numProps; i++)
63370b324cSopenharmony_ci  {
64370b324cSopenharmony_ci    if (propIDs[i] == NCoderPropID::kDefaultProp)
65370b324cSopenharmony_ci    {
66370b324cSopenharmony_ci      const PROPVARIANT &prop = coderProps[i];
67370b324cSopenharmony_ci      if (prop.vt != VT_UI4)
68370b324cSopenharmony_ci        return E_INVALIDARG;
69370b324cSopenharmony_ci      if (!SetFunctions(prop.ulVal))
70370b324cSopenharmony_ci        return E_NOTIMPL;
71370b324cSopenharmony_ci    }
72370b324cSopenharmony_ci  }
73370b324cSopenharmony_ci  return S_OK;
74370b324cSopenharmony_ci}
75370b324cSopenharmony_ci
76370b324cSopenharmony_ciZ7_COM7F_IMF2(void, CCrcHasher::Init())
77370b324cSopenharmony_ci{
78370b324cSopenharmony_ci  _crc = CRC_INIT_VAL;
79370b324cSopenharmony_ci}
80370b324cSopenharmony_ci
81370b324cSopenharmony_ciZ7_COM7F_IMF2(void, CCrcHasher::Update(const void *data, UInt32 size))
82370b324cSopenharmony_ci{
83370b324cSopenharmony_ci  _crc = _updateFunc(_crc, data, size, g_CrcTable);
84370b324cSopenharmony_ci}
85370b324cSopenharmony_ci
86370b324cSopenharmony_ciZ7_COM7F_IMF2(void, CCrcHasher::Final(Byte *digest))
87370b324cSopenharmony_ci{
88370b324cSopenharmony_ci  const UInt32 val = CRC_GET_DIGEST(_crc);
89370b324cSopenharmony_ci  SetUi32(digest, val)
90370b324cSopenharmony_ci}
91370b324cSopenharmony_ci
92370b324cSopenharmony_ciREGISTER_HASHER(CCrcHasher, 0x1, "CRC32", 4)
93