2004-05-27 03:58:55 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-05-27 03:58:55 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-05-27 03:58:55 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This code borrows heavily from "compress" source code, which is
|
|
|
|
* protected by the following copyright. (Clause 3 dropped by request
|
|
|
|
* of the Regents.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1985, 1986, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Diomidis Spinellis and James A. Woods, derived from original
|
|
|
|
* work by Spencer Thomas and Joseph Orost.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "archive_platform.h"
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-05-27 03:58:55 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-05-27 03:58:55 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-05-27 03:58:55 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-05-27 03:58:55 +00:00
|
|
|
#include <unistd.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_read_private.h"
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Because LZW decompression is pretty simple, I've just implemented
|
|
|
|
* the whole decompressor here (cribbing from "compress" source code,
|
|
|
|
* of course), rather than relying on an external library. I have
|
|
|
|
* made an effort to clarify and simplify the algorithm, so the
|
|
|
|
* names and structure here don't exactly match those used by compress.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct private_data {
|
|
|
|
/* Input variables. */
|
|
|
|
const unsigned char *next_in;
|
|
|
|
size_t avail_in;
|
|
|
|
int bit_buffer;
|
|
|
|
int bits_avail;
|
|
|
|
size_t bytes_in_section;
|
|
|
|
|
|
|
|
/* Output variables. */
|
2008-12-06 06:45:15 +00:00
|
|
|
size_t out_block_size;
|
|
|
|
void *out_block;
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
/* Decompression status variables. */
|
|
|
|
int use_reset_code;
|
|
|
|
int end_of_stream; /* EOF status. */
|
|
|
|
int maxcode; /* Largest code. */
|
|
|
|
int maxcode_bits; /* Length of largest code. */
|
|
|
|
int section_end_code; /* When to increase bits. */
|
|
|
|
int bits; /* Current code length. */
|
|
|
|
int oldcode; /* Previous code. */
|
|
|
|
int finbyte; /* Last byte of prev code. */
|
|
|
|
|
|
|
|
/* Dictionary. */
|
|
|
|
int free_ent; /* Next dictionary entry. */
|
|
|
|
unsigned char suffix[65536];
|
|
|
|
uint16_t prefix[65536];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scratch area for expanding dictionary entries. Note:
|
|
|
|
* "worst" case here comes from compressing /dev/zero: the
|
|
|
|
* last code in the dictionary will code a sequence of
|
|
|
|
* 65536-256 zero bytes. Thus, we need stack space to expand
|
|
|
|
* a 65280-byte dictionary entry. (Of course, 32640:1
|
|
|
|
* compression could also be considered the "best" case. ;-)
|
|
|
|
*/
|
|
|
|
unsigned char *stackp;
|
|
|
|
unsigned char stack[65300];
|
|
|
|
};
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
static int compress_reader_bid(struct archive_reader *, const void *, size_t);
|
|
|
|
static struct archive_read_source *compress_reader_init(struct archive_read *,
|
|
|
|
struct archive_reader *, struct archive_read_source *,
|
|
|
|
const void *, size_t);
|
|
|
|
static int compress_reader_free(struct archive_reader *);
|
|
|
|
|
|
|
|
static ssize_t compress_source_read(struct archive_read_source *, const void **);
|
|
|
|
static int compress_source_close(struct archive_read_source *);
|
|
|
|
|
|
|
|
static int getbits(struct archive_read_source *, int n);
|
|
|
|
static int next_code(struct archive_read_source *);
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_support_compression_compress(struct archive *_a)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2008-12-06 06:45:15 +00:00
|
|
|
struct archive_reader *reader = __archive_read_get_reader(a);
|
|
|
|
|
|
|
|
if (reader == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
|
|
|
|
reader->data = NULL;
|
|
|
|
reader->bid = compress_reader_bid;
|
|
|
|
reader->init = compress_reader_init;
|
|
|
|
reader->free = compress_reader_free;
|
|
|
|
return (ARCHIVE_OK);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test whether we can handle this data.
|
|
|
|
*
|
|
|
|
* This logic returns zero if any part of the signature fails. It
|
|
|
|
* also tries to Do The Right Thing if a very short buffer prevents us
|
|
|
|
* from verifying as much as we would like.
|
|
|
|
*/
|
|
|
|
static int
|
2008-12-06 06:45:15 +00:00
|
|
|
compress_reader_bid(struct archive_reader *self, const void *buff, size_t len)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
|
|
|
const unsigned char *buffer;
|
|
|
|
int bits_checked;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
(void)self; /* UNUSED */
|
|
|
|
|
2004-05-27 03:58:55 +00:00
|
|
|
if (len < 1)
|
|
|
|
return (0);
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
buffer = (const unsigned char *)buff;
|
2004-05-27 03:58:55 +00:00
|
|
|
bits_checked = 0;
|
|
|
|
if (buffer[0] != 037) /* Verify first ID byte. */
|
|
|
|
return (0);
|
|
|
|
bits_checked += 8;
|
|
|
|
if (len < 2)
|
|
|
|
return (bits_checked);
|
|
|
|
|
|
|
|
if (buffer[1] != 0235) /* Verify second ID byte. */
|
|
|
|
return (0);
|
|
|
|
bits_checked += 8;
|
|
|
|
if (len < 3)
|
|
|
|
return (bits_checked);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Verify more.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (bits_checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup the callbacks.
|
|
|
|
*/
|
2008-12-06 06:45:15 +00:00
|
|
|
static struct archive_read_source *
|
|
|
|
compress_reader_init(struct archive_read *a, struct archive_reader *reader,
|
|
|
|
struct archive_read_source *upstream, const void *buff, size_t n)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
struct archive_read_source *self;
|
2004-05-27 03:58:55 +00:00
|
|
|
struct private_data *state;
|
|
|
|
int code;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
(void)reader; /* UNUSED */
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.compression_code = ARCHIVE_COMPRESSION_COMPRESS;
|
|
|
|
a->archive.compression_name = "compress (.Z)";
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
self = calloc(sizeof(*self), 1);
|
|
|
|
if (self == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
self->read = compress_source_read;
|
|
|
|
self->skip = NULL; /* not supported */
|
|
|
|
self->close = compress_source_close;
|
|
|
|
self->upstream = upstream;
|
|
|
|
self->archive = a;
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)calloc(sizeof(*state), 1);
|
2004-05-27 03:58:55 +00:00
|
|
|
if (state == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ENOMEM,
|
2004-05-27 03:58:55 +00:00
|
|
|
"Can't allocate data for %s decompression",
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.compression_name);
|
2008-12-06 06:45:15 +00:00
|
|
|
free(self);
|
|
|
|
return (NULL);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
self->data = state;
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state->out_block_size = 64 * 1024;
|
|
|
|
state->out_block = malloc(state->out_block_size);
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
if (state->out_block == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ENOMEM,
|
2004-05-27 03:58:55 +00:00
|
|
|
"Can't allocate %s decompression buffers",
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.compression_name);
|
2004-05-27 03:58:55 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
state->next_in = (const unsigned char *)buff;
|
2004-05-27 03:58:55 +00:00
|
|
|
state->avail_in = n;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
code = getbits(self, 8);
|
2005-11-08 07:42:42 +00:00
|
|
|
if (code != 037) /* This should be impossible. */
|
2004-05-27 03:58:55 +00:00
|
|
|
goto fatal;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
code = getbits(self, 8);
|
2005-11-08 07:42:42 +00:00
|
|
|
if (code != 0235) {
|
|
|
|
/* This can happen if the library is receiving 1-byte
|
|
|
|
* blocks and gzip and compress are both enabled.
|
|
|
|
* You can't distinguish gzip and compress only from
|
|
|
|
* the first byte. */
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
|
2005-11-08 07:42:42 +00:00
|
|
|
"Compress signature did not match.");
|
2004-05-27 03:58:55 +00:00
|
|
|
goto fatal;
|
2005-11-08 07:42:42 +00:00
|
|
|
}
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
code = getbits(self, 8);
|
2004-05-27 03:58:55 +00:00
|
|
|
state->maxcode_bits = code & 0x1f;
|
|
|
|
state->maxcode = (1 << state->maxcode_bits);
|
|
|
|
state->use_reset_code = code & 0x80;
|
|
|
|
|
|
|
|
/* Initialize decompressor. */
|
|
|
|
state->free_ent = 256;
|
|
|
|
state->stackp = state->stack;
|
|
|
|
if (state->use_reset_code)
|
|
|
|
state->free_ent++;
|
|
|
|
state->bits = 9;
|
|
|
|
state->section_end_code = (1<<state->bits) - 1;
|
|
|
|
state->oldcode = -1;
|
|
|
|
for (code = 255; code >= 0; code--) {
|
|
|
|
state->prefix[code] = 0;
|
|
|
|
state->suffix[code] = code;
|
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
next_code(self);
|
|
|
|
return (self);
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
fatal:
|
2008-12-06 06:45:15 +00:00
|
|
|
compress_source_close(self);
|
|
|
|
return (NULL);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a block of data from the decompression buffer. Decompress more
|
|
|
|
* as necessary.
|
|
|
|
*/
|
|
|
|
static ssize_t
|
2008-12-06 06:45:15 +00:00
|
|
|
compress_source_read(struct archive_read_source *self, const void **pblock)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
|
|
|
struct private_data *state;
|
2008-12-06 06:45:15 +00:00
|
|
|
unsigned char *p, *start, *end;
|
2007-04-05 05:18:16 +00:00
|
|
|
int ret;
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)self->data;
|
|
|
|
if (state->end_of_stream) {
|
|
|
|
*pblock = NULL;
|
|
|
|
return (0);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
p = start = (unsigned char *)state->out_block;
|
|
|
|
end = start + state->out_block_size;
|
|
|
|
|
|
|
|
while (p < end && !state->end_of_stream) {
|
|
|
|
if (state->stackp > state->stack) {
|
|
|
|
*p++ = *--state->stackp;
|
|
|
|
} else {
|
|
|
|
ret = next_code(self);
|
|
|
|
if (ret == ARCHIVE_EOF)
|
|
|
|
state->end_of_stream = ret;
|
|
|
|
else if (ret != ARCHIVE_OK)
|
|
|
|
return (ret);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
*pblock = start;
|
|
|
|
return (p - start);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-12-06 06:45:15 +00:00
|
|
|
* Clean up the reader.
|
2004-05-27 03:58:55 +00:00
|
|
|
*/
|
2008-12-06 06:45:15 +00:00
|
|
|
static int
|
|
|
|
compress_reader_free(struct archive_reader *self)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
self->data = NULL;
|
|
|
|
return (ARCHIVE_OK);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-12-06 06:45:15 +00:00
|
|
|
* Close and release a source.
|
2004-05-27 03:58:55 +00:00
|
|
|
*/
|
|
|
|
static int
|
2008-12-06 06:45:15 +00:00
|
|
|
compress_source_close(struct archive_read_source *self)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
struct private_data *state = (struct private_data *)self->data;
|
2004-05-27 03:58:55 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
self->upstream->close(self->upstream);
|
|
|
|
free(state->out_block);
|
|
|
|
free(state);
|
|
|
|
free(self);
|
|
|
|
return (ARCHIVE_OK);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the next code and fill the stack with the expansion
|
2004-10-17 23:40:10 +00:00
|
|
|
* of the code. Returns ARCHIVE_FATAL if there is a fatal I/O or
|
|
|
|
* format error, ARCHIVE_EOF if we hit end of data, ARCHIVE_OK otherwise.
|
2004-05-27 03:58:55 +00:00
|
|
|
*/
|
|
|
|
static int
|
2008-12-06 06:45:15 +00:00
|
|
|
next_code(struct archive_read_source *self)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
struct private_data *state = (struct private_data *)self->data;
|
2004-05-27 03:58:55 +00:00
|
|
|
int code, newcode;
|
|
|
|
|
|
|
|
static int debug_buff[1024];
|
|
|
|
static unsigned debug_index;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
code = newcode = getbits(self, state->bits);
|
2004-05-27 03:58:55 +00:00
|
|
|
if (code < 0)
|
|
|
|
return (code);
|
|
|
|
|
|
|
|
debug_buff[debug_index++] = code;
|
|
|
|
if (debug_index >= sizeof(debug_buff)/sizeof(debug_buff[0]))
|
|
|
|
debug_index = 0;
|
|
|
|
|
|
|
|
/* If it's a reset code, reset the dictionary. */
|
|
|
|
if ((code == 256) && state->use_reset_code) {
|
|
|
|
/*
|
|
|
|
* The original 'compress' implementation blocked its
|
|
|
|
* I/O in a manner that resulted in junk bytes being
|
|
|
|
* inserted after every reset. The next section skips
|
|
|
|
* this junk. (Yes, the number of *bytes* to skip is
|
|
|
|
* a function of the current *bit* length.)
|
|
|
|
*/
|
|
|
|
int skip_bytes = state->bits -
|
|
|
|
(state->bytes_in_section % state->bits);
|
|
|
|
skip_bytes %= state->bits;
|
|
|
|
state->bits_avail = 0; /* Discard rest of this byte. */
|
|
|
|
while (skip_bytes-- > 0) {
|
2008-12-06 06:45:15 +00:00
|
|
|
code = getbits(self, 8);
|
2004-05-27 03:58:55 +00:00
|
|
|
if (code < 0)
|
|
|
|
return (code);
|
|
|
|
}
|
|
|
|
/* Now, actually do the reset. */
|
|
|
|
state->bytes_in_section = 0;
|
|
|
|
state->bits = 9;
|
|
|
|
state->section_end_code = (1 << state->bits) - 1;
|
|
|
|
state->free_ent = 257;
|
|
|
|
state->oldcode = -1;
|
2008-12-06 06:45:15 +00:00
|
|
|
return (next_code(self));
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
2004-10-17 23:40:10 +00:00
|
|
|
if (code > state->free_ent) {
|
|
|
|
/* An invalid code is a fatal error. */
|
2008-12-06 06:45:15 +00:00
|
|
|
archive_set_error(&(self->archive->archive), -1,
|
|
|
|
"Invalid compressed data");
|
2004-10-17 23:40:10 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
/* Special case for KwKwK string. */
|
|
|
|
if (code >= state->free_ent) {
|
|
|
|
*state->stackp++ = state->finbyte;
|
|
|
|
code = state->oldcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate output characters in reverse order. */
|
|
|
|
while (code >= 256) {
|
|
|
|
*state->stackp++ = state->suffix[code];
|
|
|
|
code = state->prefix[code];
|
|
|
|
}
|
|
|
|
*state->stackp++ = state->finbyte = code;
|
|
|
|
|
|
|
|
/* Generate the new entry. */
|
|
|
|
code = state->free_ent;
|
|
|
|
if (code < state->maxcode && state->oldcode >= 0) {
|
|
|
|
state->prefix[code] = state->oldcode;
|
|
|
|
state->suffix[code] = state->finbyte;
|
|
|
|
++state->free_ent;
|
|
|
|
}
|
|
|
|
if (state->free_ent > state->section_end_code) {
|
|
|
|
state->bits++;
|
|
|
|
state->bytes_in_section = 0;
|
|
|
|
if (state->bits == state->maxcode_bits)
|
|
|
|
state->section_end_code = state->maxcode;
|
|
|
|
else
|
|
|
|
state->section_end_code = (1 << state->bits) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember previous code. */
|
|
|
|
state->oldcode = newcode;
|
2004-10-17 23:40:10 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-05-27 03:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return next 'n' bits from stream.
|
|
|
|
*
|
|
|
|
* -1 indicates end of available data.
|
|
|
|
*/
|
|
|
|
static int
|
2008-12-06 06:45:15 +00:00
|
|
|
getbits(struct archive_read_source *self, int n)
|
2004-05-27 03:58:55 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
struct private_data *state = (struct private_data *)self->data;
|
2004-05-27 03:58:55 +00:00
|
|
|
int code, ret;
|
|
|
|
static const int mask[] = {
|
|
|
|
0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff,
|
|
|
|
0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff
|
|
|
|
};
|
2007-04-05 05:18:16 +00:00
|
|
|
const void *read_buf;
|
2004-05-27 03:58:55 +00:00
|
|
|
|
|
|
|
while (state->bits_avail < n) {
|
|
|
|
if (state->avail_in <= 0) {
|
2007-04-05 05:18:16 +00:00
|
|
|
read_buf = state->next_in;
|
2008-12-06 06:45:15 +00:00
|
|
|
ret = (self->upstream->read)(self->upstream, &read_buf);
|
2007-04-05 05:18:16 +00:00
|
|
|
state->next_in = read_buf;
|
2004-05-27 03:58:55 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
if (ret == 0)
|
|
|
|
return (ARCHIVE_EOF);
|
2008-12-06 06:45:15 +00:00
|
|
|
/* TODO: Fix this a->archive.raw_position += ret; */
|
2004-05-27 03:58:55 +00:00
|
|
|
state->avail_in = ret;
|
|
|
|
}
|
|
|
|
state->bit_buffer |= *state->next_in++ << state->bits_avail;
|
|
|
|
state->avail_in--;
|
|
|
|
state->bits_avail += 8;
|
|
|
|
state->bytes_in_section++;
|
|
|
|
}
|
|
|
|
|
|
|
|
code = state->bit_buffer;
|
|
|
|
state->bit_buffer >>= n;
|
|
|
|
state->bits_avail -= n;
|
|
|
|
|
|
|
|
return (code & mask[n]);
|
|
|
|
}
|