srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 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 |
|
| 19 | /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
|
| 20 | * so make sure, you call it before using the other
|
| 21 | * functions!
|
| 22 | */
|
| 23 | u_int32_t crc_tab[256];
|
| 24 |
|
| 25 | /* chksum_crc() -- to a given block, this one calculates the
|
| 26 | * crc32-checksum until the length is
|
| 27 | * reached. the crc32-checksum will be
|
| 28 | * the result.
|
| 29 | */
|
| 30 | u_int32_t chksum_crc32 (unsigned char *block, unsigned int length)
|
| 31 | {
|
| 32 | register unsigned long crc;
|
| 33 | unsigned long i;
|
| 34 |
|
| 35 | crc = 0xFFFFFFFF;
|
| 36 | for (i = 0; i < length; i++)
|
| 37 | {
|
| 38 | crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
|
| 39 | }
|
| 40 | return (crc ^ 0xFFFFFFFF);
|
| 41 | }
|
| 42 |
|
| 43 | /* chksum_crc32gentab() -- to a global crc_tab[256], this one will
|
| 44 | * calculate the crcTable for crc32-checksums.
|
| 45 | * it is generated to the polynom [..]
|
| 46 | */
|
| 47 |
|
| 48 | void chksum_crc32gentab ()
|
| 49 | {
|
| 50 | unsigned long crc, poly;
|
| 51 | int i, j;
|
| 52 |
|
| 53 | poly = 0xEDB88320L;
|
| 54 | for (i = 0; i < 256; i++)
|
| 55 | {
|
| 56 | crc = i;
|
| 57 | for (j = 8; j > 0; j--)
|
| 58 | {
|
| 59 | if (crc & 1)
|
| 60 | {
|
| 61 | crc = (crc >> 1) ^ poly;
|
| 62 | }
|
| 63 | else
|
| 64 | {
|
| 65 | crc >>= 1;
|
| 66 | }
|
| 67 | }
|
| 68 | crc_tab[i] = crc;
|
| 69 | }
|
| 70 | }
|