<span style="font-family: Arial, Helvetica, sans-serif;">#ifndef __CRC32_H__</span>
#define __CRC32_H__
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
uint32_t crc32(uint32_t crc, const uint8_t *buf, int len);
#ifdef __cplusplus
}
#endif
#endif
<pre name="code" class="cpp">#include <stdio.h>
#include "crc32.h"
#define DO1(buf) crc = crc_table(((int)crc ^ (*buf++)) & 0xff) ^ (crc >> 8);
#define DO2(buf) DO1(buf); DO1(buf);
#define DO4(buf) DO2(buf); DO2(buf);
#define DO8(buf) DO4(buf); DO4(buf);
static uint32_t crc_table(uint32_t index)
{
uint32_t c = index;
uint32_t poly = 0xedb88320L;
int k;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
return c;
}
uint32_t crc32(uint32_t crc, const uint8_t *buf, int len)
{
if (buf == NULL) return 0L;
crc = crc ^ 0xffffffffL;
while (len >= 8)
{
DO8(buf);
len -= 8;
}
if (len) do {
DO1(buf);
} while (--len);
return crc ^ 0xffffffffL;
}