40 lines
992 B
C++
40 lines
992 B
C++
/* ================================================================
|
|
Checksum functionality
|
|
----------------
|
|
checksum.cpp/checksum.hpp provide functionality for checking and generating checksums.
|
|
================================================================ */
|
|
#include "checksum.hpp"
|
|
#include <stdio.h>
|
|
|
|
uint32_t crc32(uint32_t crc, const char *buf, size_t len) {
|
|
static uint32_t table[256];
|
|
static int table_gen = 0;
|
|
uint32_t word;
|
|
uint8_t octet;
|
|
int i, j;
|
|
const char *p, *q;
|
|
// Generate our table if not yet generated
|
|
if (table_gen == 0) {
|
|
for (i = 0; i < 256; i++) {
|
|
word = i;
|
|
for (j = 0; j < 8; j++) {
|
|
if (word & 1) {
|
|
word = (word >> 1) ^ 0xedb88320;
|
|
} else {
|
|
word >>= 1;
|
|
}
|
|
}
|
|
table[i] = word;
|
|
}
|
|
table_gen = 1;
|
|
}
|
|
// Generate our CRC32!
|
|
crc = ~crc;
|
|
q = buf + len;
|
|
for (p = buf; p < q; p++) {
|
|
octet = *p;
|
|
crc = (crc >> 8) ^ table[(crc & 0xff) ^ octet];
|
|
}
|
|
return ~crc;
|
|
}
|