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