2015-11-06 21:49:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __CRC_H__
|
|
|
|
#define __CRC_H__
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2016-02-19 05:31:51 +00:00
|
|
|
#include <limits>
|
2016-02-19 04:36:32 +00:00
|
|
|
#include <array>
|
2015-11-06 21:49:59 +00:00
|
|
|
|
2015-12-08 18:35:54 +00:00
|
|
|
/* Inspired by
|
|
|
|
* http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
|
|
|
|
*
|
2016-02-19 04:36:32 +00:00
|
|
|
* ...then munged into a simplified implementation of boost::crc_basic and
|
|
|
|
* boost::crc_optimal.
|
2015-12-08 18:35:54 +00:00
|
|
|
* http://www.boost.org/doc/libs/release/libs/crc/
|
2016-02-19 04:36:32 +00:00
|
|
|
*
|
|
|
|
* Copyright 2001, 2004 Daryle Walker. Use, modification, and distribution are
|
|
|
|
* subject to the Boost Software License, Version 1.0. (See accompanying file
|
|
|
|
* LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
|
|
|
|
*
|
2015-12-08 18:35:54 +00:00
|
|
|
*/
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
template<size_t Width, bool RevIn = false, bool RevOut = false>
|
2015-11-06 21:49:59 +00:00
|
|
|
class CRC {
|
|
|
|
public:
|
2016-02-19 05:31:51 +00:00
|
|
|
using value_type = uint32_t;
|
|
|
|
|
2015-11-06 21:49:59 +00:00
|
|
|
constexpr CRC(
|
2016-02-19 05:31:51 +00:00
|
|
|
const value_type truncated_polynomial,
|
|
|
|
const value_type initial_remainder = 0,
|
|
|
|
const value_type final_xor_value = 0
|
2015-12-08 18:35:54 +00:00
|
|
|
) : truncated_polynomial { truncated_polynomial },
|
|
|
|
initial_remainder { initial_remainder },
|
|
|
|
final_xor_value { final_xor_value },
|
|
|
|
remainder { initial_remainder }
|
2015-11-06 21:49:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
value_type get_initial_remainder() const {
|
2015-12-08 18:35:54 +00:00
|
|
|
return initial_remainder;
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
void reset(value_type new_initial_remainder) {
|
2015-12-08 18:35:54 +00:00
|
|
|
remainder = new_initial_remainder;
|
|
|
|
}
|
2015-11-06 21:49:59 +00:00
|
|
|
|
2015-12-08 18:35:54 +00:00
|
|
|
void reset() {
|
|
|
|
remainder = initial_remainder;
|
|
|
|
}
|
2015-11-06 21:49:59 +00:00
|
|
|
|
2015-12-08 18:35:54 +00:00
|
|
|
void process_bit(bool bit) {
|
2016-02-19 04:36:32 +00:00
|
|
|
remainder ^= (bit ? top_bit() : 0U);
|
|
|
|
const auto do_poly_div = static_cast<bool>(remainder & top_bit());
|
|
|
|
remainder <<= 1;
|
|
|
|
if( do_poly_div ) {
|
|
|
|
remainder ^= truncated_polynomial;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
void process_bits(value_type bits, size_t bit_count) {
|
2016-07-05 18:57:57 +00:00
|
|
|
if( RevIn ) {
|
|
|
|
process_bits_lsb_first(bits, bit_count);
|
|
|
|
} else {
|
|
|
|
process_bits_msb_first(bits, bit_count);
|
2015-12-08 18:35:54 +00:00
|
|
|
}
|
2015-11-06 21:49:59 +00:00
|
|
|
}
|
2015-12-08 18:35:54 +00:00
|
|
|
|
2016-02-19 04:36:32 +00:00
|
|
|
void process_byte(const uint8_t byte) {
|
2016-07-05 18:57:57 +00:00
|
|
|
process_bits(byte, 8);
|
2015-11-06 21:49:59 +00:00
|
|
|
}
|
2015-12-08 18:35:54 +00:00
|
|
|
|
2016-02-19 23:30:35 +00:00
|
|
|
void process_bytes(const void* const data, const size_t length) {
|
|
|
|
const uint8_t* const p = reinterpret_cast<const uint8_t*>(data);
|
2015-12-08 18:35:54 +00:00
|
|
|
for(size_t i=0; i<length; i++) {
|
2016-02-19 23:30:35 +00:00
|
|
|
process_byte(p[i]);
|
2015-11-06 21:49:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 18:35:54 +00:00
|
|
|
|
2016-02-19 04:36:32 +00:00
|
|
|
template<size_t N>
|
|
|
|
void process_bytes(const std::array<uint8_t, N>& data) {
|
|
|
|
process_bytes(data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
value_type checksum() const {
|
|
|
|
return ((RevOut ? reflect(remainder) : remainder) ^ final_xor_value) & mask();
|
2015-12-08 18:35:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 21:49:59 +00:00
|
|
|
private:
|
2016-02-19 05:31:51 +00:00
|
|
|
const value_type truncated_polynomial;
|
|
|
|
const value_type initial_remainder;
|
|
|
|
const value_type final_xor_value;
|
|
|
|
value_type remainder;
|
2015-11-06 21:49:59 +00:00
|
|
|
|
|
|
|
static constexpr size_t width() {
|
2016-02-19 05:31:51 +00:00
|
|
|
return Width;
|
2015-11-06 21:49:59 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
static constexpr value_type top_bit() {
|
2015-11-06 21:49:59 +00:00
|
|
|
return 1U << (width() - 1);
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:31:51 +00:00
|
|
|
static constexpr value_type mask() {
|
2016-02-22 22:18:53 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
|
2016-02-19 05:31:51 +00:00
|
|
|
return (~(~(0UL) << width()));
|
2016-02-22 22:18:53 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2016-02-19 05:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static value_type reflect(value_type x) {
|
|
|
|
value_type reflection = 0;
|
2016-02-19 04:36:32 +00:00
|
|
|
for(size_t i=0; i<width(); ++i) {
|
|
|
|
reflection <<= 1;
|
|
|
|
reflection |= (x & 1);
|
|
|
|
x >>= 1;
|
|
|
|
}
|
|
|
|
return reflection;
|
|
|
|
}
|
2016-07-05 18:57:57 +00:00
|
|
|
|
|
|
|
void process_bits_msb_first(value_type bits, size_t bit_count) {
|
|
|
|
constexpr auto digits = std::numeric_limits<value_type>::digits;
|
|
|
|
constexpr auto mask = static_cast<value_type>(1) << (digits - 1);
|
|
|
|
|
|
|
|
bits <<= (std::numeric_limits<value_type>::digits - bit_count);
|
|
|
|
for(size_t i=bit_count; i>0; --i, bits <<= 1) {
|
|
|
|
process_bit(static_cast<bool>(bits & mask));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void process_bits_lsb_first(value_type bits, size_t bit_count) {
|
|
|
|
for(size_t i=bit_count; i>0; --i, bits >>= 1) {
|
|
|
|
process_bit(static_cast<bool>(bits & 0x01));
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 04:36:32 +00:00
|
|
|
};
|
2015-11-06 21:49:59 +00:00
|
|
|
|
2016-02-19 22:13:15 +00:00
|
|
|
class Adler32 {
|
|
|
|
public:
|
|
|
|
void feed(const uint8_t v) {
|
|
|
|
feed_one(v);
|
|
|
|
}
|
|
|
|
|
2016-02-19 23:30:35 +00:00
|
|
|
void feed(const void* const data, const size_t n) {
|
|
|
|
const uint8_t* const p = reinterpret_cast<const uint8_t*>(data);
|
2016-02-19 22:13:15 +00:00
|
|
|
for(size_t i=0; i<n; i++) {
|
|
|
|
feed_one(p[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void feed(const T& a) {
|
2016-02-19 23:30:35 +00:00
|
|
|
feed(a.data(), sizeof(T));
|
2016-02-19 22:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::array<uint8_t, 4> bytes() const {
|
|
|
|
return {
|
|
|
|
static_cast<uint8_t>((b >> 8) & 0xff),
|
|
|
|
static_cast<uint8_t>((b >> 0) & 0xff),
|
|
|
|
static_cast<uint8_t>((a >> 8) & 0xff),
|
|
|
|
static_cast<uint8_t>((a >> 0) & 0xff)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr uint32_t mod = 65521;
|
|
|
|
|
|
|
|
uint32_t a { 1 };
|
|
|
|
uint32_t b { 0 };
|
|
|
|
|
|
|
|
void feed_one(const uint8_t c) {
|
|
|
|
a = (a + c) % mod;
|
|
|
|
b = (b + a) % mod;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-06 21:49:59 +00:00
|
|
|
#endif/*__CRC_H__*/
|