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