CRC32 implementation

This commit is contained in:
Vladyslav Baranovskyi
2024-11-20 19:35:07 +02:00
parent 33ca4213b6
commit d2beedb8a2

View File

@@ -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 <stdint.h>
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;
}
};