2 * efone - Distributed internet phone system.
4 * (c) 1999,2000 Krzysztof Dabrowski
5 * (c) 1999,2000 ElysiuM deeZine
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 /* based on implementation by Finn Yannick Jacobs */
18 #include <sys/types.h>
20 /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
21 * so make sure, you call it before using the other
24 u_int32_t crc_tab
[256];
26 /* chksum_crc() -- to a given block, this one calculates the
27 * crc32-checksum until the length is
28 * reached. the crc32-checksum will be
31 u_int32_t
chksum_crc32 (unsigned char *block
, unsigned int length
)
33 register unsigned long crc
;
37 for (i
= 0; i
< length
; i
++)
39 crc
= ((crc
>> 8) & 0x00FFFFFF) ^ crc_tab
[(crc
^ *block
++) & 0xFF];
41 return (crc
^ 0xFFFFFFFF);
45 unsigned char *crc32(unsigned char *block
, unsigned int len
)
47 register unsigned long crc
=chksum_crc32(block
,len
);
48 unsigned char *res
=malloc(4);
50 res
[0]=crc
&0x000000FF;
51 res
[1]=(crc
&0x0000FF00)>>8;
52 res
[2]=(crc
&0x00FF0000)>>16;
53 res
[3]=(crc
&0xFF000000)>>24;
58 /* chksum_crc32gentab() -- to a global crc_tab[256], this one will
59 * calculate the crcTable for crc32-checksums.
60 * it is generated to the polynom [..]
63 void chksum_crc32gentab ()
65 unsigned long crc
, poly
;
69 for (i
= 0; i
< 256; i
++)
72 for (j
= 8; j
> 0; j
--)
76 crc
= (crc
>> 1) ^ poly
;