Oolite
Loading...
Searching...
No Matches
ioapi.c
Go to the documentation of this file.
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#if (defined(__APPLE__))
18// I/O is always 64-bit on Darwin
19#define fopen64 fopen
20#define ftello64 ftello
21#define fseeko64 fseeko
22#endif
23
24#include "ioapi.h"
25
26voidpf call_zopen64(const zlib_filefunc64_32_def* pfilefunc, const void* filename, int mode)
27{
28 if (pfilefunc->zfile_func64.zopen64_file != NULL)
29 return (*(pfilefunc->zfile_func64.zopen64_file))(pfilefunc->zfile_func64.opaque, filename, mode);
30 else {
31 return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque, (const char*)filename, mode);
32 }
33}
34
35long call_zseek64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, ZPOS64_T offset, int origin)
36{
37 if (pfilefunc->zfile_func64.zseek64_file != NULL)
38 return (*(pfilefunc->zfile_func64.zseek64_file))(pfilefunc->zfile_func64.opaque, filestream, offset, origin);
39 else {
40 uLong offsetTruncated = (uLong)offset;
41 if (offsetTruncated != offset)
42 return -1;
43 else
44 return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque, filestream, offsetTruncated, origin);
45 }
46}
47
49{
50 if (pfilefunc->zfile_func64.zseek64_file != NULL)
51 return (*(pfilefunc->zfile_func64.ztell64_file))(pfilefunc->zfile_func64.opaque, filestream);
52 else {
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
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
77static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
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;
90 mode_fopen = "rb";
92 mode_fopen = "r+b";
94 mode_fopen = "wb";
95
96 if ((filename != NULL) && (mode_fopen != NULL))
97 file = fopen(filename, mode_fopen);
98 return file;
99}
100
101static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode)
102{
103 FILE* file = NULL;
104 const char* mode_fopen = NULL;
106 mode_fopen = "rb";
108 mode_fopen = "r+b";
110 mode_fopen = "wb";
111
112 if ((filename != NULL) && (mode_fopen != NULL))
113 file = fopen64((const char*)filename, mode_fopen);
114 return file;
115}
116
118{
119 uLong ret;
120 ret = (uLong)fread(buf, 1, (size_t)size, (FILE*)stream);
121 return ret;
122}
123
125{
126 uLong ret;
127 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE*)stream);
128 return ret;
129}
130
132{
133 long ret;
134 ret = ftell((FILE*)stream);
135 return ret;
136}
137
139{
140 ZPOS64_T ret;
141 ret = ftello64((FILE*)stream);
142 return ret;
143}
144
146{
147 int fseek_origin = 0;
148 long ret;
149 switch (origin) {
151 fseek_origin = SEEK_CUR;
152 break;
154 fseek_origin = SEEK_END;
155 break;
157 fseek_origin = SEEK_SET;
158 break;
159 default:
160 return -1;
161 }
162 ret = 0;
163 if (fseek((FILE*)stream, offset, fseek_origin) != 0)
164 ret = -1;
165 return ret;
166}
167
169{
170 int fseek_origin = 0;
171 long ret;
172 switch (origin) {
174 fseek_origin = SEEK_CUR;
175 break;
177 fseek_origin = SEEK_END;
178 break;
180 fseek_origin = SEEK_SET;
181 break;
182 default:
183 return -1;
184 }
185 ret = 0;
186
187 if (fseeko64((FILE*)stream, offset, fseek_origin) != 0)
188 ret = -1;
189
190 return ret;
191}
192
194{
195 int ret;
196 ret = fclose((FILE*)stream);
197 return ret;
198}
199
201{
202 int ret;
203 ret = ferror((FILE*)stream);
204 return ret;
205}
206
207void fill_fopen_filefunc(zlib_filefunc_def* pzlib_filefunc_def)
208{
209 pzlib_filefunc_def->zopen_file = fopen_file_func;
210 pzlib_filefunc_def->zread_file = fread_file_func;
211 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
212 pzlib_filefunc_def->ztell_file = ftell_file_func;
213 pzlib_filefunc_def->zseek_file = fseek_file_func;
214 pzlib_filefunc_def->zclose_file = fclose_file_func;
215 pzlib_filefunc_def->zerror_file = ferror_file_func;
216 pzlib_filefunc_def->opaque = NULL;
217}
218
220{
221 pzlib_filefunc_def->zopen64_file = fopen64_file_func;
222 pzlib_filefunc_def->zread_file = fread_file_func;
223 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
224 pzlib_filefunc_def->ztell64_file = ftell64_file_func;
225 pzlib_filefunc_def->zseek64_file = fseek64_file_func;
226 pzlib_filefunc_def->zclose_file = fclose_file_func;
227 pzlib_filefunc_def->zerror_file = ferror_file_func;
228 pzlib_filefunc_def->opaque = NULL;
229}
static long ZCALLBACK fseek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin)
Definition ioapi.c:145
static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream)
Definition ioapi.c:138
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def *p_filefunc64_32, const zlib_filefunc_def *p_filefunc32)
Definition ioapi.c:61
static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
Definition ioapi.c:168
static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char *filename, int mode))
static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream)
Definition ioapi.c:200
static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void *buf, uLong size)
Definition ioapi.c:124
long call_zseek64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream, ZPOS64_T offset, int origin)
Definition ioapi.c:35
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
Definition ioapi.c:219
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
Definition ioapi.c:207
voidpf call_zopen64(const zlib_filefunc64_32_def *pfilefunc, const void *filename, int mode)
Definition ioapi.c:26
ZPOS64_T call_ztell64(const zlib_filefunc64_32_def *pfilefunc, voidpf filestream)
Definition ioapi.c:48
static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void *filename, int mode)
Definition ioapi.c:101
static voidpf ZCALLBACK fopen_file_func(voidpf opaque, const char *filename, int mode)
Definition ioapi.c:85
static long ZCALLBACK ftell_file_func(voidpf opaque, voidpf stream)
Definition ioapi.c:131
static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void *buf, uLong size)
Definition ioapi.c:117
static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream)
Definition ioapi.c:193
voidpf void uLong size
Definition ioapi.h:126
struct zlib_filefunc64_def_s zlib_filefunc64_def
const char * filename
Definition ioapi.h:125
#define ZLIB_FILEFUNC_MODE_READWRITEFILTER
Definition ioapi.h:112
voidpf stream
Definition ioapi.h:126
voidpf uLong int origin
Definition ioapi.h:132
voidpf uLong offset
Definition ioapi.h:132
#define ZLIB_FILEFUNC_MODE_EXISTING
Definition ioapi.h:114
#define ZLIB_FILEFUNC_SEEK_CUR
Definition ioapi.h:106
const char int mode
Definition ioapi.h:125
voidpf void * buf
Definition ioapi.h:126
#define ZLIB_FILEFUNC_SEEK_SET
Definition ioapi.h:108
struct zlib_filefunc_def_s zlib_filefunc_def
struct zlib_filefunc64_32_def_s zlib_filefunc64_32_def
#define ZCALLBACK
Definition ioapi.h:121
#define ZLIB_FILEFUNC_MODE_CREATE
Definition ioapi.h:115
#define ZLIB_FILEFUNC_SEEK_END
Definition ioapi.h:107
#define ZLIB_FILEFUNC_MODE_READ
Definition ioapi.h:110
unsigned long long int ZPOS64_T
Definition ioapi.h:97
mz_ulong uLong
Definition miniz.h:440
void * voidpf
Definition miniz.h:445
seek_file_func zseek32_file
Definition ioapi.h:169
zlib_filefunc64_def zfile_func64
Definition ioapi.h:166
open_file_func zopen32_file
Definition ioapi.h:167
tell_file_func ztell32_file
Definition ioapi.h:168
write_file_func zwrite_file
Definition ioapi.h:153
open64_file_func zopen64_file
Definition ioapi.h:151
read_file_func zread_file
Definition ioapi.h:152
tell64_file_func ztell64_file
Definition ioapi.h:154
close_file_func zclose_file
Definition ioapi.h:156
seek64_file_func zseek64_file
Definition ioapi.h:155
testerror_file_func zerror_file
Definition ioapi.h:157
seek_file_func zseek_file
Definition ioapi.h:140
open_file_func zopen_file
Definition ioapi.h:136
testerror_file_func zerror_file
Definition ioapi.h:142
write_file_func zwrite_file
Definition ioapi.h:138
read_file_func zread_file
Definition ioapi.h:137
close_file_func zclose_file
Definition ioapi.h:141
tell_file_func ztell_file
Definition ioapi.h:139