1 /* iowin32.c -- IO base function header for compress/uncompress .zip
2 files using zlib + zip or unzip API
3 This IO API version uses the Win32 API (for Microsoft Windows)
5 Version 1.01e, February 12th, 2005
7 Copyright (C) 1998-2005 Gilles Vollant
10 #if defined(WIN32) || defined(_WIN32)
18 #ifndef INVALID_HANDLE_VALUE
19 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
22 #ifndef INVALID_SET_FILE_POINTER
23 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
26 voidpf ZCALLBACK win32_open_file_func OF((
31 uLong ZCALLBACK win32_read_file_func OF((
37 uLong ZCALLBACK win32_write_file_func OF((
43 long ZCALLBACK win32_tell_file_func OF((
47 long ZCALLBACK win32_seek_file_func OF((
53 int ZCALLBACK win32_close_file_func OF((
57 int ZCALLBACK win32_error_file_func OF((
67 voidpf ZCALLBACK win32_open_file_func(voidpf opaque,
68 const char * filename,
71 //const char* mode_fopen = NULL;
72 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
76 dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
78 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
80 dwDesiredAccess = GENERIC_READ;
81 dwCreationDisposition = OPEN_EXISTING;
82 dwShareMode = FILE_SHARE_READ;
85 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
87 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
88 dwCreationDisposition = OPEN_EXISTING;
91 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
93 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
94 dwCreationDisposition = CREATE_ALWAYS;
97 if ((filename!=NULL) && (dwDesiredAccess != 0))
98 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
99 dwCreationDisposition, dwFlagsAndAttributes, NULL);
101 if (hFile == INVALID_HANDLE_VALUE)
106 WIN32FILE_IOWIN w32fiow;
109 ret = malloc(sizeof(WIN32FILE_IOWIN));
112 else *((WIN32FILE_IOWIN*)ret) = w32fiow;
118 uLong ZCALLBACK win32_read_file_func(voidpf opaque,
126 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
128 if (!ReadFile(hFile, buf, size, &ret, NULL))
130 DWORD dwErr = GetLastError();
131 if (dwErr == ERROR_HANDLE_EOF)
133 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
140 uLong ZCALLBACK win32_write_file_func(voidpf opaque,
148 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
151 if (!WriteFile(hFile, buf, size, &ret, NULL))
153 DWORD dwErr = GetLastError();
154 if (dwErr == ERROR_HANDLE_EOF)
156 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
162 long ZCALLBACK win32_tell_file_func(voidpf opaque, voidpf stream)
167 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
170 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
171 if (dwSet == INVALID_SET_FILE_POINTER)
173 DWORD dwErr = GetLastError();
174 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
183 long ZCALLBACK win32_seek_file_func(voidpf opaque,
188 DWORD dwMoveMethod=0xFFFFFFFF;
193 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
196 case ZLIB_FILEFUNC_SEEK_CUR :
197 dwMoveMethod = FILE_CURRENT;
199 case ZLIB_FILEFUNC_SEEK_END :
200 dwMoveMethod = FILE_END;
202 case ZLIB_FILEFUNC_SEEK_SET :
203 dwMoveMethod = FILE_BEGIN;
210 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
211 if (dwSet == INVALID_SET_FILE_POINTER)
213 DWORD dwErr = GetLastError();
214 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
223 int ZCALLBACK win32_close_file_func(voidpf opaque, voidpf stream)
230 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
241 int ZCALLBACK win32_error_file_func(voidpf opaque, voidpf stream)
246 ret = ((WIN32FILE_IOWIN*)stream) -> error;
251 void fill_win32_filefunc(zlib_filefunc_def * pzlib_filefunc_def)
253 pzlib_filefunc_def->zopen_file = win32_open_file_func;
254 pzlib_filefunc_def->zread_file = win32_read_file_func;
255 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
256 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
257 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
258 pzlib_filefunc_def->zclose_file = win32_close_file_func;
259 pzlib_filefunc_def->zerror_file = win32_error_file_func;
260 pzlib_filefunc_def->opaque=NULL;