ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
CRC.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include <cstring>
10 #include <type_traits>
11 #include <vector>
12 
13 
14 namespace shlublu
15 {
16 
20 namespace CRCData
21 {
22  extern "C" const uint32_t crc32Table[];
23  extern "C" const uint64_t crc64Table[];
24 }
25 
26 
32 template <typename T>
33 class CRC
34 {
35 public:
40  CRC()
41  : mHash(0)
42  {
43  static_assert_valid_base_type();
44  }
45 
46 
52  template <typename P> CRC(P param)
53  : CRC()
54  {
55  static_assert_valid_base_type();
56  accumulate(param);
57  }
58 
59 
66  CRC<T>& accumulate(std::string const & str)
67  {
68  return accumulate(str.c_str(), 0, str.length());
69  }
70 
71 
78  CRC<T>& accumulate(char const * sz)
79  {
80  return accumulate(sz, 0, ::strlen(sz));
81  }
82 
83 
90  template <typename P>
91  CRC<T>& accumulate(std::vector<P> const & v)
92  {
93  accumulate(v.begin(), v.end());
94 
95  return *this;
96  }
97 
98 
99 
108  template <typename P>
109  CRC<T>& accumulate(P value)
110  {
111  static_assert_valid_parameter_type<P>();
112  return accumulate(reinterpret_cast<char const *>(&value), 0, sizeof(P));
113  }
114 
115 
144  CRC<T>& accumulate(char const* data, size_t offset, size_t length)
145  {
146  T const * const crcTab(crcTable());
147 
148  // Compute the CRC
149  const size_t afterLastOffset(offset + length);
150 
151  for (size_t i = offset; i < afterLastOffset; ++i)
152  {
153  mHash = crcTab[(mHash ^ data[i]) & 0xff] ^ (mHash >> 8);
154  }
155 
156  mHash = mHash & T(-1);
157 
158  return *this;
159  }
160 
161 
178  template <typename Iter>
179  CRC<T>& accumulate(const Iter first, const Iter last)
180  {
181  for (Iter it = first; it != last; ++it)
182  {
183  accumulate(*it);
184  }
185 
186  return *this;
187  }
188 
189 
194  T get() const { return mHash; }
195 
196 
197 private:
199 
200  void static_assert_valid_base_type() const
201  {
202  static_assert(std::is_same<T, uint32_t>::value || std::is_same<T, uint64_t>::value, "Type should be unsigned 32 or 64.");
203  }
204 
205 
206  template<typename P>
207  void static_assert_valid_parameter_type() const
208  {
209  static_assert(std::is_arithmetic<P>::value, "Type should be arithmetic.");
210  }
211 
212 
213  T const * crcTable() const
214  {
215  return std::is_same<T, uint32_t>::value ?
216  reinterpret_cast<T const *>(CRCData::crc32Table) :
217  reinterpret_cast<T const *>(CRCData::crc64Table);
218  }
219 
220 
221 private:
222  T mHash;
223 
225 };
226 
227 
231 using crc32_t = uint32_t;
232 
236 using crc64_t = uint64_t;
237 
242 
247 
248 }
shlublu::CRC::accumulate
CRC< T > & accumulate(const Iter first, const Iter last)
Accumulates a range of elements.
Definition: CRC.h:179
shlublu::CRC::accumulate
CRC< T > & accumulate(char const *data, size_t offset, size_t length)
Accumulates arbitraty bytes.
Definition: CRC.h:144
shlublu::CRC::CRC
CRC(P param)
Constructor.
Definition: CRC.h:52
shlublu::CRC::accumulate
CRC< T > & accumulate(std::string const &str)
Accumulates a string as a series of bytes.
Definition: CRC.h:66
shlublu::CRC::accumulate
CRC< T > & accumulate(P value)
Accumulates an arithmetic value.
Definition: CRC.h:109
shlublu::CRC::CRC
CRC()
Constructor.
Definition: CRC.h:40
shlublu::CRC::accumulate
CRC< T > & accumulate(char const *sz)
Accumulates a C-string as a series of bytes.
Definition: CRC.h:78
shlublu
shlublu::CRCData::crc32Table
const uint32_t crc32Table[]
32 bits table.
Definition: CRC.h:22
shlublu::crc64_t
uint64_t crc64_t
The 64 bits CRC value type.
Definition: CRC.h:236
shlublu::CRC::accumulate
CRC< T > & accumulate(std::vector< P > const &v)
Accumulates a vector as a series of contained objects.
Definition: CRC.h:91
shlublu::crc32_t
uint32_t crc32_t
The 32 bits CRC value type.
Definition: CRC.h:231
shlublu::CRC
CRC accumulator.
Definition: CRC.h:34
shlublu::CRC::get
T get() const
Returns the CRC value resulting from the accumulation.
Definition: CRC.h:194
shlublu::CRCData::crc64Table
const uint64_t crc64Table[]
64 bits table.
Definition: CRC.h:23