Bookmark.c cleanup, still no functional changes... yet
[kugel-rb.git] / firmware / common / crc32.c
blob1cd0ca0bd574acce087d27bf1437e1d5347932aa
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Jörg Hohensohn [IDC]Dragon
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* Code copied from firmware_flash plugin. */
24 #include "crc32.h"
26 /* Tool function to calculate a CRC32 across some buffer */
27 /* third argument is either 0xFFFFFFFF to start or value from last piece */
28 unsigned crc_32(const void *src, unsigned len, unsigned crc32)
30 const unsigned char *buf = (const unsigned char *)src;
32 /* CCITT standard polynomial 0x04C11DB7 */
33 static const unsigned crc32_lookup[16] =
34 { /* lookup table for 4 bits at a time is affordable */
35 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9,
36 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005,
37 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
38 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD
41 unsigned char byte;
42 unsigned t;
44 while (len--)
46 byte = *buf++; /* get one byte of data */
48 /* upper nibble of our data */
49 t = crc32 >> 28; /* extract the 4 most significant bits */
50 t ^= byte >> 4; /* XOR in 4 bits of data into the extracted bits */
51 crc32 <<= 4; /* shift the CRC register left 4 bits */
52 crc32 ^= crc32_lookup[t]; /* do the table lookup and XOR the result */
54 /* lower nibble of our data */
55 t = crc32 >> 28; /* extract the 4 most significant bits */
56 t ^= byte & 0x0F; /* XOR in 4 bits of data into the extracted bits */
57 crc32 <<= 4; /* shift the CRC register left 4 bits */
58 crc32 ^= crc32_lookup[t]; /* do the table lookup and XOR the result */
61 return crc32;