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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* Copyright (C) 2022 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (network_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <retro_endianness.h>
#include <streams/network_stream.h>
bool netstream_open(netstream_t *stream, void *buf, size_t size, size_t used)
{
if (buf)
{
/* Pre-allocated buffer must have a non-zero size. */
if (!size || used > size)
return false;
}
else
{
if (size)
{
buf = malloc(size);
if (!buf)
return false;
}
used = 0;
}
stream->buf = buf;
stream->size = size;
stream->used = used;
stream->pos = 0;
return true;
}
void netstream_close(netstream_t *stream, bool dealloc)
{
if (dealloc)
free(stream->buf);
memset(stream, 0, sizeof(*stream));
}
void netstream_reset(netstream_t *stream)
{
stream->pos = 0;
stream->used = 0;
}
bool netstream_truncate(netstream_t *stream, size_t used)
{
if (used > stream->size)
return false;
stream->used = used;
/* If the current stream position is past our new end of stream,
set the current position to the end of the stream. */
if (stream->pos > used)
stream->pos = used;
return true;
}
void netstream_data(netstream_t *stream, void **data, size_t *len)
{
*data = stream->buf;
*len = stream->used;
}
bool netstream_eof(netstream_t *stream)
{
return stream->pos >= stream->used;
}
size_t netstream_tell(netstream_t *stream)
{
return stream->pos;
}
bool netstream_seek(netstream_t *stream, long offset, int origin)
{
long pos = (long)stream->pos;
long used = (long)stream->used;
switch (origin)
{
case NETSTREAM_SEEK_SET:
pos = offset;
break;
case NETSTREAM_SEEK_CUR:
pos += offset;
break;
case NETSTREAM_SEEK_END:
pos = used + offset;
break;
default:
return false;
}
if (pos < 0 || pos > used)
return false;
stream->pos = (size_t)pos;
return true;
}
bool netstream_read(netstream_t *stream, void *data, size_t len)
{
size_t remaining = stream->used - stream->pos;
if (!data || !remaining || len > remaining)
return false;
/* If len is 0, read all remaining bytes. */
if (!len)
len = remaining;
memcpy(data, (uint8_t*)stream->buf + stream->pos, len);
stream->pos += len;
return true;
}
/* This one doesn't require any swapping. */
bool netstream_read_byte(netstream_t *stream, uint8_t *data)
{
return netstream_read(stream, data, sizeof(*data));
}
#define NETSTREAM_READ_TYPE(name, type, swap) \
bool netstream_read_##name(netstream_t *stream, type *data) \
{ \
if (!netstream_read(stream, data, sizeof(*data))) \
return false; \
*data = swap(*data); \
return true; \
}
NETSTREAM_READ_TYPE(word, uint16_t, retro_be_to_cpu16)
NETSTREAM_READ_TYPE(dword, uint32_t, retro_be_to_cpu32)
NETSTREAM_READ_TYPE(qword, uint64_t, retro_be_to_cpu64)
#undef NETSTREAM_READ_TYPE
#ifdef __STDC_IEC_559__
#define NETSTREAM_READ_TYPE(name, type, type_alt, swap) \
bool netstream_read_##name(netstream_t *stream, type *data) \
{ \
type_alt *data_alt = (type_alt*)data; \
if (!netstream_read(stream, data, sizeof(*data))) \
return false; \
*data_alt = swap(*data_alt); \
return true; \
}
NETSTREAM_READ_TYPE(float, float, uint32_t, retro_be_to_cpu32)
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;
if (!data || !len)
return false;
if (len > remaining)
{
if (!stream->size)
{
if (stream->buf)
free(stream->buf);
stream->buf = malloc(len);
if (!stream->buf)
return false;
stream->size = len;
}
else
{
size_t size = stream->size + (len - remaining);
void *buf = realloc(stream->buf, size);
if (!buf)
return false;
stream->buf = buf;
stream->size = size;
}
}
memcpy((uint8_t*)stream->buf + stream->pos, data, len);
stream->pos += len;
if (stream->pos > stream->used)
stream->used = stream->pos;
return true;
}
/* This one doesn't require any swapping. */
bool netstream_write_byte(netstream_t *stream, uint8_t data)
{
return netstream_write(stream, &data, sizeof(data));
}
#define NETSTREAM_WRITE_TYPE(name, type, swap) \
bool netstream_write_##name(netstream_t *stream, type data) \
{ \
data = swap(data); \
return netstream_write(stream, &data, sizeof(data)); \
}
NETSTREAM_WRITE_TYPE(word, uint16_t, retro_cpu_to_be16)
NETSTREAM_WRITE_TYPE(dword, uint32_t, retro_cpu_to_be32)
NETSTREAM_WRITE_TYPE(qword, uint64_t, retro_cpu_to_be64)
#undef NETSTREAM_WRITE_TYPE
#ifdef __STDC_IEC_559__
#define NETSTREAM_WRITE_TYPE(name, type, type_alt, swap) \
bool netstream_write_##name(netstream_t *stream, type data) \
{ \
type_alt *data_alt = (type_alt*)&data; \
*data_alt = swap(*data_alt); \
return netstream_write(stream, &data, sizeof(data)); \
}
NETSTREAM_WRITE_TYPE(float, float, uint32_t, retro_cpu_to_be32)
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;
}
|