hash/crc: parameterise on type and polynomial generator
This commit is contained in:
parent
fdc8b34770
commit
b39f80e606
139
hash/crc.cpp
139
hash/crc.cpp
@ -20,22 +20,90 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
using util::hash::crc32;
|
||||
using util::hash::crc;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static
|
||||
std::array<uint32_t,256>
|
||||
make_table (void)
|
||||
template <typename DigestT, DigestT Generator>
|
||||
const std::array<DigestT,256>
|
||||
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) {
|
||||
std::uint32_t c = i;
|
||||
digest_t c = i;
|
||||
|
||||
for (int k = 0; k < 8; ++k) {
|
||||
if (c & 1)
|
||||
c = 0xEDB88320L ^ (c >> 1);
|
||||
c = gen ^ (c >> 1);
|
||||
else
|
||||
c >>= 1;
|
||||
}
|
||||
@ -47,59 +115,6 @@ make_table (void)
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
static std::array<uint32_t,256> table = make_table ();
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
crc32::crc32 () noexcept
|
||||
{
|
||||
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;
|
||||
}
|
||||
template class util::hash::crc<uint32_t, 0xEDB88320>; // png
|
||||
//template class util::hash::crc<uint32_t, 0X04C11DB7>; // ogg
|
||||
|
23
hash/crc.hpp
23
hash/crc.hpp
@ -17,19 +17,25 @@
|
||||
#ifndef __UTIL_HASH_CRC_HPP
|
||||
#define __UTIL_HASH_CRC_HPP
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
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.
|
||||
class crc32 {
|
||||
template <typename DigestT = uint32_t, DigestT Generator = 0xEDB88320>
|
||||
class crc {
|
||||
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;
|
||||
|
||||
@ -40,10 +46,17 @@ namespace util::hash {
|
||||
|
||||
digest_t digest (void) const;
|
||||
|
||||
static
|
||||
std::array<DigestT,256>
|
||||
table (void);
|
||||
|
||||
private:
|
||||
digest_t m_digest;
|
||||
|
||||
static const std::array<DigestT,256> s_table;
|
||||
};
|
||||
|
||||
using crc32 = crc<uint32_t, 0xEDB88320>;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -34,7 +34,7 @@ namespace util::hash {
|
||||
class adler32;
|
||||
class bsdsum;
|
||||
class fletcher;
|
||||
class crc32;
|
||||
class crc;
|
||||
|
||||
class xxhash;
|
||||
|
||||
|
@ -40,7 +40,7 @@ static
|
||||
const char* NAMES[] = {
|
||||
"adler32",
|
||||
"bsdsum",
|
||||
"crc32",
|
||||
"crc",
|
||||
"MD2",
|
||||
"MD4",
|
||||
"MD5",
|
||||
|
Loading…
Reference in New Issue
Block a user