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