etc/services - sync with NetBSD-8
[minix.git] / common / dist / zlib / contrib / minizip / iowin32.c
blobe01b1b4013d213ef1dd075432026269e85a34947
1 /* $NetBSD: iowin32.c,v 1.1.1.1 2006/01/14 20:10:57 christos Exp $ */
3 /* iowin32.c -- IO base function header for compress/uncompress .zip
4 files using zlib + zip or unzip API
5 This IO API version uses the Win32 API (for Microsoft Windows)
7 Version 1.01e, February 12th, 2005
9 Copyright (C) 1998-2005 Gilles Vollant
12 #include <stdlib.h>
14 #include "zlib.h"
15 #include "ioapi.h"
16 #include "iowin32.h"
18 #ifndef INVALID_HANDLE_VALUE
19 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
20 #endif
22 #ifndef INVALID_SET_FILE_POINTER
23 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
24 #endif
26 voidpf ZCALLBACK win32_open_file_func OF((
27 voidpf opaque,
28 const char* filename,
29 int mode));
31 uLong ZCALLBACK win32_read_file_func OF((
32 voidpf opaque,
33 voidpf stream,
34 void* buf,
35 uLong size));
37 uLong ZCALLBACK win32_write_file_func OF((
38 voidpf opaque,
39 voidpf stream,
40 const void* buf,
41 uLong size));
43 long ZCALLBACK win32_tell_file_func OF((
44 voidpf opaque,
45 voidpf stream));
47 long ZCALLBACK win32_seek_file_func OF((
48 voidpf opaque,
49 voidpf stream,
50 uLong offset,
51 int origin));
53 int ZCALLBACK win32_close_file_func OF((
54 voidpf opaque,
55 voidpf stream));
57 int ZCALLBACK win32_error_file_func OF((
58 voidpf opaque,
59 voidpf stream));
61 typedef struct
63 HANDLE hf;
64 int error;
65 } WIN32FILE_IOWIN;
67 voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode)
68 voidpf opaque;
69 const char* filename;
70 int mode;
72 const char* mode_fopen = NULL;
73 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
74 HANDLE hFile = 0;
75 voidpf ret=NULL;
77 dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
79 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
81 dwDesiredAccess = GENERIC_READ;
82 dwCreationDisposition = OPEN_EXISTING;
83 dwShareMode = FILE_SHARE_READ;
85 else
86 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
88 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
89 dwCreationDisposition = OPEN_EXISTING;
91 else
92 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
94 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
95 dwCreationDisposition = CREATE_ALWAYS;
98 if ((filename!=NULL) && (dwDesiredAccess != 0))
99 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
100 dwCreationDisposition, dwFlagsAndAttributes, NULL);
102 if (hFile == INVALID_HANDLE_VALUE)
103 hFile = NULL;
105 if (hFile != NULL)
107 WIN32FILE_IOWIN w32fiow;
108 w32fiow.hf = hFile;
109 w32fiow.error = 0;
110 ret = malloc(sizeof(WIN32FILE_IOWIN));
111 if (ret==NULL)
112 CloseHandle(hFile);
113 else *((WIN32FILE_IOWIN*)ret) = w32fiow;
115 return ret;
119 uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size)
120 voidpf opaque;
121 voidpf stream;
122 void* buf;
123 uLong size;
125 uLong ret=0;
126 HANDLE hFile = NULL;
127 if (stream!=NULL)
128 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
129 if (hFile != NULL)
130 if (!ReadFile(hFile, buf, size, &ret, NULL))
132 DWORD dwErr = GetLastError();
133 if (dwErr == ERROR_HANDLE_EOF)
134 dwErr = 0;
135 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
138 return ret;
142 uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size)
143 voidpf opaque;
144 voidpf stream;
145 const void* buf;
146 uLong size;
148 uLong ret=0;
149 HANDLE hFile = NULL;
150 if (stream!=NULL)
151 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
153 if (hFile !=NULL)
154 if (!WriteFile(hFile, buf, size, &ret, NULL))
156 DWORD dwErr = GetLastError();
157 if (dwErr == ERROR_HANDLE_EOF)
158 dwErr = 0;
159 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
162 return ret;
165 long ZCALLBACK win32_tell_file_func (opaque, stream)
166 voidpf opaque;
167 voidpf stream;
169 long ret=-1;
170 HANDLE hFile = NULL;
171 if (stream!=NULL)
172 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
173 if (hFile != NULL)
175 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
176 if (dwSet == INVALID_SET_FILE_POINTER)
178 DWORD dwErr = GetLastError();
179 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
180 ret = -1;
182 else
183 ret=(long)dwSet;
185 return ret;
188 long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin)
189 voidpf opaque;
190 voidpf stream;
191 uLong offset;
192 int origin;
194 DWORD dwMoveMethod=0xFFFFFFFF;
195 HANDLE hFile = NULL;
197 long ret=-1;
198 if (stream!=NULL)
199 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
200 switch (origin)
202 case ZLIB_FILEFUNC_SEEK_CUR :
203 dwMoveMethod = FILE_CURRENT;
204 break;
205 case ZLIB_FILEFUNC_SEEK_END :
206 dwMoveMethod = FILE_END;
207 break;
208 case ZLIB_FILEFUNC_SEEK_SET :
209 dwMoveMethod = FILE_BEGIN;
210 break;
211 default: return -1;
214 if (hFile != NULL)
216 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
217 if (dwSet == INVALID_SET_FILE_POINTER)
219 DWORD dwErr = GetLastError();
220 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
221 ret = -1;
223 else
224 ret=0;
226 return ret;
229 int ZCALLBACK win32_close_file_func (opaque, stream)
230 voidpf opaque;
231 voidpf stream;
233 int ret=-1;
235 if (stream!=NULL)
237 HANDLE hFile;
238 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
239 if (hFile != NULL)
241 CloseHandle(hFile);
242 ret=0;
244 free(stream);
246 return ret;
249 int ZCALLBACK win32_error_file_func (opaque, stream)
250 voidpf opaque;
251 voidpf stream;
253 int ret=-1;
254 if (stream!=NULL)
256 ret = ((WIN32FILE_IOWIN*)stream) -> error;
258 return ret;
261 void fill_win32_filefunc (pzlib_filefunc_def)
262 zlib_filefunc_def* pzlib_filefunc_def;
264 pzlib_filefunc_def->zopen_file = win32_open_file_func;
265 pzlib_filefunc_def->zread_file = win32_read_file_func;
266 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
267 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
268 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
269 pzlib_filefunc_def->zclose_file = win32_close_file_func;
270 pzlib_filefunc_def->zerror_file = win32_error_file_func;
271 pzlib_filefunc_def->opaque=NULL;