Arachnoid GLESv1.1 plugin. Compile and run (a bit glitchy and no frameskip) on the...
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / hash / CRCCalculator2.cpp
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 "CRCCalculator2.h"
23
24 #define CRC32_POLYNOMIAL    0xedb88320    //0x04C11DB7
25 typedef unsigned char byte;
26
27 //-----------------------------------------------------------------------------
28 // Static Variabels
29 //-----------------------------------------------------------------------------
30 unsigned int CRCCalculator2::m_crcTable[256] = {0};
31
32 //-----------------------------------------------------------------------------
33 //! Constructor
34 //-----------------------------------------------------------------------------
35 CRCCalculator2::CRCCalculator2()
36 {
37     static bool hashTableInitialized = false;
38
39     //Build hash table
40     //http://www.gamedev.net/reference/articles/article1941.asp
41     if ( !hashTableInitialized )
42     {
43         unsigned int crc;
44
45         //For each value 
46         for (int i=0; i<256; i++)
47         {
48             crc = _reflect( i, 8 ) << 24;
49
50             for (int j = 0; j < 8; j++) 
51             {
52                 crc = (crc << 1) ^ (crc & (1 << 31) ? CRC32_POLYNOMIAL : 0);
53             }
54             
55             m_crcTable[i] = _reflect( crc, 32 );
56         }
57
58         hashTableInitialized = true;
59     }
60 }
61
62 //-----------------------------------------------------------------------------
63 // CalculateCRC
64 //-----------------------------------------------------------------------------
65 unsigned int CRCCalculator2::calcCRC(unsigned int crc, void *buffer, unsigned int count)
66 {
67     byte* p = (byte*) buffer; 
68     unsigned int orig = crc;
69
70     p = (byte*) buffer;
71     while (count--) 
72     {
73         crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
74     }
75
76     return crc ^ orig;
77 }
78
79 //-----------------------------------------------------------------------------
80 // CalculatePaletteCRC
81 //-----------------------------------------------------------------------------
82 unsigned int CRCCalculator2::calcPaletteCRC(unsigned int crc, void *buffer, unsigned int count)
83 {
84     byte *p;
85     unsigned int orig = crc;
86
87     p = (byte*) buffer;
88     while (count--)
89     {
90         crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
91         crc = (crc >> 8) ^ m_crcTable[(crc & 0xFF) ^ *p++];
92
93         p += 6;
94     }
95
96     return crc ^ orig;
97 }
98
99 //*****************************************************************************
100 // Private Functions
101 //*****************************************************************************
102
103 //-----------------------------------------------------------------------------
104 //* Reflect
105 //! Help function when creating the CRC Table
106 //! Swaps bit 0 for bit 7
107 //!       bit 1 for bit 6
108 //!       bit 2 for bit 5 ...
109 //-----------------------------------------------------------------------------
110 unsigned int CRCCalculator2::_reflect(unsigned int ref, char ch)
111 {
112     unsigned int value = 0;
113     for (int i=1; i<(ch + 1); ++i)
114     {
115         if(ref & 1) 
116         {
117             value |= 1 << (ch - i);
118         }
119         ref >>= 1;
120     }
121     return value;
122 }