| Home | Libraries | People | FAQ | More

## Class template crc_basic

boost::crc_basic — Bit-wise CRC computer.

## Synopsis

    // In header: <boost/crc.hpp>

    template<std::size_t Bits>
    class crc_basic {
    public:
      // types
      typedef boost::uint_t< Bits >::fast value_type;

      // construct/copy/destruct
      explicit crc_basic(value_type, value_type = 0, value_type = 0, bool = false,
                         bool = false);

      // public member functions
       BOOST_STATIC_CONSTANT(std::size_t, bit_count = Bits);
      value_type get_truncated_polynominal() const;
      value_type get_initial_remainder() const;
      value_type get_final_xor_value() const;
      bool get_reflect_input() const;
      bool get_reflect_remainder() const;
      value_type get_interim_remainder() const;
      void reset(value_type);
      void reset();
      void process_bit(bool);
      void process_bits(unsigned char, std::size_t);
      void process_byte(unsigned char);
      void process_block(void const *, void const *);
      void process_bytes(void const *, std::size_t);
      value_type checksum() const;
    };

## Description

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.

### Template Parameters

1.     std::size_t Bits

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

### `crc_basic` public types

1. typedef boost::uint_t< Bits >::fast value_type;

   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.

### `crc_basic` public construct/copy/destruct

1.     explicit crc_basic(value_type truncated_polynomial,
                          value_type initial_remainder = 0,
                          value_type final_xor_value = 0,
                          bool reflect_input = false,
                          bool reflect_remainder = false);

   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.

   Parameters: |
   `final_xor_value` | 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)
   `initial_remainder` | The (unaugmented) initial state of the polynomial remainder. Defaults to `0` if omitted. (Init from the RMCA)
   `reflect_input` | If `true`, input bytes are read lowest-order bit first, otherwise highest-order bit first. Defaults to `false` if omitted. (RefIn from the RMCA)
   `reflect_remainder` | If `true`, the output remainder is reflected before the XOR-mask. Defaults to `false` if omitted. (RefOut from the RMCA)
   `truncated_polynomial` | The lowest coefficients of the divisor polynomial. The highest-order coefficient is omitted and always assumed to be 1. (Poly from the RMCA)
   Postconditions: | `truncated_polynomial == this->get_truncated_polynominal()`
   Postconditions: | `initial_remainder == this->get_initial_remainder()`
   Postconditions: | `final_xor_value == this->get_final_xor_value()`
   Postconditions: | `reflect_input == this->get_reflect_input()`
   Postconditions: | `reflect_remainder == this->get_reflect_remainder()`
   Postconditions: | `initial_remainder == this->get_interim_remainder()`
   Postconditions: | `(reflect_remainder ? REFLECT(initial_remainder) : initial_remainder) ^ final_xor_value == this->checksum()`

### `crc_basic` public member functions

1.      BOOST_STATIC_CONSTANT(std::size_t, bit_count = Bits);

   A copy of Bits provided for meta-programming purposes.

2.     value_type get_truncated_polynominal() const;

   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.

   Returns: | 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.

3.     value_type get_initial_remainder() const;

   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.

   Returns: | 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.

4.     value_type get_final_xor_value() const;

   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`).

   Returns: | 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.

5.     bool get_reflect_input() const;

   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.

6.     bool get_reflect_remainder() const;

   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.

7.     value_type get_interim_remainder() const;

   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.

   Returns: | 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.

8.     void reset(value_type new_rem);

   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.

   Parameters: |
   `new_rem` | The (unaugmented) state of the polynomial remainder starting from this point, with no output processing applied.
   Postconditions: | `new_rem == this->get_interim_remainder()`
   Postconditions: | `((this->get_reflect_remainder() ? REFLECT(new_rem) : new_rem) ^ this->get_final_xor_value()) == this->checksum()`

9.     void reset();

   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.

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

10.     void process_bit(bool bit);

    Submit a single bit for input processing.

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

    Parameters: |
    `bit` | The new input bit.
    Postconditions: | The interim remainder is updated though a modulo-2 polynomial division, where the division steps are altered for unaugmented CRCs.

11.     void process_bits(unsigned char bits, std::size_t bit_length);

    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.

    Parameters: |
    `bit_length` | The number of bits in the byte to be read.
    `bits` | The byte containing the new input bits.
    Requires: | 0 < bit_length <= `CHAR_BIT`
    Postconditions: | The interim remainder is updated though bit_length modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

12.     void process_byte(unsigned char 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.

    Parameters: |
    `byte` | The new input byte.
    Postconditions: | The interim remainder is updated though `CHAR_BIT` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

13.     void process_block(void const * bytes_begin, void const * bytes_end);

    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.

    Parameters: |
    `bytes_begin` | The address where the memory block begins.
    `bytes_end` | Points to one-byte past the address of the memory block's last byte, or bytes_begin if no bytes are to be read.
    Requires: | bytes_end has to equal bytes_begin if the latter is `NULL` or otherwise doesn't point to a valid buffer.
    Requires: | bytes_end, if not equal to bytes_begin, has to point within or one-byte-past the same buffer bytes_begin points into.
    Requires: | bytes_end has to be reachable from bytes_begin through a finite number of forward byte-pointer increments.
    Postconditions: | 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.

14.     void process_bytes(void const * buffer, std::size_t byte_count);

    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.

    Parameters: |
    `buffer` | The address where the memory block begins.
    `byte_count` | The number of bytes in the memory block.
    Requires: | byte_count has to equal 0 if buffer is `NULL` or otherwise doesn't point to valid memory.
    Requires: | If buffer points within valid memory, then that block has to have at least byte_count more valid bytes allocated from that point.
    Postconditions: | The interim remainder is updated though `CHAR_BIT * byte_count` modulo-2 polynomial divisions, where the division steps are altered for unaugmented CRCs.

15.     value_type checksum() const;

    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.

    | Note
    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.

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