CRC: Rearrange public methods to reduce user error.

If you can access process_bits() without considering RevIn value, you will likely not get the CRC value you're expecting! Put RevIn check where it belongs, in process_bits().
This commit is contained in:
Jared Boone 2016-07-05 11:57:57 -07:00
parent db700b1beb
commit df825807d6

View File

@ -78,28 +78,16 @@ public:
}
void process_bits(value_type bits, size_t bit_count) {
constexpr auto digits = std::numeric_limits<value_type>::digits;
constexpr auto mask = static_cast<value_type>(1) << (digits - 1);
bits <<= (std::numeric_limits<value_type>::digits - bit_count);
for(size_t i=bit_count; i>0; --i, bits <<= 1) {
process_bit(static_cast<bool>(bits & mask));
}
}
void process_bits_lsb_first(value_type bits, size_t bit_count) {
for(size_t i=bit_count; i>0; --i, bits >>= 1) {
process_bit(static_cast<bool>(bits & 0x01));
if( RevIn ) {
process_bits_lsb_first(bits, bit_count);
} else {
process_bits_msb_first(bits, bit_count);
}
}
void process_byte(const uint8_t byte) {
if( RevIn ) {
process_bits_lsb_first(byte, 8);
} else {
process_bits(byte, 8);
}
}
void process_bytes(const void* const data, const size_t length) {
const uint8_t* const p = reinterpret_cast<const uint8_t*>(data);
@ -147,6 +135,22 @@ private:
}
return reflection;
}
void process_bits_msb_first(value_type bits, size_t bit_count) {
constexpr auto digits = std::numeric_limits<value_type>::digits;
constexpr auto mask = static_cast<value_type>(1) << (digits - 1);
bits <<= (std::numeric_limits<value_type>::digits - bit_count);
for(size_t i=bit_count; i>0; --i, bits <<= 1) {
process_bit(static_cast<bool>(bits & mask));
}
}
void process_bits_lsb_first(value_type bits, size_t bit_count) {
for(size_t i=bit_count; i>0; --i, bits >>= 1) {
process_bit(static_cast<bool>(bits & 0x01));
}
}
};
class Adler32 {