diff --git a/hash/bsdsum.cpp b/hash/bsdsum.cpp index 347f014f..64c3bd22 100644 --- a/hash/bsdsum.cpp +++ b/hash/bsdsum.cpp @@ -18,33 +18,61 @@ #include "../debug.hpp" +using util::hash::bsdsum; + /////////////////////////////////////////////////////////////////////////////// -uint16_t -util::hash::bsdsum ( - const uint8_t *const restrict first, - const uint8_t *const restrict last -) noexcept { - CHECK_LE (first, last); - - uint16_t accum = 0; - - for (auto cursor = first; cursor != last; ++cursor) { - accum = (accum >> 1u) | ((accum & 0x01u) << 15u); - accum += *cursor; - } - - return accum; +bsdsum::bsdsum () +{ + reset (); } //----------------------------------------------------------------------------- -uint16_t -util::hash::bsdsum (const void *restrict data, size_t size) noexcept +void +bsdsum::reset (void) { - return bsdsum ( - static_cast (data), - static_cast (data) + size - ); + m_accum = 0; } + +/////////////////////////////////////////////////////////////////////////////// +void +bsdsum::update (const void *restrict data, size_t size) noexcept +{ + auto first = static_cast (data); + + update (first, first + size); +} + + +/////////////////////////////////////////////////////////////////////////////// +void +bsdsum::update ( + const uint8_t *const restrict first, + const uint8_t *const restrict last) noexcept +{ + CHECK (first); + CHECK (last); + CHECK_LE (first, last); + + for (auto cursor = first; cursor != last; ++cursor) { + m_accum = (m_accum >> 1u) | ((m_accum & 0x01u) << 15u); + m_accum += *cursor; + } +} + + +//----------------------------------------------------------------------------- +void +bsdsum::finish (void) +{ ; } + + + +//----------------------------------------------------------------------------- +typename bsdsum::digest_t +bsdsum::digest (void) const +{ + return m_accum; +} diff --git a/hash/bsdsum.hpp b/hash/bsdsum.hpp index c4194036..ba1bac03 100644 --- a/hash/bsdsum.hpp +++ b/hash/bsdsum.hpp @@ -23,8 +23,23 @@ /////////////////////////////////////////////////////////////////////////////// namespace util { namespace hash { - uint16_t bsdsum (const void *restrict data, size_t bytes) noexcept; - uint16_t bsdsum (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; + class bsdsum { + public: + using digest_t = uint16_t; + + bsdsum (void); + void reset (void); + + void update (const void *restrict data, size_t bytes) noexcept; + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; + + void finish (void); + + digest_t digest (void) const; + + private: + digest_t m_accum; + }; } } #endif