diff --git a/WhatsNew.txt b/WhatsNew.txt index 8997d0bf8..2b780d09c 100644 --- a/WhatsNew.txt +++ b/WhatsNew.txt @@ -18,7 +18,7 @@ General: * The patchlevel indicates successive prereleases, for example 2.23.1 and 2.23.2 would be prereleases during development of 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_ResetHint() to reset a hint to the default value * The hint SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS now defaults on diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index 06f6fee29..03845b678 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -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_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 void *SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len); diff --git a/src/dynapi/SDL2.exports b/src/dynapi/SDL2.exports index c790e2582..cb05dcb45 100644 --- a/src/dynapi/SDL2.exports +++ b/src/dynapi/SDL2.exports @@ -857,3 +857,4 @@ ++'_SDL_GetPointDisplayIndex'.'SDL2.dll'.'SDL_GetPointDisplayIndex' ++'_SDL_GetRectDisplayIndex'.'SDL2.dll'.'SDL_GetRectDisplayIndex' ++'_SDL_ResetHint'.'SDL2.dll'.'SDL_ResetHint' +++'_SDL_crc16'.'SDL2.dll'.'SDL_crc16' diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 48c599bbb..97daae25d 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -883,3 +883,4 @@ #define SDL_GetPointDisplayIndex SDL_GetPointDisplayIndex_REAL #define SDL_GetRectDisplayIndex SDL_GetRectDisplayIndex_REAL #define SDL_ResetHint SDL_ResetHint_REAL +#define SDL_crc16 SDL_crc16_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 1d2cbfe23..5883ade16 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -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_GetRectDisplayIndex,(const SDL_Rect *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) diff --git a/src/stdlib/SDL_crc16.c b/src/stdlib/SDL_crc16.c new file mode 100644 index 000000000..db011ba10 --- /dev/null +++ b/src/stdlib/SDL_crc16.c @@ -0,0 +1,54 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2022 Sam Lantinga + + 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: */ diff --git a/src/stdlib/SDL_crc32.c b/src/stdlib/SDL_crc32.c index ff80dcdba..505689661 100644 --- a/src/stdlib/SDL_crc32.c +++ b/src/stdlib/SDL_crc32.c @@ -33,7 +33,7 @@ static Uint32 crc32_for_byte(Uint32 r) { int i; - for(i = 0; i < 8; ++i) { + for (i = 0; i < 8; ++i) { r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1; } return r ^ (Uint32)0xFF000000L;