stuff-from-scratch/src/base/compression/CyclicRedundancyChecker.cpp
2023-01-23 11:06:30 +00:00

43 lines
896 B
C++

#include "CyclicRedundancyChecker.h"
void CyclicRedundancyChecker::createTable()
{
mTable = std::vector<unsigned long>(TABLE_SIZE, 0);
unsigned long c{0};
for (std::size_t n = 0; n < TABLE_SIZE; n++)
{
c = (unsigned long) n;
for (int k = 0; k < 8; k++)
{
if (c & 1)
{
c = 0xedb88320L ^ (c >> 1);
}
else
{
c = c >> 1;
}
}
mTable[n] = c;
}
mTableComputed = true;
}
void CyclicRedundancyChecker::addValue(unsigned char val)
{
if (!mTableComputed)
{
createTable();
}
mLastValue = mTable[(mLastValue ^ val) & 0xff] ^ (mLastValue >> 8);
}
uint32_t CyclicRedundancyChecker::getChecksum() const
{
return mLastValue ^ 0xffffffffL;
}
void CyclicRedundancyChecker::reset()
{
mLastValue = 0xffffffffL;
}