00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stdlib.h>
00015
00016 #include "zlib.h"
00017 #include "ioapi.h"
00018 #include "iowin32.h"
00019
00020 #ifndef INVALID_HANDLE_VALUE
00021 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
00022 #endif
00023
00024 #ifndef INVALID_SET_FILE_POINTER
00025 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
00026 #endif
00027
00028 voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque, const char* filename, int mode));
00029 uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
00030 uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
00031 ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream));
00032 long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
00033 int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream));
00034 int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream));
00035
00036 typedef struct
00037 {
00038 HANDLE hf;
00039 int error;
00040 } WIN32FILE_IOWIN;
00041
00042
00043 static void win32_translate_open_mode(int mode,
00044 DWORD* lpdwDesiredAccess,
00045 DWORD* lpdwCreationDisposition,
00046 DWORD* lpdwShareMode,
00047 DWORD* lpdwFlagsAndAttributes)
00048 {
00049 *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0;
00050
00051 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
00052 {
00053 *lpdwDesiredAccess = GENERIC_READ;
00054 *lpdwCreationDisposition = OPEN_EXISTING;
00055 *lpdwShareMode = FILE_SHARE_READ;
00056 }
00057 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
00058 {
00059 *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
00060 *lpdwCreationDisposition = OPEN_EXISTING;
00061 }
00062 else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
00063 {
00064 *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
00065 *lpdwCreationDisposition = CREATE_ALWAYS;
00066 }
00067 }
00068
00069 static voidpf win32_build_iowin(HANDLE hFile)
00070 {
00071 voidpf ret=NULL;
00072
00073 if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
00074 {
00075 WIN32FILE_IOWIN w32fiow;
00076 w32fiow.hf = hFile;
00077 w32fiow.error = 0;
00078 ret = malloc(sizeof(WIN32FILE_IOWIN));
00079
00080 if (ret==NULL)
00081 CloseHandle(hFile);
00082 else
00083 *((WIN32FILE_IOWIN*)ret) = w32fiow;
00084 }
00085 return ret;
00086 }
00087
00088 voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,const void* filename,int mode)
00089 {
00090 const char* mode_fopen = NULL;
00091 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
00092 HANDLE hFile = NULL;
00093
00094 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
00095
00096 if ((filename!=NULL) && (dwDesiredAccess != 0))
00097 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
00098
00099 return win32_build_iowin(hFile);
00100 }
00101
00102
00103 voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,const void* filename,int mode)
00104 {
00105 const char* mode_fopen = NULL;
00106 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
00107 HANDLE hFile = NULL;
00108
00109 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
00110
00111 if ((filename!=NULL) && (dwDesiredAccess != 0))
00112 hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
00113
00114 return win32_build_iowin(hFile);
00115 }
00116
00117
00118 voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode)
00119 {
00120 const char* mode_fopen = NULL;
00121 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
00122 HANDLE hFile = NULL;
00123
00124 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
00125
00126 if ((filename!=NULL) && (dwDesiredAccess != 0))
00127 hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
00128
00129 return win32_build_iowin(hFile);
00130 }
00131
00132
00133 voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode)
00134 {
00135 const char* mode_fopen = NULL;
00136 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
00137 HANDLE hFile = NULL;
00138
00139 win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
00140
00141 if ((filename!=NULL) && (dwDesiredAccess != 0))
00142 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
00143
00144 return win32_build_iowin(hFile);
00145 }
00146
00147
00148 uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size)
00149 {
00150 uLong ret=0;
00151 HANDLE hFile = NULL;
00152 if (stream!=NULL)
00153 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00154
00155 if (hFile != NULL)
00156 {
00157 if (!ReadFile(hFile, buf, size, &ret, NULL))
00158 {
00159 DWORD dwErr = GetLastError();
00160 if (dwErr == ERROR_HANDLE_EOF)
00161 dwErr = 0;
00162 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00163 }
00164 }
00165
00166 return ret;
00167 }
00168
00169
00170 uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size)
00171 {
00172 uLong ret=0;
00173 HANDLE hFile = NULL;
00174 if (stream!=NULL)
00175 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00176
00177 if (hFile != NULL)
00178 {
00179 if (!WriteFile(hFile, buf, size, &ret, NULL))
00180 {
00181 DWORD dwErr = GetLastError();
00182 if (dwErr == ERROR_HANDLE_EOF)
00183 dwErr = 0;
00184 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00185 }
00186 }
00187
00188 return ret;
00189 }
00190
00191 long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
00192 {
00193 long ret=-1;
00194 HANDLE hFile = NULL;
00195 if (stream!=NULL)
00196 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00197 if (hFile != NULL)
00198 {
00199 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
00200 if (dwSet == INVALID_SET_FILE_POINTER)
00201 {
00202 DWORD dwErr = GetLastError();
00203 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00204 ret = -1;
00205 }
00206 else
00207 ret=(long)dwSet;
00208 }
00209 return ret;
00210 }
00211
00212 ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
00213 {
00214 ZPOS64_T ret= (ZPOS64_T)-1;
00215 HANDLE hFile = NULL;
00216 if (stream!=NULL)
00217 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
00218
00219 if (hFile)
00220 {
00221 LARGE_INTEGER li;
00222 li.QuadPart = 0;
00223 li.u.LowPart = SetFilePointer(hFile, li.u.LowPart, &li.u.HighPart, FILE_CURRENT);
00224 if ( (li.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
00225 {
00226 DWORD dwErr = GetLastError();
00227 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00228 ret = (ZPOS64_T)-1;
00229 }
00230 else
00231 ret=li.QuadPart;
00232 }
00233 return ret;
00234 }
00235
00236
00237 long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin)
00238 {
00239 DWORD dwMoveMethod=0xFFFFFFFF;
00240 HANDLE hFile = NULL;
00241
00242 long ret=-1;
00243 if (stream!=NULL)
00244 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00245 switch (origin)
00246 {
00247 case ZLIB_FILEFUNC_SEEK_CUR :
00248 dwMoveMethod = FILE_CURRENT;
00249 break;
00250 case ZLIB_FILEFUNC_SEEK_END :
00251 dwMoveMethod = FILE_END;
00252 break;
00253 case ZLIB_FILEFUNC_SEEK_SET :
00254 dwMoveMethod = FILE_BEGIN;
00255 break;
00256 default: return -1;
00257 }
00258
00259 if (hFile != NULL)
00260 {
00261 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
00262 if (dwSet == INVALID_SET_FILE_POINTER)
00263 {
00264 DWORD dwErr = GetLastError();
00265 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00266 ret = -1;
00267 }
00268 else
00269 ret=0;
00270 }
00271 return ret;
00272 }
00273
00274 long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin)
00275 {
00276 DWORD dwMoveMethod=0xFFFFFFFF;
00277 HANDLE hFile = NULL;
00278 long ret=-1;
00279
00280 if (stream!=NULL)
00281 hFile = ((WIN32FILE_IOWIN*)stream)->hf;
00282
00283 switch (origin)
00284 {
00285 case ZLIB_FILEFUNC_SEEK_CUR :
00286 dwMoveMethod = FILE_CURRENT;
00287 break;
00288 case ZLIB_FILEFUNC_SEEK_END :
00289 dwMoveMethod = FILE_END;
00290 break;
00291 case ZLIB_FILEFUNC_SEEK_SET :
00292 dwMoveMethod = FILE_BEGIN;
00293 break;
00294 default: return -1;
00295 }
00296
00297 if (hFile)
00298 {
00299 LARGE_INTEGER* li = (LARGE_INTEGER*)&offset;
00300 DWORD dwSet = SetFilePointer(hFile, li->u.LowPart, &li->u.HighPart, dwMoveMethod);
00301 if (dwSet == INVALID_SET_FILE_POINTER)
00302 {
00303 DWORD dwErr = GetLastError();
00304 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00305 ret = -1;
00306 }
00307 else
00308 ret=0;
00309 }
00310 return ret;
00311 }
00312
00313 int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
00314 {
00315 int ret=-1;
00316
00317 if (stream!=NULL)
00318 {
00319 HANDLE hFile;
00320 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00321 if (hFile != NULL)
00322 {
00323 CloseHandle(hFile);
00324 ret=0;
00325 }
00326 free(stream);
00327 }
00328 return ret;
00329 }
00330
00331 int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
00332 {
00333 int ret=-1;
00334 if (stream!=NULL)
00335 {
00336 ret = ((WIN32FILE_IOWIN*)stream) -> error;
00337 }
00338 return ret;
00339 }
00340
00341 void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
00342 {
00343 pzlib_filefunc_def->zopen_file = win32_open_file_func;
00344 pzlib_filefunc_def->zread_file = win32_read_file_func;
00345 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
00346 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
00347 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
00348 pzlib_filefunc_def->zclose_file = win32_close_file_func;
00349 pzlib_filefunc_def->zerror_file = win32_error_file_func;
00350 pzlib_filefunc_def->opaque = NULL;
00351 }
00352
00353 void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def)
00354 {
00355 pzlib_filefunc_def->zopen64_file = win32_open64_file_func;
00356 pzlib_filefunc_def->zread_file = win32_read_file_func;
00357 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
00358 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
00359 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
00360 pzlib_filefunc_def->zclose_file = win32_close_file_func;
00361 pzlib_filefunc_def->zerror_file = win32_error_file_func;
00362 pzlib_filefunc_def->opaque = NULL;
00363 }
00364
00365
00366 void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def)
00367 {
00368 pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA;
00369 pzlib_filefunc_def->zread_file = win32_read_file_func;
00370 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
00371 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
00372 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
00373 pzlib_filefunc_def->zclose_file = win32_close_file_func;
00374 pzlib_filefunc_def->zerror_file = win32_error_file_func;
00375 pzlib_filefunc_def->opaque = NULL;
00376 }
00377
00378
00379 void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def)
00380 {
00381 pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW;
00382 pzlib_filefunc_def->zread_file = win32_read_file_func;
00383 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
00384 pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
00385 pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
00386 pzlib_filefunc_def->zclose_file = win32_close_file_func;
00387 pzlib_filefunc_def->zerror_file = win32_error_file_func;
00388 pzlib_filefunc_def->opaque = NULL;
00389 }