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 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #if HAVE_LZMA_H
43 #include <lzma.h>
44 #endif
45 
46 #include "archive.h"
47 #include "archive_endian.h"
48 #include "archive_private.h"
49 #include "archive_read_private.h"
50 
51 #if HAVE_LZMA_H && HAVE_LIBLZMA
52 
53 struct private_data {
54 	lzma_stream	 stream;
55 	unsigned char	*out_block;
56 	size_t		 out_block_size;
57 	int64_t		 total_out;
58 	char		 eof; /* True = found end of compressed data. */
59 	char		 in_stream;
60 
61 	/* Following variables are used for lzip only. */
62 	char		 lzip_ver;
63 	uint32_t	 crc32;
64 	int64_t		 member_in;
65 	int64_t		 member_out;
66 };
67 
68 #if LZMA_VERSION_MAJOR >= 5
69 /* Effectively disable the limiter. */
70 #define LZMA_MEMLIMIT	UINT64_MAX
71 #else
72 /* NOTE: This needs to check memory size which running system has. */
73 #define LZMA_MEMLIMIT	(1U << 30)
74 #endif
75 
76 /* Combined lzip/lzma/xz filter */
77 static ssize_t	xz_filter_read(struct archive_read_filter *, const void **);
78 static int	xz_filter_close(struct archive_read_filter *);
79 static int	xz_lzma_bidder_init(struct archive_read_filter *);
80 
81 #endif
82 
83 /*
84  * Note that we can detect xz and lzma compressed files even if we
85  * can't decompress them.  (In fact, we like detecting them because we
86  * can give better error messages.)  So the bid framework here gets
87  * compiled even if no lzma library is available.
88  */
89 static int	xz_bidder_bid(struct archive_read_filter_bidder *,
90 		    struct archive_read_filter *);
91 static int	xz_bidder_init(struct archive_read_filter *);
92 static int	lzma_bidder_bid(struct archive_read_filter_bidder *,
93 		    struct archive_read_filter *);
94 static int	lzma_bidder_init(struct archive_read_filter *);
95 static int	lzip_has_member(struct archive_read_filter *);
96 static int	lzip_bidder_bid(struct archive_read_filter_bidder *,
97 		    struct archive_read_filter *);
98 static int	lzip_bidder_init(struct archive_read_filter *);
99 
100 #if ARCHIVE_VERSION_NUMBER < 4000000
101 /* Deprecated; remove in libarchive 4.0 */
102 int
archive_read_support_compression_xz(struct archive * a)103 archive_read_support_compression_xz(struct archive *a)
104 {
105 	return archive_read_support_filter_xz(a);
106 }
107 #endif
108 
109 static const struct archive_read_filter_bidder_vtable
110 xz_bidder_vtable = {
111 	.bid = xz_bidder_bid,
112 	.init = xz_bidder_init,
113 };
114 
115 int
archive_read_support_filter_xz(struct archive * _a)116 archive_read_support_filter_xz(struct archive *_a)
117 {
118 	struct archive_read *a = (struct archive_read *)_a;
119 
120 	if (__archive_read_register_bidder(a, NULL, "xz",
121 				&xz_bidder_vtable) != ARCHIVE_OK)
122 		return (ARCHIVE_FATAL);
123 
124 #if HAVE_LZMA_H && HAVE_LIBLZMA
125 	return (ARCHIVE_OK);
126 #else
127 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
128 	    "Using external xz program for xz decompression");
129 	return (ARCHIVE_WARN);
130 #endif
131 }
132 
133 #if ARCHIVE_VERSION_NUMBER < 4000000
134 int
archive_read_support_compression_lzma(struct archive * a)135 archive_read_support_compression_lzma(struct archive *a)
136 {
137 	return archive_read_support_filter_lzma(a);
138 }
139 #endif
140 
141 static const struct archive_read_filter_bidder_vtable
142 lzma_bidder_vtable = {
143 	.bid = lzma_bidder_bid,
144 	.init = lzma_bidder_init,
145 };
146 
147 int
archive_read_support_filter_lzma(struct archive * _a)148 archive_read_support_filter_lzma(struct archive *_a)
149 {
150 	struct archive_read *a = (struct archive_read *)_a;
151 
152 	if (__archive_read_register_bidder(a, NULL, "lzma",
153 				&lzma_bidder_vtable) != ARCHIVE_OK)
154 		return (ARCHIVE_FATAL);
155 
156 #if HAVE_LZMA_H && HAVE_LIBLZMA
157 	return (ARCHIVE_OK);
158 #else
159 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
160 	    "Using external lzma program for lzma decompression");
161 	return (ARCHIVE_WARN);
162 #endif
163 }
164 
165 
166 #if ARCHIVE_VERSION_NUMBER < 4000000
167 int
archive_read_support_compression_lzip(struct archive * a)168 archive_read_support_compression_lzip(struct archive *a)
169 {
170 	return archive_read_support_filter_lzip(a);
171 }
172 #endif
173 
174 static const struct archive_read_filter_bidder_vtable
175 lzip_bidder_vtable = {
176 	.bid = lzip_bidder_bid,
177 	.init = lzip_bidder_init,
178 };
179 
180 int
archive_read_support_filter_lzip(struct archive * _a)181 archive_read_support_filter_lzip(struct archive *_a)
182 {
183 	struct archive_read *a = (struct archive_read *)_a;
184 
185 	if (__archive_read_register_bidder(a, NULL, "lzip",
186 				&lzip_bidder_vtable) != ARCHIVE_OK)
187 		return (ARCHIVE_FATAL);
188 
189 #if HAVE_LZMA_H && HAVE_LIBLZMA
190 	return (ARCHIVE_OK);
191 #else
192 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
193 	    "Using external lzip program for lzip decompression");
194 	return (ARCHIVE_WARN);
195 #endif
196 }
197 
198 /*
199  * Test whether we can handle this data.
200  */
201 static int
xz_bidder_bid(struct archive_read_filter_bidder * self,struct archive_read_filter * filter)202 xz_bidder_bid(struct archive_read_filter_bidder *self,
203     struct archive_read_filter *filter)
204 {
205 	const unsigned char *buffer;
206 	ssize_t avail;
207 
208 	(void)self; /* UNUSED */
209 
210 	buffer = __archive_read_filter_ahead(filter, 6, &avail);
211 	if (buffer == NULL)
212 		return (0);
213 
214 	/*
215 	 * Verify Header Magic Bytes : FD 37 7A 58 5A 00
216 	 */
217 	if (memcmp(buffer, "\xFD\x37\x7A\x58\x5A\x00", 6) != 0)
218 		return (0);
219 
220 	return (48);
221 }
222 
223 /*
224  * Test whether we can handle this data.
225  *
226  * <sigh> LZMA has a rather poor file signature.  Zeros do not
227  * make good signature bytes as a rule, and the only non-zero byte
228  * here is an ASCII character.  For example, an uncompressed tar
229  * archive whose first file is ']' would satisfy this check.  It may
230  * be necessary to exclude LZMA from compression_all() because of
231  * this.  Clients of libarchive would then have to explicitly enable
232  * LZMA checking instead of (or in addition to) compression_all() when
233  * they have other evidence (file name, command-line option) to go on.
234  */
235 static int
lzma_bidder_bid(struct archive_read_filter_bidder * self,struct archive_read_filter * filter)236 lzma_bidder_bid(struct archive_read_filter_bidder *self,
237     struct archive_read_filter *filter)
238 {
239 	const unsigned char *buffer;
240 	ssize_t avail;
241 	uint32_t dicsize;
242 	uint64_t uncompressed_size;
243 	int bits_checked;
244 
245 	(void)self; /* UNUSED */
246 
247 	buffer = __archive_read_filter_ahead(filter, 14, &avail);
248 	if (buffer == NULL)
249 		return (0);
250 
251 	/* First byte of raw LZMA stream is commonly 0x5d.
252 	 * The first byte is a special number, which consists of
253 	 * three parameters of LZMA compression, a number of literal
254 	 * context bits(which is from 0 to 8, default is 3), a number
255 	 * of literal pos bits(which is from 0 to 4, default is 0),
256 	 * a number of pos bits(which is from 0 to 4, default is 2).
257 	 * The first byte is made by
258 	 * (pos bits * 5 + literal pos bit) * 9 + * literal contest bit,
259 	 * and so the default value in this field is
260 	 * (2 * 5 + 0) * 9 + 3 = 0x5d.
261 	 * lzma of LZMA SDK has options to change those parameters.
262 	 * It means a range of this field is from 0 to 224. And lzma of
263 	 * XZ Utils with option -e records 0x5e in this field. */
264 	/* NOTE: If this checking of the first byte increases false
265 	 * recognition, we should allow only 0x5d and 0x5e for the first
266 	 * byte of LZMA stream. */
267 	bits_checked = 0;
268 	if (buffer[0] > (4 * 5 + 4) * 9 + 8)
269 		return (0);
270 	/* Most likely value in the first byte of LZMA stream. */
271 	if (buffer[0] == 0x5d || buffer[0] == 0x5e)
272 		bits_checked += 8;
273 
274 	/* Sixth through fourteenth bytes are uncompressed size,
275 	 * stored in little-endian order. `-1' means uncompressed
276 	 * size is unknown and lzma of XZ Utils always records `-1'
277 	 * in this field. */
278 	uncompressed_size = archive_le64dec(buffer+5);
279 	if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1))
280 		bits_checked += 64;
281 
282 	/* Second through fifth bytes are dictionary size, stored in
283 	 * little-endian order. The minimum dictionary size is
284 	 * 1 << 12(4KiB) which the lzma of LZMA SDK uses with option
285 	 * -d12 and the maximum dictionary size is 1 << 29(512MiB)
286 	 * which the one uses with option -d29.
287 	 * NOTE: A comment of LZMA SDK source code says this dictionary
288 	 * range is from 1 << 12 to 1 << 30. */
289 	dicsize = archive_le32dec(buffer+1);
290 	switch (dicsize) {
291 	case 0x00001000:/* lzma of LZMA SDK option -d12. */
292 	case 0x00002000:/* lzma of LZMA SDK option -d13. */
293 	case 0x00004000:/* lzma of LZMA SDK option -d14. */
294 	case 0x00008000:/* lzma of LZMA SDK option -d15. */
295 	case 0x00010000:/* lzma of XZ Utils option -0 and -1.
296 			 * lzma of LZMA SDK option -d16. */
297 	case 0x00020000:/* lzma of LZMA SDK option -d17. */
298 	case 0x00040000:/* lzma of LZMA SDK option -d18. */
299 	case 0x00080000:/* lzma of XZ Utils option -2.
300 			 * lzma of LZMA SDK option -d19. */
301 	case 0x00100000:/* lzma of XZ Utils option -3.
302 			 * lzma of LZMA SDK option -d20. */
303 	case 0x00200000:/* lzma of XZ Utils option -4.
304 			 * lzma of LZMA SDK option -d21. */
305 	case 0x00400000:/* lzma of XZ Utils option -5.
306 			 * lzma of LZMA SDK option -d22. */
307 	case 0x00800000:/* lzma of XZ Utils option -6.
308 			 * lzma of LZMA SDK option -d23. */
309 	case 0x01000000:/* lzma of XZ Utils option -7.
310 			 * lzma of LZMA SDK option -d24. */
311 	case 0x02000000:/* lzma of XZ Utils option -8.
312 			 * lzma of LZMA SDK option -d25. */
313 	case 0x04000000:/* lzma of XZ Utils option -9.
314 			 * lzma of LZMA SDK option -d26. */
315 	case 0x08000000:/* lzma of LZMA SDK option -d27. */
316 		bits_checked += 32;
317 		break;
318 	default:
319 		/* If a memory usage for encoding was not enough on
320 		 * the platform where LZMA stream was made, lzma of
321 		 * XZ Utils automatically decreased the dictionary
322 		 * size to enough memory for encoding by 1Mi bytes
323 		 * (1 << 20).*/
324 		if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 &&
325 		    (dicsize & ((1 << 20)-1)) == 0 &&
326 		    bits_checked == 8 + 64) {
327 			bits_checked += 32;
328 			break;
329 		}
330 		/* Otherwise dictionary size is unlikely. But it is
331 		 * possible that someone makes lzma stream with
332 		 * liblzma/LZMA SDK in one's dictionary size. */
333 		return (0);
334 	}
335 
336 	/* TODO: The above test is still very weak.  It would be
337 	 * good to do better. */
338 
339 	return (bits_checked);
340 }
341 
342 static int
lzip_has_member(struct archive_read_filter * filter)343 lzip_has_member(struct archive_read_filter *filter)
344 {
345 	const unsigned char *buffer;
346 	ssize_t avail;
347 	int bits_checked;
348 	int log2dic;
349 
350 	buffer = __archive_read_filter_ahead(filter, 6, &avail);
351 	if (buffer == NULL)
352 		return (0);
353 
354 	/*
355 	 * Verify Header Magic Bytes : 4C 5A 49 50 (`LZIP')
356 	 */
357 	bits_checked = 0;
358 	if (memcmp(buffer, "LZIP", 4) != 0)
359 		return (0);
360 	bits_checked += 32;
361 
362 	/* A version number must be 0 or 1 */
363 	if (buffer[4] != 0 && buffer[4] != 1)
364 		return (0);
365 	bits_checked += 8;
366 
367 	/* Dictionary size. */
368 	log2dic = buffer[5] & 0x1f;
369 	if (log2dic < 12 || log2dic > 29)
370 		return (0);
371 	bits_checked += 8;
372 
373 	return (bits_checked);
374 }
375 
376 static int
lzip_bidder_bid(struct archive_read_filter_bidder * self,struct archive_read_filter * filter)377 lzip_bidder_bid(struct archive_read_filter_bidder *self,
378     struct archive_read_filter *filter)
379 {
380 
381 	(void)self; /* UNUSED */
382 	return (lzip_has_member(filter));
383 }
384 
385 #if HAVE_LZMA_H && HAVE_LIBLZMA
386 
387 /*
388  * liblzma 4.999.7 and later support both lzma and xz streams.
389  */
390 static int
xz_bidder_init(struct archive_read_filter * self)391 xz_bidder_init(struct archive_read_filter *self)
392 {
393 	self->code = ARCHIVE_FILTER_XZ;
394 	self->name = "xz";
395 	return (xz_lzma_bidder_init(self));
396 }
397 
398 static int
lzma_bidder_init(struct archive_read_filter * self)399 lzma_bidder_init(struct archive_read_filter *self)
400 {
401 	self->code = ARCHIVE_FILTER_LZMA;
402 	self->name = "lzma";
403 	return (xz_lzma_bidder_init(self));
404 }
405 
406 static int
lzip_bidder_init(struct archive_read_filter * self)407 lzip_bidder_init(struct archive_read_filter *self)
408 {
409 	self->code = ARCHIVE_FILTER_LZIP;
410 	self->name = "lzip";
411 	return (xz_lzma_bidder_init(self));
412 }
413 
414 /*
415  * Set an error code and choose an error message
416  */
417 static void
set_error(struct archive_read_filter * self,int ret)418 set_error(struct archive_read_filter *self, int ret)
419 {
420 
421 	switch (ret) {
422 	case LZMA_STREAM_END: /* Found end of stream. */
423 	case LZMA_OK: /* Decompressor made some progress. */
424 		break;
425 	case LZMA_MEM_ERROR:
426 		archive_set_error(&self->archive->archive, ENOMEM,
427 		    "Lzma library error: Cannot allocate memory");
428 		break;
429 	case LZMA_MEMLIMIT_ERROR:
430 		archive_set_error(&self->archive->archive, ENOMEM,
431 		    "Lzma library error: Out of memory");
432 		break;
433 	case LZMA_FORMAT_ERROR:
434 		archive_set_error(&self->archive->archive,
435 		    ARCHIVE_ERRNO_MISC,
436 		    "Lzma library error: format not recognized");
437 		break;
438 	case LZMA_OPTIONS_ERROR:
439 		archive_set_error(&self->archive->archive,
440 		    ARCHIVE_ERRNO_MISC,
441 		    "Lzma library error: Invalid options");
442 		break;
443 	case LZMA_DATA_ERROR:
444 		archive_set_error(&self->archive->archive,
445 		    ARCHIVE_ERRNO_MISC,
446 		    "Lzma library error: Corrupted input data");
447 		break;
448 	case LZMA_BUF_ERROR:
449 		archive_set_error(&self->archive->archive,
450 		    ARCHIVE_ERRNO_MISC,
451 		    "Lzma library error:  No progress is possible");
452 		break;
453 	default:
454 		/* Return an error. */
455 		archive_set_error(&self->archive->archive,
456 		    ARCHIVE_ERRNO_MISC,
457 		    "Lzma decompression failed:  Unknown error");
458 		break;
459 	}
460 }
461 
462 static const struct archive_read_filter_vtable
463 xz_lzma_reader_vtable = {
464 	.read = xz_filter_read,
465 	.close = xz_filter_close,
466 };
467 
468 /*
469  * Setup the callbacks.
470  */
471 static int
xz_lzma_bidder_init(struct archive_read_filter * self)472 xz_lzma_bidder_init(struct archive_read_filter *self)
473 {
474 	static const size_t out_block_size = 64 * 1024;
475 	void *out_block;
476 	struct private_data *state;
477 	int ret;
478 
479 	state = (struct private_data *)calloc(1, sizeof(*state));
480 	out_block = (unsigned char *)malloc(out_block_size);
481 	if (state == NULL || out_block == NULL) {
482 		archive_set_error(&self->archive->archive, ENOMEM,
483 		    "Can't allocate data for xz decompression");
484 		free(out_block);
485 		free(state);
486 		return (ARCHIVE_FATAL);
487 	}
488 
489 	self->data = state;
490 	state->out_block_size = out_block_size;
491 	state->out_block = out_block;
492 	self->vtable = &xz_lzma_reader_vtable;
493 
494 	state->stream.avail_in = 0;
495 
496 	state->stream.next_out = state->out_block;
497 	state->stream.avail_out = state->out_block_size;
498 
499 	state->crc32 = 0;
500 	if (self->code == ARCHIVE_FILTER_LZIP) {
501 		/*
502 		 * We have to read a lzip header and use it to initialize
503 		 * compression library, thus we cannot initialize the
504 		 * library for lzip here.
505 		 */
506 		state->in_stream = 0;
507 		return (ARCHIVE_OK);
508 	} else
509 		state->in_stream = 1;
510 
511 	/* Initialize compression library. */
512 	if (self->code == ARCHIVE_FILTER_XZ)
513 		ret = lzma_stream_decoder(&(state->stream),
514 		    LZMA_MEMLIMIT,/* memlimit */
515 		    LZMA_CONCATENATED);
516 	else
517 		ret = lzma_alone_decoder(&(state->stream),
518 		    LZMA_MEMLIMIT);/* memlimit */
519 
520 	if (ret == LZMA_OK)
521 		return (ARCHIVE_OK);
522 
523 	/* Library setup failed: Choose an error message and clean up. */
524 	set_error(self, ret);
525 
526 	free(state->out_block);
527 	free(state);
528 	self->data = NULL;
529 	return (ARCHIVE_FATAL);
530 }
531 
532 static int
lzip_init(struct archive_read_filter * self)533 lzip_init(struct archive_read_filter *self)
534 {
535 	struct private_data *state;
536 	const unsigned char *h;
537 	lzma_filter filters[2];
538 	unsigned char props[5];
539 	ssize_t avail_in;
540 	uint32_t dicsize;
541 	int log2dic, ret;
542 
543 	state = (struct private_data *)self->data;
544 	h = __archive_read_filter_ahead(self->upstream, 6, &avail_in);
545 	if (h == NULL)
546 		return (ARCHIVE_FATAL);
547 
548 	/* Get a version number. */
549 	state->lzip_ver = h[4];
550 
551 	/*
552 	 * Setup lzma property.
553 	 */
554 	props[0] = 0x5d;
555 
556 	/* Get dictionary size. */
557 	log2dic = h[5] & 0x1f;
558 	if (log2dic < 12 || log2dic > 29)
559 		return (ARCHIVE_FATAL);
560 	dicsize = 1U << log2dic;
561 	if (log2dic > 12)
562 		dicsize -= (dicsize / 16) * (h[5] >> 5);
563 	archive_le32enc(props+1, dicsize);
564 
565 	/* Consume lzip header. */
566 	__archive_read_filter_consume(self->upstream, 6);
567 	state->member_in = 6;
568 
569 	filters[0].id = LZMA_FILTER_LZMA1;
570 	filters[0].options = NULL;
571 	filters[1].id = LZMA_VLI_UNKNOWN;
572 	filters[1].options = NULL;
573 
574 	ret = lzma_properties_decode(&filters[0], NULL, props, sizeof(props));
575 	if (ret != LZMA_OK) {
576 		set_error(self, ret);
577 		return (ARCHIVE_FATAL);
578 	}
579 	ret = lzma_raw_decoder(&(state->stream), filters);
580 	free(filters[0].options);
581 	if (ret != LZMA_OK) {
582 		set_error(self, ret);
583 		return (ARCHIVE_FATAL);
584 	}
585 	return (ARCHIVE_OK);
586 }
587 
588 static int
lzip_tail(struct archive_read_filter * self)589 lzip_tail(struct archive_read_filter *self)
590 {
591 	struct private_data *state;
592 	const unsigned char *f;
593 	ssize_t avail_in;
594 	int tail;
595 
596 	state = (struct private_data *)self->data;
597 	if (state->lzip_ver == 0)
598 		tail = 12;
599 	else
600 		tail = 20;
601 	f = __archive_read_filter_ahead(self->upstream, tail, &avail_in);
602 	if (f == NULL && avail_in < 0)
603 		return (ARCHIVE_FATAL);
604 	if (f == NULL || avail_in < tail) {
605 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
606 		    "Lzip: Remaining data is less bytes");
607 		return (ARCHIVE_FAILED);
608 	}
609 
610 	/* Check the crc32 value of the uncompressed data of the current
611 	 * member */
612 	if (state->crc32 != archive_le32dec(f)) {
613 #ifndef DONT_FAIL_ON_CRC_ERROR
614 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
615 		    "Lzip: CRC32 error");
616 		return (ARCHIVE_FAILED);
617 #endif
618 	}
619 
620 	/* Check the uncompressed size of the current member */
621 	if ((uint64_t)state->member_out != archive_le64dec(f + 4)) {
622 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
623 		    "Lzip: Uncompressed size error");
624 		return (ARCHIVE_FAILED);
625 	}
626 
627 	/* Check the total size of the current member */
628 	if (state->lzip_ver == 1 &&
629 	    (uint64_t)state->member_in + tail != archive_le64dec(f + 12)) {
630 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
631 		    "Lzip: Member size error");
632 		return (ARCHIVE_FAILED);
633 	}
634 	__archive_read_filter_consume(self->upstream, tail);
635 
636 	/* If current lzip data consists of multi member, try decompressing
637 	 * a next member. */
638 	if (lzip_has_member(self->upstream) != 0) {
639 		state->in_stream = 0;
640 		state->crc32 = 0;
641 		state->member_out = 0;
642 		state->member_in = 0;
643 		state->eof = 0;
644 	}
645 	return (ARCHIVE_OK);
646 }
647 
648 /*
649  * Return the next block of decompressed data.
650  */
651 static ssize_t
xz_filter_read(struct archive_read_filter * self,const void ** p)652 xz_filter_read(struct archive_read_filter *self, const void **p)
653 {
654 	struct private_data *state;
655 	size_t decompressed;
656 	ssize_t avail_in;
657 	int64_t member_in;
658 	int ret;
659 
660 	state = (struct private_data *)self->data;
661 
662 	redo:
663 	/* Empty our output buffer. */
664 	state->stream.next_out = state->out_block;
665 	state->stream.avail_out = state->out_block_size;
666 	member_in = state->member_in;
667 
668 	/* Try to fill the output buffer. */
669 	while (state->stream.avail_out > 0 && !state->eof) {
670 		if (!state->in_stream) {
671 			/*
672 			 * Initialize liblzma for lzip
673 			 */
674 			ret = lzip_init(self);
675 			if (ret != ARCHIVE_OK)
676 				return (ret);
677 			state->in_stream = 1;
678 		}
679 		state->stream.next_in =
680 		    __archive_read_filter_ahead(self->upstream, 1, &avail_in);
681 		if (state->stream.next_in == NULL && avail_in < 0) {
682 			archive_set_error(&self->archive->archive,
683 			    ARCHIVE_ERRNO_MISC,
684 			    "truncated input");
685 			return (ARCHIVE_FATAL);
686 		}
687 		state->stream.avail_in = avail_in;
688 
689 		/* Decompress as much as we can in one pass. */
690 		ret = lzma_code(&(state->stream),
691 		    (state->stream.avail_in == 0)? LZMA_FINISH: LZMA_RUN);
692 		switch (ret) {
693 		case LZMA_STREAM_END: /* Found end of stream. */
694 			state->eof = 1;
695 			/* FALL THROUGH */
696 		case LZMA_OK: /* Decompressor made some progress. */
697 			__archive_read_filter_consume(self->upstream,
698 			    avail_in - state->stream.avail_in);
699 			state->member_in +=
700 			    avail_in - state->stream.avail_in;
701 			break;
702 		default:
703 			set_error(self, ret);
704 			return (ARCHIVE_FATAL);
705 		}
706 	}
707 
708 	decompressed = state->stream.next_out - state->out_block;
709 	state->total_out += decompressed;
710 	state->member_out += decompressed;
711 	if (decompressed == 0) {
712 		if (member_in != state->member_in &&
713 		    self->code == ARCHIVE_FILTER_LZIP &&
714 		    state->eof) {
715 			ret = lzip_tail(self);
716 			if (ret != ARCHIVE_OK)
717 				return (ret);
718 			if (!state->eof)
719 				goto redo;
720 		}
721 		*p = NULL;
722 	} else {
723 		*p = state->out_block;
724 		if (self->code == ARCHIVE_FILTER_LZIP) {
725 			state->crc32 = lzma_crc32(state->out_block,
726 			    decompressed, state->crc32);
727 			if (state->eof) {
728 				ret = lzip_tail(self);
729 				if (ret != ARCHIVE_OK)
730 					return (ret);
731 			}
732 		}
733 	}
734 	return (decompressed);
735 }
736 
737 /*
738  * Clean up the decompressor.
739  */
740 static int
xz_filter_close(struct archive_read_filter * self)741 xz_filter_close(struct archive_read_filter *self)
742 {
743 	struct private_data *state;
744 
745 	state = (struct private_data *)self->data;
746 	lzma_end(&(state->stream));
747 	free(state->out_block);
748 	free(state);
749 	return (ARCHIVE_OK);
750 }
751 
752 #else
753 
754 /*
755  *
756  * If we have no suitable library on this system, we can't actually do
757  * the decompression.  We can, however, still detect compressed
758  * archives and emit a useful message.
759  *
760  */
761 static int
lzma_bidder_init(struct archive_read_filter * self)762 lzma_bidder_init(struct archive_read_filter *self)
763 {
764 	int r;
765 
766 	r = __archive_read_program(self, "lzma -d -qq");
767 	/* Note: We set the format here even if __archive_read_program()
768 	 * above fails.  We do, after all, know what the format is
769 	 * even if we weren't able to read it. */
770 	self->code = ARCHIVE_FILTER_LZMA;
771 	self->name = "lzma";
772 	return (r);
773 }
774 
775 static int
xz_bidder_init(struct archive_read_filter * self)776 xz_bidder_init(struct archive_read_filter *self)
777 {
778 	int r;
779 
780 	r = __archive_read_program(self, "xz -d -qq");
781 	/* Note: We set the format here even if __archive_read_program()
782 	 * above fails.  We do, after all, know what the format is
783 	 * even if we weren't able to read it. */
784 	self->code = ARCHIVE_FILTER_XZ;
785 	self->name = "xz";
786 	return (r);
787 }
788 
789 static int
lzip_bidder_init(struct archive_read_filter * self)790 lzip_bidder_init(struct archive_read_filter *self)
791 {
792 	int r;
793 
794 	r = __archive_read_program(self, "lzip -d -q");
795 	/* Note: We set the format here even if __archive_read_program()
796 	 * above fails.  We do, after all, know what the format is
797 	 * even if we weren't able to read it. */
798 	self->code = ARCHIVE_FILTER_LZIP;
799 	self->name = "lzip";
800 	return (r);
801 }
802 
803 #endif /* HAVE_LZMA_H */
804