blob: 5eca100e046ef997c50f592af897edfae3a2c08e [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>
srs5694546a9c72010-01-26 16:00:26 -050019#include "crc32.h"
srs5694e7b4ff92009-08-18 13:16:10 -040020
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 */
srs5694546a9c72010-01-26 16:00:26 -050025uint32_t crc_tab[256];
srs5694e7b4ff92009-08-18 13:16:10 -040026
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 */
srs5694546a9c72010-01-26 16:00:26 -050032uint32_t chksum_crc32 (unsigned char *block, unsigned int length)
srs5694e7b4ff92009-08-18 13:16:10 -040033{
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070034 unsigned long crc;
srs5694e7b4ff92009-08-18 13:16:10 -040035 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
50void 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}