libxcks  0.1.0.1
crc32.hpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 #ifndef INC_CRC32_HPP_1ADE12B7_336B_4906_BC45_70651142A09A
25 #define INC_CRC32_HPP_1ADE12B7_336B_4906_BC45_70651142A09A
26 
27 //---------------------------------------------------------------------------
28 #include "checksumex.hpp"
29 //---------------------------------------------------------------------------
30 
31 
32 namespace libxcks
33 {
52 class CRC32 final : public ChecksumEx
53 {
54  protected:
56  static const uint32_t crc_table[256];
57 
59  uint32_t crc32;
60 
61  public:
65  CRC32();
66 
70  void reset() override;
71 
77  uint32_t getUint32Value() const;
78 
86  std::string toString(const bool hexInUpperCase = false) const override;
87 
96  uint8_t* getValue(uint8_t* buffer) const override;
97 
105  size_t getSize() const override { return 4; }
106 
113  void update(const uint8_t* buf, size_t len) override;
114 
120  std::string getName() const override
121  {
122  return getChecksumName();
123  }
124 
130  ArrayString getAltNames() const override
131  {
132  return getAlternativeNames();
133  }
134 
144  ChecksumAlgoId getID() const override
145  {
146  return getIdentifier();
147  }
148 
154  static std::string getChecksumName()
155  {
156  return "CRC32";
157  }
158 
169  static constexpr ChecksumAlgoId getIdentifier()
170  {
171  return ChecksumAlgoId::CRC32;
172  }
173 
180 
190  {
191  return new CRC32();
192  }
193 };
194 //---------------------------------------------------------------------------
195 } // namespace libxcks
196 //---------------------------------------------------------------------------
197 
198 #endif // INC_CRC32_HPP_1ADE12B7_336B_4906_BC45_70651142A09A
Add some utilities to Checksum class.
Computes the CRC-32 from a byte stream.
Definition: crc32.hpp:53
static constexpr ChecksumAlgoId getIdentifier()
Returns an unique identifier for the checksum algorithm.
Definition: crc32.hpp:169
ChecksumAlgoId getID() const override
Returns an unique identifier for the checksum or the hash algorithm.
Definition: crc32.hpp:144
ArrayString getAltNames() const override
Returns the alternative names of the CRC32 checksum algorithm.
Definition: crc32.hpp:130
CRC32()
Default constructor.
Definition: crc32.cpp:98
static ArrayString getAlternativeNames()
Returns the alternative names of the CRC32 checksum algorithm.
Definition: crc32.cpp:181
uint32_t crc32
The current CRC32 value.
Definition: crc32.hpp:59
static Checksum * getNewInstance()
Gets a new instance of this class.
Definition: crc32.hpp:189
uint32_t getUint32Value() const
Returns the CRC32 value.
Definition: crc32.cpp:118
std::string getName() const override
Returns the name of the checksum or the hash algorithm.
Definition: crc32.hpp:120
static std::string getChecksumName()
Returns the name of the checksum algorithm.
Definition: crc32.hpp:154
uint8_t * getValue(uint8_t *buffer) const override
Returns the CRC32 value in the first 4 bytes of the given address.
Definition: crc32.cpp:128
void reset() override
Resets the CRC32 to initial value.
Definition: crc32.cpp:108
std::string toString(const bool hexInUpperCase=false) const override
Returns the CRC32 value in a string.
Definition: crc32.cpp:143
static const uint32_t crc_table[256]
Table used to compute the CRC32 value.
Definition: crc32.hpp:56
size_t getSize() const override
Returns the minimal size to allocate in memory to store the checksum with the getValue(buffer) metho...
Definition: crc32.hpp:105
void update(const uint8_t *buf, size_t len) override
Updates the CRC32 with specified array of bytes.
Definition: crc32.cpp:162
Computes a hash from a byte stream.
Definition: checksumex.hpp:72
Computes a checksum from a byte stream.
Definition: checksum.hpp:54
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65
std::vector< std::string > ArrayString
Array of strings.
Definition: types.hpp:44