mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2024-12-13 19:54:39 +00:00
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:
parent
db700b1beb
commit
df825807d6
@ -78,27 +78,15 @@ 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);
|
||||
}
|
||||
process_bits(byte, 8);
|
||||
}
|
||||
|
||||
void process_bytes(const void* const data, const size_t length) {
|
||||
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user