blob: 2b9ee9a384ca1ce57a22f50b5aff34bda7fac6de [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/*
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>
srs5694221e0872009-08-29 15:00:31 -040018#include <sys/types.h>
srs5694e7b4ff92009-08-18 13:16:10 -040019
20/* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
21 * so make sure, you call it before using the other
22 * functions!
23 */
24u_int32_t crc_tab[256];
25
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.
30 */
31u_int32_t chksum_crc32 (unsigned char *block, unsigned int length)
32{
33 register unsigned long crc;
34 unsigned long i;
35
36 crc = 0xFFFFFFFF;
37 for (i = 0; i < length; i++)
38 {
39 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
40 }
41 return (crc ^ 0xFFFFFFFF);
42}
43
44/* chksum_crc32gentab() -- to a global crc_tab[256], this one will
45 * calculate the crcTable for crc32-checksums.
46 * it is generated to the polynom [..]
47 */
48
49void chksum_crc32gentab ()
50{
51 unsigned long crc, poly;
52 int i, j;
53
54 poly = 0xEDB88320L;
55 for (i = 0; i < 256; i++)
56 {
57 crc = i;
58 for (j = 8; j > 0; j--)
59 {
60 if (crc & 1)
61 {
62 crc = (crc >> 1) ^ poly;
63 }
64 else
65 {
66 crc >>= 1;
67 }
68 }
69 crc_tab[i] = crc;
70 }
71}