Fossil SCM

fossil-scm / compat / zlib / contrib / minizip / ioapi.c
Source Blame History 231 lines
7ef7284… drh 1 /* ioapi.h -- IO base function header for compress/uncompress .zip
6ea30fb… florian 2 part of the MiniZip project - ( https://www.winimage.com/zLibDll/minizip.html )
7ef7284… drh 3
6ea30fb… florian 4 Copyright (C) 1998-2026 Gilles Vollant (minizip) ( https://www.winimage.com/zLibDll/minizip.html )
7ef7284… drh 5
7ef7284… drh 6 Modifications for Zip64 support
6ea30fb… florian 7 Copyright (C) 2009-2010 Mathias Svensson ( https://result42.com )
7ef7284… drh 8
7ef7284… drh 9 For more info read MiniZip_info.txt
7ef7284… drh 10
7ef7284… drh 11 */
7ef7284… drh 12
7ef7284… drh 13 #if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
7ef7284… drh 14 #define _CRT_SECURE_NO_WARNINGS
7ef7284… drh 15 #endif
7ef7284… drh 16
6ea30fb… florian 17 #if defined(__APPLE__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) || (defined(__ANDROID_API__) && __ANDROID_API__ < 24)
6ea30fb… florian 18 /* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */
7ef7284… drh 19 #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
7ef7284… drh 20 #define FTELLO_FUNC(stream) ftello(stream)
7ef7284… drh 21 #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
7ef7284… drh 22 #else
7ef7284… drh 23 #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
7ef7284… drh 24 #define FTELLO_FUNC(stream) ftello64(stream)
7ef7284… drh 25 #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
7ef7284… drh 26 #endif
7ef7284… drh 27
7ef7284… drh 28
7ef7284… drh 29 #include "ioapi.h"
7ef7284… drh 30
f1f1d6c… drh 31 voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc, const void*filename, int mode) {
7ef7284… drh 32 if (pfilefunc->zfile_func64.zopen64_file != NULL)
7ef7284… drh 33 return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
7ef7284… drh 34 else
7ef7284… drh 35 {
7ef7284… drh 36 return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
7ef7284… drh 37 }
7ef7284… drh 38 }
7ef7284… drh 39
f1f1d6c… drh 40 long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin) {
7ef7284… drh 41 if (pfilefunc->zfile_func64.zseek64_file != NULL)
7ef7284… drh 42 return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
7ef7284… drh 43 else
7ef7284… drh 44 {
7ef7284… drh 45 uLong offsetTruncated = (uLong)offset;
7ef7284… drh 46 if (offsetTruncated != offset)
7ef7284… drh 47 return -1;
7ef7284… drh 48 else
7ef7284… drh 49 return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
7ef7284… drh 50 }
7ef7284… drh 51 }
7ef7284… drh 52
f1f1d6c… drh 53 ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc, voidpf filestream) {
7ef7284… drh 54 if (pfilefunc->zfile_func64.zseek64_file != NULL)
7ef7284… drh 55 return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
7ef7284… drh 56 else
7ef7284… drh 57 {
adb9e8e… drh 58 uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
7ef7284… drh 59 if ((tell_uLong) == MAXU32)
7ef7284… drh 60 return (ZPOS64_T)-1;
7ef7284… drh 61 else
7ef7284… drh 62 return tell_uLong;
7ef7284… drh 63 }
7ef7284… drh 64 }
7ef7284… drh 65
f1f1d6c… drh 66 void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32, const zlib_filefunc_def* p_filefunc32) {
7ef7284… drh 67 p_filefunc64_32->zfile_func64.zopen64_file = NULL;
7ef7284… drh 68 p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
7ef7284… drh 69 p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
7ef7284… drh 70 p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
7ef7284… drh 71 p_filefunc64_32->zfile_func64.ztell64_file = NULL;
7ef7284… drh 72 p_filefunc64_32->zfile_func64.zseek64_file = NULL;
7ef7284… drh 73 p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
7ef7284… drh 74 p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
7ef7284… drh 75 p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
7ef7284… drh 76 p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
7ef7284… drh 77 p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
7ef7284… drh 78 }
7ef7284… drh 79
7ef7284… drh 80
7ef7284… drh 81
f1f1d6c… drh 82 static voidpf ZCALLBACK fopen_file_func(voidpf opaque, const char* filename, int mode) {
7ef7284… drh 83 FILE* file = NULL;
7ef7284… drh 84 const char* mode_fopen = NULL;
a9e589c… florian 85 (void)opaque;
7ef7284… drh 86 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
7ef7284… drh 87 mode_fopen = "rb";
7ef7284… drh 88 else
7ef7284… drh 89 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
7ef7284… drh 90 mode_fopen = "r+b";
7ef7284… drh 91 else
7ef7284… drh 92 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
7ef7284… drh 93 mode_fopen = "wb";
7ef7284… drh 94
7ef7284… drh 95 if ((filename!=NULL) && (mode_fopen != NULL))
7ef7284… drh 96 file = fopen(filename, mode_fopen);
7ef7284… drh 97 return file;
7ef7284… drh 98 }
7ef7284… drh 99
f1f1d6c… drh 100 static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode) {
7ef7284… drh 101 FILE* file = NULL;
7ef7284… drh 102 const char* mode_fopen = NULL;
a9e589c… florian 103 (void)opaque;
7ef7284… drh 104 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
7ef7284… drh 105 mode_fopen = "rb";
7ef7284… drh 106 else
7ef7284… drh 107 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
7ef7284… drh 108 mode_fopen = "r+b";
7ef7284… drh 109 else
7ef7284… drh 110 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
7ef7284… drh 111 mode_fopen = "wb";
7ef7284… drh 112
7ef7284… drh 113 if ((filename!=NULL) && (mode_fopen != NULL))
7ef7284… drh 114 file = FOPEN_FUNC((const char*)filename, mode_fopen);
7ef7284… drh 115 return file;
7ef7284… drh 116 }
7ef7284… drh 117
7ef7284… drh 118
f1f1d6c… drh 119 static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size) {
7ef7284… drh 120 uLong ret;
a9e589c… florian 121 (void)opaque;
7ef7284… drh 122 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
7ef7284… drh 123 return ret;
7ef7284… drh 124 }
7ef7284… drh 125
f1f1d6c… drh 126 static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size) {
7ef7284… drh 127 uLong ret;
a9e589c… florian 128 (void)opaque;
7ef7284… drh 129 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
7ef7284… drh 130 return ret;
7ef7284… drh 131 }
7ef7284… drh 132
f1f1d6c… drh 133 static long ZCALLBACK ftell_file_func(voidpf opaque, voidpf stream) {
7ef7284… drh 134 long ret;
a9e589c… florian 135 (void)opaque;
7ef7284… drh 136 ret = ftell((FILE *)stream);
7ef7284… drh 137 return ret;
7ef7284… drh 138 }
7ef7284… drh 139
7ef7284… drh 140
f1f1d6c… drh 141 static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream) {
7ef7284… drh 142 ZPOS64_T ret;
a9e589c… florian 143 (void)opaque;
adb9e8e… drh 144 ret = (ZPOS64_T)FTELLO_FUNC((FILE *)stream);
7ef7284… drh 145 return ret;
7ef7284… drh 146 }
7ef7284… drh 147
f1f1d6c… drh 148 static long ZCALLBACK fseek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin) {
7ef7284… drh 149 int fseek_origin=0;
7ef7284… drh 150 long ret;
a9e589c… florian 151 (void)opaque;
7ef7284… drh 152 switch (origin)
7ef7284… drh 153 {
7ef7284… drh 154 case ZLIB_FILEFUNC_SEEK_CUR :
7ef7284… drh 155 fseek_origin = SEEK_CUR;
7ef7284… drh 156 break;
7ef7284… drh 157 case ZLIB_FILEFUNC_SEEK_END :
7ef7284… drh 158 fseek_origin = SEEK_END;
7ef7284… drh 159 break;
7ef7284… drh 160 case ZLIB_FILEFUNC_SEEK_SET :
7ef7284… drh 161 fseek_origin = SEEK_SET;
7ef7284… drh 162 break;
7ef7284… drh 163 default: return -1;
7ef7284… drh 164 }
7ef7284… drh 165 ret = 0;
adb9e8e… drh 166 if (fseek((FILE *)stream, (long)offset, fseek_origin) != 0)
7ef7284… drh 167 ret = -1;
7ef7284… drh 168 return ret;
7ef7284… drh 169 }
7ef7284… drh 170
f1f1d6c… drh 171 static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) {
7ef7284… drh 172 int fseek_origin=0;
7ef7284… drh 173 long ret;
a9e589c… florian 174 (void)opaque;
7ef7284… drh 175 switch (origin)
7ef7284… drh 176 {
7ef7284… drh 177 case ZLIB_FILEFUNC_SEEK_CUR :
7ef7284… drh 178 fseek_origin = SEEK_CUR;
7ef7284… drh 179 break;
7ef7284… drh 180 case ZLIB_FILEFUNC_SEEK_END :
7ef7284… drh 181 fseek_origin = SEEK_END;
7ef7284… drh 182 break;
7ef7284… drh 183 case ZLIB_FILEFUNC_SEEK_SET :
7ef7284… drh 184 fseek_origin = SEEK_SET;
7ef7284… drh 185 break;
7ef7284… drh 186 default: return -1;
7ef7284… drh 187 }
7ef7284… drh 188 ret = 0;
7ef7284… drh 189
f1f1d6c… drh 190 if(FSEEKO_FUNC((FILE *)stream, (z_off64_t)offset, fseek_origin) != 0)
7ef7284… drh 191 ret = -1;
7ef7284… drh 192
7ef7284… drh 193 return ret;
7ef7284… drh 194 }
7ef7284… drh 195
7ef7284… drh 196
f1f1d6c… drh 197 static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream) {
7ef7284… drh 198 int ret;
a9e589c… florian 199 (void)opaque;
7ef7284… drh 200 ret = fclose((FILE *)stream);
7ef7284… drh 201 return ret;
7ef7284… drh 202 }
7ef7284… drh 203
f1f1d6c… drh 204 static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream) {
7ef7284… drh 205 int ret;
a9e589c… florian 206 (void)opaque;
7ef7284… drh 207 ret = ferror((FILE *)stream);
7ef7284… drh 208 return ret;
7ef7284… drh 209 }
7ef7284… drh 210
f1f1d6c… drh 211 void fill_fopen_filefunc(zlib_filefunc_def* pzlib_filefunc_def) {
7ef7284… drh 212 pzlib_filefunc_def->zopen_file = fopen_file_func;
7ef7284… drh 213 pzlib_filefunc_def->zread_file = fread_file_func;
7ef7284… drh 214 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
7ef7284… drh 215 pzlib_filefunc_def->ztell_file = ftell_file_func;
7ef7284… drh 216 pzlib_filefunc_def->zseek_file = fseek_file_func;
7ef7284… drh 217 pzlib_filefunc_def->zclose_file = fclose_file_func;
7ef7284… drh 218 pzlib_filefunc_def->zerror_file = ferror_file_func;
7ef7284… drh 219 pzlib_filefunc_def->opaque = NULL;
7ef7284… drh 220 }
7ef7284… drh 221
f1f1d6c… drh 222 void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def) {
7ef7284… drh 223 pzlib_filefunc_def->zopen64_file = fopen64_file_func;
7ef7284… drh 224 pzlib_filefunc_def->zread_file = fread_file_func;
7ef7284… drh 225 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
7ef7284… drh 226 pzlib_filefunc_def->ztell64_file = ftell64_file_func;
7ef7284… drh 227 pzlib_filefunc_def->zseek64_file = fseek64_file_func;
7ef7284… drh 228 pzlib_filefunc_def->zclose_file = fclose_file_func;
7ef7284… drh 229 pzlib_filefunc_def->zerror_file = ferror_file_func;
7ef7284… drh 230 pzlib_filefunc_def->opaque = NULL;
7ef7284… drh 231 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button