1 /*
2 * efone - Distributed internet phone system.
3 *
4 * (c) 1999,2000 Krzysztof Dabrowski
5 * (c) 1999,2000 ElysiuM deeZine
6 *
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.
11 *
12 */
13
14 /* based on implementation by Finn Yannick Jacobs */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include "crc32.h"
20
21 /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
22 * so make sure, you call it before using the other
23 * functions!
24 */
25 uint32_t crc_tab[256];
26
27 /* chksum_crc() -- to a given block, this one calculates the
28 * crc32-checksum until the length is
29 * reached. the crc32-checksum will be
30 * the result.
31 */
chksum_crc32(unsigned char * block,unsigned int length)32 uint32_t chksum_crc32 (unsigned char *block, unsigned int length)
33 {
34 register unsigned long crc;
35 unsigned long i;
36
37 crc = 0xFFFFFFFF;
38 for (i = 0; i < length; i++)
39 {
40 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
41 }
42 return (crc ^ 0xFFFFFFFF);
43 }
44
45 /* chksum_crc32gentab() -- to a global crc_tab[256], this one will
46 * calculate the crcTable for crc32-checksums.
47 * it is generated to the polynom [..]
48 */
49
chksum_crc32gentab()50 void chksum_crc32gentab ()
51 {
52 unsigned long crc, poly;
53 int i, j;
54
55 poly = 0xEDB88320L;
56 for (i = 0; i < 256; i++)
57 {
58 crc = i;
59 for (j = 8; j > 0; j--)
60 {
61 if (crc & 1)
62 {
63 crc = (crc >> 1) ^ poly;
64 }
65 else
66 {
67 crc >>= 1;
68 }
69 }
70 crc_tab[i] = crc;
71 }
72 }
73