2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
24 Used by the test execution component.
25 Original source code contributed by A. Schiffler for GSOC project.
29 #include "SDL_config.h"
34 int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
39 /* Sanity check context pointer */
40 if (crcContext==NULL) {
45 * Build auxiliary table for parallel byte-at-a-time CRC-32
47 #ifdef ORIGINAL_METHOD
48 for (i = 0; i < 256; ++i) {
49 for (c = i << 24, j = 8; j > 0; --j) {
50 c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
52 crcContext->crc32_table[i] = c;
55 for (i=0; i<256; i++) {
59 c = (c >> 1) ^ CRC32_POLY;
64 crcContext->crc32_table[i] = c;
71 /* Complete CRC32 calculation on a memory block */
73 int SDLTest_Crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
75 if (SDLTest_Crc32CalcStart(crcContext,crc32)) {
79 if (SDLTest_Crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) {
83 if (SDLTest_Crc32CalcEnd(crcContext, crc32)) {
90 /* Start crc calculation */
92 int SDLTest_Crc32CalcStart(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
94 /* Sanity check pointers */
95 if (crcContext==NULL) {
101 * Preload shift register, per CRC-32 spec
108 /* Finish crc calculation */
110 int SDLTest_Crc32CalcEnd(SDLTest_Crc32Context * crcContext, CrcUint32 *crc32)
112 /* Sanity check pointers */
113 if (crcContext==NULL) {
119 * Return complement, per CRC-32 spec
121 *crc32 = (~(*crc32));
126 /* Include memory block in crc */
128 int SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
131 register CrcUint32 crc;
133 if (crcContext==NULL) {
143 * Calculate CRC from data
146 for (p = inBuf; inLen > 0; ++p, --inLen) {
147 #ifdef ORIGINAL_METHOD
148 crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p];
150 crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[ (crc ^ *p) & 0xFF ];
158 int SDLTest_Crc32Done(SDLTest_Crc32Context * crcContext)
160 if (crcContext==NULL) {