Import libarchive-3.1.2.
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_support_filter_xz.c
1 /*-
2  * Copyright (c) 2009-2011 Michihiro NAKAJIMA
3  * Copyright (c) 2003-2008 Tim Kientzle and Miklos Vajna
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 __FBSDID("$FreeBSD$");
30
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include <stdio.h>
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #if HAVE_LZMA_H
45 #include <lzma.h>
46 #elif HAVE_LZMADEC_H
47 #include <lzmadec.h>
48 #endif
49
50 #include "archive.h"
51 #include "archive_endian.h"
52 #include "archive_private.h"
53 #include "archive_read_private.h"
54
55 #if HAVE_LZMA_H && HAVE_LIBLZMA
56
57 struct private_data {
58         lzma_stream      stream;
59         unsigned char   *out_block;
60         size_t           out_block_size;
61         int64_t          total_out;
62         char             eof; /* True = found end of compressed data. */
63         char             in_stream;
64
65         /* Following variables are used for lzip only. */
66         char             lzip_ver;
67         uint32_t         crc32;
68         int64_t          member_in;
69         int64_t          member_out;
70 };
71
72 #if LZMA_VERSION_MAJOR >= 5
73 /* Effectively disable the limiter. */
74 #define LZMA_MEMLIMIT   UINT64_MAX
75 #else
76 /* NOTE: This needs to check memory size which running system has. */
77 #define LZMA_MEMLIMIT   (1U << 30)
78 #endif
79
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 *);
84
85 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
86
87 struct private_data {
88         lzmadec_stream   stream;
89         unsigned char   *out_block;
90         size_t           out_block_size;
91         int64_t          total_out;
92         char             eof; /* True = found end of compressed data. */
93 };
94
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 *);
98 #endif
99
100 /*
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.
105  */
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 *);
116
117 #if ARCHIVE_VERSION_NUMBER < 4000000
118 /* Deprecated; remove in libarchive 4.0 */
119 int
120 archive_read_support_compression_xz(struct archive *a)
121 {
122         return archive_read_support_filter_xz(a);
123 }
124 #endif
125
126 int
127 archive_read_support_filter_xz(struct archive *_a)
128 {
129         struct archive_read *a = (struct archive_read *)_a;
130         struct archive_read_filter_bidder *bidder;
131
132         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
133             ARCHIVE_STATE_NEW, "archive_read_support_filter_xz");
134
135         if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
136                 return (ARCHIVE_FATAL);
137
138         bidder->data = NULL;
139         bidder->name = "xz";
140         bidder->bid = xz_bidder_bid;
141         bidder->init = xz_bidder_init;
142         bidder->options = NULL;
143         bidder->free = NULL;
144 #if HAVE_LZMA_H && HAVE_LIBLZMA
145         return (ARCHIVE_OK);
146 #else
147         archive_set_error(_a, ARCHIVE_ERRNO_MISC,
148             "Using external xz program for xz decompression");
149         return (ARCHIVE_WARN);
150 #endif
151 }
152
153 #if ARCHIVE_VERSION_NUMBER < 4000000
154 int
155 archive_read_support_compression_lzma(struct archive *a)
156 {
157         return archive_read_support_filter_lzma(a);
158 }
159 #endif
160
161 int
162 archive_read_support_filter_lzma(struct archive *_a)
163 {
164         struct archive_read *a = (struct archive_read *)_a;
165         struct archive_read_filter_bidder *bidder;
166
167         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
168             ARCHIVE_STATE_NEW, "archive_read_support_filter_lzma");
169
170         if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
171                 return (ARCHIVE_FATAL);
172
173         bidder->data = NULL;
174         bidder->name = "lzma";
175         bidder->bid = lzma_bidder_bid;
176         bidder->init = lzma_bidder_init;
177         bidder->options = NULL;
178         bidder->free = NULL;
179 #if HAVE_LZMA_H && HAVE_LIBLZMA
180         return (ARCHIVE_OK);
181 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
182         return (ARCHIVE_OK);
183 #else
184         archive_set_error(_a, ARCHIVE_ERRNO_MISC,
185             "Using external lzma program for lzma decompression");
186         return (ARCHIVE_WARN);
187 #endif
188 }
189
190
191 #if ARCHIVE_VERSION_NUMBER < 4000000
192 int
193 archive_read_support_compression_lzip(struct archive *a)
194 {
195         return archive_read_support_filter_lzip(a);
196 }
197 #endif
198
199 int
200 archive_read_support_filter_lzip(struct archive *_a)
201 {
202         struct archive_read *a = (struct archive_read *)_a;
203         struct archive_read_filter_bidder *bidder;
204
205         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
206             ARCHIVE_STATE_NEW, "archive_read_support_filter_lzip");
207
208         if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
209                 return (ARCHIVE_FATAL);
210
211         bidder->data = NULL;
212         bidder->name = "lzip";
213         bidder->bid = lzip_bidder_bid;
214         bidder->init = lzip_bidder_init;
215         bidder->options = NULL;
216         bidder->free = NULL;
217 #if HAVE_LZMA_H && HAVE_LIBLZMA
218         return (ARCHIVE_OK);
219 #else
220         archive_set_error(_a, ARCHIVE_ERRNO_MISC,
221             "Using external lzip program for lzip decompression");
222         return (ARCHIVE_WARN);
223 #endif
224 }
225
226 /*
227  * Test whether we can handle this data.
228  */
229 static int
230 xz_bidder_bid(struct archive_read_filter_bidder *self,
231     struct archive_read_filter *filter)
232 {
233         const unsigned char *buffer;
234         ssize_t avail;
235
236         (void)self; /* UNUSED */
237
238         buffer = __archive_read_filter_ahead(filter, 6, &avail);
239         if (buffer == NULL)
240                 return (0);
241
242         /*
243          * Verify Header Magic Bytes : FD 37 7A 58 5A 00
244          */
245         if (memcmp(buffer, "\xFD\x37\x7A\x58\x5A\x00", 6) != 0)
246                 return (0);
247
248         return (48);
249 }
250
251 /*
252  * Test whether we can handle this data.
253  *
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.
262  */
263 static int
264 lzma_bidder_bid(struct archive_read_filter_bidder *self,
265     struct archive_read_filter *filter)
266 {
267         const unsigned char *buffer;
268         ssize_t avail;
269         uint32_t dicsize;
270         uint64_t uncompressed_size;
271         int bits_checked;
272
273         (void)self; /* UNUSED */
274
275         buffer = __archive_read_filter_ahead(filter, 14, &avail);
276         if (buffer == NULL)
277                 return (0);
278
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. */
295         bits_checked = 0;
296         if (buffer[0] > (4 * 5 + 4) * 9 + 8)
297                 return (0);
298         /* Most likely value in the first byte of LZMA stream. */
299         if (buffer[0] == 0x5d || buffer[0] == 0x5e)
300                 bits_checked += 8;
301
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'
305          * in this field. */
306         uncompressed_size = archive_le64dec(buffer+5);
307         if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1))
308                 bits_checked += 64;
309
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);
318         switch (dicsize) {
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. */
344                 bits_checked += 32;
345                 break;
346         default:
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
351                  * (1 << 20).*/
352                 if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 &&
353                     (dicsize & ((1 << 20)-1)) == 0 &&
354                     bits_checked == 8 + 64) {
355                         bits_checked += 32;
356                         break;
357                 }
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. */
361                 return (0);
362         }
363
364         /* TODO: The above test is still very weak.  It would be
365          * good to do better. */
366
367         return (bits_checked);
368 }
369
370 static int
371 lzip_has_member(struct archive_read_filter *filter)
372 {
373         const unsigned char *buffer;
374         ssize_t avail;
375         int bits_checked;
376         int log2dic;
377
378         buffer = __archive_read_filter_ahead(filter, 6, &avail);
379         if (buffer == NULL)
380                 return (0);
381
382         /*
383          * Verify Header Magic Bytes : 4C 5A 49 50 (`LZIP')
384          */
385         bits_checked = 0;
386         if (memcmp(buffer, "LZIP", 4) != 0)
387                 return (0);
388         bits_checked += 32;
389
390         /* A version number must be 0 or 1 */
391         if (buffer[4] != 0 && buffer[4] != 1)
392                 return (0);
393         bits_checked += 8;
394
395         /* Dictionary size. */
396         log2dic = buffer[5] & 0x1f;
397         if (log2dic < 12 || log2dic > 27)
398                 return (0);
399         bits_checked += 8;
400
401         return (bits_checked);
402 }
403
404 static int
405 lzip_bidder_bid(struct archive_read_filter_bidder *self,
406     struct archive_read_filter *filter)
407 {
408
409         (void)self; /* UNUSED */
410         return (lzip_has_member(filter));
411 }
412
413 #if HAVE_LZMA_H && HAVE_LIBLZMA
414
415 /*
416  * liblzma 4.999.7 and later support both lzma and xz streams.
417  */
418 static int
419 xz_bidder_init(struct archive_read_filter *self)
420 {
421         self->code = ARCHIVE_FILTER_XZ;
422         self->name = "xz";
423         return (xz_lzma_bidder_init(self));
424 }
425
426 static int
427 lzma_bidder_init(struct archive_read_filter *self)
428 {
429         self->code = ARCHIVE_FILTER_LZMA;
430         self->name = "lzma";
431         return (xz_lzma_bidder_init(self));
432 }
433
434 static int
435 lzip_bidder_init(struct archive_read_filter *self)
436 {
437         self->code = ARCHIVE_FILTER_LZIP;
438         self->name = "lzip";
439         return (xz_lzma_bidder_init(self));
440 }
441
442 /*
443  * Set an error code and choose an error message
444  */
445 static void
446 set_error(struct archive_read_filter *self, int ret)
447 {
448
449         switch (ret) {
450         case LZMA_STREAM_END: /* Found end of stream. */
451         case LZMA_OK: /* Decompressor made some progress. */
452                 break;
453         case LZMA_MEM_ERROR:
454                 archive_set_error(&self->archive->archive, ENOMEM,
455                     "Lzma library error: Cannot allocate memory");
456                 break;
457         case LZMA_MEMLIMIT_ERROR:
458                 archive_set_error(&self->archive->archive, ENOMEM,
459                     "Lzma library error: Out of memory");
460                 break;
461         case LZMA_FORMAT_ERROR:
462                 archive_set_error(&self->archive->archive,
463                     ARCHIVE_ERRNO_MISC,
464                     "Lzma library error: format not recognized");
465                 break;
466         case LZMA_OPTIONS_ERROR:
467                 archive_set_error(&self->archive->archive,
468                     ARCHIVE_ERRNO_MISC,
469                     "Lzma library error: Invalid options");
470                 break;
471         case LZMA_DATA_ERROR:
472                 archive_set_error(&self->archive->archive,
473                     ARCHIVE_ERRNO_MISC,
474                     "Lzma library error: Corrupted input data");
475                 break;
476         case LZMA_BUF_ERROR:
477                 archive_set_error(&self->archive->archive,
478                     ARCHIVE_ERRNO_MISC,
479                     "Lzma library error:  No progress is possible");
480                 break;
481         default:
482                 /* Return an error. */
483                 archive_set_error(&self->archive->archive,
484                     ARCHIVE_ERRNO_MISC,
485                     "Lzma decompression failed:  Unknown error");
486                 break;
487         }
488 }
489
490 /*
491  * Setup the callbacks.
492  */
493 static int
494 xz_lzma_bidder_init(struct archive_read_filter *self)
495 {
496         static const size_t out_block_size = 64 * 1024;
497         void *out_block;
498         struct private_data *state;
499         int ret;
500
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");
506                 free(out_block);
507                 free(state);
508                 return (ARCHIVE_FATAL);
509         }
510
511         self->data = state;
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;
517
518         state->stream.avail_in = 0;
519
520         state->stream.next_out = state->out_block;
521         state->stream.avail_out = state->out_block_size;
522
523         state->crc32 = 0;
524         if (self->code == ARCHIVE_FILTER_LZIP) {
525                 /*
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.
529                  */
530                 state->in_stream = 0;
531                 return (ARCHIVE_OK);
532         } else
533                 state->in_stream = 1;
534
535         /* Initialize compression library. */
536         if (self->code == ARCHIVE_FILTER_XZ)
537                 ret = lzma_stream_decoder(&(state->stream),
538                     LZMA_MEMLIMIT,/* memlimit */
539                     LZMA_CONCATENATED);
540         else
541                 ret = lzma_alone_decoder(&(state->stream),
542                     LZMA_MEMLIMIT);/* memlimit */
543
544         if (ret == LZMA_OK)
545                 return (ARCHIVE_OK);
546
547         /* Library setup failed: Choose an error message and clean up. */
548         set_error(self, ret);
549
550         free(state->out_block);
551         free(state);
552         self->data = NULL;
553         return (ARCHIVE_FATAL);
554 }
555
556 static int
557 lzip_init(struct archive_read_filter *self)
558 {
559         struct private_data *state;
560         const unsigned char *h;
561         lzma_filter filters[2];
562         unsigned char props[5];
563         ssize_t avail_in;
564         uint32_t dicsize;
565         int log2dic, ret;
566
567         state = (struct private_data *)self->data;
568         h = __archive_read_filter_ahead(self->upstream, 6, &avail_in);
569         if (h == NULL)
570                 return (ARCHIVE_FATAL);
571
572         /* Get a version number. */
573         state->lzip_ver = h[4];
574
575         /*
576          * Setup lzma property.
577          */
578         props[0] = 0x5d;
579
580         /* Get dictionary size. */
581         log2dic = h[5] & 0x1f;
582         if (log2dic < 12 || log2dic > 27)
583                 return (ARCHIVE_FATAL);
584         dicsize = 1U << log2dic;
585         if (log2dic > 12)
586                 dicsize -= (dicsize / 16) * (h[5] >> 5);
587         archive_le32enc(props+1, dicsize);
588
589         /* Consume lzip header. */
590         __archive_read_filter_consume(self->upstream, 6);
591         state->member_in = 6;
592
593         filters[0].id = LZMA_FILTER_LZMA1;
594         filters[0].options = NULL;
595         filters[1].id = LZMA_VLI_UNKNOWN;
596         filters[1].options = NULL;
597
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);
602         }
603         ret = lzma_raw_decoder(&(state->stream), filters);
604 #if LZMA_VERSION < 50000030
605         free(filters[0].options);
606 #endif
607         if (ret != LZMA_OK) {
608                 set_error(self, ret);
609                 return (ARCHIVE_FATAL);
610         }
611         return (ARCHIVE_OK);
612 }
613
614 static int
615 lzip_tail(struct archive_read_filter *self)
616 {
617         struct private_data *state;
618         const unsigned char *f;
619         ssize_t avail_in;
620         int tail;
621
622         state = (struct private_data *)self->data;
623         if (state->lzip_ver == 0)
624                 tail = 12;
625         else
626                 tail = 20;
627         f = __archive_read_filter_ahead(self->upstream, tail, &avail_in);
628         if (f == NULL && avail_in < 0)
629                 return (ARCHIVE_FATAL);
630         if (avail_in < tail) {
631                 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
632                     "Lzip: Remaining data is less bytes");
633                 return (ARCHIVE_FAILED);
634         }
635
636         /* Check the crc32 value of the uncompressed data of the current
637          * member */
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);
642         }
643
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);
649         }
650
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);
657         }
658         __archive_read_filter_consume(self->upstream, tail);
659
660         /* If current lzip data consists of multi member, try decompressing
661          * a next member. */
662         if (lzip_has_member(self->upstream) != 0) {
663                 state->in_stream = 0;
664                 state->crc32 = 0;
665                 state->member_out = 0;
666                 state->member_in = 0;
667                 state->eof = 0;
668         }
669         return (ARCHIVE_OK);
670 }
671
672 /*
673  * Return the next block of decompressed data.
674  */
675 static ssize_t
676 xz_filter_read(struct archive_read_filter *self, const void **p)
677 {
678         struct private_data *state;
679         size_t decompressed;
680         ssize_t avail_in;
681         int ret;
682
683         state = (struct private_data *)self->data;
684
685         /* Empty our output buffer. */
686         state->stream.next_out = state->out_block;
687         state->stream.avail_out = state->out_block_size;
688
689         /* Try to fill the output buffer. */
690         while (state->stream.avail_out > 0 && !state->eof) {
691                 if (!state->in_stream) {
692                         /*
693                          * Initialize liblzma for lzip
694                          */
695                         ret = lzip_init(self);
696                         if (ret != ARCHIVE_OK)
697                                 return (ret);
698                         state->in_stream = 1;
699                 }
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,
704                             ARCHIVE_ERRNO_MISC,
705                             "truncated input");
706                         return (ARCHIVE_FATAL);
707                 }
708                 state->stream.avail_in = avail_in;
709
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);
713                 switch (ret) {
714                 case LZMA_STREAM_END: /* Found end of stream. */
715                         state->eof = 1;
716                         /* FALL THROUGH */
717                 case LZMA_OK: /* Decompressor made some progress. */
718                         __archive_read_filter_consume(self->upstream,
719                             avail_in - state->stream.avail_in);
720                         state->member_in +=
721                             avail_in - state->stream.avail_in;
722                         break;
723                 default:
724                         set_error(self, ret);
725                         return (ARCHIVE_FATAL);
726                 }
727         }
728
729         decompressed = state->stream.next_out - state->out_block;
730         state->total_out += decompressed;
731         state->member_out += decompressed;
732         if (decompressed == 0)
733                 *p = NULL;
734         else {
735                 *p = state->out_block;
736                 if (self->code == ARCHIVE_FILTER_LZIP) {
737                         state->crc32 = lzma_crc32(state->out_block,
738                             decompressed, state->crc32);
739                         if (state->eof) {
740                                 ret = lzip_tail(self);
741                                 if (ret != ARCHIVE_OK)
742                                         return (ret);
743                         }
744                 }
745         }
746         return (decompressed);
747 }
748
749 /*
750  * Clean up the decompressor.
751  */
752 static int
753 xz_filter_close(struct archive_read_filter *self)
754 {
755         struct private_data *state;
756
757         state = (struct private_data *)self->data;
758         lzma_end(&(state->stream));
759         free(state->out_block);
760         free(state);
761         return (ARCHIVE_OK);
762 }
763
764 #else
765
766 #if HAVE_LZMADEC_H && HAVE_LIBLZMADEC
767
768 /*
769  * If we have the older liblzmadec library, then we can handle
770  * LZMA streams but not XZ streams.
771  */
772
773 /*
774  * Setup the callbacks.
775  */
776 static int
777 lzma_bidder_init(struct archive_read_filter *self)
778 {
779         static const size_t out_block_size = 64 * 1024;
780         void *out_block;
781         struct private_data *state;
782         ssize_t ret, avail_in;
783
784         self->code = ARCHIVE_FILTER_LZMA;
785         self->name = "lzma";
786
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");
792                 free(out_block);
793                 free(state);
794                 return (ARCHIVE_FATAL);
795         }
796
797         self->data = state;
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;
803
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;
812
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)
818                 return (ARCHIVE_OK);
819
820         /* Library setup failed: Clean up. */
821         archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
822             "Internal error initializing lzma library");
823
824         /* Override the error message if we know what really went wrong. */
825         switch (ret) {
826         case LZMADEC_HEADER_ERROR:
827                 archive_set_error(&self->archive->archive,
828                     ARCHIVE_ERRNO_MISC,
829                     "Internal error initializing compression library: "
830                     "invalid header");
831                 break;
832         case LZMADEC_MEM_ERROR:
833                 archive_set_error(&self->archive->archive, ENOMEM,
834                     "Internal error initializing compression library: "
835                     "out of memory");
836                 break;
837         }
838
839         free(state->out_block);
840         free(state);
841         self->data = NULL;
842         return (ARCHIVE_FATAL);
843 }
844
845 /*
846  * Return the next block of decompressed data.
847  */
848 static ssize_t
849 lzma_filter_read(struct archive_read_filter *self, const void **p)
850 {
851         struct private_data *state;
852         size_t decompressed;
853         ssize_t avail_in, ret;
854
855         state = (struct private_data *)self->data;
856
857         /* Empty our output buffer. */
858         state->stream.next_out = state->out_block;
859         state->stream.avail_out = state->out_block_size;
860
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,
867                             ARCHIVE_ERRNO_MISC,
868                             "truncated lzma input");
869                         return (ARCHIVE_FATAL);
870                 }
871                 state->stream.avail_in = avail_in;
872
873                 /* Decompress as much as we can in one pass. */
874                 ret = lzmadec_decode(&(state->stream), avail_in == 0);
875                 switch (ret) {
876                 case LZMADEC_STREAM_END: /* Found end of stream. */
877                         state->eof = 1;
878                         /* FALL THROUGH */
879                 case LZMADEC_OK: /* Decompressor made some progress. */
880                         __archive_read_filter_consume(self->upstream,
881                             avail_in - state->stream.avail_in);
882                         break;
883                 case LZMADEC_BUF_ERROR: /* Insufficient input data? */
884                         archive_set_error(&self->archive->archive,
885                             ARCHIVE_ERRNO_MISC,
886                             "Insufficient compressed data");
887                         return (ARCHIVE_FATAL);
888                 default:
889                         /* Return an error. */
890                         archive_set_error(&self->archive->archive,
891                             ARCHIVE_ERRNO_MISC,
892                             "Lzma decompression failed");
893                         return (ARCHIVE_FATAL);
894                 }
895         }
896
897         decompressed = state->stream.next_out - state->out_block;
898         state->total_out += decompressed;
899         if (decompressed == 0)
900                 *p = NULL;
901         else
902                 *p = state->out_block;
903         return (decompressed);
904 }
905
906 /*
907  * Clean up the decompressor.
908  */
909 static int
910 lzma_filter_close(struct archive_read_filter *self)
911 {
912         struct private_data *state;
913         int ret;
914
915         state = (struct private_data *)self->data;
916         ret = ARCHIVE_OK;
917         switch (lzmadec_end(&(state->stream))) {
918         case LZMADEC_OK:
919                 break;
920         default:
921                 archive_set_error(&(self->archive->archive),
922                     ARCHIVE_ERRNO_MISC,
923                     "Failed to clean up %s compressor",
924                     self->archive->archive.compression_name);
925                 ret = ARCHIVE_FATAL;
926         }
927
928         free(state->out_block);
929         free(state);
930         return (ret);
931 }
932
933 #else
934
935 /*
936  *
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.
940  *
941  */
942 static int
943 lzma_bidder_init(struct archive_read_filter *self)
944 {
945         int r;
946
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;
952         self->name = "lzma";
953         return (r);
954 }
955
956 #endif /* HAVE_LZMADEC_H */
957
958
959 static int
960 xz_bidder_init(struct archive_read_filter *self)
961 {
962         int r;
963
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;
969         self->name = "xz";
970         return (r);
971 }
972
973 static int
974 lzip_bidder_init(struct archive_read_filter *self)
975 {
976         int r;
977
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;
983         self->name = "lzip";
984         return (r);
985 }
986
987
988 #endif /* HAVE_LZMA_H */