PageRenderTime 45ms CodeModel.GetById 15ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/minizip/ioapi.c

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