#define POLY (0xEDB88320L)
#ifdef GENPROGRAM
-#define INITFUNC /* the gen program needs the init func :-) */
+#define INITFUNC /* the gen program needs the init func :-) */
#endif
#ifdef INITFUNC
*/
static unsigned long crc32_table[256];
-void crc32_init(void) {
+void crc32_init(void)
+{
unsigned long crcword;
int i;
-
+
for (i = 0; i < 256; i++) {
- unsigned long newbyte, x32term;
- int j;
- crcword = 0;
- newbyte = i;
- for (j = 0; j < 8; j++) {
- x32term = (crcword ^ newbyte) & 1;
- crcword = (crcword >> 1) ^ (x32term * POLY);
- newbyte >>= 1;
- }
- crc32_table[i] = crcword;
+ unsigned long newbyte, x32term;
+ int j;
+ crcword = 0;
+ newbyte = i;
+ for (j = 0; j < 8; j++) {
+ x32term = (crcword ^ newbyte) & 1;
+ crcword = (crcword >> 1) ^ (x32term * POLY);
+ newbyte >>= 1;
+ }
+ crc32_table[i] = crcword;
}
}
#endif
#ifdef GENPROGRAM
-int main(void) {
+int main(void)
+{
unsigned long crcword;
int i;
crc32_init();
for (i = 0; i < 256; i++) {
- printf("%s0x%08XL%s",
- (i % 4 == 0 ? " " : " "),
- crc32_table[i],
- (i % 4 == 3 ? (i == 255 ? "\n" : ",\n") : ","));
+ printf("%s0x%08XL%s",
+ (i % 4 == 0 ? " " : " "),
+ crc32_table[i],
+ (i % 4 == 3 ? (i == 255 ? "\n" : ",\n") : ","));
}
return 0;
}
#endif
-unsigned long crc32(const void *buf, size_t len) {
- unsigned long crcword = 0L;
+unsigned long crc32_update(unsigned long crcword, const void *buf, size_t len)
+{
const unsigned char *p = (const unsigned char *) buf;
while (len--) {
- unsigned long newbyte = *p++;
- newbyte ^= crcword & 0xFFL;
- crcword = (crcword >> 8) ^ crc32_table[newbyte];
+ unsigned long newbyte = *p++;
+ newbyte ^= crcword & 0xFFL;
+ crcword = (crcword >> 8) ^ crc32_table[newbyte];
}
return crcword;
}
+
+unsigned long crc32(const void *buf, size_t len)
+{
+ return crc32_update(0L, buf, len);
+}