1 /*-
2 * Copyright (c) 2017 Sean Purcell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26 #include "archive_platform.h"
27
28 __FBSDID("$FreeBSD$");
29
30
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_ZSTD_H
41 #include <cm3p/zstd.h>
42 #endif
43
44 #include "archive.h"
45 #include "archive_private.h"
46 #include "archive_string.h"
47 #include "archive_write_private.h"
48
49 /* Don't compile this if we don't have zstd.h */
50
51 struct private_data {
52 int compression_level;
53 #if HAVE_ZSTD_H && HAVE_LIBZSTD
54 ZSTD_CStream *cstream;
55 int64_t total_in;
56 ZSTD_outBuffer out;
57 #else
58 struct archive_write_program_data *pdata;
59 #endif
60 };
61
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 */
67 #define CLEVEL_MAX 22
68
69 #define MINVER_NEGCLEVEL 10304
70 #define MINVER_MINCLEVEL 10306
71
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);
82 #endif
83
84
85 /*
86 * Add a zstd compression filter to this write handle.
87 */
88 int
archive_write_add_filter_zstd(struct archive * _a)89 archive_write_add_filter_zstd(struct archive *_a)
90 {
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");
96
97 data = calloc(1, sizeof(*data));
98 if (data == NULL) {
99 archive_set_error(&a->archive, ENOMEM, "Out of memory");
100 return (ARCHIVE_FATAL);
101 }
102 f->data = data;
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;
108 f->name = "zstd";
109 data->compression_level = CLEVEL_DEFAULT;
110 #if HAVE_ZSTD_H && HAVE_LIBZSTD
111 data->cstream = ZSTD_createCStream();
112 if (data->cstream == NULL) {
113 free(data);
114 archive_set_error(&a->archive, ENOMEM,
115 "Failed to allocate zstd compressor object");
116 return (ARCHIVE_FATAL);
117 }
118
119 return (ARCHIVE_OK);
120 #else
121 data->pdata = __archive_write_program_allocate("zstd");
122 if (data->pdata == NULL) {
123 free(data);
124 archive_set_error(&a->archive, ENOMEM, "Out of memory");
125 return (ARCHIVE_FATAL);
126 }
127 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
128 "Using external zstd program");
129 return (ARCHIVE_WARN);
130 #endif
131 }
132
133 static int
archive_compressor_zstd_free(struct archive_write_filter * f)134 archive_compressor_zstd_free(struct archive_write_filter *f)
135 {
136 struct private_data *data = (struct private_data *)f->data;
137 #if HAVE_ZSTD_H && HAVE_LIBZSTD
138 ZSTD_freeCStream(data->cstream);
139 free(data->out.dst);
140 #else
141 __archive_write_program_free(data->pdata);
142 #endif
143 free(data);
144 f->data = NULL;
145 return (ARCHIVE_OK);
146 }
147
string_is_numeric(const char * value)148 static int string_is_numeric (const char* value)
149 {
150 size_t len = strlen(value);
151 size_t i;
152
153 if (len == 0) {
154 return (ARCHIVE_WARN);
155 }
156 else if (len == 1 && !(value[0] >= '0' && value[0] <= '9')) {
157 return (ARCHIVE_WARN);
158 }
159 else if (!(value[0] >= '0' && value[0] <= '9') &&
160 value[0] != '-' && value[0] != '+') {
161 return (ARCHIVE_WARN);
162 }
163
164 for (i = 1; i < len; i++) {
165 if (!(value[i] >= '0' && value[i] <= '9')) {
166 return (ARCHIVE_WARN);
167 }
168 }
169
170 return (ARCHIVE_OK);
171 }
172
173 /*
174 * Set write options.
175 */
176 static int
archive_compressor_zstd_options(struct archive_write_filter * f,const char * key,const char * value)177 archive_compressor_zstd_options(struct archive_write_filter *f, const char *key,
178 const char *value)
179 {
180 struct private_data *data = (struct private_data *)f->data;
181
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);
189 }
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();
195 }
196 else
197 #endif
198 if (ZSTD_versionNumber() < MINVER_NEGCLEVEL) {
199 minimum = CLEVEL_STD_MIN;
200 }
201 #endif
202 if (level < minimum || level > maximum) {
203 return (ARCHIVE_WARN);
204 }
205 data->compression_level = level;
206 return (ARCHIVE_OK);
207 }
208
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);
213 }
214
215 #if HAVE_ZSTD_H && HAVE_LIBZSTD
216 /*
217 * Setup callback.
218 */
219 static int
archive_compressor_zstd_open(struct archive_write_filter * f)220 archive_compressor_zstd_open(struct archive_write_filter *f)
221 {
222 struct private_data *data = (struct private_data *)f->data;
223
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);
230 if (bpb > bs)
231 bs = bpb;
232 else if (bpb != 0)
233 bs -= bs % bpb;
234 }
235 data->out.size = bs;
236 data->out.pos = 0;
237 data->out.dst
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);
243 }
244 }
245
246 f->write = archive_compressor_zstd_write;
247
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);
253 }
254
255 return (ARCHIVE_OK);
256 }
257
258 /*
259 * Write data to the compressed stream.
260 */
261 static int
archive_compressor_zstd_write(struct archive_write_filter * f,const void * buff,size_t length)262 archive_compressor_zstd_write(struct archive_write_filter *f, const void *buff,
263 size_t length)
264 {
265 struct private_data *data = (struct private_data *)f->data;
266 int ret;
267
268 /* Update statistics */
269 data->total_in += length;
270
271 if ((ret = drive_compressor(f, data, 0, buff, length)) != ARCHIVE_OK)
272 return (ret);
273
274 return (ARCHIVE_OK);
275 }
276
277 /*
278 * Finish the compression...
279 */
280 static int
archive_compressor_zstd_close(struct archive_write_filter * f)281 archive_compressor_zstd_close(struct archive_write_filter *f)
282 {
283 struct private_data *data = (struct private_data *)f->data;
284
285 /* Finish zstd frame */
286 return drive_compressor(f, data, 1, NULL, 0);
287 }
288
289 /*
290 * Utility function to push input data through compressor,
291 * writing full output blocks as necessary.
292 *
293 * Note that this handles both the regular write case (finishing ==
294 * false) and the end-of-archive case (finishing == true).
295 */
296 static int
drive_compressor(struct archive_write_filter * f,struct private_data * data,int finishing,const void * src,size_t length)297 drive_compressor(struct archive_write_filter *f,
298 struct private_data *data, int finishing, const void *src, size_t length)
299 {
300 ZSTD_inBuffer in = (ZSTD_inBuffer) { src, length, 0 };
301
302 for (;;) {
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);
308 data->out.pos = 0;
309 }
310
311 /* If there's nothing to do, we're done. */
312 if (!finishing && in.pos == in.size)
313 return (ARCHIVE_OK);
314
315 {
316 const size_t zstdret = !finishing ?
317 ZSTD_compressStream(data->cstream, &data->out, &in)
318 : ZSTD_endStream(data->cstream, &data->out);
319
320 if (ZSTD_isError(zstdret)) {
321 archive_set_error(f->archive,
322 ARCHIVE_ERRNO_MISC,
323 "Zstd compression failed: %s",
324 ZSTD_getErrorName(zstdret));
325 return (ARCHIVE_FATAL);
326 }
327
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);
332 return (ret);
333 }
334 }
335 }
336 }
337
338 #else /* HAVE_ZSTD_H && HAVE_LIBZSTD */
339
340 static int
archive_compressor_zstd_open(struct archive_write_filter * f)341 archive_compressor_zstd_open(struct archive_write_filter *f)
342 {
343 struct private_data *data = (struct private_data *)f->data;
344 struct archive_string as;
345 int r;
346
347 archive_string_init(&as);
348 /* --no-check matches library default */
349 archive_strcpy(&as, "zstd --no-check");
350
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);
357 } else {
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);
363 }
364
365 if (data->compression_level > CLEVEL_STD_MAX) {
366 archive_strcat(&as, " --ultra");
367 }
368
369 f->write = archive_compressor_zstd_write;
370 r = __archive_write_program_open(f, data->pdata, as.s);
371 archive_string_free(&as);
372 return (r);
373 }
374
375 static int
archive_compressor_zstd_write(struct archive_write_filter * f,const void * buff,size_t length)376 archive_compressor_zstd_write(struct archive_write_filter *f, const void *buff,
377 size_t length)
378 {
379 struct private_data *data = (struct private_data *)f->data;
380
381 return __archive_write_program_write(f, data->pdata, buff, length);
382 }
383
384 static int
archive_compressor_zstd_close(struct archive_write_filter * f)385 archive_compressor_zstd_close(struct archive_write_filter *f)
386 {
387 struct private_data *data = (struct private_data *)f->data;
388
389 return __archive_write_program_close(f, data->pdata);
390 }
391
392 #endif /* HAVE_ZSTD_H && HAVE_LIBZSTD */
393