packvfs

annotate test/zipcat/src/minizip/ioapi.c @ 3:ef6c1472607f

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