annotate src/zlib-1.2.8/contrib/minizip/ioapi.c @ 128:5b4145a0d408

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