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 "CRCCalculator2.h"
24 #define CRC32_POLYNOMIAL 0xedb88320 //0x04C11DB7
25 typedef unsigned char byte;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
30 unsigned int CRCCalculator2::m_crcTable[256] = {0};
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 CRCCalculator2::CRCCalculator2()
37 static bool hashTableInitialized = false;
40 //http://www.gamedev.net/reference/articles/article1941.asp
41 if ( !hashTableInitialized )
46 for (int i=0; i<256; i++)
48 crc = _reflect( i, 8 ) << 24;
50 for (int j = 0; j < 8; j++)
52 crc = (crc << 1) ^ (crc & (1 << 31) ? CRC32_POLYNOMIAL : 0);
55 m_crcTable[i] = _reflect( crc, 32 );
58 hashTableInitialized = true;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 unsigned int CRCCalculator2::calcCRC(unsigned int crc, void *buffer, unsigned int count)
67 byte* p = (byte*) buffer;
68 unsigned int orig = crc;
73 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
79 //-----------------------------------------------------------------------------
80 // CalculatePaletteCRC
81 //-----------------------------------------------------------------------------
82 unsigned int CRCCalculator2::calcPaletteCRC(unsigned int crc, void *buffer, unsigned int count)
85 unsigned int orig = crc;
90 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
91 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
99 //*****************************************************************************
101 //*****************************************************************************
103 //-----------------------------------------------------------------------------
105 //! Help function when creating the CRC Table
106 //! Swaps bit 0 for bit 7
108 //! bit 2 for bit 5 ...
109 //-----------------------------------------------------------------------------
110 unsigned int CRCCalculator2::_reflect(unsigned int ref, char ch)
112 unsigned int value = 0;
113 for (int i=1; i<(ch + 1); ++i)
117 value |= 1 << (ch - i);