comparison zlib/contrib/minizip/ioapi.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents ae3a4bfb450b
children
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* ioapi.c -- IO base function header for compress/uncompress .zip 1 /* ioapi.h -- IO base function header for compress/uncompress .zip
2 files using zlib + zip or unzip API 2 part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
3 3
4 Version 1.01e, February 12th, 2005 4 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
5 5
6 Copyright (C) 1998-2005 Gilles Vollant 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
7 */ 11 */
8 12
9 #include <stdio.h> 13 #if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
10 #include <stdlib.h> 14 #define _CRT_SECURE_NO_WARNINGS
11 #include <string.h> 15 #endif
12 16
13 #include "zlib.h" 17 #if defined(__APPLE__) || defined(IOAPI_NO_64)
18 // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
19 #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
20 #define FTELLO_FUNC(stream) ftello(stream)
21 #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
22 #else
23 #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
24 #define FTELLO_FUNC(stream) ftello64(stream)
25 #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
26 #endif
27
28
14 #include "ioapi.h" 29 #include "ioapi.h"
15 30
16 31 voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
17 32 {
18 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ 33 if (pfilefunc->zfile_func64.zopen64_file != NULL)
19 34 return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
20 #ifndef SEEK_CUR 35 else
21 #define SEEK_CUR 1 36 {
22 #endif 37 return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
23 38 }
24 #ifndef SEEK_END 39 }
25 #define SEEK_END 2 40
26 #endif 41 long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
27 42 {
28 #ifndef SEEK_SET 43 if (pfilefunc->zfile_func64.zseek64_file != NULL)
29 #define SEEK_SET 0 44 return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
30 #endif 45 else
31 46 {
32 voidpf ZCALLBACK fopen_file_func OF(( 47 uLong offsetTruncated = (uLong)offset;
33 voidpf opaque, 48 if (offsetTruncated != offset)
34 const char* filename, 49 return -1;
35 int mode)); 50 else
36 51 return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
37 uLong ZCALLBACK fread_file_func OF(( 52 }
38 voidpf opaque, 53 }
39 voidpf stream, 54
40 void* buf, 55 ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
41 uLong size)); 56 {
42 57 if (pfilefunc->zfile_func64.zseek64_file != NULL)
43 uLong ZCALLBACK fwrite_file_func OF(( 58 return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
44 voidpf opaque, 59 else
45 voidpf stream, 60 {
46 const void* buf, 61 uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
47 uLong size)); 62 if ((tell_uLong) == MAXU32)
48 63 return (ZPOS64_T)-1;
49 long ZCALLBACK ftell_file_func OF(( 64 else
50 voidpf opaque, 65 return tell_uLong;
51 voidpf stream)); 66 }
52 67 }
53 long ZCALLBACK fseek_file_func OF(( 68
54 voidpf opaque, 69 void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
55 voidpf stream, 70 {
56 uLong offset, 71 p_filefunc64_32->zfile_func64.zopen64_file = NULL;
57 int origin)); 72 p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
58 73 p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
59 int ZCALLBACK fclose_file_func OF(( 74 p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
60 voidpf opaque, 75 p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
61 voidpf stream)); 76 p_filefunc64_32->zfile_func64.ztell64_file = NULL;
62 77 p_filefunc64_32->zfile_func64.zseek64_file = NULL;
63 int ZCALLBACK ferror_file_func OF(( 78 p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
64 voidpf opaque, 79 p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
65 voidpf stream)); 80 p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
66 81 p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
67 82 p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
68 voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) 83 }
69 voidpf opaque; 84
70 const char* filename; 85
71 int mode; 86
87 static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
88 static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
89 static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
90 static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
91 static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
92 static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
93 static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
94
95 static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
72 { 96 {
73 FILE* file = NULL; 97 FILE* file = NULL;
74 const char* mode_fopen = NULL; 98 const char* mode_fopen = NULL;
75 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 99 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
76 mode_fopen = "rb"; 100 mode_fopen = "rb";
84 if ((filename!=NULL) && (mode_fopen != NULL)) 108 if ((filename!=NULL) && (mode_fopen != NULL))
85 file = fopen(filename, mode_fopen); 109 file = fopen(filename, mode_fopen);
86 return file; 110 return file;
87 } 111 }
88 112
89 113 static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
90 uLong ZCALLBACK fread_file_func (opaque, stream, buf, size) 114 {
91 voidpf opaque; 115 FILE* file = NULL;
92 voidpf stream; 116 const char* mode_fopen = NULL;
93 void* buf; 117 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
94 uLong size; 118 mode_fopen = "rb";
119 else
120 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
121 mode_fopen = "r+b";
122 else
123 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
124 mode_fopen = "wb";
125
126 if ((filename!=NULL) && (mode_fopen != NULL))
127 file = FOPEN_FUNC((const char*)filename, mode_fopen);
128 return file;
129 }
130
131
132 static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
95 { 133 {
96 uLong ret; 134 uLong ret;
97 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); 135 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
98 return ret; 136 return ret;
99 } 137 }
100 138
101 139 static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
102 uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
103 voidpf opaque;
104 voidpf stream;
105 const void* buf;
106 uLong size;
107 { 140 {
108 uLong ret; 141 uLong ret;
109 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); 142 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
110 return ret; 143 return ret;
111 } 144 }
112 145
113 long ZCALLBACK ftell_file_func (opaque, stream) 146 static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
114 voidpf opaque;
115 voidpf stream;
116 { 147 {
117 long ret; 148 long ret;
118 ret = ftell((FILE *)stream); 149 ret = ftell((FILE *)stream);
119 return ret; 150 return ret;
120 } 151 }
121 152
122 long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) 153
123 voidpf opaque; 154 static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
124 voidpf stream; 155 {
125 uLong offset; 156 ZPOS64_T ret;
126 int origin; 157 ret = FTELLO_FUNC((FILE *)stream);
158 return ret;
159 }
160
161 static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
127 { 162 {
128 int fseek_origin=0; 163 int fseek_origin=0;
129 long ret; 164 long ret;
130 switch (origin) 165 switch (origin)
131 { 166 {
139 fseek_origin = SEEK_SET; 174 fseek_origin = SEEK_SET;
140 break; 175 break;
141 default: return -1; 176 default: return -1;
142 } 177 }
143 ret = 0; 178 ret = 0;
144 fseek((FILE *)stream, offset, fseek_origin); 179 if (fseek((FILE *)stream, offset, fseek_origin) != 0)
145 return ret; 180 ret = -1;
146 } 181 return ret;
147 182 }
148 int ZCALLBACK fclose_file_func (opaque, stream) 183
149 voidpf opaque; 184 static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
150 voidpf stream; 185 {
186 int fseek_origin=0;
187 long ret;
188 switch (origin)
189 {
190 case ZLIB_FILEFUNC_SEEK_CUR :
191 fseek_origin = SEEK_CUR;
192 break;
193 case ZLIB_FILEFUNC_SEEK_END :
194 fseek_origin = SEEK_END;
195 break;
196 case ZLIB_FILEFUNC_SEEK_SET :
197 fseek_origin = SEEK_SET;
198 break;
199 default: return -1;
200 }
201 ret = 0;
202
203 if(FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
204 ret = -1;
205
206 return ret;
207 }
208
209
210 static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
151 { 211 {
152 int ret; 212 int ret;
153 ret = fclose((FILE *)stream); 213 ret = fclose((FILE *)stream);
154 return ret; 214 return ret;
155 } 215 }
156 216
157 int ZCALLBACK ferror_file_func (opaque, stream) 217 static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
158 voidpf opaque;
159 voidpf stream;
160 { 218 {
161 int ret; 219 int ret;
162 ret = ferror((FILE *)stream); 220 ret = ferror((FILE *)stream);
163 return ret; 221 return ret;
164 } 222 }
173 pzlib_filefunc_def->zseek_file = fseek_file_func; 231 pzlib_filefunc_def->zseek_file = fseek_file_func;
174 pzlib_filefunc_def->zclose_file = fclose_file_func; 232 pzlib_filefunc_def->zclose_file = fclose_file_func;
175 pzlib_filefunc_def->zerror_file = ferror_file_func; 233 pzlib_filefunc_def->zerror_file = ferror_file_func;
176 pzlib_filefunc_def->opaque = NULL; 234 pzlib_filefunc_def->opaque = NULL;
177 } 235 }
236
237 void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
238 {
239 pzlib_filefunc_def->zopen64_file = fopen64_file_func;
240 pzlib_filefunc_def->zread_file = fread_file_func;
241 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
242 pzlib_filefunc_def->ztell64_file = ftell64_file_func;
243 pzlib_filefunc_def->zseek64_file = fseek64_file_func;
244 pzlib_filefunc_def->zclose_file = fclose_file_func;
245 pzlib_filefunc_def->zerror_file = ferror_file_func;
246 pzlib_filefunc_def->opaque = NULL;
247 }