/src/minizip-ng/mz_strm_zlib.c
Line | Count | Source |
1 | | /* mz_strm_zlib.c -- Stream for zlib inflate/deflate |
2 | | part of the minizip-ng project |
3 | | |
4 | | Copyright (C) Nathan Moinvaziri |
5 | | https://github.com/zlib-ng/minizip-ng |
6 | | |
7 | | This program is distributed under the terms of the same license as zlib. |
8 | | See the accompanying LICENSE file for the full text of the license. |
9 | | */ |
10 | | |
11 | | #include "mz.h" |
12 | | #include "mz_strm.h" |
13 | | #include "mz_strm_zlib.h" |
14 | | |
15 | | #if !defined(ZLIB_COMPAT) |
16 | | # include "zlib-ng.h" |
17 | | #else |
18 | | # include "zlib.h" |
19 | | #endif |
20 | | |
21 | | /***************************************************************************/ |
22 | | |
23 | | #if !defined(ZLIB_COMPAT) |
24 | | # define ZLIB_PREFIX(x) zng_##x |
25 | | typedef zng_stream zlib_stream; |
26 | | #else |
27 | 492 | # define ZLIB_PREFIX(x) x |
28 | | typedef z_stream zlib_stream; |
29 | | #endif |
30 | | |
31 | | #if !defined(DEF_MEM_LEVEL) |
32 | | # if MAX_MEM_LEVEL >= 8 |
33 | | # define DEF_MEM_LEVEL 8 |
34 | | # else |
35 | | # define DEF_MEM_LEVEL MAX_MEM_LEVEL |
36 | | # endif |
37 | | #endif |
38 | | |
39 | | /***************************************************************************/ |
40 | | |
41 | | static mz_stream_vtbl mz_stream_zlib_vtbl = { |
42 | | mz_stream_zlib_open, mz_stream_zlib_is_open, mz_stream_zlib_read, mz_stream_zlib_write, |
43 | | mz_stream_zlib_tell, mz_stream_zlib_seek, mz_stream_zlib_close, mz_stream_zlib_error, |
44 | | mz_stream_zlib_create, mz_stream_zlib_delete, mz_stream_zlib_get_prop_int64, mz_stream_zlib_set_prop_int64}; |
45 | | |
46 | | /***************************************************************************/ |
47 | | |
48 | | typedef struct mz_stream_zlib_s { |
49 | | mz_stream stream; |
50 | | zlib_stream zstream; |
51 | | uint8_t buffer[INT16_MAX]; |
52 | | int32_t buffer_len; |
53 | | int64_t total_in; |
54 | | int64_t total_out; |
55 | | int64_t max_total_in; |
56 | | int8_t initialized; |
57 | | int16_t level; |
58 | | int32_t window_bits; |
59 | | int32_t mode; |
60 | | int32_t error; |
61 | | } mz_stream_zlib; |
62 | | |
63 | | /***************************************************************************/ |
64 | | |
65 | 137 | int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode) { |
66 | 137 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
67 | | |
68 | 137 | MZ_UNUSED(path); |
69 | | |
70 | 137 | zlib->zstream.data_type = Z_BINARY; |
71 | 137 | zlib->zstream.zalloc = Z_NULL; |
72 | 137 | zlib->zstream.zfree = Z_NULL; |
73 | 137 | zlib->zstream.opaque = Z_NULL; |
74 | 137 | zlib->zstream.total_in = 0; |
75 | 137 | zlib->zstream.total_out = 0; |
76 | | |
77 | 137 | zlib->total_in = 0; |
78 | 137 | zlib->total_out = 0; |
79 | | |
80 | 137 | if (mode & MZ_OPEN_MODE_WRITE) { |
81 | | #ifdef MZ_ZIP_NO_COMPRESSION |
82 | | return MZ_SUPPORT_ERROR; |
83 | | #else |
84 | 137 | zlib->zstream.next_out = zlib->buffer; |
85 | 137 | zlib->zstream.avail_out = sizeof(zlib->buffer); |
86 | | |
87 | 137 | zlib->error = ZLIB_PREFIX(deflateInit2)(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, zlib->window_bits, |
88 | 137 | DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); |
89 | 137 | #endif |
90 | 137 | } else if (mode & MZ_OPEN_MODE_READ) { |
91 | | #ifdef MZ_ZIP_NO_DECOMPRESSION |
92 | | return MZ_SUPPORT_ERROR; |
93 | | #else |
94 | 0 | zlib->zstream.next_in = zlib->buffer; |
95 | 0 | zlib->zstream.avail_in = 0; |
96 | |
|
97 | 0 | zlib->error = ZLIB_PREFIX(inflateInit2)(&zlib->zstream, zlib->window_bits); |
98 | 0 | #endif |
99 | 0 | } |
100 | | |
101 | 137 | if (zlib->error != Z_OK) |
102 | 26 | return MZ_OPEN_ERROR; |
103 | | |
104 | 111 | zlib->initialized = 1; |
105 | 111 | zlib->mode = mode; |
106 | 111 | return MZ_OK; |
107 | 137 | } |
108 | | |
109 | 163 | int32_t mz_stream_zlib_is_open(void *stream) { |
110 | 163 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
111 | 163 | if (zlib->initialized != 1) |
112 | 0 | return MZ_OPEN_ERROR; |
113 | 163 | return MZ_OK; |
114 | 163 | } |
115 | | |
116 | 0 | int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size) { |
117 | | #ifdef MZ_ZIP_NO_DECOMPRESSION |
118 | | MZ_UNUSED(stream); |
119 | | MZ_UNUSED(buf); |
120 | | MZ_UNUSED(size); |
121 | | return MZ_SUPPORT_ERROR; |
122 | | #else |
123 | 0 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
124 | 0 | uint64_t total_in_before = 0; |
125 | 0 | uint64_t total_in_after = 0; |
126 | 0 | uint64_t total_out_before = 0; |
127 | 0 | uint64_t total_out_after = 0; |
128 | 0 | uint32_t total_in = 0; |
129 | 0 | uint32_t total_out = 0; |
130 | 0 | uint32_t in_bytes = 0; |
131 | 0 | uint32_t out_bytes = 0; |
132 | 0 | int32_t bytes_to_read = sizeof(zlib->buffer); |
133 | 0 | int32_t read = 0; |
134 | 0 | int32_t err = Z_OK; |
135 | |
|
136 | 0 | zlib->zstream.next_out = (Bytef *)buf; |
137 | 0 | zlib->zstream.avail_out = (uInt)size; |
138 | |
|
139 | 0 | do { |
140 | 0 | if (zlib->zstream.avail_in == 0) { |
141 | 0 | if (zlib->max_total_in > 0) { |
142 | 0 | if ((int64_t)bytes_to_read > (zlib->max_total_in - zlib->total_in)) |
143 | 0 | bytes_to_read = (int32_t)(zlib->max_total_in - zlib->total_in); |
144 | 0 | } |
145 | |
|
146 | 0 | read = mz_stream_read(zlib->stream.base, zlib->buffer, bytes_to_read); |
147 | |
|
148 | 0 | if (read < 0) |
149 | 0 | return read; |
150 | | |
151 | 0 | zlib->zstream.next_in = zlib->buffer; |
152 | 0 | zlib->zstream.avail_in = read; |
153 | 0 | } |
154 | | |
155 | 0 | total_in_before = zlib->zstream.avail_in; |
156 | 0 | total_out_before = zlib->zstream.total_out; |
157 | |
|
158 | 0 | err = ZLIB_PREFIX(inflate)(&zlib->zstream, Z_SYNC_FLUSH); |
159 | 0 | if ((err >= Z_OK) && (zlib->zstream.msg)) { |
160 | 0 | zlib->error = Z_DATA_ERROR; |
161 | 0 | break; |
162 | 0 | } |
163 | | |
164 | 0 | total_in_after = zlib->zstream.avail_in; |
165 | 0 | total_out_after = zlib->zstream.total_out; |
166 | |
|
167 | 0 | in_bytes = (uint32_t)(total_in_before - total_in_after); |
168 | 0 | out_bytes = (uint32_t)(total_out_after - total_out_before); |
169 | |
|
170 | 0 | total_in += in_bytes; |
171 | 0 | total_out += out_bytes; |
172 | |
|
173 | 0 | zlib->total_in += in_bytes; |
174 | 0 | zlib->total_out += out_bytes; |
175 | |
|
176 | 0 | if (err == Z_STREAM_END) |
177 | 0 | break; |
178 | 0 | if (err != Z_OK) { |
179 | 0 | zlib->error = err; |
180 | 0 | break; |
181 | 0 | } |
182 | 0 | } while (zlib->zstream.avail_out > 0); |
183 | | |
184 | 0 | MZ_UNUSED(total_in); |
185 | |
|
186 | 0 | if (zlib->error != 0) { |
187 | | /* Zlib errors are compatible with MZ */ |
188 | 0 | return zlib->error; |
189 | 0 | } |
190 | | |
191 | 0 | return total_out; |
192 | 0 | #endif |
193 | 0 | } |
194 | | |
195 | | #ifndef MZ_ZIP_NO_COMPRESSION |
196 | 333 | static int32_t mz_stream_zlib_flush(void *stream) { |
197 | 333 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
198 | 333 | if (mz_stream_write(zlib->stream.base, zlib->buffer, zlib->buffer_len) != zlib->buffer_len) |
199 | 0 | return MZ_WRITE_ERROR; |
200 | 333 | return MZ_OK; |
201 | 333 | } |
202 | | |
203 | 163 | static int32_t mz_stream_zlib_deflate(void *stream, int flush) { |
204 | 163 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
205 | 163 | uint64_t total_out_before = 0; |
206 | 163 | uint64_t total_out_after = 0; |
207 | 163 | int32_t out_bytes = 0; |
208 | 163 | int32_t err = Z_OK; |
209 | | |
210 | 381 | do { |
211 | 381 | if (zlib->zstream.avail_out == 0) { |
212 | 222 | err = mz_stream_zlib_flush(zlib); |
213 | 222 | if (err != MZ_OK) |
214 | 0 | return err; |
215 | | |
216 | 222 | zlib->zstream.avail_out = sizeof(zlib->buffer); |
217 | 222 | zlib->zstream.next_out = zlib->buffer; |
218 | | |
219 | 222 | zlib->buffer_len = 0; |
220 | 222 | } |
221 | | |
222 | 381 | total_out_before = zlib->zstream.total_out; |
223 | 381 | err = ZLIB_PREFIX(deflate)(&zlib->zstream, flush); |
224 | 381 | total_out_after = zlib->zstream.total_out; |
225 | | |
226 | 381 | out_bytes = (uint32_t)(total_out_after - total_out_before); |
227 | | |
228 | 381 | zlib->buffer_len += out_bytes; |
229 | 381 | zlib->total_out += out_bytes; |
230 | | |
231 | 381 | if (err == Z_STREAM_END) |
232 | 111 | break; |
233 | 270 | if (err != Z_OK) { |
234 | 0 | zlib->error = err; |
235 | 0 | return MZ_DATA_ERROR; |
236 | 0 | } |
237 | 270 | } while ((zlib->zstream.avail_in > 0) || (flush == Z_FINISH && err == Z_OK)); |
238 | | |
239 | 163 | return MZ_OK; |
240 | 163 | } |
241 | | #endif |
242 | | |
243 | 52 | int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size) { |
244 | | #ifdef MZ_ZIP_NO_COMPRESSION |
245 | | MZ_UNUSED(stream); |
246 | | MZ_UNUSED(buf); |
247 | | MZ_UNUSED(size); |
248 | | return MZ_SUPPORT_ERROR; |
249 | | #else |
250 | 52 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
251 | 52 | int32_t err = MZ_OK; |
252 | | |
253 | 52 | zlib->zstream.next_in = (Bytef *)(intptr_t)buf; |
254 | 52 | zlib->zstream.avail_in = (uInt)size; |
255 | | |
256 | 52 | err = mz_stream_zlib_deflate(stream, Z_NO_FLUSH); |
257 | 52 | if (err != MZ_OK) { |
258 | 0 | return err; |
259 | 0 | } |
260 | | |
261 | 52 | zlib->total_in += size; |
262 | 52 | return size; |
263 | 52 | #endif |
264 | 52 | } |
265 | | |
266 | 0 | int64_t mz_stream_zlib_tell(void *stream) { |
267 | 0 | MZ_UNUSED(stream); |
268 | |
|
269 | 0 | return MZ_TELL_ERROR; |
270 | 0 | } |
271 | | |
272 | 0 | int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin) { |
273 | 0 | MZ_UNUSED(stream); |
274 | 0 | MZ_UNUSED(offset); |
275 | 0 | MZ_UNUSED(origin); |
276 | |
|
277 | 0 | return MZ_SEEK_ERROR; |
278 | 0 | } |
279 | | |
280 | 111 | int32_t mz_stream_zlib_close(void *stream) { |
281 | 111 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
282 | | |
283 | 111 | if (zlib->mode & MZ_OPEN_MODE_WRITE) { |
284 | | #ifdef MZ_ZIP_NO_COMPRESSION |
285 | | return MZ_SUPPORT_ERROR; |
286 | | #else |
287 | 111 | mz_stream_zlib_deflate(stream, Z_FINISH); |
288 | 111 | mz_stream_zlib_flush(stream); |
289 | | |
290 | 111 | ZLIB_PREFIX(deflateEnd)(&zlib->zstream); |
291 | 111 | #endif |
292 | 111 | } else if (zlib->mode & MZ_OPEN_MODE_READ) { |
293 | | #ifdef MZ_ZIP_NO_DECOMPRESSION |
294 | | return MZ_SUPPORT_ERROR; |
295 | | #else |
296 | 0 | ZLIB_PREFIX(inflateEnd)(&zlib->zstream); |
297 | 0 | #endif |
298 | 0 | } |
299 | | |
300 | 111 | zlib->initialized = 0; |
301 | | |
302 | 111 | if (zlib->error != Z_OK) |
303 | 0 | return MZ_CLOSE_ERROR; |
304 | 111 | return MZ_OK; |
305 | 111 | } |
306 | | |
307 | 0 | int32_t mz_stream_zlib_error(void *stream) { |
308 | 0 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
309 | 0 | return zlib->error; |
310 | 0 | } |
311 | | |
312 | 222 | int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value) { |
313 | 222 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
314 | 222 | switch (prop) { |
315 | 111 | case MZ_STREAM_PROP_TOTAL_IN: |
316 | 111 | *value = zlib->total_in; |
317 | 111 | break; |
318 | 0 | case MZ_STREAM_PROP_TOTAL_IN_MAX: |
319 | 0 | *value = zlib->max_total_in; |
320 | 0 | break; |
321 | 111 | case MZ_STREAM_PROP_TOTAL_OUT: |
322 | 111 | *value = zlib->total_out; |
323 | 111 | break; |
324 | 0 | case MZ_STREAM_PROP_HEADER_SIZE: |
325 | 0 | *value = 0; |
326 | 0 | break; |
327 | 0 | case MZ_STREAM_PROP_COMPRESS_WINDOW: |
328 | 0 | *value = zlib->window_bits; |
329 | 0 | break; |
330 | 0 | default: |
331 | 0 | return MZ_EXIST_ERROR; |
332 | 222 | } |
333 | 222 | return MZ_OK; |
334 | 222 | } |
335 | | |
336 | 137 | int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value) { |
337 | 137 | mz_stream_zlib *zlib = (mz_stream_zlib *)stream; |
338 | 137 | switch (prop) { |
339 | 137 | case MZ_STREAM_PROP_COMPRESS_LEVEL: |
340 | 137 | if (value == MZ_COMPRESS_LEVEL_DEFAULT) |
341 | 69 | zlib->level = Z_DEFAULT_COMPRESSION; |
342 | 68 | else |
343 | 68 | zlib->level = (int16_t)value; |
344 | 137 | break; |
345 | 0 | case MZ_STREAM_PROP_TOTAL_IN_MAX: |
346 | 0 | zlib->max_total_in = value; |
347 | 0 | break; |
348 | 0 | case MZ_STREAM_PROP_COMPRESS_WINDOW: |
349 | 0 | zlib->window_bits = (int32_t)value; |
350 | 0 | break; |
351 | 0 | default: |
352 | 0 | return MZ_EXIST_ERROR; |
353 | 137 | } |
354 | 137 | return MZ_OK; |
355 | 137 | } |
356 | | |
357 | 137 | void *mz_stream_zlib_create(void) { |
358 | 137 | mz_stream_zlib *zlib = (mz_stream_zlib *)calloc(1, sizeof(mz_stream_zlib)); |
359 | 137 | if (zlib) { |
360 | 137 | zlib->stream.vtbl = &mz_stream_zlib_vtbl; |
361 | 137 | zlib->level = Z_DEFAULT_COMPRESSION; |
362 | 137 | zlib->window_bits = -MAX_WBITS; |
363 | 137 | } |
364 | 137 | return zlib; |
365 | 137 | } |
366 | | |
367 | 137 | void mz_stream_zlib_delete(void **stream) { |
368 | 137 | mz_stream_zlib *zlib = NULL; |
369 | 137 | if (!stream) |
370 | 0 | return; |
371 | 137 | zlib = (mz_stream_zlib *)*stream; |
372 | 137 | free(zlib); |
373 | 137 | *stream = NULL; |
374 | 137 | } |
375 | | |
376 | 0 | void *mz_stream_zlib_get_interface(void) { |
377 | 0 | return (void *)&mz_stream_zlib_vtbl; |
378 | 0 | } |