Added SDL_crc16() to be used in joystick GUIDs after 2.24.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
diff --git a/WhatsNew.txt b/WhatsNew.txt
index 8997d0b..2b780d0 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 06f6fee..03845b6 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 c790e25..cb05dcb 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 48c599b..97daae2 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 1d2cbfe..5883ade 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 0000000..db011ba
--- /dev/null
+++ b/src/stdlib/SDL_crc16.c
@@ -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: */
diff --git a/src/stdlib/SDL_crc32.c b/src/stdlib/SDL_crc32.c
index ff80dcd..5056896 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;