Debian Codegraph @

mz_strm_zstd.c

raw symbols SCIP
1 /* mz_strm_zstd.c -- Stream for zstd compress/decompress
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6 Authors: Force Charlie
7 https://github.com/fcharlie
8
9 This program is distributed under the terms of the same license as zlib.
10 See the accompanying LICENSE file for the full text of the license.
11 */
12
13 #include "mz.h"
14 #include "mz_strm.h"
15 #include "mz_strm_zstd.h"
16
17 #include <zstd.h>
18
19 /***************************************************************************/
20
21 static mz_stream_vtbl mz_stream_zstd_vtbl = {
22 mz_stream_zstd_open, mz_stream_zstd_is_open, mz_stream_zstd_read, mz_stream_zstd_write,
23 mz_stream_zstd_tell, mz_stream_zstd_seek, mz_stream_zstd_close, mz_stream_zstd_error,
24 mz_stream_zstd_create, mz_stream_zstd_delete, mz_stream_zstd_get_prop_int64, mz_stream_zstd_set_prop_int64};
25
26 /***************************************************************************/
27
28 typedef struct mz_stream_zstd_s {
29 mz_stream stream;
30 ZSTD_CStream *zcstream;
31 ZSTD_DStream *zdstream;
32 ZSTD_outBuffer out;
33 ZSTD_inBuffer in;
34 int32_t mode;
35 int32_t error;
36 uint8_t buffer[INT16_MAX];
37 int32_t buffer_len;
38 int64_t total_in;
39 int64_t total_out;
40 int64_t max_total_in;
41 int64_t max_total_out;
42 int8_t initialized;
43 int32_t preset;
44 } mz_stream_zstd;
45
46 /***************************************************************************/
47
48 int32_t mz_stream_zstd_open(void *stream, const char *path, int32_t mode) {
49 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
50
51 MZ_UNUSED(path);
52
53 if (mode & MZ_OPEN_MODE_WRITE) {
54 #ifdef MZ_ZIP_NO_COMPRESSION
55 return MZ_SUPPORT_ERROR;
56 #else
57 zstd->zcstream = ZSTD_createCStream();
58 zstd->out.dst = zstd->buffer;
59 zstd->out.size = sizeof(zstd->buffer);
60 zstd->out.pos = 0;
61 ZSTD_CCtx_setParameter(zstd->zcstream, ZSTD_c_compressionLevel, zstd->preset);
62 #endif
63 } else if (mode & MZ_OPEN_MODE_READ) {
64 #ifdef MZ_ZIP_NO_DECOMPRESSION
65 return MZ_SUPPORT_ERROR;
66 #else
67 zstd->zdstream = ZSTD_createDStream();
68 memset(&zstd->out, 0, sizeof(ZSTD_outBuffer));
69 #endif
70 }
71
72 memset(&zstd->in, 0, sizeof(ZSTD_inBuffer));
73
74 zstd->initialized = 1;
75 zstd->mode = mode;
76 zstd->error = MZ_OK;
77
78 return MZ_OK;
79 }
80
81 int32_t mz_stream_zstd_is_open(void *stream) {
82 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
83 if (zstd->initialized != 1)
84 return MZ_OPEN_ERROR;
85 return MZ_OK;
86 }
87
88 int32_t mz_stream_zstd_read(void *stream, void *buf, int32_t size) {
89 #ifdef MZ_ZIP_NO_DECOMPRESSION
90 MZ_UNUSED(stream);
91 MZ_UNUSED(buf);
92 MZ_UNUSED(size);
93 return MZ_SUPPORT_ERROR;
94 #else
95 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
96 uint64_t total_in_before = 0;
97 uint64_t total_in_after = 0;
98 uint64_t total_out_before = 0;
99 uint64_t total_out_after = 0;
100 int32_t total_out = 0;
101 int32_t in_bytes = 0;
102 int32_t out_bytes = 0;
103 int32_t bytes_to_read = sizeof(zstd->buffer);
104 int32_t read = 0;
105 size_t result = 0;
106
107 zstd->out.dst = (void *)buf;
108 zstd->out.size = (size_t)size;
109 zstd->out.pos = 0;
110
111 do {
112 if (zstd->in.pos == zstd->in.size) {
113 if (zstd->max_total_in > 0) {
114 if ((int64_t)bytes_to_read > (zstd->max_total_in - zstd->total_in))
115 bytes_to_read = (int32_t)(zstd->max_total_in - zstd->total_in);
116 }
117
118 read = mz_stream_read(zstd->stream.base, zstd->buffer, bytes_to_read);
119
120 if (read < 0)
121 return read;
122
123 zstd->in.src = (const void *)zstd->buffer;
124 zstd->in.pos = 0;
125 zstd->in.size = (size_t)read;
126 }
127
128 total_in_before = zstd->in.pos;
129 total_out_before = zstd->out.pos;
130
131 result = ZSTD_decompressStream(zstd->zdstream, &zstd->out, &zstd->in);
132
133 if (ZSTD_isError(result)) {
134 zstd->error = (int32_t)result;
135 return MZ_DATA_ERROR;
136 }
137
138 total_in_after = zstd->in.pos;
139 total_out_after = zstd->out.pos;
140 if ((zstd->max_total_out != -1) && (int64_t)total_out_after > zstd->max_total_out)
141 total_out_after = (uint64_t)zstd->max_total_out;
142
143 in_bytes = (int32_t)(total_in_after - total_in_before);
144 out_bytes = (int32_t)(total_out_after - total_out_before);
145
146 total_out += out_bytes;
147
148 zstd->total_in += in_bytes;
149 zstd->total_out += out_bytes;
150
151 } while ((zstd->in.size > 0 || out_bytes > 0) && (zstd->out.pos < zstd->out.size));
152
153 return total_out;
154 #endif
155 }
156
157 #ifndef MZ_ZIP_NO_COMPRESSION
158 static int32_t mz_stream_zstd_flush(void *stream) {
159 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
160 if (mz_stream_write(zstd->stream.base, zstd->buffer, zstd->buffer_len) != zstd->buffer_len)
161 return MZ_WRITE_ERROR;
162 return MZ_OK;
163 }
164
165 static int32_t mz_stream_zstd_compress(void *stream, ZSTD_EndDirective flush) {
166 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
167 uint64_t total_out_before = 0;
168 uint64_t total_out_after = 0;
169 int32_t out_bytes = 0;
170 size_t result = 0;
171 int32_t err = 0;
172
173 do {
174 if (zstd->out.pos == zstd->out.size) {
175 err = mz_stream_zstd_flush(zstd);
176 if (err != MZ_OK)
177 return err;
178
179 zstd->out.dst = zstd->buffer;
180 zstd->out.size = sizeof(zstd->buffer);
181 zstd->out.pos = 0;
182
183 zstd->buffer_len = 0;
184 }
185
186 total_out_before = zstd->out.pos;
187
188 result = ZSTD_compressStream2(zstd->zcstream, &zstd->out, &zstd->in, flush);
189
190 total_out_after = zstd->out.pos;
191
192 out_bytes = (uint32_t)(total_out_after - total_out_before);
193
194 zstd->buffer_len += out_bytes;
195 zstd->total_out += out_bytes;
196
197 if (ZSTD_isError(result)) {
198 zstd->error = (int32_t)result;
199 return MZ_DATA_ERROR;
200 }
201 } while ((zstd->in.pos < zstd->in.size) || (flush == ZSTD_e_end && result != 0));
202
203 return MZ_OK;
204 }
205 #endif
206
207 int32_t mz_stream_zstd_write(void *stream, const void *buf, int32_t size) {
208 #ifdef MZ_ZIP_NO_COMPRESSION
209 MZ_UNUSED(stream);
210 MZ_UNUSED(buf);
211 MZ_UNUSED(size);
212 return MZ_SUPPORT_ERROR;
213 #else
214 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
215 int32_t err = MZ_OK;
216
217 zstd->in.src = buf;
218 zstd->in.pos = 0;
219 zstd->in.size = size;
220
221 err = mz_stream_zstd_compress(stream, ZSTD_e_continue);
222 if (err != MZ_OK) {
223 return err;
224 }
225
226 zstd->total_in += size;
227 return size;
228 #endif
229 }
230
231 int64_t mz_stream_zstd_tell(void *stream) {
232 MZ_UNUSED(stream);
233
234 return MZ_TELL_ERROR;
235 }
236
237 int32_t mz_stream_zstd_seek(void *stream, int64_t offset, int32_t origin) {
238 MZ_UNUSED(stream);
239 MZ_UNUSED(offset);
240 MZ_UNUSED(origin);
241
242 return MZ_SEEK_ERROR;
243 }
244
245 int32_t mz_stream_zstd_close(void *stream) {
246 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
247
248 if (zstd->mode & MZ_OPEN_MODE_WRITE) {
249 #ifdef MZ_ZIP_NO_COMPRESSION
250 return MZ_SUPPORT_ERROR;
251 #else
252 mz_stream_zstd_compress(stream, ZSTD_e_end);
253 mz_stream_zstd_flush(stream);
254
255 ZSTD_freeCStream(zstd->zcstream);
256 zstd->zcstream = NULL;
257 #endif
258 } else if (zstd->mode & MZ_OPEN_MODE_READ) {
259 #ifdef MZ_ZIP_NO_DECOMPRESSION
260 return MZ_SUPPORT_ERROR;
261 #else
262 ZSTD_freeDStream(zstd->zdstream);
263 zstd->zdstream = NULL;
264 #endif
265 }
266 zstd->initialized = 0;
267 return MZ_OK;
268 }
269
270 int32_t mz_stream_zstd_error(void *stream) {
271 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
272 return zstd->error;
273 }
274
275 int32_t mz_stream_zstd_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
276 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
277 switch (prop) {
278 case MZ_STREAM_PROP_TOTAL_IN:
279 *value = zstd->total_in;
280 break;
281 case MZ_STREAM_PROP_TOTAL_IN_MAX:
282 *value = zstd->max_total_in;
283 break;
284 case MZ_STREAM_PROP_TOTAL_OUT:
285 *value = zstd->total_out;
286 break;
287 case MZ_STREAM_PROP_TOTAL_OUT_MAX:
288 *value = zstd->max_total_out;
289 break;
290 case MZ_STREAM_PROP_HEADER_SIZE:
291 *value = 0;
292 break;
293 default:
294 return MZ_EXIST_ERROR;
295 }
296 return MZ_OK;
297 }
298
299 int32_t mz_stream_zstd_set_prop_int64(void *stream, int32_t prop, int64_t value) {
300 mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
301 switch (prop) {
302 case MZ_STREAM_PROP_COMPRESS_LEVEL:
303 if (value == MZ_COMPRESS_LEVEL_DEFAULT)
304 zstd->preset = ZSTD_CLEVEL_DEFAULT;
305 else
306 zstd->preset = (int16_t)value;
307 return MZ_OK;
308 case MZ_STREAM_PROP_TOTAL_IN_MAX:
309 zstd->max_total_in = value;
310 return MZ_OK;
311 }
312 return MZ_EXIST_ERROR;
313 }
314
315 void *mz_stream_zstd_create(void) {
316 mz_stream_zstd *zstd = (mz_stream_zstd *)calloc(1, sizeof(mz_stream_zstd));
317 if (zstd) {
318 zstd->stream.vtbl = &mz_stream_zstd_vtbl;
319 zstd->max_total_out = -1;
320 zstd->preset = ZSTD_CLEVEL_DEFAULT;
321 }
322 return zstd;
323 }
324
325 void mz_stream_zstd_delete(void **stream) {
326 mz_stream_zstd *zstd = NULL;
327 if (!stream)
328 return;
329 zstd = (mz_stream_zstd *)*stream;
330 free(zstd);
331 *stream = NULL;
332 }
333
334 void *mz_stream_zstd_get_interface(void) {
335 return (void *)&mz_stream_zstd_vtbl;
336 }