diff options
Diffstat (limited to 'dev/lib/utility')
| -rw-r--r-- | dev/lib/utility/crc32.hpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/dev/lib/utility/crc32.hpp b/dev/lib/utility/crc32.hpp index de78d4e..fe9f770 100644 --- a/dev/lib/utility/crc32.hpp +++ b/dev/lib/utility/crc32.hpp @@ -1,6 +1,6 @@ /* - * File: embfs.hpp - * Purpose: Embedded File System. + * File: crc32.hpp + * Purpose: CRC32 module. * Author: Amlal El Mahrouss, * Copyright 2025, Amlal El Mahrouss all rights reserved. */ @@ -9,6 +9,7 @@ #define _SNU_CRC32_HPP #include <cstdint> +#include <string> #include <cstddef> /// @brief Crc32 implementation in C++ @@ -16,11 +17,11 @@ namespace snu::crc32 { - namespace details + namespace detail { - inline constexpr const std::uint16_t g_crc_sz = 256; + inline constexpr const std::uint16_t crc_sz_ = 256U; - inline std::uint32_t g_crc_table[g_crc_sz] = { + inline std::uint32_t crc_array_[crc_sz_] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, @@ -53,22 +54,28 @@ namespace snu::crc32 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; - /// + template <typename char_type> inline std::uint32_t - calculate_crc32_(const char* in, size_t len) noexcept + crc32(const char_type* in, size_t len) noexcept { if (!in || *in == 0) - return ~0; + return 0; std::uint32_t crc = 0xffffffff; while ((len--) > 0) - crc = (crc >> 8) ^ g_crc_table[(crc ^ *(in++)) & 0xFF]; + crc = (crc >> 8) ^ crc_array_[(crc ^ *(in++)) & 0xFF]; return ~crc; } - } // namespace details + } // namespace detail + + template <typename char_type = char> + inline std::uint32_t hash(const std::basic_string<char_type>& in) + { + return detail::crc32(in.c_str(), in.size()); + } } // namespace snu::crc32 #endif // !_SNU_CRC32_HPP
\ No newline at end of file |
