Added SDL_crc32()

This commit is contained in:
Sam Lantinga 2020-11-16 15:00:15 -08:00
parent 3d48fc05c3
commit 71e32f5e1b
11 changed files with 83 additions and 50 deletions

View File

@ -500,6 +500,7 @@
<ClCompile Include="..\..\src\sensor\dummy\SDL_dummysensor.c" />
<ClCompile Include="..\..\src\sensor\SDL_sensor.c" />
<ClCompile Include="..\..\src\sensor\windows\SDL_windowssensor.c" />
<ClCompile Include="..\..\src\stdlib\SDL_crc32.c" />
<ClCompile Include="..\..\src\stdlib\SDL_getenv.c" />
<ClCompile Include="..\..\src\stdlib\SDL_iconv.c" />
<ClCompile Include="..\..\src\stdlib\SDL_malloc.c" />

View File

@ -3977,6 +3977,15 @@
F3950CD8212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
F3950CD9212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
F3950CDA212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
F395BF6525633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6625633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6725633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6825633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6925633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6A25633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6B25633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6C25633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F395BF6D25633B2400942BFF /* SDL_crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = F395BF6425633B2400942BFF /* SDL_crc32.c */; };
F3A4909E2554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
F3A4909F2554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
F3A490A02554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */ = {isa = PBXBuildFile; fileRef = F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */; };
@ -4532,6 +4541,7 @@
F37DC5F225350EBC0002E6F7 /* CoreHaptics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreHaptics.framework; path = System/Library/Frameworks/CoreHaptics.framework; sourceTree = SDKROOT; };
F37DC5F425350ECC0002E6F7 /* CoreHaptics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreHaptics.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS14.0.sdk/System/Library/Frameworks/CoreHaptics.framework; sourceTree = DEVELOPER_DIR; };
F3950CD7212BC88D00F51292 /* SDL_sensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor.h; sourceTree = "<group>"; };
F395BF6425633B2400942BFF /* SDL_crc32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_crc32.c; sourceTree = "<group>"; };
F3A4909D2554D38500E92A8B /* SDL_hidapi_ps5.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_ps5.c; sourceTree = "<group>"; };
F59C710300D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = "<group>"; };
F59C710600D5CB5801000001 /* SDL.info */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = SDL.info; sourceTree = "<group>"; };
@ -5630,6 +5640,7 @@
A7D8A8D223E2514000DCD162 /* stdlib */ = {
isa = PBXGroup;
children = (
F395BF6425633B2400942BFF /* SDL_crc32.c */,
A7D8A8D423E2514000DCD162 /* SDL_getenv.c */,
A7D8A8D323E2514000DCD162 /* SDL_iconv.c */,
A7D8A8D923E2514000DCD162 /* SDL_malloc.c */,
@ -8469,6 +8480,7 @@
A75FCDE923E25AB700529352 /* SDL_drawline.c in Sources */,
A75FCDEA23E25AB700529352 /* SDL_yuv.c in Sources */,
A75FCDEB23E25AB700529352 /* SDL_sysfilesystem.m in Sources */,
F395BF6C25633B2400942BFF /* SDL_crc32.c in Sources */,
F3A490A52554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A75FCDEC23E25AB700529352 /* e_pow.c in Sources */,
A75FCDED23E25AB700529352 /* SDL_systls.c in Sources */,
@ -8669,6 +8681,7 @@
A75FCFA223E25AC700529352 /* SDL_drawline.c in Sources */,
A75FCFA323E25AC700529352 /* SDL_yuv.c in Sources */,
A75FCFA423E25AC700529352 /* SDL_sysfilesystem.m in Sources */,
F395BF6D25633B2400942BFF /* SDL_crc32.c in Sources */,
F3A490A62554D38600E92A8B /* SDL_hidapi_ps5.c in Sources */,
A75FCFA523E25AC700529352 /* e_pow.c in Sources */,
A75FCFA623E25AC700529352 /* SDL_systls.c in Sources */,
@ -9032,6 +9045,7 @@
A769B1F923E259AE00872273 /* SDL_joystick.c in Sources */,
A769B1FA23E259AE00872273 /* SDL_render_gles2.c in Sources */,
A769B1FB23E259AE00872273 /* SDL_surface.c in Sources */,
F395BF6A25633B2400942BFF /* SDL_crc32.c in Sources */,
A769B1FC23E259AE00872273 /* SDL_hidapi_xboxone.c in Sources */,
A769B1FD23E259AE00872273 /* SDL_blit_auto.c in Sources */,
A769B1FE23E259AE00872273 /* SDL_x11keyboard.c in Sources */,
@ -9094,6 +9108,7 @@
A7D8B9E423E2514400DCD162 /* SDL_drawline.c in Sources */,
A7D8AE7D23E2514100DCD162 /* SDL_yuv.c in Sources */,
A7D8B63023E2514300DCD162 /* SDL_sysfilesystem.m in Sources */,
F395BF6625633B2400942BFF /* SDL_crc32.c in Sources */,
A7D8BAC823E2514500DCD162 /* e_pow.c in Sources */,
A7D8B41D23E2514300DCD162 /* SDL_systls.c in Sources */,
A7D8AD2A23E2514100DCD162 /* SDL_vulkan_utils.c in Sources */,
@ -9294,6 +9309,7 @@
A7D8B9E523E2514400DCD162 /* SDL_drawline.c in Sources */,
A7D8AE7E23E2514100DCD162 /* SDL_yuv.c in Sources */,
A7D8B63123E2514300DCD162 /* SDL_sysfilesystem.m in Sources */,
F395BF6725633B2400942BFF /* SDL_crc32.c in Sources */,
A7D8BAC923E2514500DCD162 /* e_pow.c in Sources */,
A7D8B41E23E2514300DCD162 /* SDL_systls.c in Sources */,
A7D8AD2B23E2514100DCD162 /* SDL_vulkan_utils.c in Sources */,
@ -9633,6 +9649,7 @@
A7D8B4E023E2514300DCD162 /* SDL_joystick.c in Sources */,
A7D8BA4D23E2514400DCD162 /* SDL_render_gles2.c in Sources */,
A7D8AC3123E2514100DCD162 /* SDL_surface.c in Sources */,
F395BF6925633B2400942BFF /* SDL_crc32.c in Sources */,
A7D8B54F23E2514300DCD162 /* SDL_hidapi_xboxone.c in Sources */,
A7D8AD2723E2514100DCD162 /* SDL_blit_auto.c in Sources */,
A7D8B1AA23E2514200DCD162 /* SDL_x11keyboard.c in Sources */,
@ -9764,6 +9781,7 @@
A7D8BACD23E2514500DCD162 /* e_atan2.c in Sources */,
A7D8BA8B23E2514400DCD162 /* s_sin.c in Sources */,
A7D8BBEB23E2574800DCD162 /* SDL_uikitwindow.m in Sources */,
F395BF6525633B2400942BFF /* SDL_crc32.c in Sources */,
A7D8B5E723E2514300DCD162 /* SDL_power.c in Sources */,
A7D8AED623E2514100DCD162 /* SDL_cocoakeyboard.m in Sources */,
A7D8AB1623E2514100DCD162 /* SDL_dynapi.c in Sources */,
@ -9963,6 +9981,7 @@
A7D8B5EA23E2514300DCD162 /* SDL_power.c in Sources */,
A7D8AED923E2514100DCD162 /* SDL_cocoakeyboard.m in Sources */,
A7D8AB1923E2514100DCD162 /* SDL_dynapi.c in Sources */,
F395BF6825633B2400942BFF /* SDL_crc32.c in Sources */,
A7D8BA8823E2514400DCD162 /* SDL_shaders_gl.c in Sources */,
A7D8BAF423E2514500DCD162 /* e_log.c in Sources */,
A7D8AED323E2514100DCD162 /* SDL_cocoamessagebox.m in Sources */,
@ -10162,6 +10181,7 @@
A7D8BA9023E2514400DCD162 /* s_sin.c in Sources */,
A7D8B5EC23E2514300DCD162 /* SDL_power.c in Sources */,
A7D8AEDB23E2514100DCD162 /* SDL_cocoakeyboard.m in Sources */,
F395BF6B25633B2400942BFF /* SDL_crc32.c in Sources */,
A7D8AB1B23E2514100DCD162 /* SDL_dynapi.c in Sources */,
A7D8BA8A23E2514400DCD162 /* SDL_shaders_gl.c in Sources */,
A7D8BAF623E2514500DCD162 /* e_log.c in Sources */,

View File

@ -417,6 +417,8 @@ extern DECLSPEC int SDLCALL SDL_islower(int x);
extern DECLSPEC int SDLCALL SDL_toupper(int x);
extern DECLSPEC int SDLCALL SDL_tolower(int x);
extern DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
extern DECLSPEC void *SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
#define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))

View File

@ -779,3 +779,4 @@
#define SDL_GameControllerGetNumTouchpads SDL_GameControllerGetNumTouchpads_REAL
#define SDL_GameControllerGetNumTouchpadFingers SDL_GameControllerGetNumTouchpadFingers_REAL
#define SDL_GameControllerGetTouchpadFinger SDL_GameControllerGetTouchpadFinger_REAL
#define SDL_crc32 SDL_crc32_REAL

View File

@ -840,3 +840,4 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GameControllerHasButton,(SDL_GameController *a, SDL
SDL_DYNAPI_PROC(int,SDL_GameControllerGetNumTouchpads,(SDL_GameController *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GameControllerGetNumTouchpadFingers,(SDL_GameController *a, int b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_GameControllerGetTouchpadFinger,(SDL_GameController *a, int b, int c, Uint8 *d, float *e, float *f, float *g),(a,b,c,d,e,f,g),return)
SDL_DYNAPI_PROC(Uint32,SDL_crc32,(Uint32 a, const void *b, size_t c),(a,b,c),return)

View File

@ -70,28 +70,6 @@ static SDL_joylist_item *SDL_joylist_tail = NULL;
static int numjoysticks = 0;
/* Public domain CRC implementation adapted from:
http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
*/
static Uint32 crc32_for_byte(Uint32 r)
{
int i;
for(i = 0; i < 8; ++i) {
r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
}
return r ^ (Uint32)0xFF000000L;
}
static Uint32 crc32(const void *data, size_t count)
{
Uint32 crc = 0;
int i;
for(i = 0; i < count; ++i) {
crc = crc32_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
}
return crc;
}
/* Function to convert Android keyCodes into SDL ones.
* This code manipulation is done to get a sequential list of codes.
* FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
@ -391,7 +369,8 @@ Android_AddJoystick(int device_id, const char *name, const char *desc, int vendo
*guid16++ = SDL_SwapLE16(product_id);
*guid16++ = 0;
} else {
Uint32 crc = crc32(desc, SDL_strlen(desc));
Uint32 crc = 0;
SDL_crc32(crc, desc, SDL_strlen(desc));
SDL_memcpy(guid16, desc, SDL_min(2*sizeof(*guid16), SDL_strlen(desc)));
guid16 += 2;
*(Uint32 *)guid16 = SDL_SwapLE32(crc);

View File

@ -260,8 +260,8 @@ HIDAPI_DriverPS4_UpdateEffects(SDL_HIDAPI_Device *device)
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
Uint32 unCRC;
unCRC = crc32(0, &ubHdr, 1);
unCRC = crc32(unCRC, data, (Uint32)(report_size - sizeof(unCRC)));
unCRC = SDL_crc32(0, &ubHdr, 1);
unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}

View File

@ -236,8 +236,8 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device)
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
Uint32 unCRC;
unCRC = crc32(0, &ubHdr, 1);
unCRC = crc32(unCRC, data, (Uint32)(report_size - sizeof(unCRC)));
unCRC = SDL_crc32(0, &ubHdr, 1);
unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}

View File

@ -390,27 +390,6 @@ HIDAPI_ShutdownDiscovery()
#endif
}
/* Public domain CRC implementation adapted from:
http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
*/
static Uint32 crc32_for_byte(Uint32 r)
{
int i;
for(i = 0; i < 8; ++i) {
r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
}
return r ^ (Uint32)0xFF000000L;
}
Uint32 crc32(Uint32 crc, const void *data, int count)
{
int i;
for(i = 0; i < count; ++i) {
crc = crc32_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
}
return crc;
}
void
HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size)
{

View File

@ -127,8 +127,6 @@ extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickI
extern void HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size);
Uint32 crc32(Uint32 crc, const void *data, int count);
#endif /* SDL_JOYSTICK_HIDAPI_H */
/* vi: set ts=4 sw=4 expandtab: */

52
src/stdlib/SDL_crc32.c Normal file
View File

@ -0,0 +1,52 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "SDL_stdinc.h"
/* Public domain CRC implementation adapted from:
http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
*/
/* NOTE: DO NOT CHANGE THIS ALGORITHM
There is code that relies on this in the joystick code
*/
static Uint32 crc32_for_byte(Uint32 r)
{
int i;
for(i = 0; i < 8; ++i) {
r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
}
return r ^ (Uint32)0xFF000000L;
}
Uint32 SDL_crc32(Uint32 crc, const void *data, size_t len)
{
/* As an optimization we can precalculate a 256 entry table for each byte */
size_t i;
for(i = 0; i < len; ++i) {
crc = crc32_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
}
return crc;
}
/* vi: set ts=4 sw=4 expandtab: */