00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdlib.h>
00011
00012 #include "zlib.h"
00013 #include "ioapi.h"
00014 #include "iowin32.h"
00015
00016 #ifndef INVALID_HANDLE_VALUE
00017 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
00018 #endif
00019
00020 #ifndef INVALID_SET_FILE_POINTER
00021 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
00022 #endif
00023
00024 voidpf ZCALLBACK win32_open_file_func OF((
00025 voidpf opaque,
00026 const char* filename,
00027 int mode));
00028
00029 uLong ZCALLBACK win32_read_file_func OF((
00030 voidpf opaque,
00031 voidpf stream,
00032 void* buf,
00033 uLong size));
00034
00035 uLong ZCALLBACK win32_write_file_func OF((
00036 voidpf opaque,
00037 voidpf stream,
00038 const void* buf,
00039 uLong size));
00040
00041 long ZCALLBACK win32_tell_file_func OF((
00042 voidpf opaque,
00043 voidpf stream));
00044
00045 long ZCALLBACK win32_seek_file_func OF((
00046 voidpf opaque,
00047 voidpf stream,
00048 uLong offset,
00049 int origin));
00050
00051 int ZCALLBACK win32_close_file_func OF((
00052 voidpf opaque,
00053 voidpf stream));
00054
00055 int ZCALLBACK win32_error_file_func OF((
00056 voidpf opaque,
00057 voidpf stream));
00058
00059 typedef struct
00060 {
00061 HANDLE hf;
00062 int error;
00063 } WIN32FILE_IOWIN;
00064
00065 voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode)
00066 voidpf opaque;
00067 const char* filename;
00068 int mode;
00069 {
00070 const char* mode_fopen = NULL;
00071 DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
00072 HANDLE hFile = 0;
00073 voidpf ret=NULL;
00074
00075 dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
00076
00077 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
00078 {
00079 dwDesiredAccess = GENERIC_READ;
00080 dwCreationDisposition = OPEN_EXISTING;
00081 dwShareMode = FILE_SHARE_READ;
00082 }
00083 else
00084 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
00085 {
00086 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
00087 dwCreationDisposition = OPEN_EXISTING;
00088 }
00089 else
00090 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
00091 {
00092 dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
00093 dwCreationDisposition = CREATE_ALWAYS;
00094 }
00095
00096 if ((filename!=NULL) && (dwDesiredAccess != 0))
00097 hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
00098 dwCreationDisposition, dwFlagsAndAttributes, NULL);
00099
00100 if (hFile == INVALID_HANDLE_VALUE)
00101 hFile = NULL;
00102
00103 if (hFile != NULL)
00104 {
00105 WIN32FILE_IOWIN w32fiow;
00106 w32fiow.hf = hFile;
00107 w32fiow.error = 0;
00108 ret = malloc(sizeof(WIN32FILE_IOWIN));
00109 if (ret==NULL)
00110 CloseHandle(hFile);
00111 else *((WIN32FILE_IOWIN*)ret) = w32fiow;
00112 }
00113 return ret;
00114 }
00115
00116
00117 uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size)
00118 voidpf opaque;
00119 voidpf stream;
00120 void* buf;
00121 uLong size;
00122 {
00123 uLong ret=0;
00124 HANDLE hFile = NULL;
00125 if (stream!=NULL)
00126 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00127 if (hFile != NULL)
00128 if (!ReadFile(hFile, buf, size, &ret, NULL))
00129 {
00130 DWORD dwErr = GetLastError();
00131 if (dwErr == ERROR_HANDLE_EOF)
00132 dwErr = 0;
00133 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00134 }
00135
00136 return ret;
00137 }
00138
00139
00140 uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size)
00141 voidpf opaque;
00142 voidpf stream;
00143 const void* buf;
00144 uLong size;
00145 {
00146 uLong ret=0;
00147 HANDLE hFile = NULL;
00148 if (stream!=NULL)
00149 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00150
00151 if (hFile !=NULL)
00152 if (!WriteFile(hFile, buf, size, &ret, NULL))
00153 {
00154 DWORD dwErr = GetLastError();
00155 if (dwErr == ERROR_HANDLE_EOF)
00156 dwErr = 0;
00157 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00158 }
00159
00160 return ret;
00161 }
00162
00163 long ZCALLBACK win32_tell_file_func (opaque, stream)
00164 voidpf opaque;
00165 voidpf stream;
00166 {
00167 long ret=-1;
00168 HANDLE hFile = NULL;
00169 if (stream!=NULL)
00170 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00171 if (hFile != NULL)
00172 {
00173 DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
00174 if (dwSet == INVALID_SET_FILE_POINTER)
00175 {
00176 DWORD dwErr = GetLastError();
00177 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00178 ret = -1;
00179 }
00180 else
00181 ret=(long)dwSet;
00182 }
00183 return ret;
00184 }
00185
00186 long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin)
00187 voidpf opaque;
00188 voidpf stream;
00189 uLong offset;
00190 int origin;
00191 {
00192 DWORD dwMoveMethod=0xFFFFFFFF;
00193 HANDLE hFile = NULL;
00194
00195 long ret=-1;
00196 if (stream!=NULL)
00197 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00198 switch (origin)
00199 {
00200 case ZLIB_FILEFUNC_SEEK_CUR :
00201 dwMoveMethod = FILE_CURRENT;
00202 break;
00203 case ZLIB_FILEFUNC_SEEK_END :
00204 dwMoveMethod = FILE_END;
00205 break;
00206 case ZLIB_FILEFUNC_SEEK_SET :
00207 dwMoveMethod = FILE_BEGIN;
00208 break;
00209 default: return -1;
00210 }
00211
00212 if (hFile != NULL)
00213 {
00214 DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
00215 if (dwSet == INVALID_SET_FILE_POINTER)
00216 {
00217 DWORD dwErr = GetLastError();
00218 ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
00219 ret = -1;
00220 }
00221 else
00222 ret=0;
00223 }
00224 return ret;
00225 }
00226
00227 int ZCALLBACK win32_close_file_func (opaque, stream)
00228 voidpf opaque;
00229 voidpf stream;
00230 {
00231 int ret=-1;
00232
00233 if (stream!=NULL)
00234 {
00235 HANDLE hFile;
00236 hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
00237 if (hFile != NULL)
00238 {
00239 CloseHandle(hFile);
00240 ret=0;
00241 }
00242 free(stream);
00243 }
00244 return ret;
00245 }
00246
00247 int ZCALLBACK win32_error_file_func (opaque, stream)
00248 voidpf opaque;
00249 voidpf stream;
00250 {
00251 int ret=-1;
00252 if (stream!=NULL)
00253 {
00254 ret = ((WIN32FILE_IOWIN*)stream) -> error;
00255 }
00256 return ret;
00257 }
00258
00259 void fill_win32_filefunc (pzlib_filefunc_def)
00260 zlib_filefunc_def* pzlib_filefunc_def;
00261 {
00262 pzlib_filefunc_def->zopen_file = win32_open_file_func;
00263 pzlib_filefunc_def->zread_file = win32_read_file_func;
00264 pzlib_filefunc_def->zwrite_file = win32_write_file_func;
00265 pzlib_filefunc_def->ztell_file = win32_tell_file_func;
00266 pzlib_filefunc_def->zseek_file = win32_seek_file_func;
00267 pzlib_filefunc_def->zclose_file = win32_close_file_func;
00268 pzlib_filefunc_def->zerror_file = win32_error_file_func;
00269 pzlib_filefunc_def->opaque=NULL;
00270 }