2011-11-04 16:56:25 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-11-04 16:56:25 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
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
|
|
|
*/
|
|
|
|
|
2016-04-05 11:06:01 +10:00
|
|
|
#include "./crc.hpp"
|
2011-11-04 16:56:25 +11:00
|
|
|
|
2016-04-05 11:06:01 +10:00
|
|
|
#include "../debug.hpp"
|
2011-11-04 16:56:25 +11:00
|
|
|
|
2017-01-25 16:12:12 +11:00
|
|
|
#include <array>
|
|
|
|
|
2017-02-13 17:13:46 +11:00
|
|
|
using util::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-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
|
|
|
const std::array<DigestT,256>
|
|
|
|
crc<DigestT,Generator>::s_table = crc<DigestT,Generator>::table ();
|
2016-06-20 16:48:17 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-02-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
|
|
|
crc<DigestT,Generator>::crc () noexcept
|
2016-06-20 16:48:17 +10:00
|
|
|
{
|
2017-01-25 16:12:12 +11:00
|
|
|
reset ();
|
2016-06-17 15:46:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-17 15:56:14 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-02-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
2016-06-20 16:48:17 +10:00
|
|
|
void
|
2017-02-13 17:13:46 +11:00
|
|
|
crc<DigestT,Generator>::reset (void) noexcept
|
2016-06-20 16:48:17 +10:00
|
|
|
{
|
2017-01-25 16:12:12 +11:00
|
|
|
m_digest = 0;
|
|
|
|
m_digest = ~m_digest;
|
|
|
|
}
|
2011-11-04 16:56:25 +11:00
|
|
|
|
|
|
|
|
2017-01-25 16:12:12 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-02-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
2017-01-25 16:12:12 +11:00
|
|
|
void
|
2017-02-13 17:13:46 +11:00
|
|
|
crc<DigestT,Generator>::update (const uint8_t *restrict first,
|
|
|
|
const uint8_t *restrict last) noexcept
|
2017-01-25 16:12:12 +11:00
|
|
|
{
|
|
|
|
CHECK_LE (first, last);
|
2011-11-04 16:56:25 +11:00
|
|
|
|
2017-01-25 16:12:12 +11:00
|
|
|
for (auto cursor = first; cursor != last; ++cursor) {
|
2017-02-13 17:13:46 +11:00
|
|
|
m_digest = s_table[*cursor ^ (m_digest & 0xFF)] ^ (m_digest >> 8u);
|
2011-11-04 16:56:25 +11:00
|
|
|
}
|
2017-01-25 16:12:12 +11:00
|
|
|
}
|
2011-11-04 16:56:25 +11:00
|
|
|
|
|
|
|
|
2017-01-25 16:12:12 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-02-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
2017-01-25 16:12:12 +11:00
|
|
|
void
|
2017-02-13 17:13:46 +11:00
|
|
|
crc<DigestT,Generator>::update (const void *restrict _data, size_t len) noexcept
|
2017-01-25 16:12:12 +11:00
|
|
|
{
|
|
|
|
auto data = reinterpret_cast<const uint8_t *restrict> (_data);
|
|
|
|
return update(data, data + len);
|
2011-11-04 16:56:25 +11:00
|
|
|
}
|
2016-06-20 16:48:17 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-02-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
2016-06-20 16:48:17 +10:00
|
|
|
void
|
2017-02-13 17:13:46 +11:00
|
|
|
crc<DigestT,Generator>::finish (void)
|
2017-01-25 16:12:12 +11:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
2016-06-20 16:48:17 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-02-13 17:13:46 +11:00
|
|
|
template <typename DigestT, DigestT Generator>
|
|
|
|
DigestT
|
|
|
|
crc<DigestT,Generator>::digest (void) const
|
2017-01-25 16:12:12 +11:00
|
|
|
{
|
|
|
|
return ~m_digest;
|
2017-02-13 17:13:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
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) {
|
|
|
|
digest_t c = i;
|
|
|
|
|
|
|
|
for (int k = 0; k < 8; ++k) {
|
|
|
|
if (c & 1)
|
|
|
|
c = gen ^ (c >> 1);
|
|
|
|
else
|
|
|
|
c >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template class util::hash::crc<uint32_t, 0xEDB88320>; // png
|
|
|
|
//template class util::hash::crc<uint32_t, 0X04C11DB7>; // ogg
|