CRC++ 1.0.1.0
Easy to use and fast C++ CRC library.
Loading...
Searching...
No Matches
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
75# define crcpp_uint8 ::std::uint8_t
76# else
78# define crcpp_uint8 uint8_t
79# endif
80#endif
81
82#ifndef crcpp_uint16
83# ifdef CRCPP_USE_CPP11
85# define crcpp_uint16 ::std::uint16_t
86# else
88# define crcpp_uint16 uint16_t
89# endif
90#endif
91
92#ifndef crcpp_uint32
93# ifdef CRCPP_USE_CPP11
95# define crcpp_uint32 ::std::uint32_t
96# else
98# define crcpp_uint32 uint32_t
99# endif
100#endif
101
102#ifndef crcpp_uint64
103# ifdef CRCPP_USE_CPP11
105# define crcpp_uint64 ::std::uint64_t
106# else
108# define crcpp_uint64 uint64_t
109# endif
110#endif
111
112#ifndef crcpp_size
113# ifdef CRCPP_USE_CPP11
115# define crcpp_size ::std::size_t
116# else
118# define crcpp_size size_t
119# endif
120#endif
121
122#ifdef CRCPP_USE_CPP11
124# define crcpp_constexpr constexpr
125#else
127# define crcpp_constexpr const
128#endif
129
130#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
131/* Disable warning C4127: conditional expression is constant. */
132#pragma warning(push)
133#pragma warning(disable : 4127)
134#endif
135
136#ifdef CRCPP_USE_NAMESPACE
137namespace CRCPP
138{
139#endif
140
148class CRC
149{
150public:
151 // Forward declaration
152 template <typename CRCType, crcpp_uint16 CRCWidth>
153 struct Table;
154
158 template <typename CRCType, crcpp_uint16 CRCWidth>
160 {
161 CRCType polynomial;
162 CRCType initialValue;
163 CRCType finalXOR;
166
167 Table<CRCType, CRCWidth> MakeTable() const;
168 };
169
174 template <typename CRCType, crcpp_uint16 CRCWidth>
175 struct Table
176 {
177 // Constructors are intentionally NOT marked explicit.
178 Table(const Parameters<CRCType, CRCWidth> & parameters);
179
180#ifdef CRCPP_USE_CPP11
182#endif
183
184 const Parameters<CRCType, CRCWidth> & GetParameters() const;
185
186 const CRCType * GetTable() const;
187
188 CRCType operator[](unsigned char index) const;
189
190 private:
191 void InitTable();
192
194 CRCType table[1 << CHAR_BIT];
195 };
196
197 // The number of bits in CRCType must be at least as large as CRCWidth.
198 // CRCType must be an unsigned integer type or a custom type with operator overloads.
199 template <typename CRCType, crcpp_uint16 CRCWidth>
200 static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
201
202 template <typename CRCType, crcpp_uint16 CRCWidth>
203 static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
204
205 template <typename CRCType, crcpp_uint16 CRCWidth>
206 static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
207
208 template <typename CRCType, crcpp_uint16 CRCWidth>
209 static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
210
211 template <typename CRCType, crcpp_uint16 CRCWidth>
212 static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
213
214 template <typename CRCType, crcpp_uint16 CRCWidth>
215 static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
216
217 template <typename CRCType, crcpp_uint16 CRCWidth>
218 static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
219
220 template <typename CRCType, crcpp_uint16 CRCWidth>
221 static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
222
223 // Common CRCs up to 64 bits.
224 // Note: Check values are the computed CRCs when given an ASCII input of "123456789" (without null terminator)
225#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
226 static const Parameters< crcpp_uint8, 4> & CRC_4_ITU();
227 static const Parameters< crcpp_uint8, 5> & CRC_5_EPC();
228 static const Parameters< crcpp_uint8, 5> & CRC_5_ITU();
229 static const Parameters< crcpp_uint8, 5> & CRC_5_USB();
230 static const Parameters< crcpp_uint8, 6> & CRC_6_CDMA2000A();
231 static const Parameters< crcpp_uint8, 6> & CRC_6_CDMA2000B();
232 static const Parameters< crcpp_uint8, 6> & CRC_6_ITU();
233 static const Parameters< crcpp_uint8, 6> & CRC_6_NR();
234 static const Parameters< crcpp_uint8, 7> & CRC_7();
235#endif
236 static const Parameters< crcpp_uint8, 8> & CRC_8();
237#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
238 static const Parameters< crcpp_uint8, 8> & CRC_8_EBU();
239 static const Parameters< crcpp_uint8, 8> & CRC_8_HDLC();
240 static const Parameters< crcpp_uint8, 8> & CRC_8_MAXIM();
241 static const Parameters< crcpp_uint8, 8> & CRC_8_WCDMA();
242 static const Parameters< crcpp_uint8, 8> & CRC_8_LTE();
243 static const Parameters<crcpp_uint16, 10> & CRC_10();
244 static const Parameters<crcpp_uint16, 10> & CRC_10_CDMA2000();
245 static const Parameters<crcpp_uint16, 11> & CRC_11();
246 static const Parameters<crcpp_uint16, 11> & CRC_11_NR();
247 static const Parameters<crcpp_uint16, 12> & CRC_12_CDMA2000();
248 static const Parameters<crcpp_uint16, 12> & CRC_12_DECT();
249 static const Parameters<crcpp_uint16, 12> & CRC_12_UMTS();
250 static const Parameters<crcpp_uint16, 13> & CRC_13_BBC();
251 static const Parameters<crcpp_uint16, 15> & CRC_15();
252 static const Parameters<crcpp_uint16, 15> & CRC_15_MPT1327();
253#endif
254 static const Parameters<crcpp_uint16, 16> & CRC_16_ARC();
255 static const Parameters<crcpp_uint16, 16> & CRC_16_BUYPASS();
256 static const Parameters<crcpp_uint16, 16> & CRC_16_CCITTFALSE();
257 static const Parameters<crcpp_uint16, 16> & CRC_16_MCRF4XX();
258#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
259 static const Parameters<crcpp_uint16, 16> & CRC_16_CDMA2000();
260 static const Parameters<crcpp_uint16, 16> & CRC_16_CMS();
261 static const Parameters<crcpp_uint16, 16> & CRC_16_DECTR();
262 static const Parameters<crcpp_uint16, 16> & CRC_16_DECTX();
263 static const Parameters<crcpp_uint16, 16> & CRC_16_DNP();
264#endif
265 static const Parameters<crcpp_uint16, 16> & CRC_16_GENIBUS();
266 static const Parameters<crcpp_uint16, 16> & CRC_16_KERMIT();
267#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
268 static const Parameters<crcpp_uint16, 16> & CRC_16_MAXIM();
269 static const Parameters<crcpp_uint16, 16> & CRC_16_MODBUS();
270 static const Parameters<crcpp_uint16, 16> & CRC_16_T10DIF();
271 static const Parameters<crcpp_uint16, 16> & CRC_16_USB();
272#endif
273 static const Parameters<crcpp_uint16, 16> & CRC_16_X25();
274 static const Parameters<crcpp_uint16, 16> & CRC_16_XMODEM();
275#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
276 static const Parameters<crcpp_uint32, 17> & CRC_17_CAN();
277 static const Parameters<crcpp_uint32, 21> & CRC_21_CAN();
278 static const Parameters<crcpp_uint32, 24> & CRC_24();
279 static const Parameters<crcpp_uint32, 24> & CRC_24_FLEXRAYA();
280 static const Parameters<crcpp_uint32, 24> & CRC_24_FLEXRAYB();
281 static const Parameters<crcpp_uint32, 24> & CRC_24_LTEA();
282 static const Parameters<crcpp_uint32, 24> & CRC_24_LTEB();
283 static const Parameters<crcpp_uint32, 24> & CRC_24_NRC();
284 static const Parameters<crcpp_uint32, 30> & CRC_30();
285#endif
286 static const Parameters<crcpp_uint32, 32> & CRC_32();
287 static const Parameters<crcpp_uint32, 32> & CRC_32_BZIP2();
288#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
289 static const Parameters<crcpp_uint32, 32> & CRC_32_C();
290#endif
291 static const Parameters<crcpp_uint32, 32> & CRC_32_MPEG2();
292 static const Parameters<crcpp_uint32, 32> & CRC_32_POSIX();
293#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
294 static const Parameters<crcpp_uint32, 32> & CRC_32_Q();
295 static const Parameters<crcpp_uint64, 40> & CRC_40_GSM();
296 static const Parameters<crcpp_uint64, 64> & CRC_64();
297#endif
298
299#ifdef CRCPP_USE_CPP11
300 CRC() = delete;
301 CRC(const CRC & other) = delete;
302 CRC & operator=(const CRC & other) = delete;
303 CRC(CRC && other) = delete;
304 CRC & operator=(CRC && other) = delete;
305#endif
306
307private:
308#ifndef CRCPP_USE_CPP11
309 CRC();
310 CRC(const CRC & other);
311 CRC & operator=(const CRC & other);
312#endif
313
314 template <typename IntegerType>
315 static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits);
316
317 template <typename CRCType, crcpp_uint16 CRCWidth>
318 static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
319
320 template <typename CRCType, crcpp_uint16 CRCWidth>
321 static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
322
323 template <typename CRCType, crcpp_uint16 CRCWidth>
324 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
325
326 template <typename CRCType, crcpp_uint16 CRCWidth>
327 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder);
328
329 template <typename CRCType, crcpp_uint16 CRCWidth>
330 static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
331};
332
341template <typename CRCType, crcpp_uint16 CRCWidth>
343{
344 // This should take advantage of RVO and optimize out the copy.
345 return CRC::Table<CRCType, CRCWidth>(*this);
346}
347
354template <typename CRCType, crcpp_uint16 CRCWidth>
360
361#ifdef CRCPP_USE_CPP11
368template <typename CRCType, crcpp_uint16 CRCWidth>
374#endif
375
382template <typename CRCType, crcpp_uint16 CRCWidth>
384{
385 return parameters;
386}
387
394template <typename CRCType, crcpp_uint16 CRCWidth>
396{
397 return table;
398}
399
407template <typename CRCType, crcpp_uint16 CRCWidth>
408inline CRCType CRC::Table<CRCType, CRCWidth>::operator[](unsigned char index) const
409{
410 return table[index];
411}
412
418template <typename CRCType, crcpp_uint16 CRCWidth>
420{
421 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
423 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
424
425 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
426 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
427
428 CRCType crc;
429 unsigned char byte = 0;
430
431 // Loop over each dividend (each possible number storable in an unsigned char)
432 do
433 {
434 crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte, sizeof(byte), parameters, CRCType(0));
435
436 // This mask might not be necessary; all unit tests pass with this line commented out,
437 // but that might just be a coincidence based on the CRC parameters used for testing.
438 // In any case, this is harmless to leave in and only adds a single machine instruction per loop iteration.
439 crc &= BIT_MASK;
440
441 if (!parameters.reflectInput && CRCWidth < CHAR_BIT)
442 {
443 // Undo the special operation at the end of the CalculateRemainder()
444 // function for non-reflected CRCs < CHAR_BIT.
445 crc = static_cast<CRCType>(crc << SHIFT);
446 }
447
448 table[byte] = crc;
449 }
450 while (++byte);
451}
452
462template <typename CRCType, crcpp_uint16 CRCWidth>
464{
465 CRCType remainder = CalculateRemainder(data, size, parameters, parameters.initialValue);
466
467 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
468
469 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
470}
482template <typename CRCType, crcpp_uint16 CRCWidth>
484{
485 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
486
488
489 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
490
491 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
492}
493
503template <typename CRCType, crcpp_uint16 CRCWidth>
505{
507
508 CRCType remainder = CalculateRemainder(data, size, lookupTable, parameters.initialValue);
509
510 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
511
512 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
513}
514
526template <typename CRCType, crcpp_uint16 CRCWidth>
528{
530
531 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
532
534
535 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
536
537 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
538}
539
549template <typename CRCType, crcpp_uint16 CRCWidth>
551{
552 CRCType remainder = parameters.initialValue;
553
554 // Calculate the remainder on a whole number of bytes first, then call
555 // a special-case function for the remaining bits.
557 if (wholeNumberOfBytes > 0)
558 {
560 }
561
563 if (remainingNumberOfBits != 0)
564 {
565 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
567 }
568
569 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
570
571 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
572}
584template <typename CRCType, crcpp_uint16 CRCWidth>
586{
587 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
588
589 // Calculate the remainder on a whole number of bytes first, then call
590 // a special-case function for the remaining bits.
592 if (wholeNumberOfBytes > 0)
593 {
594 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, parameters.initialValue);
595 }
596
598 if (remainingNumberOfBits != 0)
599 {
600 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
602 }
603
604 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
605
606 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
607}
608
618template <typename CRCType, crcpp_uint16 CRCWidth>
620{
622
623 CRCType remainder = parameters.initialValue;
624
625 // Calculate the remainder on a whole number of bytes first, then call
626 // a special-case function for the remaining bits.
628 if (wholeNumberOfBytes > 0)
629 {
631 }
632
634 if (remainingNumberOfBits != 0)
635 {
636 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
638 }
639
640 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
641
642 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
643}
644
656template <typename CRCType, crcpp_uint16 CRCWidth>
658{
660
661 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
662
663 // Calculate the remainder on a whole number of bytes first, then call
664 // a special-case function for the remaining bits.
666 if (wholeNumberOfBytes > 0)
667 {
668 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, parameters.initialValue);
669 }
670
672 if (remainingNumberOfBits > 0)
673 {
674 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
676 }
677
678 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
679
680 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
681}
682
690template <typename IntegerType>
692{
694
695 for (crcpp_uint16 i = 0; i < numBits; ++i)
696 {
697 reversedValue = static_cast<IntegerType>((reversedValue << 1) | (value & 1));
698 value = static_cast<IntegerType>(value >> 1);
699 }
700
701 return reversedValue;
702}
703
713template <typename CRCType, crcpp_uint16 CRCWidth>
714inline CRCType CRC::Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
715{
716 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
717 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
718 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
719
720 if (reflectOutput)
721 {
723 }
724
725 return (remainder ^ finalXOR) & BIT_MASK;
726}
727
745template <typename CRCType, crcpp_uint16 CRCWidth>
746inline CRCType CRC::UndoFinalize(CRCType crc, CRCType finalXOR, bool reflectOutput)
747{
748 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
749 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
750 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
751
752 crc = (crc & BIT_MASK) ^ finalXOR;
753
754 if (reflectOutput)
755 {
757 }
758
759 return crc;
760}
761
772template <typename CRCType, crcpp_uint16 CRCWidth>
774{
775#ifdef CRCPP_USE_CPP11
776 // This static_assert is put here because this function will always be compiled in no matter what
777 // the template parameters are and whether or not a table lookup or bit-by-bit algorithm is used.
778 static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth, "CRCType is too small to contain a CRC of width CRCWidth.");
779#else
780 // Catching this compile-time error is very important. Sadly, the compiler error will be very cryptic, but it's
781 // better than nothing.
782 enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
783#endif
784
785 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
786
787 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
788 // computation from the inner loop (looping over each bit) as possible.
789 if (parameters.reflectInput)
790 {
791 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
792 while (size--)
793 {
794 remainder = static_cast<CRCType>(remainder ^ *current++);
795
796 // An optimizing compiler might choose to unroll this loop.
797 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
798 {
799#ifdef CRCPP_BRANCHLESS
800 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
801 // if (remainder & 1)
802 // remainder = (remainder >> 1) ^ polynomial;
803 // else
804 // remainder >>= 1;
805 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
806#else
807 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
808#endif
809 }
810 }
811 }
812 else if (CRCWidth >= CHAR_BIT)
813 {
815#ifndef CRCPP_BRANCHLESS
817#endif
818 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
819 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
820
821 while (size--)
822 {
823 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(*current++) << SHIFT));
824
825 // An optimizing compiler might choose to unroll this loop.
826 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
827 {
828#ifdef CRCPP_BRANCHLESS
829 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
830 // if (remainder & CRC_HIGHEST_BIT_MASK)
831 // remainder = (remainder << 1) ^ parameters.polynomial;
832 // else
833 // remainder <<= 1;
834 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
835#else
836 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
837#endif
838 }
839 }
840 }
841 else
842 {
844#ifndef CRCPP_BRANCHLESS
846#endif
847 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
848 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
849
850 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
851 remainder = static_cast<CRCType>(remainder << SHIFT);
852
853 while (size--)
854 {
855 remainder = static_cast<CRCType>(remainder ^ *current++);
856
857 // An optimizing compiler might choose to unroll this loop.
858 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
859 {
860#ifdef CRCPP_BRANCHLESS
861 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
862 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
863 // remainder = (remainder << 1) ^ polynomial;
864 // else
865 // remainder <<= 1;
866 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
867#else
868 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
869#endif
870 }
871 }
872
873 remainder = static_cast<CRCType>(remainder >> SHIFT);
874 }
875
876 return remainder;
877}
878
889template <typename CRCType, crcpp_uint16 CRCWidth>
891{
892 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
893
894 if (lookupTable.GetParameters().reflectInput)
895 {
896 while (size--)
897 {
898#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
899 // Disable warning about data loss when doing (remainder >> CHAR_BIT) when
900 // remainder is one byte long. The algorithm is still correct in this case,
901 // though it's possible that one additional machine instruction will be executed.
902# pragma warning (push)
903# pragma warning (disable : 4333)
904#endif
905 remainder = static_cast<CRCType>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
906#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
907# pragma warning (pop)
908#endif
909 }
910 }
911 else if (CRCWidth >= CHAR_BIT)
912 {
913 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
914 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
915
916 while (size--)
917 {
919 }
920 }
921 else
922 {
923 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
924 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
925
926 remainder = static_cast<CRCType>(remainder << SHIFT);
927
928 while (size--)
929 {
930 // Note: no need to mask here since remainder is guaranteed to fit in a single byte.
931 remainder = lookupTable[static_cast<unsigned char>(remainder ^ *current++)];
932 }
933
934 remainder = static_cast<CRCType>(remainder >> SHIFT);
935 }
936
937 return remainder;
938}
939
940template <typename CRCType, crcpp_uint16 CRCWidth>
942{
943 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
944 // computation from the inner loop (looping over each bit) as possible.
945 if (parameters.reflectInput)
946 {
947 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
948 remainder = static_cast<CRCType>(remainder ^ byte);
949
950 // An optimizing compiler might choose to unroll this loop.
951 for (crcpp_size i = 0; i < numBits; ++i)
952 {
953#ifdef CRCPP_BRANCHLESS
954 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
955 // if (remainder & 1)
956 // remainder = (remainder >> 1) ^ polynomial;
957 // else
958 // remainder >>= 1;
959 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
960#else
961 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
962#endif
963 }
964 }
965 else if (CRCWidth >= CHAR_BIT)
966 {
968#ifndef CRCPP_BRANCHLESS
970#endif
971 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
972 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
973
974 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(byte) << SHIFT));
975
976 // An optimizing compiler might choose to unroll this loop.
977 for (crcpp_size i = 0; i < numBits; ++i)
978 {
979#ifdef CRCPP_BRANCHLESS
980 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
981 // if (remainder & CRC_HIGHEST_BIT_MASK)
982 // remainder = (remainder << 1) ^ parameters.polynomial;
983 // else
984 // remainder <<= 1;
985 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
986#else
987 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
988#endif
989 }
990 }
991 else
992 {
994#ifndef CRCPP_BRANCHLESS
996#endif
997 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
998 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
999
1000 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
1001 remainder = static_cast<CRCType>((remainder << SHIFT) ^ byte);
1002
1003 // An optimizing compiler might choose to unroll this loop.
1004 for (crcpp_size i = 0; i < numBits; ++i)
1005 {
1006#ifdef CRCPP_BRANCHLESS
1007 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1008 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
1009 // remainder = (remainder << 1) ^ polynomial;
1010 // else
1011 // remainder <<= 1;
1012 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
1013#else
1014 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
1015#endif
1016 }
1017
1018 remainder = static_cast<CRCType>(remainder >> SHIFT);
1019 }
1020
1021 return remainder;
1022}
1023
1024#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1038{
1039 static const Parameters<crcpp_uint8, 4> parameters = { 0x3, 0x0, 0x0, true, true };
1040 return parameters;
1041}
1042
1056{
1057 static const Parameters<crcpp_uint8, 5> parameters = { 0x09, 0x09, 0x00, false, false };
1058 return parameters;
1059}
1060
1074{
1075 static const Parameters<crcpp_uint8, 5> parameters = { 0x15, 0x00, 0x00, true, true };
1076 return parameters;
1077}
1078
1092{
1093 static const Parameters<crcpp_uint8, 5> parameters = { 0x05, 0x1F, 0x1F, true, true };
1094 return parameters;
1095}
1096
1110{
1111 static const Parameters<crcpp_uint8, 6> parameters = { 0x27, 0x3F, 0x00, false, false };
1112 return parameters;
1113}
1114
1128{
1129 static const Parameters<crcpp_uint8, 6> parameters = { 0x07, 0x3F, 0x00, false, false };
1130 return parameters;
1131}
1132
1146{
1147 static const Parameters<crcpp_uint8, 6> parameters = { 0x03, 0x00, 0x00, true, true };
1148 return parameters;
1149}
1150
1165{
1166 static const Parameters<crcpp_uint8, 6> parameters = { 0x21, 0x00, 0x00, false, false };
1167 return parameters;
1168}
1169
1183{
1184 static const Parameters<crcpp_uint8, 7> parameters = { 0x09, 0x00, 0x00, false, false };
1185 return parameters;
1186}
1187#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1188
1202{
1203 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0x00, 0x00, false, false };
1204 return parameters;
1205}
1206
1207#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1221{
1222 static const Parameters<crcpp_uint8, 8> parameters = { 0x1D, 0xFF, 0x00, true, true };
1223 return parameters;
1224}
1225
1239{
1240 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0xFF, 0xFF, true, true };
1241 return parameters;
1242}
1243
1257{
1258 static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0x00, 0x00, true, true };
1259 return parameters;
1260}
1261
1275{
1276 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, true, true };
1277 return parameters;
1278}
1279
1293{
1294 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, false, false };
1295 return parameters;
1296}
1297
1311{
1312 static const Parameters<crcpp_uint16, 10> parameters = { 0x233, 0x000, 0x000, false, false };
1313 return parameters;
1314}
1315
1329{
1330 static const Parameters<crcpp_uint16, 10> parameters = { 0x3D9, 0x3FF, 0x000, false, false };
1331 return parameters;
1332}
1333
1347{
1348 static const Parameters<crcpp_uint16, 11> parameters = { 0x385, 0x01A, 0x000, false, false };
1349 return parameters;
1350}
1351
1366{
1367 static const Parameters<crcpp_uint16, 11> parameters = { 0x621, 0x000, 0x000, false, false };
1368 return parameters;
1369}
1370
1384{
1385 static const Parameters<crcpp_uint16, 12> parameters = { 0xF13, 0xFFF, 0x000, false, false };
1386 return parameters;
1387}
1388
1402{
1403 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, false };
1404 return parameters;
1405}
1406
1420{
1421 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, true };
1422 return parameters;
1423}
1424
1438{
1439 static const Parameters<crcpp_uint16, 13> parameters = { 0x1CF5, 0x0000, 0x0000, false, false };
1440 return parameters;
1441}
1442
1456{
1457 static const Parameters<crcpp_uint16, 15> parameters = { 0x4599, 0x0000, 0x0000, false, false };
1458 return parameters;
1459}
1460
1474{
1475 static const Parameters<crcpp_uint16, 15> parameters = { 0x6815, 0x0000, 0x0001, false, false };
1476 return parameters;
1477}
1478#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1479
1493{
1494 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, true, true };
1495 return parameters;
1496}
1497
1511{
1512 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, false, false };
1513 return parameters;
1514}
1515
1529{
1530 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, false, false };
1531 return parameters;
1532}
1533
1547{
1548 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, true, true};
1549 return parameters;
1550}
1551
1552#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1566{
1567 static const Parameters<crcpp_uint16, 16> parameters = { 0xC867, 0xFFFF, 0x0000, false, false };
1568 return parameters;
1569}
1570
1584{
1585 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, false, false };
1586 return parameters;
1587}
1588
1602{
1603 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0001, false, false };
1604 return parameters;
1605}
1606
1620{
1621 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0000, false, false };
1622 return parameters;
1623}
1624
1638{
1639 static const Parameters<crcpp_uint16, 16> parameters = { 0x3D65, 0x0000, 0xFFFF, true, true };
1640 return parameters;
1641}
1642#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1643
1657{
1658 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, false, false };
1659 return parameters;
1660}
1661
1675{
1676 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, true, true };
1677 return parameters;
1678}
1679
1680#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1694{
1695 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0xFFFF, true, true };
1696 return parameters;
1697}
1698
1712{
1713 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, true, true };
1714 return parameters;
1715}
1716
1730{
1731 static const Parameters<crcpp_uint16, 16> parameters = { 0x8BB7, 0x0000, 0x0000, false, false };
1732 return parameters;
1733}
1734
1748{
1749 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0xFFFF, true, true };
1750 return parameters;
1751}
1752
1753#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1754
1768{
1769 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, true, true };
1770 return parameters;
1771}
1772
1786{
1787 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, false, false };
1788 return parameters;
1789}
1790
1791#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1805{
1806 static const Parameters<crcpp_uint32, 17> parameters = { 0x1685B, 0x00000, 0x00000, false, false };
1807 return parameters;
1808}
1809
1823{
1824 static const Parameters<crcpp_uint32, 21> parameters = { 0x102899, 0x000000, 0x000000, false, false };
1825 return parameters;
1826}
1827
1841{
1842 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0xB704CE, 0x000000, false, false };
1843 return parameters;
1844}
1845
1859{
1860 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xFEDCBA, 0x000000, false, false };
1861 return parameters;
1862}
1863
1877{
1878 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xABCDEF, 0x000000, false, false };
1879 return parameters;
1880}
1881
1896{
1897 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0x000000, 0x000000, false, false };
1898 return parameters;
1899}
1900
1915{
1916 static const Parameters<crcpp_uint32, 24> parameters = { 0x800063, 0x000000, 0x000000, false, false };
1917 return parameters;
1918}
1919
1934{
1935 static const Parameters<crcpp_uint32, 24> parameters = { 0xB2B117, 0x000000, 0x000000, false, false };
1936 return parameters;
1937}
1938
1952{
1953 static const Parameters<crcpp_uint32, 30> parameters = { 0x2030B9C7, 0x3FFFFFFF, 0x00000000, false, false };
1954 return parameters;
1955}
1956#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1957
1971{
1972 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
1973 return parameters;
1974}
1975
1989{
1990 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false };
1991 return parameters;
1992}
1993
1994#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2008{
2009 static const Parameters<crcpp_uint32, 32> parameters = { 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
2010 return parameters;
2011}
2012#endif
2013
2027{
2028 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false };
2029 return parameters;
2030}
2031
2045{
2046 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false };
2047 return parameters;
2048}
2049
2050#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2064{
2065 static const Parameters<crcpp_uint32, 32> parameters = { 0x814141AB, 0x00000000, 0x00000000, false, false };
2066 return parameters;
2067}
2068
2082{
2083 static const Parameters<crcpp_uint64, 40> parameters = { 0x0004820009, 0x0000000000, 0xFFFFFFFFFF, false, false };
2084 return parameters;
2085}
2086
2100{
2101 static const Parameters<crcpp_uint64, 64> parameters = { 0x42F0E1EBA9EA3693, 0x0000000000000000, 0x0000000000000000, false, false };
2102 return parameters;
2103}
2104#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2105
2106#ifdef CRCPP_USE_NAMESPACE
2107}
2108#endif
2109
2110#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
2111#pragma warning(pop)
2112#endif
2113
2114#endif // CRCPP_CRC_H_
#define crcpp_constexpr
Compile-time expression definition.
Definition CRC.h:124
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
Definition CRC.h:85
#define crcpp_size
Unsigned size definition, used for specifying data sizes.
Definition CRC.h:115
Static class for computing CRCs.
Definition CRC.h:149
static CRCType CalculateBits(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:550
CRC(const CRC &other)=delete
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:1401
static const Parameters< crcpp_uint32, 24 > & CRC_24_NRC()
Returns a set of parameters for CRC-24 NR-C.
Definition CRC.h:1933
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYB()
Returns a set of parameters for CRC-24 FlexRay-B.
Definition CRC.h:1876
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEB()
Returns a set of parameters for CRC-24 LTE-B/NR-B.
Definition CRC.h:1914
static const Parameters< crcpp_uint16, 10 > & CRC_10()
Returns a set of parameters for CRC-10 ITU.
Definition CRC.h:1310
static const Parameters< crcpp_uint8, 6 > & CRC_6_ITU()
Returns a set of parameters for CRC-6 ITU.
Definition CRC.h:1145
static const Parameters< crcpp_uint8, 7 > & CRC_7()
Returns a set of parameters for CRC-7 JEDEC.
Definition CRC.h:1182
static const Parameters< crcpp_uint8, 4 > & CRC_4_ITU()
Returns a set of parameters for CRC-4 ITU.
Definition CRC.h:1037
CRC & operator=(const CRC &other)=delete
static const Parameters< crcpp_uint8, 5 > & CRC_5_EPC()
Returns a set of parameters for CRC-5 EPC.
Definition CRC.h:1055
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:1785
static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Definition CRC.h:941
static const Parameters< crcpp_uint32, 30 > & CRC_30()
Returns a set of parameters for CRC-30 CDMA.
Definition CRC.h:1951
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:1601
static const Parameters< crcpp_uint32, 17 > & CRC_17_CAN()
Returns a set of parameters for CRC-17 CAN.
Definition CRC.h:1804
static const Parameters< crcpp_uint8, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
Definition CRC.h:1201
static const Parameters< crcpp_uint32, 24 > & CRC_24()
Returns a set of parameters for CRC-24 OPENPGP.
Definition CRC.h:1840
static const Parameters< crcpp_uint16, 15 > & CRC_15_MPT1327()
Returns a set of parameters for CRC-15 MPT1327.
Definition CRC.h:1473
static const Parameters< crcpp_uint32, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
Definition CRC.h:2026
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:746
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYA()
Returns a set of parameters for CRC-24 FlexRay-A.
Definition CRC.h:1858
static const Parameters< crcpp_uint16, 16 > & CRC_16_MAXIM()
Returns a set of parameters for CRC-16 MAXIM.
Definition CRC.h:1693
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:2007
static const Parameters< crcpp_uint16, 13 > & CRC_13_BBC()
Returns a set of parameters for CRC-13 BBC.
Definition CRC.h:1437
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:1256
static const Parameters< crcpp_uint8, 8 > & CRC_8_HDLC()
Returns a set of parameters for CRC-8 HDLC (ISO/IEC 13239:2002).
Definition CRC.h:1238
static const Parameters< crcpp_uint8, 8 > & CRC_8_WCDMA()
Returns a set of parameters for CRC-8 WCDMA.
Definition CRC.h:1274
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:1510
static CRCType CalculateRemainder(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Computes a CRC remainder.
Definition CRC.h:773
static const Parameters< crcpp_uint8, 6 > & CRC_6_NR()
Returns a set of parameters for CRC-6 NR.
Definition CRC.h:1164
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEA()
Returns a set of parameters for CRC-24 LTE-A/NR-A.
Definition CRC.h:1895
static const Parameters< crcpp_uint16, 15 > & CRC_15()
Returns a set of parameters for CRC-15 CAN.
Definition CRC.h:1455
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:1619
static const Parameters< crcpp_uint32, 21 > & CRC_21_CAN()
Returns a set of parameters for CRC-21 CAN.
Definition CRC.h:1822
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:1419
static const Parameters< crcpp_uint16, 16 > & CRC_16_CMS()
Returns a set of parameters for CRC-16 CMS.
Definition CRC.h:1583
static const Parameters< crcpp_uint16, 10 > & CRC_10_CDMA2000()
Returns a set of parameters for CRC-10 CDMA2000.
Definition CRC.h:1328
CRC & operator=(CRC &&other)=delete
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000B()
Returns a set of parameters for CRC-6 CDMA2000-B.
Definition CRC.h:1127
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:1674
static const Parameters< crcpp_uint16, 11 > & CRC_11()
Returns a set of parameters for CRC-11 FlexRay.
Definition CRC.h:1346
static const Parameters< crcpp_uint32, 32 > & CRC_32_Q()
Returns a set of parameters for CRC-32 Q.
Definition CRC.h:2063
static const Parameters< crcpp_uint16, 16 > & CRC_16_T10DIF()
Returns a set of parameters for CRC-16 T10-DIF.
Definition CRC.h:1729
static const Parameters< crcpp_uint16, 16 > & CRC_16_MCRF4XX()
Returns a set of parameters for CRC-16 MCRF4XX.
Definition CRC.h:1546
static const Parameters< crcpp_uint16, 12 > & CRC_12_CDMA2000()
Returns a set of parameters for CRC-12 CDMA2000.
Definition CRC.h:1383
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:1656
static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits)
Reflects (i.e. reverses the bits within) an integer value.
Definition CRC.h:691
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:1220
static const Parameters< crcpp_uint8, 5 > & CRC_5_USB()
Returns a set of parameters for CRC-5 USB.
Definition CRC.h:1091
static const Parameters< crcpp_uint16, 16 > & CRC_16_USB()
Returns a set of parameters for CRC-16 USB.
Definition CRC.h:1747
static const Parameters< crcpp_uint16, 16 > & CRC_16_MODBUS()
Returns a set of parameters for CRC-16 MODBUS.
Definition CRC.h:1711
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:1988
static const Parameters< crcpp_uint16, 16 > & CRC_16_CDMA2000()
Returns a set of parameters for CRC-16 CDMA2000.
Definition CRC.h:1565
static const Parameters< crcpp_uint16, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
Definition CRC.h:1528
static const Parameters< crcpp_uint32, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
Definition CRC.h:2044
static const Parameters< crcpp_uint16, 11 > & CRC_11_NR()
Returns a set of parameters for CRC-11 NR.
Definition CRC.h:1365
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,...
Definition CRC.h:1767
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000A()
Returns a set of parameters for CRC-6 CDMA2000-A.
Definition CRC.h:1109
static const Parameters< crcpp_uint8, 5 > & CRC_5_ITU()
Returns a set of parameters for CRC-5 ITU.
Definition CRC.h:1073
static const Parameters< crcpp_uint8, 8 > & CRC_8_LTE()
Returns a set of parameters for CRC-8 LTE.
Definition CRC.h:1292
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:463
static const Parameters< crcpp_uint16, 16 > & CRC_16_DNP()
Returns a set of parameters for CRC-16 DNP.
Definition CRC.h:1637
CRC()=delete
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:1492
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:1970
CRC(CRC &&other)=delete
static const Parameters< crcpp_uint64, 64 > & CRC_64()
Returns a set of parameters for CRC-64 ECMA.
Definition CRC.h:2099
static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Computes the final reflection and XOR of a CRC remainder.
Definition CRC.h:714
static const Parameters< crcpp_uint64, 40 > & CRC_40_GSM()
Returns a set of parameters for CRC-40 GSM.
Definition CRC.h:2081
CRC parameters.
Definition CRC.h:160
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
Definition CRC.h:342
CRCType initialValue
Initial CRC value.
Definition CRC.h:162
CRCType polynomial
CRC polynomial.
Definition CRC.h:161
CRCType finalXOR
Value to XOR with the final CRC.
Definition CRC.h:163
bool reflectInput
true to reflect all input bytes
Definition CRC.h:164
bool reflectOutput
true to reflect the output CRC (reflection occurs before the final XOR)
Definition CRC.h:165
CRC lookup table. After construction, the CRC parameters are fixed.
Definition CRC.h:176
const CRCType * GetTable() const
Gets the CRC table.
Definition CRC.h:395
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
Definition CRC.h:383
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
Definition CRC.h:408
Table(const Parameters< CRCType, CRCWidth > &parameters)
Constructs a CRC table from a set of CRC parameters.
Definition CRC.h:355
void InitTable()
Initializes a CRC table.
Definition CRC.h:419
Parameters< CRCType, CRCWidth > parameters
CRC parameters used to construct the table.
Definition CRC.h:193