SDL: Add SDL_ReadS8 and SDL_WriteS8

From ef6123886e59a9f3960e9aa889e70756e57d96de Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Wed, 12 Jun 2024 00:39:36 +0200
Subject: [PATCH] Add SDL_ReadS8 and SDL_WriteS8

---
 include/SDL3/SDL_iostream.h       | 24 ++++++++++++++++++++++++
 src/dynapi/SDL_dynapi.sym         |  2 ++
 src/dynapi/SDL_dynapi_overrides.h |  2 ++
 src/dynapi/SDL_dynapi_procs.h     |  2 ++
 src/file/SDL_iostream.c           | 19 +++++++++++++++++++
 5 files changed, 49 insertions(+)

diff --git a/include/SDL3/SDL_iostream.h b/include/SDL3/SDL_iostream.h
index c10572597fbfb..8f2bff8dee14a 100644
--- a/include/SDL3/SDL_iostream.h
+++ b/include/SDL3/SDL_iostream.h
@@ -625,6 +625,18 @@ extern SDL_DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasiz
  */
 extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU8(SDL_IOStream *src, Uint8 *value);
 
+/**
+ * Use this function to read a signed byte from an SDL_IOStream.
+ *
+ * \param src the SDL_IOStream to read from
+ * \param value a pointer filled in with the data read
+ * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
+ *          for more information.
+ *
+ * \since This function is available since SDL 3.0.0.
+ */
+extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS8(SDL_IOStream *src, Sint8 *value);
+
 /**
  * Use this function to read 16 bits of little-endian data from an
  * SDL_IOStream and return in native format.
@@ -837,6 +849,18 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS64BE(SDL_IOStream *src, Sint64 *va
  */
 extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU8(SDL_IOStream *dst, Uint8 value);
 
+/**
+ * Use this function to write a signed byte to an SDL_IOStream.
+ *
+ * \param dst the SDL_IOStream to write to
+ * \param value the byte value to write
+ * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
+ *          SDL_GetError() for more information.
+ *
+ * \since This function is available since SDL 3.0.0.
+ */
+extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS8(SDL_IOStream *dst, Sint8 value);
+
 /**
  * Use this function to write 16 bits in native format to an SDL_IOStream as
  * little-endian data.
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index 7d962fc97c1d2..ccdb5c9a1a5c8 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -615,6 +615,7 @@ SDL3_0.0.0 {
     SDL_ReadS32LE;
     SDL_ReadS64BE;
     SDL_ReadS64LE;
+    SDL_ReadS8;
     SDL_ReadStorageFile;
     SDL_ReadSurfacePixel;
     SDL_ReadU16BE;
@@ -849,6 +850,7 @@ SDL3_0.0.0 {
     SDL_WriteS32LE;
     SDL_WriteS64BE;
     SDL_WriteS64LE;
+    SDL_WriteS8;
     SDL_WriteStorageFile;
     SDL_WriteU16BE;
     SDL_WriteU16LE;
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index b7710bb563096..c2c51c83f87df 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -640,6 +640,7 @@
 #define SDL_ReadS32LE SDL_ReadS32LE_REAL
 #define SDL_ReadS64BE SDL_ReadS64BE_REAL
 #define SDL_ReadS64LE SDL_ReadS64LE_REAL
+#define SDL_ReadS8 SDL_ReadS8_REAL
 #define SDL_ReadStorageFile SDL_ReadStorageFile_REAL
 #define SDL_ReadSurfacePixel SDL_ReadSurfacePixel_REAL
 #define SDL_ReadU16BE SDL_ReadU16BE_REAL
@@ -874,6 +875,7 @@
 #define SDL_WriteS32LE SDL_WriteS32LE_REAL
 #define SDL_WriteS64BE SDL_WriteS64BE_REAL
 #define SDL_WriteS64LE SDL_WriteS64LE_REAL
+#define SDL_WriteS8 SDL_WriteS8_REAL
 #define SDL_WriteStorageFile SDL_WriteStorageFile_REAL
 #define SDL_WriteU16BE SDL_WriteU16BE_REAL
 #define SDL_WriteU16LE SDL_WriteU16LE_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 5c32967050772..21ee3411016a9 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -651,6 +651,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_ReadS32BE,(SDL_IOStream *a, Sint32 *b),(a,b),return
 SDL_DYNAPI_PROC(SDL_bool,SDL_ReadS32LE,(SDL_IOStream *a, Sint32 *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_ReadS64BE,(SDL_IOStream *a, Sint64 *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_ReadS64LE,(SDL_IOStream *a, Sint64 *b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_bool,SDL_ReadS8,(SDL_IOStream *a, Sint8 *b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_ReadStorageFile,(SDL_Storage *a, const char *b, void *c, Uint64 d),(a,b,c,d),return)
 SDL_DYNAPI_PROC(int,SDL_ReadSurfacePixel,(SDL_Surface *a, int b, int c, Uint8 *d, Uint8 *e, Uint8 *f, Uint8 *g),(a,b,c,d,e,f,g),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_ReadU16BE,(SDL_IOStream *a, Uint16 *b),(a,b),return)
@@ -884,6 +885,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS32BE,(SDL_IOStream *a, Sint32 b),(a,b),return
 SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS32LE,(SDL_IOStream *a, Sint32 b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS64BE,(SDL_IOStream *a, Sint64 b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS64LE,(SDL_IOStream *a, Sint64 b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS8,(SDL_IOStream *a, Sint8 b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_WriteStorageFile,(SDL_Storage *a, const char *b, const void *c, Uint64 d),(a,b,c,d),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_WriteU16BE,(SDL_IOStream *a, Uint16 b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_WriteU16LE,(SDL_IOStream *a, Uint16 b),(a,b),return)
diff --git a/src/file/SDL_iostream.c b/src/file/SDL_iostream.c
index 37c60e3b6c0c6..4792b5b00fa08 100644
--- a/src/file/SDL_iostream.c
+++ b/src/file/SDL_iostream.c
@@ -1117,6 +1117,20 @@ SDL_bool SDL_ReadU8(SDL_IOStream *src, Uint8 *value)
     return result;
 }
 
+SDL_bool SDL_ReadS8(SDL_IOStream *src, Sint8 *value)
+{
+    Sint8 data = 0;
+    SDL_bool result = SDL_FALSE;
+
+    if (SDL_ReadIO(src, &data, sizeof(data)) == sizeof(data)) {
+        result = SDL_TRUE;
+    }
+    if (value) {
+        *value = data;
+    }
+    return result;
+}
+
 SDL_bool SDL_ReadU16LE(SDL_IOStream *src, Uint16 *value)
 {
     Uint16 data = 0;
@@ -1236,6 +1250,11 @@ SDL_bool SDL_WriteU8(SDL_IOStream *dst, Uint8 value)
     return (SDL_WriteIO(dst, &value, sizeof(value)) == sizeof(value));
 }
 
+SDL_bool SDL_WriteS8(SDL_IOStream *dst, Sint8 value)
+{
+    return (SDL_WriteIO(dst, &value, sizeof(value)) == sizeof(value));
+}
+
 SDL_bool SDL_WriteU16LE(SDL_IOStream *dst, Uint16 value)
 {
     const Uint16 swapped = SDL_SwapLE16(value);