mirror of
https://github.com/rkd77/elinks.git
synced 2024-10-27 08:00:32 -04:00
encoding: added deflate.
Based on the bzip2 backend. It works (checked with the lighttpd and the PHP zlib.deflate filter).
This commit is contained in:
parent
c364abd748
commit
f2859f529e
@ -2,7 +2,7 @@ top_builddir=../..
|
||||
include $(top_builddir)/Makefile.config
|
||||
|
||||
OBJS-$(CONFIG_BZIP2) += bzip2.o
|
||||
OBJS-$(CONFIG_GZIP) += gzip.o
|
||||
OBJS-$(CONFIG_GZIP) += deflate.o gzip.o
|
||||
OBJS-$(CONFIG_LZMA) += lzma.o LzmaDecode.o
|
||||
|
||||
OBJS = encoding.o
|
||||
|
188
src/encoding/deflate.c
Normal file
188
src/encoding/deflate.c
Normal file
@ -0,0 +1,188 @@
|
||||
/* 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
|
||||
#ifdef HAVE_ZLIB_H
|
||||
#include <zlib.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
|
||||
#include "elinks.h"
|
||||
|
||||
#include "encoding/deflate.h"
|
||||
#include "encoding/encoding.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
/* How many bytes of compressed data to read before decompressing. */
|
||||
#define ELINKS_DEFLATE_BUFFER_LENGTH 5000
|
||||
|
||||
struct deflate_enc_data {
|
||||
z_stream deflate_stream;
|
||||
|
||||
/* The file descriptor from which we read. */
|
||||
int fdread;
|
||||
|
||||
unsigned int last_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
|
||||
* refer to this buffer. */
|
||||
unsigned char buf[ELINKS_DEFLATE_BUFFER_LENGTH];
|
||||
};
|
||||
|
||||
static int
|
||||
deflate_open(struct stream_encoded *stream, int fd)
|
||||
{
|
||||
/* A zero-initialized z_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 z_stream null_z_stream = {0};
|
||||
int err;
|
||||
|
||||
struct deflate_enc_data *data = mem_alloc(sizeof(*data));
|
||||
|
||||
stream->data = NULL;
|
||||
if (!data) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Initialize all members of *data, except data->buf[], which
|
||||
* will be initialized on demand by bzip2_read. */
|
||||
copy_struct(&data->deflate_stream, &null_z_stream);
|
||||
data->fdread = fd;
|
||||
data->last_read = 0;
|
||||
|
||||
err = inflateInit2(&data->deflate_stream, -15);
|
||||
if (err != Z_OK) {
|
||||
mem_free(data);
|
||||
return -1;
|
||||
}
|
||||
stream->data = data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
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;
|
||||
|
||||
if (!data) return -1;
|
||||
|
||||
assert(len > 0);
|
||||
|
||||
if (data->last_read) return 0;
|
||||
|
||||
data->deflate_stream.avail_out = len;
|
||||
data->deflate_stream.next_out = buf;
|
||||
|
||||
do {
|
||||
if (data->deflate_stream.avail_in == 0) {
|
||||
int l = safe_read(data->fdread, data->buf,
|
||||
ELINKS_DEFLATE_BUFFER_LENGTH);
|
||||
|
||||
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->deflate_stream.next_in = data->buf;
|
||||
data->deflate_stream.avail_in = l;
|
||||
}
|
||||
err = inflate(&data->deflate_stream, Z_SYNC_FLUSH);
|
||||
if (err == Z_STREAM_END) {
|
||||
data->last_read = 1;
|
||||
break;
|
||||
} else if (err != Z_OK) {
|
||||
return -1;
|
||||
}
|
||||
} while (data->deflate_stream.avail_out > 0);
|
||||
|
||||
assert(len - data->deflate_stream.avail_out == data->deflate_stream.next_out - buf);
|
||||
return len - data->deflate_stream.avail_out;
|
||||
}
|
||||
|
||||
static unsigned char *
|
||||
deflate_decode_buffer(unsigned char *data, int len, int *new_len)
|
||||
{
|
||||
z_stream stream;
|
||||
unsigned char *buffer = NULL;
|
||||
int error;
|
||||
|
||||
memset(&stream, 0, sizeof(z_stream));
|
||||
stream.next_in = data;
|
||||
stream.avail_in = len;
|
||||
|
||||
if (inflateInit(&stream) != Z_OK)
|
||||
return NULL;
|
||||
|
||||
do {
|
||||
unsigned char *new_buffer;
|
||||
size_t size = stream.total_in + MAX_STR_LEN;
|
||||
|
||||
new_buffer = mem_realloc(buffer, size);
|
||||
if (!new_buffer) {
|
||||
error = Z_MEM_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
buffer = new_buffer;
|
||||
stream.next_out = buffer + stream.total_out;
|
||||
stream.avail_out = MAX_STR_LEN;
|
||||
|
||||
error = inflate(&stream, Z_SYNC_FLUSH);
|
||||
if (error == Z_STREAM_END) {
|
||||
*new_len = stream.total_out;
|
||||
error = Z_OK;
|
||||
break;
|
||||
}
|
||||
} while (error == Z_OK && stream.avail_in > 0);
|
||||
|
||||
inflateEnd(&stream);
|
||||
|
||||
if (error != Z_OK) {
|
||||
if (buffer) mem_free(buffer);
|
||||
*new_len = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static void
|
||||
deflate_close(struct stream_encoded *stream)
|
||||
{
|
||||
struct deflate_enc_data *data = (struct deflate_enc_data *) stream->data;
|
||||
|
||||
if (data) {
|
||||
inflateEnd(&data->deflate_stream);
|
||||
close(data->fdread);
|
||||
mem_free(data);
|
||||
stream->data = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char *const deflate_extensions[] = { NULL };
|
||||
|
||||
const struct decoding_backend deflate_decoding_backend = {
|
||||
"deflate",
|
||||
deflate_extensions,
|
||||
deflate_open,
|
||||
deflate_read,
|
||||
deflate_decode_buffer,
|
||||
deflate_close,
|
||||
};
|
12
src/encoding/deflate.h
Normal file
12
src/encoding/deflate.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef EL__ENCODING_DEFLATE_H
|
||||
#define EL__ENCODING_DEFLATE_H
|
||||
|
||||
#include "encoding/encoding.h"
|
||||
|
||||
#ifdef CONFIG_GZIP
|
||||
extern const struct decoding_backend deflate_decoding_backend;
|
||||
#else
|
||||
#define deflate_decoding_backend dummy_decoding_backend
|
||||
#endif
|
||||
|
||||
#endif
|
@ -84,6 +84,7 @@ static const struct decoding_backend dummy_decoding_backend = {
|
||||
/* Dynamic backend area */
|
||||
|
||||
#include "encoding/bzip2.h"
|
||||
#include "encoding/deflate.h"
|
||||
#include "encoding/gzip.h"
|
||||
#include "encoding/lzma.h"
|
||||
|
||||
@ -92,6 +93,7 @@ static const struct decoding_backend *const decoding_backends[] = {
|
||||
&gzip_decoding_backend,
|
||||
&bzip2_decoding_backend,
|
||||
&lzma_decoding_backend,
|
||||
&deflate_decoding_backend,
|
||||
};
|
||||
|
||||
|
||||
|
@ -9,6 +9,7 @@ enum stream_encoding {
|
||||
ENCODING_GZIP,
|
||||
ENCODING_BZIP2,
|
||||
ENCODING_LZMA,
|
||||
ENCODING_DEFLATE,
|
||||
|
||||
/* Max. number of known encoding including ENCODING_NONE. */
|
||||
ENCODINGS_KNOWN,
|
||||
|
@ -759,7 +759,7 @@ http_send_header(struct socket *socket)
|
||||
add_to_string(&header, ", ");
|
||||
#endif
|
||||
|
||||
add_to_string(&header, "gzip");
|
||||
add_to_string(&header, "gzip, deflate");
|
||||
#endif
|
||||
add_crlf_to_string(&header);
|
||||
#endif
|
||||
@ -1860,7 +1860,9 @@ again:
|
||||
* if it is implied by the extension, so that saving the URI
|
||||
* will leave the saved file with the correct encoding. */
|
||||
#ifdef CONFIG_GZIP
|
||||
if (file_encoding != ENCODING_GZIP
|
||||
if (!strcasecmp(d, "deflate") || !strcasecmp(d, "x-deflate"))
|
||||
conn->content_encoding = ENCODING_DEFLATE;
|
||||
else if (file_encoding != ENCODING_GZIP
|
||||
&& (!strcasecmp(d, "gzip") || !strcasecmp(d, "x-gzip")))
|
||||
conn->content_encoding = ENCODING_GZIP;
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user