A collection of function templates and class templates that compute various forms of Cyclic Redundancy Codes (CRCs).

Daryle Walker

1.5

Boost Software License, version 1.0

Contains the declarations (and definitions) of various kinds of CRC computation functions, function object types, and encapsulated policy types.

The sample CRC-computer types were just checked against the Catalogue of parametrised CRC algorithms. New type aliases were added where I got a standard wrong. However, the mistaken `typedef`s are still there for backwards compatibility.

There are references to the Rocksoft Model CRC Algorithm, as described within "A Painless Guide to CRC Error Detection Algorithms," linked from "CRC: A Paper On CRCs" by Ross Williams. It will be abbreviated "RMCA" in other documentation blocks.

std::size_t

The order of the modulo-2 polynomial divisor. (Width from the RMCA)

Bit-wise CRC computer.

Objects of this type compute the CRC checksum of submitted data, where said data can be entered piecemeal through several different kinds of groupings. Modulo-2 polynomial division steps are always performed bit-wise, without the use of pre-computation tables. Said division uses the altered algorithm, so any data has to be unaugmented.

The register type used for computations.

This type is used for CRC calculations and is the type for any returned checksums and returned or submitted remainders, (truncated) divisors, or XOR masks. It is a built-in unsigned integer type.

boost::uint_t< Bits >::fast
std::size_t
bit_count
Bits
A copy of Bits provided for meta-programming purposes.
value_type Return the (truncated) polynomial divisor.

Returns a representation of the polynomial divisor. The value of the 2i bit is the value of the coefficient of the polynomial's xi term. The omitted bit for x(#bit_count) term is always 1.

The bit-packed list of coefficients. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored (even any for x(#bit_count)) since they're unregulated.

value_type Return what the polynomial remainder was set to during construction.

Returns a representation of the polynomial remainder before any input has been submitted. The value of the 2i bit is the value of the coefficient of the polynomial's xi term.

The bit-packed list of coefficients. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored since they're unregulated.

value_type Return the XOR-mask used during output processing.

Returns the mask to be used during creation of a checksum. The mask is used for an exclusive-or (XOR) operation applied bit-wise to the interim remainder representation (after any reflection, if get_reflect_remainder() returns `true`).

The bit-mask. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored since they're unregulated.

bool Check if input-bytes will be reflected before processing.

Returns a whether or not a submitted byte will be "reflected" before it is used to update the interim remainder. Only the byte-wise operations process_byte, process_block, and process_bytes are affected.

bool Check if the remainder will be reflected during output processing.

Indicates if the interim remainder will be "reflected" before it is passed to the XOR-mask stage when returning a checksum.

value_type Return the remainder based from already-processed bits.

Returns a representation of the polynomial remainder after all the input submissions since construction or the last reset call. The value of the 2i bit is the value of the coefficient of the polynomial's xi term. If CRC processing gets interrupted here, retain the value returned, and use it to start up the next CRC computer where you left off (with reset(value_type) or construction). The next computer has to have its other parameters compatible with this computer.

The bit-packed list of coefficients. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored since they're unregulated. No output processing (reflection or XOR mask) has been applied to the value.

void `value_type

The (unaugmented) state of the polynomial remainder starting from this point, with no output processing applied.

` Change the interim remainder to a new value.

Changes the interim polynomial remainder to new_rem, purging any influence previously submitted input has had. The value of the 2i bit is the value of the coefficient of the polynomial's xi term.

`new_rem == this->get_interim_remainder()`

`((this->get_reflect_remainder() ? REFLECT(new_rem) : new_rem) ^ this->get_final_xor_value()) == this->checksum()`

void Change the interim remainder back to the initial value.

Changes the interim polynomial remainder to the initial remainder given during construction, purging any influence previously submitted input has had. The value of the 2i bit is the value of the coefficient of the polynomial's xi term.

`this->get_initial_remainder() == this->get_interim_remainder()`

`((this->get_reflect_remainder() ? REFLECT(this->get_initial_remainder()) : this->get_initial_remainder()) ^ this->get_final_xor_value()) == this->checksum()`

void `bool

The new input bit.

` Submit a single bit for input processing.

Updates the interim remainder with a single altered-CRC-division step.

The interim remainder is updated though a modulo-2 polynomial division, where the division steps are altered for unaugmented CRCs.

void `unsigned char

The byte containing the new input bits.

` `std::size_t

The number of bits in the byte to be read.

` Submit the lowest bit_length bits of a byte for input processing.

Updates the interim remainder with several altered-CRC-division steps. Each bit is processed separately, starting from the one at the 2bit_length - 1 place, then proceeding down to the lowest-placed bit. Any order imposed by `this->get_reflect_input()` is ignored.

0 < bit_length <= `CHAR_BIT`

The interim remainder is updated though bit_length modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

void `unsigned char

The new input byte.

` Submit a single byte for input processing.

Updates the interim remainder with a byte's worth of altered-CRC-division steps. The bits within the byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise.

The interim remainder is updated though `CHAR_BIT` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

void `void const *

The address where the memory block begins.

` `void const *

Points to one-byte past the address of the memory block's last byte, or bytes_begin if no bytes are to be read.

` Submit a memory block for input processing, iterator-pair style.

Updates the interim remainder with several bytes' worth of altered-CRC-division steps. The bits within each byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise. The bytes themselves are processed starting from the one pointed by bytes_begin until bytes_end is reached through forward iteration, treating the two pointers as if they point to `unsigned char` objects.

bytes_end has to equal bytes_begin if the latter is `NULL` or otherwise doesn't point to a valid buffer.

bytes_end, if not equal to bytes_begin, has to point within or one-byte-past the same buffer bytes_begin points into.

bytes_end has to be reachable from bytes_begin through a finite number of forward byte-pointer increments.

The interim remainder is updated though `CHAR_BIT * (((unsigned char const *) bytes_end) - ((unsigned char const *) bytes_begin))` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

void `void const *

The address where the memory block begins.

` `std::size_t

The number of bytes in the memory block.

` Submit a memory block for input processing, pointer-and-size style.

Updates the interim remainder with several bytes' worth of altered-CRC-division steps. The bits within each byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise. The bytes themselves are processed starting from the one pointed by buffer, forward-iterated (as if the pointed-to objects were of `unsigned char`) until byte_count bytes are read.

byte_count has to equal 0 if buffer is `NULL` or otherwise doesn't point to valid memory.

If buffer points within valid memory, then that block has to have at least byte_count more valid bytes allocated from that point.

The interim remainder is updated though `CHAR_BIT * byte_count` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

value_type Return the checksum of the already-processed bits.

Computes the checksum of all the submitted bits since construction or the last call to reset. The checksum will be the raw checksum, i.e. the (interim) remainder after all the modulo-2 polynomial division, plus any output processing.

Since checksums are meant to be compared, any higher-placed bits (when the bit-length of value_type exceeds #bit_count) will be set to 0.

`(this->get_reflect_remainder() ? REFLECT(this->get_interim_remainder()) : this->get_interim_remainder()) ^ this->get_final_xor_value()`

value_type

The lowest coefficients of the divisor polynomial. The highest-order coefficient is omitted and always assumed to be 1. (Poly from the RMCA)

value_type
0

The (unaugmented) initial state of the polynomial remainder. Defaults to `0` if omitted. (Init from the RMCA)

value_type
0

The (XOR) bit-mask to be applied to the output remainder, after possible reflection but before returning. Defaults to `0` (i.e. no bit changes) if omitted. (XorOut from the RMCA)

bool
false

If `true`, input bytes are read lowest-order bit first, otherwise highest-order bit first. Defaults to `false` if omitted. (RefIn from the RMCA)

bool
false

If `true`, the output remainder is reflected before the XOR-mask. Defaults to `false` if omitted. (RefOut from the RMCA)

Create a computer, separately listing each needed parameter.

Constructs a ``crc_basic`` object with at least the required parameters to a particular CRC formula to be processed upon receiving input.

`truncated_polynomial == this->get_truncated_polynominal()`

`initial_remainder == this->get_initial_remainder()`

`final_xor_value == this->get_final_xor_value()`

`reflect_input == this->get_reflect_input()`

`reflect_remainder == this->get_reflect_remainder()`

`initial_remainder == this->get_interim_remainder()`

`(reflect_remainder ? REFLECT(initial_remainder) : initial_remainder) ^ final_xor_value == this->checksum()`

std::size_t

The order of the modulo-2 polynomial divisor. (Width from the RMCA)

typename ::boost::uint_t< Bits >::fast

The lowest coefficients of the divisor polynomial. The highest-order coefficient is omitted and always assumed to be 1. Defaults to `0`, i.e. the only non-zero term is the implicit one for xBits. (Poly from the RMCA)

typename ::boost::uint_t< Bits >::fast

The (unaugmented) initial state of the polynomial remainder. Defaults to `0` if omitted. (Init from the RMCA)

typename ::boost::uint_t< Bits >::fast

The (XOR) bit-mask to be applied to the output remainder, after possible reflection but before returning. Defaults to `0` (i.e. no bit changes) if omitted. (XorOut from the RMCA)

bool

If `true`, input bytes are read lowest-order bit first, otherwise highest-order bit first. Defaults to `false` if omitted. (RefIn from the RMCA)

bool

If `true`, the output remainder is reflected before the XOR-mask. Defaults to `false` if omitted. (RefOut from the RMCA)

Table-driven CRC computer, usable as a function object.

Objects of this type compute the CRC checksum of submitted data, where said data can be entered piecemeal through several different kinds of groupings. Modulo-2 polynomial division steps are performed byte-wise, aided by the use of pre-computation tables. Said division uses the altered algorithm, so any data has to be unaugmented.

Todo

Get rid of the default value for TruncPoly. Choosing a divisor is an important decision with many factors, so a default is never useful, especially a bad one.

The register type used for computations.

This type is used for CRC calculations and is the type for any returned checksums and returned or submitted remainders, (truncated) divisors, or XOR masks. It is a built-in unsigned integer type.

boost::uint_t< Bits >::fast
std::size_t
bit_count
Bits
value_type
truncated_polynominal
TruncPoly
A copy of TruncPoly provided for meta-programming purposes.
value_type
initial_remainder
InitRem
A copy of InitRem provided for meta-programming purposes.
value_type
final_xor_value
FinalXor
A copy of FinalXor provided for meta-programming purposes.
bool
reflect_input
ReflectIn
A copy of ReflectIn provided for meta-programming purposes.
bool
reflect_remainder
ReflectRem
A copy of ReflectRem provided for meta-programming purposes.
value_type Return the (truncated) polynomial divisor.

Returns a representation of the polynomial divisor. The value of the 2i bit is the value of the coefficient of the polynomial's xi term. The omitted bit for x(#bit_count) term is always 1.

The bit-packed list of coefficients. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored (even any for x(#bit_count)) since they're unregulated.

value_type Return what the polynomial remainder was set to during construction.

Returns a representation of the polynomial remainder before any input has been submitted. The value of the 2i bit is the value of the coefficient of the polynomial's xi term.

The bit-packed list of coefficients. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored since they're unregulated.

value_type Return the XOR-mask used during output processing.

Returns the mask to be used during creation of a checksum. The mask is used for an exclusive-or (XOR) operation applied bit-wise to the interim remainder representation (after any reflection, if get_reflect_remainder() returns `true`).

The bit-mask. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored since they're unregulated.

bool Check if input-bytes will be reflected before processing.

Returns a whether or not a submitted byte will be "reflected" before it is used to update the interim remainder. Only the byte-wise operations process_byte, process_block, and process_bytes are affected.

bool Check if the remainder will be reflected during output processing.

Indicates if the interim remainder will be "reflected" before it is passed to the XOR-mask stage when returning a checksum.

value_type Return the remainder based from already-processed bits.

Returns a representation of the polynomial remainder after all the input submissions since construction or the last reset call. The value of the 2i bit is the value of the coefficient of the polynomial's xi term. If CRC processing gets interrupted here, retain the value returned, and use it to start up the next CRC computer where you left off (with reset(value_type) or construction). The next computer has to have its other parameters compatible with this computer.

The bit-packed list of coefficients. If the bit-length of value_type exceeds #bit_count, the values of higher-placed bits should be ignored since they're unregulated. No output processing (reflection or XOR mask) has been applied to the value.

void `value_typeinitial_remainder

The (unaugmented) state of the polynomial remainder starting from this point, with no output processing applied. Defaults to `this->get_initial_remainder()` if omitted.

` Change the interim remainder to either a given value or the initial one.

Changes the interim polynomial remainder to new_rem, purging any influence previously submitted input has had. The value of the 2i bit is the value of the coefficient of the polynomial's xi term.

`new_rem == this->get_interim_remainder()`

`((this->get_reflect_remainder() ? REFLECT(new_rem) : new_rem) ^ this->get_final_xor_value()) == this->checksum()`

void `unsigned char

The new input byte.

` Submit a single byte for input processing.

Updates the interim remainder with a byte's worth of altered-CRC-division steps. The bits within the byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise.

Any modulo-2 polynomial divisions may use a table of pre-computed remainder changes (as XOR masks) to speed computation when reading data byte-wise.

The interim remainder is updated though `CHAR_BIT` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

void `void const *

The address where the memory block begins.

` `void const *

Points to one-byte past the address of the memory block's last byte, or bytes_begin if no bytes are to be read.

` Submit a memory block for input processing, iterator-pair style.

Updates the interim remainder with several bytes' worth of altered-CRC-division steps. The bits within each byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise. The bytes themselves are processed starting from the one pointed by bytes_begin until bytes_end is reached through forward iteration, treating the two pointers as if they point to `unsigned char` objects.

Any modulo-2 polynomial divisions may use a table of pre-computed remainder changes (as XOR masks) to speed computation when reading data byte-wise.

bytes_end has to equal bytes_begin if the latter is `NULL` or otherwise doesn't point to a valid buffer.

bytes_end, if not equal to bytes_begin, has to point within or one-byte-past the same buffer bytes_begin points into.

bytes_end has to be reachable from bytes_begin through a finite number of forward byte-pointer increments.

The interim remainder is updated though `CHAR_BIT * (((unsigned char const *) bytes_end) - ((unsigned char const *) bytes_begin))` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

void `void const *

The address where the memory block begins.

` `std::size_t

The number of bytes in the memory block.

` Submit a memory block for input processing, pointer-and-size style.

Updates the interim remainder with several bytes' worth of altered-CRC-division steps. The bits within each byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise. The bytes themselves are processed starting from the one pointed by buffer, forward-iterated (as if the pointed-to objects were of `unsigned char`) until byte_count bytes are read.

Any modulo-2 polynomial divisions may use a table of pre-computed remainder changes (as XOR masks) to speed computation when reading data byte-wise.

byte_count has to equal 0 if buffer is `NULL` or otherwise doesn't point to valid memory.

If buffer points within valid memory, then that block has to have at least byte_count more valid bytes allocated from that point.

The interim remainder is updated though `CHAR_BIT * byte_count` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

value_type Return the checksum of the already-processed bits.

Computes the checksum of all the submitted bits since construction or the last call to reset. The checksum will be the raw checksum, i.e. the (interim) remainder after all the modulo-2 polynomial division, plus any output processing.

Since checksums are meant to be compared, any higher-placed bits (when the bit-length of value_type exceeds #bit_count) will be set to 0.

`(this->get_reflect_remainder() ? REFLECT(this->get_interim_remainder()) : this->get_interim_remainder()) ^ this->get_final_xor_value()`

void `unsigned char

The new input byte.

` Submit a single byte for input processing, suitable for the STL.

Updates the interim remainder with a byte's worth of altered-CRC-division steps. The bits within the byte are processed from the highest place down if `this->get_reflect_input()` is `false`, and lowest place up otherwise. This function is meant to present a function-object interface to code that wants to process a stream of bytes with `std::for_each` or similar range-processing algorithms. Since some of these algorithms takes their function object by value, make sure to copy back the result to this object so the updates can be remembered.

Any modulo-2 polynomial divisions may use a table of pre-computed remainder changes (as XOR masks) to speed computation when reading data byte-wise.

The interim remainder is updated though `CHAR_BIT` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

value_type Return the checksum of the already-processed bits, suitable for the STL.

Computes the checksum of all the submitted bits since construction or the last call to reset. The checksum will be the raw checksum, i.e. the (interim) remainder after all the modulo-2 polynomial division, plus any output processing. This function is meant to present a function-object interface to code that wants to receive data like `std::generate_n` or similar data-processing algorithms. Note that if this object is used as a generator multiple times without an intervening mutating operation, the same value will always be returned.

Since checksums are meant to be compared, any higher-placed bits (when the bit-length of value_type exceeds #bit_count) will be set to 0.

`(this->get_reflect_remainder() ? REFLECT(this->get_interim_remainder()) : this->get_interim_remainder()) ^ this->get_final_xor_value()`

value_type
initial_remainder

The (unaugmented) initial state of the polynomial remainder. Defaults to #initial_remainder if omitted.

Create a computer, giving an initial remainder if desired.

Constructs a ``crc_optimal`` object with a particular CRC formula to be processed upon receiving input. The initial remainder may be overridden.

`#truncated_polynominal == this->get_truncated_polynominal()`

`#initial_remainder == this->get_initial_remainder()`

`#final_xor_value == this->get_final_xor_value()`

`#reflect_input == this->get_reflect_input()`

`#reflect_remainder == this->get_reflect_remainder()`

`init_rem == this->get_interim_remainder()`

`(#reflect_remainder ? REFLECT(init_rem) : init_rem) ^ #final_xor_value == this->checksum()`

Computation type for ARC|CRC-16|CRC-IBM|CRC-16/ARC|CRC-16/LHA standard.
`crc_optimal`< 16, 0x8005, 0, 0, true, true >
Computation type for CRC-16/CCITT-FALSE standard.
`crc_optimal`< 16, 0x1021, 0xFFFF, 0, false, false >
Computation type for the CRC mistakenly called the CCITT standard.
crc_ccitt_false_t

Computation type for the actual KERMIT|CRC-16/CCITT|CRC-16/CCITT-TRUE|CRC-CCITT standard

`crc_optimal`< 16, 0x1021, 0, 0, true, true >

Computation type that I mistakenly called the XMODEM standard; it inverts both reflection parameters and reflects the truncated divisor (Don't use?!)

`crc_optimal`< 16, 0x8408, 0, 0, true, true >
Computation type for the actual XMODEM|ZMODEM|CRC-16/ACORN standard.
`crc_optimal`< 16, 0x1021, 0, 0, false, false >
Computation type for CRC-32|CRC-32/ADCCP|PKZIP standard.
`crc_optimal`< 32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true >
uint_t< Bits >::fast std::size_t

The order of the modulo-2 polynomial divisor. (Width from the RMCA)

typename ::boost::uint_t< Bits >::fast

The lowest coefficients of the divisor polynomial. The highest-order coefficient is omitted and always assumed to be 1. (Poly from the RMCA)

typename ::boost::uint_t< Bits >::fast

The (unaugmented) initial state of the polynomial remainder. (Init from the RMCA)

typename ::boost::uint_t< Bits >::fast

The (XOR) bit-mask to be applied to the output remainder, after possible reflection but before returning. (XorOut from the RMCA)

bool

If `True`, input bytes are read lowest-order bit first, otherwise highest-order bit first. (RefIn from the RMCA)

bool

If `True`, the output remainder is reflected before the XOR-mask. (RefOut from the RMCA)

void const *

The address where the memory block begins.

std::size_t

The number of bytes in the memory block.

Compute the (unaugmented) CRC of a memory block.

Computes the polynomial remainder of a CRC run, assuming that buffer and byte_count describe a memory block representing the polynomial dividend. The division steps are altered so the result directly gives a checksum, without need to augment the memory block with scratch-space bytes. The first byte is considered the highest order, going down for subsequent bytes.

Unaugmented-style CRC runs perform modulo-2 polynomial division in an altered order. The trailing Bits number of zero-valued bits needed to extracted an (unprocessed) checksum is virtually moved to near the beginning of the message. This is OK since the XOR operation is commutative and associative. It also means that you can get a checksum anytime. Since data is being read byte-wise, a table of pre-computed remainder changes (as XOR masks) can be used to speed computation.

0 < Bits <= `std::numeric_limits<uintmax_t>::digits`

The checksum, which is the last (interim) remainder plus any output processing.

uint_t< Bits >::fast std::size_t

The order of the modulo-2 polynomial divisor. (Width from the RMCA)

typename ::boost::uint_t< Bits >::fast

The lowest coefficients of the divisor polynomial. The highest-order coefficient is omitted and always assumed to be 1. (Poly from the RMCA)

void const *

The address where the memory block begins.

std::size_t

The number of bytes in the memory block.

typename uint_t< Bits >::fast
0u

The initial state of the polynomial remainder, defaulting to zero if omitted. If you are reading a memory block in multiple runs, put the return value of the previous run here. (Note that initial-remainders given by RMCA parameter lists, as Init, assume that the initial remainder is in its unaugmented state, so you would need to convert the value to make it suitable for this function. I currently don't provide a conversion routine.)

Compute the CRC of a memory block, with any augmentation provided by user.

Computes the polynomial remainder of a CRC run, assuming that buffer and byte_count describe a memory block representing the polynomial dividend. The first byte is considered the highest order, going down for subsequent bytes. Within a byte, the highest-order bit is read first (corresponding to RefIn = `False` in the RMCA). Check the other parts of this function's documentation to see how a checksum can be gained and/or used.

Augmented-style CRC runs use straight-up modulo-2 polynomial division. Since data is being read byte-wise, a table of pre-computed remainder changes (as XOR masks) can be used to speed computation.

Reading just a memory block will yield an interim remainder, and not the final checksum. To get that checksum, allocate Bits / `CHAR_BIT` bytes directly after the block and fill them with zero values, then extend byte_count to include those extra bytes. A data block is corrupt if the return value doesn't equal your separately given checksum.

Another way to perform a check is use the zero-byte extension method, but replace the zero values with your separately-given checksum. The checksum must be loaded in big-endian order. Here corruption, in either the data block or the given checksum, is confirmed if the return value is not zero.

The two checksum techniques assume the CRC-run is performed bit-wise, while this function works byte-wise. That means that the techniques can be used only if `CHAR_BIT` divides Bits evenly!

0 < Bits <= `std::numeric_limit<uintmax_t>::digits`

The interim remainder, if no augmentation is used. A special value if augmentation is used (see the notes). No output processing is done on the value. (In RMCA terms, RefOut is `False` and XorOut is `0`.)