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->bid = xz_bidder_bid;
140 	bidder->init = xz_bidder_init;
141 	bidder->options = NULL;
142 	bidder->free = NULL;
143 #if HAVE_LZMA_H && HAVE_LIBLZMA
144 	return (ARCHIVE_OK);
145 #else
146 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
147 	    "Using external unxz program for xz decompression");
148 	return (ARCHIVE_WARN);
149 #endif
150 }
151 
152 #if ARCHIVE_VERSION_NUMBER < 4000000
153 int
154 archive_read_support_compression_lzma(struct archive *a)
155 {
156 	return archive_read_support_filter_lzma(a);
157 }
158 #endif
159 
160 int
161 archive_read_support_filter_lzma(struct archive *_a)
162 {
163 	struct archive_read *a = (struct archive_read *)_a;
164 	struct archive_read_filter_bidder *bidder;
165 
166 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
167 	    ARCHIVE_STATE_NEW, "archive_read_support_filter_lzma");
168 
169 	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
170 		return (ARCHIVE_FATAL);
171 
172 	bidder->data = NULL;
173 	bidder->bid = lzma_bidder_bid;
174 	bidder->init = lzma_bidder_init;
175 	bidder->options = NULL;
176 	bidder->free = NULL;
177 #if HAVE_LZMA_H && HAVE_LIBLZMA
178 	return (ARCHIVE_OK);
179 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
180 	return (ARCHIVE_OK);
181 #else
182 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
183 	    "Using external unlzma program for lzma decompression");
184 	return (ARCHIVE_WARN);
185 #endif
186 }
187 
188 
189 #if ARCHIVE_VERSION_NUMBER < 4000000
190 int
191 archive_read_support_compression_lzip(struct archive *a)
192 {
193 	return archive_read_support_filter_lzip(a);
194 }
195 #endif
196 
197 int
198 archive_read_support_filter_lzip(struct archive *_a)
199 {
200 	struct archive_read *a = (struct archive_read *)_a;
201 	struct archive_read_filter_bidder *bidder;
202 
203 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
204 	    ARCHIVE_STATE_NEW, "archive_read_support_filter_lzip");
205 
206 	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
207 		return (ARCHIVE_FATAL);
208 
209 	bidder->data = NULL;
210 	bidder->bid = lzip_bidder_bid;
211 	bidder->init = lzip_bidder_init;
212 	bidder->options = NULL;
213 	bidder->free = NULL;
214 #if HAVE_LZMA_H && HAVE_LIBLZMA
215 	return (ARCHIVE_OK);
216 #else
217 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
218 	    "Using external lzip program for lzip decompression");
219 	return (ARCHIVE_WARN);
220 #endif
221 }
222 
223 /*
224  * Test whether we can handle this data.
225  */
226 static int
227 xz_bidder_bid(struct archive_read_filter_bidder *self,
228     struct archive_read_filter *filter)
229 {
230 	const unsigned char *buffer;
231 	ssize_t avail;
232 
233 	(void)self; /* UNUSED */
234 
235 	buffer = __archive_read_filter_ahead(filter, 6, &avail);
236 	if (buffer == NULL)
237 		return (0);
238 
239 	/*
240 	 * Verify Header Magic Bytes : FD 37 7A 58 5A 00
241 	 */
242 	if (memcmp(buffer, "\xFD\x37\x7A\x58\x5A\x00", 6) != 0)
243 		return (0);
244 
245 	return (48);
246 }
247 
248 /*
249  * Test whether we can handle this data.
250  *
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.
259  */
260 static int
261 lzma_bidder_bid(struct archive_read_filter_bidder *self,
262     struct archive_read_filter *filter)
263 {
264 	const unsigned char *buffer;
265 	ssize_t avail;
266 	uint32_t dicsize;
267 	uint64_t uncompressed_size;
268 	int bits_checked;
269 
270 	(void)self; /* UNUSED */
271 
272 	buffer = __archive_read_filter_ahead(filter, 14, &avail);
273 	if (buffer == NULL)
274 		return (0);
275 
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. */
292 	bits_checked = 0;
293 	if (buffer[0] > (4 * 5 + 4) * 9 + 8)
294 		return (0);
295 	/* Most likely value in the first byte of LZMA stream. */
296 	if (buffer[0] == 0x5d || buffer[0] == 0x5e)
297 		bits_checked += 8;
298 
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'
302 	 * in this field. */
303 	uncompressed_size = archive_le64dec(buffer+5);
304 	if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1))
305 		bits_checked += 64;
306 
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);
315 	switch (dicsize) {
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. */
341 		bits_checked += 32;
342 		break;
343 	default:
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
348 		 * (1 << 20).*/
349 		if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 &&
350 		    (dicsize & ((1 << 20)-1)) == 0 &&
351 		    bits_checked == 8 + 64) {
352 			bits_checked += 32;
353 			break;
354 		}
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. */
358 		return (0);
359 	}
360 
361 	/* TODO: The above test is still very weak.  It would be
362 	 * good to do better. */
363 
364 	return (bits_checked);
365 }
366 
367 static int
368 lzip_has_member(struct archive_read_filter *filter)
369 {
370 	const unsigned char *buffer;
371 	ssize_t avail;
372 	int bits_checked;
373 	int log2dic;
374 
375 	buffer = __archive_read_filter_ahead(filter, 6, &avail);
376 	if (buffer == NULL)
377 		return (0);
378 
379 	/*
380 	 * Verify Header Magic Bytes : 4C 5A 49 50 (`LZIP')
381 	 */
382 	bits_checked = 0;
383 	if (memcmp(buffer, "LZIP", 4) != 0)
384 		return (0);
385 	bits_checked += 32;
386 
387 	/* A version number must be 0 or 1 */
388 	if (buffer[4] != 0 && buffer[4] != 1)
389 		return (0);
390 	bits_checked += 8;
391 
392 	/* Dictionary size. */
393 	log2dic = buffer[5] & 0x1f;
394 	if (log2dic < 12 || log2dic > 27)
395 		return (0);
396 	bits_checked += 8;
397 
398 	return (bits_checked);
399 }
400 
401 static int
402 lzip_bidder_bid(struct archive_read_filter_bidder *self,
403     struct archive_read_filter *filter)
404 {
405 
406 	(void)self; /* UNUSED */
407 	return (lzip_has_member(filter));
408 }
409 
410 #if HAVE_LZMA_H && HAVE_LIBLZMA
411 
412 /*
413  * liblzma 4.999.7 and later support both lzma and xz streams.
414  */
415 static int
416 xz_bidder_init(struct archive_read_filter *self)
417 {
418 	self->code = ARCHIVE_COMPRESSION_XZ;
419 	self->name = "xz";
420 	return (xz_lzma_bidder_init(self));
421 }
422 
423 static int
424 lzma_bidder_init(struct archive_read_filter *self)
425 {
426 	self->code = ARCHIVE_COMPRESSION_LZMA;
427 	self->name = "lzma";
428 	return (xz_lzma_bidder_init(self));
429 }
430 
431 static int
432 lzip_bidder_init(struct archive_read_filter *self)
433 {
434 	self->code = ARCHIVE_COMPRESSION_LZIP;
435 	self->name = "lzip";
436 	return (xz_lzma_bidder_init(self));
437 }
438 
439 /*
440  * Set an error code and choose an error message
441  */
442 static void
443 set_error(struct archive_read_filter *self, int ret)
444 {
445 
446 	switch (ret) {
447 	case LZMA_STREAM_END: /* Found end of stream. */
448 	case LZMA_OK: /* Decompressor made some progress. */
449 		break;
450 	case LZMA_MEM_ERROR:
451 		archive_set_error(&self->archive->archive, ENOMEM,
452 		    "Lzma library error: Cannot allocate memory");
453 		break;
454 	case LZMA_MEMLIMIT_ERROR:
455 		archive_set_error(&self->archive->archive, ENOMEM,
456 		    "Lzma library error: Out of memory");
457 		break;
458 	case LZMA_FORMAT_ERROR:
459 		archive_set_error(&self->archive->archive,
460 		    ARCHIVE_ERRNO_MISC,
461 		    "Lzma library error: format not recognized");
462 		break;
463 	case LZMA_OPTIONS_ERROR:
464 		archive_set_error(&self->archive->archive,
465 		    ARCHIVE_ERRNO_MISC,
466 		    "Lzma library error: Invalid options");
467 		break;
468 	case LZMA_DATA_ERROR:
469 		archive_set_error(&self->archive->archive,
470 		    ARCHIVE_ERRNO_MISC,
471 		    "Lzma library error: Corrupted input data");
472 		break;
473 	case LZMA_BUF_ERROR:
474 		archive_set_error(&self->archive->archive,
475 		    ARCHIVE_ERRNO_MISC,
476 		    "Lzma library error:  No progress is possible");
477 		break;
478 	default:
479 		/* Return an error. */
480 		archive_set_error(&self->archive->archive,
481 		    ARCHIVE_ERRNO_MISC,
482 		    "Lzma decompression failed:  Unknown error");
483 		break;
484 	}
485 }
486 
487 /*
488  * Setup the callbacks.
489  */
490 static int
491 xz_lzma_bidder_init(struct archive_read_filter *self)
492 {
493 	static const size_t out_block_size = 64 * 1024;
494 	void *out_block;
495 	struct private_data *state;
496 	int ret;
497 
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");
503 		free(out_block);
504 		free(state);
505 		return (ARCHIVE_FATAL);
506 	}
507 
508 	self->data = state;
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;
514 
515 	state->stream.avail_in = 0;
516 
517 	state->stream.next_out = state->out_block;
518 	state->stream.avail_out = state->out_block_size;
519 
520 	state->crc32 = 0;
521 	if (self->code == ARCHIVE_COMPRESSION_LZIP) {
522 		/*
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.
526 		 */
527 		state->in_stream = 0;
528 		return (ARCHIVE_OK);
529 	} else
530 		state->in_stream = 1;
531 
532 	/* Initialize compression library. */
533 	if (self->code == ARCHIVE_COMPRESSION_XZ)
534 		ret = lzma_stream_decoder(&(state->stream),
535 		    LZMA_MEMLIMIT,/* memlimit */
536 		    LZMA_CONCATENATED);
537 	else
538 		ret = lzma_alone_decoder(&(state->stream),
539 		    LZMA_MEMLIMIT);/* memlimit */
540 
541 	if (ret == LZMA_OK)
542 		return (ARCHIVE_OK);
543 
544 	/* Library setup failed: Choose an error message and clean up. */
545 	set_error(self, ret);
546 
547 	free(state->out_block);
548 	free(state);
549 	self->data = NULL;
550 	return (ARCHIVE_FATAL);
551 }
552 
553 static int
554 lzip_init(struct archive_read_filter *self)
555 {
556 	struct private_data *state;
557 	const unsigned char *h;
558 	lzma_filter filters[2];
559 	unsigned char props[5];
560 	ssize_t avail_in;
561 	uint32_t dicsize;
562 	int log2dic, ret;
563 
564 	state = (struct private_data *)self->data;
565 	h = __archive_read_filter_ahead(self->upstream, 6, &avail_in);
566 	if (h == NULL)
567 		return (ARCHIVE_FATAL);
568 
569 	/* Get a version number. */
570 	state->lzip_ver = h[4];
571 
572 	/*
573 	 * Setup lzma property.
574 	 */
575 	props[0] = 0x5d;
576 
577 	/* Get dictionary size. */
578 	log2dic = h[5] & 0x1f;
579 	if (log2dic < 12 || log2dic > 27)
580 		return (ARCHIVE_FATAL);
581 	dicsize = 1U << log2dic;
582 	if (log2dic > 12)
583 		dicsize -= (dicsize / 16) * (h[5] >> 5);
584 	archive_le32enc(props+1, dicsize);
585 
586 	/* Consume lzip header. */
587 	__archive_read_filter_consume(self->upstream, 6);
588 	state->member_in = 6;
589 
590 	filters[0].id = LZMA_FILTER_LZMA1;
591 	filters[0].options = NULL;
592 	filters[1].id = LZMA_VLI_UNKNOWN;
593 	filters[1].options = NULL;
594 
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);
599 	}
600 	ret = lzma_raw_decoder(&(state->stream), filters);
601 #if LZMA_VERSION < 50000030
602 	free(filters[0].options);
603 #endif
604 	if (ret != LZMA_OK) {
605 		set_error(self, ret);
606 		return (ARCHIVE_FATAL);
607 	}
608 	return (ARCHIVE_OK);
609 }
610 
611 static int
612 lzip_tail(struct archive_read_filter *self)
613 {
614 	struct private_data *state;
615 	const unsigned char *f;
616 	ssize_t avail_in;
617 	int tail;
618 
619 	state = (struct private_data *)self->data;
620 	if (state->lzip_ver == 0)
621 		tail = 12;
622 	else
623 		tail = 20;
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);
631 	}
632 
633 	/* Check the crc32 value of the uncompressed data of the current
634 	 * member */
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);
639 	}
640 
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);
646 	}
647 
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);
654 	}
655 	__archive_read_filter_consume(self->upstream, tail);
656 
657 	/* If current lzip data consists of multi member, try decompressing
658 	 * a next member. */
659 	if (lzip_has_member(self->upstream) != 0) {
660 		state->in_stream = 0;
661 		state->crc32 = 0;
662 		state->member_out = 0;
663 		state->member_in = 0;
664 		state->eof = 0;
665 	}
666 	return (ARCHIVE_OK);
667 }
668 
669 /*
670  * Return the next block of decompressed data.
671  */
672 static ssize_t
673 xz_filter_read(struct archive_read_filter *self, const void **p)
674 {
675 	struct private_data *state;
676 	size_t decompressed;
677 	ssize_t avail_in;
678 	int ret;
679 
680 	state = (struct private_data *)self->data;
681 
682 	/* Empty our output buffer. */
683 	state->stream.next_out = state->out_block;
684 	state->stream.avail_out = state->out_block_size;
685 
686 	/* Try to fill the output buffer. */
687 	while (state->stream.avail_out > 0 && !state->eof) {
688 		if (!state->in_stream) {
689 			/*
690 			 * Initialize liblzma for lzip
691 			 */
692 			ret = lzip_init(self);
693 			if (ret != ARCHIVE_OK)
694 				return (ret);
695 			state->in_stream = 1;
696 		}
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,
701 			    ARCHIVE_ERRNO_MISC,
702 			    "truncated input");
703 			return (ARCHIVE_FATAL);
704 		}
705 		state->stream.avail_in = avail_in;
706 
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);
710 		switch (ret) {
711 		case LZMA_STREAM_END: /* Found end of stream. */
712 			state->eof = 1;
713 			/* FALL THROUGH */
714 		case LZMA_OK: /* Decompressor made some progress. */
715 			__archive_read_filter_consume(self->upstream,
716 			    avail_in - state->stream.avail_in);
717 			state->member_in +=
718 			    avail_in - state->stream.avail_in;
719 			break;
720 		default:
721 			set_error(self, ret);
722 			return (ARCHIVE_FATAL);
723 		}
724 	}
725 
726 	decompressed = state->stream.next_out - state->out_block;
727 	state->total_out += decompressed;
728 	state->member_out += decompressed;
729 	if (decompressed == 0)
730 		*p = NULL;
731 	else {
732 		*p = state->out_block;
733 		if (self->code == ARCHIVE_COMPRESSION_LZIP) {
734 			state->crc32 = lzma_crc32(state->out_block,
735 			    decompressed, state->crc32);
736 			if (state->eof) {
737 				ret = lzip_tail(self);
738 				if (ret != ARCHIVE_OK)
739 					return (ret);
740 			}
741 		}
742 	}
743 	return (decompressed);
744 }
745 
746 /*
747  * Clean up the decompressor.
748  */
749 static int
750 xz_filter_close(struct archive_read_filter *self)
751 {
752 	struct private_data *state;
753 
754 	state = (struct private_data *)self->data;
755 	lzma_end(&(state->stream));
756 	free(state->out_block);
757 	free(state);
758 	return (ARCHIVE_OK);
759 }
760 
761 #else
762 
763 #if HAVE_LZMADEC_H && HAVE_LIBLZMADEC
764 
765 /*
766  * If we have the older liblzmadec library, then we can handle
767  * LZMA streams but not XZ streams.
768  */
769 
770 /*
771  * Setup the callbacks.
772  */
773 static int
774 lzma_bidder_init(struct archive_read_filter *self)
775 {
776 	static const size_t out_block_size = 64 * 1024;
777 	void *out_block;
778 	struct private_data *state;
779 	ssize_t ret, avail_in;
780 
781 	self->code = ARCHIVE_COMPRESSION_LZMA;
782 	self->name = "lzma";
783 
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");
789 		free(out_block);
790 		free(state);
791 		return (ARCHIVE_FATAL);
792 	}
793 
794 	self->data = state;
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;
800 
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;
809 
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)
815 		return (ARCHIVE_OK);
816 
817 	/* Library setup failed: Clean up. */
818 	archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
819 	    "Internal error initializing lzma library");
820 
821 	/* Override the error message if we know what really went wrong. */
822 	switch (ret) {
823 	case LZMADEC_HEADER_ERROR:
824 		archive_set_error(&self->archive->archive,
825 		    ARCHIVE_ERRNO_MISC,
826 		    "Internal error initializing compression library: "
827 		    "invalid header");
828 		break;
829 	case LZMADEC_MEM_ERROR:
830 		archive_set_error(&self->archive->archive, ENOMEM,
831 		    "Internal error initializing compression library: "
832 		    "out of memory");
833 		break;
834 	}
835 
836 	free(state->out_block);
837 	free(state);
838 	self->data = NULL;
839 	return (ARCHIVE_FATAL);
840 }
841 
842 /*
843  * Return the next block of decompressed data.
844  */
845 static ssize_t
846 lzma_filter_read(struct archive_read_filter *self, const void **p)
847 {
848 	struct private_data *state;
849 	size_t decompressed;
850 	ssize_t avail_in, ret;
851 
852 	state = (struct private_data *)self->data;
853 
854 	/* Empty our output buffer. */
855 	state->stream.next_out = state->out_block;
856 	state->stream.avail_out = state->out_block_size;
857 
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,
864 			    ARCHIVE_ERRNO_MISC,
865 			    "truncated lzma input");
866 			return (ARCHIVE_FATAL);
867 		}
868 		state->stream.avail_in = avail_in;
869 
870 		/* Decompress as much as we can in one pass. */
871 		ret = lzmadec_decode(&(state->stream), avail_in == 0);
872 		switch (ret) {
873 		case LZMADEC_STREAM_END: /* Found end of stream. */
874 			state->eof = 1;
875 			/* FALL THROUGH */
876 		case LZMADEC_OK: /* Decompressor made some progress. */
877 			__archive_read_filter_consume(self->upstream,
878 			    avail_in - state->stream.avail_in);
879 			break;
880 		case LZMADEC_BUF_ERROR: /* Insufficient input data? */
881 			archive_set_error(&self->archive->archive,
882 			    ARCHIVE_ERRNO_MISC,
883 			    "Insufficient compressed data");
884 			return (ARCHIVE_FATAL);
885 		default:
886 			/* Return an error. */
887 			archive_set_error(&self->archive->archive,
888 			    ARCHIVE_ERRNO_MISC,
889 			    "Lzma decompression failed");
890 			return (ARCHIVE_FATAL);
891 		}
892 	}
893 
894 	decompressed = state->stream.next_out - state->out_block;
895 	state->total_out += decompressed;
896 	if (decompressed == 0)
897 		*p = NULL;
898 	else
899 		*p = state->out_block;
900 	return (decompressed);
901 }
902 
903 /*
904  * Clean up the decompressor.
905  */
906 static int
907 lzma_filter_close(struct archive_read_filter *self)
908 {
909 	struct private_data *state;
910 	int ret;
911 
912 	state = (struct private_data *)self->data;
913 	ret = ARCHIVE_OK;
914 	switch (lzmadec_end(&(state->stream))) {
915 	case LZMADEC_OK:
916 		break;
917 	default:
918 		archive_set_error(&(self->archive->archive),
919 		    ARCHIVE_ERRNO_MISC,
920 		    "Failed to clean up %s compressor",
921 		    self->archive->archive.compression_name);
922 		ret = ARCHIVE_FATAL;
923 	}
924 
925 	free(state->out_block);
926 	free(state);
927 	return (ret);
928 }
929 
930 #else
931 
932 /*
933  *
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.
937  *
938  */
939 static int
940 lzma_bidder_init(struct archive_read_filter *self)
941 {
942 	int r;
943 
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;
949 	self->name = "lzma";
950 	return (r);
951 }
952 
953 #endif /* HAVE_LZMADEC_H */
954 
955 
956 static int
957 xz_bidder_init(struct archive_read_filter *self)
958 {
959 	int r;
960 
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;
966 	self->name = "xz";
967 	return (r);
968 }
969 
970 static int
971 lzip_bidder_init(struct archive_read_filter *self)
972 {
973 	int r;
974 
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;
980 	self->name = "lzip";
981 	return (r);
982 }
983 
984 
985 #endif /* HAVE_LZMA_H */
986