64 #ifdef CRCPP_USE_CPP11 75 # ifdef CRCPP_USE_CPP11 76 # define crcpp_uint8 ::std::uint8_t 79 # define crcpp_uint8 uint8_t 85 # ifdef CRCPP_USE_CPP11 86 # define crcpp_uint16 ::std::uint16_t 89 # define crcpp_uint16 uint16_t 95 # ifdef CRCPP_USE_CPP11 96 # define crcpp_uint32 ::std::uint32_t 99 # define crcpp_uint32 uint32_t 105 # ifdef CRCPP_USE_CPP11 106 # define crcpp_uint64 ::std::uint64_t 109 # define crcpp_uint64 uint64_t 115 # ifdef CRCPP_USE_CPP11 116 # define crcpp_size ::std::size_t 119 # define crcpp_size size_t 124 #ifdef CRCPP_USE_CPP11 125 # define crcpp_constexpr constexpr 128 # define crcpp_constexpr const 132 #ifdef CRCPP_USE_NAMESPACE 148 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
154 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
170 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
176 #ifdef CRCPP_USE_CPP11 182 const CRCType * GetTable()
const;
184 CRCType operator[](
unsigned char index)
const;
190 CRCType table[1 << CHAR_BIT];
195 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
198 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
201 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
204 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
209 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 220 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 237 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 246 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 254 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 264 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 269 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 275 #ifdef CRCPP_USE_CPP11 277 CRC(
const CRC & other) =
delete;
278 CRC & operator=(
const CRC & other) =
delete;
279 CRC(
CRC && other) =
delete;
280 CRC & operator=(
CRC && other) =
delete;
284 #ifndef CRCPP_USE_CPP11 287 CRC & operator=(
const CRC & other);
290 template <
typename IntegerType>
291 static IntegerType Reflect(IntegerType value,
crcpp_uint16 numBits);
293 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
294 static CRCType Finalize(CRCType remainder, CRCType finalXOR,
bool reflectOutput);
296 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
297 static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR,
bool reflectOutput);
299 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
302 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
314 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
327 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
334 #ifdef CRCPP_USE_CPP11 341 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
355 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
367 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
380 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
391 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
395 static crcpp_constexpr CRCType BIT_MASK((CRCType(1) << (CRCWidth - CRCType(1))) |
396 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
399 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
402 unsigned char byte = 0;
407 crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte,
sizeof(byte),
parameters, CRCType(0));
414 if (!
parameters.reflectInput && CRCWidth < CHAR_BIT)
418 crc =
static_cast<CRCType
>(crc << SHIFT);
435 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
455 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
476 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
499 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
520 template <
typename IntegerType>
523 IntegerType reversedValue(0);
527 reversedValue =
static_cast<IntegerType
>((reversedValue << 1) | (value & 1));
528 value =
static_cast<IntegerType
>(value >> 1);
531 return reversedValue;
543 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
544 inline CRCType
CRC::Finalize(CRCType remainder, CRCType finalXOR,
bool reflectOutput)
547 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
548 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
552 remainder =
Reflect(remainder, CRCWidth);
555 return (remainder ^ finalXOR) & BIT_MASK;
575 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
579 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
580 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
582 crc = (crc & BIT_MASK) ^ finalXOR;
602 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
605 #ifdef CRCPP_USE_CPP11 608 static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth,
"CRCType is too small to contain a CRC of width CRCWidth.");
612 enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
615 const unsigned char * current =
reinterpret_cast<const unsigned char *
>(data);
624 remainder =
static_cast<CRCType
>(remainder ^ *current++);
629 #ifdef CRCPP_BRANCHLESS 635 remainder =
static_cast<CRCType
>((remainder >> 1) ^ ((remainder & 1) * polynomial));
637 remainder =
static_cast<CRCType
>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
642 else if (CRCWidth >= CHAR_BIT)
644 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
645 #ifndef CRCPP_BRANCHLESS 646 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
649 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
653 remainder =
static_cast<CRCType
>(remainder ^ (
static_cast<CRCType
>(*current++) << SHIFT));
658 #ifdef CRCPP_BRANCHLESS 664 remainder =
static_cast<CRCType
>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.
polynomial));
666 remainder =
static_cast<CRCType
>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.
polynomial) : (remainder << 1));
674 #ifndef CRCPP_BRANCHLESS 675 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
678 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
680 CRCType polynomial =
static_cast<CRCType
>(parameters.
polynomial << SHIFT);
681 remainder =
static_cast<CRCType
>(remainder << SHIFT);
685 remainder =
static_cast<CRCType
>(remainder ^ *current++);
690 #ifdef CRCPP_BRANCHLESS 696 remainder =
static_cast<CRCType
>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
698 remainder =
static_cast<CRCType
>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
703 remainder =
static_cast<CRCType
>(remainder >> SHIFT);
719 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
722 const unsigned char * current =
reinterpret_cast<const unsigned char *
>(data);
728 #if defined(WIN32) || defined(_WIN32) || defined(WINCE) 732 # pragma warning (push) 733 # pragma warning (disable : 4333) 735 remainder =
static_cast<CRCType
>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
736 #if defined(WIN32) || defined(_WIN32) || defined(WINCE) 737 # pragma warning (pop) 741 else if (CRCWidth >= CHAR_BIT)
744 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
748 remainder =
static_cast<CRCType
>((remainder << CHAR_BIT) ^ lookupTable[static_cast<unsigned char>((remainder >> SHIFT) ^ *current++)]);
754 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
756 remainder =
static_cast<CRCType
>(remainder << SHIFT);
761 remainder = lookupTable[
static_cast<unsigned char>(remainder ^ *current++)];
764 remainder =
static_cast<CRCType
>(remainder >> SHIFT);
770 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 914 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 934 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1150 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1206 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1296 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1334 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1407 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1445 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1553 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1591 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1647 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1701 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1703 #ifdef CRCPP_USE_NAMESPACE 1707 #endif // CRCPP_CRC_H_ const CRCType * GetTable() const
Gets the CRC table.
static const Parameters< crcpp_uint16, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
static const Parameters< crcpp_uint32, 32 > & CRC_32()
Returns a set of parameters for CRC-32 (aka CRC-32 ADCCP, CRC-32 PKZip).
CRCType initialValue
Initial CRC value.
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
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).
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
Parameters< CRCType, CRCWidth > parameters
CRC parameters used to construct the table.
static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Computes the final reflection and XOR of a CRC remainder.
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).
bool reflectInput
true to reflect all input bytes
static const Parameters< crcpp_uint16, 16 > & CRC_16_X25()
Returns a set of parameters for CRC-16 X-25 (aka CRC-16 IBM-SDLC, CRC-16 ISO-HDLC, CRC-16 B).
CRCType table[1<< CHAR_BIT]
CRC lookup table.
static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits)
Reflects (i.e. reverses the bits within) an integer value.
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)...
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).
void InitTable()
Initializes a CRC table.
CRCType finalXOR
Value to XOR with the final CRC.
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Undoes the process of computing the final reflection and XOR of a CRC remainder.
#define crcpp_size
Unsigned size definition, used for specifying data sizes.
CRC lookup table. After construction, the CRC parameters are fixed.
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > ¶meters)
Computes a CRC.
Static class for computing CRCs.
static const Parameters< crcpp_uint32, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
static CRCType CalculateRemainder(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > ¶meters, CRCType remainder)
Computes a CRC remainder.
Table(const Parameters< CRCType, CRCWidth > ¶meters)
Constructs a CRC table from a set of CRC parameters.
CRCType polynomial
CRC polynomial.
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)...
static const Parameters< crcpp_uint32, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
static const Parameters< crcpp_uint8, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
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)...
bool reflectOutput
true to reflect the output CRC (reflection occurs before the final XOR)
#define crcpp_constexpr
Compile-time expression definition.