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 static int archive_compressor_zstd_options(struct archive_write_filter *,
63 const char *, const char *);
64 static int archive_compressor_zstd_open(struct archive_write_filter *);
65 static int archive_compressor_zstd_write(struct archive_write_filter *,
66 const void *, size_t);
67 static int archive_compressor_zstd_close(struct archive_write_filter *);
68 static int archive_compressor_zstd_free(struct archive_write_filter *);
69 #if HAVE_ZSTD_H && HAVE_LIBZSTD
70 static int drive_compressor(struct archive_write_filter *,
71 struct private_data *, int, const void *, size_t);
76 * Add a zstd compression filter to this write handle.
79 archive_write_add_filter_zstd(struct archive *_a)
81 struct archive_write *a = (struct archive_write *)_a;
82 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
83 struct private_data *data;
84 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
85 ARCHIVE_STATE_NEW, "archive_write_add_filter_zstd");
87 data = calloc(1, sizeof(*data));
89 archive_set_error(&a->archive, ENOMEM, "Out of memory");
90 return (ARCHIVE_FATAL);
93 f->open = &archive_compressor_zstd_open;
94 f->options = &archive_compressor_zstd_options;
95 f->close = &archive_compressor_zstd_close;
96 f->free = &archive_compressor_zstd_free;
97 f->code = ARCHIVE_FILTER_ZSTD;
99 data->compression_level = 3; /* Default level used by the zstd CLI */
100 #if HAVE_ZSTD_H && HAVE_LIBZSTD
101 data->cstream = ZSTD_createCStream();
102 if (data->cstream == NULL) {
104 archive_set_error(&a->archive, ENOMEM,
105 "Failed to allocate zstd compressor object");
106 return (ARCHIVE_FATAL);
111 data->pdata = __archive_write_program_allocate("zstd");
112 if (data->pdata == NULL) {
114 archive_set_error(&a->archive, ENOMEM, "Out of memory");
115 return (ARCHIVE_FATAL);
117 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
118 "Using external zstd program");
119 return (ARCHIVE_WARN);
124 archive_compressor_zstd_free(struct archive_write_filter *f)
126 struct private_data *data = (struct private_data *)f->data;
127 #if HAVE_ZSTD_H && HAVE_LIBZSTD
128 ZSTD_freeCStream(data->cstream);
131 __archive_write_program_free(data->pdata);
142 archive_compressor_zstd_options(struct archive_write_filter *f, const char *key,
145 struct private_data *data = (struct private_data *)f->data;
147 if (strcmp(key, "compression-level") == 0) {
148 int level = atoi(value);
149 #if HAVE_ZSTD_H && HAVE_LIBZSTD
150 if (level < 1 || level > ZSTD_maxCLevel()) {
152 /* If we don't have the library, hard-code the max level */
153 if (level < 1 || level > 22) {
155 return (ARCHIVE_WARN);
157 data->compression_level = level;
161 /* Note: The "warn" return is just to inform the options
162 * supervisor that we didn't handle it. It will generate
163 * a suitable error if no one used this option. */
164 return (ARCHIVE_WARN);
167 #if HAVE_ZSTD_H && HAVE_LIBZSTD
172 archive_compressor_zstd_open(struct archive_write_filter *f)
174 struct private_data *data = (struct private_data *)f->data;
177 ret = __archive_write_open_filter(f->next_filter);
178 if (ret != ARCHIVE_OK)
181 if (data->out.dst == NULL) {
182 size_t bs = ZSTD_CStreamOutSize(), bpb;
183 if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
184 /* Buffer size should be a multiple number of
185 * the of bytes per block for performance. */
186 bpb = archive_write_get_bytes_per_block(f->archive);
195 = (unsigned char *)malloc(data->out.size);
196 if (data->out.dst == NULL) {
197 archive_set_error(f->archive, ENOMEM,
198 "Can't allocate data for compression buffer");
199 return (ARCHIVE_FATAL);
203 f->write = archive_compressor_zstd_write;
205 if (ZSTD_isError(ZSTD_initCStream(data->cstream,
206 data->compression_level))) {
207 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
208 "Internal error initializing zstd compressor object");
209 return (ARCHIVE_FATAL);
216 * Write data to the compressed stream.
219 archive_compressor_zstd_write(struct archive_write_filter *f, const void *buff,
222 struct private_data *data = (struct private_data *)f->data;
225 /* Update statistics */
226 data->total_in += length;
228 if ((ret = drive_compressor(f, data, 0, buff, length)) != ARCHIVE_OK)
235 * Finish the compression...
238 archive_compressor_zstd_close(struct archive_write_filter *f)
240 struct private_data *data = (struct private_data *)f->data;
243 /* Finish zstd frame */
244 r1 = drive_compressor(f, data, 1, NULL, 0);
246 r2 = __archive_write_close_filter(f->next_filter);
248 return r1 < r2 ? r1 : r2;
252 * Utility function to push input data through compressor,
253 * writing full output blocks as necessary.
255 * Note that this handles both the regular write case (finishing ==
256 * false) and the end-of-archive case (finishing == true).
259 drive_compressor(struct archive_write_filter *f,
260 struct private_data *data, int finishing, const void *src, size_t length)
262 ZSTD_inBuffer in = (ZSTD_inBuffer) { src, length, 0 };
265 if (data->out.pos == data->out.size) {
266 const int ret = __archive_write_filter(f->next_filter,
267 data->out.dst, data->out.size);
268 if (ret != ARCHIVE_OK)
269 return (ARCHIVE_FATAL);
273 /* If there's nothing to do, we're done. */
274 if (!finishing && in.pos == in.size)
278 const size_t zstdret = !finishing ?
279 ZSTD_compressStream(data->cstream, &data->out, &in)
280 : ZSTD_endStream(data->cstream, &data->out);
282 if (ZSTD_isError(zstdret)) {
283 archive_set_error(f->archive,
285 "Zstd compression failed: %s",
286 ZSTD_getErrorName(zstdret));
287 return (ARCHIVE_FATAL);
290 /* If we're finishing, 0 means nothing left to flush */
291 if (finishing && zstdret == 0) {
292 const int ret = __archive_write_filter(f->next_filter,
293 data->out.dst, data->out.pos);
300 #else /* HAVE_ZSTD_H && HAVE_LIBZSTD */
303 archive_compressor_zstd_open(struct archive_write_filter *f)
305 struct private_data *data = (struct private_data *)f->data;
306 struct archive_string as;
309 archive_string_init(&as);
310 archive_string_sprintf(&as, "zstd -%d", data->compression_level);
312 f->write = archive_compressor_zstd_write;
313 r = __archive_write_program_open(f, data->pdata, as.s);
314 archive_string_free(&as);
319 archive_compressor_zstd_write(struct archive_write_filter *f, const void *buff,
322 struct private_data *data = (struct private_data *)f->data;
324 return __archive_write_program_write(f, data->pdata, buff, length);
328 archive_compressor_zstd_close(struct archive_write_filter *f)
330 struct private_data *data = (struct private_data *)f->data;
332 return __archive_write_program_close(f, data->pdata);
335 #endif /* HAVE_ZSTD_H && HAVE_LIBZSTD */