Debian Codegraph @

src/mpt/crc/crc.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_CRC_CRC_HPP
4 #define MPT_CRC_CRC_HPP
5
6
7
8 #include "mpt/base/array.hpp"
9 #include "mpt/base/detect.hpp"
10 #include "mpt/base/integer.hpp"
11 #include "mpt/base/memory.hpp"
12 #include "mpt/base/namespace.hpp"
13
14 #include <array>
15
16 #include <cstddef>
17
18
19
20 namespace mpt {
21 inline namespace MPT_INLINE_NS {
22
23
24
25 template <typename T, T polynomial, T initial, T resultXOR, bool reverseData>
26 class crc {
27
28 public:
29 using self_type = crc;
30 using value_type = T;
31 using byte_type = uint8;
32
33 static constexpr std::size_t size_bytes = sizeof(value_type);
34 static constexpr std::size_t size_bits = sizeof(value_type) * 8;
35 static constexpr value_type top_bit = static_cast<value_type>(1) << ((sizeof(value_type) * 8) - 1);
36
37 private:
38 template <typename Tint>
39 static constexpr Tint reverse(Tint value) noexcept {
40 const std::size_t bits = sizeof(Tint) * 8;
41 Tint result = 0;
42 for (std::size_t i = 0; i < bits; ++i) {
43 result <<= 1;
44 result |= static_cast<Tint>(value & 0x1);
45 value >>= 1;
46 }
47 return result;
48 }
49
50 static constexpr value_type calculate_table_entry(byte_type pos) noexcept {
51 value_type value = 0;
52 value = (static_cast<value_type>(reverseData ? reverse(pos) : pos) << (size_bits - 8));
53 for (std::size_t bit = 0; bit < 8; ++bit) {
54 if (value & top_bit) {
55 value = (value << 1) ^ polynomial;
56 } else {
57 value = (value << 1);
58 }
59 }
60 value = (reverseData ? reverse(value) : value);
61 return value;
62 }
63
64 private:
65 static constexpr std::array<value_type, 256> calculate_table() noexcept {
66 std::array<value_type, 256> t = mpt::init_array<value_type, 256>(value_type{});
67 for (std::size_t i = 0; i < 256; ++i) {
68 t[i] = calculate_table_entry(static_cast<byte_type>(i));
69 }
70 return t;
71 }
72
73 static constexpr std::array<value_type, 256> table = calculate_table();
74
75 private:
76 constexpr value_type read_table(byte_type pos) const noexcept {
77 return table[pos];
78 }
79
80 private:
81 value_type value;
82
83 public:
84 constexpr crc() noexcept
85 : value(initial) {
86 return;
87 }
88
89 constexpr void processByte(byte_type byte) noexcept {
90 if constexpr (reverseData) {
91 value = (value >> 8) ^ read_table(static_cast<byte_type>((value & 0xff) ^ byte));
92 } else {
93 value = (value << 8) ^ read_table(static_cast<byte_type>(((value >> (size_bits - 8)) & 0xff) ^ byte));
94 }
95 }
96
97 constexpr value_type result() const noexcept {
98 return (value ^ resultXOR);
99 }
100
101 public:
102 constexpr operator value_type() const noexcept {
103 return result();
104 }
105
106 inline crc & process(char c) noexcept {
107 processByte(mpt::byte_cast<byte_type>(c));
108 return *this;
109 }
110
111 inline crc & process(signed char c) noexcept {
112 processByte(static_cast<byte_type>(c));
113 return *this;
114 }
115
116 inline crc & process(unsigned char c) noexcept {
117 processByte(mpt::byte_cast<byte_type>(c));
118 return *this;
119 }
120
121 inline crc & process(std::byte c) noexcept {
122 processByte(mpt::byte_cast<byte_type>(c));
123 return *this;
124 }
125
126 template <typename InputIt>
127 inline crc & process(InputIt beg, InputIt end) {
128 for (InputIt it = beg; it != end; ++it) {
129 static_assert(sizeof(*it) == 1, "1 byte type required");
130 process(*it);
131 }
132 return *this;
133 }
134
135 template <typename Container>
136 inline crc & process(const Container & data) {
137 operator()(data.begin(), data.end());
138 return *this;
139 }
140
141 inline crc & operator()(char c) noexcept {
142 processByte(mpt::byte_cast<byte_type>(c));
143 return *this;
144 }
145
146 inline crc & operator()(signed char c) noexcept {
147 processByte(static_cast<byte_type>(c));
148 return *this;
149 }
150
151 inline crc & operator()(unsigned char c) noexcept {
152 processByte(mpt::byte_cast<byte_type>(c));
153 return *this;
154 }
155
156 inline crc & operator()(std::byte c) noexcept {
157 processByte(mpt::byte_cast<byte_type>(c));
158 return *this;
159 }
160
161 template <typename InputIt>
162 crc & operator()(InputIt beg, InputIt end) {
163 for (InputIt it = beg; it != end; ++it) {
164 static_assert(sizeof(*it) == 1, "1 byte type required");
165 operator()(*it);
166 }
167 return *this;
168 }
169
170 template <typename Container>
171 inline crc & operator()(const Container & data) {
172 operator()(data.begin(), data.end());
173 return *this;
174 }
175
176 template <typename InputIt>
177 crc(InputIt beg, InputIt end)
178 : value(initial) {
179 for (InputIt it = beg; it != end; ++it) {
180 static_assert(sizeof(*it) == 1, "1 byte type required");
181 process(*it);
182 }
183 }
184
185 template <typename Container>
186 inline crc(const Container & data)
187 : value(initial) {
188 process(data.begin(), data.end());
189 }
190 };
191
192 using crc16 = crc<uint16, 0x8005, 0, 0, true>;
193 using crc32 = crc<uint32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true>;
194 using crc32_ogg = crc<uint32, 0x04C11DB7, 0, 0, false>;
195 using crc32c = crc<uint32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true>;
196 using crc64_jones = crc<uint64, 0xAD93D23594C935A9ull, 0xFFFFFFFFFFFFFFFFull, 0, true>;
197
198
199 } // namespace MPT_INLINE_NS
200 } // namespace mpt
201
202
203
204 #endif // MPT_CRC_CRC_HPP