From d2beedb8a2de2c66128f64be8f8b7f981d3bd79b Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Wed, 20 Nov 2024 19:35:07 +0200 Subject: [PATCH] CRC32 implementation --- openVulkanoCpp/Math/CRC32.hpp | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 openVulkanoCpp/Math/CRC32.hpp diff --git a/openVulkanoCpp/Math/CRC32.hpp b/openVulkanoCpp/Math/CRC32.hpp new file mode 100644 index 0000000..7c95649 --- /dev/null +++ b/openVulkanoCpp/Math/CRC32.hpp @@ -0,0 +1,52 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +class CRC32 +{ + uint32_t m_table[256]; + uint32_t m_currentValue = 0; + +public: + CRC32() + { + uint32_t polynomial = 0xEDB88320; + for (uint32_t i = 0; i < 256; i++) + { + uint32_t c = i; + for (size_t j = 0; j < 8; j++) + { + if (c & 1) + { + c = polynomial ^ (c >> 1); + } + else + { + c >>= 1; + } + } + m_table[i] = c; + } + } + + void Update(size_t len, const uint8_t* buf) + { + uint32_t c = m_currentValue ^ 0xFFFFFFFF; + for (size_t i = 0; i < len; ++i) + { + c = m_table[(c ^ buf[i]) & 0xFF] ^ (c >> 8); + } + m_currentValue = c ^ 0xFFFFFFFF; + } + + uint32_t GetValue() const + { + return m_currentValue; + } +}; \ No newline at end of file