packio-simple

annotate src/minizip/ioapi.c @ 2:4767e7769c32

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