Barry Wardell | 54c73a2 | 2007-06-04 13:48:21 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Barry Wardell | 54c73a2 | 2007-06-04 13:48:21 +0000 | [diff] [blame] | 16 | * |
| 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 Sikken | e15f8a2 | 2008-05-03 08:35:14 +0000 | [diff] [blame] | 42 | #include "crc32-mi4.h" |
Barry Wardell | 54c73a2 | 2007-06-04 13:48:21 +0000 | [diff] [blame] | 43 | |
| 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 | */ |
| 48 | static 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 | */ |
| 55 | unsigned 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 | |
| 73 | void 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 | } |