58 #ifndef VESC_DRIVER_CRC_H_ 59 #define VESC_DRIVER_CRC_H_ 62 #ifdef CRCPP_USE_CPP11 73 # ifdef CRCPP_USE_CPP11 74 # define crcpp_uint8 ::std::uint8_t 77 # define crcpp_uint8 uint8_t 83 # ifdef CRCPP_USE_CPP11 84 # define crcpp_uint16 ::std::uint16_t 87 # define crcpp_uint16 uint16_t 93 # ifdef CRCPP_USE_CPP11 94 # define crcpp_uint32 ::std::uint32_t 97 # define crcpp_uint32 uint32_t 103 # ifdef CRCPP_USE_CPP11 104 # define crcpp_uint64 ::std::uint64_t 107 # define crcpp_uint64 uint64_t 113 # ifdef CRCPP_USE_CPP11 114 # define crcpp_size ::std::size_t 117 # define crcpp_size size_t 122 #ifdef CRCPP_USE_CPP11 123 # define crcpp_constexpr constexpr 126 # define crcpp_constexpr const 130 #ifdef CRCPP_USE_NAMESPACE 146 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
152 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
168 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
174 #ifdef CRCPP_USE_CPP11 180 const CRCType * GetTable()
const;
182 CRCType operator[](
unsigned char index)
const;
188 CRCType table[1 << CHAR_BIT];
193 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
196 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
199 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
202 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
207 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 218 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 235 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 244 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 252 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 262 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 267 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 273 #ifdef CRCPP_USE_CPP11 275 CRC(
const CRC & other) =
delete;
276 CRC & operator=(
const CRC & other) =
delete;
277 CRC(
CRC && other) =
delete;
278 CRC & operator=(
CRC && other) =
delete;
282 #ifndef CRCPP_USE_CPP11 285 CRC & operator=(
const CRC & other);
288 template <
typename IntegerType>
289 static IntegerType Reflect(IntegerType value,
crcpp_uint16 numBits);
291 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
292 static CRCType Finalize(CRCType remainder, CRCType finalXOR,
bool reflectOutput);
294 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
295 static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR,
bool reflectOutput);
297 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
300 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
312 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
325 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
332 #ifdef CRCPP_USE_CPP11 339 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
353 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
365 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
378 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
389 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
393 static crcpp_constexpr CRCType BIT_MASK((CRCType(1) << (CRCWidth - CRCType(1))) |
394 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
397 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
400 unsigned char byte = 0;
405 crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte,
sizeof(byte),
parameters, CRCType(0));
412 if (!
parameters.reflectInput && CRCWidth < CHAR_BIT)
416 crc =
static_cast<CRCType
>(crc << SHIFT);
433 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
453 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
474 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
497 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
518 template <
typename IntegerType>
521 IntegerType reversedValue(0);
525 reversedValue =
static_cast<IntegerType
>((reversedValue << 1) | (value & 1));
526 value =
static_cast<IntegerType
>(value >> 1);
529 return reversedValue;
541 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
542 inline CRCType
CRC::Finalize(CRCType remainder, CRCType finalXOR,
bool reflectOutput)
545 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
546 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
550 remainder =
Reflect(remainder, CRCWidth);
553 return (remainder ^ finalXOR) & BIT_MASK;
573 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
577 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
578 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
580 crc = (crc & BIT_MASK) ^ finalXOR;
600 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
603 #ifdef CRCPP_USE_CPP11 606 static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth,
"CRCType is too small to contain a CRC of width CRCWidth.");
610 enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
613 const unsigned char * current =
reinterpret_cast<const unsigned char *
>(data);
622 remainder =
static_cast<CRCType
>(remainder ^ *current++);
627 #ifdef CRCPP_BRANCHLESS 633 remainder =
static_cast<CRCType
>((remainder >> 1) ^ ((remainder & 1) * polynomial));
635 remainder =
static_cast<CRCType
>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
640 else if (CRCWidth >= CHAR_BIT)
642 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
643 #ifndef CRCPP_BRANCHLESS 644 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
647 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
651 remainder =
static_cast<CRCType
>(remainder ^ (
static_cast<CRCType
>(*current++) << SHIFT));
656 #ifdef CRCPP_BRANCHLESS 662 remainder =
static_cast<CRCType
>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.
polynomial));
664 remainder =
static_cast<CRCType
>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.
polynomial) : (remainder << 1));
672 #ifndef CRCPP_BRANCHLESS 673 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
676 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
678 CRCType polynomial =
static_cast<CRCType
>(parameters.
polynomial << SHIFT);
679 remainder =
static_cast<CRCType
>(remainder << SHIFT);
683 remainder =
static_cast<CRCType
>(remainder ^ *current++);
688 #ifdef CRCPP_BRANCHLESS 694 remainder =
static_cast<CRCType
>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
696 remainder =
static_cast<CRCType
>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
701 remainder =
static_cast<CRCType
>(remainder >> SHIFT);
717 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
720 const unsigned char * current =
reinterpret_cast<const unsigned char *
>(data);
726 #if defined(WIN32) || defined(_WIN32) || defined(WINCE) 730 # pragma warning (push) 731 # pragma warning (disable : 4333) 733 remainder =
static_cast<CRCType
>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
734 #if defined(WIN32) || defined(_WIN32) || defined(WINCE) 735 # pragma warning (pop) 739 else if (CRCWidth >= CHAR_BIT)
742 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
746 remainder =
static_cast<CRCType
>((remainder << CHAR_BIT) ^ lookupTable[static_cast<unsigned char>((remainder >> SHIFT) ^ *current++)]);
752 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
754 remainder =
static_cast<CRCType
>(remainder << SHIFT);
759 remainder = lookupTable[
static_cast<unsigned char>(remainder ^ *current++)];
762 remainder =
static_cast<CRCType
>(remainder >> SHIFT);
768 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 912 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 932 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1148 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1204 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1294 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1332 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1405 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1443 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1551 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1589 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1645 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1699 #endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1701 #ifdef CRCPP_USE_NAMESPACE 1705 #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_size
Unsigned size definition, used for specifying data sizes.
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.
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.
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
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.
#define crcpp_constexpr
Compile-time expression definition.
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)