]> git.lyx.org Git - lyx.git/blob - src/support/minizip/iowin32.c
Introduce FileName::changePermission() and fix ConverterCache.
[lyx.git] / src / support / minizip / iowin32.c
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)
4
5    Version 1.01e, February 12th, 2005
6
7    Copyright (C) 1998-2005 Gilles Vollant
8 */
9
10 #if defined(WIN32) || defined(_WIN32)
11
12 #include <stdlib.h>
13
14 #include "zlib.h"
15 #include "ioapi.h"
16 #include "iowin32.h"
17
18 #ifndef INVALID_HANDLE_VALUE
19 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
20 #endif
21
22 #ifndef INVALID_SET_FILE_POINTER
23 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
24 #endif
25
26 voidpf ZCALLBACK win32_open_file_func OF((
27    voidpf opaque,
28    const char* filename,
29    int mode));
30
31 uLong ZCALLBACK win32_read_file_func OF((
32    voidpf opaque,
33    voidpf stream,
34    void* buf,
35    uLong size));
36
37 uLong ZCALLBACK win32_write_file_func OF((
38    voidpf opaque,
39    voidpf stream,
40    const void* buf,
41    uLong size));
42
43 long ZCALLBACK win32_tell_file_func OF((
44    voidpf opaque,
45    voidpf stream));
46
47 long ZCALLBACK win32_seek_file_func OF((
48    voidpf opaque,
49    voidpf stream,
50    uLong offset,
51    int origin));
52
53 int ZCALLBACK win32_close_file_func OF((
54    voidpf opaque,
55    voidpf stream));
56
57 int ZCALLBACK win32_error_file_func OF((
58    voidpf opaque,
59    voidpf stream));
60
61 typedef struct
62 {
63     HANDLE hf;
64     int error;
65 } WIN32FILE_IOWIN;
66
67 voidpf ZCALLBACK win32_open_file_func(voidpf opaque,
68                                       const char * filename,
69                                       int mode)
70 {
71     //const char* mode_fopen = NULL;
72     DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
73     HANDLE hFile = 0;
74     voidpf ret=NULL;
75
76     dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
77
78     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
79     {
80         dwDesiredAccess = GENERIC_READ;
81         dwCreationDisposition = OPEN_EXISTING;
82         dwShareMode = FILE_SHARE_READ;
83     }
84     else
85     if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
86     {
87         dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
88         dwCreationDisposition = OPEN_EXISTING;
89     }
90     else
91     if (mode & ZLIB_FILEFUNC_MODE_CREATE)
92     {
93         dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
94         dwCreationDisposition = CREATE_ALWAYS;
95     }
96
97     if ((filename!=NULL) && (dwDesiredAccess != 0))
98         hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
99                       dwCreationDisposition, dwFlagsAndAttributes, NULL);
100
101     if (hFile == INVALID_HANDLE_VALUE)
102         hFile = NULL;
103
104     if (hFile != NULL)
105     {
106         WIN32FILE_IOWIN w32fiow;
107         w32fiow.hf = hFile;
108         w32fiow.error = 0;
109         ret = malloc(sizeof(WIN32FILE_IOWIN));
110         if (ret==NULL)
111             CloseHandle(hFile);
112         else *((WIN32FILE_IOWIN*)ret) = w32fiow;
113     }
114     return ret;
115 }
116
117
118 uLong ZCALLBACK win32_read_file_func(voidpf opaque,
119                                      voidpf stream,
120                                      void * buf,
121                                      uLong size)
122 {
123     uLong ret=0;
124     HANDLE hFile = NULL;
125     if (stream!=NULL)
126         hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
127     if (hFile != NULL)
128         if (!ReadFile(hFile, buf, size, &ret, NULL))
129         {
130             DWORD dwErr = GetLastError();
131             if (dwErr == ERROR_HANDLE_EOF)
132                 dwErr = 0;
133             ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
134         }
135
136     return ret;
137 }
138
139
140 uLong ZCALLBACK win32_write_file_func(voidpf opaque,
141                                       voidpf stream,
142                                       const void * buf,
143                                       uLong size)
144 {
145     uLong ret=0;
146     HANDLE hFile = NULL;
147     if (stream!=NULL)
148         hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
149
150     if (hFile !=NULL)
151         if (!WriteFile(hFile, buf, size, &ret, NULL))
152         {
153             DWORD dwErr = GetLastError();
154             if (dwErr == ERROR_HANDLE_EOF)
155                 dwErr = 0;
156             ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
157         }
158
159     return ret;
160 }
161
162 long ZCALLBACK win32_tell_file_func(voidpf opaque, voidpf stream)
163 {
164     long ret=-1;
165     HANDLE hFile = NULL;
166     if (stream!=NULL)
167         hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
168     if (hFile != NULL)
169     {
170         DWORD dwSet = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
171         if (dwSet == INVALID_SET_FILE_POINTER)
172         {
173             DWORD dwErr = GetLastError();
174             ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
175             ret = -1;
176         }
177         else
178             ret=(long)dwSet;
179     }
180     return ret;
181 }
182
183 long ZCALLBACK win32_seek_file_func(voidpf opaque,
184                                     voidpf stream,
185                                     uLong offset,
186                                     int origin)
187 {
188     DWORD dwMoveMethod=0xFFFFFFFF;
189     HANDLE hFile = NULL;
190
191     long ret=-1;
192     if (stream!=NULL)
193         hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
194     switch (origin)
195     {
196     case ZLIB_FILEFUNC_SEEK_CUR :
197         dwMoveMethod = FILE_CURRENT;
198         break;
199     case ZLIB_FILEFUNC_SEEK_END :
200         dwMoveMethod = FILE_END;
201         break;
202     case ZLIB_FILEFUNC_SEEK_SET :
203         dwMoveMethod = FILE_BEGIN;
204         break;
205     default: return -1;
206     }
207
208     if (hFile != NULL)
209     {
210         DWORD dwSet = SetFilePointer(hFile, offset, NULL, dwMoveMethod);
211         if (dwSet == INVALID_SET_FILE_POINTER)
212         {
213             DWORD dwErr = GetLastError();
214             ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
215             ret = -1;
216         }
217         else
218             ret=0;
219     }
220     return ret;
221 }
222
223 int ZCALLBACK win32_close_file_func(voidpf opaque, voidpf stream)
224 {
225     int ret=-1;
226
227     if (stream!=NULL)
228     {
229         HANDLE hFile;
230         hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
231         if (hFile != NULL)
232         {
233             CloseHandle(hFile);
234             ret=0;
235         }
236         free(stream);
237     }
238     return ret;
239 }
240
241 int ZCALLBACK win32_error_file_func(voidpf opaque, voidpf stream)
242 {
243     int ret=-1;
244     if (stream!=NULL)
245     {
246         ret = ((WIN32FILE_IOWIN*)stream) -> error;
247     }
248     return ret;
249 }
250
251 void fill_win32_filefunc(zlib_filefunc_def * pzlib_filefunc_def)
252 {
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;
261 }
262
263 #endif