static unsigned long crctable[256]; static unsigned char crcinvtable[256]; void init_crc(void) { unsigned long poly = 0xedb88320, value, idx, i; for (idx = 0 ; idx < 256 ; idx++) { for (value = idx, i = 0 ; i < 8 ; i++) { if (value & 1) { value >>= 1; value ^= poly; } else { value >>= 1; } } crctable[idx] = value; crcinvtable[value >> 24] = idx & 0xff; } } static inline unsigned long iter_crc(unsigned long crc, unsigned char chr) { return (crc >> 8) ^ crctable[chr ^ (crc & 255)]; } unsigned long calc_crc(unsigned char *buf, unsigned long buflen, unsigned long crc) { while (buflen--) crc = iter_crc(crc, *buf++); return ~crc; }