sandbox
Loading...
Searching...
No Matches
crc32.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_UTILITY_CRC32_HPP_
3#define LIBSBX_UTILITY_CRC32_HPP_
4
5#include <cstdint>
6
7#include <span>
8#include <concepts>
9
10namespace sbx::utility {
11
12auto crc32(std::span<const std::uint8_t> buffer, std::uint32_t crc = 0xFFFFFFFFu) -> std::uint32_t;
13
14template<std::unsigned_integral Type, std::size_t N>
15requires (sizeof(Type) == N - 1u)
16static consteval auto make_magic(const char (&string)[N]) -> Type {
17 auto result = Type{};
18
19 for (auto i = 0u; i < N - 1u; ++i) {
20 result |= static_cast<Type>(static_cast<unsigned char>(string[i])) << (i * 8u);
21 }
22
23 return result;
24}
25
26} // namespace sbx::utility
27
28#endif // LIBSBX_UTILITY_CRC32_HPP_