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