packvfs

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/zipcat/src/minizip/ioapi.c	Mon Nov 04 06:46:17 2013 +0200
     1.3 @@ -0,0 +1,247 @@
     1.4 +/* ioapi.h -- IO base function header for compress/uncompress .zip
     1.5 +   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     1.6 +
     1.7 +         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     1.8 +
     1.9 +         Modifications for Zip64 support
    1.10 +         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
    1.11 +
    1.12 +         For more info read MiniZip_info.txt
    1.13 +
    1.14 +*/
    1.15 +
    1.16 +#if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
    1.17 +        #define _CRT_SECURE_NO_WARNINGS
    1.18 +#endif
    1.19 +
    1.20 +#if defined(__APPLE__) || defined(IOAPI_NO_64)
    1.21 +// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
    1.22 +#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
    1.23 +#define FTELLO_FUNC(stream) ftello(stream)
    1.24 +#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
    1.25 +#else
    1.26 +#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
    1.27 +#define FTELLO_FUNC(stream) ftello64(stream)
    1.28 +#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
    1.29 +#endif
    1.30 +
    1.31 +
    1.32 +#include "ioapi.h"
    1.33 +
    1.34 +voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
    1.35 +{
    1.36 +    if (pfilefunc->zfile_func64.zopen64_file != NULL)
    1.37 +        return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
    1.38 +    else
    1.39 +    {
    1.40 +        return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
    1.41 +    }
    1.42 +}
    1.43 +
    1.44 +long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
    1.45 +{
    1.46 +    if (pfilefunc->zfile_func64.zseek64_file != NULL)
    1.47 +        return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
    1.48 +    else
    1.49 +    {
    1.50 +        uLong offsetTruncated = (uLong)offset;
    1.51 +        if (offsetTruncated != offset)
    1.52 +            return -1;
    1.53 +        else
    1.54 +            return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
    1.55 +    }
    1.56 +}
    1.57 +
    1.58 +ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
    1.59 +{
    1.60 +    if (pfilefunc->zfile_func64.zseek64_file != NULL)
    1.61 +        return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
    1.62 +    else
    1.63 +    {
    1.64 +        uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
    1.65 +        if ((tell_uLong) == MAXU32)
    1.66 +            return (ZPOS64_T)-1;
    1.67 +        else
    1.68 +            return tell_uLong;
    1.69 +    }
    1.70 +}
    1.71 +
    1.72 +void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
    1.73 +{
    1.74 +    p_filefunc64_32->zfile_func64.zopen64_file = NULL;
    1.75 +    p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
    1.76 +    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
    1.77 +    p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
    1.78 +    p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
    1.79 +    p_filefunc64_32->zfile_func64.ztell64_file = NULL;
    1.80 +    p_filefunc64_32->zfile_func64.zseek64_file = NULL;
    1.81 +    p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
    1.82 +    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
    1.83 +    p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
    1.84 +    p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
    1.85 +    p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
    1.86 +}
    1.87 +
    1.88 +
    1.89 +
    1.90 +static voidpf  ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
    1.91 +static uLong   ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
    1.92 +static uLong   ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
    1.93 +static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
    1.94 +static long    ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
    1.95 +static int     ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
    1.96 +static int     ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
    1.97 +
    1.98 +static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
    1.99 +{
   1.100 +    FILE* file = NULL;
   1.101 +    const char* mode_fopen = NULL;
   1.102 +    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
   1.103 +        mode_fopen = "rb";
   1.104 +    else
   1.105 +    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
   1.106 +        mode_fopen = "r+b";
   1.107 +    else
   1.108 +    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
   1.109 +        mode_fopen = "wb";
   1.110 +
   1.111 +    if ((filename!=NULL) && (mode_fopen != NULL))
   1.112 +        file = fopen(filename, mode_fopen);
   1.113 +    return file;
   1.114 +}
   1.115 +
   1.116 +static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
   1.117 +{
   1.118 +    FILE* file = NULL;
   1.119 +    const char* mode_fopen = NULL;
   1.120 +    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
   1.121 +        mode_fopen = "rb";
   1.122 +    else
   1.123 +    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
   1.124 +        mode_fopen = "r+b";
   1.125 +    else
   1.126 +    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
   1.127 +        mode_fopen = "wb";
   1.128 +
   1.129 +    if ((filename!=NULL) && (mode_fopen != NULL))
   1.130 +        file = FOPEN_FUNC((const char*)filename, mode_fopen);
   1.131 +    return file;
   1.132 +}
   1.133 +
   1.134 +
   1.135 +static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
   1.136 +{
   1.137 +    uLong ret;
   1.138 +    ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
   1.139 +    return ret;
   1.140 +}
   1.141 +
   1.142 +static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
   1.143 +{
   1.144 +    uLong ret;
   1.145 +    ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
   1.146 +    return ret;
   1.147 +}
   1.148 +
   1.149 +static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
   1.150 +{
   1.151 +    long ret;
   1.152 +    ret = ftell((FILE *)stream);
   1.153 +    return ret;
   1.154 +}
   1.155 +
   1.156 +
   1.157 +static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
   1.158 +{
   1.159 +    ZPOS64_T ret;
   1.160 +    ret = FTELLO_FUNC((FILE *)stream);
   1.161 +    return ret;
   1.162 +}
   1.163 +
   1.164 +static long ZCALLBACK fseek_file_func (voidpf  opaque, voidpf stream, uLong offset, int origin)
   1.165 +{
   1.166 +    int fseek_origin=0;
   1.167 +    long ret;
   1.168 +    switch (origin)
   1.169 +    {
   1.170 +    case ZLIB_FILEFUNC_SEEK_CUR :
   1.171 +        fseek_origin = SEEK_CUR;
   1.172 +        break;
   1.173 +    case ZLIB_FILEFUNC_SEEK_END :
   1.174 +        fseek_origin = SEEK_END;
   1.175 +        break;
   1.176 +    case ZLIB_FILEFUNC_SEEK_SET :
   1.177 +        fseek_origin = SEEK_SET;
   1.178 +        break;
   1.179 +    default: return -1;
   1.180 +    }
   1.181 +    ret = 0;
   1.182 +    if (fseek((FILE *)stream, offset, fseek_origin) != 0)
   1.183 +        ret = -1;
   1.184 +    return ret;
   1.185 +}
   1.186 +
   1.187 +static long ZCALLBACK fseek64_file_func (voidpf  opaque, voidpf stream, ZPOS64_T offset, int origin)
   1.188 +{
   1.189 +    int fseek_origin=0;
   1.190 +    long ret;
   1.191 +    switch (origin)
   1.192 +    {
   1.193 +    case ZLIB_FILEFUNC_SEEK_CUR :
   1.194 +        fseek_origin = SEEK_CUR;
   1.195 +        break;
   1.196 +    case ZLIB_FILEFUNC_SEEK_END :
   1.197 +        fseek_origin = SEEK_END;
   1.198 +        break;
   1.199 +    case ZLIB_FILEFUNC_SEEK_SET :
   1.200 +        fseek_origin = SEEK_SET;
   1.201 +        break;
   1.202 +    default: return -1;
   1.203 +    }
   1.204 +    ret = 0;
   1.205 +
   1.206 +    if(FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
   1.207 +                        ret = -1;
   1.208 +
   1.209 +    return ret;
   1.210 +}
   1.211 +
   1.212 +
   1.213 +static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
   1.214 +{
   1.215 +    int ret;
   1.216 +    ret = fclose((FILE *)stream);
   1.217 +    return ret;
   1.218 +}
   1.219 +
   1.220 +static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
   1.221 +{
   1.222 +    int ret;
   1.223 +    ret = ferror((FILE *)stream);
   1.224 +    return ret;
   1.225 +}
   1.226 +
   1.227 +void fill_fopen_filefunc (pzlib_filefunc_def)
   1.228 +  zlib_filefunc_def* pzlib_filefunc_def;
   1.229 +{
   1.230 +    pzlib_filefunc_def->zopen_file = fopen_file_func;
   1.231 +    pzlib_filefunc_def->zread_file = fread_file_func;
   1.232 +    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
   1.233 +    pzlib_filefunc_def->ztell_file = ftell_file_func;
   1.234 +    pzlib_filefunc_def->zseek_file = fseek_file_func;
   1.235 +    pzlib_filefunc_def->zclose_file = fclose_file_func;
   1.236 +    pzlib_filefunc_def->zerror_file = ferror_file_func;
   1.237 +    pzlib_filefunc_def->opaque = NULL;
   1.238 +}
   1.239 +
   1.240 +void fill_fopen64_filefunc (zlib_filefunc64_def*  pzlib_filefunc_def)
   1.241 +{
   1.242 +    pzlib_filefunc_def->zopen64_file = fopen64_file_func;
   1.243 +    pzlib_filefunc_def->zread_file = fread_file_func;
   1.244 +    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
   1.245 +    pzlib_filefunc_def->ztell64_file = ftell64_file_func;
   1.246 +    pzlib_filefunc_def->zseek64_file = fseek64_file_func;
   1.247 +    pzlib_filefunc_def->zclose_file = fclose_file_func;
   1.248 +    pzlib_filefunc_def->zerror_file = ferror_file_func;
   1.249 +    pzlib_filefunc_def->opaque = NULL;
   1.250 +}