2 * Copyright (c) 2009 Michihiro NAKAJIMA
3 * Copyright (c) 2003-2008 Tim Kientzle and Miklos Vajna
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
51 #include "archive_private.h"
52 #include "archive_read_private.h"
54 #if HAVE_LZMA_H && HAVE_LIBLZMA
58 unsigned char *out_block;
59 size_t out_block_size;
61 char eof; /* True = found end of compressed data. */
64 /* Combined lzma/xz filter */
65 static ssize_t xz_filter_read(struct archive_read_filter *, const void **);
66 static int xz_filter_close(struct archive_read_filter *);
67 static int xz_lzma_bidder_init(struct archive_read_filter *);
69 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
72 lzmadec_stream stream;
73 unsigned char *out_block;
74 size_t out_block_size;
76 char eof; /* True = found end of compressed data. */
79 /* Lzma-only filter */
80 static ssize_t lzma_filter_read(struct archive_read_filter *, const void **);
81 static int lzma_filter_close(struct archive_read_filter *);
85 * Note that we can detect xz and lzma compressed files even if we
86 * can't decompress them. (In fact, we like detecting them because we
87 * can give better error messages.) So the bid framework here gets
88 * compiled even if no lzma library is available.
90 static int xz_bidder_bid(struct archive_read_filter_bidder *,
91 struct archive_read_filter *);
92 static int xz_bidder_init(struct archive_read_filter *);
93 static int lzma_bidder_bid(struct archive_read_filter_bidder *,
94 struct archive_read_filter *);
95 static int lzma_bidder_init(struct archive_read_filter *);
98 archive_read_support_compression_xz(struct archive *_a)
100 struct archive_read *a = (struct archive_read *)_a;
101 struct archive_read_filter_bidder *bidder = __archive_read_get_bidder(a);
103 archive_clear_error(_a);
105 return (ARCHIVE_FATAL);
108 bidder->bid = xz_bidder_bid;
109 bidder->init = xz_bidder_init;
110 bidder->options = NULL;
112 #if HAVE_LZMA_H && HAVE_LIBLZMA
115 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
116 "Using external unxz program for xz decompression");
117 return (ARCHIVE_WARN);
122 archive_read_support_compression_lzma(struct archive *_a)
124 struct archive_read *a = (struct archive_read *)_a;
125 struct archive_read_filter_bidder *bidder = __archive_read_get_bidder(a);
127 archive_clear_error(_a);
129 return (ARCHIVE_FATAL);
132 bidder->bid = lzma_bidder_bid;
133 bidder->init = lzma_bidder_init;
134 bidder->options = NULL;
136 #if HAVE_LZMA_H && HAVE_LIBLZMA
138 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
141 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
142 "Using external unlzma program for lzma decompression");
143 return (ARCHIVE_WARN);
148 * Test whether we can handle this data.
151 xz_bidder_bid(struct archive_read_filter_bidder *self,
152 struct archive_read_filter *filter)
154 const unsigned char *buffer;
158 (void)self; /* UNUSED */
160 buffer = __archive_read_filter_ahead(filter, 6, &avail);
165 * Verify Header Magic Bytes : FD 37 7A 58 5A 00
168 if (buffer[0] != 0xFD)
171 if (buffer[1] != 0x37)
174 if (buffer[2] != 0x7A)
177 if (buffer[3] != 0x58)
180 if (buffer[4] != 0x5A)
183 if (buffer[5] != 0x00)
187 return (bits_checked);
191 * Test whether we can handle this data.
193 * <sigh> LZMA has a rather poor file signature. Zeros do not
194 * make good signature bytes as a rule, and the only non-zero byte
195 * here is an ASCII character. For example, an uncompressed tar
196 * archive whose first file is ']' would satisfy this check. It may
197 * be necessary to exclude LZMA from compression_all() because of
198 * this. Clients of libarchive would then have to explicitly enable
199 * LZMA checking instead of (or in addition to) compression_all() when
200 * they have other evidence (file name, command-line option) to go on.
203 lzma_bidder_bid(struct archive_read_filter_bidder *self,
204 struct archive_read_filter *filter)
206 const unsigned char *buffer;
210 (void)self; /* UNUSED */
212 buffer = __archive_read_filter_ahead(filter, 6, &avail);
216 /* First byte of raw LZMA stream is always 0x5d. */
218 if (buffer[0] != 0x5d)
222 /* Second through fifth bytes are dictionary code, stored in
223 * little-endian order. The two least-significant bytes are
225 if (buffer[1] != 0 || buffer[2] != 0)
229 /* ??? TODO: Fix this. ??? */
230 /* NSIS format check uses this, but I've seen tar.lzma
231 * archives where this byte is 0xff, not 0. Can it
232 * ever be anything other than 0 or 0xff?
240 /* TODO: The above test is still very weak. It would be
241 * good to do better. */
243 return (bits_checked);
246 #if HAVE_LZMA_H && HAVE_LIBLZMA
249 * liblzma 4.999.7 and later support both lzma and xz streams.
252 xz_bidder_init(struct archive_read_filter *self)
254 self->code = ARCHIVE_COMPRESSION_XZ;
256 return (xz_lzma_bidder_init(self));
260 lzma_bidder_init(struct archive_read_filter *self)
262 self->code = ARCHIVE_COMPRESSION_LZMA;
264 return (xz_lzma_bidder_init(self));
268 * Setup the callbacks.
271 xz_lzma_bidder_init(struct archive_read_filter *self)
273 static const size_t out_block_size = 64 * 1024;
275 struct private_data *state;
278 state = (struct private_data *)calloc(sizeof(*state), 1);
279 out_block = (unsigned char *)malloc(out_block_size);
280 if (state == NULL || out_block == NULL) {
281 archive_set_error(&self->archive->archive, ENOMEM,
282 "Can't allocate data for xz decompression");
285 return (ARCHIVE_FATAL);
289 state->out_block_size = out_block_size;
290 state->out_block = out_block;
291 self->read = xz_filter_read;
292 self->skip = NULL; /* not supported */
293 self->close = xz_filter_close;
295 state->stream.avail_in = 0;
297 state->stream.next_out = state->out_block;
298 state->stream.avail_out = state->out_block_size;
300 /* Initialize compression library.
301 * TODO: I don't know what value is best for memlimit.
302 * maybe, it needs to check memory size which
303 * running system has.
305 if (self->code == ARCHIVE_COMPRESSION_XZ)
306 ret = lzma_stream_decoder(&(state->stream),
307 (1U << 23) + (1U << 21),/* memlimit */
310 ret = lzma_alone_decoder(&(state->stream),
311 (1U << 23) + (1U << 21));/* memlimit */
316 /* Library setup failed: Choose an error message and clean up. */
319 archive_set_error(&self->archive->archive, ENOMEM,
320 "Internal error initializing compression library: "
321 "Cannot allocate memory");
323 case LZMA_OPTIONS_ERROR:
324 archive_set_error(&self->archive->archive,
326 "Internal error initializing compression library: "
327 "Invalid or unsupported options");
330 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
331 "Internal error initializing lzma library");
335 free(state->out_block);
338 return (ARCHIVE_FATAL);
342 * Return the next block of decompressed data.
345 xz_filter_read(struct archive_read_filter *self, const void **p)
347 struct private_data *state;
352 state = (struct private_data *)self->data;
354 /* Empty our output buffer. */
355 state->stream.next_out = state->out_block;
356 state->stream.avail_out = state->out_block_size;
358 /* Try to fill the output buffer. */
359 while (state->stream.avail_out > 0 && !state->eof) {
360 state->stream.next_in =
361 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
362 if (state->stream.next_in == NULL && avail_in < 0)
363 return (ARCHIVE_FATAL);
364 state->stream.avail_in = avail_in;
366 /* Decompress as much as we can in one pass. */
367 ret = lzma_code(&(state->stream),
368 (state->stream.avail_in == 0)? LZMA_FINISH: LZMA_RUN);
370 case LZMA_STREAM_END: /* Found end of stream. */
373 case LZMA_OK: /* Decompressor made some progress. */
374 __archive_read_filter_consume(self->upstream,
375 avail_in - state->stream.avail_in);
378 archive_set_error(&self->archive->archive, ENOMEM,
379 "Lzma library error: Cannot allocate memory");
380 return (ARCHIVE_FATAL);
381 case LZMA_MEMLIMIT_ERROR:
382 archive_set_error(&self->archive->archive, ENOMEM,
383 "Lzma library error: Out of memory");
384 return (ARCHIVE_FATAL);
385 case LZMA_FORMAT_ERROR:
386 archive_set_error(&self->archive->archive,
388 "Lzma library error: format not recognized");
389 return (ARCHIVE_FATAL);
390 case LZMA_OPTIONS_ERROR:
391 archive_set_error(&self->archive->archive,
393 "Lzma library error: Invalid options");
394 return (ARCHIVE_FATAL);
395 case LZMA_DATA_ERROR:
396 archive_set_error(&self->archive->archive,
398 "Lzma library error: Corrupted input data");
399 return (ARCHIVE_FATAL);
401 archive_set_error(&self->archive->archive,
403 "Lzma library error: No progress is possible");
404 return (ARCHIVE_FATAL);
406 /* Return an error. */
407 archive_set_error(&self->archive->archive,
409 "Lzma decompression failed: Unknown error");
410 return (ARCHIVE_FATAL);
414 decompressed = state->stream.next_out - state->out_block;
415 state->total_out += decompressed;
416 if (decompressed == 0)
419 *p = state->out_block;
420 return (decompressed);
424 * Clean up the decompressor.
427 xz_filter_close(struct archive_read_filter *self)
429 struct private_data *state;
431 state = (struct private_data *)self->data;
432 lzma_end(&(state->stream));
433 free(state->out_block);
440 #if HAVE_LZMADEC_H && HAVE_LIBLZMADEC
443 * If we have the older liblzmadec library, then we can handle
444 * LZMA streams but not XZ streams.
448 * Setup the callbacks.
451 lzma_bidder_init(struct archive_read_filter *self)
453 static const size_t out_block_size = 64 * 1024;
455 struct private_data *state;
456 ssize_t ret, avail_in;
458 self->code = ARCHIVE_COMPRESSION_LZMA;
461 state = (struct private_data *)calloc(sizeof(*state), 1);
462 out_block = (unsigned char *)malloc(out_block_size);
463 if (state == NULL || out_block == NULL) {
464 archive_set_error(&self->archive->archive, ENOMEM,
465 "Can't allocate data for lzma decompression");
468 return (ARCHIVE_FATAL);
472 state->out_block_size = out_block_size;
473 state->out_block = out_block;
474 self->read = lzma_filter_read;
475 self->skip = NULL; /* not supported */
476 self->close = lzma_filter_close;
478 /* Prime the lzma library with 18 bytes of input. */
479 state->stream.next_in = (unsigned char *)(uintptr_t)
480 __archive_read_filter_ahead(self->upstream, 18, &avail_in);
481 if (state->stream.next_in == NULL)
482 return (ARCHIVE_FATAL);
483 state->stream.avail_in = avail_in;
484 state->stream.next_out = state->out_block;
485 state->stream.avail_out = state->out_block_size;
487 /* Initialize compression library. */
488 ret = lzmadec_init(&(state->stream));
489 __archive_read_filter_consume(self->upstream,
490 avail_in - state->stream.avail_in);
491 if (ret == LZMADEC_OK)
494 /* Library setup failed: Clean up. */
495 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
496 "Internal error initializing lzma library");
498 /* Override the error message if we know what really went wrong. */
500 case LZMADEC_HEADER_ERROR:
501 archive_set_error(&self->archive->archive,
503 "Internal error initializing compression library: "
506 case LZMADEC_MEM_ERROR:
507 archive_set_error(&self->archive->archive, ENOMEM,
508 "Internal error initializing compression library: "
513 free(state->out_block);
516 return (ARCHIVE_FATAL);
520 * Return the next block of decompressed data.
523 lzma_filter_read(struct archive_read_filter *self, const void **p)
525 struct private_data *state;
527 ssize_t avail_in, ret;
529 state = (struct private_data *)self->data;
531 /* Empty our output buffer. */
532 state->stream.next_out = state->out_block;
533 state->stream.avail_out = state->out_block_size;
535 /* Try to fill the output buffer. */
536 while (state->stream.avail_out > 0 && !state->eof) {
537 state->stream.next_in = (unsigned char *)(uintptr_t)
538 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
539 if (state->stream.next_in == NULL && avail_in < 0)
540 return (ARCHIVE_FATAL);
541 state->stream.avail_in = avail_in;
543 /* Decompress as much as we can in one pass. */
544 ret = lzmadec_decode(&(state->stream), avail_in == 0);
546 case LZMADEC_STREAM_END: /* Found end of stream. */
549 case LZMADEC_OK: /* Decompressor made some progress. */
550 __archive_read_filter_consume(self->upstream,
551 avail_in - state->stream.avail_in);
553 case LZMADEC_BUF_ERROR: /* Insufficient input data? */
554 archive_set_error(&self->archive->archive,
556 "Insufficient compressed data");
557 return (ARCHIVE_FATAL);
559 /* Return an error. */
560 archive_set_error(&self->archive->archive,
562 "Lzma decompression failed");
563 return (ARCHIVE_FATAL);
567 decompressed = state->stream.next_out - state->out_block;
568 state->total_out += decompressed;
569 if (decompressed == 0)
572 *p = state->out_block;
573 return (decompressed);
577 * Clean up the decompressor.
580 lzma_filter_close(struct archive_read_filter *self)
582 struct private_data *state;
585 state = (struct private_data *)self->data;
587 switch (lzmadec_end(&(state->stream))) {
591 archive_set_error(&(self->archive->archive),
593 "Failed to clean up %s compressor",
594 self->archive->archive.compression_name);
598 free(state->out_block);
607 * If we have no suitable library on this system, we can't actually do
608 * the decompression. We can, however, still detect compressed
609 * archives and emit a useful message.
613 lzma_bidder_init(struct archive_read_filter *self)
617 r = __archive_read_program(self, "unlzma");
618 /* Note: We set the format here even if __archive_read_program()
619 * above fails. We do, after all, know what the format is
620 * even if we weren't able to read it. */
621 self->code = ARCHIVE_COMPRESSION_LZMA;
626 #endif /* HAVE_LZMADEC_H */
630 xz_bidder_init(struct archive_read_filter *self)
634 r = __archive_read_program(self, "unxz");
635 /* Note: We set the format here even if __archive_read_program()
636 * above fails. We do, after all, know what the format is
637 * even if we weren't able to read it. */
638 self->code = ARCHIVE_COMPRESSION_XZ;
644 #endif /* HAVE_LZMA_H */