CRC++  1.0.1.0
Easy to use and fast C++ CRC library.
 All Classes Files Functions Variables Macros
CRC.h
Go to the documentation of this file.
1 
38 /*
39  CRC++ can be configured by setting various #defines before #including this header file:
40 
41  #define crcpp_uint8 - Specifies the type used to store CRCs that have a width of 8 bits or less.
42  This type is not used in CRC calculations. Defaults to ::std::uint8_t.
43  #define crcpp_uint16 - Specifies the type used to store CRCs that have a width between 9 and 16 bits (inclusive).
44  This type is not used in CRC calculations. Defaults to ::std::uint16_t.
45  #define crcpp_uint32 - Specifies the type used to store CRCs that have a width between 17 and 32 bits (inclusive).
46  This type is not used in CRC calculations. Defaults to ::std::uint32_t.
47  #define crcpp_uint64 - Specifies the type used to store CRCs that have a width between 33 and 64 bits (inclusive).
48  This type is not used in CRC calculations. Defaults to ::std::uint64_t.
49  #define crcpp_size - This type is used for loop iteration and function signatures only. Defaults to ::std::size_t.
50  #define CRCPP_USE_NAMESPACE - Define to place all CRC++ code within the ::CRCPP namespace.
51  #define CRCPP_BRANCHLESS - Define to enable a branchless CRC implementation. The branchless implementation uses a single integer
52  multiplication in the bit-by-bit calculation instead of a small conditional. The branchless implementation
53  may be faster on processor architectures which support single-instruction integer multiplication.
54  #define CRCPP_USE_CPP11 - Define to enables C++11 features (move semantics, constexpr, static_assert, etc.).
55  #define CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS - Define to include definitions for little-used CRCs.
56 */
57 
58 #ifndef CRCPP_CRC_H_
59 #define CRCPP_CRC_H_
60 
61 #include <climits> // Includes CHAR_BIT
62 #ifdef CRCPP_USE_CPP11
63 #include <cstddef> // Includes ::std::size_t
64 #include <cstdint> // Includes ::std::uint8_t, ::std::uint16_t, ::std::uint32_t, ::std::uint64_t
65 #else
66 #include <stddef.h> // Includes size_t
67 #include <stdint.h> // Includes uint8_t, uint16_t, uint32_t, uint64_t
68 #endif
69 #include <limits> // Includes ::std::numeric_limits
70 #include <utility> // Includes ::std::move
71 
72 #ifndef crcpp_uint8
73 # ifdef CRCPP_USE_CPP11
74 # define crcpp_uint8 ::std::uint8_t
76 # else
77 # define crcpp_uint8 uint8_t
79 # endif
80 #endif
81 
82 #ifndef crcpp_uint16
83 # ifdef CRCPP_USE_CPP11
84 # define crcpp_uint16 ::std::uint16_t
86 # else
87 # define crcpp_uint16 uint16_t
89 # endif
90 #endif
91 
92 #ifndef crcpp_uint32
93 # ifdef CRCPP_USE_CPP11
94 # define crcpp_uint32 ::std::uint32_t
96 # else
97 # define crcpp_uint32 uint32_t
99 # endif
100 #endif
101 
102 #ifndef crcpp_uint64
103 # ifdef CRCPP_USE_CPP11
104 # define crcpp_uint64 ::std::uint64_t
106 # else
107 # define crcpp_uint64 uint64_t
109 # endif
110 #endif
111 
112 #ifndef crcpp_size
113 # ifdef CRCPP_USE_CPP11
114 # define crcpp_size ::std::size_t
116 # else
117 # define crcpp_size size_t
119 # endif
120 #endif
121 
122 #ifdef CRCPP_USE_CPP11
123 # define crcpp_constexpr constexpr
125 #else
126 # define crcpp_constexpr const
128 #endif
129 
130 #ifdef CRCPP_USE_NAMESPACE
131 namespace CRCPP
132 {
133 #endif
134 
142 class CRC
143 {
144 public:
145  // Forward declaration
146  template <typename CRCType, crcpp_uint16 CRCWidth>
147  struct Table;
148 
152  template <typename CRCType, crcpp_uint16 CRCWidth>
153  struct Parameters
154  {
155  CRCType polynomial;
156  CRCType initialValue;
157  CRCType finalXOR;
160 
161  Table<CRCType, CRCWidth> MakeTable() const;
162  };
163 
168  template <typename CRCType, crcpp_uint16 CRCWidth>
169  struct Table
170  {
171  // Constructors are intentionally NOT marked explicit.
172  Table(const Parameters<CRCType, CRCWidth> & parameters);
173 
174 #ifdef CRCPP_USE_CPP11
175  Table(Parameters<CRCType, CRCWidth> && parameters);
176 #endif
177 
178  const Parameters<CRCType, CRCWidth> & GetParameters() const;
179 
180  const CRCType * GetTable() const;
181 
182  CRCType operator[](unsigned char index) const;
183 
184  private:
185  void InitTable();
186 
188  CRCType table[1 << CHAR_BIT];
189  };
190 
191  // The number of bits in CRCType must be at least as large as CRCWidth.
192  // CRCType must be an unsigned integer type or a custom type with operator overloads.
193  template <typename CRCType, crcpp_uint16 CRCWidth>
194  static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
195 
196  template <typename CRCType, crcpp_uint16 CRCWidth>
197  static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
198 
199  template <typename CRCType, crcpp_uint16 CRCWidth>
200  static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
201 
202  template <typename CRCType, crcpp_uint16 CRCWidth>
203  static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
204 
205  template <typename CRCType, crcpp_uint16 CRCWidth>
206  static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
207 
208  template <typename CRCType, crcpp_uint16 CRCWidth>
209  static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
210 
211  template <typename CRCType, crcpp_uint16 CRCWidth>
212  static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
213 
214  template <typename CRCType, crcpp_uint16 CRCWidth>
215  static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
216 
217  // Common CRCs up to 64 bits.
218  // Note: Check values are the computed CRCs when given an ASCII input of "123456789" (without null terminator)
219 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
220  static const Parameters< crcpp_uint8, 4> & CRC_4_ITU();
221  static const Parameters< crcpp_uint8, 5> & CRC_5_EPC();
222  static const Parameters< crcpp_uint8, 5> & CRC_5_ITU();
223  static const Parameters< crcpp_uint8, 5> & CRC_5_USB();
224  static const Parameters< crcpp_uint8, 6> & CRC_6_CDMA2000A();
225  static const Parameters< crcpp_uint8, 6> & CRC_6_CDMA2000B();
226  static const Parameters< crcpp_uint8, 6> & CRC_6_ITU();
227  static const Parameters< crcpp_uint8, 6> & CRC_6_NR();
228  static const Parameters< crcpp_uint8, 7> & CRC_7();
229 #endif
230  static const Parameters< crcpp_uint8, 8> & CRC_8();
231 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
232  static const Parameters< crcpp_uint8, 8> & CRC_8_EBU();
233  static const Parameters< crcpp_uint8, 8> & CRC_8_MAXIM();
234  static const Parameters< crcpp_uint8, 8> & CRC_8_WCDMA();
235  static const Parameters< crcpp_uint8, 8> & CRC_8_LTE();
236  static const Parameters<crcpp_uint16, 10> & CRC_10();
237  static const Parameters<crcpp_uint16, 10> & CRC_10_CDMA2000();
238  static const Parameters<crcpp_uint16, 11> & CRC_11();
239  static const Parameters<crcpp_uint16, 11> & CRC_11_NR();
240  static const Parameters<crcpp_uint16, 12> & CRC_12_CDMA2000();
241  static const Parameters<crcpp_uint16, 12> & CRC_12_DECT();
242  static const Parameters<crcpp_uint16, 12> & CRC_12_UMTS();
243  static const Parameters<crcpp_uint16, 13> & CRC_13_BBC();
244  static const Parameters<crcpp_uint16, 15> & CRC_15();
245  static const Parameters<crcpp_uint16, 15> & CRC_15_MPT1327();
246 #endif
247  static const Parameters<crcpp_uint16, 16> & CRC_16_ARC();
248  static const Parameters<crcpp_uint16, 16> & CRC_16_BUYPASS();
249  static const Parameters<crcpp_uint16, 16> & CRC_16_CCITTFALSE();
250  static const Parameters<crcpp_uint16, 16> & CRC_16_MCRF4XX();
251 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
252  static const Parameters<crcpp_uint16, 16> & CRC_16_CDMA2000();
253  static const Parameters<crcpp_uint16, 16> & CRC_16_CMS();
254  static const Parameters<crcpp_uint16, 16> & CRC_16_DECTR();
255  static const Parameters<crcpp_uint16, 16> & CRC_16_DECTX();
256  static const Parameters<crcpp_uint16, 16> & CRC_16_DNP();
257 #endif
258  static const Parameters<crcpp_uint16, 16> & CRC_16_GENIBUS();
259  static const Parameters<crcpp_uint16, 16> & CRC_16_KERMIT();
260 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
261  static const Parameters<crcpp_uint16, 16> & CRC_16_MAXIM();
262  static const Parameters<crcpp_uint16, 16> & CRC_16_MODBUS();
263  static const Parameters<crcpp_uint16, 16> & CRC_16_T10DIF();
264  static const Parameters<crcpp_uint16, 16> & CRC_16_USB();
265 #endif
266  static const Parameters<crcpp_uint16, 16> & CRC_16_X25();
267  static const Parameters<crcpp_uint16, 16> & CRC_16_XMODEM();
268 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
269  static const Parameters<crcpp_uint32, 17> & CRC_17_CAN();
270  static const Parameters<crcpp_uint32, 21> & CRC_21_CAN();
271  static const Parameters<crcpp_uint32, 24> & CRC_24();
272  static const Parameters<crcpp_uint32, 24> & CRC_24_FLEXRAYA();
273  static const Parameters<crcpp_uint32, 24> & CRC_24_FLEXRAYB();
274  static const Parameters<crcpp_uint32, 24> & CRC_24_LTEA();
275  static const Parameters<crcpp_uint32, 24> & CRC_24_LTEB();
276  static const Parameters<crcpp_uint32, 24> & CRC_24_NRC();
277  static const Parameters<crcpp_uint32, 30> & CRC_30();
278 #endif
279  static const Parameters<crcpp_uint32, 32> & CRC_32();
280  static const Parameters<crcpp_uint32, 32> & CRC_32_BZIP2();
281 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
282  static const Parameters<crcpp_uint32, 32> & CRC_32_C();
283 #endif
284  static const Parameters<crcpp_uint32, 32> & CRC_32_MPEG2();
285  static const Parameters<crcpp_uint32, 32> & CRC_32_POSIX();
286 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
287  static const Parameters<crcpp_uint32, 32> & CRC_32_Q();
288  static const Parameters<crcpp_uint64, 40> & CRC_40_GSM();
289  static const Parameters<crcpp_uint64, 64> & CRC_64();
290 #endif
291 
292 #ifdef CRCPP_USE_CPP11
293  CRC() = delete;
294  CRC(const CRC & other) = delete;
295  CRC & operator=(const CRC & other) = delete;
296  CRC(CRC && other) = delete;
297  CRC & operator=(CRC && other) = delete;
298 #endif
299 
300 private:
301 #ifndef CRCPP_USE_CPP11
302  CRC();
303  CRC(const CRC & other);
304  CRC & operator=(const CRC & other);
305 #endif
306 
307  template <typename IntegerType>
308  static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits);
309 
310  template <typename CRCType, crcpp_uint16 CRCWidth>
311  static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
312 
313  template <typename CRCType, crcpp_uint16 CRCWidth>
314  static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
315 
316  template <typename CRCType, crcpp_uint16 CRCWidth>
317  static CRCType CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
318 
319  template <typename CRCType, crcpp_uint16 CRCWidth>
320  static CRCType CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder);
321 
322  template <typename CRCType, crcpp_uint16 CRCWidth>
323  static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
324 };
325 
334 template <typename CRCType, crcpp_uint16 CRCWidth>
336 {
337  // This should take advantage of RVO and optimize out the copy.
338  return CRC::Table<CRCType, CRCWidth>(*this);
339 }
340 
347 template <typename CRCType, crcpp_uint16 CRCWidth>
349  parameters(params)
350 {
351  InitTable();
352 }
353 
354 #ifdef CRCPP_USE_CPP11
355 
361 template <typename CRCType, crcpp_uint16 CRCWidth>
363  parameters(::std::move(params))
364 {
365  InitTable();
366 }
367 #endif
368 
375 template <typename CRCType, crcpp_uint16 CRCWidth>
377 {
378  return parameters;
379 }
380 
387 template <typename CRCType, crcpp_uint16 CRCWidth>
388 inline const CRCType * CRC::Table<CRCType, CRCWidth>::GetTable() const
389 {
390  return table;
391 }
392 
400 template <typename CRCType, crcpp_uint16 CRCWidth>
401 inline CRCType CRC::Table<CRCType, CRCWidth>::operator[](unsigned char index) const
402 {
403  return table[index];
404 }
405 
411 template <typename CRCType, crcpp_uint16 CRCWidth>
413 {
414  // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
415  static crcpp_constexpr CRCType BIT_MASK((CRCType(1) << (CRCWidth - CRCType(1))) |
416  ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
417 
418  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
419  static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
420 
421  CRCType crc;
422  unsigned char byte = 0;
423 
424  // Loop over each dividend (each possible number storable in an unsigned char)
425  do
426  {
427  crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte, sizeof(byte), parameters, CRCType(0));
428 
429  // This mask might not be necessary; all unit tests pass with this line commented out,
430  // but that might just be a coincidence based on the CRC parameters used for testing.
431  // In any case, this is harmless to leave in and only adds a single machine instruction per loop iteration.
432  crc &= BIT_MASK;
433 
434  if (!parameters.reflectInput && CRCWidth < CHAR_BIT)
435  {
436  // Undo the special operation at the end of the CalculateRemainder()
437  // function for non-reflected CRCs < CHAR_BIT.
438  crc = static_cast<CRCType>(crc << SHIFT);
439  }
440 
441  table[byte] = crc;
442  }
443  while (++byte);
444 }
445 
455 template <typename CRCType, crcpp_uint16 CRCWidth>
456 inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
457 {
458  CRCType remainder = CalculateRemainder(data, size, parameters, parameters.initialValue);
459 
460  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
461 
462  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
463 }
475 template <typename CRCType, crcpp_uint16 CRCWidth>
476 inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
477 {
478  CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
479 
480  remainder = CalculateRemainder(data, size, parameters, remainder);
481 
482  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
483 
484  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
485 }
486 
496 template <typename CRCType, crcpp_uint16 CRCWidth>
497 inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
498 {
499  const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
500 
501  CRCType remainder = CalculateRemainder(data, size, lookupTable, parameters.initialValue);
502 
503  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
504 
505  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
506 }
507 
519 template <typename CRCType, crcpp_uint16 CRCWidth>
520 inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
521 {
522  const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
523 
524  CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
525 
526  remainder = CalculateRemainder(data, size, lookupTable, remainder);
527 
528  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
529 
530  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
531 }
532 
542 template <typename CRCType, crcpp_uint16 CRCWidth>
543 inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
544 {
545  CRCType remainder = parameters.initialValue;
546 
547  // Calculate the remainder on a whole number of bytes first, then call
548  // a special-case function for the remaining bits.
549  crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
550  if (wholeNumberOfBytes > 0)
551  {
552  remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, remainder);
553  }
554 
555  crcpp_size remainingNumberOfBits = size % CHAR_BIT;
556  if (remainingNumberOfBits != 0)
557  {
558  unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
559  remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
560  }
561 
562  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
563 
564  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
565 }
577 template <typename CRCType, crcpp_uint16 CRCWidth>
578 inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
579 {
580  CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
581 
582  // Calculate the remainder on a whole number of bytes first, then call
583  // a special-case function for the remaining bits.
584  crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
585  if (wholeNumberOfBytes > 0)
586  {
587  remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, parameters.initialValue);
588  }
589 
590  crcpp_size remainingNumberOfBits = size % CHAR_BIT;
591  if (remainingNumberOfBits != 0)
592  {
593  unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
594  remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
595  }
596 
597  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
598 
599  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
600 }
601 
611 template <typename CRCType, crcpp_uint16 CRCWidth>
612 inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
613 {
614  const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
615 
616  CRCType remainder = parameters.initialValue;
617 
618  // Calculate the remainder on a whole number of bytes first, then call
619  // a special-case function for the remaining bits.
620  crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
621  if (wholeNumberOfBytes > 0)
622  {
623  remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, remainder);
624  }
625 
626  crcpp_size remainingNumberOfBits = size % CHAR_BIT;
627  if (remainingNumberOfBits != 0)
628  {
629  unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
630  remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
631  }
632 
633  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
634 
635  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
636 }
637 
649 template <typename CRCType, crcpp_uint16 CRCWidth>
650 inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
651 {
652  const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
653 
654  CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
655 
656  // Calculate the remainder on a whole number of bytes first, then call
657  // a special-case function for the remaining bits.
658  crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
659  if (wholeNumberOfBytes > 0)
660  {
661  remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, parameters.initialValue);
662  }
663 
664  crcpp_size remainingNumberOfBits = size % CHAR_BIT;
665  if (remainingNumberOfBits > 0)
666  {
667  unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
668  remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
669  }
670 
671  // No need to mask the remainder here; the mask will be applied in the Finalize() function.
672 
673  return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
674 }
675 
683 template <typename IntegerType>
684 inline IntegerType CRC::Reflect(IntegerType value, crcpp_uint16 numBits)
685 {
686  IntegerType reversedValue(0);
687 
688  for (crcpp_uint16 i = 0; i < numBits; ++i)
689  {
690  reversedValue = static_cast<IntegerType>((reversedValue << 1) | (value & 1));
691  value = static_cast<IntegerType>(value >> 1);
692  }
693 
694  return reversedValue;
695 }
696 
706 template <typename CRCType, crcpp_uint16 CRCWidth>
707 inline CRCType CRC::Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
708 {
709  // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
710  static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
711  ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
712 
713  if (reflectOutput)
714  {
715  remainder = Reflect(remainder, CRCWidth);
716  }
717 
718  return (remainder ^ finalXOR) & BIT_MASK;
719 }
720 
738 template <typename CRCType, crcpp_uint16 CRCWidth>
739 inline CRCType CRC::UndoFinalize(CRCType crc, CRCType finalXOR, bool reflectOutput)
740 {
741  // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
742  static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
743  ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
744 
745  crc = (crc & BIT_MASK) ^ finalXOR;
746 
747  if (reflectOutput)
748  {
749  crc = Reflect(crc, CRCWidth);
750  }
751 
752  return crc;
753 }
754 
765 template <typename CRCType, crcpp_uint16 CRCWidth>
766 inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
767 {
768 #ifdef CRCPP_USE_CPP11
769  // This static_assert is put here because this function will always be compiled in no matter what
770  // the template parameters are and whether or not a table lookup or bit-by-bit algorithm is used.
771  static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth, "CRCType is too small to contain a CRC of width CRCWidth.");
772 #else
773  // Catching this compile-time error is very important. Sadly, the compiler error will be very cryptic, but it's
774  // better than nothing.
775  enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
776 #endif
777 
778  const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
779 
780  // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
781  // computation from the inner loop (looping over each bit) as possible.
782  if (parameters.reflectInput)
783  {
784  CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
785  while (size--)
786  {
787  remainder = static_cast<CRCType>(remainder ^ *current++);
788 
789  // An optimizing compiler might choose to unroll this loop.
790  for (crcpp_size i = 0; i < CHAR_BIT; ++i)
791  {
792 #ifdef CRCPP_BRANCHLESS
793  // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
794  // if (remainder & 1)
795  // remainder = (remainder >> 1) ^ polynomial;
796  // else
797  // remainder >>= 1;
798  remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
799 #else
800  remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
801 #endif
802  }
803  }
804  }
805  else if (CRCWidth >= CHAR_BIT)
806  {
807  static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
808 #ifndef CRCPP_BRANCHLESS
809  static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
810 #endif
811  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
812  static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
813 
814  while (size--)
815  {
816  remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(*current++) << SHIFT));
817 
818  // An optimizing compiler might choose to unroll this loop.
819  for (crcpp_size i = 0; i < CHAR_BIT; ++i)
820  {
821 #ifdef CRCPP_BRANCHLESS
822  // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
823  // if (remainder & CRC_HIGHEST_BIT_MASK)
824  // remainder = (remainder << 1) ^ parameters.polynomial;
825  // else
826  // remainder <<= 1;
827  remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
828 #else
829  remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
830 #endif
831  }
832  }
833  }
834  else
835  {
836  static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
837 #ifndef CRCPP_BRANCHLESS
838  static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
839 #endif
840  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
841  static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
842 
843  CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
844  remainder = static_cast<CRCType>(remainder << SHIFT);
845 
846  while (size--)
847  {
848  remainder = static_cast<CRCType>(remainder ^ *current++);
849 
850  // An optimizing compiler might choose to unroll this loop.
851  for (crcpp_size i = 0; i < CHAR_BIT; ++i)
852  {
853 #ifdef CRCPP_BRANCHLESS
854  // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
855  // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
856  // remainder = (remainder << 1) ^ polynomial;
857  // else
858  // remainder <<= 1;
859  remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
860 #else
861  remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
862 #endif
863  }
864  }
865 
866  remainder = static_cast<CRCType>(remainder >> SHIFT);
867  }
868 
869  return remainder;
870 }
871 
882 template <typename CRCType, crcpp_uint16 CRCWidth>
883 inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder)
884 {
885  const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
886 
887  if (lookupTable.GetParameters().reflectInput)
888  {
889  while (size--)
890  {
891 #if defined(WIN32) || defined(_WIN32) || defined(WINCE)
892  // Disable warning about data loss when doing (remainder >> CHAR_BIT) when
893  // remainder is one byte long. The algorithm is still correct in this case,
894  // though it's possible that one additional machine instruction will be executed.
895 # pragma warning (push)
896 # pragma warning (disable : 4333)
897 #endif
898  remainder = static_cast<CRCType>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
899 #if defined(WIN32) || defined(_WIN32) || defined(WINCE)
900 # pragma warning (pop)
901 #endif
902  }
903  }
904  else if (CRCWidth >= CHAR_BIT)
905  {
906  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
907  static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
908 
909  while (size--)
910  {
911  remainder = static_cast<CRCType>((remainder << CHAR_BIT) ^ lookupTable[static_cast<unsigned char>((remainder >> SHIFT) ^ *current++)]);
912  }
913  }
914  else
915  {
916  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
917  static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
918 
919  remainder = static_cast<CRCType>(remainder << SHIFT);
920 
921  while (size--)
922  {
923  // Note: no need to mask here since remainder is guaranteed to fit in a single byte.
924  remainder = lookupTable[static_cast<unsigned char>(remainder ^ *current++)];
925  }
926 
927  remainder = static_cast<CRCType>(remainder >> SHIFT);
928  }
929 
930  return remainder;
931 }
932 
933 template <typename CRCType, crcpp_uint16 CRCWidth>
934 inline CRCType CRC::CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
935 {
936  // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
937  // computation from the inner loop (looping over each bit) as possible.
938  if (parameters.reflectInput)
939  {
940  CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
941  remainder = static_cast<CRCType>(remainder ^ byte);
942 
943  // An optimizing compiler might choose to unroll this loop.
944  for (crcpp_size i = 0; i < numBits; ++i)
945  {
946 #ifdef CRCPP_BRANCHLESS
947  // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
948  // if (remainder & 1)
949  // remainder = (remainder >> 1) ^ polynomial;
950  // else
951  // remainder >>= 1;
952  remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
953 #else
954  remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
955 #endif
956  }
957  }
958  else if (CRCWidth >= CHAR_BIT)
959  {
960  static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
961 #ifndef CRCPP_BRANCHLESS
962  static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
963 #endif
964  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
965  static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
966 
967  remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(byte) << SHIFT));
968 
969  // An optimizing compiler might choose to unroll this loop.
970  for (crcpp_size i = 0; i < numBits; ++i)
971  {
972 #ifdef CRCPP_BRANCHLESS
973  // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
974  // if (remainder & CRC_HIGHEST_BIT_MASK)
975  // remainder = (remainder << 1) ^ parameters.polynomial;
976  // else
977  // remainder <<= 1;
978  remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
979 #else
980  remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
981 #endif
982  }
983  }
984  else
985  {
986  static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
987 #ifndef CRCPP_BRANCHLESS
988  static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
989 #endif
990  // The conditional expression is used to avoid a -Wshift-count-overflow warning.
991  static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
992 
993  CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
994  remainder = static_cast<CRCType>((remainder << SHIFT) ^ byte);
995 
996  // An optimizing compiler might choose to unroll this loop.
997  for (crcpp_size i = 0; i < numBits; ++i)
998  {
999 #ifdef CRCPP_BRANCHLESS
1000  // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1001  // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
1002  // remainder = (remainder << 1) ^ polynomial;
1003  // else
1004  // remainder <<= 1;
1005  remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
1006 #else
1007  remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
1008 #endif
1009  }
1010 
1011  remainder = static_cast<CRCType>(remainder >> SHIFT);
1012  }
1013 
1014  return remainder;
1015 }
1016 
1017 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1018 
1031 {
1032  static const Parameters<crcpp_uint8, 4> parameters = { 0x3, 0x0, 0x0, true, true };
1033  return parameters;
1034 }
1035 
1049 {
1050  static const Parameters<crcpp_uint8, 5> parameters = { 0x09, 0x09, 0x00, false, false };
1051  return parameters;
1052 }
1053 
1067 {
1068  static const Parameters<crcpp_uint8, 5> parameters = { 0x15, 0x00, 0x00, true, true };
1069  return parameters;
1070 }
1071 
1085 {
1086  static const Parameters<crcpp_uint8, 5> parameters = { 0x05, 0x1F, 0x1F, true, true };
1087  return parameters;
1088 }
1089 
1103 {
1104  static const Parameters<crcpp_uint8, 6> parameters = { 0x27, 0x3F, 0x00, false, false };
1105  return parameters;
1106 }
1107 
1121 {
1122  static const Parameters<crcpp_uint8, 6> parameters = { 0x07, 0x3F, 0x00, false, false };
1123  return parameters;
1124 }
1125 
1139 {
1140  static const Parameters<crcpp_uint8, 6> parameters = { 0x03, 0x00, 0x00, true, true };
1141  return parameters;
1142 }
1143 
1158 {
1159  static const Parameters<crcpp_uint8, 6> parameters = { 0x21, 0x00, 0x00, false, false };
1160  return parameters;
1161 }
1162 
1176 {
1177  static const Parameters<crcpp_uint8, 7> parameters = { 0x09, 0x00, 0x00, false, false };
1178  return parameters;
1179 }
1180 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1181 
1195 {
1196  static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0x00, 0x00, false, false };
1197  return parameters;
1198 }
1199 
1200 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1201 
1214 {
1215  static const Parameters<crcpp_uint8, 8> parameters = { 0x1D, 0xFF, 0x00, true, true };
1216  return parameters;
1217 }
1218 
1232 {
1233  static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0x00, 0x00, true, true };
1234  return parameters;
1235 }
1236 
1250 {
1251  static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, true, true };
1252  return parameters;
1253 }
1254 
1268 {
1269  static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, false, false };
1270  return parameters;
1271 }
1272 
1286 {
1287  static const Parameters<crcpp_uint16, 10> parameters = { 0x233, 0x000, 0x000, false, false };
1288  return parameters;
1289 }
1290 
1304 {
1305  static const Parameters<crcpp_uint16, 10> parameters = { 0x3D9, 0x3FF, 0x000, false, false };
1306  return parameters;
1307 }
1308 
1322 {
1323  static const Parameters<crcpp_uint16, 11> parameters = { 0x385, 0x01A, 0x000, false, false };
1324  return parameters;
1325 }
1326 
1341 {
1342  static const Parameters<crcpp_uint16, 11> parameters = { 0x621, 0x000, 0x000, false, false };
1343  return parameters;
1344 }
1345 
1359 {
1360  static const Parameters<crcpp_uint16, 12> parameters = { 0xF13, 0xFFF, 0x000, false, false };
1361  return parameters;
1362 }
1363 
1377 {
1378  static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, false };
1379  return parameters;
1380 }
1381 
1395 {
1396  static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, true };
1397  return parameters;
1398 }
1399 
1413 {
1414  static const Parameters<crcpp_uint16, 13> parameters = { 0x1CF5, 0x0000, 0x0000, false, false };
1415  return parameters;
1416 }
1417 
1431 {
1432  static const Parameters<crcpp_uint16, 15> parameters = { 0x4599, 0x0000, 0x0000, false, false };
1433  return parameters;
1434 }
1435 
1449 {
1450  static const Parameters<crcpp_uint16, 15> parameters = { 0x6815, 0x0000, 0x0001, false, false };
1451  return parameters;
1452 }
1453 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1454 
1468 {
1469  static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, true, true };
1470  return parameters;
1471 }
1472 
1486 {
1487  static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, false, false };
1488  return parameters;
1489 }
1490 
1504 {
1505  static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, false, false };
1506  return parameters;
1507 }
1508 
1522 {
1523  static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, true, true};
1524  return parameters;
1525 }
1526 
1527 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1528 
1541 {
1542  static const Parameters<crcpp_uint16, 16> parameters = { 0xC867, 0xFFFF, 0x0000, false, false };
1543  return parameters;
1544 }
1545 
1559 {
1560  static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, false, false };
1561  return parameters;
1562 }
1563 
1577 {
1578  static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0001, false, false };
1579  return parameters;
1580 }
1581 
1595 {
1596  static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0000, false, false };
1597  return parameters;
1598 }
1599 
1613 {
1614  static const Parameters<crcpp_uint16, 16> parameters = { 0x3D65, 0x0000, 0xFFFF, true, true };
1615  return parameters;
1616 }
1617 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1618 
1632 {
1633  static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, false, false };
1634  return parameters;
1635 }
1636 
1650 {
1651  static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, true, true };
1652  return parameters;
1653 }
1654 
1655 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1656 
1669 {
1670  static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0xFFFF, true, true };
1671  return parameters;
1672 }
1673 
1687 {
1688  static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, true, true };
1689  return parameters;
1690 }
1691 
1705 {
1706  static const Parameters<crcpp_uint16, 16> parameters = { 0x8BB7, 0x0000, 0x0000, false, false };
1707  return parameters;
1708 }
1709 
1723 {
1724  static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0xFFFF, true, true };
1725  return parameters;
1726 }
1727 
1728 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1729 
1743 {
1744  static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, true, true };
1745  return parameters;
1746 }
1747 
1761 {
1762  static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, false, false };
1763  return parameters;
1764 }
1765 
1766 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1767 
1780 {
1781  static const Parameters<crcpp_uint32, 17> parameters = { 0x1685B, 0x00000, 0x00000, false, false };
1782  return parameters;
1783 }
1784 
1798 {
1799  static const Parameters<crcpp_uint32, 21> parameters = { 0x102899, 0x000000, 0x000000, false, false };
1800  return parameters;
1801 }
1802 
1816 {
1817  static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0xB704CE, 0x000000, false, false };
1818  return parameters;
1819 }
1820 
1834 {
1835  static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xFEDCBA, 0x000000, false, false };
1836  return parameters;
1837 }
1838 
1852 {
1853  static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xABCDEF, 0x000000, false, false };
1854  return parameters;
1855 }
1856 
1871 {
1872  static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0x000000, 0x000000, false, false };
1873  return parameters;
1874 }
1875 
1890 {
1891  static const Parameters<crcpp_uint32, 24> parameters = { 0x800063, 0x000000, 0x000000, false, false };
1892  return parameters;
1893 }
1894 
1909 {
1910  static const Parameters<crcpp_uint32, 24> parameters = { 0xB2B117, 0x000000, 0x000000, false, false };
1911  return parameters;
1912 }
1913 
1927 {
1928  static const Parameters<crcpp_uint32, 30> parameters = { 0x2030B9C7, 0x3FFFFFFF, 0x00000000, false, false };
1929  return parameters;
1930 }
1931 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1932 
1946 {
1947  static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
1948  return parameters;
1949 }
1950 
1964 {
1965  static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false };
1966  return parameters;
1967 }
1968 
1969 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1970 
1983 {
1984  static const Parameters<crcpp_uint32, 32> parameters = { 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
1985  return parameters;
1986 }
1987 #endif
1988 
2002 {
2003  static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false };
2004  return parameters;
2005 }
2006 
2020 {
2021  static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false };
2022  return parameters;
2023 }
2024 
2025 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2026 
2039 {
2040  static const Parameters<crcpp_uint32, 32> parameters = { 0x814141AB, 0x00000000, 0x00000000, false, false };
2041  return parameters;
2042 }
2043 
2057 {
2058  static const Parameters<crcpp_uint64, 40> parameters = { 0x0004820009, 0x0000000000, 0xFFFFFFFFFF, false, false };
2059  return parameters;
2060 }
2061 
2075 {
2076  static const Parameters<crcpp_uint64, 64> parameters = { 0x42F0E1EBA9EA3693, 0x0000000000000000, 0x0000000000000000, false, false };
2077  return parameters;
2078 }
2079 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2080 
2081 #ifdef CRCPP_USE_NAMESPACE
2082 }
2083 #endif
2084 
2085 #endif // CRCPP_CRC_H_
#define crcpp_constexpr
Compile-time expression definition.
Definition: CRC.h:124
static const Parameters< crcpp_uint16, 16 > & CRC_16_MCRF4XX()
Returns a set of parameters for CRC-16 MCRF4XX.
Definition: CRC.h:1521
static const Parameters< crcpp_uint16, 16 > & CRC_16_X25()
Returns a set of parameters for CRC-16 X-25 (aka CRC-16 IBM-SDLC, CRC-16 ISO-HDLC, CRC-16 B).
Definition: CRC.h:1742
static const Parameters< crcpp_uint16, 16 > & CRC_16_DNP()
Returns a set of parameters for CRC-16 DNP.
Definition: CRC.h:1612
Parameters< CRCType, CRCWidth > parameters
CRC parameters used to construct the table.
Definition: CRC.h:187
static const Parameters< crcpp_uint16, 15 > & CRC_15_MPT1327()
Returns a set of parameters for CRC-15 MPT1327.
Definition: CRC.h:1448
static const Parameters< crcpp_uint16, 12 > & CRC_12_CDMA2000()
Returns a set of parameters for CRC-12 CDMA2000.
Definition: CRC.h:1358
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYA()
Returns a set of parameters for CRC-24 FlexRay-A.
Definition: CRC.h:1833
static const Parameters< crcpp_uint32, 32 > & CRC_32_BZIP2()
Returns a set of parameters for CRC-32 BZIP2 (aka CRC-32 AAL5, CRC-32 DECT-B, CRC-32 B-CRC)...
Definition: CRC.h:1963
static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Definition: CRC.h:934
static const Parameters< crcpp_uint32, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
Definition: CRC.h:2001
CRCType polynomial
CRC polynomial.
Definition: CRC.h:155
static const Parameters< crcpp_uint8, 8 > & CRC_8_MAXIM()
Returns a set of parameters for CRC-8 MAXIM (aka CRC-8 DOW-CRC).
Definition: CRC.h:1231
CRCType finalXOR
Value to XOR with the final CRC.
Definition: CRC.h:157
static const Parameters< crcpp_uint16, 16 > & CRC_16_BUYPASS()
Returns a set of parameters for CRC-16 BUYPASS (aka CRC-16 VERIFONE, CRC-16 UMTS).
Definition: CRC.h:1485
CRC parameters.
Definition: CRC.h:153
static const Parameters< crcpp_uint16, 16 > & CRC_16_GENIBUS()
Returns a set of parameters for CRC-16 GENIBUS (aka CRC-16 EPC, CRC-16 I-CODE, CRC-16 DARC)...
Definition: CRC.h:1631
#define crcpp_size
Unsigned size definition, used for specifying data sizes.
Definition: CRC.h:115
bool reflectOutput
true to reflect the output CRC (reflection occurs before the final XOR)
Definition: CRC.h:159
static const Parameters< crcpp_uint16, 16 > & CRC_16_ARC()
Returns a set of parameters for CRC-16 ARC (aka CRC-16 IBM, CRC-16 LHA).
Definition: CRC.h:1467
static const Parameters< crcpp_uint16, 16 > & CRC_16_DECTX()
Returns a set of parameters for CRC-16 DECT-X (aka CRC-16 X-CRC).
Definition: CRC.h:1594
static const Parameters< crcpp_uint8, 7 > & CRC_7()
Returns a set of parameters for CRC-7 JEDEC.
Definition: CRC.h:1175
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000A()
Returns a set of parameters for CRC-6 CDMA2000-A.
Definition: CRC.h:1102
static CRCType CalculateBits(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition: CRC.h:543
static const Parameters< crcpp_uint16, 16 > & CRC_16_USB()
Returns a set of parameters for CRC-16 USB.
Definition: CRC.h:1722
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
Definition: CRC.h:401
static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Computes the final reflection and XOR of a CRC remainder.
Definition: CRC.h:707
static const Parameters< crcpp_uint32, 32 > & CRC_32_Q()
Returns a set of parameters for CRC-32 Q.
Definition: CRC.h:2038
static const Parameters< crcpp_uint16, 13 > & CRC_13_BBC()
Returns a set of parameters for CRC-13 BBC.
Definition: CRC.h:1412
static const Parameters< crcpp_uint8, 5 > & CRC_5_EPC()
Returns a set of parameters for CRC-5 EPC.
Definition: CRC.h:1048
const CRCType * GetTable() const
Gets the CRC table.
Definition: CRC.h:388
void InitTable()
Initializes a CRC table.
Definition: CRC.h:412
static const Parameters< crcpp_uint16, 16 > & CRC_16_KERMIT()
Returns a set of parameters for CRC-16 KERMIT (aka CRC-16 CCITT, CRC-16 CCITT-TRUE).
Definition: CRC.h:1649
static const Parameters< crcpp_uint16, 16 > & CRC_16_CDMA2000()
Returns a set of parameters for CRC-16 CDMA2000.
Definition: CRC.h:1540
static const Parameters< crcpp_uint32, 21 > & CRC_21_CAN()
Returns a set of parameters for CRC-21 CAN.
Definition: CRC.h:1797
static const Parameters< crcpp_uint16, 15 > & CRC_15()
Returns a set of parameters for CRC-15 CAN.
Definition: CRC.h:1430
static const Parameters< crcpp_uint32, 24 > & CRC_24()
Returns a set of parameters for CRC-24 OPENPGP.
Definition: CRC.h:1815
static const Parameters< crcpp_uint8, 4 > & CRC_4_ITU()
Returns a set of parameters for CRC-4 ITU.
Definition: CRC.h:1030
bool reflectInput
true to reflect all input bytes
Definition: CRC.h:158
static const Parameters< crcpp_uint8, 5 > & CRC_5_USB()
Returns a set of parameters for CRC-5 USB.
Definition: CRC.h:1084
CRCType initialValue
Initial CRC value.
Definition: CRC.h:156
Static class for computing CRCs.
Definition: CRC.h:142
static const Parameters< crcpp_uint16, 11 > & CRC_11_NR()
Returns a set of parameters for CRC-11 NR.
Definition: CRC.h:1340
static const Parameters< crcpp_uint32, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
Definition: CRC.h:2019
static const Parameters< crcpp_uint8, 6 > & CRC_6_NR()
Returns a set of parameters for CRC-6 NR.
Definition: CRC.h:1157
static const Parameters< crcpp_uint8, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
Definition: CRC.h:1194
static CRCType CalculateRemainder(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Computes a CRC remainder.
Definition: CRC.h:766
static const Parameters< crcpp_uint16, 16 > & CRC_16_MAXIM()
Returns a set of parameters for CRC-16 MAXIM.
Definition: CRC.h:1668
static const Parameters< crcpp_uint16, 16 > & CRC_16_CMS()
Returns a set of parameters for CRC-16 CMS.
Definition: CRC.h:1558
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition: CRC.h:456
static const Parameters< crcpp_uint16, 16 > & CRC_16_MODBUS()
Returns a set of parameters for CRC-16 MODBUS.
Definition: CRC.h:1686
static const Parameters< crcpp_uint64, 40 > & CRC_40_GSM()
Returns a set of parameters for CRC-40 GSM.
Definition: CRC.h:2056
static const Parameters< crcpp_uint16, 11 > & CRC_11()
Returns a set of parameters for CRC-11 FlexRay.
Definition: CRC.h:1321
static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Undoes the process of computing the final reflection and XOR of a CRC remainder.
Definition: CRC.h:739
static const Parameters< crcpp_uint64, 64 > & CRC_64()
Returns a set of parameters for CRC-64 ECMA.
Definition: CRC.h:2074
static const Parameters< crcpp_uint16, 16 > & CRC_16_DECTR()
Returns a set of parameters for CRC-16 DECT-R (aka CRC-16 R-CRC).
Definition: CRC.h:1576
static const Parameters< crcpp_uint16, 10 > & CRC_10()
Returns a set of parameters for CRC-10 ITU.
Definition: CRC.h:1285
static const Parameters< crcpp_uint8, 8 > & CRC_8_LTE()
Returns a set of parameters for CRC-8 LTE.
Definition: CRC.h:1267
static const Parameters< crcpp_uint16, 10 > & CRC_10_CDMA2000()
Returns a set of parameters for CRC-10 CDMA2000.
Definition: CRC.h:1303
CRC lookup table. After construction, the CRC parameters are fixed.
Definition: CRC.h:147
static const Parameters< crcpp_uint32, 32 > & CRC_32()
Returns a set of parameters for CRC-32 (aka CRC-32 ADCCP, CRC-32 PKZip).
Definition: CRC.h:1945
static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits)
Reflects (i.e. reverses the bits within) an integer value.
Definition: CRC.h:684
static const Parameters< crcpp_uint8, 5 > & CRC_5_ITU()
Returns a set of parameters for CRC-5 ITU.
Definition: CRC.h:1066
static const Parameters< crcpp_uint16, 12 > & CRC_12_UMTS()
Returns a set of parameters for CRC-12 UMTS (aka CRC-12 3GPP).
Definition: CRC.h:1394
static const Parameters< crcpp_uint8, 8 > & CRC_8_WCDMA()
Returns a set of parameters for CRC-8 WCDMA.
Definition: CRC.h:1249
static const Parameters< crcpp_uint16, 12 > & CRC_12_DECT()
Returns a set of parameters for CRC-12 DECT (aka CRC-12 X-CRC).
Definition: CRC.h:1376
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
Definition: CRC.h:335
Table(const Parameters< CRCType, CRCWidth > &parameters)
Constructs a CRC table from a set of CRC parameters.
Definition: CRC.h:348
static const Parameters< crcpp_uint32, 32 > & CRC_32_C()
Returns a set of parameters for CRC-32 C (aka CRC-32 ISCSI, CRC-32 Castagnoli, CRC-32 Interlaken)...
Definition: CRC.h:1982
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEA()
Returns a set of parameters for CRC-24 LTE-A/NR-A.
Definition: CRC.h:1870
static const Parameters< crcpp_uint8, 8 > & CRC_8_EBU()
Returns a set of parameters for CRC-8 EBU (aka CRC-8 AES).
Definition: CRC.h:1213
static const Parameters< crcpp_uint8, 6 > & CRC_6_ITU()
Returns a set of parameters for CRC-6 ITU.
Definition: CRC.h:1138
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000B()
Returns a set of parameters for CRC-6 CDMA2000-B.
Definition: CRC.h:1120
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEB()
Returns a set of parameters for CRC-24 LTE-B/NR-B.
Definition: CRC.h:1889
static const Parameters< crcpp_uint32, 24 > & CRC_24_NRC()
Returns a set of parameters for CRC-24 NR-C.
Definition: CRC.h:1908
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYB()
Returns a set of parameters for CRC-24 FlexRay-B.
Definition: CRC.h:1851
static const Parameters< crcpp_uint32, 30 > & CRC_30()
Returns a set of parameters for CRC-30 CDMA.
Definition: CRC.h:1926
static const Parameters< crcpp_uint16, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
Definition: CRC.h:1503
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
Definition: CRC.h:85
static const Parameters< crcpp_uint16, 16 > & CRC_16_XMODEM()
Returns a set of parameters for CRC-16 XMODEM (aka CRC-16 ZMODEM, CRC-16 ACORN, CRC-16 LTE)...
Definition: CRC.h:1760
static const Parameters< crcpp_uint32, 17 > & CRC_17_CAN()
Returns a set of parameters for CRC-17 CAN.
Definition: CRC.h:1779
static const Parameters< crcpp_uint16, 16 > & CRC_16_T10DIF()
Returns a set of parameters for CRC-16 T10-DIF.
Definition: CRC.h:1704
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
Definition: CRC.h:376