fw4spl
ioapi.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 /* ioapi.h -- IO base function header for compress/uncompress .zip
8  part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
9 
10  Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
11 
12  Modifications for Zip64 support
13  Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
14 
15  For more info read MiniZip_info.txt
16 
17  */
18 
19 #if (defined(_WIN32))
20  #define _CRT_SECURE_NO_WARNINGS
21 #endif
22 
23 //FIXME : ANDROID does not yet support 64 bit functions
24 #if defined(__APPLE__) || defined(ANDROID)
25 // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
26 #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
27 #define FTELLO_FUNC(stream) ftello(stream)
28 #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
29 #else
30 #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
31 #define FTELLO_FUNC(stream) ftello64(stream)
32 #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
33 #endif
34 
35 
36 #include "minizip/ioapi.h"
37 
38 voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
39 {
40  if (pfilefunc->zfile_func64.zopen64_file != NULL)
41  {
42  return (*(pfilefunc->zfile_func64.zopen64_file))(pfilefunc->zfile_func64.opaque,filename,mode);
43  }
44  else
45  {
46  return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
47  }
48 }
49 
50 long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
51 {
52  if (pfilefunc->zfile_func64.zseek64_file != NULL)
53  {
54  return (*(pfilefunc->zfile_func64.zseek64_file))(pfilefunc->zfile_func64.opaque,filestream,offset,origin);
55  }
56  else
57  {
58  uLong offsetTruncated = (uLong)offset;
59  if (offsetTruncated != offset)
60  {
61  return -1;
62  }
63  else
64  {
65  return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
66  }
67  }
68 }
69 
70 ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
71 {
72  if (pfilefunc->zfile_func64.zseek64_file != NULL)
73  {
74  return (*(pfilefunc->zfile_func64.ztell64_file))(pfilefunc->zfile_func64.opaque,filestream);
75  }
76  else
77  {
78  uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
79  if ((tell_uLong) == ((uLong)-1))
80  {
81  return (ZPOS64_T)-1;
82  }
83  else
84  {
85  return tell_uLong;
86  }
87  }
88 }
89 
90 void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,
91  const zlib_filefunc_def* p_filefunc32)
92 {
93  p_filefunc64_32->zfile_func64.zopen64_file = NULL;
94  p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
95  p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
96  p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
97  p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
98  p_filefunc64_32->zfile_func64.ztell64_file = NULL;
99  p_filefunc64_32->zfile_func64.zseek64_file = NULL;
100  p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
101  p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
102  p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
103  p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
104  p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
105 }
106 
107 
108 
109 static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
110 static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
111 static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
112 static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
113 static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
114 static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
115 static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
116 
117 static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
118 {
119  FILE* file = NULL;
120  const char* mode_fopen = NULL;
121  if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
122  {
123  mode_fopen = "rb";
124  }
125  else
126  if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
127  {
128  mode_fopen = "r+b";
129  }
130  else
131  if (mode & ZLIB_FILEFUNC_MODE_CREATE)
132  {
133  mode_fopen = "wb";
134  }
135 
136  if ((filename!=NULL) && (mode_fopen != NULL))
137  {
138  file = fopen(filename, mode_fopen);
139  }
140  return file;
141 }
142 
143 static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
144 {
145  FILE* file = NULL;
146  const char* mode_fopen = NULL;
147  if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
148  {
149  mode_fopen = "rb";
150  }
151  else
152  if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
153  {
154  mode_fopen = "r+b";
155  }
156  else
157  if (mode & ZLIB_FILEFUNC_MODE_CREATE)
158  {
159  mode_fopen = "wb";
160  }
161 
162  if ((filename!=NULL) && (mode_fopen != NULL))
163  {
164  file = FOPEN_FUNC((const char*)filename, mode_fopen);
165  }
166  return file;
167 }
168 
169 
170 static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
171 {
172  uLong ret;
173  ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
174  return ret;
175 }
176 
177 static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
178 {
179  uLong ret;
180  ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
181  return ret;
182 }
183 
184 static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
185 {
186  long ret;
187  ret = ftell((FILE *)stream);
188  return ret;
189 }
190 
191 
192 static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
193 {
194  ZPOS64_T ret;
195  ret = FTELLO_FUNC((FILE *)stream);
196  return ret;
197 }
198 
199 static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
200 {
201  int fseek_origin = 0;
202  long ret;
203  switch (origin)
204  {
205  case ZLIB_FILEFUNC_SEEK_CUR:
206  fseek_origin = SEEK_CUR;
207  break;
208  case ZLIB_FILEFUNC_SEEK_END:
209  fseek_origin = SEEK_END;
210  break;
211  case ZLIB_FILEFUNC_SEEK_SET:
212  fseek_origin = SEEK_SET;
213  break;
214  default: return -1;
215  }
216  ret = 0;
217  if (fseek((FILE *)stream, offset, fseek_origin) != 0)
218  {
219  ret = -1;
220  }
221  return ret;
222 }
223 
224 static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
225 {
226  int fseek_origin = 0;
227  long ret;
228  switch (origin)
229  {
230  case ZLIB_FILEFUNC_SEEK_CUR:
231  fseek_origin = SEEK_CUR;
232  break;
233  case ZLIB_FILEFUNC_SEEK_END:
234  fseek_origin = SEEK_END;
235  break;
236  case ZLIB_FILEFUNC_SEEK_SET:
237  fseek_origin = SEEK_SET;
238  break;
239  default: return -1;
240  }
241  ret = 0;
242 
243  if(FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
244  {
245  ret = -1;
246  }
247 
248  return ret;
249 }
250 
251 
252 static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
253 {
254  int ret;
255  ret = fclose((FILE *)stream);
256  return ret;
257 }
258 
259 static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
260 {
261  int ret;
262  ret = ferror((FILE *)stream);
263  return ret;
264 }
265 
266 void fill_fopen_filefunc (pzlib_filefunc_def)
267 zlib_filefunc_def* pzlib_filefunc_def;
268 {
269  pzlib_filefunc_def->zopen_file = fopen_file_func;
270  pzlib_filefunc_def->zread_file = fread_file_func;
271  pzlib_filefunc_def->zwrite_file = fwrite_file_func;
272  pzlib_filefunc_def->ztell_file = ftell_file_func;
273  pzlib_filefunc_def->zseek_file = fseek_file_func;
274  pzlib_filefunc_def->zclose_file = fclose_file_func;
275  pzlib_filefunc_def->zerror_file = ferror_file_func;
276  pzlib_filefunc_def->opaque = NULL;
277 }
278 
279 void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
280 {
281  pzlib_filefunc_def->zopen64_file = fopen64_file_func;
282  pzlib_filefunc_def->zread_file = fread_file_func;
283  pzlib_filefunc_def->zwrite_file = fwrite_file_func;
284  pzlib_filefunc_def->ztell64_file = ftell64_file_func;
285  pzlib_filefunc_def->zseek64_file = fseek64_file_func;
286  pzlib_filefunc_def->zclose_file = fclose_file_func;
287  pzlib_filefunc_def->zerror_file = ferror_file_func;
288  pzlib_filefunc_def->opaque = NULL;
289 }