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 xz {
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 xz *, int finishing);
125 static void free_data(struct xz *);
126
127 struct option_value {
128 uint32_t dict_size;
129 uint32_t nice_len;
130 lzma_match_finder mf;
131 };
132 static const struct option_value option_values[] = {
133 { 1 << 16, 32, LZMA_MF_HC3},
134 { 1 << 20, 32, LZMA_MF_HC3},
135 { 3 << 19, 32, LZMA_MF_HC4},
136 { 1 << 21, 32, LZMA_MF_BT4},
137 { 3 << 20, 32, LZMA_MF_BT4},
138 { 1 << 22, 32, LZMA_MF_BT4},
139 { 1 << 23, 64, LZMA_MF_BT4},
140 { 1 << 24, 64, LZMA_MF_BT4},
141 { 3 << 23, 64, LZMA_MF_BT4},
142 { 1 << 25, 64, LZMA_MF_BT4}
143 };
144
145 static int
common_setup(struct archive * a,const char * name,int code)146 common_setup(struct archive *a, const char *name, int code)
147 {
148 struct xz *xz;
149 struct archive_write_filter *f;
150
151 xz = calloc(1, sizeof(*xz));
152 if (xz == NULL)
153 goto memerr;
154 xz->compression_level = LZMA_PRESET_DEFAULT;
155 xz->threads = 1;
156
157 f = __archive_write_allocate_filter(a);
158 if (f == NULL)
159 goto memerr;
160 f->name = name;
161 f->code = code;
162 f->data = xz;
163 f->options = archive_compressor_xz_options;
164 f->open = archive_compressor_xz_open;
165 f->write = archive_compressor_xz_write;
166 f->close = archive_compressor_xz_close;
167 f->free = archive_compressor_xz_free;
168
169 return (ARCHIVE_OK);
170 memerr:
171 free_data(xz);
172 archive_set_error(a, ENOMEM, "Out of memory");
173 return (ARCHIVE_FATAL);
174 }
175
176 /*
177 * Add an xz compression filter to this write handle.
178 */
179 int
archive_write_add_filter_xz(struct archive * a)180 archive_write_add_filter_xz(struct archive *a)
181 {
182 archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
183 ARCHIVE_STATE_NEW, "archive_write_add_filter_xz");
184
185 return common_setup(a, "xz", ARCHIVE_FILTER_XZ);
186 }
187
188 /* LZMA is handled identically, we just need a different compression
189 * code set. (The liblzma setup looks at the code to determine
190 * the one place that XZ and LZMA require different handling.) */
191 int
archive_write_add_filter_lzma(struct archive * a)192 archive_write_add_filter_lzma(struct archive *a)
193 {
194 archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
195 ARCHIVE_STATE_NEW, "archive_write_add_filter_lzma");
196
197 return common_setup(a, "lzma", ARCHIVE_FILTER_LZMA);
198 }
199
200 int
archive_write_add_filter_lzip(struct archive * a)201 archive_write_add_filter_lzip(struct archive *a)
202 {
203 archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
204 ARCHIVE_STATE_NEW, "archive_write_add_filter_lzip");
205
206 return common_setup(a, "lzip", ARCHIVE_FILTER_LZIP);
207 }
208
209 static int
archive_compressor_xz_init_stream(struct archive_write_filter * f,struct xz * xz)210 archive_compressor_xz_init_stream(struct archive_write_filter *f,
211 struct xz *xz)
212 {
213 static const lzma_stream lzma_stream_init_data = LZMA_STREAM_INIT;
214 int ret;
215 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
216 lzma_mt mt_options;
217 #endif
218
219 xz->stream = lzma_stream_init_data;
220 xz->stream.next_out = xz->compressed;
221 xz->stream.avail_out = xz->compressed_buffer_size;
222 if (f->code == ARCHIVE_FILTER_XZ) {
223 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
224 if (xz->threads != 1) {
225 memset(&mt_options, 0, sizeof(mt_options));
226 mt_options.threads = xz->threads;
227 mt_options.timeout = 300;
228 mt_options.filters = xz->lzmafilters;
229 mt_options.check = LZMA_CHECK_CRC64;
230 ret = lzma_stream_encoder_mt(&(xz->stream),
231 &mt_options);
232 } else
233 #endif
234 ret = lzma_stream_encoder(&(xz->stream),
235 xz->lzmafilters, LZMA_CHECK_CRC64);
236 } else if (f->code == ARCHIVE_FILTER_LZMA) {
237 ret = lzma_alone_encoder(&(xz->stream), &xz->lzma_opt);
238 } else { /* ARCHIVE_FILTER_LZIP */
239 int dict_size = xz->lzma_opt.dict_size;
240 int ds, log2dic, wedges;
241
242 /* Calculate a coded dictionary size */
243 if (dict_size < (1 << 12) || dict_size > (1 << 29)) {
244 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
245 "Unacceptable dictionary size for lzip: %d",
246 dict_size);
247 return (ARCHIVE_FATAL);
248 }
249 for (log2dic = 29; log2dic >= 12; log2dic--) {
250 if (dict_size & (1 << log2dic))
251 break;
252 }
253 if (dict_size > (1 << log2dic)) {
254 log2dic++;
255 wedges =
256 ((1 << log2dic) - dict_size) / (1 << (log2dic - 4));
257 } else
258 wedges = 0;
259 ds = ((wedges << 5) & 0xe0) | (log2dic & 0x1f);
260
261 xz->crc32 = 0;
262 /* Make a header */
263 xz->compressed[0] = 0x4C;
264 xz->compressed[1] = 0x5A;
265 xz->compressed[2] = 0x49;
266 xz->compressed[3] = 0x50;
267 xz->compressed[4] = 1;/* Version */
268 xz->compressed[5] = (unsigned char)ds;
269 xz->stream.next_out += 6;
270 xz->stream.avail_out -= 6;
271
272 ret = lzma_raw_encoder(&(xz->stream), xz->lzmafilters);
273 }
274 if (ret == LZMA_OK)
275 return (ARCHIVE_OK);
276
277 switch (ret) {
278 case LZMA_MEM_ERROR:
279 archive_set_error(f->archive, ENOMEM,
280 "Internal error initializing compression library: "
281 "Cannot allocate memory");
282 break;
283 default:
284 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
285 "Internal error initializing compression library: "
286 "It's a bug in liblzma");
287 break;
288 }
289 return (ARCHIVE_FATAL);
290 }
291
292 /*
293 * Setup callback.
294 */
295 static int
archive_compressor_xz_open(struct archive_write_filter * f)296 archive_compressor_xz_open(struct archive_write_filter *f)
297 {
298 struct xz *xz = f->data;
299 int ret;
300
301 if (xz->compressed == NULL) {
302 size_t bs = 65536, bpb;
303 if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
304 /* Buffer size should be a multiple number of the bytes
305 * per block for performance. */
306 bpb = archive_write_get_bytes_per_block(f->archive);
307 if (bpb > bs)
308 bs = bpb;
309 else if (bpb != 0)
310 bs -= bs % bpb;
311 }
312 xz->compressed_buffer_size = bs;
313 xz->compressed = malloc(xz->compressed_buffer_size);
314 if (xz->compressed == NULL) {
315 archive_set_error(f->archive, ENOMEM,
316 "Can't allocate data for compression buffer");
317 return (ARCHIVE_FATAL);
318 }
319 }
320
321 /* Initialize compression library. */
322 if (f->code == ARCHIVE_FILTER_LZIP) {
323 const struct option_value *val =
324 &option_values[xz->compression_level];
325
326 xz->lzma_opt.dict_size = val->dict_size;
327 xz->lzma_opt.preset_dict = NULL;
328 xz->lzma_opt.preset_dict_size = 0;
329 xz->lzma_opt.lc = LZMA_LC_DEFAULT;
330 xz->lzma_opt.lp = LZMA_LP_DEFAULT;
331 xz->lzma_opt.pb = LZMA_PB_DEFAULT;
332 xz->lzma_opt.mode =
333 xz->compression_level<= 2? LZMA_MODE_FAST:LZMA_MODE_NORMAL;
334 xz->lzma_opt.nice_len = val->nice_len;
335 xz->lzma_opt.mf = val->mf;
336 xz->lzma_opt.depth = 0;
337 xz->lzmafilters[0].id = LZMA_FILTER_LZMA1;
338 xz->lzmafilters[0].options = &xz->lzma_opt;
339 xz->lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
340 } else {
341 if (lzma_lzma_preset(&xz->lzma_opt, xz->compression_level)) {
342 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
343 "Internal error initializing compression library");
344 }
345 xz->lzmafilters[0].id = LZMA_FILTER_LZMA2;
346 xz->lzmafilters[0].options = &xz->lzma_opt;
347 xz->lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
348 }
349 ret = archive_compressor_xz_init_stream(f, xz);
350 if (ret == LZMA_OK) {
351 return (ARCHIVE_OK);
352 }
353 return (ARCHIVE_FATAL);
354 }
355
356 /*
357 * Set write options.
358 */
359 static int
archive_compressor_xz_options(struct archive_write_filter * f,const char * key,const char * value)360 archive_compressor_xz_options(struct archive_write_filter *f,
361 const char *key, const char *value)
362 {
363 struct xz *xz = f->data;
364
365 if (strcmp(key, "compression-level") == 0) {
366 if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
367 value[1] != '\0') {
368 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
369 "compression-level invalid");
370 return (ARCHIVE_FAILED);
371 }
372 xz->compression_level = value[0] - '0';
373 if (xz->compression_level > 9)
374 xz->compression_level = 9;
375 return (ARCHIVE_OK);
376 } else if (strcmp(key, "threads") == 0) {
377 char *endptr;
378 unsigned long val;
379
380 if (value == NULL) {
381 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
382 "threads option requires an argument");
383 return (ARCHIVE_FAILED);
384 }
385 errno = 0;
386 val = strtoul(value, &endptr, 10);
387 if (errno != 0 || *endptr != '\0' || val > (unsigned)INT_MAX) {
388 xz->threads = 1;
389 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
390 "threads invalid");
391 return (ARCHIVE_FAILED);
392 }
393 xz->threads = (int)val;
394 if (xz->threads == 0) {
395 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
396 xz->threads = lzma_cputhreads();
397 #else
398 xz->threads = 1;
399 #endif
400 }
401 return (ARCHIVE_OK);
402 }
403
404 /* Note: The "warn" return is just to inform the options
405 * supervisor that we didn't handle it. It will generate
406 * a suitable error if no one used this option. */
407 return (ARCHIVE_WARN);
408 }
409
410 /*
411 * Write data to the compressed stream.
412 */
413 static int
archive_compressor_xz_write(struct archive_write_filter * f,const void * buff,size_t length)414 archive_compressor_xz_write(struct archive_write_filter *f,
415 const void *buff, size_t length)
416 {
417 struct xz *xz = f->data;
418 int ret;
419
420 /* Update statistics */
421 xz->total_in += length;
422 if (f->code == ARCHIVE_FILTER_LZIP)
423 xz->crc32 = lzma_crc32(buff, length, xz->crc32);
424
425 /* Compress input data to output buffer */
426 xz->stream.next_in = buff;
427 xz->stream.avail_in = length;
428 if ((ret = drive_compressor(f, xz, 0)) != ARCHIVE_OK)
429 return (ret);
430
431 return (ARCHIVE_OK);
432 }
433
434
435 /*
436 * Finish the compression...
437 */
438 static int
archive_compressor_xz_close(struct archive_write_filter * f)439 archive_compressor_xz_close(struct archive_write_filter *f)
440 {
441 struct xz *xz = f->data;
442 int ret;
443
444 ret = drive_compressor(f, xz, 1);
445 if (ret == ARCHIVE_OK) {
446 xz->total_out +=
447 xz->compressed_buffer_size - xz->stream.avail_out;
448 ret = __archive_write_filter(f->next_filter,
449 xz->compressed,
450 xz->compressed_buffer_size - xz->stream.avail_out);
451 if (f->code == ARCHIVE_FILTER_LZIP && ret == ARCHIVE_OK) {
452 archive_le32enc(xz->compressed, xz->crc32);
453 archive_le64enc(xz->compressed+4, xz->total_in);
454 archive_le64enc(xz->compressed+12, xz->total_out + 20);
455 ret = __archive_write_filter(f->next_filter,
456 xz->compressed, 20);
457 }
458 }
459 lzma_end(&(xz->stream));
460 return ret;
461 }
462
463 static int
archive_compressor_xz_free(struct archive_write_filter * f)464 archive_compressor_xz_free(struct archive_write_filter *f)
465 {
466 free_data(f->data);
467 f->data = NULL;
468 return (ARCHIVE_OK);
469 }
470
471 /*
472 * Utility function to push input data through compressor,
473 * writing full output blocks as necessary.
474 *
475 * Note that this handles both the regular write case (finishing ==
476 * false) and the end-of-archive case (finishing == true).
477 */
478 static int
drive_compressor(struct archive_write_filter * f,struct xz * xz,int finishing)479 drive_compressor(struct archive_write_filter *f,
480 struct xz *xz, int finishing)
481 {
482 int ret;
483
484 for (;;) {
485 if (xz->stream.avail_out == 0) {
486 xz->total_out += xz->compressed_buffer_size;
487 ret = __archive_write_filter(f->next_filter,
488 xz->compressed,
489 xz->compressed_buffer_size);
490 if (ret != ARCHIVE_OK)
491 return (ARCHIVE_FATAL);
492 xz->stream.next_out = xz->compressed;
493 xz->stream.avail_out = xz->compressed_buffer_size;
494 }
495
496 /* If there's nothing to do, we're done. */
497 if (!finishing && xz->stream.avail_in == 0)
498 return (ARCHIVE_OK);
499
500 ret = lzma_code(&(xz->stream),
501 finishing ? LZMA_FINISH : LZMA_RUN );
502
503 switch (ret) {
504 case LZMA_OK:
505 /* In non-finishing case, check if compressor
506 * consumed everything */
507 if (!finishing && xz->stream.avail_in == 0)
508 return (ARCHIVE_OK);
509 /* In finishing case, this return always means
510 * there's more work */
511 break;
512 case LZMA_STREAM_END:
513 /* This return can only occur in finishing case. */
514 if (finishing)
515 return (ARCHIVE_OK);
516 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
517 "lzma compression data error");
518 return (ARCHIVE_FATAL);
519 case LZMA_MEMLIMIT_ERROR:
520 archive_set_error(f->archive, ENOMEM,
521 "lzma compression error: "
522 "%ju MiB would have been needed",
523 (uintmax_t)((lzma_memusage(&(xz->stream))
524 + 1024 * 1024 -1)
525 / (1024 * 1024)));
526 return (ARCHIVE_FATAL);
527 default:
528 /* Any other return value indicates an error. */
529 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
530 "lzma compression failed:"
531 " lzma_code() call returned status %d",
532 ret);
533 return (ARCHIVE_FATAL);
534 }
535 }
536 }
537
538 static void
free_data(struct xz * xz)539 free_data(struct xz *xz)
540 {
541 if (xz != NULL) {
542 free(xz->compressed);
543 free(xz);
544 }
545 }
546
547 #endif /* HAVE_LZMA_H */
548