nmxp_crc32.c 753 Bytes
Newer Older
Matteo Quintiliani's avatar
Matteo Quintiliani committed
1 2 3 4
/*! \file
 *
 * \brief Computing a 32 bit CRC.
 *
5
 * $Id: nmxp_crc32.c,v 1.7 2009-08-31 12:16:41 mtheo Exp $
Matteo Quintiliani's avatar
Matteo Quintiliani committed
6 7 8
 *
 *
 */
9 10 11

#include "nmxp_crc32.h"

12
void crc32_init_table (uint32_t crc32_tab[256]) {
13 14 15 16 17
    unsigned int i, j;
    uint32_t h = 1;
    crc32_tab[0] = 0;
    for (i = 128; i; i >>= 1) {
	h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
18
	/* h is now crc_table[i]*/
19 20 21 22 23 24 25
	for (j = 0; j < 256; j += 2 * i) {
	    crc32_tab[i + j] = crc32_tab[j] ^ h;
	}
    }
}

uint32_t crc32(uint32_t crc32val, const char *s, uint32_t len)
26
{
27
	uint32_t i;
28
	uint32_t crc32_tab[256];
29

30
	crc32_init_table(crc32_tab);
31

32 33 34
	crc32val ^= 0xffffffff;
	for (i = 0;  i < len;  i ++) {
		crc32val = crc32_tab[(crc32val ^ s[i]) & 0xff] ^ (crc32val >> 8);
35
	}
36

37
	return crc32val ^ 0xffffffff;
38 39
}