summaryrefslogtreecommitdiff
path: root/contrib/minizip/iowin32.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/minizip/iowin32.c')
-rw-r--r--contrib/minizip/iowin32.c541
1 files changed, 270 insertions, 271 deletions
diff --git a/contrib/minizip/iowin32.c b/contrib/minizip/iowin32.c
index 53198b6..8e6d43d 100644
--- a/contrib/minizip/iowin32.c
+++ b/contrib/minizip/iowin32.c
@@ -1,271 +1,270 @@
1/* iowin32.c -- IO base function header for compress/uncompress .zip 1/* iowin32.c -- IO base function header for compress/uncompress .zip
2 files using zlib + zip or unzip API 2 files using zlib + zip or unzip API
3 This IO API version uses the Win32 API (for Microsoft Windows) 3 This IO API version uses the Win32 API (for Microsoft Windows)
4 4
5 Version 0.22, May 19th, 2003 5 Version 0.22, May 19th, 2003
6 6
7 Copyright (C) 1998-2003 Gilles Vollant 7 Copyright (C) 1998-2003 Gilles Vollant
8*/ 8*/
9 9
10#include <windows.h> 10#include <stdlib.h>
11#include <stdlib.h> 11
12 12#include "zlib.h"
13#include "zlib.h" 13#include "ioapi.h"
14#include "ioapi.h" 14#include "iowin32.h"
15#include "iowin32.h" 15
16 16#ifndef INVALID_HANDLE_VALUE
17#ifndef INVALID_HANDLE_VALUE 17#define INVALID_HANDLE_VALUE (0xFFFFFFFF)
18#define INVALID_HANDLE_VALUE (0xFFFFFFFF) 18#endif
19#endif 19
20 20#ifndef INVALID_SET_FILE_POINTER
21#ifndef INVALID_SET_FILE_POINTER 21#define INVALID_SET_FILE_POINTER ((DWORD)-1)
22#define INVALID_SET_FILE_POINTER ((DWORD)-1) 22#endif
23#endif 23
24 24voidpf ZCALLBACK win32_open_file_func OF((
25voidpf ZCALLBACK win32_open_file_func OF(( 25 voidpf opaque,
26 voidpf opaque, 26 const char* filename,
27 const char* filename, 27 int mode));
28 int mode)); 28
29 29uLong ZCALLBACK win32_read_file_func OF((
30uLong ZCALLBACK win32_read_file_func OF(( 30 voidpf opaque,
31 voidpf opaque, 31 voidpf stream,
32 voidpf stream, 32 void* buf,
33 void* buf, 33 uLong size));
34 uLong size)); 34
35 35uLong ZCALLBACK win32_write_file_func OF((
36uLong ZCALLBACK win32_write_file_func OF(( 36 voidpf opaque,
37 voidpf opaque, 37 voidpf stream,
38 voidpf stream, 38 const void* buf,
39 const void* buf, 39 uLong size));
40 uLong size)); 40
41 41long ZCALLBACK win32_tell_file_func OF((
42long ZCALLBACK win32_tell_file_func OF(( 42 voidpf opaque,
43 voidpf opaque, 43 voidpf stream));
44 voidpf stream)); 44
45 45long ZCALLBACK win32_seek_file_func OF((
46long ZCALLBACK win32_seek_file_func OF(( 46 voidpf opaque,
47 voidpf opaque, 47 voidpf stream,
48 voidpf stream, 48 uLong offset,
49 uLong offset, 49 int origin));
50 int origin)); 50
51 51int ZCALLBACK win32_close_file_func OF((
52long ZCALLBACK win32_close_file_func OF(( 52 voidpf opaque,
53 voidpf opaque, 53 voidpf stream));
54 voidpf stream)); 54
55 55int ZCALLBACK win32_error_file_func OF((
56int ZCALLBACK win32_error_file_func OF(( 56 voidpf opaque,
57 voidpf opaque, 57 voidpf stream));
58 voidpf stream)); 58
59 59typedef struct
60typedef struct 60{
61{ 61 HANDLE hf;
62 HANDLE hf; 62 int error;
63 int error; 63} WIN32FILE_IOWIN;
64} WIN32FILE_IOWIN; 64
65 65voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode)
66voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode) 66 voidpf opaque;
67 voidpf opaque; 67 const char* filename;
68 const char* filename; 68 int mode;
69 int mode; 69{
70{ 70 const char* mode_fopen = NULL;
71 const char* mode_fopen = NULL; 71 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
72 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; 72 HANDLE hFile = 0;
73 HANDLE hFile = 0; 73 voidpf ret=NULL;
74 voidpf ret=NULL; 74
75 75 dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
76 dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0; 76
77 77 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
78 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 78 {
79 { 79 dwDesiredAccess = GENERIC_READ;
80 dwDesiredAccess = GENERIC_READ; 80 dwCreationDisposition = OPEN_EXISTING;
81 dwCreationDisposition = OPEN_EXISTING; 81 dwShareMode = FILE_SHARE_READ;
82 dwShareMode = FILE_SHARE_READ; 82 }
83 } 83 else
84 else 84 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
85 if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 85 {
86 { 86 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
87 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ; 87 dwCreationDisposition = OPEN_EXISTING;
88 dwCreationDisposition = OPEN_EXISTING; 88 }
89 } 89 else
90 else 90 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
91 if (mode & ZLIB_FILEFUNC_MODE_CREATE) 91 {
92 { 92 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
93 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ; 93 dwCreationDisposition = CREATE_ALWAYS;
94 dwCreationDisposition = CREATE_ALWAYS; 94 }
95 } 95
96 96 if ((filename!=NULL) && (dwDesiredAccess != 0))
97 if ((filename!=NULL) && (dwDesiredAccess != 0)) 97 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
98 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, 98 dwCreationDisposition, dwFlagsAndAttributes, NULL);
99 dwCreationDisposition, dwFlagsAndAttributes, NULL); 99
100 100 if (hFile == INVALID_HANDLE_VALUE)
101 if (hFile == INVALID_HANDLE_VALUE) 101 hFile = NULL;
102 hFile = NULL; 102
103 103 if (hFile != NULL)
104 if (hFile != NULL) 104 {
105 { 105 WIN32FILE_IOWIN w32fiow;
106 WIN32FILE_IOWIN w32fiow; 106 w32fiow.hf = hFile;
107 w32fiow.hf = hFile; 107 w32fiow.error = 0;
108 w32fiow.error = 0; 108 ret = malloc(sizeof(WIN32FILE_IOWIN));
109 ret = malloc(sizeof(WIN32FILE_IOWIN)); 109 if (ret==NULL)
110 if (ret==NULL) 110 CloseHandle(hFile);
111 CloseHandle(hFile); 111 else *((WIN32FILE_IOWIN*)ret) = w32fiow;
112 else *((WIN32FILE_IOWIN*)ret) = w32fiow; 112 }
113 } 113 return ret;
114 return ret; 114}
115} 115
116 116
117 117uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size)
118uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size) 118 voidpf opaque;
119 voidpf opaque; 119 voidpf stream;
120 voidpf stream; 120 void* buf;
121 void* buf; 121 uLong size;
122 uLong size; 122{
123{ 123 uLong ret=0;
124 uLong ret=0; 124 HANDLE hFile = NULL;
125 HANDLE hFile = NULL; 125 if (stream!=NULL)
126 if (stream!=NULL) 126 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
127 hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 127 if (hFile != NULL)
128 if (hFile != NULL) 128 if (!ReadFile(hFile, buf, size, &ret, NULL))
129 if (!ReadFile(hFile, buf, size, &ret, NULL)) 129 {
130 { 130 DWORD dwErr = GetLastError();
131 DWORD dwErr = GetLastError(); 131 if (dwErr == ERROR_HANDLE_EOF)
132 if (dwErr == ERROR_HANDLE_EOF) 132 dwErr = 0;
133 dwErr = 0; 133 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
134 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 134 }
135 } 135
136 136 return ret;
137 return ret; 137}
138} 138
139 139
140 140uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size)
141uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size) 141 voidpf opaque;
142 voidpf opaque; 142 voidpf stream;
143 voidpf stream; 143 const void* buf;
144 const void* buf; 144 uLong size;
145 uLong size; 145{
146{ 146 uLong ret=0;
147 uLong ret=0; 147 HANDLE hFile = NULL;
148 HANDLE hFile = NULL; 148 if (stream!=NULL)
149 if (stream!=NULL) 149 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
150 hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 150
151 151 if (hFile !=NULL)
152 if (hFile !=NULL) 152 if (!WriteFile(hFile, buf, size, &ret, NULL))
153 if (!WriteFile(hFile, buf, size, &ret, NULL)) 153 {
154 { 154 DWORD dwErr = GetLastError();
155 DWORD dwErr = GetLastError(); 155 if (dwErr == ERROR_HANDLE_EOF)
156 if (dwErr == ERROR_HANDLE_EOF) 156 dwErr = 0;
157 dwErr = 0; 157 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
158 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 158 }
159 } 159
160 160 return ret;
161 return ret; 161}
162} 162
163 163long ZCALLBACK win32_tell_file_func (opaque, stream)
164long ZCALLBACK win32_tell_file_func (opaque, stream) 164 voidpf opaque;
165 voidpf opaque; 165 voidpf stream;
166 voidpf stream; 166{
167{ 167 long ret=-1;
168 long ret=-1; 168 HANDLE hFile = NULL;
169 HANDLE hFile = NULL; 169 if (stream!=NULL)
170 if (stream!=NULL) 170 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
171 hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 171 if (hFile != NULL)
172 if (hFile != NULL) 172 {
173 { 173 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
174 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); 174 if (dwSet == INVALID_SET_FILE_POINTER)
175 if (dwSet == INVALID_SET_FILE_POINTER) 175 {
176 { 176 DWORD dwErr = GetLastError();
177 DWORD dwErr = GetLastError(); 177 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
178 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 178 ret = -1;
179 ret = -1; 179 }
180 } 180 else
181 else 181 ret=(long)dwSet;
182 ret=(long)dwSet; 182 }
183 } 183 return ret;
184 return ret; 184}
185} 185
186 186long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin)
187long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin) 187 voidpf opaque;
188 voidpf opaque; 188 voidpf stream;
189 voidpf stream; 189 uLong offset;
190 uLong offset; 190 int origin;
191 int origin; 191{
192{ 192 DWORD dwMoveMethod=0xFFFFFFFF;
193 DWORD dwMoveMethod=0xFFFFFFFF; 193 HANDLE hFile = NULL;
194 HANDLE hFile = NULL; 194
195 195 long ret=-1;
196 long ret=-1; 196 if (stream!=NULL)
197 if (stream!=NULL) 197 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
198 hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 198 switch (origin)
199 switch (origin) 199 {
200 { 200 case ZLIB_FILEFUNC_SEEK_CUR :
201 case ZLIB_FILEFUNC_SEEK_CUR : 201 dwMoveMethod = FILE_CURRENT;
202 dwMoveMethod = FILE_CURRENT; 202 break;
203 break; 203 case ZLIB_FILEFUNC_SEEK_END :
204 case ZLIB_FILEFUNC_SEEK_END : 204 dwMoveMethod = FILE_END;
205 dwMoveMethod = FILE_END; 205 break;
206 break; 206 case ZLIB_FILEFUNC_SEEK_SET :
207 case ZLIB_FILEFUNC_SEEK_SET : 207 dwMoveMethod = FILE_BEGIN;
208 dwMoveMethod = FILE_BEGIN; 208 break;
209 break; 209 default: return -1;
210 default: return -1; 210 }
211 } 211
212 212 if (hFile != NULL)
213 if (hFile != NULL) 213 {
214 { 214 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
215 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod); 215 if (dwSet == INVALID_SET_FILE_POINTER)
216 if (dwSet == INVALID_SET_FILE_POINTER) 216 {
217 { 217 DWORD dwErr = GetLastError();
218 DWORD dwErr = GetLastError(); 218 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
219 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr; 219 ret = -1;
220 ret = -1; 220 }
221 } 221 else
222 else 222 ret=0;
223 ret=0; 223 }
224 } 224 return ret;
225 return ret; 225}
226} 226
227 227int ZCALLBACK win32_close_file_func (opaque, stream)
228long ZCALLBACK win32_close_file_func (opaque, stream) 228 voidpf opaque;
229 voidpf opaque; 229 voidpf stream;
230 voidpf stream; 230{
231{ 231 int ret=-1;
232 long ret=-1; 232
233 233 if (stream!=NULL)
234 if (stream!=NULL) 234 {
235 { 235 HANDLE hFile;
236 HANDLE hFile; 236 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
237 hFile = ((WIN32FILE_IOWIN*)stream) -> hf; 237 if (hFile != NULL)
238 if (hFile != NULL) 238 {
239 { 239 CloseHandle(hFile);
240 CloseHandle(hFile); 240 ret=0;
241 ret=0; 241 }
242 } 242 free(stream);
243 free(stream); 243 }
244 } 244 return ret;
245 return ret; 245}
246} 246
247 247int ZCALLBACK win32_error_file_func (opaque, stream)
248int ZCALLBACK win32_error_file_func (opaque, stream) 248 voidpf opaque;
249 voidpf opaque; 249 voidpf stream;
250 voidpf stream; 250{
251{ 251 int ret=-1;
252 int ret=-1; 252 if (stream!=NULL)
253 if (stream!=NULL) 253 {
254 { 254 ret = ((WIN32FILE_IOWIN*)stream) -> error;
255 ret = ((WIN32FILE_IOWIN*)stream) -> error; 255 }
256 } 256 return ret;
257 return ret; 257}
258} 258
259 259void fill_win32_filefunc (pzlib_filefunc_def)
260void fill_win32_filefunc (pzlib_filefunc_def) 260 zlib_filefunc_def* pzlib_filefunc_def;
261 zlib_filefunc_def* pzlib_filefunc_def; 261{
262{ 262 pzlib_filefunc_def->zopen_file = win32_open_file_func;
263 pzlib_filefunc_def->zopen_file = win32_open_file_func; 263 pzlib_filefunc_def->zread_file = win32_read_file_func;
264 pzlib_filefunc_def->zread_file = win32_read_file_func; 264 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
265 pzlib_filefunc_def->zwrite_file = win32_write_file_func; 265 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
266 pzlib_filefunc_def->ztell_file = win32_tell_file_func; 266 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
267 pzlib_filefunc_def->zseek_file = win32_seek_file_func; 267 pzlib_filefunc_def->zclose_file = win32_close_file_func;
268 pzlib_filefunc_def->zclose_file = win32_close_file_func; 268 pzlib_filefunc_def->zerror_file = win32_error_file_func;
269 pzlib_filefunc_def->zerror_file = win32_error_file_func; 269 pzlib_filefunc_def->opaque=NULL;
270 pzlib_filefunc_def->opaque=NULL; 270}
271}