| 1 | /* mz_strm_mem.c -- Stream for memory access |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | This interface is designed to access memory rather than files. |
| 5 | We do use a region of memory to put data in to and take it out of. |
| 6 | |
| 7 | Based on Unzip ioapi.c version 0.22, May 19th, 2003 |
| 8 | |
| 9 | Copyright (C) Nathan Moinvaziri |
| 10 | https://github.com/zlib-ng/minizip-ng |
| 11 | Copyright (C) 2003 Justin Fletcher |
| 12 | Copyright (C) 1998-2003 Gilles Vollant |
| 13 | https://www.winimage.com/zLibDll/minizip.html |
| 14 | |
| 15 | This program is distributed under the terms of the same license as zlib. |
| 16 | See the accompanying LICENSE file for the full text of the license. |
| 17 | */ |
| 18 | |
| 19 | #include "mz.h"cxx . . $ `<file>/mz.h`/ |
| 20 | #include "mz_strm.h"cxx . . $ `<file>/mz_strm.h`/ |
| 21 | #include "mz_strm_mem.h"cxx . . $ `<file>/mz_strm_mem.h`/ |
| 22 | |
| 23 | /***************************************************************************/ |
| 24 | |
| 25 | static mz_stream_vtblcxx . . $ mz_stream_vtbl# mz_stream_mem_vtblcxx . . $ mz_stream_mem_vtbl. = {mz_stream_mem_opencxx . . $ mz_stream_mem_open(7083a8984c0735e8)., |
| 26 | mz_stream_mem_is_opencxx . . $ mz_stream_mem_is_open(dedf1e9b1c614a07)., |
| 27 | mz_stream_mem_readcxx . . $ mz_stream_mem_read(8bc41909eac9e5d8)., |
| 28 | mz_stream_mem_writecxx . . $ mz_stream_mem_write(91e54fd90adbb21)., |
| 29 | mz_stream_mem_tellcxx . . $ mz_stream_mem_tell(72c1818a5177f981)., |
| 30 | mz_stream_mem_seekcxx . . $ mz_stream_mem_seek(8bc0ff128761c75d)., |
| 31 | mz_stream_mem_closecxx . . $ mz_stream_mem_close(dedf1e9b1c614a07)., |
| 32 | mz_stream_mem_errorcxx . . $ mz_stream_mem_error(dedf1e9b1c614a07)., |
| 33 | mz_stream_mem_createcxx . . $ mz_stream_mem_create(50c7df7de5344ebb)., |
| 34 | mz_stream_mem_deletecxx . . $ mz_stream_mem_delete(539e082cf00e1f43)., |
| 35 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 36 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!}; |
| 37 | |
| 38 | /***************************************************************************/ |
| 39 | |
| 40 | typedef struct mz_stream_mem_scxx . . $ mz_stream_mem_s# { |
| 41 | mz_streamcxx . . $ mz_stream# streamcxx . . $ mz_stream_mem_s#stream.; |
| 42 | int32_tcxx . . $ int32_t# modecxx . . $ mz_stream_mem_s#mode.; |
| 43 | uint8_tcxx . . $ uint8_t# *buffercxx . . $ mz_stream_mem_s#buffer.; /* Memory buffer pointer */ |
| 44 | int32_tcxx . . $ int32_t# sizecxx . . $ mz_stream_mem_s#size.; /* Size of the memory buffer */ |
| 45 | int32_tcxx . . $ int32_t# limitcxx . . $ mz_stream_mem_s#limit.; /* Furthest we've written */ |
| 46 | int32_tcxx . . $ int32_t# positioncxx . . $ mz_stream_mem_s#position.; /* Current position in the memory */ |
| 47 | int32_tcxx . . $ int32_t# grow_sizecxx . . $ mz_stream_mem_s#grow_size.; /* Size to grow when full */ |
| 48 | } mz_stream_memcxx . . $ mz_stream_mem#; |
| 49 | |
| 50 | /***************************************************************************/ |
| 51 | |
| 52 | static int32_tcxx . . $ int32_t# mz_stream_mem_set_sizecxx . . $ mz_stream_mem_set_size(7fb3b8e28157cebd).(void *stream, int32_tcxx . . $ int32_t# size) { |
| 53 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 54 | int32_tcxx . . $ int32_t# new_size = size; |
| 55 | uint8_tcxx . . $ uint8_t# *new_buf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 56 | |
| 57 | new_buf = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).((uint32_tcxx . . $ uint32_t#)new_size); |
| 58 | if (!new_buf) |
| 59 | return MZ_BUF_ERRORcxx . . $ `mz.h:25:9`!; |
| 60 | |
| 61 | if (mem->buffercxx . . $ mz_stream_mem_s#buffer.) { |
| 62 | memcpycxx . . $ memcpy(cc817cb175f282d).(new_buf, mem->buffercxx . . $ mz_stream_mem_s#buffer., mem->sizecxx . . $ mz_stream_mem_s#size.); |
| 63 | freecxx . . $ free(5d22bdacc48458e8).(mem->buffercxx . . $ mz_stream_mem_s#buffer.); |
| 64 | } |
| 65 | |
| 66 | mem->buffercxx . . $ mz_stream_mem_s#buffer. = new_buf; |
| 67 | mem->sizecxx . . $ mz_stream_mem_s#size. = new_size; |
| 68 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 69 | } |
| 70 | |
| 71 | int32_tcxx . . $ int32_t# mz_stream_mem_opencxx . . $ mz_stream_mem_open(7083a8984c0735e8).(void *stream, const char *path, int32_tcxx . . $ int32_t# mode) { |
| 72 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 73 | int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!; |
| 74 | |
| 75 | MZ_UNUSEDcxx . . $ `mz.h:147:9`!(path); |
| 76 | |
| 77 | mem->modecxx . . $ mz_stream_mem_s#mode. = mode; |
| 78 | mem->limitcxx . . $ mz_stream_mem_s#limit. = 0; |
| 79 | mem->positioncxx . . $ mz_stream_mem_s#position. = 0; |
| 80 | |
| 81 | if (mem->modecxx . . $ mz_stream_mem_s#mode. & MZ_OPEN_MODE_CREATEcxx . . $ `mz.h:54:9`!) |
| 82 | err = mz_stream_mem_set_sizecxx . . $ mz_stream_mem_set_size(7fb3b8e28157cebd).(stream, mem->grow_sizecxx . . $ mz_stream_mem_s#grow_size.); |
| 83 | else |
| 84 | mem->limitcxx . . $ mz_stream_mem_s#limit. = mem->sizecxx . . $ mz_stream_mem_s#size.; |
| 85 | |
| 86 | return err; |
| 87 | } |
| 88 | |
| 89 | int32_tcxx . . $ int32_t# mz_stream_mem_is_opencxx . . $ mz_stream_mem_is_open(dedf1e9b1c614a07).(void *stream) { |
| 90 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 91 | if (!mem->buffercxx . . $ mz_stream_mem_s#buffer.) |
| 92 | return MZ_OPEN_ERRORcxx . . $ `mz.h:40:9`!; |
| 93 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 94 | } |
| 95 | |
| 96 | int32_tcxx . . $ int32_t# mz_stream_mem_readcxx . . $ mz_stream_mem_read(8bc41909eac9e5d8).(void *stream, void *buf, int32_tcxx . . $ int32_t# size) { |
| 97 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 98 | |
| 99 | if (size > mem->sizecxx . . $ mz_stream_mem_s#size. - mem->positioncxx . . $ mz_stream_mem_s#position.) |
| 100 | size = mem->sizecxx . . $ mz_stream_mem_s#size. - mem->positioncxx . . $ mz_stream_mem_s#position.; |
| 101 | if (mem->positioncxx . . $ mz_stream_mem_s#position. + size > mem->limitcxx . . $ mz_stream_mem_s#limit.) |
| 102 | size = mem->limitcxx . . $ mz_stream_mem_s#limit. - mem->positioncxx . . $ mz_stream_mem_s#position.; |
| 103 | |
| 104 | if (size <= 0) |
| 105 | return 0; |
| 106 | |
| 107 | memcpycxx . . $ memcpy(cc817cb175f282d).(buf, mem->buffercxx . . $ mz_stream_mem_s#buffer. + mem->positioncxx . . $ mz_stream_mem_s#position., size); |
| 108 | mem->positioncxx . . $ mz_stream_mem_s#position. += size; |
| 109 | |
| 110 | return size; |
| 111 | } |
| 112 | |
| 113 | int32_tcxx . . $ int32_t# mz_stream_mem_writecxx . . $ mz_stream_mem_write(91e54fd90adbb21).(void *stream, const void *buf, int32_tcxx . . $ int32_t# size) { |
| 114 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 115 | int32_tcxx . . $ int32_t# new_size = 0; |
| 116 | int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!; |
| 117 | |
| 118 | if (!size) |
| 119 | return size; |
| 120 | |
| 121 | if (size > mem->sizecxx . . $ mz_stream_mem_s#size. - mem->positioncxx . . $ mz_stream_mem_s#position.) { |
| 122 | if (mem->modecxx . . $ mz_stream_mem_s#mode. & MZ_OPEN_MODE_CREATEcxx . . $ `mz.h:54:9`!) { |
| 123 | new_size = mem->sizecxx . . $ mz_stream_mem_s#size.; |
| 124 | if (size < mem->grow_sizecxx . . $ mz_stream_mem_s#grow_size.) |
| 125 | new_size += mem->grow_sizecxx . . $ mz_stream_mem_s#grow_size.; |
| 126 | else |
| 127 | new_size += size; |
| 128 | |
| 129 | err = mz_stream_mem_set_sizecxx . . $ mz_stream_mem_set_size(7fb3b8e28157cebd).(stream, new_size); |
| 130 | if (err != MZ_OKcxx . . $ `mz.h:21:9`!) |
| 131 | return err; |
| 132 | } else { |
| 133 | size = mem->sizecxx . . $ mz_stream_mem_s#size. - mem->positioncxx . . $ mz_stream_mem_s#position.; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | memcpycxx . . $ memcpy(cc817cb175f282d).(mem->buffercxx . . $ mz_stream_mem_s#buffer. + mem->positioncxx . . $ mz_stream_mem_s#position., buf, size); |
| 138 | |
| 139 | mem->positioncxx . . $ mz_stream_mem_s#position. += size; |
| 140 | if (mem->positioncxx . . $ mz_stream_mem_s#position. > mem->limitcxx . . $ mz_stream_mem_s#limit.) |
| 141 | mem->limitcxx . . $ mz_stream_mem_s#limit. = mem->positioncxx . . $ mz_stream_mem_s#position.; |
| 142 | |
| 143 | return size; |
| 144 | } |
| 145 | |
| 146 | int64_tcxx . . $ int64_t# mz_stream_mem_tellcxx . . $ mz_stream_mem_tell(72c1818a5177f981).(void *stream) { |
| 147 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 148 | return mem->positioncxx . . $ mz_stream_mem_s#position.; |
| 149 | } |
| 150 | |
| 151 | int32_tcxx . . $ int32_t# mz_stream_mem_seekcxx . . $ mz_stream_mem_seek(8bc0ff128761c75d).(void *stream, int64_tcxx . . $ int64_t# offset, int32_tcxx . . $ int32_t# origin) { |
| 152 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 153 | int64_tcxx . . $ int64_t# new_pos = 0; |
| 154 | int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!; |
| 155 | |
| 156 | switch (origin) { |
| 157 | case MZ_SEEK_CURcxx . . $ `mz.h:59:9`!: |
| 158 | new_pos = mem->positioncxx . . $ mz_stream_mem_s#position. + offset; |
| 159 | break; |
| 160 | case MZ_SEEK_ENDcxx . . $ `mz.h:60:9`!: |
| 161 | new_pos = mem->limitcxx . . $ mz_stream_mem_s#limit. + offset; |
| 162 | break; |
| 163 | case MZ_SEEK_SETcxx . . $ `mz.h:58:9`!: |
| 164 | new_pos = offset; |
| 165 | break; |
| 166 | default: |
| 167 | return MZ_SEEK_ERRORcxx . . $ `mz.h:42:9`!; |
| 168 | } |
| 169 | |
| 170 | if (new_pos > mem->sizecxx . . $ mz_stream_mem_s#size.) { |
| 171 | if ((mem->modecxx . . $ mz_stream_mem_s#mode. & MZ_OPEN_MODE_CREATEcxx . . $ `mz.h:54:9`!) == 0) |
| 172 | return MZ_SEEK_ERRORcxx . . $ `mz.h:42:9`!; |
| 173 | |
| 174 | err = mz_stream_mem_set_sizecxx . . $ mz_stream_mem_set_size(7fb3b8e28157cebd).(stream, (int32_tcxx . . $ int32_t#)new_pos); |
| 175 | if (err != MZ_OKcxx . . $ `mz.h:21:9`!) |
| 176 | return err; |
| 177 | } else if (new_pos < 0) { |
| 178 | return MZ_SEEK_ERRORcxx . . $ `mz.h:42:9`!; |
| 179 | } |
| 180 | |
| 181 | mem->positioncxx . . $ mz_stream_mem_s#position. = (int32_tcxx . . $ int32_t#)new_pos; |
| 182 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 183 | } |
| 184 | |
| 185 | int32_tcxx . . $ int32_t# mz_stream_mem_closecxx . . $ mz_stream_mem_close(dedf1e9b1c614a07).(void *stream) { |
| 186 | MZ_UNUSEDcxx . . $ `mz.h:147:9`!(stream); |
| 187 | |
| 188 | /* We never return errors */ |
| 189 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 190 | } |
| 191 | |
| 192 | int32_tcxx . . $ int32_t# mz_stream_mem_errorcxx . . $ mz_stream_mem_error(dedf1e9b1c614a07).(void *stream) { |
| 193 | MZ_UNUSEDcxx . . $ `mz.h:147:9`!(stream); |
| 194 | |
| 195 | /* We never return errors */ |
| 196 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 197 | } |
| 198 | |
| 199 | void mz_stream_mem_set_buffercxx . . $ mz_stream_mem_set_buffer(5be095a8278d2bf5).(void *stream, void *buf, int32_tcxx . . $ int32_t# size) { |
| 200 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 201 | mem->buffercxx . . $ mz_stream_mem_s#buffer. = (uint8_tcxx . . $ uint8_t# *)buf; |
| 202 | mem->sizecxx . . $ mz_stream_mem_s#size. = size; |
| 203 | mem->limitcxx . . $ mz_stream_mem_s#limit. = size; |
| 204 | } |
| 205 | |
| 206 | int32_tcxx . . $ int32_t# mz_stream_mem_get_buffercxx . . $ mz_stream_mem_get_buffer(d7fce53f66dcd9ef).(void *stream, const void **buf) { |
| 207 | return mz_stream_mem_get_buffer_atcxx . . $ mz_stream_mem_get_buffer_at(46b739f8dccdd346).(stream, 0, buf); |
| 208 | } |
| 209 | |
| 210 | int32_tcxx . . $ int32_t# mz_stream_mem_get_buffer_atcxx . . $ mz_stream_mem_get_buffer_at(46b739f8dccdd346).(void *stream, int64_tcxx . . $ int64_t# position, const void **buf) { |
| 211 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 212 | if (!buf || position < 0 || !mem->buffercxx . . $ mz_stream_mem_s#buffer. || mem->sizecxx . . $ mz_stream_mem_s#size. < position) |
| 213 | return MZ_SEEK_ERRORcxx . . $ `mz.h:42:9`!; |
| 214 | *buf = mem->buffercxx . . $ mz_stream_mem_s#buffer. + position; |
| 215 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 216 | } |
| 217 | |
| 218 | int32_tcxx . . $ int32_t# mz_stream_mem_get_buffer_at_currentcxx . . $ mz_stream_mem_get_buffer_at_current(d7fce53f66dcd9ef).(void *stream, const void **buf) { |
| 219 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 220 | return mz_stream_mem_get_buffer_atcxx . . $ mz_stream_mem_get_buffer_at(46b739f8dccdd346).(stream, mem->positioncxx . . $ mz_stream_mem_s#position., buf); |
| 221 | } |
| 222 | |
| 223 | void mz_stream_mem_get_buffer_lengthcxx . . $ mz_stream_mem_get_buffer_length(6d7fe0143cf266f5).(void *stream, int32_tcxx . . $ int32_t# *length) { |
| 224 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 225 | *length = mem->limitcxx . . $ mz_stream_mem_s#limit.; |
| 226 | } |
| 227 | |
| 228 | void mz_stream_mem_set_buffer_limitcxx . . $ mz_stream_mem_set_buffer_limit(d0320739773070f2).(void *stream, int32_tcxx . . $ int32_t# limit) { |
| 229 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 230 | mem->limitcxx . . $ mz_stream_mem_s#limit. = limit; |
| 231 | } |
| 232 | |
| 233 | void mz_stream_mem_set_grow_sizecxx . . $ mz_stream_mem_set_grow_size(d0320739773070f2).(void *stream, int32_tcxx . . $ int32_t# grow_size) { |
| 234 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)stream; |
| 235 | mem->grow_sizecxx . . $ mz_stream_mem_s#grow_size. = grow_size; |
| 236 | } |
| 237 | |
| 238 | void *mz_stream_mem_createcxx . . $ mz_stream_mem_create(50c7df7de5344ebb).(void) { |
| 239 | mz_stream_memcxx . . $ mz_stream_mem# *mem = (mz_stream_memcxx . . $ mz_stream_mem# *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(mz_stream_memcxx . . $ mz_stream_mem#)); |
| 240 | if (mem) { |
| 241 | mem->streamcxx . . $ mz_stream_mem_s#stream..vtblcxx . . $ mz_stream_s#vtbl. = &mz_stream_mem_vtblcxx . . $ mz_stream_mem_vtbl.; |
| 242 | mem->grow_sizecxx . . $ mz_stream_mem_s#grow_size. = 4096; |
| 243 | } |
| 244 | return mem; |
| 245 | } |
| 246 | |
| 247 | void mz_stream_mem_deletecxx . . $ mz_stream_mem_delete(539e082cf00e1f43).(void **stream) { |
| 248 | mz_stream_memcxx . . $ mz_stream_mem# *mem = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 249 | if (!stream) |
| 250 | return; |
| 251 | mem = (mz_stream_memcxx . . $ mz_stream_mem# *)*stream; |
| 252 | if (mem) { |
| 253 | if ((mem->modecxx . . $ mz_stream_mem_s#mode. & MZ_OPEN_MODE_CREATEcxx . . $ `mz.h:54:9`!) && (mem->buffercxx . . $ mz_stream_mem_s#buffer.)) |
| 254 | freecxx . . $ free(5d22bdacc48458e8).(mem->buffercxx . . $ mz_stream_mem_s#buffer.); |
| 255 | freecxx . . $ free(5d22bdacc48458e8).(mem); |
| 256 | } |
| 257 | *stream = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 258 | } |
| 259 | |
| 260 | void *mz_stream_mem_get_interfacecxx . . $ mz_stream_mem_get_interface(50c7df7de5344ebb).(void) { |
| 261 | return (void *)&mz_stream_mem_vtblcxx . . $ mz_stream_mem_vtbl.; |
| 262 | } |