Debian Codegraph @

mz_strm_libcomp.c

raw symbols SCIP
1 /* mz_strm_libcomp.c -- Stream for apple compression
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_libcomp.h"
14
15 #include <compression.h>
16
17 /***************************************************************************/
18
19 static mz_stream_vtbl mz_stream_libcomp_vtbl = {
20 mz_stream_libcomp_open, mz_stream_libcomp_is_open, mz_stream_libcomp_read,
21 mz_stream_libcomp_write, mz_stream_libcomp_tell, mz_stream_libcomp_seek,
22 mz_stream_libcomp_close, mz_stream_libcomp_error, mz_stream_libcomp_create,
23 mz_stream_libcomp_delete, mz_stream_libcomp_get_prop_int64, mz_stream_libcomp_set_prop_int64};
24
25 /***************************************************************************/
26
27 typedef struct mz_stream_libcomp_s {
28 mz_stream stream;
29 compression_stream cstream;
30 uint8_t buffer[INT16_MAX];
31 int32_t buffer_len;
32 int64_t total_in;
33 int64_t total_out;
34 int64_t max_total_in;
35 int8_t initialized;
36 int32_t mode;
37 int32_t error;
38 int16_t method;
39 } mz_stream_libcomp;
40
41 /***************************************************************************/
42
43 int32_t mz_stream_libcomp_open(void *stream, const char *path, int32_t mode) {
44 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
45 int32_t err = 0;
46 int16_t operation = 0;
47 compression_algorithm algorithm = 0;
48
49 MZ_UNUSED(path);
50
51 if (libcomp->method == 0)
52 return MZ_PARAM_ERROR;
53
54 libcomp->total_in = 0;
55 libcomp->total_out = 0;
56
57 if (mode & MZ_OPEN_MODE_WRITE) {
58 #ifdef MZ_ZIP_NO_COMPRESSION
59 return MZ_SUPPORT_ERROR;
60 #else
61 operation = COMPRESSION_STREAM_ENCODE;
62 #endif
63 } else if (mode & MZ_OPEN_MODE_READ) {
64 #ifdef MZ_ZIP_NO_DECOMPRESSION
65 return MZ_SUPPORT_ERROR;
66 #else
67 operation = COMPRESSION_STREAM_DECODE;
68 #endif
69 }
70
71 if (libcomp->method == MZ_COMPRESS_METHOD_DEFLATE)
72 algorithm = COMPRESSION_ZLIB;
73 else if (libcomp->method == MZ_COMPRESS_METHOD_XZ)
74 algorithm = COMPRESSION_LZMA;
75 else
76 return MZ_SUPPORT_ERROR;
77
78 err = compression_stream_init(&libcomp->cstream, (compression_stream_operation)operation, algorithm);
79
80 if (err == COMPRESSION_STATUS_ERROR) {
81 libcomp->error = err;
82 return MZ_OPEN_ERROR;
83 }
84
85 libcomp->initialized = 1;
86 libcomp->mode = mode;
87 return MZ_OK;
88 }
89
90 int32_t mz_stream_libcomp_is_open(void *stream) {
91 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
92 if (libcomp->initialized != 1)
93 return MZ_OPEN_ERROR;
94 return MZ_OK;
95 }
96
97 int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size) {
98 #ifdef MZ_ZIP_NO_DECOMPRESSION
99 MZ_UNUSED(stream);
100 MZ_UNUSED(buf);
101 MZ_UNUSED(size);
102 return MZ_SUPPORT_ERROR;
103 #else
104 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
105 uint64_t total_in_before = 0;
106 uint64_t total_in_after = 0;
107 uint64_t total_out_before = 0;
108 uint64_t total_out_after = 0;
109 int32_t total_out = 0;
110 int32_t in_bytes = 0;
111 int32_t out_bytes = 0;
112 int32_t bytes_to_read = sizeof(libcomp->buffer);
113 int32_t read = 0;
114 int32_t err = MZ_OK;
115 int16_t flags = 0;
116
117 libcomp->cstream.dst_ptr = buf;
118 libcomp->cstream.dst_size = (size_t)size;
119
120 do {
121 if (libcomp->cstream.src_size == 0) {
122 if (libcomp->max_total_in > 0) {
123 if ((int64_t)bytes_to_read > (libcomp->max_total_in - libcomp->total_in))
124 bytes_to_read = (int32_t)(libcomp->max_total_in - libcomp->total_in);
125 }
126
127 read = mz_stream_read(libcomp->stream.base, libcomp->buffer, bytes_to_read);
128
129 if (read < 0)
130 return read;
131 if (read == 0)
132 flags = COMPRESSION_STREAM_FINALIZE;
133
134 libcomp->cstream.src_ptr = libcomp->buffer;
135 libcomp->cstream.src_size = (size_t)read;
136 }
137
138 total_in_before = libcomp->cstream.src_size;
139 total_out_before = libcomp->cstream.dst_size;
140
141 err = compression_stream_process(&libcomp->cstream, flags);
142 if (err == COMPRESSION_STATUS_ERROR) {
143 libcomp->error = err;
144 break;
145 }
146
147 total_in_after = libcomp->cstream.src_size;
148 total_out_after = libcomp->cstream.dst_size;
149
150 in_bytes = (int32_t)(total_in_before - total_in_after);
151 out_bytes = (int32_t)(total_out_before - total_out_after);
152
153 total_out += out_bytes;
154
155 libcomp->total_in += in_bytes;
156 libcomp->total_out += out_bytes;
157
158 if (err == COMPRESSION_STATUS_END)
159 break;
160 if (err != COMPRESSION_STATUS_OK) {
161 libcomp->error = err;
162 break;
163 }
164 } while (libcomp->cstream.dst_size > 0);
165
166 if (libcomp->error != 0)
167 return MZ_DATA_ERROR;
168
169 return total_out;
170 #endif
171 }
172
173 static int32_t mz_stream_libcomp_flush(void *stream) {
174 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
175 if (mz_stream_write(libcomp->stream.base, libcomp->buffer, libcomp->buffer_len) != libcomp->buffer_len)
176 return MZ_WRITE_ERROR;
177 return MZ_OK;
178 }
179
180 static int32_t mz_stream_libcomp_deflate(void *stream, int flush) {
181 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
182 uint64_t total_out_before = 0;
183 uint64_t total_out_after = 0;
184 uint32_t out_bytes = 0;
185 int32_t err = MZ_OK;
186
187 do {
188 if (libcomp->cstream.dst_size == 0) {
189 err = mz_stream_libcomp_flush(libcomp);
190 if (err != MZ_OK) {
191 libcomp->error = err;
192 return err;
193 }
194
195 libcomp->cstream.dst_size = sizeof(libcomp->buffer);
196 libcomp->cstream.dst_ptr = libcomp->buffer;
197
198 libcomp->buffer_len = 0;
199 }
200
201 total_out_before = libcomp->cstream.dst_size;
202 err = compression_stream_process(&libcomp->cstream, flush);
203 total_out_after = libcomp->cstream.dst_size;
204
205 out_bytes = (uint32_t)(total_out_before - total_out_after);
206
207 libcomp->buffer_len += out_bytes;
208 libcomp->total_out += out_bytes;
209
210 if (err == COMPRESSION_STATUS_END)
211 break;
212 if (err != COMPRESSION_STATUS_OK) {
213 libcomp->error = err;
214 return MZ_DATA_ERROR;
215 }
216 } while ((libcomp->cstream.src_size > 0) || (flush == COMPRESSION_STREAM_FINALIZE && err == COMPRESSION_STATUS_OK));
217
218 return MZ_OK;
219 }
220
221 int32_t mz_stream_libcomp_write(void *stream, const void *buf, int32_t size) {
222 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
223 int32_t err = size;
224
225 #ifdef MZ_ZIP_NO_COMPRESSION
226 MZ_UNUSED(libcomp);
227 err = MZ_SUPPORT_ERROR;
228 #else
229 libcomp->cstream.src_ptr = buf;
230 libcomp->cstream.src_size = (size_t)size;
231
232 mz_stream_libcomp_deflate(stream, 0);
233
234 libcomp->total_in += size;
235 #endif
236 return err;
237 }
238
239 int64_t mz_stream_libcomp_tell(void *stream) {
240 MZ_UNUSED(stream);
241
242 return MZ_TELL_ERROR;
243 }
244
245 int32_t mz_stream_libcomp_seek(void *stream, int64_t offset, int32_t origin) {
246 MZ_UNUSED(stream);
247 MZ_UNUSED(offset);
248 MZ_UNUSED(origin);
249
250 return MZ_SEEK_ERROR;
251 }
252
253 int32_t mz_stream_libcomp_close(void *stream) {
254 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
255
256 if (libcomp->mode & MZ_OPEN_MODE_WRITE) {
257 #ifdef MZ_ZIP_NO_COMPRESSION
258 return MZ_SUPPORT_ERROR;
259 #else
260 mz_stream_libcomp_deflate(stream, COMPRESSION_STREAM_FINALIZE);
261 mz_stream_libcomp_flush(stream);
262 #endif
263 } else if (libcomp->mode & MZ_OPEN_MODE_READ) {
264 #ifdef MZ_ZIP_NO_DECOMPRESSION
265 return MZ_SUPPORT_ERROR;
266 #endif
267 }
268
269 compression_stream_destroy(&libcomp->cstream);
270
271 libcomp->initialized = 0;
272
273 if (libcomp->error != MZ_OK)
274 return MZ_CLOSE_ERROR;
275 return MZ_OK;
276 }
277
278 int32_t mz_stream_libcomp_error(void *stream) {
279 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
280 return libcomp->error;
281 }
282
283 int32_t mz_stream_libcomp_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
284 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
285 switch (prop) {
286 case MZ_STREAM_PROP_TOTAL_IN:
287 *value = libcomp->total_in;
288 break;
289 case MZ_STREAM_PROP_TOTAL_IN_MAX:
290 *value = libcomp->max_total_in;
291 break;
292 case MZ_STREAM_PROP_TOTAL_OUT:
293 *value = libcomp->total_out;
294 break;
295 case MZ_STREAM_PROP_HEADER_SIZE:
296 *value = 0;
297 break;
298 default:
299 return MZ_EXIST_ERROR;
300 }
301 return MZ_OK;
302 }
303
304 int32_t mz_stream_libcomp_set_prop_int64(void *stream, int32_t prop, int64_t value) {
305 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
306 switch (prop) {
307 case MZ_STREAM_PROP_COMPRESS_METHOD:
308 libcomp->method = (int16_t)value;
309 break;
310 case MZ_STREAM_PROP_TOTAL_IN_MAX:
311 libcomp->max_total_in = value;
312 break;
313 default:
314 return MZ_EXIST_ERROR;
315 }
316 return MZ_OK;
317 }
318
319 void *mz_stream_libcomp_create(void) {
320 mz_stream_libcomp *libcomp = (mz_stream_libcomp *)calloc(1, sizeof(mz_stream_libcomp));
321 if (libcomp)
322 libcomp->stream.vtbl = &mz_stream_libcomp_vtbl;
323 return libcomp;
324 }
325
326 void mz_stream_libcomp_delete(void **stream) {
327 mz_stream_libcomp *libcomp = NULL;
328 if (!stream)
329 return;
330 libcomp = (mz_stream_libcomp *)*stream;
331 if (libcomp)
332 free(libcomp);
333 *stream = NULL;
334 }