| 1 | /* mz_strm_posix.c -- Stream for filesystem access for posix/linux |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | Copyright (C) Nathan Moinvaziri |
| 5 | https://github.com/zlib-ng/minizip-ng |
| 6 | Modifications for Zip64 support |
| 7 | Copyright (C) 2009-2010 Mathias Svensson |
| 8 | http://result42.com |
| 9 | Copyright (C) 1998-2010 Gilles Vollant |
| 10 | https://www.winimage.com/zLibDll/minizip.html |
| 11 | |
| 12 | This program is distributed under the terms of the same license as zlib. |
| 13 | See the accompanying LICENSE file for the full text of the license. |
| 14 | */ |
| 15 | |
| 16 | #include "mz.h"cxx . . $ `<file>/mz.h`/ |
| 17 | #include "mz_strm.h"cxx . . $ `<file>/mz_strm.h`/ |
| 18 | #include "mz_strm_os.h"cxx . . $ `<file>/mz_strm_os.h`/ |
| 19 | |
| 20 | #include <stdio.h> /* fopen, fread.. */ |
| 21 | #include <errno.h> |
| 22 | |
| 23 | /***************************************************************************/ |
| 24 | |
| 25 | #define fopen64cxx . . $ `mz_strm_os_posix.c:25:9`! fopen |
| 26 | #ifndef MZ_FILE32_API |
| 27 | # ifndef NO_FSEEKO |
| 28 | # define ftello64cxx . . $ `mz_strm_os_posix.c:28:13`! ftello |
| 29 | # define fseeko64cxx . . $ `mz_strm_os_posix.c:29:13`! fseeko |
| 30 | # elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 31 | # define ftello64 _ftelli64 |
| 32 | # define fseeko64 _fseeki64 |
| 33 | # endif |
| 34 | #endif |
| 35 | #ifndef ftello64cxx . . $ `mz_strm_os_posix.c:28:13`! |
| 36 | # define ftello64 ftell |
| 37 | #endif |
| 38 | #ifndef fseeko64cxx . . $ `mz_strm_os_posix.c:29:13`! |
| 39 | # define fseeko64 fseek |
| 40 | #endif |
| 41 | |
| 42 | /***************************************************************************/ |
| 43 | |
| 44 | static mz_stream_vtblcxx . . $ mz_stream_vtbl# mz_stream_os_vtblcxx . . $ mz_stream_os_vtbl. = {mz_stream_os_opencxx . . $ mz_stream_os_open(7083a8984c0735e8)., |
| 45 | mz_stream_os_is_opencxx . . $ mz_stream_os_is_open(dedf1e9b1c614a07)., |
| 46 | mz_stream_os_readcxx . . $ mz_stream_os_read(8bc41909eac9e5d8)., |
| 47 | mz_stream_os_writecxx . . $ mz_stream_os_write(91e54fd90adbb21)., |
| 48 | mz_stream_os_tellcxx . . $ mz_stream_os_tell(72c1818a5177f981)., |
| 49 | mz_stream_os_seekcxx . . $ mz_stream_os_seek(8bc0ff128761c75d)., |
| 50 | mz_stream_os_closecxx . . $ mz_stream_os_close(dedf1e9b1c614a07)., |
| 51 | mz_stream_os_errorcxx . . $ mz_stream_os_error(dedf1e9b1c614a07)., |
| 52 | mz_stream_os_createcxx . . $ mz_stream_os_create(50c7df7de5344ebb)., |
| 53 | mz_stream_os_deletecxx . . $ mz_stream_os_delete(539e082cf00e1f43)., |
| 54 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 55 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!}; |
| 56 | |
| 57 | /***************************************************************************/ |
| 58 | |
| 59 | typedef struct mz_stream_posix_scxx . . $ mz_stream_posix_s# { |
| 60 | mz_streamcxx . . $ mz_stream# streamcxx . . $ mz_stream_posix_s#stream.; |
| 61 | int32_tcxx . . $ int32_t# errorcxx . . $ mz_stream_posix_s#error.; |
| 62 | FILEcxx . . $ FILE# *handlecxx . . $ mz_stream_posix_s#handle.; |
| 63 | } mz_stream_posixcxx . . $ mz_stream_posix#; |
| 64 | |
| 65 | /***************************************************************************/ |
| 66 | |
| 67 | int32_tcxx . . $ int32_t# mz_stream_os_opencxx . . $ mz_stream_os_open(7083a8984c0735e8).(void *stream, const char *path, int32_tcxx . . $ int32_t# mode) { |
| 68 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 69 | const char *mode_fopen = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 70 | |
| 71 | if (!path) |
| 72 | return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!; |
| 73 | |
| 74 | if ((mode & MZ_OPEN_MODE_READWRITEcxx . . $ `mz.h:52:9`!) == MZ_OPEN_MODE_READcxx . . $ `mz.h:50:9`!) |
| 75 | mode_fopen = "rb"; |
| 76 | else if (mode & MZ_OPEN_MODE_APPENDcxx . . $ `mz.h:53:9`!) |
| 77 | mode_fopen = "r+b"; |
| 78 | else if (mode & MZ_OPEN_MODE_CREATEcxx . . $ `mz.h:54:9`!) |
| 79 | mode_fopen = "wb"; |
| 80 | else |
| 81 | return MZ_OPEN_ERRORcxx . . $ `mz.h:40:9`!; |
| 82 | |
| 83 | posix->handlecxx . . $ mz_stream_posix_s#handle. = fopen64cxx . . $ `mz_strm_os_posix.c:25:9`!(path, mode_fopen); |
| 84 | if (!posix->handlecxx . . $ mz_stream_posix_s#handle.) { |
| 85 | posix->errorcxx . . $ mz_stream_posix_s#error. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 86 | return MZ_OPEN_ERRORcxx . . $ `mz.h:40:9`!; |
| 87 | } |
| 88 | |
| 89 | if (mode & MZ_OPEN_MODE_APPENDcxx . . $ `mz.h:53:9`!) |
| 90 | return mz_stream_os_seekcxx . . $ mz_stream_os_seek(8bc0ff128761c75d).(stream, 0, MZ_SEEK_ENDcxx . . $ `mz.h:60:9`!); |
| 91 | |
| 92 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 93 | } |
| 94 | |
| 95 | int32_tcxx . . $ int32_t# mz_stream_os_is_opencxx . . $ mz_stream_os_is_open(dedf1e9b1c614a07).(void *stream) { |
| 96 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 97 | if (!posix->handlecxx . . $ mz_stream_posix_s#handle.) |
| 98 | return MZ_OPEN_ERRORcxx . . $ `mz.h:40:9`!; |
| 99 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 100 | } |
| 101 | |
| 102 | int32_tcxx . . $ int32_t# mz_stream_os_readcxx . . $ mz_stream_os_read(8bc41909eac9e5d8).(void *stream, void *buf, int32_tcxx . . $ int32_t# size) { |
| 103 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 104 | int32_tcxx . . $ int32_t# read = (int32_tcxx . . $ int32_t#)freadcxx . . $ fread(738fc8ac1d711e58).(buf, 1, (size_t)size, posix->handlecxx . . $ mz_stream_posix_s#handle.); |
| 105 | if (read < size && ferrorcxx . . $ ferror(89bc1efcdac0ca22).(posix->handlecxx . . $ mz_stream_posix_s#handle.)) { |
| 106 | posix->errorcxx . . $ mz_stream_posix_s#error. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 107 | return MZ_READ_ERRORcxx . . $ `mz.h:44:9`!; |
| 108 | } |
| 109 | return read; |
| 110 | } |
| 111 | |
| 112 | int32_tcxx . . $ int32_t# mz_stream_os_writecxx . . $ mz_stream_os_write(91e54fd90adbb21).(void *stream, const void *buf, int32_tcxx . . $ int32_t# size) { |
| 113 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 114 | int32_tcxx . . $ int32_t# written = (int32_tcxx . . $ int32_t#)fwritecxx . . $ fwrite(ab0609fa07c47d6e).(buf, 1, (size_t)size, posix->handlecxx . . $ mz_stream_posix_s#handle.); |
| 115 | if (written < size && ferrorcxx . . $ ferror(89bc1efcdac0ca22).(posix->handlecxx . . $ mz_stream_posix_s#handle.)) { |
| 116 | posix->errorcxx . . $ mz_stream_posix_s#error. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 117 | return MZ_WRITE_ERRORcxx . . $ `mz.h:45:9`!; |
| 118 | } |
| 119 | return written; |
| 120 | } |
| 121 | |
| 122 | int64_tcxx . . $ int64_t# mz_stream_os_tellcxx . . $ mz_stream_os_tell(72c1818a5177f981).(void *stream) { |
| 123 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 124 | int64_tcxx . . $ int64_t# position = ftello64cxx . . $ `mz_strm_os_posix.c:28:13`!(posix->handlecxx . . $ mz_stream_posix_s#handle.); |
| 125 | if (position == -1) { |
| 126 | posix->errorcxx . . $ mz_stream_posix_s#error. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 127 | return MZ_TELL_ERRORcxx . . $ `mz.h:43:9`!; |
| 128 | } |
| 129 | return position; |
| 130 | } |
| 131 | |
| 132 | int32_tcxx . . $ int32_t# mz_stream_os_seekcxx . . $ mz_stream_os_seek(8bc0ff128761c75d).(void *stream, int64_tcxx . . $ int64_t# offset, int32_tcxx . . $ int32_t# origin) { |
| 133 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 134 | int32_tcxx . . $ int32_t# fseek_origin = 0; |
| 135 | |
| 136 | switch (origin) { |
| 137 | case MZ_SEEK_CURcxx . . $ `mz.h:59:9`!: |
| 138 | fseek_origin = SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!; |
| 139 | break; |
| 140 | case MZ_SEEK_ENDcxx . . $ `mz.h:60:9`!: |
| 141 | fseek_origin = SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!; |
| 142 | break; |
| 143 | case MZ_SEEK_SETcxx . . $ `mz.h:58:9`!: |
| 144 | fseek_origin = SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!; |
| 145 | break; |
| 146 | default: |
| 147 | return MZ_SEEK_ERRORcxx . . $ `mz.h:42:9`!; |
| 148 | } |
| 149 | |
| 150 | if (fseeko64cxx . . $ `mz_strm_os_posix.c:29:13`!(posix->handlecxx . . $ mz_stream_posix_s#handle., offset, fseek_origin) != 0) { |
| 151 | posix->errorcxx . . $ mz_stream_posix_s#error. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 152 | return MZ_SEEK_ERRORcxx . . $ `mz.h:42:9`!; |
| 153 | } |
| 154 | |
| 155 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 156 | } |
| 157 | |
| 158 | int32_tcxx . . $ int32_t# mz_stream_os_closecxx . . $ mz_stream_os_close(dedf1e9b1c614a07).(void *stream) { |
| 159 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 160 | int32_tcxx . . $ int32_t# closed = 0; |
| 161 | if (posix->handlecxx . . $ mz_stream_posix_s#handle.) { |
| 162 | closed = fclosecxx . . $ fclose(89bc1efcdac0ca22).(posix->handlecxx . . $ mz_stream_posix_s#handle.); |
| 163 | posix->handlecxx . . $ mz_stream_posix_s#handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 164 | } |
| 165 | if (closed != 0) { |
| 166 | posix->errorcxx . . $ mz_stream_posix_s#error. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 167 | return MZ_CLOSE_ERRORcxx . . $ `mz.h:41:9`!; |
| 168 | } |
| 169 | return MZ_OKcxx . . $ `mz.h:21:9`!; |
| 170 | } |
| 171 | |
| 172 | int32_tcxx . . $ int32_t# mz_stream_os_errorcxx . . $ mz_stream_os_error(dedf1e9b1c614a07).(void *stream) { |
| 173 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)stream; |
| 174 | return posix->errorcxx . . $ mz_stream_posix_s#error.; |
| 175 | } |
| 176 | |
| 177 | void *mz_stream_os_createcxx . . $ mz_stream_os_create(50c7df7de5344ebb).(void) { |
| 178 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(mz_stream_posixcxx . . $ mz_stream_posix#)); |
| 179 | if (posix) |
| 180 | posix->streamcxx . . $ mz_stream_posix_s#stream..vtblcxx . . $ mz_stream_s#vtbl. = &mz_stream_os_vtblcxx . . $ mz_stream_os_vtbl.; |
| 181 | return posix; |
| 182 | } |
| 183 | |
| 184 | void mz_stream_os_deletecxx . . $ mz_stream_os_delete(539e082cf00e1f43).(void **stream) { |
| 185 | mz_stream_posixcxx . . $ mz_stream_posix# *posix = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 186 | if (!stream) |
| 187 | return; |
| 188 | posix = (mz_stream_posixcxx . . $ mz_stream_posix# *)*stream; |
| 189 | freecxx . . $ free(5d22bdacc48458e8).(posix); |
| 190 | *stream = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 191 | } |
| 192 | |
| 193 | void *mz_stream_os_get_interfacecxx . . $ mz_stream_os_get_interface(50c7df7de5344ebb).(void) { |
| 194 | return (void *)&mz_stream_os_vtblcxx . . $ mz_stream_os_vtbl.; |
| 195 | } |