-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
mz_strm_os_posix.c
196 lines (166 loc) · 5.43 KB
/
mz_strm_os_posix.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
/* mz_strm_posix.c -- Stream for filesystem access for posix/linux
part of the minizip-ng project
Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
Modifications for Zip64 support
Copyright (C) 2009-2010 Mathias Svensson
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_strm.h"
#include "mz_strm_os.h"
#include <stdio.h> /* fopen, fread.. */
#include <errno.h>
/***************************************************************************/
#define fopen64 fopen
#ifndef MZ_FILE32_API
# ifndef NO_FSEEKO
# define ftello64 ftello
# define fseeko64 fseeko
# elif defined(_MSC_VER) && (_MSC_VER >= 1400)
# define ftello64 _ftelli64
# define fseeko64 _fseeki64
# endif
#endif
#ifndef ftello64
# define ftello64 ftell
#endif
#ifndef fseeko64
# define fseeko64 fseek
#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_posix_s {
mz_stream stream;
int32_t error;
FILE *handle;
} mz_stream_posix;
/***************************************************************************/
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
const char *mode_fopen = NULL;
if (!path)
return MZ_PARAM_ERROR;
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
mode_fopen = "rb";
else if (mode & MZ_OPEN_MODE_APPEND)
mode_fopen = "r+b";
else if (mode & MZ_OPEN_MODE_CREATE)
mode_fopen = "wb";
else
return MZ_OPEN_ERROR;
posix->handle = fopen64(path, mode_fopen);
if (!posix->handle) {
posix->error = errno;
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_posix *posix = (mz_stream_posix *)stream;
if (!posix->handle)
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
int32_t read = (int32_t)fread(buf, 1, (size_t)size, posix->handle);
if (read < size && ferror(posix->handle)) {
posix->error = errno;
return MZ_READ_ERROR;
}
return read;
}
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
int32_t written = (int32_t)fwrite(buf, 1, (size_t)size, posix->handle);
if (written < size && ferror(posix->handle)) {
posix->error = errno;
return MZ_WRITE_ERROR;
}
return written;
}
int64_t mz_stream_os_tell(void *stream) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
int64_t position = ftello64(posix->handle);
if (position == -1) {
posix->error = errno;
return MZ_TELL_ERROR;
}
return position;
}
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
int32_t fseek_origin = 0;
switch (origin) {
case MZ_SEEK_CUR:
fseek_origin = SEEK_CUR;
break;
case MZ_SEEK_END:
fseek_origin = SEEK_END;
break;
case MZ_SEEK_SET:
fseek_origin = SEEK_SET;
break;
default:
return MZ_SEEK_ERROR;
}
if (fseeko64(posix->handle, offset, fseek_origin) != 0) {
posix->error = errno;
return MZ_SEEK_ERROR;
}
return MZ_OK;
}
int32_t mz_stream_os_close(void *stream) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
int32_t closed = 0;
if (posix->handle) {
closed = fclose(posix->handle);
posix->handle = NULL;
}
if (closed != 0) {
posix->error = errno;
return MZ_CLOSE_ERROR;
}
return MZ_OK;
}
int32_t mz_stream_os_error(void *stream) {
mz_stream_posix *posix = (mz_stream_posix *)stream;
return posix->error;
}
void *mz_stream_os_create(void) {
mz_stream_posix *posix = (mz_stream_posix *)calloc(1, sizeof(mz_stream_posix));
if (posix)
posix->stream.vtbl = &mz_stream_os_vtbl;
return posix;
}
void mz_stream_os_delete(void **stream) {
mz_stream_posix *posix = NULL;
if (!stream)
return;
posix = (mz_stream_posix *)*stream;
if (posix)
free(posix);
*stream = NULL;
}
void *mz_stream_os_get_interface(void) {
return (void *)&mz_stream_os_vtbl;
}