Arachnoid GLESv1.1 plugin. Compile and run (a bit glitchy and no frameskip) on the...
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / hash / CRCCalculator.cpp
CommitLineData
22726e4d 1/******************************************************************************
2 * Arachnoid Graphics Plugin for Mupen64Plus
3 * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
4 *
5 * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
6 *
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.
11 *
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.
16 *
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 *****************************************************************************/
21
22#include "CRCCalculator.h"
23
24#define CRC32_POLYNOMIAL 0xedb88320L //0x04C11DB7
25typedef unsigned char byte;
26
27//-----------------------------------------------------------------------------
28// Static Variabels
29//-----------------------------------------------------------------------------
30unsigned int CRCCalculator::m_crcTable[256] = {0};
31
32static unsigned int crc_table[256];
33
34//-----------------------------------------------------------------------------
35//! Constructor
36//-----------------------------------------------------------------------------
37CRCCalculator::CRCCalculator()
38{
39 static bool hashTableInitialized = false;
40
41 //Build hash table
42 //http://www.gamedev.net/reference/articles/article1941.asp
43 if ( !hashTableInitialized )
44 {
45 // unsigned int crc;
46
47 // //For each value
48 // for (int i=0; i<256; i++)
49 //{
50 // crc = _reflect( i, 8 ) << 24;
51
52 // for (int j = 0; j < 8; j++)
53 // {
54 // crc = (crc << 1) ^ (crc & (1 << 31) ? CRC32_POLYNOMIAL : 0);
55 // }
56 //
57 // m_crcTable[i] = _reflect( crc, 32 );
58 // }
59
60 unsigned int crc;
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};
64
65 /* make exclusive-or pattern from polynomial (0xedb88320L) */
66 poly = 0L;
67 for (unsigned int n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
68 {
69 poly |= 1L << (31 - p[n]);
70 }
71
72 for (int n=0; n<256; ++n)
73 {
74 crc = (unsigned int)n;
75
76 for (int k = 0; k < 8; k++)
77 {
78 crc = (crc & 1) ? (poly ^ (crc >> 1)) : crc >> 1;
79 }
80 crc_table[n] = crc;
81 }
82
83 hashTableInitialized = true;
84 }
85}
86
87//-----------------------------------------------------------------------------
88// CalculateCRC
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++]
92unsigned int CRCCalculator::calcCRC(unsigned int crc, void *buffer, unsigned int count)
93{
94 byte* p = (byte*) buffer;
95 //unsigned int orig = crc;
96
97 // p = (byte*) buffer;
98 // while (count--)
99 // {
100 // crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
101 // }
102
103 // return crc ^ orig;
104
105 if (buffer == 0) return 0L;
106
107 crc = crc ^ 0xffffffffL;
108 while (count--)
109 {
110 crc = crc_table[((int)crc ^ (*p++)) & 0xff] ^ (crc >> 8);
111 }
112 // if (len) do {
113 // DO1(buf);
114 // } while (--len);
115 return crc ^ 0xffffffffL;
116}
117
118//-----------------------------------------------------------------------------
119// CalculatePaletteCRC
120//-----------------------------------------------------------------------------
121unsigned int CRCCalculator::calcPaletteCRC(unsigned int crc, void *buffer, unsigned int count)
122{
123 byte *p;
124 unsigned int orig = crc;
125
126 p = (byte*) buffer;
127 while (count--)
128 {
129 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
130 crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
131
132 p += 6;
133 }
134
135 return crc ^ orig;
136}
137
138//*****************************************************************************
139// Private Functions
140//*****************************************************************************
141
142//-----------------------------------------------------------------------------
143//* Reflect
144//! Help function when creating the CRC Table
145//! Swaps bit 0 for bit 7
146//! bit 1 for bit 6
147//! bit 2 for bit 5 ...
148//-----------------------------------------------------------------------------
149unsigned int CRCCalculator::_reflect(unsigned int ref, char ch)
150{
151 unsigned int value = 0;
152 for (int i=1; i<(ch + 1); ++i)
153 {
154 if(ref & 1)
155 {
156 value |= 1 << (ch - i);
157 }
158 ref >>= 1;
159 }
160 return value;
161}