From df825807d69f64c02a25274e0ca6a788c7ba3979 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 5 Jul 2016 11:57:57 -0700 Subject: [PATCH] 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(). --- firmware/common/crc.hpp | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/firmware/common/crc.hpp b/firmware/common/crc.hpp index cc56597c..fab78a6b 100644 --- a/firmware/common/crc.hpp +++ b/firmware/common/crc.hpp @@ -78,27 +78,15 @@ public: } void process_bits(value_type bits, size_t bit_count) { - constexpr auto digits = std::numeric_limits::digits; - constexpr auto mask = static_cast(1) << (digits - 1); - - bits <<= (std::numeric_limits::digits - bit_count); - for(size_t i=bit_count; i>0; --i, bits <<= 1) { - process_bit(static_cast(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(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::digits; + constexpr auto mask = static_cast(1) << (digits - 1); + + bits <<= (std::numeric_limits::digits - bit_count); + for(size_t i=bit_count; i>0; --i, bits <<= 1) { + process_bit(static_cast(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(bits & 0x01)); + } + } }; class Adler32 {