2 * Copyright (c) 2017 Sean Purcell
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
45 #include "archive_private.h"
46 #include "archive_string.h"
47 #include "archive_write_private.h"
49 /* Don't compile this if we don't have zstd.h */
52 int compression_level;
53 #if HAVE_ZSTD_H && HAVE_LIBZSTD
54 ZSTD_CStream *cstream;
58 struct archive_write_program_data *pdata;
62 /* If we don't have the library use default range values (zstdcli.c v1.4.0) */
63 #define CLEVEL_MIN -99
64 #define CLEVEL_STD_MIN 0 /* prior to 1.3.4 and more recent without using --fast */
65 #define CLEVEL_DEFAULT 3
66 #define CLEVEL_STD_MAX 19 /* without using --ultra */
69 #define MINVER_NEGCLEVEL 10304
70 #define MINVER_MINCLEVEL 10306
72 static int archive_compressor_zstd_options(struct archive_write_filter *,
73 const char *, const char *);
74 static int archive_compressor_zstd_open(struct archive_write_filter *);
75 static int archive_compressor_zstd_write(struct archive_write_filter *,
76 const void *, size_t);
77 static int archive_compressor_zstd_close(struct archive_write_filter *);
78 static int archive_compressor_zstd_free(struct archive_write_filter *);
79 #if HAVE_ZSTD_H && HAVE_LIBZSTD
80 static int drive_compressor(struct archive_write_filter *,
81 struct private_data *, int, const void *, size_t);
86 * Add a zstd compression filter to this write handle.
89 archive_write_add_filter_zstd(struct archive *_a)
91 struct archive_write *a = (struct archive_write *)_a;
92 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
93 struct private_data *data;
94 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
95 ARCHIVE_STATE_NEW, "archive_write_add_filter_zstd");
97 data = calloc(1, sizeof(*data));
99 archive_set_error(&a->archive, ENOMEM, "Out of memory");
100 return (ARCHIVE_FATAL);
103 f->open = &archive_compressor_zstd_open;
104 f->options = &archive_compressor_zstd_options;
105 f->close = &archive_compressor_zstd_close;
106 f->free = &archive_compressor_zstd_free;
107 f->code = ARCHIVE_FILTER_ZSTD;
109 data->compression_level = CLEVEL_DEFAULT;
110 #if HAVE_ZSTD_H && HAVE_LIBZSTD
111 data->cstream = ZSTD_createCStream();
112 if (data->cstream == NULL) {
114 archive_set_error(&a->archive, ENOMEM,
115 "Failed to allocate zstd compressor object");
116 return (ARCHIVE_FATAL);
121 data->pdata = __archive_write_program_allocate("zstd");
122 if (data->pdata == NULL) {
124 archive_set_error(&a->archive, ENOMEM, "Out of memory");
125 return (ARCHIVE_FATAL);
127 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
128 "Using external zstd program");
129 return (ARCHIVE_WARN);
134 archive_compressor_zstd_free(struct archive_write_filter *f)
136 struct private_data *data = (struct private_data *)f->data;
137 #if HAVE_ZSTD_H && HAVE_LIBZSTD
138 ZSTD_freeCStream(data->cstream);
141 __archive_write_program_free(data->pdata);
148 static int string_is_numeric (const char* value)
150 size_t len = strlen(value);
154 return (ARCHIVE_WARN);
156 else if (len == 1 && !(value[0] >= '0' && value[0] <= '9')) {
157 return (ARCHIVE_WARN);
159 else if (!(value[0] >= '0' && value[0] <= '9') &&
160 value[0] != '-' && value[0] != '+') {
161 return (ARCHIVE_WARN);
164 for (i = 1; i < len; i++) {
165 if (!(value[i] >= '0' && value[i] <= '9')) {
166 return (ARCHIVE_WARN);
177 archive_compressor_zstd_options(struct archive_write_filter *f, const char *key,
180 struct private_data *data = (struct private_data *)f->data;
182 if (strcmp(key, "compression-level") == 0) {
183 int level = atoi(value);
184 /* If we don't have the library, hard-code the max level */
185 int minimum = CLEVEL_MIN;
186 int maximum = CLEVEL_MAX;
187 if (string_is_numeric(value) != ARCHIVE_OK) {
188 return (ARCHIVE_WARN);
190 #if HAVE_ZSTD_H && HAVE_LIBZSTD
191 maximum = ZSTD_maxCLevel();
192 #if ZSTD_VERSION_NUMBER >= MINVER_MINCLEVEL
193 if (ZSTD_versionNumber() >= MINVER_MINCLEVEL) {
194 minimum = ZSTD_minCLevel();
198 if (ZSTD_versionNumber() < MINVER_NEGCLEVEL) {
199 minimum = CLEVEL_STD_MIN;
202 if (level < minimum || level > maximum) {
203 return (ARCHIVE_WARN);
205 data->compression_level = level;
209 /* Note: The "warn" return is just to inform the options
210 * supervisor that we didn't handle it. It will generate
211 * a suitable error if no one used this option. */
212 return (ARCHIVE_WARN);
215 #if HAVE_ZSTD_H && HAVE_LIBZSTD
220 archive_compressor_zstd_open(struct archive_write_filter *f)
222 struct private_data *data = (struct private_data *)f->data;
224 if (data->out.dst == NULL) {
225 size_t bs = ZSTD_CStreamOutSize(), bpb;
226 if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
227 /* Buffer size should be a multiple number of
228 * the of bytes per block for performance. */
229 bpb = archive_write_get_bytes_per_block(f->archive);
238 = (unsigned char *)malloc(data->out.size);
239 if (data->out.dst == NULL) {
240 archive_set_error(f->archive, ENOMEM,
241 "Can't allocate data for compression buffer");
242 return (ARCHIVE_FATAL);
246 f->write = archive_compressor_zstd_write;
248 if (ZSTD_isError(ZSTD_initCStream(data->cstream,
249 data->compression_level))) {
250 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
251 "Internal error initializing zstd compressor object");
252 return (ARCHIVE_FATAL);
259 * Write data to the compressed stream.
262 archive_compressor_zstd_write(struct archive_write_filter *f, const void *buff,
265 struct private_data *data = (struct private_data *)f->data;
268 /* Update statistics */
269 data->total_in += length;
271 if ((ret = drive_compressor(f, data, 0, buff, length)) != ARCHIVE_OK)
278 * Finish the compression...
281 archive_compressor_zstd_close(struct archive_write_filter *f)
283 struct private_data *data = (struct private_data *)f->data;
285 /* Finish zstd frame */
286 return drive_compressor(f, data, 1, NULL, 0);
290 * Utility function to push input data through compressor,
291 * writing full output blocks as necessary.
293 * Note that this handles both the regular write case (finishing ==
294 * false) and the end-of-archive case (finishing == true).
297 drive_compressor(struct archive_write_filter *f,
298 struct private_data *data, int finishing, const void *src, size_t length)
300 ZSTD_inBuffer in = (ZSTD_inBuffer) { src, length, 0 };
303 if (data->out.pos == data->out.size) {
304 const int ret = __archive_write_filter(f->next_filter,
305 data->out.dst, data->out.size);
306 if (ret != ARCHIVE_OK)
307 return (ARCHIVE_FATAL);
311 /* If there's nothing to do, we're done. */
312 if (!finishing && in.pos == in.size)
316 const size_t zstdret = !finishing ?
317 ZSTD_compressStream(data->cstream, &data->out, &in)
318 : ZSTD_endStream(data->cstream, &data->out);
320 if (ZSTD_isError(zstdret)) {
321 archive_set_error(f->archive,
323 "Zstd compression failed: %s",
324 ZSTD_getErrorName(zstdret));
325 return (ARCHIVE_FATAL);
328 /* If we're finishing, 0 means nothing left to flush */
329 if (finishing && zstdret == 0) {
330 const int ret = __archive_write_filter(f->next_filter,
331 data->out.dst, data->out.pos);
338 #else /* HAVE_ZSTD_H && HAVE_LIBZSTD */
341 archive_compressor_zstd_open(struct archive_write_filter *f)
343 struct private_data *data = (struct private_data *)f->data;
344 struct archive_string as;
347 archive_string_init(&as);
348 /* --no-check matches library default */
349 archive_strcpy(&as, "zstd --no-check");
351 if (data->compression_level < CLEVEL_STD_MIN) {
352 struct archive_string as2;
353 archive_string_init(&as2);
354 archive_string_sprintf(&as2, " --fast=%d", -data->compression_level);
355 archive_string_concat(&as, &as2);
356 archive_string_free(&as2);
358 struct archive_string as2;
359 archive_string_init(&as2);
360 archive_string_sprintf(&as2, " -%d", data->compression_level);
361 archive_string_concat(&as, &as2);
362 archive_string_free(&as2);
365 if (data->compression_level > CLEVEL_STD_MAX) {
366 archive_strcat(&as, " --ultra");
369 f->write = archive_compressor_zstd_write;
370 r = __archive_write_program_open(f, data->pdata, as.s);
371 archive_string_free(&as);
376 archive_compressor_zstd_write(struct archive_write_filter *f, const void *buff,
379 struct private_data *data = (struct private_data *)f->data;
381 return __archive_write_program_write(f, data->pdata, buff, length);
385 archive_compressor_zstd_close(struct archive_write_filter *f)
387 struct private_data *data = (struct private_data *)f->data;
389 return __archive_write_program_close(f, data->pdata);
392 #endif /* HAVE_ZSTD_H && HAVE_LIBZSTD */