xref: /freebsd/contrib/libarchive/libarchive/archive_write_add_filter_xz.c (revision eb5165bb491138f60d9004bc4c781490016d9288)
1 /*-
2  * Copyright (c) 2003-2010 Tim Kientzle
3  * Copyright (c) 2009-2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "archive_platform.h"
28 
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #include <time.h>
42 #ifdef HAVE_LZMA_H
43 #include <lzma.h>
44 #endif
45 
46 #include "archive.h"
47 #include "archive_endian.h"
48 #include "archive_private.h"
49 #include "archive_write_private.h"
50 
51 #if ARCHIVE_VERSION_NUMBER < 4000000
52 int
archive_write_set_compression_lzip(struct archive * a)53 archive_write_set_compression_lzip(struct archive *a)
54 {
55 	__archive_write_filters_free(a);
56 	return (archive_write_add_filter_lzip(a));
57 }
58 
59 int
archive_write_set_compression_lzma(struct archive * a)60 archive_write_set_compression_lzma(struct archive *a)
61 {
62 	__archive_write_filters_free(a);
63 	return (archive_write_add_filter_lzma(a));
64 }
65 
66 int
archive_write_set_compression_xz(struct archive * a)67 archive_write_set_compression_xz(struct archive *a)
68 {
69 	__archive_write_filters_free(a);
70 	return (archive_write_add_filter_xz(a));
71 }
72 
73 #endif
74 
75 #ifndef HAVE_LZMA_H
76 int
archive_write_add_filter_xz(struct archive * a)77 archive_write_add_filter_xz(struct archive *a)
78 {
79 	archive_set_error(a, ARCHIVE_ERRNO_MISC,
80 	    "xz compression not supported on this platform");
81 	return (ARCHIVE_FATAL);
82 }
83 
84 int
archive_write_add_filter_lzma(struct archive * a)85 archive_write_add_filter_lzma(struct archive *a)
86 {
87 	archive_set_error(a, ARCHIVE_ERRNO_MISC,
88 	    "lzma compression not supported on this platform");
89 	return (ARCHIVE_FATAL);
90 }
91 
92 int
archive_write_add_filter_lzip(struct archive * a)93 archive_write_add_filter_lzip(struct archive *a)
94 {
95 	archive_set_error(a, ARCHIVE_ERRNO_MISC,
96 	    "lzma compression not supported on this platform");
97 	return (ARCHIVE_FATAL);
98 }
99 #else
100 /* Don't compile this if we don't have liblzma. */
101 
102 struct private_data {
103 	int		 compression_level;
104 	uint32_t	 threads;
105 	lzma_stream	 stream;
106 	lzma_filter	 lzmafilters[2];
107 	lzma_options_lzma lzma_opt;
108 	int64_t		 total_in;
109 	unsigned char	*compressed;
110 	size_t		 compressed_buffer_size;
111 	int64_t		 total_out;
112 	/* the CRC32 value of uncompressed data for lzip */
113 	uint32_t	 crc32;
114 };
115 
116 static int	archive_compressor_xz_options(struct archive_write_filter *,
117 		    const char *, const char *);
118 static int	archive_compressor_xz_open(struct archive_write_filter *);
119 static int	archive_compressor_xz_write(struct archive_write_filter *,
120 		    const void *, size_t);
121 static int	archive_compressor_xz_close(struct archive_write_filter *);
122 static int	archive_compressor_xz_free(struct archive_write_filter *);
123 static int	drive_compressor(struct archive_write_filter *,
124 		    struct private_data *, int finishing);
125 
126 struct option_value {
127 	uint32_t dict_size;
128 	uint32_t nice_len;
129 	lzma_match_finder mf;
130 };
131 static const struct option_value option_values[] = {
132 	{ 1 << 16, 32, LZMA_MF_HC3},
133 	{ 1 << 20, 32, LZMA_MF_HC3},
134 	{ 3 << 19, 32, LZMA_MF_HC4},
135 	{ 1 << 21, 32, LZMA_MF_BT4},
136 	{ 3 << 20, 32, LZMA_MF_BT4},
137 	{ 1 << 22, 32, LZMA_MF_BT4},
138 	{ 1 << 23, 64, LZMA_MF_BT4},
139 	{ 1 << 24, 64, LZMA_MF_BT4},
140 	{ 3 << 23, 64, LZMA_MF_BT4},
141 	{ 1 << 25, 64, LZMA_MF_BT4}
142 };
143 
144 static int
common_setup(struct archive_write_filter * f)145 common_setup(struct archive_write_filter *f)
146 {
147 	struct private_data *data;
148 	struct archive_write *a = (struct archive_write *)f->archive;
149 	data = calloc(1, sizeof(*data));
150 	if (data == NULL) {
151 		archive_set_error(&a->archive, ENOMEM, "Out of memory");
152 		return (ARCHIVE_FATAL);
153 	}
154 	f->data = data;
155 	data->compression_level = LZMA_PRESET_DEFAULT;
156 	data->threads = 1;
157 	f->open = &archive_compressor_xz_open;
158 	f->close = archive_compressor_xz_close;
159 	f->free = archive_compressor_xz_free;
160 	f->options = &archive_compressor_xz_options;
161 	return (ARCHIVE_OK);
162 }
163 
164 /*
165  * Add an xz compression filter to this write handle.
166  */
167 int
archive_write_add_filter_xz(struct archive * _a)168 archive_write_add_filter_xz(struct archive *_a)
169 {
170 	struct archive_write_filter *f;
171 	int r;
172 
173 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
174 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_xz");
175 	f = __archive_write_allocate_filter(_a);
176 	r = common_setup(f);
177 	if (r == ARCHIVE_OK) {
178 		f->code = ARCHIVE_FILTER_XZ;
179 		f->name = "xz";
180 	}
181 	return (r);
182 }
183 
184 /* LZMA is handled identically, we just need a different compression
185  * code set.  (The liblzma setup looks at the code to determine
186  * the one place that XZ and LZMA require different handling.) */
187 int
archive_write_add_filter_lzma(struct archive * _a)188 archive_write_add_filter_lzma(struct archive *_a)
189 {
190 	struct archive_write_filter *f;
191 	int r;
192 
193 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
194 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_lzma");
195 	f = __archive_write_allocate_filter(_a);
196 	r = common_setup(f);
197 	if (r == ARCHIVE_OK) {
198 		f->code = ARCHIVE_FILTER_LZMA;
199 		f->name = "lzma";
200 	}
201 	return (r);
202 }
203 
204 int
archive_write_add_filter_lzip(struct archive * _a)205 archive_write_add_filter_lzip(struct archive *_a)
206 {
207 	struct archive_write_filter *f;
208 	int r;
209 
210 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
211 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_lzip");
212 	f = __archive_write_allocate_filter(_a);
213 	r = common_setup(f);
214 	if (r == ARCHIVE_OK) {
215 		f->code = ARCHIVE_FILTER_LZIP;
216 		f->name = "lzip";
217 	}
218 	return (r);
219 }
220 
221 static int
archive_compressor_xz_init_stream(struct archive_write_filter * f,struct private_data * data)222 archive_compressor_xz_init_stream(struct archive_write_filter *f,
223     struct private_data *data)
224 {
225 	static const lzma_stream lzma_stream_init_data = LZMA_STREAM_INIT;
226 	int ret;
227 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
228 	lzma_mt mt_options;
229 #endif
230 
231 	data->stream = lzma_stream_init_data;
232 	data->stream.next_out = data->compressed;
233 	data->stream.avail_out = data->compressed_buffer_size;
234 	if (f->code == ARCHIVE_FILTER_XZ) {
235 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
236 		if (data->threads != 1) {
237 			memset(&mt_options, 0, sizeof(mt_options));
238 			mt_options.threads = data->threads;
239 			mt_options.timeout = 300;
240 			mt_options.filters = data->lzmafilters;
241 			mt_options.check = LZMA_CHECK_CRC64;
242 			ret = lzma_stream_encoder_mt(&(data->stream),
243 			    &mt_options);
244 		} else
245 #endif
246 			ret = lzma_stream_encoder(&(data->stream),
247 			    data->lzmafilters, LZMA_CHECK_CRC64);
248 	} else if (f->code == ARCHIVE_FILTER_LZMA) {
249 		ret = lzma_alone_encoder(&(data->stream), &data->lzma_opt);
250 	} else {	/* ARCHIVE_FILTER_LZIP */
251 		int dict_size = data->lzma_opt.dict_size;
252 		int ds, log2dic, wedges;
253 
254 		/* Calculate a coded dictionary size */
255 		if (dict_size < (1 << 12) || dict_size > (1 << 29)) {
256 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
257 			    "Unacceptable dictionary size for lzip: %d",
258 			    dict_size);
259 			return (ARCHIVE_FATAL);
260 		}
261 		for (log2dic = 29; log2dic >= 12; log2dic--) {
262 			if (dict_size & (1 << log2dic))
263 				break;
264 		}
265 		if (dict_size > (1 << log2dic)) {
266 			log2dic++;
267 			wedges =
268 			    ((1 << log2dic) - dict_size) / (1 << (log2dic - 4));
269 		} else
270 			wedges = 0;
271 		ds = ((wedges << 5) & 0xe0) | (log2dic & 0x1f);
272 
273 		data->crc32 = 0;
274 		/* Make a header */
275 		data->compressed[0] = 0x4C;
276 		data->compressed[1] = 0x5A;
277 		data->compressed[2] = 0x49;
278 		data->compressed[3] = 0x50;
279 		data->compressed[4] = 1;/* Version */
280 		data->compressed[5] = (unsigned char)ds;
281 		data->stream.next_out += 6;
282 		data->stream.avail_out -= 6;
283 
284 		ret = lzma_raw_encoder(&(data->stream), data->lzmafilters);
285 	}
286 	if (ret == LZMA_OK)
287 		return (ARCHIVE_OK);
288 
289 	switch (ret) {
290 	case LZMA_MEM_ERROR:
291 		archive_set_error(f->archive, ENOMEM,
292 		    "Internal error initializing compression library: "
293 		    "Cannot allocate memory");
294 		break;
295 	default:
296 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
297 		    "Internal error initializing compression library: "
298 		    "It's a bug in liblzma");
299 		break;
300 	}
301 	return (ARCHIVE_FATAL);
302 }
303 
304 /*
305  * Setup callback.
306  */
307 static int
archive_compressor_xz_open(struct archive_write_filter * f)308 archive_compressor_xz_open(struct archive_write_filter *f)
309 {
310 	struct private_data *data = f->data;
311 	int ret;
312 
313 	if (data->compressed == NULL) {
314 		size_t bs = 65536, bpb;
315 		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
316 			/* Buffer size should be a multiple number of the bytes
317 			 * per block for performance. */
318 			bpb = archive_write_get_bytes_per_block(f->archive);
319 			if (bpb > bs)
320 				bs = bpb;
321 			else if (bpb != 0)
322 				bs -= bs % bpb;
323 		}
324 		data->compressed_buffer_size = bs;
325 		data->compressed = malloc(data->compressed_buffer_size);
326 		if (data->compressed == NULL) {
327 			archive_set_error(f->archive, ENOMEM,
328 			    "Can't allocate data for compression buffer");
329 			return (ARCHIVE_FATAL);
330 		}
331 	}
332 
333 	f->write = archive_compressor_xz_write;
334 
335 	/* Initialize compression library. */
336 	if (f->code == ARCHIVE_FILTER_LZIP) {
337 		const struct option_value *val =
338 		    &option_values[data->compression_level];
339 
340 		data->lzma_opt.dict_size = val->dict_size;
341 		data->lzma_opt.preset_dict = NULL;
342 		data->lzma_opt.preset_dict_size = 0;
343 		data->lzma_opt.lc = LZMA_LC_DEFAULT;
344 		data->lzma_opt.lp = LZMA_LP_DEFAULT;
345 		data->lzma_opt.pb = LZMA_PB_DEFAULT;
346 		data->lzma_opt.mode =
347 		    data->compression_level<= 2? LZMA_MODE_FAST:LZMA_MODE_NORMAL;
348 		data->lzma_opt.nice_len = val->nice_len;
349 		data->lzma_opt.mf = val->mf;
350 		data->lzma_opt.depth = 0;
351 		data->lzmafilters[0].id = LZMA_FILTER_LZMA1;
352 		data->lzmafilters[0].options = &data->lzma_opt;
353 		data->lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
354 	} else {
355 		if (lzma_lzma_preset(&data->lzma_opt, data->compression_level)) {
356 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
357 			    "Internal error initializing compression library");
358 		}
359 		data->lzmafilters[0].id = LZMA_FILTER_LZMA2;
360 		data->lzmafilters[0].options = &data->lzma_opt;
361 		data->lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
362 	}
363 	ret = archive_compressor_xz_init_stream(f, data);
364 	if (ret == LZMA_OK) {
365 		f->data = data;
366 		return (0);
367 	}
368 	return (ARCHIVE_FATAL);
369 }
370 
371 /*
372  * Set write options.
373  */
374 static int
archive_compressor_xz_options(struct archive_write_filter * f,const char * key,const char * value)375 archive_compressor_xz_options(struct archive_write_filter *f,
376     const char *key, const char *value)
377 {
378 	struct private_data *data = (struct private_data *)f->data;
379 
380 	if (strcmp(key, "compression-level") == 0) {
381 		if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
382 		    value[1] != '\0') {
383 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
384 			    "compression-level invalid");
385 			return (ARCHIVE_FAILED);
386 		}
387 		data->compression_level = value[0] - '0';
388 		if (data->compression_level > 9)
389 			data->compression_level = 9;
390 		return (ARCHIVE_OK);
391 	} else if (strcmp(key, "threads") == 0) {
392 		char *endptr;
393 		unsigned long val;
394 
395 		if (value == NULL) {
396 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
397 			    "threads option requires an argument");
398 			return (ARCHIVE_FAILED);
399 		}
400 		errno = 0;
401 		val = strtoul(value, &endptr, 10);
402 		if (errno != 0 || *endptr != '\0' || val > (unsigned)INT_MAX) {
403 			data->threads = 1;
404 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
405 			    "threads invalid");
406 			return (ARCHIVE_FAILED);
407 		}
408 		data->threads = (int)val;
409 		if (data->threads == 0) {
410 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
411 			data->threads = lzma_cputhreads();
412 #else
413 			data->threads = 1;
414 #endif
415 		}
416 		return (ARCHIVE_OK);
417 	}
418 
419 	/* Note: The "warn" return is just to inform the options
420 	 * supervisor that we didn't handle it.  It will generate
421 	 * a suitable error if no one used this option. */
422 	return (ARCHIVE_WARN);
423 }
424 
425 /*
426  * Write data to the compressed stream.
427  */
428 static int
archive_compressor_xz_write(struct archive_write_filter * f,const void * buff,size_t length)429 archive_compressor_xz_write(struct archive_write_filter *f,
430     const void *buff, size_t length)
431 {
432 	struct private_data *data = (struct private_data *)f->data;
433 	int ret;
434 
435 	/* Update statistics */
436 	data->total_in += length;
437 	if (f->code == ARCHIVE_FILTER_LZIP)
438 		data->crc32 = lzma_crc32(buff, length, data->crc32);
439 
440 	/* Compress input data to output buffer */
441 	data->stream.next_in = buff;
442 	data->stream.avail_in = length;
443 	if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK)
444 		return (ret);
445 
446 	return (ARCHIVE_OK);
447 }
448 
449 
450 /*
451  * Finish the compression...
452  */
453 static int
archive_compressor_xz_close(struct archive_write_filter * f)454 archive_compressor_xz_close(struct archive_write_filter *f)
455 {
456 	struct private_data *data = (struct private_data *)f->data;
457 	int ret;
458 
459 	ret = drive_compressor(f, data, 1);
460 	if (ret == ARCHIVE_OK) {
461 		data->total_out +=
462 		    data->compressed_buffer_size - data->stream.avail_out;
463 		ret = __archive_write_filter(f->next_filter,
464 		    data->compressed,
465 		    data->compressed_buffer_size - data->stream.avail_out);
466 		if (f->code == ARCHIVE_FILTER_LZIP && ret == ARCHIVE_OK) {
467 			archive_le32enc(data->compressed, data->crc32);
468 			archive_le64enc(data->compressed+4, data->total_in);
469 			archive_le64enc(data->compressed+12, data->total_out + 20);
470 			ret = __archive_write_filter(f->next_filter,
471 			    data->compressed, 20);
472 		}
473 	}
474 	lzma_end(&(data->stream));
475 	return ret;
476 }
477 
478 static int
archive_compressor_xz_free(struct archive_write_filter * f)479 archive_compressor_xz_free(struct archive_write_filter *f)
480 {
481 	struct private_data *data = (struct private_data *)f->data;
482 	free(data->compressed);
483 	free(data);
484 	f->data = NULL;
485 	return (ARCHIVE_OK);
486 }
487 
488 /*
489  * Utility function to push input data through compressor,
490  * writing full output blocks as necessary.
491  *
492  * Note that this handles both the regular write case (finishing ==
493  * false) and the end-of-archive case (finishing == true).
494  */
495 static int
drive_compressor(struct archive_write_filter * f,struct private_data * data,int finishing)496 drive_compressor(struct archive_write_filter *f,
497     struct private_data *data, int finishing)
498 {
499 	int ret;
500 
501 	for (;;) {
502 		if (data->stream.avail_out == 0) {
503 			data->total_out += data->compressed_buffer_size;
504 			ret = __archive_write_filter(f->next_filter,
505 			    data->compressed,
506 			    data->compressed_buffer_size);
507 			if (ret != ARCHIVE_OK)
508 				return (ARCHIVE_FATAL);
509 			data->stream.next_out = data->compressed;
510 			data->stream.avail_out = data->compressed_buffer_size;
511 		}
512 
513 		/* If there's nothing to do, we're done. */
514 		if (!finishing && data->stream.avail_in == 0)
515 			return (ARCHIVE_OK);
516 
517 		ret = lzma_code(&(data->stream),
518 		    finishing ? LZMA_FINISH : LZMA_RUN );
519 
520 		switch (ret) {
521 		case LZMA_OK:
522 			/* In non-finishing case, check if compressor
523 			 * consumed everything */
524 			if (!finishing && data->stream.avail_in == 0)
525 				return (ARCHIVE_OK);
526 			/* In finishing case, this return always means
527 			 * there's more work */
528 			break;
529 		case LZMA_STREAM_END:
530 			/* This return can only occur in finishing case. */
531 			if (finishing)
532 				return (ARCHIVE_OK);
533 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
534 			    "lzma compression data error");
535 			return (ARCHIVE_FATAL);
536 		case LZMA_MEMLIMIT_ERROR:
537 			archive_set_error(f->archive, ENOMEM,
538 			    "lzma compression error: "
539 			    "%ju MiB would have been needed",
540 			    (uintmax_t)((lzma_memusage(&(data->stream))
541 				    + 1024 * 1024 -1)
542 				/ (1024 * 1024)));
543 			return (ARCHIVE_FATAL);
544 		default:
545 			/* Any other return value indicates an error. */
546 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
547 			    "lzma compression failed:"
548 			    " lzma_code() call returned status %d",
549 			    ret);
550 			return (ARCHIVE_FATAL);
551 		}
552 	}
553 }
554 
555 #endif /* HAVE_LZMA_H */
556