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