blob: 82c6f3e15be6392c4594fd40a3755d120be6718c [file] [log] [blame]
Barry Wardell54c73a22007-06-04 13:48:21 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: crc32.c 10464 2006-08-05 20:19:10Z miipekk $
9 *
10 * Copyright (C) 2007 Barry Wardell
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Barry Wardell54c73a22007-06-04 13:48:21 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22/*
23 * We can't use the CRC32 implementation in the firmware library as it uses a
24 * different polynomial. The polynomial needed is 0xEDB88320L
25 *
26 * CRC32 implementation taken from:
27 *
28 * efone - Distributed internet phone system.
29 *
30 * (c) 1999,2000 Krzysztof Dabrowski
31 * (c) 1999,2000 ElysiuM deeZine
32 *
33 * This program is free software; you can redistribute it and/or
34 * modify it under the terms of the GNU General Public License
35 * as published by the Free Software Foundation; either version
36 * 2 of the License, or (at your option) any later version.
37 *
38 */
39
40/* based on implementation by Finn Yannick Jacobs */
41
Bertrik Sikkene15f8a22008-05-03 08:35:14 +000042#include "crc32-mi4.h"
Barry Wardell54c73a22007-06-04 13:48:21 +000043
44/* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
45 * so make sure, you call it before using the other
46 * functions!
47 */
48static unsigned int crc_tab[256];
49
50/* chksum_crc() -- to a given block, this one calculates the
51 * crc32-checksum until the length is
52 * reached. the crc32-checksum will be
53 * the result.
54 */
55unsigned int chksum_crc32 (unsigned char *block, unsigned int length)
56{
57 register unsigned long crc;
58 unsigned long i;
59
60 crc = 0;
61 for (i = 0; i < length; i++)
62 {
63 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
64 }
65 return (crc);
66}
67
68/* chksum_crc32gentab() -- to a global crc_tab[256], this one will
69 * calculate the crcTable for crc32-checksums.
70 * it is generated to the polynom [..]
71 */
72
73void chksum_crc32gentab (void)
74{
75 unsigned long crc, poly;
76 int i, j;
77
78 poly = 0xEDB88320L;
79 for (i = 0; i < 256; i++)
80 {
81 crc = i;
82 for (j = 8; j > 0; j--)
83 {
84 if (crc & 1)
85 {
86 crc = (crc >> 1) ^ poly;
87 }
88 else
89 {
90 crc >>= 1;
91 }
92 }
93 crc_tab[i] = crc;
94 }
95}