62 #ifdef CRCPP_USE_CPP11
73 #ifdef CRCPP_USE_CPP11
75 #define crcpp_uint8 ::std::uint8_t
78 #define crcpp_uint8 uint8_t
83 #ifdef CRCPP_USE_CPP11
85 #define crcpp_uint16 ::std::uint16_t
88 #define crcpp_uint16 uint16_t
93 #ifdef CRCPP_USE_CPP11
95 #define crcpp_uint32 ::std::uint32_t
98 #define crcpp_uint32 uint32_t
103 #ifdef CRCPP_USE_CPP11
105 #define crcpp_uint64 ::std::uint64_t
108 #define crcpp_uint64 uint64_t
113 #ifdef CRCPP_USE_CPP11
115 #define crcpp_size ::std::size_t
118 #define crcpp_size size_t
122 #ifdef CRCPP_USE_CPP11
124 #define crcpp_constexpr constexpr
127 #define crcpp_constexpr const
130 #ifdef CRCPP_USE_NAMESPACE
146 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
struct Table;
151 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
struct Parameters
166 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
struct Table
171 #ifdef CRCPP_USE_CPP11
177 const CRCType *GetTable()
const;
179 CRCType operator[](
unsigned char index )
const;
185 CRCType table[1 << CHAR_BIT];
190 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
193 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
197 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
200 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
206 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
217 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
234 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
242 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
250 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
260 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
265 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
271 #ifdef CRCPP_USE_CPP11
273 CRC(
const CRC &other ) =
delete;
274 CRC &operator=(
const CRC &other ) =
delete;
275 CRC(
CRC &&other ) =
delete;
276 CRC &operator=(
CRC &&other ) =
delete;
280 #ifndef CRCPP_USE_CPP11
283 CRC &operator=(
const CRC &other );
286 template <
typename IntegerType>
static IntegerType Reflect( IntegerType value,
crcpp_uint16 numBits );
288 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
289 static CRCType Finalize( CRCType remainder, CRCType finalXOR,
bool reflectOutput );
291 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
292 static CRCType UndoFinalize( CRCType remainder, CRCType finalXOR,
bool reflectOutput );
294 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
295 static CRCType CalculateRemainder(
const void *data,
crcpp_size size,
298 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
302 template <
typename IntegerType>
static crcpp_constexpr IntegerType BoundedConstexprValue( IntegerType x );
313 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
326 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
328 parameters( parameters )
333 #ifdef CRCPP_USE_CPP11
340 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
342 parameters( ::
std::move( parameters ) )
354 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
366 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
379 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 ) ) );
396 static crcpp_constexpr CRCType SHIFT( CRC::BoundedConstexprValue( CHAR_BIT - CRCWidth ) );
399 unsigned char byte = 0;
404 crc = CRC::CalculateRemainder<CRCType, CRCWidth>( &
byte,
sizeof(
byte ), parameters, CRCType( 0 ) );
411 if ( !parameters.reflectInput && CRCWidth < CHAR_BIT )
431 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
434 CRCType remainder = CalculateRemainder( data,
size, parameters, parameters.
initialValue );
438 return Finalize<CRCType, CRCWidth>( remainder, parameters.
finalXOR,
452 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
456 CRCType remainder = UndoFinalize<CRCType, CRCWidth>( crc, parameters.
finalXOR,
459 remainder = CalculateRemainder( data,
size, parameters, remainder );
463 return Finalize<CRCType, CRCWidth>( remainder, parameters.
finalXOR,
476 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
481 CRCType remainder = CalculateRemainder( data,
size, lookupTable, parameters.
initialValue );
485 return Finalize<CRCType, CRCWidth>( remainder, parameters.
finalXOR,
500 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
506 CRCType remainder = UndoFinalize<CRCType, CRCWidth>( crc, parameters.
finalXOR,
509 remainder = CalculateRemainder( data,
size, lookupTable, remainder );
513 return Finalize<CRCType, CRCWidth>( remainder, parameters.
finalXOR,
524 template <
typename IntegerType>
inline IntegerType CRC::Reflect( IntegerType value,
crcpp_uint16 numBits )
526 IntegerType reversedValue( 0 );
530 reversedValue = ( reversedValue << 1 ) | ( value & 1 );
534 return reversedValue;
546 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
547 inline CRCType CRC::Finalize( CRCType remainder, CRCType finalXOR,
bool reflectOutput )
550 static crcpp_constexpr CRCType BIT_MASK = ( CRCType( 1 ) << ( CRCWidth - CRCType( 1 ) ) ) |
551 ( ( CRCType( 1 ) << ( CRCWidth - CRCType( 1 ) ) ) - CRCType( 1 ) );
555 remainder = Reflect( remainder, CRCWidth );
558 return ( remainder ^ finalXOR ) & BIT_MASK;
579 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
580 inline CRCType CRC::UndoFinalize( CRCType crc, CRCType finalXOR,
bool reflectOutput )
583 static crcpp_constexpr CRCType BIT_MASK = ( CRCType( 1 ) << ( CRCWidth - CRCType( 1 ) ) ) |
584 ( ( CRCType( 1 ) << ( CRCWidth - CRCType( 1 ) ) ) - CRCType( 1 ) );
586 crc = ( crc & BIT_MASK ) ^ finalXOR;
590 crc = Reflect( crc, CRCWidth );
607 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
608 inline CRCType CRC::CalculateRemainder(
const void *data,
crcpp_size size,
611 #ifdef CRCPP_USE_CPP11
614 static_assert( ::std::numeric_limits<CRCType>::digits >= CRCWidth,
615 "CRCType is too small to contain a CRC of width CRCWidth." );
621 static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth =
622 1 / ( ::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0 )
626 const unsigned char *current =
reinterpret_cast<const unsigned char *
>( data );
630 if ( parameters.reflectInput )
632 CRCType polynomial = CRC::Reflect( parameters.polynomial, CRCWidth );
635 remainder ^= *current++;
640 #ifdef CRCPP_BRANCHLESS
646 remainder = ( remainder >> 1 ) ^ ( ( remainder & 1 ) * polynomial );
648 remainder = ( remainder & 1 ) ? ( ( remainder >> 1 ) ^ polynomial ) : ( remainder >> 1 );
653 else if ( CRCWidth >= CHAR_BIT )
655 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE( CRCWidth - CRCType( 1 ) );
656 #ifndef CRCPP_BRANCHLESS
657 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK( CRCType( 1 ) << CRC_WIDTH_MINUS_ONE );
663 remainder ^= (
static_cast<CRCType
>( *current++ ) << SHIFT );
668 #ifdef CRCPP_BRANCHLESS
675 ( remainder << 1 ) ^ ( ( ( remainder >> CRC_WIDTH_MINUS_ONE ) & 1 ) * parameters.polynomial );
677 remainder = ( remainder & CRC_HIGHEST_BIT_MASK ) ? ( ( remainder << 1 ) ^ parameters.polynomial )
678 : ( remainder << 1 );
686 #ifndef CRCPP_BRANCHLESS
687 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK( CRCType( 1 ) << CHAR_BIT_MINUS_ONE );
691 CRCType polynomial = parameters.polynomial <<
SHIFT;
696 remainder ^= *current++;
701 #ifdef CRCPP_BRANCHLESS
707 remainder = ( remainder << 1 ) ^ ( ( ( remainder >> CHAR_BIT_MINUS_ONE ) & 1 ) * polynomial );
710 ( remainder & CHAR_BIT_HIGHEST_BIT_MASK ) ? ( ( remainder << 1 ) ^ polynomial ) : ( remainder << 1 );
732 template <
typename CRCType, crcpp_u
int16 CRCW
idth>
733 inline CRCType CRC::CalculateRemainder(
const void *data,
crcpp_size size,
736 const unsigned char *current =
reinterpret_cast<const unsigned char *
>( data );
738 if ( lookupTable.GetParameters().reflectInput )
742 #if defined( WIN32 ) || defined( _WIN32 ) || defined( WINCE )
746 #pragma warning( push )
747 #pragma warning( disable : 4333 )
749 remainder = ( remainder >> CHAR_BIT ) ^ lookupTable[
static_cast<unsigned char>( remainder ^ *current++ )];
750 #if defined( WIN32 ) || defined( _WIN32 ) || defined( WINCE )
751 #pragma warning( pop )
755 else if ( CRCWidth >= CHAR_BIT )
761 remainder = ( remainder << CHAR_BIT ) ^
762 lookupTable[
static_cast<unsigned char>( ( remainder >> SHIFT ) ^ *current++ )];
774 remainder = lookupTable[
static_cast<unsigned char>( remainder ^ *current++ )];
793 template <
typename IntegerType>
inline crcpp_constexpr IntegerType CRC::BoundedConstexprValue( IntegerType x )
795 return ( x < IntegerType( 0 ) ) ? IntegerType( 0 ) : x;
798 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
962 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1234 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1344 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1454 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1600 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1656 #ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1708 0x0000000000000000,
false,
false };
1713 #ifdef CRCPP_USE_NAMESPACE
#define crcpp_constexpr
Compile-time expression definition.
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
#define crcpp_size
Unsigned size definition, used for specifying data sizes.
Static class for computing CRCs.
static CRCType Calculate(const void *data, size_t size, const Parameters< CRCType, CRCWidth > ¶meters)
Computes a CRC.
static const Parameters< uint8_t, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
static const Parameters< uint16_t, 16 > & CRC_16_KERMIT()
Returns a set of parameters for CRC-16 KERMIT (aka CRC-16 CCITT, CRC-16 CCITT-TRUE).
static const Parameters< uint32_t, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
static const Parameters< uint32_t, 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< uint16_t, 16 > & CRC_16_BUYPASS()
Returns a set of parameters for CRC-16 BUYPASS (aka CRC-16 VERIFONE, CRC-16 UMTS).
static const Parameters< uint32_t, 32 > & CRC_32()
Returns a set of parameters for CRC-32 (aka CRC-32 ADCCP, CRC-32 PKZip).
static const Parameters< uint16_t, 16 > & CRC_16_X25()
Returns a set of parameters for CRC-16 X-25 (aka CRC-16 IBM-SDLC, CRC-16 ISO-HDLC,...
static const Parameters< uint16_t, 16 > & CRC_16_GENIBUS()
Returns a set of parameters for CRC-16 GENIBUS (aka CRC-16 EPC, CRC-16 I-CODE, CRC-16 DARC).
static const Parameters< uint16_t, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
static const Parameters< uint32_t, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
static const Parameters< uint16_t, 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< uint16_t, 16 > & CRC_16_ARC()
Returns a set of parameters for CRC-16 ARC (aka CRC-16 IBM, CRC-16 LHA).
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
CRCType initialValue
Initial CRC value.
CRCType polynomial
CRC polynomial.
CRCType finalXOR
Value to XOR with the final CRC.
bool reflectInput
true to reflect all input bytes
bool reflectOutput
true to reflect the output CRC (reflection occurs before the final XOR)
CRC lookup table. After construction, the CRC parameters are fixed.
const CRCType * GetTable() const
Gets the CRC table.
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
Table(const Parameters< CRCType, CRCWidth > ¶meters)
Constructs a CRC table from a set of CRC parameters.