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
37
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#if __cplusplus >= 201103L && !defined(CRCPP_USE_CPP11)
62#define CRCPP_USE_CPP11
63#endif
64
65#include <climits> // Includes CHAR_BIT
66#ifdef CRCPP_USE_CPP11
67#include <cstddef> // Includes ::std::size_t
68#include <cstdint> // Includes ::std::uint8_t, ::std::uint16_t, ::std::uint32_t, ::std::uint64_t
69#else
70#include <stddef.h> // Includes size_t
71#include <stdint.h> // Includes uint8_t, uint16_t, uint32_t, uint64_t
72#endif
73#include <limits> // Includes ::std::numeric_limits
74#include <utility> // Includes ::std::move
75
76#ifndef crcpp_uint8
77# ifdef CRCPP_USE_CPP11
79# define crcpp_uint8 ::std::uint8_t
80# else
82# define crcpp_uint8 uint8_t
83# endif
84#endif
85
86#ifndef crcpp_uint16
87# ifdef CRCPP_USE_CPP11
89# define crcpp_uint16 ::std::uint16_t
90# else
92# define crcpp_uint16 uint16_t
93# endif
94#endif
95
96#ifndef crcpp_uint32
97# ifdef CRCPP_USE_CPP11
99# define crcpp_uint32 ::std::uint32_t
100# else
102# define crcpp_uint32 uint32_t
103# endif
104#endif
105
106#ifndef crcpp_uint64
107# ifdef CRCPP_USE_CPP11
109# define crcpp_uint64 ::std::uint64_t
110# else
112# define crcpp_uint64 uint64_t
113# endif
114#endif
115
116#ifndef crcpp_size
117# ifdef CRCPP_USE_CPP11
119# define crcpp_size ::std::size_t
120# else
122# define crcpp_size size_t
123# endif
124#endif
125
126#ifdef CRCPP_USE_CPP11
128# define crcpp_constexpr constexpr
129#else
131# define crcpp_constexpr const
132#endif
133
134#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
135/* Disable warning C4127: conditional expression is constant. */
136#pragma warning(push)
137#pragma warning(disable : 4127)
138#endif
139
140#ifdef CRCPP_USE_NAMESPACE
141namespace CRCPP
142{
143#endif
144
145#ifdef CRCPP_USE_CPP11
150crcpp_constexpr char CRCPP_COPYRIGHT[] = "Copyright (c) 2022-2026, Daniel Bahr";
151#else
152#define CRCPP_MAJOR_VERSION 1
153#define CRCPP_MINOR_VERSION 2
154#define CRCPP_PATCH_VERSION 2
155#define CRCPP_REVISION_VERSION 0
156#define CRCPP_COPYRIGHT "Copyright (c) 2022-2026, Daniel Bahr"
157#endif
158
166class CRC
167{
168public:
169 // Forward declaration
170 template <typename CRCType, crcpp_uint16 CRCWidth>
171 struct Table;
172
176 template <typename CRCType, crcpp_uint16 CRCWidth>
178 {
179 CRCType polynomial;
180 CRCType initialValue;
181 CRCType finalXOR;
184
186 };
187
192 template <typename CRCType, crcpp_uint16 CRCWidth>
193 struct Table
194 {
195 // Constructors are intentionally NOT marked explicit.
197
198#ifdef CRCPP_USE_CPP11
200#endif
201
203
204 const CRCType * GetTable() const;
205
206 CRCType operator[](unsigned char index) const;
207
208 private:
209 void InitTable();
210
212 CRCType table[1 << CHAR_BIT];
213 };
214
215 // The number of bits in CRCType must be at least as large as CRCWidth.
216 // CRCType must be an unsigned integer type or a custom type with operator overloads.
217 template <typename CRCType, crcpp_uint16 CRCWidth>
218 static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
219
220 template <typename CRCType, crcpp_uint16 CRCWidth>
221 static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
222
223 template <typename CRCType, crcpp_uint16 CRCWidth>
224 static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
225
226 template <typename CRCType, crcpp_uint16 CRCWidth>
227 static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
228
229 template <typename CRCType, crcpp_uint16 CRCWidth>
230 static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
231
232 template <typename CRCType, crcpp_uint16 CRCWidth>
233 static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
234
235 template <typename CRCType, crcpp_uint16 CRCWidth>
236 static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
237
238 template <typename CRCType, crcpp_uint16 CRCWidth>
239 static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
240
241 // Common CRCs up to 64 bits.
242 // Note: Check values are the computed CRCs when given an ASCII input of "123456789" (without null terminator)
243#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
244 static const Parameters< crcpp_uint8, 4> & CRC_4_ITU();
245 static const Parameters< crcpp_uint8, 5> & CRC_5_EPC();
246 static const Parameters< crcpp_uint8, 5> & CRC_5_ITU();
247 static const Parameters< crcpp_uint8, 5> & CRC_5_USB();
250 static const Parameters< crcpp_uint8, 6> & CRC_6_ITU();
251 static const Parameters< crcpp_uint8, 6> & CRC_6_NR();
252 static const Parameters< crcpp_uint8, 7> & CRC_7();
253#endif
254 static const Parameters< crcpp_uint8, 8> & CRC_8();
255#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
256 static const Parameters< crcpp_uint8, 8> & CRC_8_EBU();
260 static const Parameters< crcpp_uint8, 8> & CRC_8_LTE();
262 static const Parameters<crcpp_uint16, 10> & CRC_10();
264 static const Parameters<crcpp_uint16, 11> & CRC_11();
270 static const Parameters<crcpp_uint16, 15> & CRC_15();
272#endif
277#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
283#endif
286#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
291#endif
294#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
297 static const Parameters<crcpp_uint32, 24> & CRC_24();
303 static const Parameters<crcpp_uint32, 30> & CRC_30();
304#endif
305 static const Parameters<crcpp_uint32, 32> & CRC_32();
307#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
308 static const Parameters<crcpp_uint32, 32> & CRC_32_C();
309#endif
312#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
313 static const Parameters<crcpp_uint32, 32> & CRC_32_Q();
315 static const Parameters<crcpp_uint64, 64> & CRC_64();
316#endif
317
318#ifdef CRCPP_USE_CPP11
319 CRC() = delete;
320 CRC(const CRC & other) = delete;
321 CRC & operator=(const CRC & other) = delete;
322 CRC(CRC && other) = delete;
323 CRC & operator=(CRC && other) = delete;
324#endif
325
326private:
327#ifndef CRCPP_USE_CPP11
328 CRC();
329 CRC(const CRC & other);
330 CRC & operator=(const CRC & other);
331#endif
332
333 template <typename IntegerType>
334 static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits);
335
336 template <typename CRCType, crcpp_uint16 CRCWidth>
337 static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
338
339 template <typename CRCType, crcpp_uint16 CRCWidth>
340 static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
341
342 template <typename CRCType, crcpp_uint16 CRCWidth>
343 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
344
345 template <typename CRCType, crcpp_uint16 CRCWidth>
346 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder);
347
348 template <typename CRCType, crcpp_uint16 CRCWidth>
349 static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
350};
351
360template <typename CRCType, crcpp_uint16 CRCWidth>
362{
363 // This should take advantage of RVO and optimize out the copy.
364 return CRC::Table<CRCType, CRCWidth>(*this);
365}
366
373template <typename CRCType, crcpp_uint16 CRCWidth>
379
380#ifdef CRCPP_USE_CPP11
387template <typename CRCType, crcpp_uint16 CRCWidth>
389 parameters(::std::move(params))
390{
391 InitTable();
392}
393#endif
394
401template <typename CRCType, crcpp_uint16 CRCWidth>
406
413template <typename CRCType, crcpp_uint16 CRCWidth>
414inline const CRCType * CRC::Table<CRCType, CRCWidth>::GetTable() const
415{
416 return table;
417}
418
426template <typename CRCType, crcpp_uint16 CRCWidth>
427inline CRCType CRC::Table<CRCType, CRCWidth>::operator[](unsigned char index) const
428{
429 return table[index];
430}
431
437template <typename CRCType, crcpp_uint16 CRCWidth>
439{
440 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
441 static crcpp_constexpr CRCType BIT_MASK((CRCType(1) << (CRCWidth - CRCType(1))) |
442 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
443
444 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
445 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
446
447 CRCType crc;
448 unsigned char byte = 0;
449
450 // Loop over each dividend (each possible number storable in an unsigned char)
451 do
452 {
453 crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte, sizeof(byte), parameters, CRCType(0));
454
455 // This mask might not be necessary; all unit tests pass with this line commented out,
456 // but that might just be a coincidence based on the CRC parameters used for testing.
457 // In any case, this is harmless to leave in and only adds a single machine instruction per loop iteration.
458 crc &= BIT_MASK;
459
460 if (!parameters.reflectInput && CRCWidth < CHAR_BIT)
461 {
462 // Undo the special operation at the end of the CalculateRemainder()
463 // function for non-reflected CRCs < CHAR_BIT.
464 crc = static_cast<CRCType>(crc << SHIFT);
465 }
466
467 table[byte] = crc;
468 }
469 while (++byte);
470}
471
481template <typename CRCType, crcpp_uint16 CRCWidth>
482inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
483{
484 CRCType initialValue = parameters.reflectInput ? Reflect(parameters.initialValue, CRCWidth) : parameters.initialValue;
485 CRCType remainder = CalculateRemainder(data, size, parameters, initialValue);
486
487 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
488
489 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
490}
491
502template <typename CRCType, crcpp_uint16 CRCWidth>
503inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
504{
505 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
506
507 remainder = CalculateRemainder(data, size, parameters, remainder);
508
509 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
510
511 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
512}
513
523template <typename CRCType, crcpp_uint16 CRCWidth>
524inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
525{
526 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
527
528 CRCType initialValue = parameters.reflectInput ? Reflect(parameters.initialValue, CRCWidth) : parameters.initialValue;
529 CRCType remainder = CalculateRemainder(data, size, lookupTable, initialValue);
530
531 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
532
533 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
534}
535
547template <typename CRCType, crcpp_uint16 CRCWidth>
548inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
549{
550 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
551
552 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
553
554 remainder = CalculateRemainder(data, size, lookupTable, remainder);
555
556 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
557
558 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
559}
560
570template <typename CRCType, crcpp_uint16 CRCWidth>
571inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
572{
573 CRCType remainder = parameters.initialValue;
574
575 // Calculate the remainder on a whole number of bytes first, then call
576 // a special-case function for the remaining bits.
577 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
578 if (wholeNumberOfBytes > 0)
579 {
580 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, remainder);
581 }
582
583 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
584 if (remainingNumberOfBits != 0)
585 {
586 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
587 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
588 }
589
590 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
591
592 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
593}
594
605template <typename CRCType, crcpp_uint16 CRCWidth>
606inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
607{
608 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
609
610 // Calculate the remainder on a whole number of bytes first, then call
611 // a special-case function for the remaining bits.
612 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
613 if (wholeNumberOfBytes > 0)
614 {
615 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, parameters.initialValue);
616 }
617
618 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
619 if (remainingNumberOfBits != 0)
620 {
621 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
622 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
623 }
624
625 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
626
627 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
628}
629
639template <typename CRCType, crcpp_uint16 CRCWidth>
640inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
641{
642 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
643
644 CRCType remainder = parameters.initialValue;
645
646 // Calculate the remainder on a whole number of bytes first, then call
647 // a special-case function for the remaining bits.
648 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
649 if (wholeNumberOfBytes > 0)
650 {
651 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, remainder);
652 }
653
654 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
655 if (remainingNumberOfBits != 0)
656 {
657 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
658 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
659 }
660
661 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
662
663 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
664}
665
677template <typename CRCType, crcpp_uint16 CRCWidth>
678inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
679{
680 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
681
682 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
683
684 // Calculate the remainder on a whole number of bytes first, then call
685 // a special-case function for the remaining bits.
686 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
687 if (wholeNumberOfBytes > 0)
688 {
689 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, parameters.initialValue);
690 }
691
692 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
693 if (remainingNumberOfBits > 0)
694 {
695 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
696 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
697 }
698
699 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
700
701 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
702}
703
711template <typename IntegerType>
712inline IntegerType CRC::Reflect(IntegerType value, crcpp_uint16 numBits)
713{
714 IntegerType reversedValue(0);
715
716 for (crcpp_uint16 i = 0; i < numBits; ++i)
717 {
718 reversedValue = static_cast<IntegerType>((reversedValue << 1) | (value & 1));
719 value = static_cast<IntegerType>(value >> 1);
720 }
721
722 return reversedValue;
723}
724
734template <typename CRCType, crcpp_uint16 CRCWidth>
735inline CRCType CRC::Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
736{
737 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
738 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
739 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
740
741 if (reflectOutput)
742 {
743 remainder = Reflect(remainder, CRCWidth);
744 }
745
746 return (remainder ^ finalXOR) & BIT_MASK;
747}
748
766template <typename CRCType, crcpp_uint16 CRCWidth>
767inline CRCType CRC::UndoFinalize(CRCType crc, CRCType finalXOR, bool reflectOutput)
768{
769 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
770 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
771 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
772
773 crc = (crc & BIT_MASK) ^ finalXOR;
774
775 if (reflectOutput)
776 {
777 crc = Reflect(crc, CRCWidth);
778 }
779
780 return crc;
781}
782
793template <typename CRCType, crcpp_uint16 CRCWidth>
794inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
795{
796#ifdef CRCPP_USE_CPP11
797 // This static_assert is put here because this function will always be compiled in no matter what
798 // the template parameters are and whether or not a table lookup or bit-by-bit algorithm is used.
799 static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth, "CRCType is too small to contain a CRC of width CRCWidth.");
800#else
801 // Catching this compile-time error is very important. Sadly, the compiler error will be very cryptic, but it's
802 // better than nothing.
803 enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
804#endif
805
806 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
807
808 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
809 // computation from the inner loop (looping over each bit) as possible.
810 if (parameters.reflectInput)
811 {
812 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
813 while (size--)
814 {
815 remainder = static_cast<CRCType>(remainder ^ *current++);
816
817 // An optimizing compiler might choose to unroll this loop.
818 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
819 {
820#ifdef CRCPP_BRANCHLESS
821 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
822 // if (remainder & 1)
823 // remainder = (remainder >> 1) ^ polynomial;
824 // else
825 // remainder >>= 1;
826 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
827#else
828 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
829#endif
830 }
831 }
832 }
833 else if (CRCWidth >= CHAR_BIT)
834 {
835 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
836#ifndef CRCPP_BRANCHLESS
837 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
838#endif
839 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
840 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
841
842 while (size--)
843 {
844 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(*current++) << SHIFT));
845
846 // An optimizing compiler might choose to unroll this loop.
847 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
848 {
849#ifdef CRCPP_BRANCHLESS
850 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
851 // if (remainder & CRC_HIGHEST_BIT_MASK)
852 // remainder = (remainder << 1) ^ parameters.polynomial;
853 // else
854 // remainder <<= 1;
855 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
856#else
857 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
858#endif
859 }
860 }
861 }
862 else
863 {
864 static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
865#ifndef CRCPP_BRANCHLESS
866 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
867#endif
868 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
869 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
870
871 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
872 remainder = static_cast<CRCType>(remainder << SHIFT);
873
874 while (size--)
875 {
876 remainder = static_cast<CRCType>(remainder ^ *current++);
877
878 // An optimizing compiler might choose to unroll this loop.
879 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
880 {
881#ifdef CRCPP_BRANCHLESS
882 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
883 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
884 // remainder = (remainder << 1) ^ polynomial;
885 // else
886 // remainder <<= 1;
887 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
888#else
889 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
890#endif
891 }
892 }
893
894 remainder = static_cast<CRCType>(remainder >> SHIFT);
895 }
896
897 return remainder;
898}
899
910template <typename CRCType, crcpp_uint16 CRCWidth>
911inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder)
912{
913 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
914
915 if (lookupTable.GetParameters().reflectInput)
916 {
917 while (size--)
918 {
919#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
920 // Disable warning about data loss when doing (remainder >> CHAR_BIT) when
921 // remainder is one byte long. The algorithm is still correct in this case,
922 // though it's possible that one additional machine instruction will be executed.
923# pragma warning (push)
924# pragma warning (disable : 4333)
925#endif
926 remainder = static_cast<CRCType>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
927#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
928# pragma warning (pop)
929#endif
930 }
931 }
932 else if (CRCWidth >= CHAR_BIT)
933 {
934 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
935 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
936
937 while (size--)
938 {
939 remainder = static_cast<CRCType>((remainder << CHAR_BIT) ^ lookupTable[static_cast<unsigned char>((remainder >> SHIFT) ^ *current++)]);
940 }
941 }
942 else
943 {
944 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
945 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
946
947 remainder = static_cast<CRCType>(remainder << SHIFT);
948
949 while (size--)
950 {
951 // Note: no need to mask here since remainder is guaranteed to fit in a single byte.
952 remainder = lookupTable[static_cast<unsigned char>(remainder ^ *current++)];
953 }
954
955 remainder = static_cast<CRCType>(remainder >> SHIFT);
956 }
957
958 return remainder;
959}
960
961template <typename CRCType, crcpp_uint16 CRCWidth>
962inline CRCType CRC::CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
963{
964 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
965 // computation from the inner loop (looping over each bit) as possible.
966 if (parameters.reflectInput)
967 {
968 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
969 remainder = static_cast<CRCType>(remainder ^ byte);
970
971 // An optimizing compiler might choose to unroll this loop.
972 for (crcpp_size i = 0; i < numBits; ++i)
973 {
974#ifdef CRCPP_BRANCHLESS
975 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
976 // if (remainder & 1)
977 // remainder = (remainder >> 1) ^ polynomial;
978 // else
979 // remainder >>= 1;
980 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
981#else
982 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
983#endif
984 }
985 }
986 else if (CRCWidth >= CHAR_BIT)
987 {
988 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
989#ifndef CRCPP_BRANCHLESS
990 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
991#endif
992 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
993 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
994
995 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(byte) << SHIFT));
996
997 // An optimizing compiler might choose to unroll this loop.
998 for (crcpp_size i = 0; i < numBits; ++i)
999 {
1000#ifdef CRCPP_BRANCHLESS
1001 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1002 // if (remainder & CRC_HIGHEST_BIT_MASK)
1003 // remainder = (remainder << 1) ^ parameters.polynomial;
1004 // else
1005 // remainder <<= 1;
1006 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
1007#else
1008 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
1009#endif
1010 }
1011 }
1012 else
1013 {
1014 static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
1015#ifndef CRCPP_BRANCHLESS
1016 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
1017#endif
1018 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
1019 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
1020
1021 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
1022 remainder = static_cast<CRCType>((remainder << SHIFT) ^ byte);
1023
1024 // An optimizing compiler might choose to unroll this loop.
1025 for (crcpp_size i = 0; i < numBits; ++i)
1026 {
1027#ifdef CRCPP_BRANCHLESS
1028 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1029 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
1030 // remainder = (remainder << 1) ^ polynomial;
1031 // else
1032 // remainder <<= 1;
1033 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
1034#else
1035 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
1036#endif
1037 }
1038
1039 remainder = static_cast<CRCType>(remainder >> SHIFT);
1040 }
1041
1042 return remainder;
1043}
1044
1045#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1059{
1060 static const Parameters<crcpp_uint8, 4> parameters = { 0x3, 0x0, 0x0, true, true };
1061 return parameters;
1062}
1063
1077{
1078 static const Parameters<crcpp_uint8, 5> parameters = { 0x09, 0x09, 0x00, false, false };
1079 return parameters;
1080}
1081
1095{
1096 static const Parameters<crcpp_uint8, 5> parameters = { 0x15, 0x00, 0x00, true, true };
1097 return parameters;
1098}
1099
1113{
1114 static const Parameters<crcpp_uint8, 5> parameters = { 0x05, 0x1F, 0x1F, true, true };
1115 return parameters;
1116}
1117
1131{
1132 static const Parameters<crcpp_uint8, 6> parameters = { 0x27, 0x3F, 0x00, false, false };
1133 return parameters;
1134}
1135
1149{
1150 static const Parameters<crcpp_uint8, 6> parameters = { 0x07, 0x3F, 0x00, false, false };
1151 return parameters;
1152}
1153
1167{
1168 static const Parameters<crcpp_uint8, 6> parameters = { 0x03, 0x00, 0x00, true, true };
1169 return parameters;
1170}
1171
1186{
1187 static const Parameters<crcpp_uint8, 6> parameters = { 0x21, 0x00, 0x00, false, false };
1188 return parameters;
1189}
1190
1204{
1205 static const Parameters<crcpp_uint8, 7> parameters = { 0x09, 0x00, 0x00, false, false };
1206 return parameters;
1207}
1208#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1209
1223{
1224 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0x00, 0x00, false, false };
1225 return parameters;
1226}
1227
1228#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1242{
1243 static const Parameters<crcpp_uint8, 8> parameters = { 0x1D, 0xFF, 0x00, true, true };
1244 return parameters;
1245}
1246
1260{
1261 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0xFF, 0xFF, true, true };
1262 return parameters;
1263}
1264
1278{
1279 static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0x00, 0x00, true, true };
1280 return parameters;
1281}
1282
1296{
1297 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, true, true };
1298 return parameters;
1299}
1300
1314{
1315 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, false, false };
1316 return parameters;
1317}
1318
1332{
1333 static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0xFF, 0x00, false, false };
1334 return parameters;
1335}
1336
1350{
1351 static const Parameters<crcpp_uint16, 10> parameters = { 0x233, 0x000, 0x000, false, false };
1352 return parameters;
1353}
1354
1368{
1369 static const Parameters<crcpp_uint16, 10> parameters = { 0x3D9, 0x3FF, 0x000, false, false };
1370 return parameters;
1371}
1372
1386{
1387 static const Parameters<crcpp_uint16, 11> parameters = { 0x385, 0x01A, 0x000, false, false };
1388 return parameters;
1389}
1390
1405{
1406 static const Parameters<crcpp_uint16, 11> parameters = { 0x621, 0x000, 0x000, false, false };
1407 return parameters;
1408}
1409
1423{
1424 static const Parameters<crcpp_uint16, 12> parameters = { 0xF13, 0xFFF, 0x000, false, false };
1425 return parameters;
1426}
1427
1441{
1442 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, false };
1443 return parameters;
1444}
1445
1459{
1460 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, true };
1461 return parameters;
1462}
1463
1477{
1478 static const Parameters<crcpp_uint16, 13> parameters = { 0x1CF5, 0x0000, 0x0000, false, false };
1479 return parameters;
1480}
1481
1495{
1496 static const Parameters<crcpp_uint16, 15> parameters = { 0x4599, 0x0000, 0x0000, false, false };
1497 return parameters;
1498}
1499
1513{
1514 static const Parameters<crcpp_uint16, 15> parameters = { 0x6815, 0x0000, 0x0001, false, false };
1515 return parameters;
1516}
1517#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1518
1532{
1533 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, true, true };
1534 return parameters;
1535}
1536
1550{
1551 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, false, false };
1552 return parameters;
1553}
1554
1568{
1569 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, false, false };
1570 return parameters;
1571}
1572
1586{
1587 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, true, true};
1588 return parameters;
1589}
1590
1591#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1605{
1606 static const Parameters<crcpp_uint16, 16> parameters = { 0xC867, 0xFFFF, 0x0000, false, false };
1607 return parameters;
1608}
1609
1623{
1624 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, false, false };
1625 return parameters;
1626}
1627
1641{
1642 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0001, false, false };
1643 return parameters;
1644}
1645
1659{
1660 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0000, false, false };
1661 return parameters;
1662}
1663
1677{
1678 static const Parameters<crcpp_uint16, 16> parameters = { 0x3D65, 0x0000, 0xFFFF, true, true };
1679 return parameters;
1680}
1681#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1682
1696{
1697 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, false, false };
1698 return parameters;
1699}
1700
1714{
1715 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, true, true };
1716 return parameters;
1717}
1718
1719#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1733{
1734 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0xFFFF, true, true };
1735 return parameters;
1736}
1737
1751{
1752 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, true, true };
1753 return parameters;
1754}
1755
1769{
1770 static const Parameters<crcpp_uint16, 16> parameters = { 0x8BB7, 0x0000, 0x0000, false, false };
1771 return parameters;
1772}
1773
1787{
1788 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0xFFFF, true, true };
1789 return parameters;
1790}
1791
1792#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1793
1807{
1808 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, true, true };
1809 return parameters;
1810}
1811
1825{
1826 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, false, false };
1827 return parameters;
1828}
1829
1830#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1844{
1845 static const Parameters<crcpp_uint32, 17> parameters = { 0x1685B, 0x00000, 0x00000, false, false };
1846 return parameters;
1847}
1848
1862{
1863 static const Parameters<crcpp_uint32, 21> parameters = { 0x102899, 0x000000, 0x000000, false, false };
1864 return parameters;
1865}
1866
1880{
1881 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0xB704CE, 0x000000, false, false };
1882 return parameters;
1883}
1884
1898{
1899 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xFEDCBA, 0x000000, false, false };
1900 return parameters;
1901}
1902
1916{
1917 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xABCDEF, 0x000000, false, false };
1918 return parameters;
1919}
1920
1935{
1936 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0x000000, 0x000000, false, false };
1937 return parameters;
1938}
1939
1954{
1955 static const Parameters<crcpp_uint32, 24> parameters = { 0x800063, 0x000000, 0x000000, false, false };
1956 return parameters;
1957}
1958
1973{
1974 static const Parameters<crcpp_uint32, 24> parameters = { 0xB2B117, 0x000000, 0x000000, false, false };
1975 return parameters;
1976}
1977
1991{
1992 static const Parameters<crcpp_uint32, 30> parameters = { 0x2030B9C7, 0x3FFFFFFF, 0x00000000, false, false };
1993 return parameters;
1994}
1995#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1996
2010{
2011 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
2012 return parameters;
2013}
2014
2028{
2029 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false };
2030 return parameters;
2031}
2032
2033#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2047{
2048 static const Parameters<crcpp_uint32, 32> parameters = { 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
2049 return parameters;
2050}
2051#endif
2052
2066{
2067 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false };
2068 return parameters;
2069}
2070
2084{
2085 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false };
2086 return parameters;
2087}
2088
2089#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2103{
2104 static const Parameters<crcpp_uint32, 32> parameters = { 0x814141AB, 0x00000000, 0x00000000, false, false };
2105 return parameters;
2106}
2107
2121{
2122 static const Parameters<crcpp_uint64, 40> parameters = { 0x0004820009, 0x0000000000, 0xFFFFFFFFFF, false, false };
2123 return parameters;
2124}
2125
2139{
2140 static const Parameters<crcpp_uint64, 64> parameters = { 0x42F0E1EBA9EA3693, 0x0000000000000000, 0x0000000000000000, false, false };
2141 return parameters;
2142}
2143#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2144
2145#ifdef CRCPP_USE_NAMESPACE
2146}
2147#endif
2148
2149#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
2150#pragma warning(pop)
2151#endif
2152
2153#endif // CRCPP_CRC_H_
crcpp_constexpr int CRCPP_PATCH_VERSION
Definition CRC.h:148
crcpp_constexpr int CRCPP_MAJOR_VERSION
Definition CRC.h:146
crcpp_constexpr char CRCPP_COPYRIGHT[]
Definition CRC.h:150
crcpp_constexpr int CRCPP_MINOR_VERSION
Definition CRC.h:147
#define crcpp_constexpr
Compile-time expression definition.
Definition CRC.h:128
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
Definition CRC.h:89
crcpp_constexpr int CRCPP_REVISION_VERSION
Definition CRC.h:149
#define crcpp_size
Unsigned size definition, used for specifying data sizes.
Definition CRC.h:119
static CRCType CalculateBits(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:571
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:1440
static const Parameters< crcpp_uint32, 24 > & CRC_24_NRC()
Returns a set of parameters for CRC-24 NR-C.
Definition CRC.h:1972
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYB()
Returns a set of parameters for CRC-24 FlexRay-B.
Definition CRC.h:1915
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEB()
Returns a set of parameters for CRC-24 LTE-B/NR-B.
Definition CRC.h:1953
static const Parameters< crcpp_uint16, 10 > & CRC_10()
Returns a set of parameters for CRC-10 ITU.
Definition CRC.h:1349
static const Parameters< crcpp_uint8, 6 > & CRC_6_ITU()
Returns a set of parameters for CRC-6 ITU.
Definition CRC.h:1166
static const Parameters< crcpp_uint8, 7 > & CRC_7()
Returns a set of parameters for CRC-7 JEDEC.
Definition CRC.h:1203
static const Parameters< crcpp_uint8, 4 > & CRC_4_ITU()
Returns a set of parameters for CRC-4 ITU.
Definition CRC.h:1058
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:1076
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:1824
static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Definition CRC.h:962
static const Parameters< crcpp_uint32, 30 > & CRC_30()
Returns a set of parameters for CRC-30 CDMA.
Definition CRC.h:1990
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:1640
static const Parameters< crcpp_uint32, 17 > & CRC_17_CAN()
Returns a set of parameters for CRC-17 CAN.
Definition CRC.h:1843
static const Parameters< crcpp_uint8, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
Definition CRC.h:1222
static const Parameters< crcpp_uint32, 24 > & CRC_24()
Returns a set of parameters for CRC-24 OPENPGP.
Definition CRC.h:1879
static const Parameters< crcpp_uint16, 15 > & CRC_15_MPT1327()
Returns a set of parameters for CRC-15 MPT1327.
Definition CRC.h:1512
static const Parameters< crcpp_uint32, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
Definition CRC.h:2065
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:767
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYA()
Returns a set of parameters for CRC-24 FlexRay-A.
Definition CRC.h:1897
static const Parameters< crcpp_uint16, 16 > & CRC_16_MAXIM()
Returns a set of parameters for CRC-16 MAXIM.
Definition CRC.h:1732
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:2046
static const Parameters< crcpp_uint16, 13 > & CRC_13_BBC()
Returns a set of parameters for CRC-13 BBC.
Definition CRC.h:1476
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:1277
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:1259
static const Parameters< crcpp_uint8, 8 > & CRC_8_WCDMA()
Returns a set of parameters for CRC-8 WCDMA.
Definition CRC.h:1295
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:1549
static CRCType CalculateRemainder(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Computes a CRC remainder.
Definition CRC.h:794
static const Parameters< crcpp_uint8, 6 > & CRC_6_NR()
Returns a set of parameters for CRC-6 NR.
Definition CRC.h:1185
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEA()
Returns a set of parameters for CRC-24 LTE-A/NR-A.
Definition CRC.h:1934
static const Parameters< crcpp_uint16, 15 > & CRC_15()
Returns a set of parameters for CRC-15 CAN.
Definition CRC.h:1494
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:1658
static const Parameters< crcpp_uint32, 21 > & CRC_21_CAN()
Returns a set of parameters for CRC-21 CAN.
Definition CRC.h:1861
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:1458
static const Parameters< crcpp_uint16, 16 > & CRC_16_CMS()
Returns a set of parameters for CRC-16 CMS.
Definition CRC.h:1622
static const Parameters< crcpp_uint16, 10 > & CRC_10_CDMA2000()
Returns a set of parameters for CRC-10 CDMA2000.
Definition CRC.h:1367
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:1148
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:1713
static const Parameters< crcpp_uint16, 11 > & CRC_11()
Returns a set of parameters for CRC-11 FlexRay.
Definition CRC.h:1385
static const Parameters< crcpp_uint32, 32 > & CRC_32_Q()
Returns a set of parameters for CRC-32 Q.
Definition CRC.h:2102
static const Parameters< crcpp_uint16, 16 > & CRC_16_T10DIF()
Returns a set of parameters for CRC-16 T10-DIF.
Definition CRC.h:1768
static const Parameters< crcpp_uint16, 16 > & CRC_16_MCRF4XX()
Returns a set of parameters for CRC-16 MCRF4XX.
Definition CRC.h:1585
static const Parameters< crcpp_uint16, 12 > & CRC_12_CDMA2000()
Returns a set of parameters for CRC-12 CDMA2000.
Definition CRC.h:1422
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:1695
static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits)
Reflects (i.e. reverses the bits within) an integer value.
Definition CRC.h:712
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:1241
static const Parameters< crcpp_uint8, 5 > & CRC_5_USB()
Returns a set of parameters for CRC-5 USB.
Definition CRC.h:1112
static const Parameters< crcpp_uint16, 16 > & CRC_16_USB()
Returns a set of parameters for CRC-16 USB.
Definition CRC.h:1786
static const Parameters< crcpp_uint16, 16 > & CRC_16_MODBUS()
Returns a set of parameters for CRC-16 MODBUS.
Definition CRC.h:1750
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:2027
static const Parameters< crcpp_uint16, 16 > & CRC_16_CDMA2000()
Returns a set of parameters for CRC-16 CDMA2000.
Definition CRC.h:1604
static const Parameters< crcpp_uint16, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
Definition CRC.h:1567
static const Parameters< crcpp_uint32, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
Definition CRC.h:2083
static const Parameters< crcpp_uint16, 11 > & CRC_11_NR()
Returns a set of parameters for CRC-11 NR.
Definition CRC.h:1404
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:1806
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000A()
Returns a set of parameters for CRC-6 CDMA2000-A.
Definition CRC.h:1130
static const Parameters< crcpp_uint8, 5 > & CRC_5_ITU()
Returns a set of parameters for CRC-5 ITU.
Definition CRC.h:1094
static const Parameters< crcpp_uint8, 8 > & CRC_8_LTE()
Returns a set of parameters for CRC-8 LTE.
Definition CRC.h:1313
static const Parameters< crcpp_uint8, 8 > & CRC_8_NRSC5()
Returns a set of parameters for CRC-8 NRSC-5.
Definition CRC.h:1331
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:482
static const Parameters< crcpp_uint16, 16 > & CRC_16_DNP()
Returns a set of parameters for CRC-16 DNP.
Definition CRC.h:1676
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:1531
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:2009
CRC(CRC &&other)=delete
static const Parameters< crcpp_uint64, 64 > & CRC_64()
Returns a set of parameters for CRC-64 ECMA.
Definition CRC.h:2138
static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Computes the final reflection and XOR of a CRC remainder.
Definition CRC.h:735
static const Parameters< crcpp_uint64, 40 > & CRC_40_GSM()
Returns a set of parameters for CRC-40 GSM.
Definition CRC.h:2120
CRC parameters.
Definition CRC.h:178
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
Definition CRC.h:361
CRCType initialValue
Initial CRC value.
Definition CRC.h:180
CRCType polynomial
CRC polynomial.
Definition CRC.h:179
CRCType finalXOR
Value to XOR with the final CRC.
Definition CRC.h:181
bool reflectInput
true to reflect all input bytes
Definition CRC.h:182
bool reflectOutput
true to reflect the output CRC (reflection occurs before the final XOR)
Definition CRC.h:183
CRC lookup table. After construction, the CRC parameters are fixed.
Definition CRC.h:194
const CRCType * GetTable() const
Gets the CRC table.
Definition CRC.h:414
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
Definition CRC.h:402
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
Definition CRC.h:427
Table(const Parameters< CRCType, CRCWidth > &parameters)
Constructs a CRC table from a set of CRC parameters.
Definition CRC.h:374
CRCType table[1<< CHAR_BIT]
CRC lookup table.
Definition CRC.h:212
void InitTable()
Initializes a CRC table.
Definition CRC.h:438
Parameters< CRCType, CRCWidth > parameters
CRC parameters used to construct the table.
Definition CRC.h:211