1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
4 | |
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. |
8 | |
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: |
12 | |
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. |
20 | */ |
21 | |
22 | /* |
23 | |
24 | Used by the test execution component. |
25 | Original source code contributed by A. Schiffler for GSOC project. |
26 | |
27 | */ |
28 | #include <SDL3/SDL_test.h> |
29 | |
30 | bool SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext) |
31 | { |
32 | int i, j; |
33 | CrcUint32 c; |
34 | |
35 | /* Sanity check context pointer */ |
36 | if (!crcContext) { |
37 | return SDL_InvalidParamError("crcContext" ); |
38 | } |
39 | |
40 | /* |
41 | * Build auxiliary table for parallel byte-at-a-time CRC-32 |
42 | */ |
43 | #ifdef ORIGINAL_METHOD |
44 | for (i = 0; i < 256; ++i) { |
45 | for (c = i << 24, j = 8; j > 0; --j) { |
46 | c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1); |
47 | } |
48 | crcContext->crc32_table[i] = c; |
49 | } |
50 | #else |
51 | for (i = 0; i < 256; i++) { |
52 | c = i; |
53 | for (j = 8; j > 0; j--) { |
54 | if (c & 1) { |
55 | c = (c >> 1) ^ CRC32_POLY; |
56 | } else { |
57 | c >>= 1; |
58 | } |
59 | } |
60 | crcContext->crc32_table[i] = c; |
61 | } |
62 | #endif |
63 | |
64 | return true; |
65 | } |
66 | |
67 | /* Complete CRC32 calculation on a memory block */ |
68 | bool SDLTest_Crc32Calc(SDLTest_Crc32Context *crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32) |
69 | { |
70 | if (!SDLTest_Crc32CalcStart(crcContext, crc32)) { |
71 | return false; |
72 | } |
73 | |
74 | if (!SDLTest_Crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) { |
75 | return false; |
76 | } |
77 | |
78 | if (!SDLTest_Crc32CalcEnd(crcContext, crc32)) { |
79 | return false; |
80 | } |
81 | |
82 | return true; |
83 | } |
84 | |
85 | /* Start crc calculation */ |
86 | |
87 | bool SDLTest_Crc32CalcStart(SDLTest_Crc32Context *crcContext, CrcUint32 *crc32) |
88 | { |
89 | /* Sanity check pointers */ |
90 | if (!crcContext) { |
91 | *crc32 = 0; |
92 | return SDL_InvalidParamError("crcContext" ); |
93 | } |
94 | |
95 | /* |
96 | * Preload shift register, per CRC-32 spec |
97 | */ |
98 | *crc32 = 0xffffffff; |
99 | |
100 | return true; |
101 | } |
102 | |
103 | /* Finish crc calculation */ |
104 | |
105 | bool SDLTest_Crc32CalcEnd(SDLTest_Crc32Context *crcContext, CrcUint32 *crc32) |
106 | { |
107 | /* Sanity check pointers */ |
108 | if (!crcContext) { |
109 | *crc32 = 0; |
110 | return SDL_InvalidParamError("crcContext" ); |
111 | } |
112 | |
113 | /* |
114 | * Return complement, per CRC-32 spec |
115 | */ |
116 | *crc32 = (~(*crc32)); |
117 | |
118 | return true; |
119 | } |
120 | |
121 | /* Include memory block in crc */ |
122 | |
123 | bool SDLTest_Crc32CalcBuffer(SDLTest_Crc32Context *crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32) |
124 | { |
125 | CrcUint8 *p; |
126 | register CrcUint32 crc; |
127 | |
128 | if (!crcContext) { |
129 | *crc32 = 0; |
130 | return SDL_InvalidParamError("crcContext" ); |
131 | } |
132 | |
133 | if (!inBuf) { |
134 | return SDL_InvalidParamError("inBuf" ); |
135 | } |
136 | |
137 | /* |
138 | * Calculate CRC from data |
139 | */ |
140 | crc = *crc32; |
141 | for (p = inBuf; inLen > 0; ++p, --inLen) { |
142 | #ifdef ORIGINAL_METHOD |
143 | crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p]; |
144 | #else |
145 | crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[(crc ^ *p) & 0xFF]; |
146 | #endif |
147 | } |
148 | *crc32 = crc; |
149 | |
150 | return true; |
151 | } |
152 | |
153 | bool SDLTest_Crc32Done(SDLTest_Crc32Context *crcContext) |
154 | { |
155 | if (!crcContext) { |
156 | return SDL_InvalidParamError("crcContext" ); |
157 | } |
158 | |
159 | return true; |
160 | } |
161 | |