1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

1034: Fixed deflate decompression.

First try decompress in zlib format.
If this fails, restart with the raw deflate.
Works for both blogs.msdn.com and for URL of the bug 1034.
This commit is contained in:
Witold Filipczyk 2008-07-20 12:01:49 +02:00 committed by Kalle Olavi Niemitalo
parent 83ccaa3673
commit e213a91bad

View File

@ -30,6 +30,7 @@ struct deflate_enc_data {
int fdread;
unsigned int last_read:1;
unsigned int after_first_read:1;
/* A buffer for data that has been read from the file but not
* yet decompressed. z_stream.next_in and z_stream.avail_in
@ -59,6 +60,7 @@ deflate_open(int window_size, struct stream_encoded *stream, int fd)
copy_struct(&data->deflate_stream, &null_z_stream);
data->fdread = fd;
data->last_read = 0;
data->after_first_read = 0;
err = inflateInit2(&data->deflate_stream, window_size);
if (err != Z_OK) {
@ -70,12 +72,14 @@ deflate_open(int window_size, struct stream_encoded *stream, int fd)
return 0;
}
#if 0
static int
deflate_raw_open(struct stream_encoded *stream, int fd)
{
/* raw DEFLATE with neither zlib nor gzip header */
return deflate_open(-MAX_WBITS, stream, fd);
}
#endif
static int
deflate_gzip_open(struct stream_encoded *stream, int fd)
@ -89,6 +93,7 @@ deflate_read(struct stream_encoded *stream, unsigned char *buf, int len)
{
struct deflate_enc_data *data = (struct deflate_enc_data *) stream->data;
int err = 0;
int l;
if (!data) return -1;
@ -101,7 +106,7 @@ deflate_read(struct stream_encoded *stream, unsigned char *buf, int len)
do {
if (data->deflate_stream.avail_in == 0) {
int l = safe_read(data->fdread, data->buf,
l = safe_read(data->fdread, data->buf,
ELINKS_DEFLATE_BUFFER_LENGTH);
if (l == -1) {
@ -117,7 +122,19 @@ deflate_read(struct stream_encoded *stream, unsigned char *buf, int len)
data->deflate_stream.next_in = data->buf;
data->deflate_stream.avail_in = l;
}
restart:
err = inflate(&data->deflate_stream, Z_SYNC_FLUSH);
if (err == Z_DATA_ERROR && !data->after_first_read) {
data->after_first_read = 1;
inflateEnd(&data->deflate_stream);
data->deflate_stream.avail_out = len;
data->deflate_stream.next_out = buf;
data->deflate_stream.next_in = data->buf;
data->deflate_stream.avail_in = l;
err = inflateInit2(&data->deflate_stream, -MAX_WBITS);
if (err == Z_OK) goto restart;
}
data->after_first_read = 1;
if (err == Z_STREAM_END) {
data->last_read = 1;
break;
@ -211,7 +228,7 @@ static const unsigned char *const deflate_extensions[] = { NULL };
const struct decoding_backend deflate_decoding_backend = {
"deflate",
deflate_extensions,
deflate_raw_open,
deflate_gzip_open,
deflate_read,
deflate_raw_decode_buffer,
deflate_close,