2016-05-02 09:57:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
#include <nall/hash/hash.hpp>
|
2016-05-02 09:57:04 +00:00
|
|
|
|
|
|
|
namespace nall { namespace Hash {
|
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
struct CRC64 : Hash {
|
|
|
|
nallHash(CRC64)
|
2016-05-02 09:57:04 +00:00
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
auto reset() -> void override {
|
2016-05-02 09:57:04 +00:00
|
|
|
checksum = ~0;
|
|
|
|
}
|
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
auto input(uint8_t value) -> void override {
|
2016-05-02 09:57:04 +00:00
|
|
|
checksum = (checksum >> 8) ^ table(checksum ^ value);
|
|
|
|
}
|
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
auto output() const -> vector<uint8_t> {
|
|
|
|
vector<uint8_t> result;
|
|
|
|
for(auto n : rrange(8)) result.append(~checksum >> n * 8);
|
|
|
|
return result;
|
2016-05-16 09:51:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 09:57:04 +00:00
|
|
|
auto value() const -> uint64_t {
|
|
|
|
return ~checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static auto table(uint8_t index) -> uint64_t {
|
|
|
|
static uint64_t table[256] = {0};
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if(!initialized) {
|
|
|
|
initialized = true;
|
|
|
|
for(auto index : range(256)) {
|
|
|
|
uint64_t crc = index;
|
|
|
|
for(auto bit : range(8)) {
|
|
|
|
crc = (crc >> 1) ^ (crc & 1 ? 0xc96c'5795'd787'0f42 : 0);
|
|
|
|
}
|
|
|
|
table[index] = crc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return table[index];
|
|
|
|
}
|
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
uint64_t checksum = 0;
|
2016-05-02 09:57:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}}
|