From f27c8c0c1d3c47fb6fcc162e9e40a6cc435440a0 Mon Sep 17 00:00:00 2001 From: Cthulhu-throwaway <96153783+Cthulhu-throwaway@users.noreply.github.com> Date: Sat, 20 Aug 2022 11:36:13 -0300 Subject: [PATCH] (Network Stream) Add string functions (#14333) --- .../include/streams/network_stream.h | 53 ++++++++++++++ libretro-common/streams/network_stream.c | 72 +++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/libretro-common/include/streams/network_stream.h b/libretro-common/include/streams/network_stream.h index 0a80232f23..f1150c15ad 100644 --- a/libretro-common/include/streams/network_stream.h +++ b/libretro-common/include/streams/network_stream.h @@ -165,6 +165,33 @@ bool netstream_read_float(netstream_t *stream, float *data); bool netstream_read_double(netstream_t *stream, double *data); #endif +/** + * netstream_read_string: + * + * @stream : Pointer to a network stream object. + * @s : Pointer to a string buffer. + * @len : Size of the string buffer. + * + * Reads a string from the network stream. + * + * Returns: Length of the original string on success or + * a negative value on error. + */ +int netstream_read_string(netstream_t *stream, char *s, size_t len); + +/** + * netstream_read_fixed_string: + * + * @stream : Pointer to a network stream object. + * @s : Pointer to a string buffer. + * @len : Size of the string buffer. + * + * Reads a fixed-length string from the network stream. + * + * Returns: true on success, false otherwise. + */ +bool netstream_read_fixed_string(netstream_t *stream, char *s, size_t len); + /** * netstream_write: * @@ -198,6 +225,32 @@ bool netstream_write_float(netstream_t *stream, float data); bool netstream_write_double(netstream_t *stream, double data); #endif +/** + * netstream_write_string: + * + * @stream : Pointer to a network stream object. + * @s : Pointer to a string. + * + * Writes a null-terminated string into the network stream. + * + * Returns: true on success, false otherwise. + */ +bool netstream_write_string(netstream_t *stream, const char *s); + +/** + * netstream_write_fixed_string: + * + * @stream : Pointer to a network stream object. + * @s : Pointer to a string. + * @len : Size of the string. + * + * Writes a null-terminated fixed-length string into the network stream. + * + * Returns: true on success, false otherwise. + */ +bool netstream_write_fixed_string(netstream_t *stream, const char *s, + size_t len); + RETRO_END_DECLS #endif diff --git a/libretro-common/streams/network_stream.c b/libretro-common/streams/network_stream.c index 569e93659f..16071fd57b 100644 --- a/libretro-common/streams/network_stream.c +++ b/libretro-common/streams/network_stream.c @@ -178,6 +178,55 @@ NETSTREAM_READ_TYPE(double, double, uint64_t, retro_be_to_cpu64) #undef NETSTREAM_READ_TYPE #endif +int netstream_read_string(netstream_t *stream, char *s, size_t len) +{ + char c; + int ret = 0; + + if (!s || !len) + return -1; + + for (; --len; ret++) + { + if (!netstream_read(stream, &c, sizeof(c))) + return -1; + + *s++ = c; + + if (!c) + break; + } + + if (!len) + { + *s = '\0'; + + for (;; ret++) + { + if (!netstream_read(stream, &c, sizeof(c))) + return -1; + if (!c) + break; + } + } + + return ret; +} + +bool netstream_read_fixed_string(netstream_t *stream, char *s, size_t len) +{ + if (!len) + return false; + + if (!netstream_read(stream, s, len)) + return false; + + /* Ensure the string is always null-terminated. */ + s[len - 1] = '\0'; + + return true; +} + bool netstream_write(netstream_t *stream, const void *data, size_t len) { size_t remaining = stream->size - stream->pos; @@ -250,3 +299,26 @@ NETSTREAM_WRITE_TYPE(double, double, uint64_t, retro_cpu_to_be64) #undef NETSTREAM_WRITE_TYPE #endif + +bool netstream_write_string(netstream_t *stream, const char *s) +{ + if (!s) + return false; + + return netstream_write(stream, s, strlen(s) + 1); +} + +bool netstream_write_fixed_string(netstream_t *stream, const char *s, + size_t len) +{ + char end = '\0'; + + if (!netstream_write(stream, s, len)) + return false; + + /* Ensure the string is always null-terminated. */ + netstream_seek(stream, -1, NETSTREAM_SEEK_CUR); + netstream_write(stream, &end, sizeof(end)); + + return true; +}