175fd0b74Schristos /* ioapi.h -- IO base function header for compress/uncompress .zip
275fd0b74Schristos part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
375fd0b74Schristos
475fd0b74Schristos Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
575fd0b74Schristos
675fd0b74Schristos Modifications for Zip64 support
775fd0b74Schristos Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
875fd0b74Schristos
975fd0b74Schristos For more info read MiniZip_info.txt
1075fd0b74Schristos
1175fd0b74Schristos */
1275fd0b74Schristos
1375fd0b74Schristos #if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
1475fd0b74Schristos #define _CRT_SECURE_NO_WARNINGS
1575fd0b74Schristos #endif
1675fd0b74Schristos
1775fd0b74Schristos #if defined(__APPLE__) || defined(IOAPI_NO_64)
1875fd0b74Schristos // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
1975fd0b74Schristos #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
2075fd0b74Schristos #define FTELLO_FUNC(stream) ftello(stream)
2175fd0b74Schristos #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
2275fd0b74Schristos #else
2375fd0b74Schristos #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
2475fd0b74Schristos #define FTELLO_FUNC(stream) ftello64(stream)
2575fd0b74Schristos #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
2675fd0b74Schristos #endif
2775fd0b74Schristos
2875fd0b74Schristos
2975fd0b74Schristos #include "ioapi.h"
3075fd0b74Schristos
call_zopen64(const zlib_filefunc64_32_def * pfilefunc,const void * filename,int mode)3175fd0b74Schristos voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
3275fd0b74Schristos {
3375fd0b74Schristos if (pfilefunc->zfile_func64.zopen64_file != NULL)
3475fd0b74Schristos return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
3575fd0b74Schristos else
3675fd0b74Schristos {
3775fd0b74Schristos return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
3875fd0b74Schristos }
3975fd0b74Schristos }
4075fd0b74Schristos
call_zseek64(const zlib_filefunc64_32_def * pfilefunc,voidpf filestream,ZPOS64_T offset,int origin)4175fd0b74Schristos long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
4275fd0b74Schristos {
4375fd0b74Schristos if (pfilefunc->zfile_func64.zseek64_file != NULL)
4475fd0b74Schristos return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
4575fd0b74Schristos else
4675fd0b74Schristos {
4775fd0b74Schristos uLong offsetTruncated = (uLong)offset;
4875fd0b74Schristos if (offsetTruncated != offset)
4975fd0b74Schristos return -1;
5075fd0b74Schristos else
5175fd0b74Schristos return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
5275fd0b74Schristos }
5375fd0b74Schristos }
5475fd0b74Schristos
call_ztell64(const zlib_filefunc64_32_def * pfilefunc,voidpf filestream)5575fd0b74Schristos ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
5675fd0b74Schristos {
5775fd0b74Schristos if (pfilefunc->zfile_func64.zseek64_file != NULL)
5875fd0b74Schristos return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
5975fd0b74Schristos else
6075fd0b74Schristos {
61*e992f068Schristos uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
6275fd0b74Schristos if ((tell_uLong) == MAXU32)
6375fd0b74Schristos return (ZPOS64_T)-1;
6475fd0b74Schristos else
6575fd0b74Schristos return tell_uLong;
6675fd0b74Schristos }
6775fd0b74Schristos }
6875fd0b74Schristos
fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def * p_filefunc64_32,const zlib_filefunc_def * p_filefunc32)6975fd0b74Schristos void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
7075fd0b74Schristos {
7175fd0b74Schristos p_filefunc64_32->zfile_func64.zopen64_file = NULL;
7275fd0b74Schristos p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
7375fd0b74Schristos p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
7475fd0b74Schristos p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
7575fd0b74Schristos p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
7675fd0b74Schristos p_filefunc64_32->zfile_func64.ztell64_file = NULL;
7775fd0b74Schristos p_filefunc64_32->zfile_func64.zseek64_file = NULL;
7875fd0b74Schristos p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
7975fd0b74Schristos p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
8075fd0b74Schristos p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
8175fd0b74Schristos p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
8275fd0b74Schristos p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
8375fd0b74Schristos }
8475fd0b74Schristos
8575fd0b74Schristos
8675fd0b74Schristos
8775fd0b74Schristos static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
8875fd0b74Schristos static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
8975fd0b74Schristos static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
9075fd0b74Schristos static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
9175fd0b74Schristos static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
9275fd0b74Schristos static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
9375fd0b74Schristos static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
9475fd0b74Schristos
fopen_file_func(voidpf opaque,const char * filename,int mode)9575fd0b74Schristos static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
9675fd0b74Schristos {
97*e992f068Schristos (void)opaque;
9875fd0b74Schristos FILE* file = NULL;
9975fd0b74Schristos const char* mode_fopen = NULL;
10075fd0b74Schristos if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
10175fd0b74Schristos mode_fopen = "rb";
10275fd0b74Schristos else
10375fd0b74Schristos if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
10475fd0b74Schristos mode_fopen = "r+b";
10575fd0b74Schristos else
10675fd0b74Schristos if (mode & ZLIB_FILEFUNC_MODE_CREATE)
10775fd0b74Schristos mode_fopen = "wb";
10875fd0b74Schristos
10975fd0b74Schristos if ((filename!=NULL) && (mode_fopen != NULL))
11075fd0b74Schristos file = fopen(filename, mode_fopen);
11175fd0b74Schristos return file;
11275fd0b74Schristos }
11375fd0b74Schristos
fopen64_file_func(voidpf opaque,const void * filename,int mode)11475fd0b74Schristos static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
11575fd0b74Schristos {
116*e992f068Schristos (void)opaque;
11775fd0b74Schristos FILE* file = NULL;
11875fd0b74Schristos const char* mode_fopen = NULL;
11975fd0b74Schristos if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
12075fd0b74Schristos mode_fopen = "rb";
12175fd0b74Schristos else
12275fd0b74Schristos if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
12375fd0b74Schristos mode_fopen = "r+b";
12475fd0b74Schristos else
12575fd0b74Schristos if (mode & ZLIB_FILEFUNC_MODE_CREATE)
12675fd0b74Schristos mode_fopen = "wb";
12775fd0b74Schristos
12875fd0b74Schristos if ((filename!=NULL) && (mode_fopen != NULL))
12975fd0b74Schristos file = FOPEN_FUNC((const char*)filename, mode_fopen);
13075fd0b74Schristos return file;
13175fd0b74Schristos }
13275fd0b74Schristos
13375fd0b74Schristos
fread_file_func(voidpf opaque,voidpf stream,void * buf,uLong size)13475fd0b74Schristos static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
13575fd0b74Schristos {
136*e992f068Schristos (void)opaque;
13775fd0b74Schristos uLong ret;
13875fd0b74Schristos ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
13975fd0b74Schristos return ret;
14075fd0b74Schristos }
14175fd0b74Schristos
fwrite_file_func(voidpf opaque,voidpf stream,const void * buf,uLong size)14275fd0b74Schristos static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
14375fd0b74Schristos {
144*e992f068Schristos (void)opaque;
14575fd0b74Schristos uLong ret;
14675fd0b74Schristos ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
14775fd0b74Schristos return ret;
14875fd0b74Schristos }
14975fd0b74Schristos
ftell_file_func(voidpf opaque,voidpf stream)15075fd0b74Schristos static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
15175fd0b74Schristos {
152*e992f068Schristos (void)opaque;
15375fd0b74Schristos long ret;
15475fd0b74Schristos ret = ftell((FILE *)stream);
15575fd0b74Schristos return ret;
15675fd0b74Schristos }
15775fd0b74Schristos
15875fd0b74Schristos
ftell64_file_func(voidpf opaque,voidpf stream)15975fd0b74Schristos static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
16075fd0b74Schristos {
161*e992f068Schristos (void)opaque;
16275fd0b74Schristos ZPOS64_T ret;
163*e992f068Schristos ret = (ZPOS64_T)FTELLO_FUNC((FILE *)stream);
16475fd0b74Schristos return ret;
16575fd0b74Schristos }
16675fd0b74Schristos
fseek_file_func(voidpf opaque,voidpf stream,uLong offset,int origin)16775fd0b74Schristos static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
16875fd0b74Schristos {
169*e992f068Schristos (void)opaque;
17075fd0b74Schristos int fseek_origin=0;
17175fd0b74Schristos long ret;
17275fd0b74Schristos switch (origin)
17375fd0b74Schristos {
17475fd0b74Schristos case ZLIB_FILEFUNC_SEEK_CUR :
17575fd0b74Schristos fseek_origin = SEEK_CUR;
17675fd0b74Schristos break;
17775fd0b74Schristos case ZLIB_FILEFUNC_SEEK_END :
17875fd0b74Schristos fseek_origin = SEEK_END;
17975fd0b74Schristos break;
18075fd0b74Schristos case ZLIB_FILEFUNC_SEEK_SET :
18175fd0b74Schristos fseek_origin = SEEK_SET;
18275fd0b74Schristos break;
18375fd0b74Schristos default: return -1;
18475fd0b74Schristos }
18575fd0b74Schristos ret = 0;
186*e992f068Schristos if (fseek((FILE *)stream, (long)offset, fseek_origin) != 0)
18775fd0b74Schristos ret = -1;
18875fd0b74Schristos return ret;
18975fd0b74Schristos }
19075fd0b74Schristos
fseek64_file_func(voidpf opaque,voidpf stream,ZPOS64_T offset,int origin)19175fd0b74Schristos static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
19275fd0b74Schristos {
193*e992f068Schristos (void)opaque;
19475fd0b74Schristos int fseek_origin=0;
19575fd0b74Schristos long ret;
19675fd0b74Schristos switch (origin)
19775fd0b74Schristos {
19875fd0b74Schristos case ZLIB_FILEFUNC_SEEK_CUR :
19975fd0b74Schristos fseek_origin = SEEK_CUR;
20075fd0b74Schristos break;
20175fd0b74Schristos case ZLIB_FILEFUNC_SEEK_END :
20275fd0b74Schristos fseek_origin = SEEK_END;
20375fd0b74Schristos break;
20475fd0b74Schristos case ZLIB_FILEFUNC_SEEK_SET :
20575fd0b74Schristos fseek_origin = SEEK_SET;
20675fd0b74Schristos break;
20775fd0b74Schristos default: return -1;
20875fd0b74Schristos }
20975fd0b74Schristos ret = 0;
21075fd0b74Schristos
211*e992f068Schristos if(FSEEKO_FUNC((FILE *)stream, (long)offset, fseek_origin) != 0)
21275fd0b74Schristos ret = -1;
21375fd0b74Schristos
21475fd0b74Schristos return ret;
21575fd0b74Schristos }
21675fd0b74Schristos
21775fd0b74Schristos
fclose_file_func(voidpf opaque,voidpf stream)21875fd0b74Schristos static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
21975fd0b74Schristos {
220*e992f068Schristos (void)opaque;
22175fd0b74Schristos int ret;
22275fd0b74Schristos ret = fclose((FILE *)stream);
22375fd0b74Schristos return ret;
22475fd0b74Schristos }
22575fd0b74Schristos
ferror_file_func(voidpf opaque,voidpf stream)22675fd0b74Schristos static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
22775fd0b74Schristos {
228*e992f068Schristos (void)opaque;
22975fd0b74Schristos int ret;
23075fd0b74Schristos ret = ferror((FILE *)stream);
23175fd0b74Schristos return ret;
23275fd0b74Schristos }
23375fd0b74Schristos
fill_fopen_filefunc(pzlib_filefunc_def)23475fd0b74Schristos void fill_fopen_filefunc (pzlib_filefunc_def)
23575fd0b74Schristos zlib_filefunc_def* pzlib_filefunc_def;
23675fd0b74Schristos {
23775fd0b74Schristos pzlib_filefunc_def->zopen_file = fopen_file_func;
23875fd0b74Schristos pzlib_filefunc_def->zread_file = fread_file_func;
23975fd0b74Schristos pzlib_filefunc_def->zwrite_file = fwrite_file_func;
24075fd0b74Schristos pzlib_filefunc_def->ztell_file = ftell_file_func;
24175fd0b74Schristos pzlib_filefunc_def->zseek_file = fseek_file_func;
24275fd0b74Schristos pzlib_filefunc_def->zclose_file = fclose_file_func;
24375fd0b74Schristos pzlib_filefunc_def->zerror_file = ferror_file_func;
24475fd0b74Schristos pzlib_filefunc_def->opaque = NULL;
24575fd0b74Schristos }
24675fd0b74Schristos
fill_fopen64_filefunc(zlib_filefunc64_def * pzlib_filefunc_def)24775fd0b74Schristos void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
24875fd0b74Schristos {
24975fd0b74Schristos pzlib_filefunc_def->zopen64_file = fopen64_file_func;
25075fd0b74Schristos pzlib_filefunc_def->zread_file = fread_file_func;
25175fd0b74Schristos pzlib_filefunc_def->zwrite_file = fwrite_file_func;
25275fd0b74Schristos pzlib_filefunc_def->ztell64_file = ftell64_file_func;
25375fd0b74Schristos pzlib_filefunc_def->zseek64_file = fseek64_file_func;
25475fd0b74Schristos pzlib_filefunc_def->zclose_file = fclose_file_func;
25575fd0b74Schristos pzlib_filefunc_def->zerror_file = ferror_file_func;
25675fd0b74Schristos pzlib_filefunc_def->opaque = NULL;
25775fd0b74Schristos }
258