2 * C Implementation: crc32
4 * code from http://www.w3.org/TR/PNG/#D-CRCAppendix
9 /* Table of CRCs of all 8-bit messages. */
10 static unsigned long crc_table
[256];
12 /* Flag: has the table been computed? Initially false. */
13 static int crc_table_computed
= 0;
15 /* Make the table for a fast CRC. */
16 static void make_crc_table(void)
21 for (n
= 0; n
< 256; n
++) {
22 c
= (unsigned long) n
;
23 for (k
= 0; k
< 8; k
++) {
25 c
= 0xedb88320L
^ (c
>> 1);
31 crc_table_computed
= 1;
35 /* Update a running CRC with the bytes buf[0..len-1]--the CRC
36 should be initialized to all 1's, and the transmitted value
37 is the 1's complement of the final running CRC (see the
38 crc() routine below). */
40 static unsigned long update_crc(unsigned long crc
, unsigned char *buf
, int len
)
42 unsigned long c
= crc
;
45 if (!crc_table_computed
)
47 for (n
= 0; n
< len
; n
++) {
48 c
= crc_table
[(c
^ buf
[n
]) & 0xff] ^ (c
>> 8);
53 /* Return the CRC of the bytes buf[0..len-1]. */
54 unsigned long mpc_crc32(unsigned char *buf
, int len
)
56 return update_crc(0xffffffffL
, buf
, len
) ^ 0xffffffffL
;