Added SDL_crc16() to be used in joystick GUIDs after 2.24.0

This commit is contained in:
Sam Lantinga 2022-08-11 09:52:58 -07:00
parent a1e34b5e35
commit b4c4dd84c2
7 changed files with 60 additions and 2 deletions

View File

@ -18,7 +18,7 @@ General:
* The patchlevel indicates successive prereleases, for example * The patchlevel indicates successive prereleases, for example
2.23.1 and 2.23.2 would be prereleases during development of 2.23.1 and 2.23.2 would be prereleases during development of
the SDL 2.24.0 stable release. the SDL 2.24.0 stable release.
* Added SDL_bsearch() and SDL_utf8strnlen() to the stdlib routines * Added SDL_bsearch(), SDL_crc16(), and SDL_utf8strnlen() to the stdlib routines
* Added SDL_size_mul_overflow() and SDL_size_add_overflow() for better size overflow protection * Added SDL_size_mul_overflow() and SDL_size_add_overflow() for better size overflow protection
* Added SDL_ResetHint() to reset a hint to the default value * Added SDL_ResetHint() to reset a hint to the default value
* The hint SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS now defaults on * The hint SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS now defaults on

View File

@ -503,6 +503,7 @@ extern DECLSPEC int SDLCALL SDL_isgraph(int x);
extern DECLSPEC int SDLCALL SDL_toupper(int x); extern DECLSPEC int SDLCALL SDL_toupper(int x);
extern DECLSPEC int SDLCALL SDL_tolower(int x); extern DECLSPEC int SDLCALL SDL_tolower(int x);
extern DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
extern DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len); 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); extern DECLSPEC void *SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);

View File

@ -857,3 +857,4 @@
++'_SDL_GetPointDisplayIndex'.'SDL2.dll'.'SDL_GetPointDisplayIndex' ++'_SDL_GetPointDisplayIndex'.'SDL2.dll'.'SDL_GetPointDisplayIndex'
++'_SDL_GetRectDisplayIndex'.'SDL2.dll'.'SDL_GetRectDisplayIndex' ++'_SDL_GetRectDisplayIndex'.'SDL2.dll'.'SDL_GetRectDisplayIndex'
++'_SDL_ResetHint'.'SDL2.dll'.'SDL_ResetHint' ++'_SDL_ResetHint'.'SDL2.dll'.'SDL_ResetHint'
++'_SDL_crc16'.'SDL2.dll'.'SDL_crc16'

View File

@ -883,3 +883,4 @@
#define SDL_GetPointDisplayIndex SDL_GetPointDisplayIndex_REAL #define SDL_GetPointDisplayIndex SDL_GetPointDisplayIndex_REAL
#define SDL_GetRectDisplayIndex SDL_GetRectDisplayIndex_REAL #define SDL_GetRectDisplayIndex SDL_GetRectDisplayIndex_REAL
#define SDL_ResetHint SDL_ResetHint_REAL #define SDL_ResetHint SDL_ResetHint_REAL
#define SDL_crc16 SDL_crc16_REAL

View File

@ -966,3 +966,4 @@ SDL_DYNAPI_PROC(int,SDL_GetDefaultAudioInfo,(char **a, SDL_AudioSpec *b, int c),
SDL_DYNAPI_PROC(int,SDL_GetPointDisplayIndex,(const SDL_Point *a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetPointDisplayIndex,(const SDL_Point *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetRectDisplayIndex,(const SDL_Rect *a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetRectDisplayIndex,(const SDL_Rect *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_ResetHint,(const char *a),(a),return) SDL_DYNAPI_PROC(SDL_bool,SDL_ResetHint,(const char *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_crc16,(Uint16 a, const void *b, size_t c),(a,b,c),return)

54
src/stdlib/SDL_crc16.c Normal file
View File

@ -0,0 +1,54 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 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 Uint16 crc16_for_byte(Uint8 r)
{
Uint16 crc = 0;
int i;
for (i = 0; i < 8; ++i) {
crc = ((crc ^ r) & 1? 0xA001 : 0) ^ crc >> 1;
r >>= 1;
}
return crc;
}
Uint16 SDL_crc16(Uint16 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 = crc16_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
}
return crc;
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@ -33,7 +33,7 @@
static Uint32 crc32_for_byte(Uint32 r) static Uint32 crc32_for_byte(Uint32 r)
{ {
int i; int i;
for(i = 0; i < 8; ++i) { for (i = 0; i < 8; ++i) {
r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1; r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
} }
return r ^ (Uint32)0xFF000000L; return r ^ (Uint32)0xFF000000L;