forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSWFL_CRC32.Cpp
237 lines (187 loc) · 7.6 KB
/
NSWFL_CRC32.Cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_CRC32_CPP_
#define _NSWFL_CRC32_CPP_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
#ifdef _USE_GLOBAL_MEMPOOL
extern NSWFL::Memory::MemoryPool *pMem; //pMem must be defined and initalized elsewhere.
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace Hashing {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
This function initializes "CRC Lookup Table". You only need to call it once to
initalize the table before using any of the other CRC32 calculation functions.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CRC32::CRC32(void)
{
this->Initialize();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CRC32::~CRC32(void)
{
//No destructor code.
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
This function initializes "CRC Lookup Table". You only need to call it once to
initalize the table before using any of the other CRC32 calculation functions.
*/
void CRC32::Initialize(void)
{
//0x04C11DB7 is the official polynomial used by PKZip, WinZip and Ethernet.
unsigned int iPolynomial = 0x04C11DB7;
memset(&this->iTable, 0, sizeof(this->iTable));
// 256 values representing ASCII character codes.
for (int iCodes = 0; iCodes <= 0xFF; iCodes++)
{
this->iTable[iCodes] = this->Reflect(iCodes, 8) << 24;
for (int iPos = 0; iPos < 8; iPos++)
{
this->iTable[iCodes] = (this->iTable[iCodes] << 1)
^ ((this->iTable[iCodes] & (1 << 31)) ? iPolynomial : 0);
}
this->iTable[iCodes] = this->Reflect(this->iTable[iCodes], 32);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Reflection is a requirement for the official CRC-32 standard.
You can create CRCs without it, but they won't conform to the standard.
*/
unsigned int CRC32::Reflect(unsigned int iReflect, const char cChar)
{
unsigned int iValue = 0;
// Swap bit 0 for bit 7, bit 1 For bit 6, etc....
for (int iPos = 1; iPos < (cChar + 1); iPos++)
{
if (iReflect & 1)
{
iValue |= (1 << (cChar - iPos));
}
iReflect >>= 1;
}
return iValue;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Calculates the CRC32 by looping through each of the bytes in sData.
Note: For Example usage example, see FileCRC().
*/
void CRC32::PartialCRC(unsigned int *iCRC, const unsigned char *sData, size_t iDataLength)
{
while (iDataLength--)
{
//If your compiler complains about the following line, try changing
// each occurrence of *iCRC with ((unsigned int)*iCRC).
*iCRC = (*iCRC >> 8) ^ this->iTable[(*iCRC & 0xFF) ^ *sData++];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Returns the calculated CRC32 (through iOutCRC) for the given string.
*/
void CRC32::FullCRC(const unsigned char *sData, size_t iDataLength, unsigned int *iOutCRC)
{
((unsigned int)*iOutCRC) = 0xffffffff; //Initilaize the CRC.
this->PartialCRC(iOutCRC, sData, iDataLength);
((unsigned int)*iOutCRC) ^= 0xffffffff; //Finalize the CRC.
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Returns the calculated CRC23 for the given string.
*/
unsigned int CRC32::FullCRC(const unsigned char *sData, size_t iDataLength)
{
unsigned int iCRC = 0xffffffff; //Initilaize the CRC.
this->PartialCRC(&iCRC, sData, iDataLength);
return(iCRC ^ 0xffffffff); //Finalize the CRC and return.
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Calculates the CRC32 of a file using the a user defined buffer.
Note: The buffer size DOES NOT affect the resulting CRC,
it has been provided for performance purposes only.
*/
bool CRC32::FileCRC(const char *sFileName, unsigned int *iOutCRC, size_t iBufferSize)
{
((unsigned int)*iOutCRC) = 0xffffffff; //Initilaize the CRC.
FILE *fSource = NULL;
unsigned char *sBuf = NULL;
size_t iBytesRead = 0;
if ((fopen_s(&fSource, sFileName, "rb")) != 0)
{
return false; //Failed to open file for read access.
}
#ifdef _USE_GLOBAL_MEMPOOL
if (!(sBuf = (unsigned char *)pMem->Allocate(iBufferSize, 1))) //Allocate memory for file buffering.
#else
if (!(sBuf = (unsigned char *)malloc(iBufferSize))) //Allocate memory for file buffering.
#endif
{
fclose(fSource);
return false; //Out of memory.
}
while ((iBytesRead = fread(sBuf, sizeof(char), iBufferSize, fSource)))
{
this->PartialCRC(iOutCRC, sBuf, iBytesRead);
}
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sBuf);
#else
free(sBuf);
#endif
fclose(fSource);
((unsigned int)*iOutCRC) ^= 0xffffffff; //Finalize the CRC.
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Calculates the CRC32 of a file using the a default buffer size of 1MB.
*/
unsigned int CRC32::FileCRC(const char *sFileName)
{
unsigned int iCRC;
if (this->FileCRC(sFileName, &iCRC, 1048576))
{
return iCRC;
}
else return 0xffffffff; //While we return this as an error code, it is infact a valid CRC!
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Calculates the CRC32 of a file using the a default buffer size of 1MB.
Note: The buffer size DOES NOT affect the resulting CRC,
it has been provided for performance purposes only.
*/
unsigned int CRC32::FileCRC(const char *sFileName, size_t iBufferSize)
{
unsigned int iCRC;
if (this->FileCRC(sFileName, &iCRC, iBufferSize))
{
return iCRC;
}
else return 0xffffffff; //While we return this as an error code, it is infact a valid CRC!
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Calculates the CRC32 of a file using the a default buffer size of 1MB.
*/
bool CRC32::FileCRC(const char *sFileName, unsigned int *iOutCRC)
{
return this->FileCRC(sFileName, iOutCRC, 1048576);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace::Hashing
} //namespace::NSWFL
#endif