| 1 | #cxx . . $ `<file>/mednafen/MemoryStream.cpp`/include <stdint.h> |
| 2 | #include "MemoryStream.h"cxx . . $ `<file>/mednafen/MemoryStream.h`/ |
| 3 | |
| 4 | #ifdef _XBOX |
| 5 | #ifndef SIZE_MAX |
| 6 | #define SIZE_MAX _UI32_MAX |
| 7 | #endif |
| 8 | #endif |
| 9 | |
| 10 | /* |
| 11 | TODO: |
| 12 | Write and Seek expansion that fail should not corrupt the state. |
| 13 | |
| 14 | Copy and assignment constructor fixes. |
| 15 | */ |
| 16 | |
| 17 | // TODO 128-bit integers for range checking? |
| 18 | |
| 19 | |
| 20 | MemoryStreamcxx . . $ MemoryStream#::MemoryStreamcxx . . $ MemoryStream#MemoryStream(49f6e7a06ebc5aa8).() : data_buffercxx . . $ MemoryStream#data_buffer.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!), data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.(0), data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.(0), positioncxx . . $ MemoryStream#position.(0) |
| 21 | { |
| 22 | data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. = 0; |
| 23 | data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced. = 64; |
| 24 | if(!(data_buffercxx . . $ MemoryStream#data_buffer. = (uint8cxx . . $ uint8#*)realloccxx . . $ realloc(42b2a9dd8f084bf2).(data_buffercxx . . $ MemoryStream#data_buffer., data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.))) |
| 25 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 26 | } |
| 27 | |
| 28 | MemoryStreamcxx . . $ MemoryStream#::MemoryStreamcxx . . $ MemoryStream#MemoryStream(2c44cd906ae4e829).(uint64cxx . . $ uint64# size_hint) : data_buffercxx . . $ MemoryStream#data_buffer.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!), data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.(0), data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.(0), positioncxx . . $ MemoryStream#position.(0) |
| 29 | { |
| 30 | data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. = 0; |
| 31 | data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced. = (size_hint > SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!) ? SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`! : size_hint; |
| 32 | |
| 33 | if(!(data_buffercxx . . $ MemoryStream#data_buffer. = (uint8cxx . . $ uint8#*)realloccxx . . $ realloc(42b2a9dd8f084bf2).(data_buffercxx . . $ MemoryStream#data_buffer., data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.))) |
| 34 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 35 | } |
| 36 | |
| 37 | MemoryStreamcxx . . $ MemoryStream#::MemoryStreamcxx . . $ MemoryStream#MemoryStream(6b6a019ff8e61ec0).(Streamcxx . . $ Stream# *stream) : data_buffercxx . . $ MemoryStream#data_buffer.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!), data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.(0), data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.(0), positioncxx . . $ MemoryStream#position.(0) |
| 38 | { |
| 39 | if((positioncxx . . $ MemoryStream#position. = stream->tellcxx . . $ Stream#tell(da6335ded91d79c2).()) != 0) |
| 40 | stream->seekcxx . . $ Stream#seek(3b75d2d14f60ac6a).(0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 41 | |
| 42 | data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. = stream->sizecxx . . $ Stream#size(da6335ded91d79c2).(); |
| 43 | data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced. = data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.; |
| 44 | if(!(data_buffercxx . . $ MemoryStream#data_buffer. = (uint8cxx . . $ uint8#*)realloccxx . . $ realloc(42b2a9dd8f084bf2).(data_buffercxx . . $ MemoryStream#data_buffer., data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.))) |
| 45 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 46 | |
| 47 | stream->readcxx . . $ Stream#read(a1f03d5c460544d4).(data_buffercxx . . $ MemoryStream#data_buffer., data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.); |
| 48 | |
| 49 | stream->closecxx . . $ Stream#close(49f6e7a06ebc5aa8).(); |
| 50 | delete stream; |
| 51 | } |
| 52 | |
| 53 | MemoryStreamcxx . . $ MemoryStream#::MemoryStreamcxx . . $ MemoryStream#MemoryStream(49c4ec8e03c7c495).(const MemoryStreamcxx . . $ MemoryStream# &zs) |
| 54 | { |
| 55 | data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. = zs.data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.; |
| 56 | data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced. = zs.data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.; |
| 57 | if(!(data_buffercxx . . $ MemoryStream#data_buffer. = (uint8cxx . . $ uint8#*)malloccxx . . $ malloc(9f008b2a9698428b).(data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.))) |
| 58 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 59 | |
| 60 | memcpy(data_buffercxx . . $ MemoryStream#data_buffer., zs.data_buffercxx . . $ MemoryStream#data_buffer., data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.); |
| 61 | |
| 62 | positioncxx . . $ MemoryStream#position. = zs.positioncxx . . $ MemoryStream#position.; |
| 63 | } |
| 64 | |
| 65 | MemoryStreamcxx . . $ MemoryStream#::~cxx . . $ MemoryStream#`~MemoryStream`(ced63f7c635d850d).MemoryStreamcxx . . $ MemoryStream#() |
| 66 | { |
| 67 | if(data_buffercxx . . $ MemoryStream#data_buffer.) |
| 68 | { |
| 69 | freecxx . . $ free(bd21765a0afc8e3c).(data_buffercxx . . $ MemoryStream#data_buffer.); |
| 70 | data_buffercxx . . $ MemoryStream#data_buffer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | uint64cxx . . $ uint64# MemoryStreamcxx . . $ MemoryStream#::attributescxx . . $ MemoryStream#attributes(f708dce9a8856c70).(void) |
| 75 | { |
| 76 | return (ATTRIBUTE_READABLEcxx . . $ Stream#ATTRIBUTE_READABLE. | ATTRIBUTE_WRITEABLEcxx . . $ Stream#ATTRIBUTE_WRITEABLE. | ATTRIBUTE_SEEKABLEcxx . . $ Stream#ATTRIBUTE_SEEKABLE.); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | uint8cxx . . $ uint8# *MemoryStreamcxx . . $ MemoryStream#::mapcxx . . $ MemoryStream#map(37d8967d10cb1e7b).(void) |
| 81 | { |
| 82 | return data_buffercxx . . $ MemoryStream#data_buffer.; |
| 83 | } |
| 84 | |
| 85 | void MemoryStreamcxx . . $ MemoryStream#::unmapcxx . . $ MemoryStream#unmap(49f6e7a06ebc5aa8).(void) |
| 86 | { |
| 87 | |
| 88 | } |
| 89 | |
| 90 | |
| 91 | INLINEcxx . . $ `mednafen/mednafen-types.h:21:11`! void MemoryStreamcxx . . $ MemoryStream#::grow_if_necessarycxx . . $ MemoryStream#grow_if_necessary(2c44cd906ae4e829).(uint64cxx . . $ uint64# new_required_size) |
| 92 | { |
| 93 | if(new_required_size > data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.) |
| 94 | { |
| 95 | if(new_required_size > data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced.) |
| 96 | { |
| 97 | uint64cxx . . $ uint64# new_required_alloced = round_up_pow2cxx . . $ round_up_pow2(2141b656ce2cc063).(new_required_size); |
| 98 | uint8cxx . . $ uint8# *new_data_buffer; |
| 99 | |
| 100 | // first condition will happen at new_required_size > (1ULL << 63) due to round_up_pow2() "wrapping". |
| 101 | // second condition can occur when running on a 32-bit system. |
| 102 | if(new_required_alloced < new_required_size || new_required_alloced > SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!) |
| 103 | new_required_alloced = SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!; |
| 104 | |
| 105 | // If constrained alloc size isn't enough, throw an out-of-memory/address-space type error. |
| 106 | if(new_required_alloced < new_required_size) |
| 107 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!)); |
| 108 | |
| 109 | if(!(new_data_buffer = (uint8cxx . . $ uint8#*)realloccxx . . $ realloc(42b2a9dd8f084bf2).(data_buffercxx . . $ MemoryStream#data_buffer., new_required_alloced))) |
| 110 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 111 | |
| 112 | // |
| 113 | // Assign all in one go after the realloc() so we don't leave our object in an inconsistent state if the realloc() fails. |
| 114 | // |
| 115 | data_buffercxx . . $ MemoryStream#data_buffer. = new_data_buffer; |
| 116 | data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. = new_required_size; |
| 117 | data_buffer_allocedcxx . . $ MemoryStream#data_buffer_alloced. = new_required_alloced; |
| 118 | } |
| 119 | else |
| 120 | data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. = new_required_size; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | uint64cxx . . $ uint64# MemoryStreamcxx . . $ MemoryStream#::readcxx . . $ MemoryStream#read(a1f03d5c460544d4).(void *data, uint64cxx . . $ uint64# count, bool error_on_eos) |
| 125 | { |
| 126 | if(count > data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.) |
| 127 | { |
| 128 | count = data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.; |
| 129 | } |
| 130 | |
| 131 | if((uint64cxx . . $ uint64#)positioncxx . . $ MemoryStream#position. > (data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. - count)) |
| 132 | { |
| 133 | count = data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. - positioncxx . . $ MemoryStream#position.; |
| 134 | } |
| 135 | |
| 136 | memmove(data, &data_buffercxx . . $ MemoryStream#data_buffer.[positioncxx . . $ MemoryStream#position.], count); |
| 137 | positioncxx . . $ MemoryStream#position. += count; |
| 138 | |
| 139 | return count; |
| 140 | } |
| 141 | |
| 142 | void MemoryStreamcxx . . $ MemoryStream#::writecxx . . $ MemoryStream#write(8c7266571c1d65cc).(const void *data, uint64cxx . . $ uint64# count) |
| 143 | { |
| 144 | uint64cxx . . $ uint64# nrs = positioncxx . . $ MemoryStream#position. + count; |
| 145 | |
| 146 | if(nrs < positioncxx . . $ MemoryStream#position.) |
| 147 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(EFBIGcxx . . $ `<external>/8987895939074593727/errno-base.h:31:9`!)); |
| 148 | |
| 149 | grow_if_necessarycxx . . $ MemoryStream#grow_if_necessary(2c44cd906ae4e829).(nrs); |
| 150 | |
| 151 | memmove(&data_buffercxx . . $ MemoryStream#data_buffer.[positioncxx . . $ MemoryStream#position.], data, count); |
| 152 | positioncxx . . $ MemoryStream#position. += count; |
| 153 | } |
| 154 | |
| 155 | void MemoryStreamcxx . . $ MemoryStream#::seekcxx . . $ MemoryStream#seek(3b75d2d14f60ac6a).(int64cxx . . $ int64# offset, int whence) |
| 156 | { |
| 157 | int64cxx . . $ int64# new_position; |
| 158 | |
| 159 | switch(whence) |
| 160 | { |
| 161 | default: |
| 162 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!)); |
| 163 | break; |
| 164 | |
| 165 | case SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!: |
| 166 | new_position = offset; |
| 167 | break; |
| 168 | |
| 169 | case SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!: |
| 170 | new_position = positioncxx . . $ MemoryStream#position. + offset; |
| 171 | break; |
| 172 | |
| 173 | case SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!: |
| 174 | new_position = data_buffer_sizecxx . . $ MemoryStream#data_buffer_size. + offset; |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | if(new_position < 0) |
| 179 | throw MDFN_Errorcxx . . $ MDFN_Error#(ErrnoHoldercxx . . $ ErrnoHolder#ErrnoHolder(d4f767463ce0a6b3).(EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!)); |
| 180 | else |
| 181 | grow_if_necessarycxx . . $ MemoryStream#grow_if_necessary(2c44cd906ae4e829).(new_position); |
| 182 | |
| 183 | positioncxx . . $ MemoryStream#position. = new_position; |
| 184 | } |
| 185 | |
| 186 | int64cxx . . $ int64# MemoryStreamcxx . . $ MemoryStream#::tellcxx . . $ MemoryStream#tell(da6335ded91d79c2).(void) |
| 187 | { |
| 188 | return positioncxx . . $ MemoryStream#position.; |
| 189 | } |
| 190 | |
| 191 | int64cxx . . $ int64# MemoryStreamcxx . . $ MemoryStream#::sizecxx . . $ MemoryStream#size(da6335ded91d79c2).(void) |
| 192 | { |
| 193 | return data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.; |
| 194 | } |
| 195 | |
| 196 | void MemoryStreamcxx . . $ MemoryStream#::closecxx . . $ MemoryStream#close(49f6e7a06ebc5aa8).(void) |
| 197 | { |
| 198 | |
| 199 | } |
| 200 | |
| 201 | |
| 202 | int MemoryStreamcxx . . $ MemoryStream#::get_linecxx . . $ MemoryStream#get_line(9b8588921b9c6c2f).(stdcxx . . $ std/::stringcxx . . $ std/string# &str) |
| 203 | { |
| 204 | str.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).(); // or str.resize(0)?? |
| 205 | |
| 206 | while(positioncxx . . $ MemoryStream#position. < data_buffer_sizecxx . . $ MemoryStream#data_buffer_size.) |
| 207 | { |
| 208 | uint8cxx . . $ uint8# c = data_buffercxx . . $ MemoryStream#data_buffer.[positioncxx . . $ MemoryStream#position.++]; |
| 209 | |
| 210 | if(c == '\r' || c == '\n' || c == 0) |
| 211 | return(c); |
| 212 | |
| 213 | str.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(c); // Should be faster than str.append(1, c) |
| 214 | } |
| 215 | |
| 216 | return(-1); |
| 217 | } |
| 218 |