24 #include "minizip/ioapi.h" 25 #include "minizip/iowin32.h" 27 #ifndef INVALID_HANDLE_VALUE 28 #define INVALID_HANDLE_VALUE (0xFFFFFFFF) 31 #ifndef INVALID_SET_FILE_POINTER 32 #define INVALID_SET_FILE_POINTER ((DWORD)-1) 35 voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque,
const char* filename,
int mode));
36 uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream,
void* buf, uLong size));
37 uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream,
const void* buf, uLong size));
38 ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream));
39 long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset,
int origin));
40 int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream));
41 int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream));
50 static void win32_translate_open_mode(
int mode,
51 DWORD* lpdwDesiredAccess,
52 DWORD* lpdwCreationDisposition,
54 DWORD* lpdwFlagsAndAttributes)
56 *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0;
58 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
60 *lpdwDesiredAccess = GENERIC_READ;
61 *lpdwCreationDisposition = OPEN_EXISTING;
62 *lpdwShareMode = FILE_SHARE_READ;
64 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
66 *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
67 *lpdwCreationDisposition = OPEN_EXISTING;
69 else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
71 *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
72 *lpdwCreationDisposition = CREATE_ALWAYS;
76 static voidpf win32_build_iowin(HANDLE hFile)
80 if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
82 WIN32FILE_IOWIN w32fiow;
85 ret = malloc(
sizeof(WIN32FILE_IOWIN));
93 *((WIN32FILE_IOWIN*)ret) = w32fiow;
99 voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,
const void* filename,
int mode)
101 const char* mode_fopen = NULL;
102 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes;
105 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
107 if ((filename!=NULL) && (dwDesiredAccess != 0))
109 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition,
110 dwFlagsAndAttributes, NULL);
113 return win32_build_iowin(hFile);
117 voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,
const void* filename,
int mode)
119 const char* mode_fopen = NULL;
120 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes;
123 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
125 if ((filename!=NULL) && (dwDesiredAccess != 0))
127 hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition,
128 dwFlagsAndAttributes, NULL);
131 return win32_build_iowin(hFile);
135 voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,
const void* filename,
int mode)
137 const char* mode_fopen = NULL;
138 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes;
141 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
143 if ((filename!=NULL) && (dwDesiredAccess != 0))
145 hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition,
146 dwFlagsAndAttributes, NULL);
149 return win32_build_iowin(hFile);
153 voidpf ZCALLBACK win32_open_file_func (voidpf opaque,
const char* filename,
int mode)
155 const char* mode_fopen = NULL;
156 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes;
159 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
161 if ((filename!=NULL) && (dwDesiredAccess != 0))
163 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition,
164 dwFlagsAndAttributes, NULL);
167 return win32_build_iowin(hFile);
171 uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream,
void* buf,uLong size)
177 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
182 if (!ReadFile(hFile, buf, size, &ret, NULL))
184 DWORD dwErr = GetLastError();
185 if (dwErr == ERROR_HANDLE_EOF)
189 ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr;
197 uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,
const void* buf,uLong size)
203 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
208 if (!WriteFile(hFile, buf, size, &ret, NULL))
210 DWORD dwErr = GetLastError();
211 if (dwErr == ERROR_HANDLE_EOF)
215 ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr;
222 long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
228 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
232 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
233 if (dwSet == INVALID_SET_FILE_POINTER)
235 DWORD dwErr = GetLastError();
236 ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr;
247 ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
249 ZPOS64_T ret = (ZPOS64_T)-1;
253 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
260 li.u.LowPart = SetFilePointer(hFile, li.u.LowPart, &li.u.HighPart, FILE_CURRENT);
261 if ( (li.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
263 DWORD dwErr = GetLastError();
264 ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr;
276 long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,
int origin)
278 DWORD dwMoveMethod = 0xFFFFFFFF;
284 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
288 case ZLIB_FILEFUNC_SEEK_CUR:
289 dwMoveMethod = FILE_CURRENT;
291 case ZLIB_FILEFUNC_SEEK_END:
292 dwMoveMethod = FILE_END;
294 case ZLIB_FILEFUNC_SEEK_SET:
295 dwMoveMethod = FILE_BEGIN;
302 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
303 if (dwSet == INVALID_SET_FILE_POINTER)
305 DWORD dwErr = GetLastError();
306 ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr;
317 long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,
int origin)
319 DWORD dwMoveMethod = 0xFFFFFFFF;
325 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
330 case ZLIB_FILEFUNC_SEEK_CUR:
331 dwMoveMethod = FILE_CURRENT;
333 case ZLIB_FILEFUNC_SEEK_END:
334 dwMoveMethod = FILE_END;
336 case ZLIB_FILEFUNC_SEEK_SET:
337 dwMoveMethod = FILE_BEGIN;
344 LARGE_INTEGER* li = (LARGE_INTEGER*)&offset;
345 DWORD dwSet = SetFilePointer(hFile, li->u.LowPart, &li->u.HighPart, dwMoveMethod);
346 if (dwSet == INVALID_SET_FILE_POINTER)
348 DWORD dwErr = GetLastError();
349 ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr;
360 int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
367 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
378 int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
383 ret = ((WIN32FILE_IOWIN*)stream)->error;
390 pzlib_filefunc_def->zopen_file = win32_open_file_func;
391 pzlib_filefunc_def->zread_file = win32_read_file_func;
392 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
393 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
394 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
395 pzlib_filefunc_def->zclose_file = win32_close_file_func;
396 pzlib_filefunc_def->zerror_file = win32_error_file_func;
397 pzlib_filefunc_def->opaque = NULL;
402 pzlib_filefunc_def->zopen64_file = win32_open64_file_func;
403 pzlib_filefunc_def->zread_file = win32_read_file_func;
404 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
405 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
406 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
407 pzlib_filefunc_def->zclose_file = win32_close_file_func;
408 pzlib_filefunc_def->zerror_file = win32_error_file_func;
409 pzlib_filefunc_def->opaque = NULL;
415 pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA;
416 pzlib_filefunc_def->zread_file = win32_read_file_func;
417 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
418 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
419 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
420 pzlib_filefunc_def->zclose_file = win32_close_file_func;
421 pzlib_filefunc_def->zerror_file = win32_error_file_func;
422 pzlib_filefunc_def->opaque = NULL;
428 pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW;
429 pzlib_filefunc_def->zread_file = win32_read_file_func;
430 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
431 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
432 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
433 pzlib_filefunc_def->zclose_file = win32_close_file_func;
434 pzlib_filefunc_def->zerror_file = win32_error_file_func;
435 pzlib_filefunc_def->opaque = NULL;