CRC implementations use tables for speed. They are not required.
Below is a short CRC32 using either the Castagnoli polynomial (the same as used by Intel crc32) or the Ethernet polynomial (the same as used in zip, gzip, etc.).
#include <stddef.h> #include <stdint.h> /* CRC-32C (iSCSI) polynomial in reversed bit order. */ #define POLY 0x82f63b78 /* CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order. */ /* #define POLY 0xedb88320 */ uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len) { int k; crc = ~crc; while (len--) { crc ^= *buf++; for (k = 0; k < 8; k++) crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1; } return ~crc; }
The initial crc value must be zero. A procedure can be called sequentially chunks of data to update the CRC. You can expand the inner loop for speed, although your compiler can do this for you anyway.
source share