Skip to content

Instantly share code, notes, and snippets.

@junhuanchen
Forked from timepp/crc32.h
Created December 12, 2023 14:15
Show Gist options
  • Save junhuanchen/2c554bed9c372a27c8a3bb1b82265d07 to your computer and use it in GitHub Desktop.
Save junhuanchen/2c554bed9c372a27c8a3bb1b82265d07 to your computer and use it in GitHub Desktop.
simplest crc32 c++ implementation
#pragma once
#include <stdint.h>
struct crc32
{
static void generate_table(uint32_t(&table)[256])
{
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;
}
}
table[i] = c;
}
}
static uint32_t update(uint32_t (&table)[256], uint32_t initial, const void* buf, size_t len)
{
uint32_t c = initial ^ 0xFFFFFFFF;
const uint8_t* u = static_cast<const uint8_t*>(buf);
for (size_t i = 0; i < len; ++i)
{
c = table[(c ^ u[i]) & 0xFF] ^ (c >> 8);
}
return c ^ 0xFFFFFFFF;
}
};
// usage: the following code generates crc for 2 pieces of data
// uint32_t table[256];
// crc32::generate_table(table);
// uint32_t crc = crc32::update(table, 0, data_piece1, len1);
// crc = crc32::update(table, crc, data_piece2, len2);
// output(crc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment