From aeba9033e0fc013e1a15c50c1aa051ca70b9ce10 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 17 Jun 2016 15:46:11 +1000 Subject: [PATCH] hash: prefer iterator style over base/length --- hash/bsdsum.cpp | 24 ++++++++++++++++++++---- hash/bsdsum.hpp | 3 ++- hash/crc.cpp | 8 ++++++++ hash/crc.hpp | 3 ++- hash/md2.cpp | 11 +++++++++++ hash/md2.hpp | 1 + hash/md4.cpp | 12 +++++++++++- hash/md4.hpp | 1 + hash/md5.cpp | 9 +++++++++ hash/md5.hpp | 1 + hash/ripemd.cpp | 10 ++++++++++ hash/ripemd.hpp | 2 ++ hash/sha1.cpp | 10 ++++++++++ hash/sha1.hpp | 2 ++ hash/sha2.cpp | 10 ++++++++++ hash/sha2.hpp | 2 ++ 16 files changed, 102 insertions(+), 7 deletions(-) diff --git a/hash/bsdsum.cpp b/hash/bsdsum.cpp index ec3877b2..b367a168 100644 --- a/hash/bsdsum.cpp +++ b/hash/bsdsum.cpp @@ -16,17 +16,33 @@ #include "bsdsum.hpp" +#include "../debug.hpp" + +/////////////////////////////////////////////////////////////////////////////// uint16_t -bsdsum (const void *restrict _data, size_t size) { - const uint8_t *restrict data = static_cast (_data); +bsdsum (const uint8_t *const restrict first, const uint8_t *const restrict last) +{ + CHECK_LE (first, last); + uint16_t accum = 0; - for (size_t i = 0; i < size; ++i) { + for (auto cursor = first; cursor != last; ++cursor) { accum = (accum >> 1u) | ((accum & 0x01u) << 15u); - accum += data[i]; + accum += *cursor; } return accum; } + +/////////////////////////////////////////////////////////////////////////////// +uint16_t +bsdsum (const void *restrict data, size_t size) +{ + return bsdsum ( + static_cast (data), + static_cast (data) + size + ); +} + diff --git a/hash/bsdsum.hpp b/hash/bsdsum.hpp index 76f6c872..0add7602 100644 --- a/hash/bsdsum.hpp +++ b/hash/bsdsum.hpp @@ -20,6 +20,7 @@ #include #include -uint16_t bsdsum (const void *restrict, size_t); +uint16_t bsdsum (const void *restrict data, size_t bytes); +uint16_t bsdsum (const uint8_t *restrict first, const uint8_t *restrict last); #endif diff --git a/hash/crc.cpp b/hash/crc.cpp index f29ce7ab..d0a7e5d6 100644 --- a/hash/crc.cpp +++ b/hash/crc.cpp @@ -19,6 +19,14 @@ #include "../endian.hpp" #include "../debug.hpp" + +uint32_t +crc32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + not_implemented (); +} + + uint32_t crc32 (const void *restrict, size_t) { not_implemented (); diff --git a/hash/crc.hpp b/hash/crc.hpp index 50845409..4a511bf3 100644 --- a/hash/crc.hpp +++ b/hash/crc.hpp @@ -20,7 +20,8 @@ #include #include -uint32_t crc32 (const void *restrict, size_t); +uint32_t crc32 (const void *restrict data, size_t bytes); +uint32_t crc32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; #endif diff --git a/hash/md2.cpp b/hash/md2.cpp index 62890ab1..16155a1c 100644 --- a/hash/md2.cpp +++ b/hash/md2.cpp @@ -16,6 +16,7 @@ #include "md2.hpp" +#include "../debug.hpp" #include "../types.hpp" #include @@ -72,6 +73,16 @@ MD2::reset (void) /////////////////////////////////////////////////////////////////////////////// void +MD2::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + CHECK_LE (first, last); + + update (first, last - first); +} + + +//----------------------------------------------------------------------------- +void MD2::update (const void *data, size_t size) { update (static_cast (data), size); diff --git a/hash/md2.hpp b/hash/md2.hpp index 4b7c57d0..0e20e4b9 100644 --- a/hash/md2.hpp +++ b/hash/md2.hpp @@ -32,6 +32,7 @@ namespace util { void update (const uint8_t *data, size_t len); void update (const void *data, size_t len); + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; void finish (void); digest_t digest (void) const; diff --git a/hash/md4.cpp b/hash/md4.cpp index 78616847..22e30333 100644 --- a/hash/md4.cpp +++ b/hash/md4.cpp @@ -17,8 +17,9 @@ #include "md4.hpp" #include "../bitwise.hpp" -#include "../types.hpp" +#include "../debug.hpp" #include "../endian.hpp" +#include "../types.hpp" #include @@ -71,6 +72,15 @@ MD4::reset (void) { } +void +MD4::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + CHECK_LE (first, last); + + update (first, last - first); +} + + void MD4::update (const void *data, size_t size) { update (static_cast (data), size); } diff --git a/hash/md4.hpp b/hash/md4.hpp index aa4eab0f..4428404c 100644 --- a/hash/md4.hpp +++ b/hash/md4.hpp @@ -31,6 +31,7 @@ namespace util { void update (const void *data, size_t len); void update (const uint8_t *data, size_t len); + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; void finish (void); digest_t digest (void) const; diff --git a/hash/md5.cpp b/hash/md5.cpp index c969fc4d..f9a2fa56 100644 --- a/hash/md5.cpp +++ b/hash/md5.cpp @@ -102,6 +102,15 @@ MD5::reset (void) { } +void +MD5::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + CHECK_LE (first, last); + + update (first, last - first); +} + + void MD5::update (const void *data, size_t len) { MD5::update (static_cast (data), len); } diff --git a/hash/md5.hpp b/hash/md5.hpp index 4fbc23be..6d105bde 100644 --- a/hash/md5.hpp +++ b/hash/md5.hpp @@ -34,6 +34,7 @@ namespace util { void update (const void *data, size_t len); void update (const uint8_t *data, size_t len); + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; void finish (void); digest_t digest (void) const; diff --git a/hash/ripemd.cpp b/hash/ripemd.cpp index 9680cda7..fbfe8098 100644 --- a/hash/ripemd.cpp +++ b/hash/ripemd.cpp @@ -49,6 +49,16 @@ RIPEMD::reset (void) { /////////////////////////////////////////////////////////////////////////////// void +RIPEMD::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + CHECK_LE (first, last); + + update (first, last - first); +} + + +//----------------------------------------------------------------------------- +void RIPEMD::update (const uint8_t *data, size_t len) { CHECK (data); diff --git a/hash/ripemd.hpp b/hash/ripemd.hpp index c4237a01..17a56a30 100644 --- a/hash/ripemd.hpp +++ b/hash/ripemd.hpp @@ -30,6 +30,8 @@ namespace util { RIPEMD(); void update (const uint8_t*, size_t); + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; + digest_t digest (void) const; void finish (void); void reset (void); diff --git a/hash/sha1.cpp b/hash/sha1.cpp index 7ebfc980..a0385399 100644 --- a/hash/sha1.cpp +++ b/hash/sha1.cpp @@ -104,6 +104,16 @@ SHA1::reset (void) { } +//----------------------------------------------------------------------------- +void +SHA1::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + CHECK_LE (first, last); + + update (first, last - first); +} + + //----------------------------------------------------------------------------- void SHA1::update (const uint8_t *data, size_t size) { diff --git a/hash/sha1.hpp b/hash/sha1.hpp index 3cb837c1..8842cfd5 100644 --- a/hash/sha1.hpp +++ b/hash/sha1.hpp @@ -35,6 +35,8 @@ namespace util { namespace hash { SHA1(); void update (const uint8_t *, size_t); + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; + void finish (void); digest_t digest (void) const; void reset (void); diff --git a/hash/sha2.cpp b/hash/sha2.cpp index 55057667..923ffad8 100644 --- a/hash/sha2.cpp +++ b/hash/sha2.cpp @@ -18,6 +18,7 @@ #include "sha2.hpp" #include "../bitwise.hpp" +#include "../debug.hpp" #include "../endian.hpp" #include @@ -170,6 +171,15 @@ SHA256::SHA256 (): } +void +SHA256::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept +{ + CHECK_LE (first, last); + + update (first, last - first); +} + + void SHA256::update (const uint8_t *data, size_t length) { while (length) { diff --git a/hash/sha2.hpp b/hash/sha2.hpp index 3827036f..91631922 100644 --- a/hash/sha2.hpp +++ b/hash/sha2.hpp @@ -31,6 +31,8 @@ namespace util { SHA256(); void update (const uint8_t *, size_t); + void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; + void finish (void); digest_t digest (void) const;