/src/httrack/src/minizip/ioapi.c
Line | Count | Source |
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(__ANDROID__) || 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 | 0 | #define FOPEN_FUNC(filename, mode) fopen64(filename, mode) |
24 | 0 | #define FTELLO_FUNC(stream) ftello64(stream) |
25 | 0 | #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin) |
26 | | #endif |
27 | | |
28 | | |
29 | | #include "ioapi.h" |
30 | | |
31 | | #if defined(_WIN32) || defined(WIN32) |
32 | | #include <io.h> /* _chsize_s, _fileno */ |
33 | | #else |
34 | | #include <unistd.h> /* ftruncate */ |
35 | | #endif |
36 | | |
37 | 0 | voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc, const void*filename, int mode) { |
38 | 0 | if (pfilefunc->zfile_func64.zopen64_file != NULL) |
39 | 0 | return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode); |
40 | 0 | else |
41 | 0 | { |
42 | 0 | return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | 0 | long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin) { |
47 | 0 | if (pfilefunc->zfile_func64.zseek64_file != NULL) |
48 | 0 | return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin); |
49 | 0 | else |
50 | 0 | { |
51 | 0 | uLong offsetTruncated = (uLong)offset; |
52 | 0 | if (offsetTruncated != offset) |
53 | 0 | return -1; |
54 | 0 | else |
55 | 0 | return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | 0 | ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc, voidpf filestream) { |
60 | 0 | if (pfilefunc->zfile_func64.zseek64_file != NULL) |
61 | 0 | return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream); |
62 | 0 | else |
63 | 0 | { |
64 | 0 | uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream); |
65 | 0 | if ((tell_uLong) == MAXU32) |
66 | 0 | return (ZPOS64_T)-1; |
67 | 0 | else |
68 | 0 | return tell_uLong; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | int call_ztruncate64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, |
73 | 0 | ZPOS64_T size) { |
74 | | /* a backend with no truncate keeps its tail: better than failing a rollback |
75 | | the caller has already performed, but the caller has to hear about it */ |
76 | 0 | if (pfilefunc->zfile_func64.ztruncate64_file == NULL) |
77 | 0 | return 1; |
78 | | /* a backend may report failure as an errno rather than -1 (_chsize_s) */ |
79 | 0 | return (*(pfilefunc->zfile_func64.ztruncate64_file))( |
80 | 0 | pfilefunc->zfile_func64.opaque, filestream, size) == 0 |
81 | 0 | ? 0 |
82 | 0 | : -1; |
83 | 0 | } |
84 | | |
85 | 0 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32, const zlib_filefunc_def* p_filefunc32) { |
86 | 0 | p_filefunc64_32->zfile_func64.zopen64_file = NULL; |
87 | 0 | p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file; |
88 | 0 | p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file; |
89 | 0 | p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file; |
90 | 0 | p_filefunc64_32->zfile_func64.zflush_file = p_filefunc32->zflush_file; |
91 | 0 | p_filefunc64_32->zfile_func64.ztell64_file = NULL; |
92 | 0 | p_filefunc64_32->zfile_func64.zseek64_file = NULL; |
93 | 0 | p_filefunc64_32->zfile_func64.ztruncate64_file = |
94 | 0 | NULL; /* no 32-bit counterpart */ |
95 | 0 | p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file; |
96 | 0 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file; |
97 | 0 | p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque; |
98 | 0 | p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file; |
99 | 0 | p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file; |
100 | 0 | } |
101 | | |
102 | | |
103 | | |
104 | 0 | static voidpf ZCALLBACK fopen_file_func(voidpf opaque, const char* filename, int mode) { |
105 | 0 | FILE* file = NULL; |
106 | 0 | const char* mode_fopen = NULL; |
107 | 0 | (void)opaque; |
108 | 0 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) |
109 | 0 | mode_fopen = "rb"; |
110 | 0 | else |
111 | 0 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) |
112 | 0 | mode_fopen = "r+b"; |
113 | 0 | else |
114 | 0 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) |
115 | 0 | mode_fopen = "wb"; |
116 | |
|
117 | 0 | if ((filename!=NULL) && (mode_fopen != NULL)) |
118 | 0 | file = fopen(filename, mode_fopen); |
119 | 0 | return file; |
120 | 0 | } |
121 | | |
122 | 0 | static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode) { |
123 | 0 | FILE* file = NULL; |
124 | 0 | const char* mode_fopen = NULL; |
125 | 0 | (void)opaque; |
126 | 0 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) |
127 | 0 | mode_fopen = "rb"; |
128 | 0 | else |
129 | 0 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) |
130 | 0 | mode_fopen = "r+b"; |
131 | 0 | else |
132 | 0 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) |
133 | 0 | mode_fopen = "wb"; |
134 | |
|
135 | 0 | if ((filename!=NULL) && (mode_fopen != NULL)) |
136 | 0 | file = FOPEN_FUNC((const char*)filename, mode_fopen); |
137 | 0 | return file; |
138 | 0 | } |
139 | | |
140 | | |
141 | 0 | static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size) { |
142 | 0 | uLong ret; |
143 | 0 | (void)opaque; |
144 | 0 | ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); |
145 | 0 | return ret; |
146 | 0 | } |
147 | | |
148 | 0 | static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size) { |
149 | 0 | uLong ret; |
150 | 0 | (void)opaque; |
151 | 0 | ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); |
152 | 0 | return ret; |
153 | 0 | } |
154 | | |
155 | 0 | static long ZCALLBACK ftell_file_func(voidpf opaque, voidpf stream) { |
156 | 0 | long ret; |
157 | 0 | (void)opaque; |
158 | 0 | ret = ftell((FILE *)stream); |
159 | 0 | return ret; |
160 | 0 | } |
161 | | |
162 | | |
163 | 0 | static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream) { |
164 | 0 | ZPOS64_T ret; |
165 | 0 | (void)opaque; |
166 | 0 | ret = (ZPOS64_T)FTELLO_FUNC((FILE *)stream); |
167 | 0 | return ret; |
168 | 0 | } |
169 | | |
170 | 0 | static long ZCALLBACK fseek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin) { |
171 | 0 | int fseek_origin=0; |
172 | 0 | long ret; |
173 | 0 | (void)opaque; |
174 | 0 | switch (origin) |
175 | 0 | { |
176 | 0 | case ZLIB_FILEFUNC_SEEK_CUR : |
177 | 0 | fseek_origin = SEEK_CUR; |
178 | 0 | break; |
179 | 0 | case ZLIB_FILEFUNC_SEEK_END : |
180 | 0 | fseek_origin = SEEK_END; |
181 | 0 | break; |
182 | 0 | case ZLIB_FILEFUNC_SEEK_SET : |
183 | 0 | fseek_origin = SEEK_SET; |
184 | 0 | break; |
185 | 0 | default: return -1; |
186 | 0 | } |
187 | 0 | ret = 0; |
188 | 0 | if (fseek((FILE *)stream, (long)offset, fseek_origin) != 0) |
189 | 0 | ret = -1; |
190 | 0 | return ret; |
191 | 0 | } |
192 | | |
193 | 0 | static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) { |
194 | 0 | int fseek_origin=0; |
195 | 0 | long ret; |
196 | 0 | (void)opaque; |
197 | 0 | switch (origin) |
198 | 0 | { |
199 | 0 | case ZLIB_FILEFUNC_SEEK_CUR : |
200 | 0 | fseek_origin = SEEK_CUR; |
201 | 0 | break; |
202 | 0 | case ZLIB_FILEFUNC_SEEK_END : |
203 | 0 | fseek_origin = SEEK_END; |
204 | 0 | break; |
205 | 0 | case ZLIB_FILEFUNC_SEEK_SET : |
206 | 0 | fseek_origin = SEEK_SET; |
207 | 0 | break; |
208 | 0 | default: return -1; |
209 | 0 | } |
210 | 0 | ret = 0; |
211 | |
|
212 | 0 | if(FSEEKO_FUNC((FILE *)stream, (z_off64_t)offset, fseek_origin) != 0) |
213 | 0 | ret = -1; |
214 | |
|
215 | 0 | return ret; |
216 | 0 | } |
217 | | |
218 | | static int ZCALLBACK fflush_file_func (voidpf opaque, voidpf stream) |
219 | 0 | { |
220 | 0 | int ret; |
221 | 0 | ret = fflush((FILE *)stream); |
222 | 0 | return ret; |
223 | 0 | } |
224 | | |
225 | | /* Shorten the file to `size`, dropping what a rolled-back member flushed. */ |
226 | | static int ZCALLBACK ftruncate64_file_func(voidpf opaque, voidpf stream, |
227 | 0 | ZPOS64_T size) { |
228 | 0 | FILE *file = (FILE *) stream; |
229 | 0 | (void) opaque; |
230 | | /* buffered bytes would otherwise be written back past the new end */ |
231 | 0 | if (fflush(file) != 0) |
232 | 0 | return -1; |
233 | | #if defined(_WIN32) || defined(WIN32) |
234 | | return _chsize_s(_fileno(file), (__int64) size); |
235 | | #else |
236 | 0 | return ftruncate(fileno(file), (off_t) size); |
237 | 0 | #endif |
238 | 0 | } |
239 | | |
240 | 0 | static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream) { |
241 | 0 | int ret; |
242 | 0 | (void)opaque; |
243 | 0 | ret = fclose((FILE *)stream); |
244 | 0 | return ret; |
245 | 0 | } |
246 | | |
247 | 0 | static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream) { |
248 | 0 | int ret; |
249 | 0 | (void)opaque; |
250 | 0 | ret = ferror((FILE *)stream); |
251 | 0 | return ret; |
252 | 0 | } |
253 | | |
254 | 0 | void fill_fopen_filefunc(zlib_filefunc_def* pzlib_filefunc_def) { |
255 | 0 | pzlib_filefunc_def->zopen_file = fopen_file_func; |
256 | 0 | pzlib_filefunc_def->zread_file = fread_file_func; |
257 | 0 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; |
258 | 0 | pzlib_filefunc_def->ztell_file = ftell_file_func; |
259 | 0 | pzlib_filefunc_def->zseek_file = fseek_file_func; |
260 | 0 | pzlib_filefunc_def->zflush_file = fflush_file_func; |
261 | 0 | pzlib_filefunc_def->zclose_file = fclose_file_func; |
262 | 0 | pzlib_filefunc_def->zerror_file = ferror_file_func; |
263 | 0 | pzlib_filefunc_def->opaque = NULL; |
264 | 0 | } |
265 | | |
266 | 0 | void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def) { |
267 | 0 | pzlib_filefunc_def->zopen64_file = fopen64_file_func; |
268 | 0 | pzlib_filefunc_def->zread_file = fread_file_func; |
269 | 0 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; |
270 | 0 | pzlib_filefunc_def->ztell64_file = ftell64_file_func; |
271 | 0 | pzlib_filefunc_def->zseek64_file = fseek64_file_func; |
272 | 0 | pzlib_filefunc_def->ztruncate64_file = ftruncate64_file_func; |
273 | 0 | pzlib_filefunc_def->zflush_file = fflush_file_func; |
274 | 0 | pzlib_filefunc_def->zclose_file = fclose_file_func; |
275 | 0 | pzlib_filefunc_def->zerror_file = ferror_file_func; |
276 | | pzlib_filefunc_def->opaque = NULL; |
277 | 0 | } |