2 * Copyright (c) 2009-2011 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_endian.h"
52 #include "archive_private.h"
53 #include "archive_read_private.h"
55 #if HAVE_LZMA_H && HAVE_LIBLZMA
59 unsigned char *out_block;
60 size_t out_block_size;
62 char eof; /* True = found end of compressed data. */
65 /* Following variables are used for lzip only. */
72 #if LZMA_VERSION_MAJOR >= 5
73 /* Effectively disable the limiter. */
74 #define LZMA_MEMLIMIT UINT64_MAX
76 /* NOTE: This needs to check memory size which running system has. */
77 #define LZMA_MEMLIMIT (1U << 30)
80 /* Combined lzip/lzma/xz filter */
81 static ssize_t xz_filter_read(struct archive_read_filter *, const void **);
82 static int xz_filter_close(struct archive_read_filter *);
83 static int xz_lzma_bidder_init(struct archive_read_filter *);
85 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
88 lzmadec_stream stream;
89 unsigned char *out_block;
90 size_t out_block_size;
92 char eof; /* True = found end of compressed data. */
95 /* Lzma-only filter */
96 static ssize_t lzma_filter_read(struct archive_read_filter *, const void **);
97 static int lzma_filter_close(struct archive_read_filter *);
101 * Note that we can detect xz and lzma compressed files even if we
102 * can't decompress them. (In fact, we like detecting them because we
103 * can give better error messages.) So the bid framework here gets
104 * compiled even if no lzma library is available.
106 static int xz_bidder_bid(struct archive_read_filter_bidder *,
107 struct archive_read_filter *);
108 static int xz_bidder_init(struct archive_read_filter *);
109 static int lzma_bidder_bid(struct archive_read_filter_bidder *,
110 struct archive_read_filter *);
111 static int lzma_bidder_init(struct archive_read_filter *);
112 static int lzip_has_member(struct archive_read_filter *);
113 static int lzip_bidder_bid(struct archive_read_filter_bidder *,
114 struct archive_read_filter *);
115 static int lzip_bidder_init(struct archive_read_filter *);
117 #if ARCHIVE_VERSION_NUMBER < 4000000
118 /* Deprecated; remove in libarchive 4.0 */
120 archive_read_support_compression_xz(struct archive *a)
122 return archive_read_support_filter_xz(a);
127 archive_read_support_filter_xz(struct archive *_a)
129 struct archive_read *a = (struct archive_read *)_a;
130 struct archive_read_filter_bidder *bidder;
132 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
133 ARCHIVE_STATE_NEW, "archive_read_support_filter_xz");
135 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
136 return (ARCHIVE_FATAL);
139 bidder->bid = xz_bidder_bid;
140 bidder->init = xz_bidder_init;
141 bidder->options = NULL;
143 #if HAVE_LZMA_H && HAVE_LIBLZMA
146 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
147 "Using external unxz program for xz decompression");
148 return (ARCHIVE_WARN);
152 #if ARCHIVE_VERSION_NUMBER < 4000000
154 archive_read_support_compression_lzma(struct archive *a)
156 return archive_read_support_filter_lzma(a);
161 archive_read_support_filter_lzma(struct archive *_a)
163 struct archive_read *a = (struct archive_read *)_a;
164 struct archive_read_filter_bidder *bidder;
166 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
167 ARCHIVE_STATE_NEW, "archive_read_support_filter_lzma");
169 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
170 return (ARCHIVE_FATAL);
173 bidder->bid = lzma_bidder_bid;
174 bidder->init = lzma_bidder_init;
175 bidder->options = NULL;
177 #if HAVE_LZMA_H && HAVE_LIBLZMA
179 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
182 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
183 "Using external unlzma program for lzma decompression");
184 return (ARCHIVE_WARN);
189 #if ARCHIVE_VERSION_NUMBER < 4000000
191 archive_read_support_compression_lzip(struct archive *a)
193 return archive_read_support_filter_lzip(a);
198 archive_read_support_filter_lzip(struct archive *_a)
200 struct archive_read *a = (struct archive_read *)_a;
201 struct archive_read_filter_bidder *bidder;
203 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
204 ARCHIVE_STATE_NEW, "archive_read_support_filter_lzip");
206 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
207 return (ARCHIVE_FATAL);
210 bidder->bid = lzip_bidder_bid;
211 bidder->init = lzip_bidder_init;
212 bidder->options = NULL;
214 #if HAVE_LZMA_H && HAVE_LIBLZMA
217 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
218 "Using external lzip program for lzip decompression");
219 return (ARCHIVE_WARN);
224 * Test whether we can handle this data.
227 xz_bidder_bid(struct archive_read_filter_bidder *self,
228 struct archive_read_filter *filter)
230 const unsigned char *buffer;
233 (void)self; /* UNUSED */
235 buffer = __archive_read_filter_ahead(filter, 6, &avail);
240 * Verify Header Magic Bytes : FD 37 7A 58 5A 00
242 if (memcmp(buffer, "\xFD\x37\x7A\x58\x5A\x00", 6) != 0)
249 * Test whether we can handle this data.
251 * <sigh> LZMA has a rather poor file signature. Zeros do not
252 * make good signature bytes as a rule, and the only non-zero byte
253 * here is an ASCII character. For example, an uncompressed tar
254 * archive whose first file is ']' would satisfy this check. It may
255 * be necessary to exclude LZMA from compression_all() because of
256 * this. Clients of libarchive would then have to explicitly enable
257 * LZMA checking instead of (or in addition to) compression_all() when
258 * they have other evidence (file name, command-line option) to go on.
261 lzma_bidder_bid(struct archive_read_filter_bidder *self,
262 struct archive_read_filter *filter)
264 const unsigned char *buffer;
267 uint64_t uncompressed_size;
270 (void)self; /* UNUSED */
272 buffer = __archive_read_filter_ahead(filter, 14, &avail);
276 /* First byte of raw LZMA stream is commonly 0x5d.
277 * The first byte is a special number, which consists of
278 * three parameters of LZMA compression, a number of literal
279 * context bits(which is from 0 to 8, default is 3), a number
280 * of literal pos bits(which is from 0 to 4, default is 0),
281 * a number of pos bits(which is from 0 to 4, default is 2).
282 * The first byte is made by
283 * (pos bits * 5 + literal pos bit) * 9 + * literal contest bit,
284 * and so the default value in this field is
285 * (2 * 5 + 0) * 9 + 3 = 0x5d.
286 * lzma of LZMA SDK has options to change those parameters.
287 * It means a range of this field is from 0 to 224. And lzma of
288 * XZ Utils with option -e records 0x5e in this field. */
289 /* NOTE: If this checking of the first byte increases false
290 * recognition, we should allow only 0x5d and 0x5e for the first
291 * byte of LZMA stream. */
293 if (buffer[0] > (4 * 5 + 4) * 9 + 8)
295 /* Most likely value in the first byte of LZMA stream. */
296 if (buffer[0] == 0x5d || buffer[0] == 0x5e)
299 /* Sixth through fourteenth bytes are uncompressed size,
300 * stored in little-endian order. `-1' means uncompressed
301 * size is unknown and lzma of XZ Utils always records `-1'
303 uncompressed_size = archive_le64dec(buffer+5);
304 if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1))
307 /* Second through fifth bytes are dictionary size, stored in
308 * little-endian order. The minimum dictionary size is
309 * 1 << 12(4KiB) which the lzma of LZMA SDK uses with option
310 * -d12 and the maxinam dictionary size is 1 << 27(128MiB)
311 * which the one uses with option -d27.
312 * NOTE: A comment of LZMA SDK source code says this dictionary
313 * range is from 1 << 12 to 1 << 30. */
314 dicsize = archive_le32dec(buffer+1);
316 case 0x00001000:/* lzma of LZMA SDK option -d12. */
317 case 0x00002000:/* lzma of LZMA SDK option -d13. */
318 case 0x00004000:/* lzma of LZMA SDK option -d14. */
319 case 0x00008000:/* lzma of LZMA SDK option -d15. */
320 case 0x00010000:/* lzma of XZ Utils option -0 and -1.
321 * lzma of LZMA SDK option -d16. */
322 case 0x00020000:/* lzma of LZMA SDK option -d17. */
323 case 0x00040000:/* lzma of LZMA SDK option -d18. */
324 case 0x00080000:/* lzma of XZ Utils option -2.
325 * lzma of LZMA SDK option -d19. */
326 case 0x00100000:/* lzma of XZ Utils option -3.
327 * lzma of LZMA SDK option -d20. */
328 case 0x00200000:/* lzma of XZ Utils option -4.
329 * lzma of LZMA SDK option -d21. */
330 case 0x00400000:/* lzma of XZ Utils option -5.
331 * lzma of LZMA SDK option -d22. */
332 case 0x00800000:/* lzma of XZ Utils option -6.
333 * lzma of LZMA SDK option -d23. */
334 case 0x01000000:/* lzma of XZ Utils option -7.
335 * lzma of LZMA SDK option -d24. */
336 case 0x02000000:/* lzma of XZ Utils option -8.
337 * lzma of LZMA SDK option -d25. */
338 case 0x04000000:/* lzma of XZ Utils option -9.
339 * lzma of LZMA SDK option -d26. */
340 case 0x08000000:/* lzma of LZMA SDK option -d27. */
344 /* If a memory usage for encoding was not enough on
345 * the platform where LZMA stream was made, lzma of
346 * XZ Utils automatically decreased the dictionary
347 * size to enough memory for encoding by 1Mi bytes
349 if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 &&
350 (dicsize & ((1 << 20)-1)) == 0 &&
351 bits_checked == 8 + 64) {
355 /* Otherwise dictionary size is unlikely. But it is
356 * possible that someone makes lzma stream with
357 * liblzma/LZMA SDK in one's dictionary size. */
361 /* TODO: The above test is still very weak. It would be
362 * good to do better. */
364 return (bits_checked);
368 lzip_has_member(struct archive_read_filter *filter)
370 const unsigned char *buffer;
375 buffer = __archive_read_filter_ahead(filter, 6, &avail);
380 * Verify Header Magic Bytes : 4C 5A 49 50 (`LZIP')
383 if (memcmp(buffer, "LZIP", 4) != 0)
387 /* A version number must be 0 or 1 */
388 if (buffer[4] != 0 && buffer[4] != 1)
392 /* Dictionary size. */
393 log2dic = buffer[5] & 0x1f;
394 if (log2dic < 12 || log2dic > 27)
398 return (bits_checked);
402 lzip_bidder_bid(struct archive_read_filter_bidder *self,
403 struct archive_read_filter *filter)
406 (void)self; /* UNUSED */
407 return (lzip_has_member(filter));
410 #if HAVE_LZMA_H && HAVE_LIBLZMA
413 * liblzma 4.999.7 and later support both lzma and xz streams.
416 xz_bidder_init(struct archive_read_filter *self)
418 self->code = ARCHIVE_COMPRESSION_XZ;
420 return (xz_lzma_bidder_init(self));
424 lzma_bidder_init(struct archive_read_filter *self)
426 self->code = ARCHIVE_COMPRESSION_LZMA;
428 return (xz_lzma_bidder_init(self));
432 lzip_bidder_init(struct archive_read_filter *self)
434 self->code = ARCHIVE_COMPRESSION_LZIP;
436 return (xz_lzma_bidder_init(self));
440 * Set an error code and choose an error message
443 set_error(struct archive_read_filter *self, int ret)
447 case LZMA_STREAM_END: /* Found end of stream. */
448 case LZMA_OK: /* Decompressor made some progress. */
451 archive_set_error(&self->archive->archive, ENOMEM,
452 "Lzma library error: Cannot allocate memory");
454 case LZMA_MEMLIMIT_ERROR:
455 archive_set_error(&self->archive->archive, ENOMEM,
456 "Lzma library error: Out of memory");
458 case LZMA_FORMAT_ERROR:
459 archive_set_error(&self->archive->archive,
461 "Lzma library error: format not recognized");
463 case LZMA_OPTIONS_ERROR:
464 archive_set_error(&self->archive->archive,
466 "Lzma library error: Invalid options");
468 case LZMA_DATA_ERROR:
469 archive_set_error(&self->archive->archive,
471 "Lzma library error: Corrupted input data");
474 archive_set_error(&self->archive->archive,
476 "Lzma library error: No progress is possible");
479 /* Return an error. */
480 archive_set_error(&self->archive->archive,
482 "Lzma decompression failed: Unknown error");
488 * Setup the callbacks.
491 xz_lzma_bidder_init(struct archive_read_filter *self)
493 static const size_t out_block_size = 64 * 1024;
495 struct private_data *state;
498 state = (struct private_data *)calloc(sizeof(*state), 1);
499 out_block = (unsigned char *)malloc(out_block_size);
500 if (state == NULL || out_block == NULL) {
501 archive_set_error(&self->archive->archive, ENOMEM,
502 "Can't allocate data for xz decompression");
505 return (ARCHIVE_FATAL);
509 state->out_block_size = out_block_size;
510 state->out_block = out_block;
511 self->read = xz_filter_read;
512 self->skip = NULL; /* not supported */
513 self->close = xz_filter_close;
515 state->stream.avail_in = 0;
517 state->stream.next_out = state->out_block;
518 state->stream.avail_out = state->out_block_size;
521 if (self->code == ARCHIVE_COMPRESSION_LZIP) {
523 * We have to read a lzip header and use it to initialize
524 * compression library, thus we cannot initialize the
525 * library for lzip here.
527 state->in_stream = 0;
530 state->in_stream = 1;
532 /* Initialize compression library. */
533 if (self->code == ARCHIVE_COMPRESSION_XZ)
534 ret = lzma_stream_decoder(&(state->stream),
535 LZMA_MEMLIMIT,/* memlimit */
538 ret = lzma_alone_decoder(&(state->stream),
539 LZMA_MEMLIMIT);/* memlimit */
544 /* Library setup failed: Choose an error message and clean up. */
545 set_error(self, ret);
547 free(state->out_block);
550 return (ARCHIVE_FATAL);
554 lzip_init(struct archive_read_filter *self)
556 struct private_data *state;
557 const unsigned char *h;
558 lzma_filter filters[2];
559 unsigned char props[5];
564 state = (struct private_data *)self->data;
565 h = __archive_read_filter_ahead(self->upstream, 6, &avail_in);
567 return (ARCHIVE_FATAL);
569 /* Get a version number. */
570 state->lzip_ver = h[4];
573 * Setup lzma property.
577 /* Get dictionary size. */
578 log2dic = h[5] & 0x1f;
579 if (log2dic < 12 || log2dic > 27)
580 return (ARCHIVE_FATAL);
581 dicsize = 1U << log2dic;
583 dicsize -= (dicsize / 16) * (h[5] >> 5);
584 archive_le32enc(props+1, dicsize);
586 /* Consume lzip header. */
587 __archive_read_filter_consume(self->upstream, 6);
588 state->member_in = 6;
590 filters[0].id = LZMA_FILTER_LZMA1;
591 filters[0].options = NULL;
592 filters[1].id = LZMA_VLI_UNKNOWN;
593 filters[1].options = NULL;
595 ret = lzma_properties_decode(&filters[0], NULL, props, sizeof(props));
596 if (ret != LZMA_OK) {
597 set_error(self, ret);
598 return (ARCHIVE_FATAL);
600 ret = lzma_raw_decoder(&(state->stream), filters);
601 #if LZMA_VERSION < 50000030
602 free(filters[0].options);
604 if (ret != LZMA_OK) {
605 set_error(self, ret);
606 return (ARCHIVE_FATAL);
612 lzip_tail(struct archive_read_filter *self)
614 struct private_data *state;
615 const unsigned char *f;
619 state = (struct private_data *)self->data;
620 if (state->lzip_ver == 0)
624 f = __archive_read_filter_ahead(self->upstream, tail, &avail_in);
625 if (f == NULL && avail_in < 0)
626 return (ARCHIVE_FATAL);
627 if (avail_in < tail) {
628 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
629 "Lzip: Remaining data is less bytes");
630 return (ARCHIVE_FAILED);
633 /* Check the crc32 value of the uncompressed data of the current
635 if (state->crc32 != archive_le32dec(f)) {
636 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
637 "Lzip: CRC32 error");
638 return (ARCHIVE_FAILED);
641 /* Check the uncompressed size of the current member */
642 if ((uint64_t)state->member_out != archive_le64dec(f + 4)) {
643 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
644 "Lzip: Uncompressed size error");
645 return (ARCHIVE_FAILED);
648 /* Check the total size of the current member */
649 if (state->lzip_ver == 1 &&
650 (uint64_t)state->member_in + tail != archive_le64dec(f + 12)) {
651 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
652 "Lzip: Member size error");
653 return (ARCHIVE_FAILED);
655 __archive_read_filter_consume(self->upstream, tail);
657 /* If current lzip data consists of multi member, try decompressing
659 if (lzip_has_member(self->upstream) != 0) {
660 state->in_stream = 0;
662 state->member_out = 0;
663 state->member_in = 0;
670 * Return the next block of decompressed data.
673 xz_filter_read(struct archive_read_filter *self, const void **p)
675 struct private_data *state;
680 state = (struct private_data *)self->data;
682 /* Empty our output buffer. */
683 state->stream.next_out = state->out_block;
684 state->stream.avail_out = state->out_block_size;
686 /* Try to fill the output buffer. */
687 while (state->stream.avail_out > 0 && !state->eof) {
688 if (!state->in_stream) {
690 * Initialize liblzma for lzip
692 ret = lzip_init(self);
693 if (ret != ARCHIVE_OK)
695 state->in_stream = 1;
697 state->stream.next_in =
698 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
699 if (state->stream.next_in == NULL && avail_in < 0) {
700 archive_set_error(&self->archive->archive,
703 return (ARCHIVE_FATAL);
705 state->stream.avail_in = avail_in;
707 /* Decompress as much as we can in one pass. */
708 ret = lzma_code(&(state->stream),
709 (state->stream.avail_in == 0)? LZMA_FINISH: LZMA_RUN);
711 case LZMA_STREAM_END: /* Found end of stream. */
714 case LZMA_OK: /* Decompressor made some progress. */
715 __archive_read_filter_consume(self->upstream,
716 avail_in - state->stream.avail_in);
718 avail_in - state->stream.avail_in;
721 set_error(self, ret);
722 return (ARCHIVE_FATAL);
726 decompressed = state->stream.next_out - state->out_block;
727 state->total_out += decompressed;
728 state->member_out += decompressed;
729 if (decompressed == 0)
732 *p = state->out_block;
733 if (self->code == ARCHIVE_COMPRESSION_LZIP) {
734 state->crc32 = lzma_crc32(state->out_block,
735 decompressed, state->crc32);
737 ret = lzip_tail(self);
738 if (ret != ARCHIVE_OK)
743 return (decompressed);
747 * Clean up the decompressor.
750 xz_filter_close(struct archive_read_filter *self)
752 struct private_data *state;
754 state = (struct private_data *)self->data;
755 lzma_end(&(state->stream));
756 free(state->out_block);
763 #if HAVE_LZMADEC_H && HAVE_LIBLZMADEC
766 * If we have the older liblzmadec library, then we can handle
767 * LZMA streams but not XZ streams.
771 * Setup the callbacks.
774 lzma_bidder_init(struct archive_read_filter *self)
776 static const size_t out_block_size = 64 * 1024;
778 struct private_data *state;
779 ssize_t ret, avail_in;
781 self->code = ARCHIVE_COMPRESSION_LZMA;
784 state = (struct private_data *)calloc(sizeof(*state), 1);
785 out_block = (unsigned char *)malloc(out_block_size);
786 if (state == NULL || out_block == NULL) {
787 archive_set_error(&self->archive->archive, ENOMEM,
788 "Can't allocate data for lzma decompression");
791 return (ARCHIVE_FATAL);
795 state->out_block_size = out_block_size;
796 state->out_block = out_block;
797 self->read = lzma_filter_read;
798 self->skip = NULL; /* not supported */
799 self->close = lzma_filter_close;
801 /* Prime the lzma library with 18 bytes of input. */
802 state->stream.next_in = (unsigned char *)(uintptr_t)
803 __archive_read_filter_ahead(self->upstream, 18, &avail_in);
804 if (state->stream.next_in == NULL)
805 return (ARCHIVE_FATAL);
806 state->stream.avail_in = avail_in;
807 state->stream.next_out = state->out_block;
808 state->stream.avail_out = state->out_block_size;
810 /* Initialize compression library. */
811 ret = lzmadec_init(&(state->stream));
812 __archive_read_filter_consume(self->upstream,
813 avail_in - state->stream.avail_in);
814 if (ret == LZMADEC_OK)
817 /* Library setup failed: Clean up. */
818 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
819 "Internal error initializing lzma library");
821 /* Override the error message if we know what really went wrong. */
823 case LZMADEC_HEADER_ERROR:
824 archive_set_error(&self->archive->archive,
826 "Internal error initializing compression library: "
829 case LZMADEC_MEM_ERROR:
830 archive_set_error(&self->archive->archive, ENOMEM,
831 "Internal error initializing compression library: "
836 free(state->out_block);
839 return (ARCHIVE_FATAL);
843 * Return the next block of decompressed data.
846 lzma_filter_read(struct archive_read_filter *self, const void **p)
848 struct private_data *state;
850 ssize_t avail_in, ret;
852 state = (struct private_data *)self->data;
854 /* Empty our output buffer. */
855 state->stream.next_out = state->out_block;
856 state->stream.avail_out = state->out_block_size;
858 /* Try to fill the output buffer. */
859 while (state->stream.avail_out > 0 && !state->eof) {
860 state->stream.next_in = (unsigned char *)(uintptr_t)
861 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
862 if (state->stream.next_in == NULL && avail_in < 0) {
863 archive_set_error(&self->archive->archive,
865 "truncated lzma input");
866 return (ARCHIVE_FATAL);
868 state->stream.avail_in = avail_in;
870 /* Decompress as much as we can in one pass. */
871 ret = lzmadec_decode(&(state->stream), avail_in == 0);
873 case LZMADEC_STREAM_END: /* Found end of stream. */
876 case LZMADEC_OK: /* Decompressor made some progress. */
877 __archive_read_filter_consume(self->upstream,
878 avail_in - state->stream.avail_in);
880 case LZMADEC_BUF_ERROR: /* Insufficient input data? */
881 archive_set_error(&self->archive->archive,
883 "Insufficient compressed data");
884 return (ARCHIVE_FATAL);
886 /* Return an error. */
887 archive_set_error(&self->archive->archive,
889 "Lzma decompression failed");
890 return (ARCHIVE_FATAL);
894 decompressed = state->stream.next_out - state->out_block;
895 state->total_out += decompressed;
896 if (decompressed == 0)
899 *p = state->out_block;
900 return (decompressed);
904 * Clean up the decompressor.
907 lzma_filter_close(struct archive_read_filter *self)
909 struct private_data *state;
912 state = (struct private_data *)self->data;
914 switch (lzmadec_end(&(state->stream))) {
918 archive_set_error(&(self->archive->archive),
920 "Failed to clean up %s compressor",
921 self->archive->archive.compression_name);
925 free(state->out_block);
934 * If we have no suitable library on this system, we can't actually do
935 * the decompression. We can, however, still detect compressed
936 * archives and emit a useful message.
940 lzma_bidder_init(struct archive_read_filter *self)
944 r = __archive_read_program(self, "unlzma");
945 /* Note: We set the format here even if __archive_read_program()
946 * above fails. We do, after all, know what the format is
947 * even if we weren't able to read it. */
948 self->code = ARCHIVE_COMPRESSION_LZMA;
953 #endif /* HAVE_LZMADEC_H */
957 xz_bidder_init(struct archive_read_filter *self)
961 r = __archive_read_program(self, "unxz");
962 /* Note: We set the format here even if __archive_read_program()
963 * above fails. We do, after all, know what the format is
964 * even if we weren't able to read it. */
965 self->code = ARCHIVE_COMPRESSION_XZ;
971 lzip_bidder_init(struct archive_read_filter *self)
975 r = __archive_read_program(self, "unlzip");
976 /* Note: We set the format here even if __archive_read_program()
977 * above fails. We do, after all, know what the format is
978 * even if we weren't able to read it. */
979 self->code = ARCHIVE_COMPRESSION_LZIP;
985 #endif /* HAVE_LZMA_H */