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