aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/mpt/crc/crc.hpp
blob: 5d6e851f275daadbe68c45cb86d7f26d7755cd92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */

#ifndef MPT_CRC_CRC_HPP
#define MPT_CRC_CRC_HPP



#include "mpt/base/array.hpp"
#include "mpt/base/detect.hpp"
#include "mpt/base/namespace.hpp"

#include "mpt/base/integer.hpp"
#include "mpt/base/memory.hpp"

#include <array>

#include <cstddef>



namespace mpt {
inline namespace MPT_INLINE_NS {



template <typename T, T polynomial, T initial, T resultXOR, bool reverseData>
class crc {

public:
	using self_type = crc;
	using value_type = T;
	using byte_type = uint8;

	static constexpr std::size_t size_bytes = sizeof(value_type);
	static constexpr std::size_t size_bits = sizeof(value_type) * 8;
	static constexpr value_type top_bit = static_cast<value_type>(1) << ((sizeof(value_type) * 8) - 1);

private:
	template <typename Tint>
	static constexpr Tint reverse(Tint value) noexcept {
		const std::size_t bits = sizeof(Tint) * 8;
		Tint result = 0;
		for (std::size_t i = 0; i < bits; ++i) {
			result <<= 1;
			result |= static_cast<Tint>(value & 0x1);
			value >>= 1;
		}
		return result;
	}

	static constexpr value_type calculate_table_entry(byte_type pos) noexcept {
		value_type value = 0;
		value = (static_cast<value_type>(reverseData ? reverse(pos) : pos) << (size_bits - 8));
		for (std::size_t bit = 0; bit < 8; ++bit) {
			if (value & top_bit) {
				value = (value << 1) ^ polynomial;
			} else {
				value = (value << 1);
			}
		}
		value = (reverseData ? reverse(value) : value);
		return value;
	}

private:
	static constexpr std::array<value_type, 256> calculate_table() noexcept {
		std::array<value_type, 256> t = mpt::init_array<value_type, 256>(value_type{});
		for (std::size_t i = 0; i < 256; ++i) {
			t[i] = calculate_table_entry(static_cast<byte_type>(i));
		}
		return t;
	}

	static constexpr std::array<value_type, 256> table = calculate_table();

private:
	constexpr value_type read_table(byte_type pos) const noexcept {
		return table[pos];
	}

private:
	value_type value;

public:
	constexpr crc() noexcept
		: value(initial) {
		return;
	}

	constexpr void processByte(byte_type byte) noexcept {
		if constexpr (reverseData) {
			value = (value >> 8) ^ read_table(static_cast<byte_type>((value & 0xff) ^ byte));
		} else {
			value = (value << 8) ^ read_table(static_cast<byte_type>(((value >> (size_bits - 8)) & 0xff) ^ byte));
		}
	}

	constexpr value_type result() const noexcept {
		return (value ^ resultXOR);
	}

public:
	constexpr operator value_type() const noexcept {
		return result();
	}

	inline crc & process(char c) noexcept {
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

	inline crc & process(signed char c) noexcept {
		processByte(static_cast<byte_type>(c));
		return *this;
	}

	inline crc & process(unsigned char c) noexcept {
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

	inline crc & process(std::byte c) noexcept {
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

	template <typename InputIt>
	inline crc & process(InputIt beg, InputIt end) {
		for (InputIt it = beg; it != end; ++it) {
			static_assert(sizeof(*it) == 1, "1 byte type required");
			process(*it);
		}
		return *this;
	}

	template <typename Container>
	inline crc & process(const Container & data) {
		operator()(data.begin(), data.end());
		return *this;
	}

	inline crc & operator()(char c) noexcept {
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

	inline crc & operator()(signed char c) noexcept {
		processByte(static_cast<byte_type>(c));
		return *this;
	}

	inline crc & operator()(unsigned char c) noexcept {
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

	inline crc & operator()(std::byte c) noexcept {
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

	template <typename InputIt>
	crc & operator()(InputIt beg, InputIt end) {
		for (InputIt it = beg; it != end; ++it) {
			static_assert(sizeof(*it) == 1, "1 byte type required");
			operator()(*it);
		}
		return *this;
	}

	template <typename Container>
	inline crc & operator()(const Container & data) {
		operator()(data.begin(), data.end());
		return *this;
	}

	template <typename InputIt>
	crc(InputIt beg, InputIt end)
		: value(initial) {
		for (InputIt it = beg; it != end; ++it) {
			static_assert(sizeof(*it) == 1, "1 byte type required");
			process(*it);
		}
	}

	template <typename Container>
	inline crc(const Container & data)
		: value(initial) {
		process(data.begin(), data.end());
	}
};

using crc16 = crc<uint16, 0x8005, 0, 0, true>;
using crc32 = crc<uint32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true>;
using crc32_ogg = crc<uint32, 0x04C11DB7, 0, 0, false>;
using crc32c = crc<uint32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true>;
using crc64_jones = crc<uint64, 0xAD93D23594C935A9ull, 0xFFFFFFFFFFFFFFFFull, 0, true>;


} // namespace MPT_INLINE_NS
} // namespace mpt



#endif // MPT_CRC_CRC_HPP