]> CyberLeo.Net >> Repos - CDN/.attic/crc32sum.git/blob - crc32.c
Stub manpage
[CDN/.attic/crc32sum.git] / crc32.c
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  * Hacked by CyberLeo to make it capable of digesting long streams
13  *  one buffer at a time. - 20090105
14  */
15
16 /* based on implementation by Finn Yannick Jacobs */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21
22 /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
23  *                so make sure, you call it before using the other
24  *                functions!
25  */
26 uint32_t chksum_crc_tab[256];
27
28 /* chksum_crc() -- to a given block, this one calculates the
29  *                                crc32-checksum until the length is
30  *                                reached. the crc32-checksum will be
31  *                                the result.
32  */
33 uint32_t chksum_crc32 (unsigned char *block, unsigned int length, uint32_t *chksum_crc)
34 {
35    register unsigned long crc;
36    unsigned long i;
37    
38    crc = *chksum_crc ^ 0xFFFFFFFF;
39    for (i = 0; i < length; i++)
40    {
41       crc = ((crc >> 8) & 0x00FFFFFF) ^ chksum_crc_tab[(crc ^ *block++) & 0xFF];
42    }
43    *chksum_crc = crc ^ 0xFFFFFFFF;
44    return *chksum_crc;
45 }
46
47 /* chksum_crc32_gentab() --      to a global crc_tab[256], this one will
48  *                                calculate the crcTable for crc32-checksums.
49  *                                it is generated to the polynom [..]
50  */
51
52 void chksum_crc32_gentab ()
53 {
54    unsigned long crc, poly;
55    int i, j;
56
57    poly = 0xEDB88320L;
58    for (i = 0; i < 256; i++)
59    {
60       crc = i;
61       for (j = 8; j > 0; j--)
62       {
63          if (crc & 1)
64          {
65             crc = (crc >> 1) ^ poly;
66          }
67          else
68          {
69             crc >>= 1;
70          }
71       }
72       chksum_crc_tab[i] = crc;
73    }
74 }