flate

deflate implementation
git clone git://git.suckless.org/flate
Log | Files | Refs | README

crc.c (457B)


      1 #include "flate.h"
      2 
      3 uint crctab[256];
      4 
      5 void crc32init(void) {
      6 	static const uint poly = 0xedb88320;
      7 	int i,j;
      8 
      9 	for (i = 0; i < 256; ++i) {
     10 		uint crc = i;
     11 
     12 		for (j = 0; j < 8; j++) {
     13 			if (crc & 1)
     14 				crc = (crc >> 1) ^ poly;
     15 			else
     16 				crc >>= 1;
     17 		}
     18 		crctab[i] = crc;
     19 	}
     20 }
     21 
     22 uint crc32(uchar *p, int n, uint crc) {
     23 	uchar *ep = p + n;
     24 
     25 	crc ^= 0xffffffff;
     26 	while (p < ep)
     27 		crc = crctab[(crc & 0xff) ^ *p++] ^ (crc >> 8);
     28 	return crc ^ 0xffffffff;
     29 }