3 * Copyright © 2006-2008 Daniele Lacamera
4 * from an idea by Renzo Davoli
6 * Released under the terms of GNU GPL v.2
7 * (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
8 * with the additional exemption that
9 * compiling, linking, and/or using OpenSSL is allowed.
11 * based on implementation by Finn Yannick Jacobs Krzysztof Dabrowski, ElysiuM deeZine
17 #include <sys/types.h>
21 #include <vdecommon.h>
23 /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
24 * so make sure, you call it before using the other
27 u_int32_t crc_tab
[256];
29 /* chksum_crc() -- to a given block, this one calculates the
30 * crc32-checksum until the length is
31 * reached. the crc32-checksum will be
34 u_int32_t
chksum_crc32 (unsigned char *block
, unsigned int length
)
36 register unsigned long crc
;
40 for (i
= 0; i
< length
; i
++)
42 crc
= ((crc
>> 8) & 0x00FFFFFF) ^ crc_tab
[(crc
^ *block
++) & 0xFF];
44 return (crc
^ 0xFFFFFFFF);
48 unsigned char *crc32(unsigned char *block
, unsigned int len
)
50 register unsigned long crc
=chksum_crc32(block
,len
);
51 unsigned char *res
=malloc(4);
53 res
[0]=crc
&0x000000FF;
54 res
[1]=(crc
&0x0000FF00)>>8;
55 res
[2]=(crc
&0x00FF0000)>>16;
56 res
[3]=(crc
&0xFF000000)>>24;
61 /* chksum_crc32gentab() -- to a global crc_tab[256], this one will
62 * calculate the crcTable for crc32-checksums.
63 * it is generated to the polynom [..]
66 void chksum_crc32gentab ()
68 unsigned long crc
, poly
;
72 for (i
= 0; i
< 256; i
++)
75 for (j
= 8; j
> 0; j
--)
79 crc
= (crc
>> 1) ^ poly
;