vde_cryptcab refactoring:
[vde.git] / vde-2 / src / vde_cryptcab / crc32.c
blobdd2f35be7e732f4da6c247353d26d026d4268890
1 /*
2 * VDE Cryptcab
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
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
19 #include <config.h>
20 #include <vde.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
25 * functions!
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
32 * the result.
34 u_int32_t chksum_crc32 (unsigned char *block, unsigned int length)
36 register unsigned long crc;
37 unsigned long i;
39 crc = 0xFFFFFFFF;
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;
57 return res;
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;
69 int i, j;
71 poly = 0xEDB88320L;
72 for (i = 0; i < 256; i++)
74 crc = i;
75 for (j = 8; j > 0; j--)
77 if (crc & 1)
79 crc = (crc >> 1) ^ poly;
81 else
83 crc >>= 1;
86 crc_tab[i] = crc;