2005-09-15 09:58:31 -04:00
|
|
|
/* Bzip2 encoding (ENCODING_BZIP2) backend */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2020-09-05 16:02:16 -04:00
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#include <bzlib.h> /* Everything needs this after stdio.h */
|
2020-09-05 16:02:16 -04:00
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
#include <errno.h>
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "encoding/bzip2.h"
|
|
|
|
#include "encoding/encoding.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
|
2007-02-24 15:51:12 -05:00
|
|
|
/* How many bytes of compressed data to read before decompressing.
|
|
|
|
* This is currently defined as BZ_MAX_UNUSED to make the behaviour
|
|
|
|
* similar to BZ2_bzRead; but other values would work too. */
|
2007-01-07 10:17:43 -05:00
|
|
|
#define ELINKS_BZ_BUFFER_LENGTH BZ_MAX_UNUSED
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
struct bz2_enc_data {
|
2007-01-07 10:17:43 -05:00
|
|
|
bz_stream fbz_stream;
|
2007-02-24 15:51:12 -05:00
|
|
|
|
|
|
|
/* The file descriptor from which we read. */
|
2007-01-07 10:17:43 -05:00
|
|
|
int fdread;
|
2007-02-24 15:51:12 -05:00
|
|
|
|
|
|
|
/* Initially 0; set to 1 when BZ2_bzDecompress indicates
|
|
|
|
* BZ_STREAM_END, which means it has found the bzip2-specific
|
|
|
|
* end-of-stream marker and all data has been decompressed.
|
|
|
|
* Then we neither read from the file nor call BZ2_bzDecompress
|
|
|
|
* any more. */
|
2010-09-24 10:12:35 -04:00
|
|
|
int last_read:1;
|
|
|
|
int after_end:1;
|
2007-02-24 15:51:12 -05:00
|
|
|
|
|
|
|
/* A buffer for data that has been read from the file but not
|
|
|
|
* yet decompressed. fbz_stream.next_in and fbz_stream.avail_in
|
|
|
|
* refer to this buffer. */
|
2021-01-02 10:20:27 -05:00
|
|
|
char buf[ELINKS_BZ_BUFFER_LENGTH];
|
2005-09-15 09:58:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
bzip2_open(struct stream_encoded *stream, int fd)
|
|
|
|
{
|
2007-02-24 15:51:12 -05:00
|
|
|
/* A zero-initialized bz_stream. The compiler ensures that all
|
|
|
|
* pointer members in it are null. (Can't do this with memset
|
|
|
|
* because C99 does not require all-bits-zero to be a null
|
|
|
|
* pointer.) */
|
|
|
|
static const bz_stream null_bz_stream = {0};
|
|
|
|
|
2022-01-16 13:09:27 -05:00
|
|
|
struct bz2_enc_data *data = (struct bz2_enc_data *)mem_alloc(sizeof(*data));
|
2005-09-15 09:58:31 -04:00
|
|
|
int err;
|
|
|
|
|
2007-02-24 15:51:12 -05:00
|
|
|
stream->data = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!data) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-24 15:51:12 -05:00
|
|
|
/* Initialize all members of *data, except data->buf[], which
|
|
|
|
* will be initialized on demand by bzip2_read. */
|
|
|
|
copy_struct(&data->fbz_stream, &null_bz_stream);
|
2007-01-07 10:17:43 -05:00
|
|
|
data->fdread = fd;
|
2007-02-24 15:51:12 -05:00
|
|
|
data->last_read = 0;
|
2015-05-10 11:27:55 -04:00
|
|
|
data->after_end = 0;
|
2007-09-14 09:12:32 -04:00
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
err = BZ2_bzDecompressInit(&data->fbz_stream, 0, 0);
|
|
|
|
if (err != BZ_OK) {
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(data);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->data = data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-01-02 10:20:27 -05:00
|
|
|
bzip2_read(struct stream_encoded *stream, char *buf, int len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
|
|
|
|
int err = 0;
|
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
if (!data) return -1;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-09-14 09:12:32 -04:00
|
|
|
assert(len > 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
if (data->last_read) return 0;
|
|
|
|
|
|
|
|
data->fbz_stream.avail_out = len;
|
|
|
|
data->fbz_stream.next_out = buf;
|
|
|
|
|
2007-09-14 09:12:32 -04:00
|
|
|
do {
|
2007-01-07 10:17:43 -05:00
|
|
|
if (data->fbz_stream.avail_in == 0) {
|
|
|
|
int l = safe_read(data->fdread, data->buf,
|
|
|
|
ELINKS_BZ_BUFFER_LENGTH);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
if (l == -1) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return -1; /* I/O error */
|
|
|
|
} else if (l == 0) {
|
|
|
|
/* EOF. It is error: we wait for more bytes */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->fbz_stream.next_in = data->buf;
|
|
|
|
data->fbz_stream.avail_in = l;
|
|
|
|
}
|
2007-09-14 09:12:32 -04:00
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
err = BZ2_bzDecompress(&data->fbz_stream);
|
2007-09-14 09:12:32 -04:00
|
|
|
if (err == BZ_STREAM_END) {
|
2007-01-07 10:17:43 -05:00
|
|
|
data->last_read = 1;
|
|
|
|
break;
|
2007-09-14 09:12:32 -04:00
|
|
|
} else if (err != BZ_OK) {
|
2007-01-07 10:17:43 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (data->fbz_stream.avail_out > 0);
|
2007-02-24 15:51:12 -05:00
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
assert(len - data->fbz_stream.avail_out == data->fbz_stream.next_out - buf);
|
2007-01-07 10:17:43 -05:00
|
|
|
return len - data->fbz_stream.avail_out;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMALL
|
|
|
|
#define BZIP2_SMALL 1
|
|
|
|
#else
|
|
|
|
#define BZIP2_SMALL 0
|
|
|
|
#endif
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
|
|
|
bzip2_decode_buffer(struct stream_encoded *st, char *data, int len, int *new_len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2010-09-24 10:12:35 -04:00
|
|
|
struct bz2_enc_data *enc_data = (struct bz2_enc_data *)st->data;
|
|
|
|
bz_stream *stream = &enc_data->fbz_stream;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *buffer = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
int error;
|
|
|
|
|
2008-02-17 11:55:41 -05:00
|
|
|
*new_len = 0; /* default, left there if an error occurs */
|
|
|
|
|
2010-09-24 10:12:35 -04:00
|
|
|
stream->next_in = data;
|
|
|
|
stream->avail_in = len;
|
|
|
|
stream->total_out_lo32 = 0;
|
|
|
|
stream->total_out_hi32 = 0;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
do {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *new_buffer;
|
2010-09-24 10:12:35 -04:00
|
|
|
size_t size = stream->total_out_lo32 + MAX_STR_LEN;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* FIXME: support for 64 bit. real size is
|
|
|
|
*
|
|
|
|
* (total_in_hi32 << * 32) + total_in_lo32
|
|
|
|
*
|
|
|
|
* --jonas */
|
2010-09-24 10:12:35 -04:00
|
|
|
assertm(!stream->total_out_hi32, "64 bzip2 decoding not supported");
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2022-01-16 13:38:30 -05:00
|
|
|
new_buffer = (char *)mem_realloc(buffer, size);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!new_buffer) {
|
|
|
|
error = BZ_MEM_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new_buffer;
|
2010-09-24 10:12:35 -04:00
|
|
|
stream->next_out = buffer + stream->total_out_lo32;
|
|
|
|
stream->avail_out = MAX_STR_LEN;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2010-09-24 10:12:35 -04:00
|
|
|
error = BZ2_bzDecompress(stream);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (error == BZ_STREAM_END) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apparently BZ_STREAM_END is not forced when the end of input
|
|
|
|
* is reached. At least lindi- reported that it caused a
|
|
|
|
* reproducable infinite loop. Maybe it has to do with decoding
|
|
|
|
* an incomplete file. */
|
2010-09-24 10:12:35 -04:00
|
|
|
} while (error == BZ_OK && stream->avail_in > 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2010-09-24 10:12:35 -04:00
|
|
|
if (error == BZ_STREAM_END) {
|
|
|
|
BZ2_bzDecompressEnd(stream);
|
|
|
|
enc_data->after_end = 1;
|
|
|
|
error = BZ_OK;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-17 11:55:41 -05:00
|
|
|
if (error == BZ_OK) {
|
2010-09-24 10:12:35 -04:00
|
|
|
*new_len = stream->total_out_lo32;
|
2008-02-17 11:55:41 -05:00
|
|
|
return buffer;
|
|
|
|
} else {
|
2015-05-10 11:27:55 -04:00
|
|
|
mem_free_if(buffer);
|
2005-09-15 09:58:31 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bzip2_close(struct stream_encoded *stream)
|
|
|
|
{
|
|
|
|
struct bz2_enc_data *data = (struct bz2_enc_data *) stream->data;
|
|
|
|
|
2007-01-07 10:17:43 -05:00
|
|
|
if (data) {
|
2010-09-24 10:12:35 -04:00
|
|
|
if (!data->after_end) {
|
|
|
|
BZ2_bzDecompressEnd(&data->fbz_stream);
|
|
|
|
}
|
|
|
|
if (data->fdread != -1) {
|
|
|
|
close(data->fdread);
|
|
|
|
}
|
2007-01-07 10:17:43 -05:00
|
|
|
mem_free(data);
|
|
|
|
stream->data = 0;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2021-12-27 11:02:58 -05:00
|
|
|
const char *
|
|
|
|
get_bzip2_version(void)
|
|
|
|
{
|
|
|
|
static char version[16];
|
|
|
|
|
|
|
|
if (!version[0]) {
|
|
|
|
strncpy(version, BZ2_bzlibVersion(), 15);
|
|
|
|
|
|
|
|
size_t ok = strspn(version, "0123456789.");
|
|
|
|
|
|
|
|
version[ok] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static const char *const bzip2_extensions[] = { ".bz2", ".tbz", NULL };
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-02-24 14:42:56 -05:00
|
|
|
const struct decoding_backend bzip2_decoding_backend = {
|
2005-09-15 09:58:31 -04:00
|
|
|
"bzip2",
|
|
|
|
bzip2_extensions,
|
|
|
|
bzip2_open,
|
|
|
|
bzip2_read,
|
|
|
|
bzip2_decode_buffer,
|
|
|
|
bzip2_close,
|
|
|
|
};
|