hash/crc: parameterise on type and polynomial generator

This commit is contained in:
Danny Robson 2017-02-13 17:13:46 +11:00
parent fdc8b34770
commit b39f80e606
4 changed files with 97 additions and 69 deletions

View File

@ -20,22 +20,90 @@
#include <array> #include <array>
using util::hash::crc32; using util::hash::crc;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static template <typename DigestT, DigestT Generator>
std::array<uint32_t,256> const std::array<DigestT,256>
make_table (void) crc<DigestT,Generator>::s_table = crc<DigestT,Generator>::table ();
///////////////////////////////////////////////////////////////////////////////
template <typename DigestT, DigestT Generator>
crc<DigestT,Generator>::crc () noexcept
{ {
std::array<uint32_t,256> value {}; reset ();
}
//-----------------------------------------------------------------------------
template <typename DigestT, DigestT Generator>
void
crc<DigestT,Generator>::reset (void) noexcept
{
m_digest = 0;
m_digest = ~m_digest;
}
///////////////////////////////////////////////////////////////////////////////
template <typename DigestT, DigestT Generator>
void
crc<DigestT,Generator>::update (const uint8_t *restrict first,
const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
for (auto cursor = first; cursor != last; ++cursor) {
m_digest = s_table[*cursor ^ (m_digest & 0xFF)] ^ (m_digest >> 8u);
}
}
//-----------------------------------------------------------------------------
template <typename DigestT, DigestT Generator>
void
crc<DigestT,Generator>::update (const void *restrict _data, size_t len) noexcept
{
auto data = reinterpret_cast<const uint8_t *restrict> (_data);
return update(data, data + len);
}
//-----------------------------------------------------------------------------
template <typename DigestT, DigestT Generator>
void
crc<DigestT,Generator>::finish (void)
{
;
}
//-----------------------------------------------------------------------------
template <typename DigestT, DigestT Generator>
DigestT
crc<DigestT,Generator>::digest (void) const
{
return ~m_digest;
}
///////////////////////////////////////////////////////////////////////////////
template <typename DigestT, DigestT Generator>
std::array<DigestT,256>
util::hash::crc<DigestT,Generator>::table (void)
{
auto gen = Generator;
std::array<digest_t,256> value {};
for (int i = 0; i < 256; ++i) { for (int i = 0; i < 256; ++i) {
std::uint32_t c = i; digest_t c = i;
for (int k = 0; k < 8; ++k) { for (int k = 0; k < 8; ++k) {
if (c & 1) if (c & 1)
c = 0xEDB88320L ^ (c >> 1); c = gen ^ (c >> 1);
else else
c >>= 1; c >>= 1;
} }
@ -47,59 +115,6 @@ make_table (void)
}; };
//-----------------------------------------------------------------------------
static std::array<uint32_t,256> table = make_table ();
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
crc32::crc32 () noexcept template class util::hash::crc<uint32_t, 0xEDB88320>; // png
{ //template class util::hash::crc<uint32_t, 0X04C11DB7>; // ogg
reset ();
}
//-----------------------------------------------------------------------------
void
crc32::reset (void) noexcept
{
m_digest = 0;
m_digest = ~m_digest;
}
///////////////////////////////////////////////////////////////////////////////
void
crc32::update (const uint8_t *restrict first,
const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
for (auto cursor = first; cursor != last; ++cursor) {
m_digest = table[*cursor ^ (m_digest & 0xFF)] ^ (m_digest >> 8u);
}
}
//-----------------------------------------------------------------------------
void
crc32::update (const void *restrict _data, size_t len) noexcept
{
auto data = reinterpret_cast<const uint8_t *restrict> (_data);
return update(data, data + len);
}
//-----------------------------------------------------------------------------
void
crc32::finish (void)
{
;
}
//-----------------------------------------------------------------------------
typename crc32::digest_t
crc32::digest (void) const
{
return ~m_digest;
}

View File

@ -17,19 +17,25 @@
#ifndef __UTIL_HASH_CRC_HPP #ifndef __UTIL_HASH_CRC_HPP
#define __UTIL_HASH_CRC_HPP #define __UTIL_HASH_CRC_HPP
#include <array>
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <type_traits>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
namespace util::hash { namespace util::hash {
// Implements the crc32 checksum (from ethernet, png, etc). // Implements the crc checksum (from ethernet, png, etc).
// Adapted from the PNG specification (ISO/IEC 15948:2003), appendix D. // Adapted from the PNG specification (ISO/IEC 15948:2003), appendix D.
class crc32 { template <typename DigestT = uint32_t, DigestT Generator = 0xEDB88320>
class crc {
public: public:
using digest_t = uint32_t; static_assert (std::is_same<DigestT, std::uint32_t>::value,
"only 32 bit crc is supported at this time");
crc32 () noexcept; using digest_t = DigestT;
crc () noexcept;
void reset (void) noexcept; void reset (void) noexcept;
@ -40,10 +46,17 @@ namespace util::hash {
digest_t digest (void) const; digest_t digest (void) const;
static
std::array<DigestT,256>
table (void);
private: private:
digest_t m_digest; digest_t m_digest;
static const std::array<DigestT,256> s_table;
}; };
using crc32 = crc<uint32_t, 0xEDB88320>;
} }
#endif #endif

View File

@ -34,7 +34,7 @@ namespace util::hash {
class adler32; class adler32;
class bsdsum; class bsdsum;
class fletcher; class fletcher;
class crc32; class crc;
class xxhash; class xxhash;

View File

@ -40,7 +40,7 @@ static
const char* NAMES[] = { const char* NAMES[] = {
"adler32", "adler32",
"bsdsum", "bsdsum",
"crc32", "crc",
"MD2", "MD2",
"MD4", "MD4",
"MD5", "MD5",