Blame libarchive/archive_read_support_filter_xz.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2009-2011 Michihiro NAKAJIMA
Packit Service 1d0348
 * Copyright (c) 2003-2008 Tim Kientzle and Miklos Vajna
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
Packit Service 1d0348
__FBSDID("$FreeBSD$");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#include <stdio.h>
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_UNISTD_H
Packit Service 1d0348
#include <unistd.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#if HAVE_LZMA_H
Packit Service 1d0348
#include <lzma.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_endian.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_read_private.h"
Packit Service 1d0348
Packit Service 1d0348
#if HAVE_LZMA_H && HAVE_LIBLZMA
Packit Service 1d0348
Packit Service 1d0348
struct private_data {
Packit Service 1d0348
	lzma_stream	 stream;
Packit Service 1d0348
	unsigned char	*out_block;
Packit Service 1d0348
	size_t		 out_block_size;
Packit Service 1d0348
	int64_t		 total_out;
Packit Service 1d0348
	char		 eof; /* True = found end of compressed data. */
Packit Service 1d0348
	char		 in_stream;
Packit Service 1d0348
Packit Service 1d0348
	/* Following variables are used for lzip only. */
Packit Service 1d0348
	char		 lzip_ver;
Packit Service 1d0348
	uint32_t	 crc32;
Packit Service 1d0348
	int64_t		 member_in;
Packit Service 1d0348
	int64_t		 member_out;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
#if LZMA_VERSION_MAJOR >= 5
Packit Service 1d0348
/* Effectively disable the limiter. */
Packit Service 1d0348
#define LZMA_MEMLIMIT	UINT64_MAX
Packit Service 1d0348
#else
Packit Service 1d0348
/* NOTE: This needs to check memory size which running system has. */
Packit Service 1d0348
#define LZMA_MEMLIMIT	(1U << 30)
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
/* Combined lzip/lzma/xz filter */
Packit Service 1d0348
static ssize_t	xz_filter_read(struct archive_read_filter *, const void **);
Packit Service 1d0348
static int	xz_filter_close(struct archive_read_filter *);
Packit Service 1d0348
static int	xz_lzma_bidder_init(struct archive_read_filter *);
Packit Service 1d0348
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Note that we can detect xz and lzma compressed files even if we
Packit Service 1d0348
 * can't decompress them.  (In fact, we like detecting them because we
Packit Service 1d0348
 * can give better error messages.)  So the bid framework here gets
Packit Service 1d0348
 * compiled even if no lzma library is available.
Packit Service 1d0348
 */
Packit Service 1d0348
static int	xz_bidder_bid(struct archive_read_filter_bidder *,
Packit Service 1d0348
		    struct archive_read_filter *);
Packit Service 1d0348
static int	xz_bidder_init(struct archive_read_filter *);
Packit Service 1d0348
static int	lzma_bidder_bid(struct archive_read_filter_bidder *,
Packit Service 1d0348
		    struct archive_read_filter *);
Packit Service 1d0348
static int	lzma_bidder_init(struct archive_read_filter *);
Packit Service 1d0348
static int	lzip_has_member(struct archive_read_filter *);
Packit Service 1d0348
static int	lzip_bidder_bid(struct archive_read_filter_bidder *,
Packit Service 1d0348
		    struct archive_read_filter *);
Packit Service 1d0348
static int	lzip_bidder_init(struct archive_read_filter *);
Packit Service 1d0348
Packit Service 1d0348
#if ARCHIVE_VERSION_NUMBER < 4000000
Packit Service 1d0348
/* Deprecated; remove in libarchive 4.0 */
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_compression_xz(struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	return archive_read_support_filter_xz(a);
Packit Service 1d0348
}
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_filter_xz(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	struct archive_read_filter_bidder *bidder;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_read_support_filter_xz");
Packit Service 1d0348
Packit Service 1d0348
	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
	bidder->data = NULL;
Packit Service 1d0348
	bidder->name = "xz";
Packit Service 1d0348
	bidder->bid = xz_bidder_bid;
Packit Service 1d0348
	bidder->init = xz_bidder_init;
Packit Service 1d0348
	bidder->options = NULL;
Packit Service 1d0348
	bidder->free = NULL;
Packit Service 1d0348
#if HAVE_LZMA_H && HAVE_LIBLZMA
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
#else
Packit Service 1d0348
	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
	    "Using external xz program for xz decompression");
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
#endif
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#if ARCHIVE_VERSION_NUMBER < 4000000
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_compression_lzma(struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	return archive_read_support_filter_lzma(a);
Packit Service 1d0348
}
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_filter_lzma(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	struct archive_read_filter_bidder *bidder;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_read_support_filter_lzma");
Packit Service 1d0348
Packit Service 1d0348
	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
	bidder->data = NULL;
Packit Service 1d0348
	bidder->name = "lzma";
Packit Service 1d0348
	bidder->bid = lzma_bidder_bid;
Packit Service 1d0348
	bidder->init = lzma_bidder_init;
Packit Service 1d0348
	bidder->options = NULL;
Packit Service 1d0348
	bidder->free = NULL;
Packit Service 1d0348
#if HAVE_LZMA_H && HAVE_LIBLZMA
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
#else
Packit Service 1d0348
	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
	    "Using external lzma program for lzma decompression");
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
#endif
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
#if ARCHIVE_VERSION_NUMBER < 4000000
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_compression_lzip(struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	return archive_read_support_filter_lzip(a);
Packit Service 1d0348
}
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_filter_lzip(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	struct archive_read_filter_bidder *bidder;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_read_support_filter_lzip");
Packit Service 1d0348
Packit Service 1d0348
	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
	bidder->data = NULL;
Packit Service 1d0348
	bidder->name = "lzip";
Packit Service 1d0348
	bidder->bid = lzip_bidder_bid;
Packit Service 1d0348
	bidder->init = lzip_bidder_init;
Packit Service 1d0348
	bidder->options = NULL;
Packit Service 1d0348
	bidder->free = NULL;
Packit Service 1d0348
#if HAVE_LZMA_H && HAVE_LIBLZMA
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
#else
Packit Service 1d0348
	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
	    "Using external lzip program for lzip decompression");
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
#endif
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Test whether we can handle this data.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
xz_bidder_bid(struct archive_read_filter_bidder *self,
Packit Service 1d0348
    struct archive_read_filter *filter)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned char *buffer;
Packit Service 1d0348
	ssize_t avail;
Packit Service 1d0348
Packit Service 1d0348
	(void)self; /* UNUSED */
Packit Service 1d0348
Packit Service 1d0348
	buffer = __archive_read_filter_ahead(filter, 6, &avail);
Packit Service 1d0348
	if (buffer == NULL)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Verify Header Magic Bytes : FD 37 7A 58 5A 00
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (memcmp(buffer, "\xFD\x37\x7A\x58\x5A\x00", 6) != 0)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	return (48);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Test whether we can handle this data.
Packit Service 1d0348
 *
Packit Service 1d0348
 * <sigh> LZMA has a rather poor file signature.  Zeros do not
Packit Service 1d0348
 * make good signature bytes as a rule, and the only non-zero byte
Packit Service 1d0348
 * here is an ASCII character.  For example, an uncompressed tar
Packit Service 1d0348
 * archive whose first file is ']' would satisfy this check.  It may
Packit Service 1d0348
 * be necessary to exclude LZMA from compression_all() because of
Packit Service 1d0348
 * this.  Clients of libarchive would then have to explicitly enable
Packit Service 1d0348
 * LZMA checking instead of (or in addition to) compression_all() when
Packit Service 1d0348
 * they have other evidence (file name, command-line option) to go on.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
lzma_bidder_bid(struct archive_read_filter_bidder *self,
Packit Service 1d0348
    struct archive_read_filter *filter)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned char *buffer;
Packit Service 1d0348
	ssize_t avail;
Packit Service 1d0348
	uint32_t dicsize;
Packit Service 1d0348
	uint64_t uncompressed_size;
Packit Service 1d0348
	int bits_checked;
Packit Service 1d0348
Packit Service 1d0348
	(void)self; /* UNUSED */
Packit Service 1d0348
Packit Service 1d0348
	buffer = __archive_read_filter_ahead(filter, 14, &avail);
Packit Service 1d0348
	if (buffer == NULL)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	/* First byte of raw LZMA stream is commonly 0x5d.
Packit Service 1d0348
	 * The first byte is a special number, which consists of
Packit Service 1d0348
	 * three parameters of LZMA compression, a number of literal
Packit Service 1d0348
	 * context bits(which is from 0 to 8, default is 3), a number
Packit Service 1d0348
	 * of literal pos bits(which is from 0 to 4, default is 0),
Packit Service 1d0348
	 * a number of pos bits(which is from 0 to 4, default is 2).
Packit Service 1d0348
	 * The first byte is made by
Packit Service 1d0348
	 * (pos bits * 5 + literal pos bit) * 9 + * literal contest bit,
Packit Service 1d0348
	 * and so the default value in this field is
Packit Service 1d0348
	 * (2 * 5 + 0) * 9 + 3 = 0x5d.
Packit Service 1d0348
	 * lzma of LZMA SDK has options to change those parameters.
Packit Service 1d0348
	 * It means a range of this field is from 0 to 224. And lzma of
Packit Service 1d0348
	 * XZ Utils with option -e records 0x5e in this field. */
Packit Service 1d0348
	/* NOTE: If this checking of the first byte increases false
Packit Service 1d0348
	 * recognition, we should allow only 0x5d and 0x5e for the first
Packit Service 1d0348
	 * byte of LZMA stream. */
Packit Service 1d0348
	bits_checked = 0;
Packit Service 1d0348
	if (buffer[0] > (4 * 5 + 4) * 9 + 8)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	/* Most likely value in the first byte of LZMA stream. */
Packit Service 1d0348
	if (buffer[0] == 0x5d || buffer[0] == 0x5e)
Packit Service 1d0348
		bits_checked += 8;
Packit Service 1d0348
Packit Service 1d0348
	/* Sixth through fourteenth bytes are uncompressed size,
Packit Service 1d0348
	 * stored in little-endian order. `-1' means uncompressed
Packit Service 1d0348
	 * size is unknown and lzma of XZ Utils always records `-1'
Packit Service 1d0348
	 * in this field. */
Packit Service 1d0348
	uncompressed_size = archive_le64dec(buffer+5);
Packit Service 1d0348
	if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1))
Packit Service 1d0348
		bits_checked += 64;
Packit Service 1d0348
Packit Service 1d0348
	/* Second through fifth bytes are dictionary size, stored in
Packit Service 1d0348
	 * little-endian order. The minimum dictionary size is
Packit Service 1d0348
	 * 1 << 12(4KiB) which the lzma of LZMA SDK uses with option
Packit Service 1d0348
	 * -d12 and the maximum dictionary size is 1 << 27(128MiB)
Packit Service 1d0348
	 * which the one uses with option -d27.
Packit Service 1d0348
	 * NOTE: A comment of LZMA SDK source code says this dictionary
Packit Service 1d0348
	 * range is from 1 << 12 to 1 << 30. */
Packit Service 1d0348
	dicsize = archive_le32dec(buffer+1);
Packit Service 1d0348
	switch (dicsize) {
Packit Service 1d0348
	case 0x00001000:/* lzma of LZMA SDK option -d12. */
Packit Service 1d0348
	case 0x00002000:/* lzma of LZMA SDK option -d13. */
Packit Service 1d0348
	case 0x00004000:/* lzma of LZMA SDK option -d14. */
Packit Service 1d0348
	case 0x00008000:/* lzma of LZMA SDK option -d15. */
Packit Service 1d0348
	case 0x00010000:/* lzma of XZ Utils option -0 and -1.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d16. */
Packit Service 1d0348
	case 0x00020000:/* lzma of LZMA SDK option -d17. */
Packit Service 1d0348
	case 0x00040000:/* lzma of LZMA SDK option -d18. */
Packit Service 1d0348
	case 0x00080000:/* lzma of XZ Utils option -2.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d19. */
Packit Service 1d0348
	case 0x00100000:/* lzma of XZ Utils option -3.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d20. */
Packit Service 1d0348
	case 0x00200000:/* lzma of XZ Utils option -4.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d21. */
Packit Service 1d0348
	case 0x00400000:/* lzma of XZ Utils option -5.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d22. */
Packit Service 1d0348
	case 0x00800000:/* lzma of XZ Utils option -6.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d23. */
Packit Service 1d0348
	case 0x01000000:/* lzma of XZ Utils option -7.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d24. */
Packit Service 1d0348
	case 0x02000000:/* lzma of XZ Utils option -8.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d25. */
Packit Service 1d0348
	case 0x04000000:/* lzma of XZ Utils option -9.
Packit Service 1d0348
			 * lzma of LZMA SDK option -d26. */
Packit Service 1d0348
	case 0x08000000:/* lzma of LZMA SDK option -d27. */
Packit Service 1d0348
		bits_checked += 32;
Packit Service 1d0348
		break;
Packit Service 1d0348
	default:
Packit Service 1d0348
		/* If a memory usage for encoding was not enough on
Packit Service 1d0348
		 * the platform where LZMA stream was made, lzma of
Packit Service 1d0348
		 * XZ Utils automatically decreased the dictionary
Packit Service 1d0348
		 * size to enough memory for encoding by 1Mi bytes
Packit Service 1d0348
		 * (1 << 20).*/
Packit Service 1d0348
		if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 &&
Packit Service 1d0348
		    (dicsize & ((1 << 20)-1)) == 0 &&
Packit Service 1d0348
		    bits_checked == 8 + 64) {
Packit Service 1d0348
			bits_checked += 32;
Packit Service 1d0348
			break;
Packit Service 1d0348
		}
Packit Service 1d0348
		/* Otherwise dictionary size is unlikely. But it is
Packit Service 1d0348
		 * possible that someone makes lzma stream with
Packit Service 1d0348
		 * liblzma/LZMA SDK in one's dictionary size. */
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* TODO: The above test is still very weak.  It would be
Packit Service 1d0348
	 * good to do better. */
Packit Service 1d0348
Packit Service 1d0348
	return (bits_checked);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzip_has_member(struct archive_read_filter *filter)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned char *buffer;
Packit Service 1d0348
	ssize_t avail;
Packit Service 1d0348
	int bits_checked;
Packit Service 1d0348
	int log2dic;
Packit Service 1d0348
Packit Service 1d0348
	buffer = __archive_read_filter_ahead(filter, 6, &avail);
Packit Service 1d0348
	if (buffer == NULL)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Verify Header Magic Bytes : 4C 5A 49 50 (`LZIP')
Packit Service 1d0348
	 */
Packit Service 1d0348
	bits_checked = 0;
Packit Service 1d0348
	if (memcmp(buffer, "LZIP", 4) != 0)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	bits_checked += 32;
Packit Service 1d0348
Packit Service 1d0348
	/* A version number must be 0 or 1 */
Packit Service 1d0348
	if (buffer[4] != 0 && buffer[4] != 1)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	bits_checked += 8;
Packit Service 1d0348
Packit Service 1d0348
	/* Dictionary size. */
Packit Service 1d0348
	log2dic = buffer[5] & 0x1f;
Packit Service 1d0348
	if (log2dic < 12 || log2dic > 27)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	bits_checked += 8;
Packit Service 1d0348
Packit Service 1d0348
	return (bits_checked);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzip_bidder_bid(struct archive_read_filter_bidder *self,
Packit Service 1d0348
    struct archive_read_filter *filter)
Packit Service 1d0348
{
Packit Service 1d0348
Packit Service 1d0348
	(void)self; /* UNUSED */
Packit Service 1d0348
	return (lzip_has_member(filter));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#if HAVE_LZMA_H && HAVE_LIBLZMA
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * liblzma 4.999.7 and later support both lzma and xz streams.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
xz_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_XZ;
Packit Service 1d0348
	self->name = "xz";
Packit Service 1d0348
	return (xz_lzma_bidder_init(self));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzma_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_LZMA;
Packit Service 1d0348
	self->name = "lzma";
Packit Service 1d0348
	return (xz_lzma_bidder_init(self));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzip_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_LZIP;
Packit Service 1d0348
	self->name = "lzip";
Packit Service 1d0348
	return (xz_lzma_bidder_init(self));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Set an error code and choose an error message
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
set_error(struct archive_read_filter *self, int ret)
Packit Service 1d0348
{
Packit Service 1d0348
Packit Service 1d0348
	switch (ret) {
Packit Service 1d0348
	case LZMA_STREAM_END: /* Found end of stream. */
Packit Service 1d0348
	case LZMA_OK: /* Decompressor made some progress. */
Packit Service 1d0348
		break;
Packit Service 1d0348
	case LZMA_MEM_ERROR:
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ENOMEM,
Packit Service 1d0348
		    "Lzma library error: Cannot allocate memory");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case LZMA_MEMLIMIT_ERROR:
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ENOMEM,
Packit Service 1d0348
		    "Lzma library error: Out of memory");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case LZMA_FORMAT_ERROR:
Packit Service 1d0348
		archive_set_error(&self->archive->archive,
Packit Service 1d0348
		    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzma library error: format not recognized");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case LZMA_OPTIONS_ERROR:
Packit Service 1d0348
		archive_set_error(&self->archive->archive,
Packit Service 1d0348
		    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzma library error: Invalid options");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case LZMA_DATA_ERROR:
Packit Service 1d0348
		archive_set_error(&self->archive->archive,
Packit Service 1d0348
		    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzma library error: Corrupted input data");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case LZMA_BUF_ERROR:
Packit Service 1d0348
		archive_set_error(&self->archive->archive,
Packit Service 1d0348
		    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzma library error:  No progress is possible");
Packit Service 1d0348
		break;
Packit Service 1d0348
	default:
Packit Service 1d0348
		/* Return an error. */
Packit Service 1d0348
		archive_set_error(&self->archive->archive,
Packit Service 1d0348
		    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzma decompression failed:  Unknown error");
Packit Service 1d0348
		break;
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Setup the callbacks.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
xz_lzma_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	static const size_t out_block_size = 64 * 1024;
Packit Service 1d0348
	void *out_block;
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)calloc(sizeof(*state), 1);
Packit Service 1d0348
	out_block = (unsigned char *)malloc(out_block_size);
Packit Service 1d0348
	if (state == NULL || out_block == NULL) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate data for xz decompression");
Packit Service 1d0348
		free(out_block);
Packit Service 1d0348
		free(state);
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	self->data = state;
Packit Service 1d0348
	state->out_block_size = out_block_size;
Packit Service 1d0348
	state->out_block = out_block;
Packit Service 1d0348
	self->read = xz_filter_read;
Packit Service 1d0348
	self->skip = NULL; /* not supported */
Packit Service 1d0348
	self->close = xz_filter_close;
Packit Service 1d0348
Packit Service 1d0348
	state->stream.avail_in = 0;
Packit Service 1d0348
Packit Service 1d0348
	state->stream.next_out = state->out_block;
Packit Service 1d0348
	state->stream.avail_out = state->out_block_size;
Packit Service 1d0348
Packit Service 1d0348
	state->crc32 = 0;
Packit Service 1d0348
	if (self->code == ARCHIVE_FILTER_LZIP) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * We have to read a lzip header and use it to initialize
Packit Service 1d0348
		 * compression library, thus we cannot initialize the
Packit Service 1d0348
		 * library for lzip here.
Packit Service 1d0348
		 */
Packit Service 1d0348
		state->in_stream = 0;
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
	} else
Packit Service 1d0348
		state->in_stream = 1;
Packit Service 1d0348
Packit Service 1d0348
	/* Initialize compression library. */
Packit Service 1d0348
	if (self->code == ARCHIVE_FILTER_XZ)
Packit Service 1d0348
		ret = lzma_stream_decoder(&(state->stream),
Packit Service 1d0348
		    LZMA_MEMLIMIT,/* memlimit */
Packit Service 1d0348
		    LZMA_CONCATENATED);
Packit Service 1d0348
	else
Packit Service 1d0348
		ret = lzma_alone_decoder(&(state->stream),
Packit Service 1d0348
		    LZMA_MEMLIMIT);/* memlimit */
Packit Service 1d0348
Packit Service 1d0348
	if (ret == LZMA_OK)
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
Packit Service 1d0348
	/* Library setup failed: Choose an error message and clean up. */
Packit Service 1d0348
	set_error(self, ret);
Packit Service 1d0348
Packit Service 1d0348
	free(state->out_block);
Packit Service 1d0348
	free(state);
Packit Service 1d0348
	self->data = NULL;
Packit Service 1d0348
	return (ARCHIVE_FATAL);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzip_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
	const unsigned char *h;
Packit Service 1d0348
	lzma_filter filters[2];
Packit Service 1d0348
	unsigned char props[5];
Packit Service 1d0348
	ssize_t avail_in;
Packit Service 1d0348
	uint32_t dicsize;
Packit Service 1d0348
	int log2dic, ret;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)self->data;
Packit Service 1d0348
	h = __archive_read_filter_ahead(self->upstream, 6, &avail_in);
Packit Service 1d0348
	if (h == NULL)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
	/* Get a version number. */
Packit Service 1d0348
	state->lzip_ver = h[4];
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Setup lzma property.
Packit Service 1d0348
	 */
Packit Service 1d0348
	props[0] = 0x5d;
Packit Service 1d0348
Packit Service 1d0348
	/* Get dictionary size. */
Packit Service 1d0348
	log2dic = h[5] & 0x1f;
Packit Service 1d0348
	if (log2dic < 12 || log2dic > 27)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	dicsize = 1U << log2dic;
Packit Service 1d0348
	if (log2dic > 12)
Packit Service 1d0348
		dicsize -= (dicsize / 16) * (h[5] >> 5);
Packit Service 1d0348
	archive_le32enc(props+1, dicsize);
Packit Service 1d0348
Packit Service 1d0348
	/* Consume lzip header. */
Packit Service 1d0348
	__archive_read_filter_consume(self->upstream, 6);
Packit Service 1d0348
	state->member_in = 6;
Packit Service 1d0348
Packit Service 1d0348
	filters[0].id = LZMA_FILTER_LZMA1;
Packit Service 1d0348
	filters[0].options = NULL;
Packit Service 1d0348
	filters[1].id = LZMA_VLI_UNKNOWN;
Packit Service 1d0348
	filters[1].options = NULL;
Packit Service 1d0348
Packit Service 1d0348
	ret = lzma_properties_decode(&filters[0], NULL, props, sizeof(props));
Packit Service 1d0348
	if (ret != LZMA_OK) {
Packit Service 1d0348
		set_error(self, ret);
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	ret = lzma_raw_decoder(&(state->stream), filters);
Packit Service 1d0348
	free(filters[0].options);
Packit Service 1d0348
	if (ret != LZMA_OK) {
Packit Service 1d0348
		set_error(self, ret);
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzip_tail(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
	const unsigned char *f;
Packit Service 1d0348
	ssize_t avail_in;
Packit Service 1d0348
	int tail;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)self->data;
Packit Service 1d0348
	if (state->lzip_ver == 0)
Packit Service 1d0348
		tail = 12;
Packit Service 1d0348
	else
Packit Service 1d0348
		tail = 20;
Packit Service 1d0348
	f = __archive_read_filter_ahead(self->upstream, tail, &avail_in);
Packit Service 1d0348
	if (f == NULL && avail_in < 0)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	if (f == NULL || avail_in < tail) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzip: Remaining data is less bytes");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Check the crc32 value of the uncompressed data of the current
Packit Service 1d0348
	 * member */
Packit Service 1d0348
	if (state->crc32 != archive_le32dec(f)) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzip: CRC32 error");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Check the uncompressed size of the current member */
Packit Service 1d0348
	if ((uint64_t)state->member_out != archive_le64dec(f + 4)) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzip: Uncompressed size error");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Check the total size of the current member */
Packit Service 1d0348
	if (state->lzip_ver == 1 &&
Packit Service 1d0348
	    (uint64_t)state->member_in + tail != archive_le64dec(f + 12)) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Lzip: Member size error");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
	__archive_read_filter_consume(self->upstream, tail);
Packit Service 1d0348
Packit Service 1d0348
	/* If current lzip data consists of multi member, try decompressing
Packit Service 1d0348
	 * a next member. */
Packit Service 1d0348
	if (lzip_has_member(self->upstream) != 0) {
Packit Service 1d0348
		state->in_stream = 0;
Packit Service 1d0348
		state->crc32 = 0;
Packit Service 1d0348
		state->member_out = 0;
Packit Service 1d0348
		state->member_in = 0;
Packit Service 1d0348
		state->eof = 0;
Packit Service 1d0348
	}
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Return the next block of decompressed data.
Packit Service 1d0348
 */
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
xz_filter_read(struct archive_read_filter *self, const void **p)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
	size_t decompressed;
Packit Service 1d0348
	ssize_t avail_in;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)self->data;
Packit Service 1d0348
Packit Service 1d0348
	/* Empty our output buffer. */
Packit Service 1d0348
	state->stream.next_out = state->out_block;
Packit Service 1d0348
	state->stream.avail_out = state->out_block_size;
Packit Service 1d0348
Packit Service 1d0348
	/* Try to fill the output buffer. */
Packit Service 1d0348
	while (state->stream.avail_out > 0 && !state->eof) {
Packit Service 1d0348
		if (!state->in_stream) {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * Initialize liblzma for lzip
Packit Service 1d0348
			 */
Packit Service 1d0348
			ret = lzip_init(self);
Packit Service 1d0348
			if (ret != ARCHIVE_OK)
Packit Service 1d0348
				return (ret);
Packit Service 1d0348
			state->in_stream = 1;
Packit Service 1d0348
		}
Packit Service 1d0348
		state->stream.next_in =
Packit Service 1d0348
		    __archive_read_filter_ahead(self->upstream, 1, &avail_in);
Packit Service 1d0348
		if (state->stream.next_in == NULL && avail_in < 0) {
Packit Service 1d0348
			archive_set_error(&self->archive->archive,
Packit Service 1d0348
			    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "truncated input");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		state->stream.avail_in = avail_in;
Packit Service 1d0348
Packit Service 1d0348
		/* Decompress as much as we can in one pass. */
Packit Service 1d0348
		ret = lzma_code(&(state->stream),
Packit Service 1d0348
		    (state->stream.avail_in == 0)? LZMA_FINISH: LZMA_RUN);
Packit Service 1d0348
		switch (ret) {
Packit Service 1d0348
		case LZMA_STREAM_END: /* Found end of stream. */
Packit Service 1d0348
			state->eof = 1;
Packit Service 1d0348
			/* FALL THROUGH */
Packit Service 1d0348
		case LZMA_OK: /* Decompressor made some progress. */
Packit Service 1d0348
			__archive_read_filter_consume(self->upstream,
Packit Service 1d0348
			    avail_in - state->stream.avail_in);
Packit Service 1d0348
			state->member_in +=
Packit Service 1d0348
			    avail_in - state->stream.avail_in;
Packit Service 1d0348
			break;
Packit Service 1d0348
		default:
Packit Service 1d0348
			set_error(self, ret);
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	decompressed = state->stream.next_out - state->out_block;
Packit Service 1d0348
	state->total_out += decompressed;
Packit Service 1d0348
	state->member_out += decompressed;
Packit Service 1d0348
	if (decompressed == 0)
Packit Service 1d0348
		*p = NULL;
Packit Service 1d0348
	else {
Packit Service 1d0348
		*p = state->out_block;
Packit Service 1d0348
		if (self->code == ARCHIVE_FILTER_LZIP) {
Packit Service 1d0348
			state->crc32 = lzma_crc32(state->out_block,
Packit Service 1d0348
			    decompressed, state->crc32);
Packit Service 1d0348
			if (state->eof) {
Packit Service 1d0348
				ret = lzip_tail(self);
Packit Service 1d0348
				if (ret != ARCHIVE_OK)
Packit Service 1d0348
					return (ret);
Packit Service 1d0348
			}
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
	return (decompressed);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Clean up the decompressor.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
xz_filter_close(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)self->data;
Packit Service 1d0348
	lzma_end(&(state->stream));
Packit Service 1d0348
	free(state->out_block);
Packit Service 1d0348
	free(state);
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#else
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 *
Packit Service 1d0348
 * If we have no suitable library on this system, we can't actually do
Packit Service 1d0348
 * the decompression.  We can, however, still detect compressed
Packit Service 1d0348
 * archives and emit a useful message.
Packit Service 1d0348
 *
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
lzma_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	r = __archive_read_program(self, "lzma -d -qq");
Packit Service 1d0348
	/* Note: We set the format here even if __archive_read_program()
Packit Service 1d0348
	 * above fails.  We do, after all, know what the format is
Packit Service 1d0348
	 * even if we weren't able to read it. */
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_LZMA;
Packit Service 1d0348
	self->name = "lzma";
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
xz_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	r = __archive_read_program(self, "xz -d -qq");
Packit Service 1d0348
	/* Note: We set the format here even if __archive_read_program()
Packit Service 1d0348
	 * above fails.  We do, after all, know what the format is
Packit Service 1d0348
	 * even if we weren't able to read it. */
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_XZ;
Packit Service 1d0348
	self->name = "xz";
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
lzip_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	r = __archive_read_program(self, "lzip -d -q");
Packit Service 1d0348
	/* Note: We set the format here even if __archive_read_program()
Packit Service 1d0348
	 * above fails.  We do, after all, know what the format is
Packit Service 1d0348
	 * even if we weren't able to read it. */
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_LZIP;
Packit Service 1d0348
	self->name = "lzip";
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#endif /* HAVE_LZMA_H */