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