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);
140 bidder->bid = xz_bidder_bid;
141 bidder->init = xz_bidder_init;
142 bidder->options = NULL;
144 #if HAVE_LZMA_H && HAVE_LIBLZMA
147 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
148 "Using external xz program for xz decompression");
149 return (ARCHIVE_WARN);
153 #if ARCHIVE_VERSION_NUMBER < 4000000
155 archive_read_support_compression_lzma(struct archive *a)
157 return archive_read_support_filter_lzma(a);
162 archive_read_support_filter_lzma(struct archive *_a)
164 struct archive_read *a = (struct archive_read *)_a;
165 struct archive_read_filter_bidder *bidder;
167 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
168 ARCHIVE_STATE_NEW, "archive_read_support_filter_lzma");
170 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
171 return (ARCHIVE_FATAL);
174 bidder->name = "lzma";
175 bidder->bid = lzma_bidder_bid;
176 bidder->init = lzma_bidder_init;
177 bidder->options = NULL;
179 #if HAVE_LZMA_H && HAVE_LIBLZMA
181 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
184 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
185 "Using external lzma program for lzma decompression");
186 return (ARCHIVE_WARN);
191 #if ARCHIVE_VERSION_NUMBER < 4000000
193 archive_read_support_compression_lzip(struct archive *a)
195 return archive_read_support_filter_lzip(a);
200 archive_read_support_filter_lzip(struct archive *_a)
202 struct archive_read *a = (struct archive_read *)_a;
203 struct archive_read_filter_bidder *bidder;
205 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
206 ARCHIVE_STATE_NEW, "archive_read_support_filter_lzip");
208 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
209 return (ARCHIVE_FATAL);
212 bidder->name = "lzip";
213 bidder->bid = lzip_bidder_bid;
214 bidder->init = lzip_bidder_init;
215 bidder->options = NULL;
217 #if HAVE_LZMA_H && HAVE_LIBLZMA
220 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
221 "Using external lzip program for lzip decompression");
222 return (ARCHIVE_WARN);
227 * Test whether we can handle this data.
230 xz_bidder_bid(struct archive_read_filter_bidder *self,
231 struct archive_read_filter *filter)
233 const unsigned char *buffer;
236 (void)self; /* UNUSED */
238 buffer = __archive_read_filter_ahead(filter, 6, &avail);
243 * Verify Header Magic Bytes : FD 37 7A 58 5A 00
245 if (memcmp(buffer, "\xFD\x37\x7A\x58\x5A\x00", 6) != 0)
252 * Test whether we can handle this data.
254 * <sigh> LZMA has a rather poor file signature. Zeros do not
255 * make good signature bytes as a rule, and the only non-zero byte
256 * here is an ASCII character. For example, an uncompressed tar
257 * archive whose first file is ']' would satisfy this check. It may
258 * be necessary to exclude LZMA from compression_all() because of
259 * this. Clients of libarchive would then have to explicitly enable
260 * LZMA checking instead of (or in addition to) compression_all() when
261 * they have other evidence (file name, command-line option) to go on.
264 lzma_bidder_bid(struct archive_read_filter_bidder *self,
265 struct archive_read_filter *filter)
267 const unsigned char *buffer;
270 uint64_t uncompressed_size;
273 (void)self; /* UNUSED */
275 buffer = __archive_read_filter_ahead(filter, 14, &avail);
279 /* First byte of raw LZMA stream is commonly 0x5d.
280 * The first byte is a special number, which consists of
281 * three parameters of LZMA compression, a number of literal
282 * context bits(which is from 0 to 8, default is 3), a number
283 * of literal pos bits(which is from 0 to 4, default is 0),
284 * a number of pos bits(which is from 0 to 4, default is 2).
285 * The first byte is made by
286 * (pos bits * 5 + literal pos bit) * 9 + * literal contest bit,
287 * and so the default value in this field is
288 * (2 * 5 + 0) * 9 + 3 = 0x5d.
289 * lzma of LZMA SDK has options to change those parameters.
290 * It means a range of this field is from 0 to 224. And lzma of
291 * XZ Utils with option -e records 0x5e in this field. */
292 /* NOTE: If this checking of the first byte increases false
293 * recognition, we should allow only 0x5d and 0x5e for the first
294 * byte of LZMA stream. */
296 if (buffer[0] > (4 * 5 + 4) * 9 + 8)
298 /* Most likely value in the first byte of LZMA stream. */
299 if (buffer[0] == 0x5d || buffer[0] == 0x5e)
302 /* Sixth through fourteenth bytes are uncompressed size,
303 * stored in little-endian order. `-1' means uncompressed
304 * size is unknown and lzma of XZ Utils always records `-1'
306 uncompressed_size = archive_le64dec(buffer+5);
307 if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1))
310 /* Second through fifth bytes are dictionary size, stored in
311 * little-endian order. The minimum dictionary size is
312 * 1 << 12(4KiB) which the lzma of LZMA SDK uses with option
313 * -d12 and the maxinam dictionary size is 1 << 27(128MiB)
314 * which the one uses with option -d27.
315 * NOTE: A comment of LZMA SDK source code says this dictionary
316 * range is from 1 << 12 to 1 << 30. */
317 dicsize = archive_le32dec(buffer+1);
319 case 0x00001000:/* lzma of LZMA SDK option -d12. */
320 case 0x00002000:/* lzma of LZMA SDK option -d13. */
321 case 0x00004000:/* lzma of LZMA SDK option -d14. */
322 case 0x00008000:/* lzma of LZMA SDK option -d15. */
323 case 0x00010000:/* lzma of XZ Utils option -0 and -1.
324 * lzma of LZMA SDK option -d16. */
325 case 0x00020000:/* lzma of LZMA SDK option -d17. */
326 case 0x00040000:/* lzma of LZMA SDK option -d18. */
327 case 0x00080000:/* lzma of XZ Utils option -2.
328 * lzma of LZMA SDK option -d19. */
329 case 0x00100000:/* lzma of XZ Utils option -3.
330 * lzma of LZMA SDK option -d20. */
331 case 0x00200000:/* lzma of XZ Utils option -4.
332 * lzma of LZMA SDK option -d21. */
333 case 0x00400000:/* lzma of XZ Utils option -5.
334 * lzma of LZMA SDK option -d22. */
335 case 0x00800000:/* lzma of XZ Utils option -6.
336 * lzma of LZMA SDK option -d23. */
337 case 0x01000000:/* lzma of XZ Utils option -7.
338 * lzma of LZMA SDK option -d24. */
339 case 0x02000000:/* lzma of XZ Utils option -8.
340 * lzma of LZMA SDK option -d25. */
341 case 0x04000000:/* lzma of XZ Utils option -9.
342 * lzma of LZMA SDK option -d26. */
343 case 0x08000000:/* lzma of LZMA SDK option -d27. */
347 /* If a memory usage for encoding was not enough on
348 * the platform where LZMA stream was made, lzma of
349 * XZ Utils automatically decreased the dictionary
350 * size to enough memory for encoding by 1Mi bytes
352 if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 &&
353 (dicsize & ((1 << 20)-1)) == 0 &&
354 bits_checked == 8 + 64) {
358 /* Otherwise dictionary size is unlikely. But it is
359 * possible that someone makes lzma stream with
360 * liblzma/LZMA SDK in one's dictionary size. */
364 /* TODO: The above test is still very weak. It would be
365 * good to do better. */
367 return (bits_checked);
371 lzip_has_member(struct archive_read_filter *filter)
373 const unsigned char *buffer;
378 buffer = __archive_read_filter_ahead(filter, 6, &avail);
383 * Verify Header Magic Bytes : 4C 5A 49 50 (`LZIP')
386 if (memcmp(buffer, "LZIP", 4) != 0)
390 /* A version number must be 0 or 1 */
391 if (buffer[4] != 0 && buffer[4] != 1)
395 /* Dictionary size. */
396 log2dic = buffer[5] & 0x1f;
397 if (log2dic < 12 || log2dic > 27)
401 return (bits_checked);
405 lzip_bidder_bid(struct archive_read_filter_bidder *self,
406 struct archive_read_filter *filter)
409 (void)self; /* UNUSED */
410 return (lzip_has_member(filter));
413 #if HAVE_LZMA_H && HAVE_LIBLZMA
416 * liblzma 4.999.7 and later support both lzma and xz streams.
419 xz_bidder_init(struct archive_read_filter *self)
421 self->code = ARCHIVE_FILTER_XZ;
423 return (xz_lzma_bidder_init(self));
427 lzma_bidder_init(struct archive_read_filter *self)
429 self->code = ARCHIVE_FILTER_LZMA;
431 return (xz_lzma_bidder_init(self));
435 lzip_bidder_init(struct archive_read_filter *self)
437 self->code = ARCHIVE_FILTER_LZIP;
439 return (xz_lzma_bidder_init(self));
443 * Set an error code and choose an error message
446 set_error(struct archive_read_filter *self, int ret)
450 case LZMA_STREAM_END: /* Found end of stream. */
451 case LZMA_OK: /* Decompressor made some progress. */
454 archive_set_error(&self->archive->archive, ENOMEM,
455 "Lzma library error: Cannot allocate memory");
457 case LZMA_MEMLIMIT_ERROR:
458 archive_set_error(&self->archive->archive, ENOMEM,
459 "Lzma library error: Out of memory");
461 case LZMA_FORMAT_ERROR:
462 archive_set_error(&self->archive->archive,
464 "Lzma library error: format not recognized");
466 case LZMA_OPTIONS_ERROR:
467 archive_set_error(&self->archive->archive,
469 "Lzma library error: Invalid options");
471 case LZMA_DATA_ERROR:
472 archive_set_error(&self->archive->archive,
474 "Lzma library error: Corrupted input data");
477 archive_set_error(&self->archive->archive,
479 "Lzma library error: No progress is possible");
482 /* Return an error. */
483 archive_set_error(&self->archive->archive,
485 "Lzma decompression failed: Unknown error");
491 * Setup the callbacks.
494 xz_lzma_bidder_init(struct archive_read_filter *self)
496 static const size_t out_block_size = 64 * 1024;
498 struct private_data *state;
501 state = (struct private_data *)calloc(sizeof(*state), 1);
502 out_block = (unsigned char *)malloc(out_block_size);
503 if (state == NULL || out_block == NULL) {
504 archive_set_error(&self->archive->archive, ENOMEM,
505 "Can't allocate data for xz decompression");
508 return (ARCHIVE_FATAL);
512 state->out_block_size = out_block_size;
513 state->out_block = out_block;
514 self->read = xz_filter_read;
515 self->skip = NULL; /* not supported */
516 self->close = xz_filter_close;
518 state->stream.avail_in = 0;
520 state->stream.next_out = state->out_block;
521 state->stream.avail_out = state->out_block_size;
524 if (self->code == ARCHIVE_FILTER_LZIP) {
526 * We have to read a lzip header and use it to initialize
527 * compression library, thus we cannot initialize the
528 * library for lzip here.
530 state->in_stream = 0;
533 state->in_stream = 1;
535 /* Initialize compression library. */
536 if (self->code == ARCHIVE_FILTER_XZ)
537 ret = lzma_stream_decoder(&(state->stream),
538 LZMA_MEMLIMIT,/* memlimit */
541 ret = lzma_alone_decoder(&(state->stream),
542 LZMA_MEMLIMIT);/* memlimit */
547 /* Library setup failed: Choose an error message and clean up. */
548 set_error(self, ret);
550 free(state->out_block);
553 return (ARCHIVE_FATAL);
557 lzip_init(struct archive_read_filter *self)
559 struct private_data *state;
560 const unsigned char *h;
561 lzma_filter filters[2];
562 unsigned char props[5];
567 state = (struct private_data *)self->data;
568 h = __archive_read_filter_ahead(self->upstream, 6, &avail_in);
570 return (ARCHIVE_FATAL);
572 /* Get a version number. */
573 state->lzip_ver = h[4];
576 * Setup lzma property.
580 /* Get dictionary size. */
581 log2dic = h[5] & 0x1f;
582 if (log2dic < 12 || log2dic > 27)
583 return (ARCHIVE_FATAL);
584 dicsize = 1U << log2dic;
586 dicsize -= (dicsize / 16) * (h[5] >> 5);
587 archive_le32enc(props+1, dicsize);
589 /* Consume lzip header. */
590 __archive_read_filter_consume(self->upstream, 6);
591 state->member_in = 6;
593 filters[0].id = LZMA_FILTER_LZMA1;
594 filters[0].options = NULL;
595 filters[1].id = LZMA_VLI_UNKNOWN;
596 filters[1].options = NULL;
598 ret = lzma_properties_decode(&filters[0], NULL, props, sizeof(props));
599 if (ret != LZMA_OK) {
600 set_error(self, ret);
601 return (ARCHIVE_FATAL);
603 ret = lzma_raw_decoder(&(state->stream), filters);
604 #if LZMA_VERSION < 50010000
605 free(filters[0].options);
607 if (ret != LZMA_OK) {
608 set_error(self, ret);
609 return (ARCHIVE_FATAL);
615 lzip_tail(struct archive_read_filter *self)
617 struct private_data *state;
618 const unsigned char *f;
622 state = (struct private_data *)self->data;
623 if (state->lzip_ver == 0)
627 f = __archive_read_filter_ahead(self->upstream, tail, &avail_in);
628 if (f == NULL && avail_in < 0)
629 return (ARCHIVE_FATAL);
630 if (f == NULL || avail_in < tail) {
631 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
632 "Lzip: Remaining data is less bytes");
633 return (ARCHIVE_FAILED);
636 /* Check the crc32 value of the uncompressed data of the current
638 if (state->crc32 != archive_le32dec(f)) {
639 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
640 "Lzip: CRC32 error");
641 return (ARCHIVE_FAILED);
644 /* Check the uncompressed size of the current member */
645 if ((uint64_t)state->member_out != archive_le64dec(f + 4)) {
646 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
647 "Lzip: Uncompressed size error");
648 return (ARCHIVE_FAILED);
651 /* Check the total size of the current member */
652 if (state->lzip_ver == 1 &&
653 (uint64_t)state->member_in + tail != archive_le64dec(f + 12)) {
654 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
655 "Lzip: Member size error");
656 return (ARCHIVE_FAILED);
658 __archive_read_filter_consume(self->upstream, tail);
660 /* If current lzip data consists of multi member, try decompressing
662 if (lzip_has_member(self->upstream) != 0) {
663 state->in_stream = 0;
665 state->member_out = 0;
666 state->member_in = 0;
673 * Return the next block of decompressed data.
676 xz_filter_read(struct archive_read_filter *self, const void **p)
678 struct private_data *state;
683 state = (struct private_data *)self->data;
685 /* Empty our output buffer. */
686 state->stream.next_out = state->out_block;
687 state->stream.avail_out = state->out_block_size;
689 /* Try to fill the output buffer. */
690 while (state->stream.avail_out > 0 && !state->eof) {
691 if (!state->in_stream) {
693 * Initialize liblzma for lzip
695 ret = lzip_init(self);
696 if (ret != ARCHIVE_OK)
698 state->in_stream = 1;
700 state->stream.next_in =
701 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
702 if (state->stream.next_in == NULL && avail_in < 0) {
703 archive_set_error(&self->archive->archive,
706 return (ARCHIVE_FATAL);
708 state->stream.avail_in = avail_in;
710 /* Decompress as much as we can in one pass. */
711 ret = lzma_code(&(state->stream),
712 (state->stream.avail_in == 0)? LZMA_FINISH: LZMA_RUN);
714 case LZMA_STREAM_END: /* Found end of stream. */
717 case LZMA_OK: /* Decompressor made some progress. */
718 __archive_read_filter_consume(self->upstream,
719 avail_in - state->stream.avail_in);
721 avail_in - state->stream.avail_in;
724 set_error(self, ret);
725 return (ARCHIVE_FATAL);
729 decompressed = state->stream.next_out - state->out_block;
730 state->total_out += decompressed;
731 state->member_out += decompressed;
732 if (decompressed == 0)
735 *p = state->out_block;
736 if (self->code == ARCHIVE_FILTER_LZIP) {
737 state->crc32 = lzma_crc32(state->out_block,
738 decompressed, state->crc32);
740 ret = lzip_tail(self);
741 if (ret != ARCHIVE_OK)
746 return (decompressed);
750 * Clean up the decompressor.
753 xz_filter_close(struct archive_read_filter *self)
755 struct private_data *state;
757 state = (struct private_data *)self->data;
758 lzma_end(&(state->stream));
759 free(state->out_block);
766 #if HAVE_LZMADEC_H && HAVE_LIBLZMADEC
769 * If we have the older liblzmadec library, then we can handle
770 * LZMA streams but not XZ streams.
774 * Setup the callbacks.
777 lzma_bidder_init(struct archive_read_filter *self)
779 static const size_t out_block_size = 64 * 1024;
781 struct private_data *state;
782 ssize_t ret, avail_in;
784 self->code = ARCHIVE_FILTER_LZMA;
787 state = (struct private_data *)calloc(sizeof(*state), 1);
788 out_block = (unsigned char *)malloc(out_block_size);
789 if (state == NULL || out_block == NULL) {
790 archive_set_error(&self->archive->archive, ENOMEM,
791 "Can't allocate data for lzma decompression");
794 return (ARCHIVE_FATAL);
798 state->out_block_size = out_block_size;
799 state->out_block = out_block;
800 self->read = lzma_filter_read;
801 self->skip = NULL; /* not supported */
802 self->close = lzma_filter_close;
804 /* Prime the lzma library with 18 bytes of input. */
805 state->stream.next_in = (unsigned char *)(uintptr_t)
806 __archive_read_filter_ahead(self->upstream, 18, &avail_in);
807 if (state->stream.next_in == NULL)
808 return (ARCHIVE_FATAL);
809 state->stream.avail_in = avail_in;
810 state->stream.next_out = state->out_block;
811 state->stream.avail_out = state->out_block_size;
813 /* Initialize compression library. */
814 ret = lzmadec_init(&(state->stream));
815 __archive_read_filter_consume(self->upstream,
816 avail_in - state->stream.avail_in);
817 if (ret == LZMADEC_OK)
820 /* Library setup failed: Clean up. */
821 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
822 "Internal error initializing lzma library");
824 /* Override the error message if we know what really went wrong. */
826 case LZMADEC_HEADER_ERROR:
827 archive_set_error(&self->archive->archive,
829 "Internal error initializing compression library: "
832 case LZMADEC_MEM_ERROR:
833 archive_set_error(&self->archive->archive, ENOMEM,
834 "Internal error initializing compression library: "
839 free(state->out_block);
842 return (ARCHIVE_FATAL);
846 * Return the next block of decompressed data.
849 lzma_filter_read(struct archive_read_filter *self, const void **p)
851 struct private_data *state;
853 ssize_t avail_in, ret;
855 state = (struct private_data *)self->data;
857 /* Empty our output buffer. */
858 state->stream.next_out = state->out_block;
859 state->stream.avail_out = state->out_block_size;
861 /* Try to fill the output buffer. */
862 while (state->stream.avail_out > 0 && !state->eof) {
863 state->stream.next_in = (unsigned char *)(uintptr_t)
864 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
865 if (state->stream.next_in == NULL && avail_in < 0) {
866 archive_set_error(&self->archive->archive,
868 "truncated lzma input");
869 return (ARCHIVE_FATAL);
871 state->stream.avail_in = avail_in;
873 /* Decompress as much as we can in one pass. */
874 ret = lzmadec_decode(&(state->stream), avail_in == 0);
876 case LZMADEC_STREAM_END: /* Found end of stream. */
879 case LZMADEC_OK: /* Decompressor made some progress. */
880 __archive_read_filter_consume(self->upstream,
881 avail_in - state->stream.avail_in);
883 case LZMADEC_BUF_ERROR: /* Insufficient input data? */
884 archive_set_error(&self->archive->archive,
886 "Insufficient compressed data");
887 return (ARCHIVE_FATAL);
889 /* Return an error. */
890 archive_set_error(&self->archive->archive,
892 "Lzma decompression failed");
893 return (ARCHIVE_FATAL);
897 decompressed = state->stream.next_out - state->out_block;
898 state->total_out += decompressed;
899 if (decompressed == 0)
902 *p = state->out_block;
903 return (decompressed);
907 * Clean up the decompressor.
910 lzma_filter_close(struct archive_read_filter *self)
912 struct private_data *state;
915 state = (struct private_data *)self->data;
917 switch (lzmadec_end(&(state->stream))) {
921 archive_set_error(&(self->archive->archive),
923 "Failed to clean up %s compressor",
924 self->archive->archive.compression_name);
928 free(state->out_block);
937 * If we have no suitable library on this system, we can't actually do
938 * the decompression. We can, however, still detect compressed
939 * archives and emit a useful message.
943 lzma_bidder_init(struct archive_read_filter *self)
947 r = __archive_read_program(self, "lzma -d -qq");
948 /* Note: We set the format here even if __archive_read_program()
949 * above fails. We do, after all, know what the format is
950 * even if we weren't able to read it. */
951 self->code = ARCHIVE_FILTER_LZMA;
956 #endif /* HAVE_LZMADEC_H */
960 xz_bidder_init(struct archive_read_filter *self)
964 r = __archive_read_program(self, "xz -d -qq");
965 /* Note: We set the format here even if __archive_read_program()
966 * above fails. We do, after all, know what the format is
967 * even if we weren't able to read it. */
968 self->code = ARCHIVE_FILTER_XZ;
974 lzip_bidder_init(struct archive_read_filter *self)
978 r = __archive_read_program(self, "lzip -d -q");
979 /* Note: We set the format here even if __archive_read_program()
980 * above fails. We do, after all, know what the format is
981 * even if we weren't able to read it. */
982 self->code = ARCHIVE_FILTER_LZIP;
988 #endif /* HAVE_LZMA_H */