Blame libarchive/archive_read_support_filter_compress.c

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