bugfix (fdctl is not used)
[vde.git] / vde-2 / vde_cryptcab / crc32.c
blobe13b333ed5423b948a1e4a0b7a6f600e983ad024
1 /*
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 */
16 #include <stdio.h>
17 #include <stdlib.h>
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
22 * functions!
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
29 * the result.
31 u_int32_t chksum_crc32 (unsigned char *block, unsigned int length)
33 register unsigned long crc;
34 unsigned long i;
36 crc = 0xFFFFFFFF;
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;
54 return res;
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;
66 int i, j;
68 poly = 0xEDB88320L;
69 for (i = 0; i < 256; i++)
71 crc = i;
72 for (j = 8; j > 0; j--)
74 if (crc & 1)
76 crc = (crc >> 1) ^ poly;
78 else
80 crc >>= 1;
83 crc_tab[i] = crc;