1 /* Copyright (C) 2022 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (network_stream.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <retro_endianness.h>
28 #include <streams/network_stream.h>
30 bool netstream_open(netstream_t *stream, void *buf, size_t size, size_t used)
34 /* Pre-allocated buffer must have a non-zero size. */
35 if (!size || used > size)
58 void netstream_close(netstream_t *stream, bool dealloc)
62 memset(stream, 0, sizeof(*stream));
65 void netstream_reset(netstream_t *stream)
71 bool netstream_truncate(netstream_t *stream, size_t used)
73 if (used > stream->size)
78 /* If the current stream position is past our new end of stream,
79 set the current position to the end of the stream. */
80 if (stream->pos > used)
86 void netstream_data(netstream_t *stream, void **data, size_t *len)
92 bool netstream_eof(netstream_t *stream)
94 return stream->pos >= stream->used;
97 size_t netstream_tell(netstream_t *stream)
102 bool netstream_seek(netstream_t *stream, long offset, int origin)
104 long pos = (long)stream->pos;
105 long used = (long)stream->used;
109 case NETSTREAM_SEEK_SET:
112 case NETSTREAM_SEEK_CUR:
115 case NETSTREAM_SEEK_END:
122 if (pos < 0 || pos > used)
125 stream->pos = (size_t)pos;
130 bool netstream_read(netstream_t *stream, void *data, size_t len)
132 size_t remaining = stream->used - stream->pos;
134 if (!data || !remaining || len > remaining)
137 /* If len is 0, read all remaining bytes. */
141 memcpy(data, (uint8_t*)stream->buf + stream->pos, len);
148 /* This one doesn't require any swapping. */
149 bool netstream_read_byte(netstream_t *stream, uint8_t *data)
151 return netstream_read(stream, data, sizeof(*data));
154 #define NETSTREAM_READ_TYPE(name, type, swap) \
155 bool netstream_read_##name(netstream_t *stream, type *data) \
157 if (!netstream_read(stream, data, sizeof(*data))) \
159 *data = swap(*data); \
163 NETSTREAM_READ_TYPE(word, uint16_t, retro_be_to_cpu16)
164 NETSTREAM_READ_TYPE(dword, uint32_t, retro_be_to_cpu32)
165 NETSTREAM_READ_TYPE(qword, uint64_t, retro_be_to_cpu64)
167 #undef NETSTREAM_READ_TYPE
169 #ifdef __STDC_IEC_559__
170 #define NETSTREAM_READ_TYPE(name, type, type_alt, swap) \
171 bool netstream_read_##name(netstream_t *stream, type *data) \
173 type_alt *data_alt = (type_alt*)data; \
174 if (!netstream_read(stream, data, sizeof(*data))) \
176 *data_alt = swap(*data_alt); \
180 NETSTREAM_READ_TYPE(float, float, uint32_t, retro_be_to_cpu32)
181 NETSTREAM_READ_TYPE(double, double, uint64_t, retro_be_to_cpu64)
183 #undef NETSTREAM_READ_TYPE
186 int netstream_read_string(netstream_t *stream, char *s, size_t len)
196 if (!netstream_read(stream, &c, sizeof(c)))
211 if (!netstream_read(stream, &c, sizeof(c)))
221 bool netstream_read_fixed_string(netstream_t *stream, char *s, size_t len)
226 if (!netstream_read(stream, s, len))
229 /* Ensure the string is always null-terminated. */
235 bool netstream_write(netstream_t *stream, const void *data, size_t len)
237 size_t remaining = stream->size - stream->pos;
246 stream->buf = malloc(len);
253 size_t size = stream->size + (len - remaining);
254 void *buf = realloc(stream->buf, size);
264 memcpy((uint8_t*)stream->buf + stream->pos, data, len);
268 if (stream->pos > stream->used)
269 stream->used = stream->pos;
274 /* This one doesn't require any swapping. */
275 bool netstream_write_byte(netstream_t *stream, uint8_t data)
277 return netstream_write(stream, &data, sizeof(data));
280 #define NETSTREAM_WRITE_TYPE(name, type, swap) \
281 bool netstream_write_##name(netstream_t *stream, type data) \
284 return netstream_write(stream, &data, sizeof(data)); \
287 NETSTREAM_WRITE_TYPE(word, uint16_t, retro_cpu_to_be16)
288 NETSTREAM_WRITE_TYPE(dword, uint32_t, retro_cpu_to_be32)
289 NETSTREAM_WRITE_TYPE(qword, uint64_t, retro_cpu_to_be64)
291 #undef NETSTREAM_WRITE_TYPE
293 #ifdef __STDC_IEC_559__
294 #define NETSTREAM_WRITE_TYPE(name, type, type_alt, swap) \
295 bool netstream_write_##name(netstream_t *stream, type data) \
297 type_alt *data_alt = (type_alt*)&data; \
298 *data_alt = swap(*data_alt); \
299 return netstream_write(stream, &data, sizeof(data)); \
302 NETSTREAM_WRITE_TYPE(float, float, uint32_t, retro_cpu_to_be32)
303 NETSTREAM_WRITE_TYPE(double, double, uint64_t, retro_cpu_to_be64)
305 #undef NETSTREAM_WRITE_TYPE
308 bool netstream_write_string(netstream_t *stream, const char *s)
313 return netstream_write(stream, s, strlen(s) + 1);
316 bool netstream_write_fixed_string(netstream_t *stream, const char *s,
321 if (!netstream_write(stream, s, len))
324 /* Ensure the string is always null-terminated. */
325 netstream_seek(stream, -1, NETSTREAM_SEEK_CUR);
326 netstream_write(stream, &end, sizeof(end));