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
|
/* Copyright (C) 2022 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (network_stream.h).
* ---------------------------------------------------------------------------------------
*
* 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.
*/
#ifndef _LIBRETRO_SDK_NETWORK_STREAM_H
#define _LIBRETRO_SDK_NETWORK_STREAM_H
#include <stddef.h>
#include <stdint.h>
#include <boolean.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
enum
{
NETSTREAM_SEEK_SET = 0,
NETSTREAM_SEEK_CUR,
NETSTREAM_SEEK_END
};
typedef struct netstream
{
void *buf;
size_t size;
size_t used;
size_t pos;
} netstream_t;
/**
* netstream_open:
*
* @stream : Pointer to a network stream object.
* @buf : Pre-allocated buffer. Pass NULL to dynamically allocate a buffer.
* @size : Buffer size. Pass 0 for no pre-allocated/initial buffer.
* @used : Buffer bytes in use. Ignored for non pre-allocated buffers.
*
* Opens a network stream.
*
* Returns: true on success, false otherwise.
*/
bool netstream_open(netstream_t *stream, void *buf, size_t size, size_t used);
/**
* netstream_close:
*
* @stream : Pointer to a network stream object.
* @dealloc : Whether to deallocate/free the buffer or not.
*
* Closes a network stream.
*
*/
void netstream_close(netstream_t *stream, bool dealloc);
/**
* netstream_reset:
*
* @stream : Pointer to a network stream object.
*
* Resets a network stream to its initial position,
* discarding any used bytes in the process.
*
*/
void netstream_reset(netstream_t *stream);
/**
* netstream_truncate:
*
* @stream : Pointer to a network stream object.
* @used : Amount of bytes used.
*
* Truncates the network stream.
* Truncation can either extend or reduce the amount of bytes used.
*
* Returns: true on success, false otherwise.
*/
bool netstream_truncate(netstream_t *stream, size_t used);
/**
* netstream_data:
*
* @stream : Pointer to a network stream object.
* @data : Pointer to an object to store a reference of the stream's data.
* @len : Pointer to an object to store the amount of bytes in use.
*
* Gets the network stream's data.
*
*/
void netstream_data(netstream_t *stream, void **data, size_t *len);
/**
* netstream_eof:
*
* @stream : Pointer to a network stream object.
*
* Checks whether the network stream is at EOF or not.
*
* Returns: true if the stream is at EOF, false otherwise.
*/
bool netstream_eof(netstream_t *stream);
/**
* netstream_tell:
*
* @stream : Pointer to a network stream object.
*
* Gets the network stream's current position.
*
* Returns: current value of the position indicator.
*/
size_t netstream_tell(netstream_t *stream);
/**
* netstream_seek:
*
* @stream : Pointer to a network stream object.
* @offset : Position's offset.
* @origin : Position used as reference for the offset.
*
* Sets the network stream's current position.
*
* Returns: true on success, false otherwise.
*/
bool netstream_seek(netstream_t *stream, long offset, int origin);
/**
* netstream_read:
*
* @stream : Pointer to a network stream object.
* @data : Pointer to a storage for data read from the network stream.
* @len : Amount of bytes to read. Pass 0 to read all remaining bytes.
*
* Reads raw data from the network stream.
*
* Returns: true on success, false otherwise.
*/
bool netstream_read(netstream_t *stream, void *data, size_t len);
/**
* netstream_read_(type):
*
* @stream : Pointer to a network stream object.
* @data : Pointer to a storage for data read from the network stream.
*
* Reads data from the network stream.
* Network byte order is always big endian.
*
* Returns: true on success, false otherwise.
*/
bool netstream_read_byte(netstream_t *stream, uint8_t *data);
bool netstream_read_word(netstream_t *stream, uint16_t *data);
bool netstream_read_dword(netstream_t *stream, uint32_t *data);
bool netstream_read_qword(netstream_t *stream, uint64_t *data);
#ifdef __STDC_IEC_559__
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:
*
* @stream : Pointer to a network stream object.
* @data : Data to write into the network stream.
* @len : Amount of bytes to write.
*
* Writes raw data into the network stream.
*
* Returns: true on success, false otherwise.
*/
bool netstream_write(netstream_t *stream, const void *data, size_t len);
/**
* netstream_write_(type):
*
* @stream : Pointer to a network stream object.
* @data : Data to write into the network stream.
*
* Writes data into the network stream.
* Network byte order is always big endian.
*
* Returns: true on success, false otherwise.
*/
bool netstream_write_byte(netstream_t *stream, uint8_t data);
bool netstream_write_word(netstream_t *stream, uint16_t data);
bool netstream_write_dword(netstream_t *stream, uint32_t data);
bool netstream_write_qword(netstream_t *stream, uint64_t data);
#ifdef __STDC_IEC_559__
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
|