Debian Codegraph @

mz_strm_os_posix.c

raw symbols SCIP
1 /* mz_strm_posix.c -- Stream for filesystem access for posix/linux
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6 Modifications for Zip64 support
7 Copyright (C) 2009-2010 Mathias Svensson
8 http://result42.com
9 Copyright (C) 1998-2010 Gilles Vollant
10 https://www.winimage.com/zLibDll/minizip.html
11
12 This program is distributed under the terms of the same license as zlib.
13 See the accompanying LICENSE file for the full text of the license.
14 */
15
16 #include "mz.h"
17 #include "mz_strm.h"
18 #include "mz_strm_os.h"
19
20 #include <stdio.h> /* fopen, fread.. */
21 #include <errno.h>
22
23 /***************************************************************************/
24
25 #define fopen64 fopen
26 #ifndef MZ_FILE32_API
27 # ifndef NO_FSEEKO
28 # define ftello64 ftello
29 # define fseeko64 fseeko
30 # elif defined(_MSC_VER) && (_MSC_VER >= 1400)
31 # define ftello64 _ftelli64
32 # define fseeko64 _fseeki64
33 # endif
34 #endif
35 #ifndef ftello64
36 # define ftello64 ftell
37 #endif
38 #ifndef fseeko64
39 # define fseeko64 fseek
40 #endif
41
42 /***************************************************************************/
43
44 static mz_stream_vtbl mz_stream_os_vtbl = {mz_stream_os_open,
45 mz_stream_os_is_open,
46 mz_stream_os_read,
47 mz_stream_os_write,
48 mz_stream_os_tell,
49 mz_stream_os_seek,
50 mz_stream_os_close,
51 mz_stream_os_error,
52 mz_stream_os_create,
53 mz_stream_os_delete,
54 NULL,
55 NULL};
56
57 /***************************************************************************/
58
59 typedef struct mz_stream_posix_s {
60 mz_stream stream;
61 int32_t error;
62 FILE *handle;
63 } mz_stream_posix;
64
65 /***************************************************************************/
66
67 int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode) {
68 mz_stream_posix *posix = (mz_stream_posix *)stream;
69 const char *mode_fopen = NULL;
70
71 if (!path)
72 return MZ_PARAM_ERROR;
73
74 if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
75 mode_fopen = "rb";
76 else if (mode & MZ_OPEN_MODE_APPEND)
77 mode_fopen = "r+b";
78 else if (mode & MZ_OPEN_MODE_CREATE)
79 mode_fopen = "wb";
80 else
81 return MZ_OPEN_ERROR;
82
83 posix->handle = fopen64(path, mode_fopen);
84 if (!posix->handle) {
85 posix->error = errno;
86 return MZ_OPEN_ERROR;
87 }
88
89 if (mode & MZ_OPEN_MODE_APPEND)
90 return mz_stream_os_seek(stream, 0, MZ_SEEK_END);
91
92 return MZ_OK;
93 }
94
95 int32_t mz_stream_os_is_open(void *stream) {
96 mz_stream_posix *posix = (mz_stream_posix *)stream;
97 if (!posix->handle)
98 return MZ_OPEN_ERROR;
99 return MZ_OK;
100 }
101
102 int32_t mz_stream_os_read(void *stream, void *buf, int32_t size) {
103 mz_stream_posix *posix = (mz_stream_posix *)stream;
104 int32_t read = (int32_t)fread(buf, 1, (size_t)size, posix->handle);
105 if (read < size && ferror(posix->handle)) {
106 posix->error = errno;
107 return MZ_READ_ERROR;
108 }
109 return read;
110 }
111
112 int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size) {
113 mz_stream_posix *posix = (mz_stream_posix *)stream;
114 int32_t written = (int32_t)fwrite(buf, 1, (size_t)size, posix->handle);
115 if (written < size && ferror(posix->handle)) {
116 posix->error = errno;
117 return MZ_WRITE_ERROR;
118 }
119 return written;
120 }
121
122 int64_t mz_stream_os_tell(void *stream) {
123 mz_stream_posix *posix = (mz_stream_posix *)stream;
124 int64_t position = ftello64(posix->handle);
125 if (position == -1) {
126 posix->error = errno;
127 return MZ_TELL_ERROR;
128 }
129 return position;
130 }
131
132 int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin) {
133 mz_stream_posix *posix = (mz_stream_posix *)stream;
134 int32_t fseek_origin = 0;
135
136 switch (origin) {
137 case MZ_SEEK_CUR:
138 fseek_origin = SEEK_CUR;
139 break;
140 case MZ_SEEK_END:
141 fseek_origin = SEEK_END;
142 break;
143 case MZ_SEEK_SET:
144 fseek_origin = SEEK_SET;
145 break;
146 default:
147 return MZ_SEEK_ERROR;
148 }
149
150 if (fseeko64(posix->handle, offset, fseek_origin) != 0) {
151 posix->error = errno;
152 return MZ_SEEK_ERROR;
153 }
154
155 return MZ_OK;
156 }
157
158 int32_t mz_stream_os_close(void *stream) {
159 mz_stream_posix *posix = (mz_stream_posix *)stream;
160 int32_t closed = 0;
161 if (posix->handle) {
162 closed = fclose(posix->handle);
163 posix->handle = NULL;
164 }
165 if (closed != 0) {
166 posix->error = errno;
167 return MZ_CLOSE_ERROR;
168 }
169 return MZ_OK;
170 }
171
172 int32_t mz_stream_os_error(void *stream) {
173 mz_stream_posix *posix = (mz_stream_posix *)stream;
174 return posix->error;
175 }
176
177 void *mz_stream_os_create(void) {
178 mz_stream_posix *posix = (mz_stream_posix *)calloc(1, sizeof(mz_stream_posix));
179 if (posix)
180 posix->stream.vtbl = &mz_stream_os_vtbl;
181 return posix;
182 }
183
184 void mz_stream_os_delete(void **stream) {
185 mz_stream_posix *posix = NULL;
186 if (!stream)
187 return;
188 posix = (mz_stream_posix *)*stream;
189 free(posix);
190 *stream = NULL;
191 }
192
193 void *mz_stream_os_get_interface(void) {
194 return (void *)&mz_stream_os_vtbl;
195 }