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