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