2005-09-15 09:58:31 -04:00
|
|
|
/* Stream reading and decoding (mostly decompression) */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "config/options.h"
|
|
|
|
#include "encoding/encoding.h"
|
|
|
|
#include "network/state.h"
|
|
|
|
#include "osdep/osdep.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
Dummy encoding (ENCODING_NONE)
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
struct dummy_enc_data {
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
dummy_open(struct stream_encoded *stream, int fd)
|
|
|
|
{
|
|
|
|
stream->data = mem_alloc(sizeof(struct dummy_enc_data));
|
|
|
|
if (!stream->data) return -1;
|
|
|
|
|
|
|
|
((struct dummy_enc_data *) stream->data)->fd = fd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dummy_read(struct stream_encoded *stream, unsigned char *data, int len)
|
|
|
|
{
|
|
|
|
return safe_read(((struct dummy_enc_data *) stream->data)->fd, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *
|
|
|
|
dummy_decode_buffer(unsigned char *data, int len, int *new_len)
|
|
|
|
{
|
|
|
|
unsigned char *buffer = memacpy(data, len);
|
|
|
|
|
|
|
|
if (!buffer) return NULL;
|
|
|
|
|
|
|
|
*new_len = len;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dummy_close(struct stream_encoded *stream)
|
|
|
|
{
|
|
|
|
close(((struct dummy_enc_data *) stream->data)->fd);
|
|
|
|
mem_free(stream->data);
|
|
|
|
}
|
|
|
|
|
2007-02-24 14:42:56 -05:00
|
|
|
static const unsigned char *const dummy_extensions[] = { NULL };
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-02-24 14:42:56 -05:00
|
|
|
static const struct decoding_backend dummy_decoding_backend = {
|
2005-09-15 09:58:31 -04:00
|
|
|
"none",
|
|
|
|
dummy_extensions,
|
|
|
|
dummy_open,
|
|
|
|
dummy_read,
|
|
|
|
dummy_decode_buffer,
|
|
|
|
dummy_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Dynamic backend area */
|
|
|
|
|
|
|
|
#include "encoding/bzip2.h"
|
2008-02-11 09:30:14 -05:00
|
|
|
#include "encoding/deflate.h"
|
2006-03-24 06:30:54 -05:00
|
|
|
#include "encoding/lzma.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-02-24 14:42:56 -05:00
|
|
|
static const struct decoding_backend *const decoding_backends[] = {
|
2005-09-15 09:58:31 -04:00
|
|
|
&dummy_decoding_backend,
|
|
|
|
&gzip_decoding_backend,
|
|
|
|
&bzip2_decoding_backend,
|
2006-03-24 06:30:54 -05:00
|
|
|
&lzma_decoding_backend,
|
2008-02-11 09:30:14 -05:00
|
|
|
&deflate_decoding_backend,
|
2005-09-15 09:58:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
Public functions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* Associates encoded stream with a fd. */
|
|
|
|
struct stream_encoded *
|
|
|
|
open_encoded(int fd, enum stream_encoding encoding)
|
|
|
|
{
|
|
|
|
struct stream_encoded *stream;
|
|
|
|
|
|
|
|
stream = mem_alloc(sizeof(*stream));
|
|
|
|
if (!stream) return NULL;
|
|
|
|
|
|
|
|
stream->encoding = encoding;
|
|
|
|
if (decoding_backends[stream->encoding]->open(stream, fd) >= 0)
|
|
|
|
return stream;
|
|
|
|
|
|
|
|
mem_free(stream);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read available data from stream and decode them. Note that when data change
|
|
|
|
* their size during decoding, 'len' indicates desired size of _returned_ data,
|
|
|
|
* not desired size of data read from stream. */
|
|
|
|
int
|
|
|
|
read_encoded(struct stream_encoded *stream, unsigned char *data, int len)
|
|
|
|
{
|
|
|
|
return decoding_backends[stream->encoding]->read(stream, data, len);
|
|
|
|
}
|
|
|
|
|
2007-02-24 14:35:29 -05:00
|
|
|
/* Decode an entire file from a buffer. This function is not suitable
|
|
|
|
* for parts of files. @data contains the original data, @len bytes
|
|
|
|
* long. The resulting decoded data chunk is *@new_len bytes long. */
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char *
|
|
|
|
decode_encoded_buffer(enum stream_encoding encoding, unsigned char *data, int len,
|
|
|
|
int *new_len)
|
|
|
|
{
|
|
|
|
return decoding_backends[encoding]->decode_buffer(data, len, new_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Closes encoded stream. Note that fd associated with the stream will be
|
|
|
|
* closed here. */
|
|
|
|
void
|
|
|
|
close_encoded(struct stream_encoded *stream)
|
|
|
|
{
|
|
|
|
decoding_backends[stream->encoding]->close(stream);
|
|
|
|
mem_free(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return a list of extensions associated with that encoding. */
|
2007-02-24 14:42:56 -05:00
|
|
|
const unsigned char *const *listext_encoded(enum stream_encoding encoding)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
return decoding_backends[encoding]->extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum stream_encoding
|
|
|
|
guess_encoding(unsigned char *filename)
|
|
|
|
{
|
|
|
|
int fname_len = strlen(filename);
|
|
|
|
unsigned char *fname_end = filename + fname_len;
|
|
|
|
int enc;
|
|
|
|
|
|
|
|
for (enc = 1; enc < ENCODINGS_KNOWN; enc++) {
|
2007-02-24 14:42:56 -05:00
|
|
|
const unsigned char *const *ext = decoding_backends[enc]->extensions;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
while (ext && *ext) {
|
|
|
|
int len = strlen(*ext);
|
|
|
|
|
|
|
|
if (fname_len >= len && !strcmp(fname_end - len, *ext))
|
|
|
|
return enc;
|
|
|
|
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ENCODING_NONE;
|
|
|
|
}
|
|
|
|
|
2007-02-24 14:42:56 -05:00
|
|
|
const unsigned char *
|
2005-09-15 09:58:31 -04:00
|
|
|
get_encoding_name(enum stream_encoding encoding)
|
|
|
|
{
|
|
|
|
return decoding_backends[encoding]->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* File reading */
|
|
|
|
|
|
|
|
/* Tries to open @prefixname with each of the supported encoding extensions
|
|
|
|
* appended. */
|
|
|
|
static inline enum stream_encoding
|
|
|
|
try_encoding_extensions(struct string *filename, int *fd)
|
|
|
|
{
|
|
|
|
int length = filename->length;
|
|
|
|
int encoding;
|
|
|
|
|
|
|
|
/* No file of that name was found, try some others names. */
|
|
|
|
for (encoding = 1; encoding < ENCODINGS_KNOWN; encoding++) {
|
2007-02-24 14:42:56 -05:00
|
|
|
const unsigned char *const *ext = listext_encoded(encoding);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
for (; ext && *ext; ext++) {
|
|
|
|
add_to_string(filename, *ext);
|
|
|
|
|
|
|
|
/* We try with some extensions. */
|
|
|
|
*fd = open(filename->source, O_RDONLY | O_NOCTTY);
|
|
|
|
|
|
|
|
if (*fd >= 0)
|
|
|
|
/* Ok, found one, use it. */
|
|
|
|
return encoding;
|
|
|
|
|
|
|
|
filename->source[length] = 0;
|
|
|
|
filename->length = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ENCODING_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reads the file from @stream in chunks of size @readsize. */
|
|
|
|
/* Returns a connection state. S_OK if all is well. */
|
|
|
|
enum connection_state
|
|
|
|
read_file(struct stream_encoded *stream, int readsize, struct string *page)
|
|
|
|
{
|
|
|
|
if (!init_string(page)) return S_OUT_OF_MEM;
|
|
|
|
|
|
|
|
/* We read with granularity of stt.st_size (given as @readsize) - this
|
|
|
|
* does best job for uncompressed files, and doesn't hurt for
|
|
|
|
* compressed ones anyway - very large files usually tend to inflate
|
|
|
|
* fast anyway. At least I hope ;). --pasky */
|
|
|
|
/* Also there because of bug in Linux. Read returns -EACCES when
|
|
|
|
* reading 0 bytes to invalid address so ensure never to try and
|
|
|
|
* allocate zero number of bytes. */
|
|
|
|
if (!readsize) readsize = 4096;
|
|
|
|
|
|
|
|
while (realloc_string(page, page->length + readsize)) {
|
|
|
|
unsigned char *string_pos = page->source + page->length;
|
|
|
|
int readlen = read_encoded(stream, string_pos, readsize);
|
|
|
|
|
|
|
|
if (readlen < 0) {
|
|
|
|
done_string(page);
|
|
|
|
|
|
|
|
/* If it is some I/O error (and errno is set) that will
|
|
|
|
* do. Since errno == 0 == S_WAIT and we cannot have
|
|
|
|
* that. */
|
|
|
|
if (errno)
|
|
|
|
return (enum connection_state) -errno;
|
|
|
|
|
|
|
|
/* FIXME: This is indeed an internal error. If readed from a
|
|
|
|
* corrupted encoded file nothing or only some of the
|
|
|
|
* data will be read. */
|
|
|
|
return S_ENCODE_ERROR;
|
|
|
|
|
|
|
|
} else if (readlen == 0) {
|
|
|
|
/* NUL-terminate just in case */
|
|
|
|
page->source[page->length] = '\0';
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
page->length += readlen;
|
|
|
|
#if 0
|
|
|
|
/* This didn't work so well as it should (I had to implement
|
|
|
|
* end of stream handling to bzip2 anyway), so I rather
|
|
|
|
* disabled this. */
|
|
|
|
if (readlen < readsize) {
|
|
|
|
/* This is much safer. It should always mean that we
|
|
|
|
* already read everything possible, and it permits us
|
|
|
|
* more elegant of handling end of file with bzip2. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
done_string(page);
|
|
|
|
return S_OUT_OF_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
is_stdin_pipe(struct stat *stt, struct string *filename)
|
|
|
|
{
|
|
|
|
/* On Mac OS X, /dev/stdin has type S_IFSOCK. (bug 616) */
|
|
|
|
return !strlcmp(filename->source, filename->length, "/dev/stdin", 10)
|
|
|
|
&& (
|
|
|
|
#ifdef S_ISSOCK
|
|
|
|
S_ISSOCK(stt->st_mode) ||
|
|
|
|
#endif
|
|
|
|
S_ISFIFO(stt->st_mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
enum connection_state
|
|
|
|
read_encoded_file(struct string *filename, struct string *page)
|
|
|
|
{
|
|
|
|
struct stream_encoded *stream;
|
|
|
|
struct stat stt;
|
|
|
|
enum stream_encoding encoding = ENCODING_NONE;
|
|
|
|
int fd = open(filename->source, O_RDONLY | O_NOCTTY);
|
|
|
|
enum connection_state state = -errno;
|
|
|
|
|
2007-08-28 12:41:18 -04:00
|
|
|
if (fd == -1 && get_opt_bool("protocol.file.try_encoding_extensions", NULL)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
encoding = try_encoding_extensions(filename, &fd);
|
|
|
|
|
|
|
|
} else if (fd != -1) {
|
|
|
|
encoding = guess_encoding(filename->source);
|
|
|
|
}
|
|
|
|
|
2006-05-05 15:12:10 -04:00
|
|
|
if (fd == -1)
|
2005-09-15 09:58:31 -04:00
|
|
|
return state;
|
|
|
|
|
|
|
|
/* Some file was opened so let's get down to bi'ness */
|
|
|
|
set_bin(fd);
|
|
|
|
|
|
|
|
/* Do all the necessary checks before trying to read the file.
|
|
|
|
* @state code is used to block further progress. */
|
|
|
|
if (fstat(fd, &stt)) {
|
|
|
|
state = -errno;
|
|
|
|
|
|
|
|
} else if (!S_ISREG(stt.st_mode) && encoding != ENCODING_NONE) {
|
|
|
|
/* We only want to open regular encoded files. */
|
|
|
|
/* Leave @state being the saved errno */
|
|
|
|
|
|
|
|
} else if (!S_ISREG(stt.st_mode) && !is_stdin_pipe(&stt, filename)
|
2007-08-28 12:41:18 -04:00
|
|
|
&& !get_opt_bool("protocol.file.allow_special_files", NULL)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
state = S_FILE_TYPE;
|
|
|
|
|
|
|
|
} else if (!(stream = open_encoded(fd, encoding))) {
|
|
|
|
state = S_OUT_OF_MEM;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int readsize = (int) stt.st_size;
|
|
|
|
|
|
|
|
/* Check if st_size will cause overflow. */
|
|
|
|
/* FIXME: See bug 497 for info about support for big files. */
|
|
|
|
if (readsize != stt.st_size || readsize < 0) {
|
|
|
|
#ifdef EFBIG
|
|
|
|
state = (enum connection_state) -(EFBIG);
|
|
|
|
#else
|
|
|
|
state = S_FILE_ERROR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} else {
|
|
|
|
state = read_file(stream, stt.st_size, page);
|
|
|
|
}
|
|
|
|
close_encoded(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return state;
|
|
|
|
}
|
2008-02-18 08:30:49 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
accept_encoding_header(struct string *header)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_GZIP) || defined(CONFIG_BZIP2) || defined(CONFIG_LZMA)
|
|
|
|
int comma = 0;
|
|
|
|
|
|
|
|
add_to_string(header, "Accept-Encoding: ");
|
|
|
|
|
|
|
|
#ifdef CONFIG_BZIP2
|
|
|
|
add_to_string(header, "bzip2");
|
|
|
|
comma = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_GZIP
|
|
|
|
if (comma) add_to_string(header, ", ");
|
|
|
|
add_to_string(header, "deflate, gzip");
|
|
|
|
comma = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_LZMA
|
|
|
|
if (comma) add_to_string(header, ", ");
|
|
|
|
add_to_string(header, "lzma");
|
|
|
|
#endif
|
|
|
|
add_crlf_to_string(header);
|
|
|
|
#endif
|
|
|
|
}
|