Blame libarchive/archive_read_support_filter_compress.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
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
/*
Packit Service 1d0348
 * This code borrows heavily from "compress" source code, which is
Packit Service 1d0348
 * protected by the following copyright.  (Clause 3 dropped by request
Packit Service 1d0348
 * of the Regents.)
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 1985, 1986, 1992, 1993
Packit Service 1d0348
 *	The Regents of the University of California.  All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * This code is derived from software contributed to Berkeley by
Packit Service 1d0348
 * Diomidis Spinellis and James A. Woods, derived from original
Packit Service 1d0348
 * work by Spencer Thomas and Joseph Orost.
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
 * 4. Neither the name of the University nor the names of its contributors
Packit Service 1d0348
 *    may be used to endorse or promote products derived from this software
Packit Service 1d0348
 *    without specific prior written permission.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit Service 1d0348
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service 1d0348
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service 1d0348
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit Service 1d0348
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 1d0348
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service 1d0348
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service 1d0348
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service 1d0348
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service 1d0348
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service 1d0348
 * SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
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
#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
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_read_private.h"
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Because LZW decompression is pretty simple, I've just implemented
Packit Service 1d0348
 * the whole decompressor here (cribbing from "compress" source code,
Packit Service 1d0348
 * of course), rather than relying on an external library.  I have
Packit Service 1d0348
 * made an effort to clarify and simplify the algorithm, so the
Packit Service 1d0348
 * names and structure here don't exactly match those used by compress.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
struct private_data {
Packit Service 1d0348
	/* Input variables. */
Packit Service 1d0348
	const unsigned char	*next_in;
Packit Service 1d0348
	size_t			 avail_in;
Packit Service 1d0348
	size_t			 consume_unnotified;
Packit Service 1d0348
	int			 bit_buffer;
Packit Service 1d0348
	int			 bits_avail;
Packit Service 1d0348
	size_t			 bytes_in_section;
Packit Service 1d0348
Packit Service 1d0348
	/* Output variables. */
Packit Service 1d0348
	size_t			 out_block_size;
Packit Service 1d0348
	void			*out_block;
Packit Service 1d0348
Packit Service 1d0348
	/* Decompression status variables. */
Packit Service 1d0348
	int			 use_reset_code;
Packit Service 1d0348
	int			 end_of_stream;	/* EOF status. */
Packit Service 1d0348
	int			 maxcode;	/* Largest code. */
Packit Service 1d0348
	int			 maxcode_bits;	/* Length of largest code. */
Packit Service 1d0348
	int			 section_end_code; /* When to increase bits. */
Packit Service 1d0348
	int			 bits;		/* Current code length. */
Packit Service 1d0348
	int			 oldcode;	/* Previous code. */
Packit Service 1d0348
	int			 finbyte;	/* Last byte of prev code. */
Packit Service 1d0348
Packit Service 1d0348
	/* Dictionary. */
Packit Service 1d0348
	int			 free_ent;       /* Next dictionary entry. */
Packit Service 1d0348
	unsigned char		 suffix[65536];
Packit Service 1d0348
	uint16_t		 prefix[65536];
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Scratch area for expanding dictionary entries.  Note:
Packit Service 1d0348
	 * "worst" case here comes from compressing /dev/zero: the
Packit Service 1d0348
	 * last code in the dictionary will code a sequence of
Packit Service 1d0348
	 * 65536-256 zero bytes.  Thus, we need stack space to expand
Packit Service 1d0348
	 * a 65280-byte dictionary entry.  (Of course, 32640:1
Packit Service 1d0348
	 * compression could also be considered the "best" case. ;-)
Packit Service 1d0348
	 */
Packit Service 1d0348
	unsigned char		*stackp;
Packit Service 1d0348
	unsigned char		 stack[65300];
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
static int	compress_bidder_bid(struct archive_read_filter_bidder *, struct archive_read_filter *);
Packit Service 1d0348
static int	compress_bidder_init(struct archive_read_filter *);
Packit Service 1d0348
static int	compress_bidder_free(struct archive_read_filter_bidder *);
Packit Service 1d0348
Packit Service 1d0348
static ssize_t	compress_filter_read(struct archive_read_filter *, const void **);
Packit Service 1d0348
static int	compress_filter_close(struct archive_read_filter *);
Packit Service 1d0348
Packit Service 1d0348
static int	getbits(struct archive_read_filter *, int n);
Packit Service 1d0348
static int	next_code(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_compress(struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	return archive_read_support_filter_compress(a);
Packit Service 1d0348
}
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_filter_compress(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_compress");
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 = "compress (.Z)";
Packit Service 1d0348
	bidder->bid = compress_bidder_bid;
Packit Service 1d0348
	bidder->init = compress_bidder_init;
Packit Service 1d0348
	bidder->options = NULL;
Packit Service 1d0348
	bidder->free = compress_bidder_free;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Test whether we can handle this data.
Packit Service 1d0348
 * This logic returns zero if any part of the signature fails.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
compress_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
	int bits_checked;
Packit Service 1d0348
Packit Service 1d0348
	(void)self; /* UNUSED */
Packit Service 1d0348
Packit Service 1d0348
	/* Shortest valid compress file is 3 bytes. */
Packit Service 1d0348
	buffer = __archive_read_filter_ahead(filter, 3, &avail);
Packit Service 1d0348
Packit Service 1d0348
	if (buffer == NULL)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
Packit Service 1d0348
	bits_checked = 0;
Packit Service 1d0348
	/* First two bytes are the magic value */
Packit Service 1d0348
	if (buffer[0] != 0x1F || buffer[1] != 0x9D)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	/* Third byte holds compression parameters. */
Packit Service 1d0348
	if (buffer[2] & 0x20) /* Reserved bit, must be zero. */
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	if (buffer[2] & 0x40) /* Reserved bit, must be zero. */
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	bits_checked += 18;
Packit Service 1d0348
Packit Service 1d0348
	return (bits_checked);
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
compress_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
	static const size_t out_block_size = 64 * 1024;
Packit Service 1d0348
	void *out_block;
Packit Service 1d0348
	int code;
Packit Service 1d0348
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_COMPRESS;
Packit Service 1d0348
	self->name = "compress (.Z)";
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)calloc(sizeof(*state), 1);
Packit Service 1d0348
	out_block = malloc(out_block_size);
Packit Service 1d0348
	if (state == NULL || out_block == NULL) {
Packit Service 1d0348
		free(out_block);
Packit Service 1d0348
		free(state);
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate data for %s decompression",
Packit Service 1d0348
		    self->name);
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 = compress_filter_read;
Packit Service 1d0348
	self->skip = NULL; /* not supported */
Packit Service 1d0348
	self->close = compress_filter_close;
Packit Service 1d0348
Packit Service 1d0348
	/* XXX MOVE THE FOLLOWING OUT OF INIT() XXX */
Packit Service 1d0348
Packit Service 1d0348
	(void)getbits(self, 8); /* Skip first signature byte. */
Packit Service 1d0348
	(void)getbits(self, 8); /* Skip second signature byte. */
Packit Service 1d0348
Packit Service 1d0348
	/* Get compression parameters. */
Packit Service 1d0348
	code = getbits(self, 8);
Packit Service 1d0348
	if ((code & 0x1f) > 16) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, -1,
Packit Service 1d0348
		    "Invalid compressed data");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	state->maxcode_bits = code & 0x1f;
Packit Service 1d0348
	state->maxcode = (1 << state->maxcode_bits);
Packit Service 1d0348
	state->use_reset_code = code & 0x80;
Packit Service 1d0348
Packit Service 1d0348
	/* Initialize decompressor. */
Packit Service 1d0348
	state->free_ent = 256;
Packit Service 1d0348
	state->stackp = state->stack;
Packit Service 1d0348
	if (state->use_reset_code)
Packit Service 1d0348
		state->free_ent++;
Packit Service 1d0348
	state->bits = 9;
Packit Service 1d0348
	state->section_end_code = (1<<state->bits) - 1;
Packit Service 1d0348
	state->oldcode = -1;
Packit Service 1d0348
	for (code = 255; code >= 0; code--) {
Packit Service 1d0348
		state->prefix[code] = 0;
Packit Service 1d0348
		state->suffix[code] = code;
Packit Service 1d0348
	}
Packit Service 1d0348
	next_code(self);
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Return a block of data from the decompression buffer.  Decompress more
Packit Service 1d0348
 * as necessary.
Packit Service 1d0348
 */
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
compress_filter_read(struct archive_read_filter *self, const void **pblock)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state;
Packit Service 1d0348
	unsigned char *p, *start, *end;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct private_data *)self->data;
Packit Service 1d0348
	if (state->end_of_stream) {
Packit Service 1d0348
		*pblock = NULL;
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	}
Packit Service 1d0348
	p = start = (unsigned char *)state->out_block;
Packit Service 1d0348
	end = start + state->out_block_size;
Packit Service 1d0348
Packit Service 1d0348
	while (p < end && !state->end_of_stream) {
Packit Service 1d0348
		if (state->stackp > state->stack) {
Packit Service 1d0348
			*p++ = *--state->stackp;
Packit Service 1d0348
		} else {
Packit Service 1d0348
			ret = next_code(self);
Packit Service 1d0348
			if (ret == -1)
Packit Service 1d0348
				state->end_of_stream = ret;
Packit Service 1d0348
			else if (ret != ARCHIVE_OK)
Packit Service 1d0348
				return (ret);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	*pblock = start;
Packit Service 1d0348
	return (p - start);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Clean up the reader.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
compress_bidder_free(struct archive_read_filter_bidder *self)
Packit Service 1d0348
{
Packit Service 1d0348
	self->data = NULL;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Close and release the filter.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
compress_filter_close(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state = (struct private_data *)self->data;
Packit Service 1d0348
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
/*
Packit Service 1d0348
 * Process the next code and fill the stack with the expansion
Packit Service 1d0348
 * of the code.  Returns ARCHIVE_FATAL if there is a fatal I/O or
Packit Service 1d0348
 * format error, ARCHIVE_EOF if we hit end of data, ARCHIVE_OK otherwise.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
next_code(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state = (struct private_data *)self->data;
Packit Service 1d0348
	int code, newcode;
Packit Service 1d0348
Packit Service 1d0348
	static int debug_buff[1024];
Packit Service 1d0348
	static unsigned debug_index;
Packit Service 1d0348
Packit Service 1d0348
	code = newcode = getbits(self, state->bits);
Packit Service 1d0348
	if (code < 0)
Packit Service 1d0348
		return (code);
Packit Service 1d0348
Packit Service 1d0348
	debug_buff[debug_index++] = code;
Packit Service 1d0348
	if (debug_index >= sizeof(debug_buff)/sizeof(debug_buff[0]))
Packit Service 1d0348
		debug_index = 0;
Packit Service 1d0348
Packit Service 1d0348
	/* If it's a reset code, reset the dictionary. */
Packit Service 1d0348
	if ((code == 256) && state->use_reset_code) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * The original 'compress' implementation blocked its
Packit Service 1d0348
		 * I/O in a manner that resulted in junk bytes being
Packit Service 1d0348
		 * inserted after every reset.  The next section skips
Packit Service 1d0348
		 * this junk.  (Yes, the number of *bytes* to skip is
Packit Service 1d0348
		 * a function of the current *bit* length.)
Packit Service 1d0348
		 */
Packit Service 1d0348
		int skip_bytes =  state->bits -
Packit Service 1d0348
		    (state->bytes_in_section % state->bits);
Packit Service 1d0348
		skip_bytes %= state->bits;
Packit Service 1d0348
		state->bits_avail = 0; /* Discard rest of this byte. */
Packit Service 1d0348
		while (skip_bytes-- > 0) {
Packit Service 1d0348
			code = getbits(self, 8);
Packit Service 1d0348
			if (code < 0)
Packit Service 1d0348
				return (code);
Packit Service 1d0348
		}
Packit Service 1d0348
		/* Now, actually do the reset. */
Packit Service 1d0348
		state->bytes_in_section = 0;
Packit Service 1d0348
		state->bits = 9;
Packit Service 1d0348
		state->section_end_code = (1 << state->bits) - 1;
Packit Service 1d0348
		state->free_ent = 257;
Packit Service 1d0348
		state->oldcode = -1;
Packit Service 1d0348
		return (next_code(self));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (code > state->free_ent
Packit Service 1d0348
	    || (code == state->free_ent && state->oldcode < 0)) {
Packit Service 1d0348
		/* An invalid code is a fatal error. */
Packit Service 1d0348
		archive_set_error(&(self->archive->archive), -1,
Packit Service 1d0348
		    "Invalid compressed data");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Special case for KwKwK string. */
Packit Service 1d0348
	if (code >= state->free_ent) {
Packit Service 1d0348
		*state->stackp++ = state->finbyte;
Packit Service 1d0348
		code = state->oldcode;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Generate output characters in reverse order. */
Packit Service 1d0348
	while (code >= 256) {
Packit Service 1d0348
		*state->stackp++ = state->suffix[code];
Packit Service 1d0348
		code = state->prefix[code];
Packit Service 1d0348
	}
Packit Service 1d0348
	*state->stackp++ = state->finbyte = code;
Packit Service 1d0348
Packit Service 1d0348
	/* Generate the new entry. */
Packit Service 1d0348
	code = state->free_ent;
Packit Service 1d0348
	if (code < state->maxcode && state->oldcode >= 0) {
Packit Service 1d0348
		state->prefix[code] = state->oldcode;
Packit Service 1d0348
		state->suffix[code] = state->finbyte;
Packit Service 1d0348
		++state->free_ent;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (state->free_ent > state->section_end_code) {
Packit Service 1d0348
		state->bits++;
Packit Service 1d0348
		state->bytes_in_section = 0;
Packit Service 1d0348
		if (state->bits == state->maxcode_bits)
Packit Service 1d0348
			state->section_end_code = state->maxcode;
Packit Service 1d0348
		else
Packit Service 1d0348
			state->section_end_code = (1 << state->bits) - 1;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Remember previous code. */
Packit Service 1d0348
	state->oldcode = newcode;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Return next 'n' bits from stream.
Packit Service 1d0348
 *
Packit Service 1d0348
 * -1 indicates end of available data.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
getbits(struct archive_read_filter *self, int n)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *state = (struct private_data *)self->data;
Packit Service 1d0348
	int code;
Packit Service 1d0348
	ssize_t ret;
Packit Service 1d0348
	static const int mask[] = {
Packit Service 1d0348
		0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff,
Packit Service 1d0348
		0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff
Packit Service 1d0348
	};
Packit Service 1d0348
Packit Service 1d0348
	while (state->bits_avail < n) {
Packit Service 1d0348
		if (state->avail_in <= 0) {
Packit Service 1d0348
			if (state->consume_unnotified) {
Packit Service 1d0348
				__archive_read_filter_consume(self->upstream,
Packit Service 1d0348
					state->consume_unnotified);
Packit Service 1d0348
				state->consume_unnotified = 0;
Packit Service 1d0348
			}
Packit Service 1d0348
			state->next_in
Packit Service 1d0348
			    = __archive_read_filter_ahead(self->upstream,
Packit Service 1d0348
				1, &ret;;
Packit Service 1d0348
			if (ret == 0)
Packit Service 1d0348
				return (-1);
Packit Service 1d0348
			if (ret < 0 || state->next_in == NULL)
Packit Service 1d0348
				return (ARCHIVE_FATAL);
Packit Service 1d0348
			state->consume_unnotified = state->avail_in = ret;
Packit Service 1d0348
		}
Packit Service 1d0348
		state->bit_buffer |= *state->next_in++ << state->bits_avail;
Packit Service 1d0348
		state->avail_in--;
Packit Service 1d0348
		state->bits_avail += 8;
Packit Service 1d0348
		state->bytes_in_section++;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	code = state->bit_buffer;
Packit Service 1d0348
	state->bit_buffer >>= n;
Packit Service 1d0348
	state->bits_avail -= n;
Packit Service 1d0348
Packit Service 1d0348
	return (code & mask[n]);
Packit Service 1d0348
}