1 /******************************************************************************
2 * Arachnoid Graphics Plugin for Mupen64Plus
3 * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
5 * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
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 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *****************************************************************************/
22 #include "CRCCalculator.h"
24 #define CRC32_POLYNOMIAL 0xedb88320L //0x04C11DB7
25 typedef unsigned char byte;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
30 unsigned int CRCCalculator::m_crcTable[256] = {0};
32 static unsigned int crc_table[256];
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
37 CRCCalculator::CRCCalculator()
39 static bool hashTableInitialized = false;
42 //http://www.gamedev.net/reference/articles/article1941.asp
43 if ( !hashTableInitialized )
48 // for (int i=0; i<256; i++)
50 // crc = _reflect( i, 8 ) << 24;
52 // for (int j = 0; j < 8; j++)
54 // crc = (crc << 1) ^ (crc & (1 << 31) ? CRC32_POLYNOMIAL : 0);
57 // m_crcTable[i] = _reflect( crc, 32 );
61 unsigned int poly; // polynomial exclusive-or pattern
62 // terms of polynomial defining this crc (except x^32):
63 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
65 /* make exclusive-or pattern from polynomial (0xedb88320L) */
67 for (unsigned int n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
69 poly |= 1L << (31 - p[n]);
72 for (int n=0; n<256; ++n)
74 crc = (unsigned int)n;
76 for (int k = 0; k < 8; k++)
78 crc = (crc & 1) ? (poly ^ (crc >> 1)) : crc >> 1;
83 hashTableInitialized = true;
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
91 #define DO1b(buf) crc = (crc >> 8) ^ crc_table[(crc & 0xFF) ^ *buf++]
92 unsigned int CRCCalculator::calcCRC(unsigned int crc, void *buffer, unsigned int count)
94 byte* p = (byte*) buffer;
95 //unsigned int orig = crc;
97 // p = (byte*) buffer;
100 // crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
103 // return crc ^ orig;
105 if (buffer == 0) return 0L;
107 crc = crc ^ 0xffffffffL;
110 crc = crc_table[((int)crc ^ (*p++)) & 0xff] ^ (crc >> 8);
115 return crc ^ 0xffffffffL;
118 //-----------------------------------------------------------------------------
119 // CalculatePaletteCRC
120 //-----------------------------------------------------------------------------
121 unsigned int CRCCalculator::calcPaletteCRC(unsigned int crc, void *buffer, unsigned int count)
124 unsigned int orig = crc;
129 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
130 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
138 //*****************************************************************************
140 //*****************************************************************************
142 //-----------------------------------------------------------------------------
144 //! Help function when creating the CRC Table
145 //! Swaps bit 0 for bit 7
147 //! bit 2 for bit 5 ...
148 //-----------------------------------------------------------------------------
149 unsigned int CRCCalculator::_reflect(unsigned int ref, char ch)
151 unsigned int value = 0;
152 for (int i=1; i<(ch + 1); ++i)
156 value |= 1 << (ch - i);