Oops. Should commit patches correctly.
[Rockbox.git] / firmware / common / crc32-mi4.c
blob8956364f10add8009fbca2e67afc7ac057b75587
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: crc32.c 10464 2006-08-05 20:19:10Z miipekk $
10 * Copyright (C) 2007 Barry Wardell
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 * We can't use the CRC32 implementation in the firmware library as it uses a
22 * different polynomial. The polynomial needed is 0xEDB88320L
24 * CRC32 implementation taken from:
26 * efone - Distributed internet phone system.
28 * (c) 1999,2000 Krzysztof Dabrowski
29 * (c) 1999,2000 ElysiuM deeZine
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
38 /* based on implementation by Finn Yannick Jacobs */
42 /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
43 * so make sure, you call it before using the other
44 * functions!
46 static unsigned int crc_tab[256];
48 /* chksum_crc() -- to a given block, this one calculates the
49 * crc32-checksum until the length is
50 * reached. the crc32-checksum will be
51 * the result.
53 unsigned int chksum_crc32 (unsigned char *block, unsigned int length)
55 register unsigned long crc;
56 unsigned long i;
58 crc = 0;
59 for (i = 0; i < length; i++)
61 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
63 return (crc);
66 /* chksum_crc32gentab() -- to a global crc_tab[256], this one will
67 * calculate the crcTable for crc32-checksums.
68 * it is generated to the polynom [..]
71 void chksum_crc32gentab (void)
73 unsigned long crc, poly;
74 int i, j;
76 poly = 0xEDB88320L;
77 for (i = 0; i < 256; i++)
79 crc = i;
80 for (j = 8; j > 0; j--)
82 if (crc & 1)
84 crc = (crc >> 1) ^ poly;
86 else
88 crc >>= 1;
91 crc_tab[i] = crc;