File: mptCRC.h

package info (click to toggle)
libopenmpt 0.4.3-1+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,724 kB
  • sloc: cpp: 99,820; sh: 4,503; ansic: 3,449; makefile: 480
file content (263 lines) | stat: -rw-r--r-- 5,057 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * mptCRC.h
 * --------
 * Purpose: generic CRC implementation
 * Notes  : (currently none)
 * Authors: Joern Heusipp
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#pragma once

#include "BuildSettings.h"

OPENMPT_NAMESPACE_BEGIN

namespace mpt
{

namespace checksum
{

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

public:
	
	typedef crc self_type;
	typedef T value_type;
	typedef uint8 byte_type;

	enum : std::size_t { size_bytes = sizeof(value_type) };
	enum : std::size_t { size_bits = sizeof(value_type) * 8 };
	enum : value_type { top_bit = static_cast<value_type>(1) << ((sizeof(value_type) * 8) - 1) };

private:
	
	template <typename Tint>
	static inline Tint reverse(Tint value)
	{
		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 inline value_type calculate_table_entry(byte_type pos)
	{
		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 value_type table[256];
	
	static inline void fill_table()
	{
		for(std::size_t i = 0; i < 256; ++i)
		{
			table[i] = calculate_table_entry(static_cast<byte_type>(i));
		}
	}
	
	struct table_filler
	{
		inline table_filler()
		{
			self_type::fill_table();
		}
	};

	static inline void init()
	{
		static table_filler table_filler;
	}

private:

	inline value_type read_table(byte_type pos) const
	{
		return table[pos];
	}

private:

	value_type value;

public:

	crc()
		: value(initial)
	{
		init();
	}

	inline void processByte(byte_type byte)
	{
		MPT_CONSTANT_IF(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));
		}
	}

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

public:

	inline operator value_type () const
	{
		return result();
	}

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

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

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

#if MPT_BYTE_IS_STD_BYTE
	inline crc & process(mpt::byte c)
	{
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}
#endif

	template <typename InputIt>
	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)
	{
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}

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

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

#if MPT_BYTE_IS_STD_BYTE
	inline crc & operator () (mpt::byte c)
	{
		processByte(mpt::byte_cast<byte_type>(c));
		return *this;
	}
#endif

	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)
	{
		init();
		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)
	{
		init();
		process(data.begin(), data.end());
	}

};

template <typename T, T polynomial, T initial, T resultXOR, bool reverseData>
typename crc<T, polynomial, initial, resultXOR, reverseData>::value_type crc<T, polynomial, initial, resultXOR, reverseData>::table[256];

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

} // namespace checksum

using mpt::checksum::crc32;
using mpt::checksum::crc32_ogg;

} // namespace mpt

OPENMPT_NAMESPACE_END