xref: /aosp_15_r20/external/lzma/CPP/Common/Sha1Reg.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // Sha1Reg.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../C/Sha1.h"
6 
7 #include "../Common/MyBuffer2.h"
8 #include "../Common/MyCom.h"
9 
10 #include "../7zip/Common/RegisterCodec.h"
11 
12 Z7_CLASS_IMP_COM_2(
13   CSha1Hasher
14   , IHasher
15   , ICompressSetCoderProperties
16 )
17   CAlignedBuffer1 _buf;
18 public:
19   Byte _mtDummy[1 << 7];
20 
21   CSha1 *Sha() { return (CSha1 *)(void *)(Byte *)_buf; }
22 public:
23   CSha1Hasher():
24     _buf(sizeof(CSha1))
25   {
26     Sha1_SetFunction(Sha(), 0);
27     Sha1_InitState(Sha());
28   }
29 };
30 
31 Z7_COM7F_IMF2(void, CSha1Hasher::Init())
32 {
33   Sha1_InitState(Sha());
34 }
35 
36 Z7_COM7F_IMF2(void, CSha1Hasher::Update(const void *data, UInt32 size))
37 {
38   Sha1_Update(Sha(), (const Byte *)data, size);
39 }
40 
41 Z7_COM7F_IMF2(void, CSha1Hasher::Final(Byte *digest))
42 {
43   Sha1_Final(Sha(), digest);
44 }
45 
46 
47 Z7_COM7F_IMF(CSha1Hasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps))
48 {
49   unsigned algo = 0;
50   for (UInt32 i = 0; i < numProps; i++)
51   {
52     if (propIDs[i] == NCoderPropID::kDefaultProp)
53     {
54       const PROPVARIANT &prop = coderProps[i];
55       if (prop.vt != VT_UI4)
56         return E_INVALIDARG;
57       if (prop.ulVal > 2)
58         return E_NOTIMPL;
59       algo = (unsigned)prop.ulVal;
60     }
61   }
62   if (!Sha1_SetFunction(Sha(), algo))
63     return E_NOTIMPL;
64   return S_OK;
65 }
66 
67 REGISTER_HASHER(CSha1Hasher, 0x201, "SHA1", SHA1_DIGEST_SIZE)
68