GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
CRC32.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_FORMATS_CRC32_HPP
5#define GREM_CORE_FORMATS_CRC32_HPP
6
8
13
14#include <cstddef> // std::size_t
15#include <functional> // std::hash
16
17namespace grem {
18
19class CRC32 {
20private:
21 static constexpr Array<uint32_t, 256> CRC_TABLE = [] {
22 Array<uint32_t, 256> result{};
23 uint32_t crc32 = 1;
24 for (size_t i = 128; i != 0; i >>= 1) {
25 crc32 = (crc32 >> 1) ^ ((crc32 & 1) ? 0xEDB88320 : 0);
26 for (size_t j = 0; j < 256; j += i * 2) {
27 result[i + j] = crc32 ^ result[j];
28 }
29 }
30 return result;
31 }();
32
33public:
34 constexpr CRC32() noexcept = default;
35
36 constexpr explicit CRC32(uint32_t value) noexcept
37 : value(value) {}
38
39 constexpr explicit CRC32(Span<const byte> bytes) noexcept {
40 append(bytes);
41 }
42
43 constexpr explicit CRC32(StringView chars) noexcept {
44 append(chars);
45 }
46
47 constexpr explicit operator uint32_t() const noexcept {
48 return value;
49 }
50
51 constexpr bool operator==(const CRC32&) const noexcept = default;
52
53 constexpr void append(Span<const byte> bytes) noexcept {
54 value = ~value;
55 for (const byte byte : bytes) {
56 value = (value >> 8) ^ CRC_TABLE[(value ^ static_cast<uint32_t>(bit_cast<uint8_t>(byte))) & 0xFF];
57 }
58 value = ~value;
59 }
60
61 constexpr void append(StringView chars) noexcept {
62 value = ~value;
63 for (const char ch : chars) {
64 value = (value >> 8) ^ CRC_TABLE[(value ^ static_cast<uint32_t>(bit_cast<uint8_t>(ch))) & 0xFF];
65 }
66 value = ~value;
67 }
68
69 constexpr CRC32& operator+=(Span<const byte> bytes) noexcept {
70 append(bytes);
71 return *this;
72 }
73
74 constexpr CRC32& operator+=(StringView chars) noexcept {
75 append(chars);
76 return *this;
77 }
78
79 [[nodiscard]] friend constexpr CRC32 operator+(CRC32 a, Span<const byte> bytes) noexcept {
80 return a += bytes;
81 }
82
83 [[nodiscard]] friend constexpr CRC32 operator+(CRC32 a, StringView chars) noexcept {
84 return a += chars;
85 }
86
87 template <typename Stream>
88 friend constexpr Stream& operator<<(Stream& stream, CRC32 crc32) {
89 return stream << crc32.value;
90 }
91
92 template <typename Stream>
93 friend constexpr Stream& operator>>(Stream& stream, CRC32& crc32) {
94 return stream >> crc32.value;
95 }
96
97private:
98 uint32_t value = 0;
99};
100
101} // namespace grem
102
103template <>
104struct std::hash<grem::CRC32> {
105 [[nodiscard]] std::size_t operator()(const grem::CRC32& crc32) const {
106 return hasher(static_cast<uint32_t>(crc32));
107 }
108
109private:
110 std::hash<uint32_t> hasher;
111};
112
113#endif
Definition CRC32.hpp:19
friend constexpr CRC32 operator+(CRC32 a, Span< const byte > bytes) noexcept
Definition CRC32.hpp:79
friend constexpr Stream & operator>>(Stream &stream, CRC32 &crc32)
Definition CRC32.hpp:93
constexpr CRC32 & operator+=(StringView chars) noexcept
Definition CRC32.hpp:74
constexpr CRC32(StringView chars) noexcept
Definition CRC32.hpp:43
constexpr void append(StringView chars) noexcept
Definition CRC32.hpp:61
constexpr CRC32 & operator+=(Span< const byte > bytes) noexcept
Definition CRC32.hpp:69
friend constexpr Stream & operator<<(Stream &stream, CRC32 crc32)
Definition CRC32.hpp:88
constexpr CRC32() noexcept=default
friend constexpr CRC32 operator+(CRC32 a, StringView chars) noexcept
Definition CRC32.hpp:83
constexpr bool operator==(const CRC32 &) const noexcept=default
constexpr CRC32(Span< const byte > bytes) noexcept
Definition CRC32.hpp:39
constexpr void append(Span< const byte > bytes) noexcept
Definition CRC32.hpp:53
Definition Span.hpp:146
Definition aliases.hpp:9
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
Definition Array.hpp:20
std::size_t operator()(const grem::CRC32 &crc32) const
Definition CRC32.hpp:105