ioapi.c 8.2 KB
Newer Older
M
Mark Adler 已提交
1 2
/* ioapi.h -- IO base function header for compress/uncompress .zip
   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
M
Mark Adler 已提交
3

M
Mark Adler 已提交
4
         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
M
Mark Adler 已提交
5

M
Mark Adler 已提交
6 7
         Modifications for Zip64 support
         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
M
Mark Adler 已提交
8

M
Mark Adler 已提交
9
         For more info read MiniZip_info.txt
M
Mark Adler 已提交
10

M
Mark Adler 已提交
11
*/
M
Mark Adler 已提交
12

13
#if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
M
Mark Adler 已提交
14
        #define _CRT_SECURE_NO_WARNINGS
M
Mark Adler 已提交
15 16
#endif

17
#if defined(__APPLE__) || defined(IOAPI_NO_64)
M
Mark Adler 已提交
18 19 20 21 22 23 24 25 26 27 28
// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
#define FTELLO_FUNC(stream) ftello(stream)
#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
#else
#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
#define FTELLO_FUNC(stream) ftello64(stream)
#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
#endif


M
Mark Adler 已提交
29
#include "ioapi.h"
M
Mark Adler 已提交
30

M
Mark Adler 已提交
31 32 33 34 35 36 37 38 39
voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
{
    if (pfilefunc->zfile_func64.zopen64_file != NULL)
        return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
    else
    {
        return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
    }
}
M
Mark Adler 已提交
40

M
Mark Adler 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
{
    if (pfilefunc->zfile_func64.zseek64_file != NULL)
        return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
    else
    {
        uLong offsetTruncated = (uLong)offset;
        if (offsetTruncated != offset)
            return -1;
        else
            return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
    }
}
M
Mark Adler 已提交
54

M
Mark Adler 已提交
55 56 57 58 59 60
ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
{
    if (pfilefunc->zfile_func64.zseek64_file != NULL)
        return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
    else
    {
S
shuaiyutao 已提交
61
        uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
62
        if ((tell_uLong) == MAXU32)
M
Mark Adler 已提交
63 64 65 66 67
            return (ZPOS64_T)-1;
        else
            return tell_uLong;
    }
}
M
Mark Adler 已提交
68

M
Mark Adler 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
{
    p_filefunc64_32->zfile_func64.zopen64_file = NULL;
    p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
    p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
    p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
    p_filefunc64_32->zfile_func64.ztell64_file = NULL;
    p_filefunc64_32->zfile_func64.zseek64_file = NULL;
    p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
    p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
    p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
    p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
}
M
Mark Adler 已提交
84 85 86



M
Mark Adler 已提交
87 88 89 90 91 92 93
static voidpf  ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
static uLong   ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
static uLong   ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
static long    ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
static int     ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
static int     ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
M
Mark Adler 已提交
94

M
Mark Adler 已提交
95 96
static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
{
S
shuaiyutao 已提交
97
    (void)opaque;
M
Mark Adler 已提交
98 99 100 101 102 103 104 105 106 107
    FILE* file = NULL;
    const char* mode_fopen = NULL;
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
        mode_fopen = "rb";
    else
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
        mode_fopen = "r+b";
    else
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
        mode_fopen = "wb";
M
Mark Adler 已提交
108

M
Mark Adler 已提交
109 110 111 112
    if ((filename!=NULL) && (mode_fopen != NULL))
        file = fopen(filename, mode_fopen);
    return file;
}
M
Mark Adler 已提交
113

M
Mark Adler 已提交
114
static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
M
Mark Adler 已提交
115
{
S
shuaiyutao 已提交
116
    (void)opaque;
M
Mark Adler 已提交
117 118 119 120 121 122 123 124 125 126 127 128
    FILE* file = NULL;
    const char* mode_fopen = NULL;
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
        mode_fopen = "rb";
    else
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
        mode_fopen = "r+b";
    else
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
        mode_fopen = "wb";

    if ((filename!=NULL) && (mode_fopen != NULL))
M
Mark Adler 已提交
129
        file = FOPEN_FUNC((const char*)filename, mode_fopen);
M
Mark Adler 已提交
130 131 132 133
    return file;
}


M
Mark Adler 已提交
134
static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
M
Mark Adler 已提交
135
{
S
shuaiyutao 已提交
136
    (void)opaque;
M
Mark Adler 已提交
137 138 139 140 141
    uLong ret;
    ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
    return ret;
}

M
Mark Adler 已提交
142
static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
M
Mark Adler 已提交
143
{
S
shuaiyutao 已提交
144
    (void)opaque;
M
Mark Adler 已提交
145 146 147 148 149
    uLong ret;
    ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
    return ret;
}

M
Mark Adler 已提交
150
static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
M
Mark Adler 已提交
151
{
S
shuaiyutao 已提交
152
    (void)opaque;
M
Mark Adler 已提交
153 154 155 156 157
    long ret;
    ret = ftell((FILE *)stream);
    return ret;
}

M
Mark Adler 已提交
158 159 160

static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
{
S
shuaiyutao 已提交
161
    (void)opaque;
M
Mark Adler 已提交
162
    ZPOS64_T ret;
S
shuaiyutao 已提交
163
    ret = (ZPOS64_T)FTELLO_FUNC((FILE *)stream);
M
Mark Adler 已提交
164 165 166 167
    return ret;
}

static long ZCALLBACK fseek_file_func (voidpf  opaque, voidpf stream, uLong offset, int origin)
M
Mark Adler 已提交
168
{
S
shuaiyutao 已提交
169
    (void)opaque;
M
Mark Adler 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    int fseek_origin=0;
    long ret;
    switch (origin)
    {
    case ZLIB_FILEFUNC_SEEK_CUR :
        fseek_origin = SEEK_CUR;
        break;
    case ZLIB_FILEFUNC_SEEK_END :
        fseek_origin = SEEK_END;
        break;
    case ZLIB_FILEFUNC_SEEK_SET :
        fseek_origin = SEEK_SET;
        break;
    default: return -1;
    }
    ret = 0;
S
shuaiyutao 已提交
186
    if (fseek((FILE *)stream, (long)offset, fseek_origin) != 0)
M
Mark Adler 已提交
187
        ret = -1;
M
Mark Adler 已提交
188 189 190
    return ret;
}

M
Mark Adler 已提交
191 192
static long ZCALLBACK fseek64_file_func (voidpf  opaque, voidpf stream, ZPOS64_T offset, int origin)
{
S
shuaiyutao 已提交
193
    (void)opaque;
M
Mark Adler 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    int fseek_origin=0;
    long ret;
    switch (origin)
    {
    case ZLIB_FILEFUNC_SEEK_CUR :
        fseek_origin = SEEK_CUR;
        break;
    case ZLIB_FILEFUNC_SEEK_END :
        fseek_origin = SEEK_END;
        break;
    case ZLIB_FILEFUNC_SEEK_SET :
        fseek_origin = SEEK_SET;
        break;
    default: return -1;
    }
    ret = 0;

S
shuaiyutao 已提交
211
    if(FSEEKO_FUNC((FILE *)stream, (long)offset, fseek_origin) != 0)
M
Mark Adler 已提交
212
                        ret = -1;
M
Mark Adler 已提交
213 214 215 216 217 218

    return ret;
}


static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
M
Mark Adler 已提交
219
{
S
shuaiyutao 已提交
220
    (void)opaque;
M
Mark Adler 已提交
221 222 223 224 225
    int ret;
    ret = fclose((FILE *)stream);
    return ret;
}

M
Mark Adler 已提交
226
static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
M
Mark Adler 已提交
227
{
S
shuaiyutao 已提交
228
    (void)opaque;
M
Mark Adler 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    int ret;
    ret = ferror((FILE *)stream);
    return ret;
}

void fill_fopen_filefunc (pzlib_filefunc_def)
  zlib_filefunc_def* pzlib_filefunc_def;
{
    pzlib_filefunc_def->zopen_file = fopen_file_func;
    pzlib_filefunc_def->zread_file = fread_file_func;
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
    pzlib_filefunc_def->ztell_file = ftell_file_func;
    pzlib_filefunc_def->zseek_file = fseek_file_func;
    pzlib_filefunc_def->zclose_file = fclose_file_func;
    pzlib_filefunc_def->zerror_file = ferror_file_func;
    pzlib_filefunc_def->opaque = NULL;
}
M
Mark Adler 已提交
246 247 248 249 250 251 252 253 254 255 256 257

void fill_fopen64_filefunc (zlib_filefunc64_def*  pzlib_filefunc_def)
{
    pzlib_filefunc_def->zopen64_file = fopen64_file_func;
    pzlib_filefunc_def->zread_file = fread_file_func;
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
    pzlib_filefunc_def->ztell64_file = ftell64_file_func;
    pzlib_filefunc_def->zseek64_file = fseek64_file_func;
    pzlib_filefunc_def->zclose_file = fclose_file_func;
    pzlib_filefunc_def->zerror_file = ferror_file_func;
    pzlib_filefunc_def->opaque = NULL;
}