-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
mz_strm_os_win32.c
280 lines (218 loc) · 8.03 KB
/
mz_strm_os_win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* mz_strm_win32.c -- Stream for filesystem access for windows
part of the minizip-ng project
Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_os.h"
#include <windows.h>
/***************************************************************************/
#ifndef INVALID_HANDLE_VALUE
# define INVALID_HANDLE_VALUE 0xFFFFFFFF
#endif
#ifndef INVALID_SET_FILE_POINTER
# define INVALID_SET_FILE_POINTER (DWORD)(-1)
#endif
#ifndef _WIN32_WINNT_WIN8
# define _WIN32_WINNT_WIN8 0x0602
#endif
/***************************************************************************/
static mz_stream_vtbl mz_stream_os_vtbl = {mz_stream_os_open,
mz_stream_os_is_open,
mz_stream_os_read,
mz_stream_os_write,
mz_stream_os_tell,
mz_stream_os_seek,
mz_stream_os_close,
mz_stream_os_error,
mz_stream_os_create,
mz_stream_os_delete,
NULL,
NULL};
/***************************************************************************/
typedef struct mz_stream_win32_s {
mz_stream stream;
HANDLE handle;
int32_t error;
} mz_stream_win32;
/***************************************************************************/
#if 0
# define mz_stream_os_print printf
#else
# define mz_stream_os_print(fmt, ...)
#endif
/***************************************************************************/
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t desired_access = 0;
uint32_t creation_disposition = 0;
uint32_t share_mode = FILE_SHARE_READ;
uint32_t flags_attribs = FILE_ATTRIBUTE_NORMAL;
wchar_t *path_wide = NULL;
if (!path)
return MZ_PARAM_ERROR;
/* Some use cases require write sharing as well */
share_mode |= FILE_SHARE_WRITE;
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ) {
desired_access = GENERIC_READ;
creation_disposition = OPEN_EXISTING;
} else if (mode & MZ_OPEN_MODE_APPEND) {
desired_access = GENERIC_WRITE | GENERIC_READ;
creation_disposition = OPEN_EXISTING;
} else if (mode & MZ_OPEN_MODE_CREATE) {
desired_access = GENERIC_WRITE | GENERIC_READ;
creation_disposition = CREATE_ALWAYS;
} else {
return MZ_PARAM_ERROR;
}
mz_stream_os_print("Win32 - Open - %s (mode %" PRId32 ")\n", path);
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
win32->handle = CreateFile2(path_wide, desired_access, share_mode, creation_disposition, NULL);
#else
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL, creation_disposition, flags_attribs, NULL);
#endif
mz_os_unicode_string_delete(&path_wide);
if (mz_stream_os_is_open(stream) != MZ_OK) {
win32->error = GetLastError();
return MZ_OPEN_ERROR;
}
if (mode & MZ_OPEN_MODE_APPEND)
return mz_stream_os_seek(stream, 0, MZ_SEEK_END);
return MZ_OK;
}
int32_t mz_stream_os_is_open(void *stream) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
if (!win32->handle || win32->handle == INVALID_HANDLE_VALUE)
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t read = 0;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (!ReadFile(win32->handle, buf, size, (DWORD *)&read, NULL)) {
win32->error = GetLastError();
if (win32->error == ERROR_HANDLE_EOF)
win32->error = 0;
}
mz_stream_os_print("Win32 - Read - %" PRId32 "\n", read);
return read;
}
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
int32_t written = 0;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (!WriteFile(win32->handle, buf, size, (DWORD *)&written, NULL)) {
win32->error = GetLastError();
if (win32->error == ERROR_HANDLE_EOF)
win32->error = 0;
}
mz_stream_os_print("Win32 - Write - %" PRId32 "\n", written);
return written;
}
static int32_t mz_stream_os_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos,
uint32_t move_method) {
#if _WIN32_WINNT >= _WIN32_WINNT_WINXP
BOOL success = FALSE;
success = SetFilePointerEx(handle, large_pos, new_pos, move_method);
if ((success == FALSE) && (GetLastError() != NO_ERROR))
return MZ_SEEK_ERROR;
return MZ_OK;
#else
LONG high_part = 0;
uint32_t pos = 0;
high_part = large_pos.HighPart;
pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
if ((pos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
return MZ_SEEK_ERROR;
if (new_pos) {
new_pos->LowPart = pos;
new_pos->HighPart = high_part;
}
return MZ_OK;
#endif
}
int64_t mz_stream_os_tell(void *stream) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
LARGE_INTEGER large_pos;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
large_pos.QuadPart = 0;
if (mz_stream_os_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) != MZ_OK)
win32->error = GetLastError();
mz_stream_os_print("Win32 - Tell - %" PRId64 "\n", large_pos.QuadPart);
return large_pos.QuadPart;
}
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t move_method = 0xFFFFFFFF;
int32_t err = MZ_OK;
LARGE_INTEGER large_pos;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
switch (origin) {
case MZ_SEEK_CUR:
move_method = FILE_CURRENT;
break;
case MZ_SEEK_END:
move_method = FILE_END;
break;
case MZ_SEEK_SET:
move_method = FILE_BEGIN;
break;
default:
return MZ_SEEK_ERROR;
}
mz_stream_os_print("Win32 - Seek - %" PRId64 " (origin %" PRId32 ")\n", offset, origin);
large_pos.QuadPart = offset;
err = mz_stream_os_seekinternal(win32->handle, large_pos, NULL, move_method);
if (err != MZ_OK) {
win32->error = GetLastError();
return err;
}
return MZ_OK;
}
int32_t mz_stream_os_close(void *stream) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
if (win32->handle)
CloseHandle(win32->handle);
mz_stream_os_print("Win32 - Close\n");
win32->handle = NULL;
return MZ_OK;
}
int32_t mz_stream_os_error(void *stream) {
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
return win32->error;
}
void *mz_stream_os_create(void) {
mz_stream_win32 *win32 = (mz_stream_win32 *)calloc(1, sizeof(mz_stream_win32));
if (win32)
win32->stream.vtbl = &mz_stream_os_vtbl;
return win32;
}
void mz_stream_os_delete(void **stream) {
mz_stream_win32 *win32 = NULL;
if (!stream)
return;
win32 = (mz_stream_win32 *)*stream;
if (win32)
free(win32);
*stream = NULL;
}
void *mz_stream_os_get_interface(void) {
return (void *)&mz_stream_os_vtbl;
}