2011-11-04 16:56:25 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-11-04 16:56:25 +11:00
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
2011-11-04 16:56:25 +11:00
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "crc.hpp"
|
2011-11-04 16:56:25 +11:00
|
|
|
|
2017-05-12 15:56:41 +10:00
|
|
|
#include "../bitwise.hpp"
|
2011-11-04 16:56:25 +11:00
|
|
|
|
2017-01-25 16:12:12 +11:00
|
|
|
#include <array>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::hash::crc;
|
2016-06-20 16:48:17 +10:00
|
|
|
|
2016-06-17 15:46:11 +10:00
|
|
|
|
2016-06-17 15:56:14 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-02-14 19:47:12 +11:00
|
|
|
template <
|
|
|
|
typename DigestT,
|
|
|
|
DigestT Generator,
|
|
|
|
DigestT Initial,
|
|
|
|
DigestT Final,
|
|
|
|
bool ReflectIn,
|
|
|
|
bool ReflectOut
|
|
|
|
>
|
2017-02-13 17:13:46 +11:00
|
|
|
const std::array<DigestT,256>
|
2017-02-14 19:47:12 +11:00
|
|
|
crc<
|
|
|
|
DigestT,Generator,Initial,Final,ReflectIn,ReflectOut
|
|
|
|
>::s_table = crc<DigestT,Generator,Initial,Final,ReflectIn,ReflectOut>::table ();
|
2016-06-20 16:48:17 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-02-14 19:47:12 +11:00
|
|
|
template <
|
|
|
|
typename DigestT,
|
|
|
|
DigestT Generator,
|
|
|
|
DigestT Initial,
|
|
|
|
DigestT Final,
|
|
|
|
bool ReflectIn,
|
|
|
|
bool ReflectOut
|
|
|
|
>
|
2018-01-13 13:48:58 +11:00
|
|
|
typename crc<DigestT,Generator,Initial,Final,ReflectIn,ReflectOut>::digest_t
|
2017-02-14 19:47:12 +11:00
|
|
|
crc<
|
|
|
|
DigestT,Generator,Initial,Final,ReflectIn,ReflectOut
|
2018-08-05 14:42:02 +10:00
|
|
|
>::operator() (const cruft::view<const uint8_t*> data) const noexcept
|
2016-06-20 16:48:17 +10:00
|
|
|
{
|
2018-01-13 13:48:58 +11:00
|
|
|
auto accum = Initial;
|
2011-11-04 16:56:25 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
for (auto i: data) {
|
2017-02-14 19:47:12 +11:00
|
|
|
if (ReflectIn)
|
2018-01-13 13:48:58 +11:00
|
|
|
accum = s_table[i ^ (accum & 0xFFu)] ^ (accum >> 8u);
|
2017-02-14 19:47:12 +11:00
|
|
|
else {
|
|
|
|
constexpr auto shift = sizeof (DigestT) * 8u - 8u;
|
2018-01-13 13:48:58 +11:00
|
|
|
accum = (accum << 8u) ^ s_table[(accum >> shift) ^ i];
|
2017-02-14 19:47:12 +11:00
|
|
|
}
|
2011-11-04 16:56:25 +11:00
|
|
|
}
|
2016-06-20 16:48:17 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
return (ReflectIn != ReflectOut ? cruft::reverse (accum) : accum) ^ Final;
|
2017-02-13 17:13:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-02-14 19:47:12 +11:00
|
|
|
template <
|
|
|
|
typename DigestT,
|
|
|
|
DigestT Generator,
|
|
|
|
DigestT Initial,
|
|
|
|
DigestT Final,
|
|
|
|
bool ReflectIn,
|
|
|
|
bool ReflectOut
|
|
|
|
>
|
|
|
|
constexpr
|
2017-02-13 17:13:46 +11:00
|
|
|
std::array<DigestT,256>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::hash::crc<
|
2017-02-14 19:47:12 +11:00
|
|
|
DigestT,Generator,Initial,Final,ReflectIn,ReflectOut
|
|
|
|
>::table (void)
|
2017-02-13 17:13:46 +11:00
|
|
|
{
|
2017-02-20 18:01:40 +11:00
|
|
|
// We want to use a std::array here for a (slight) safety advantage, but
|
|
|
|
// it means that we have to use calls to array::data instead of operator[]
|
|
|
|
// to retain constexpr.
|
2017-02-14 19:47:12 +11:00
|
|
|
std::array<digest_t,256> values {};
|
2017-02-13 17:13:46 +11:00
|
|
|
|
2017-02-14 19:47:12 +11:00
|
|
|
if (ReflectIn) {
|
2018-08-05 14:42:02 +10:00
|
|
|
constexpr auto gen = cruft::reverse (Generator);
|
2017-02-13 17:13:46 +11:00
|
|
|
|
2017-02-14 19:47:12 +11:00
|
|
|
for (int i = 0; i < 256; ++i) {
|
|
|
|
digest_t c = i;
|
2017-02-13 17:13:46 +11:00
|
|
|
|
2017-02-14 19:47:12 +11:00
|
|
|
for (int k = 0; k < 8; ++k) {
|
|
|
|
if (c & 1)
|
|
|
|
c = gen ^ (c >> 1);
|
|
|
|
else
|
|
|
|
c >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
values.data ()[i] = c;
|
2017-02-13 17:13:46 +11:00
|
|
|
}
|
2017-02-14 19:47:12 +11:00
|
|
|
} else {
|
|
|
|
constexpr DigestT shift = sizeof (DigestT) * 8u - 8u;
|
|
|
|
constexpr DigestT highbit = DigestT(1) << (sizeof (DigestT) * 8u - 1u);
|
|
|
|
|
|
|
|
for (int idx = 0; idx < 256; ++idx) {
|
|
|
|
DigestT c = DigestT (idx) << shift;
|
2017-02-13 17:13:46 +11:00
|
|
|
|
2017-02-14 19:47:12 +11:00
|
|
|
for (int i = 0; i < 8; ++i) {
|
|
|
|
if (c & highbit)
|
|
|
|
c = (c << DigestT (1)) ^ Generator;
|
|
|
|
else
|
|
|
|
c <<= DigestT(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
values.data()[idx] = c;
|
|
|
|
}
|
2017-02-13 17:13:46 +11:00
|
|
|
}
|
|
|
|
|
2017-02-14 19:47:12 +11:00
|
|
|
return values;
|
2017-02-13 17:13:46 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
template class cruft::hash::crc<uint32_t, 0x04C11DB7, 0xffffffff, 0xffffffff, true, true >; // crc32
|
|
|
|
template class cruft::hash::crc<uint32_t, 0x04C11DB7, 0xffffffff, 0xffffffff, false, false>; // crc32b
|
|
|
|
template class cruft::hash::crc<uint32_t, 0x1edc6f41, 0xffffffff, 0xffffffff, true, true>; // crc32c
|
|
|
|
template class cruft::hash::crc<uint32_t, 0xa833982b, 0xffffffff, 0xffffffff, true, true>; // crc32d
|
2017-02-14 19:47:12 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
template class cruft::hash::crc<uint32_t, 0x04C11DB7, 0x00000000, 0x00000000, false, false>; // ogg
|
2017-02-14 19:47:12 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
template class cruft::hash::crc<uint64_t, 0x42f0e1eba9ea3693, 0, 0, false, false>; // crc64
|