content_encoding.c [plain text]
#include "setup.h"
#ifdef HAVE_LIBZ
#include <stdlib.h>
#include <string.h>
#include "urldata.h"
#include <curl/curl.h>
#include "sendf.h"
#include "content_encoding.h"
#include "memory.h"
#include "memdebug.h"
#define OLD_ZLIB_SUPPORT 1
#define DSIZ 0x10000
#define GZIP_MAGIC_0 0x1f
#define GZIP_MAGIC_1 0x8b
#define ASCII_FLAG 0x01
#define HEAD_CRC 0x02
#define EXTRA_FIELD 0x04
#define ORIG_NAME 0x08
#define COMMENT 0x10
#define RESERVED 0xE0
enum zlibState {
ZLIB_UNINIT,
ZLIB_INIT,
ZLIB_GZIP_HEADER,
ZLIB_GZIP_INFLATING,
ZLIB_INIT_GZIP
};
static CURLcode
process_zlib_error(struct SessionHandle *data,
z_stream *z)
{
if (z->msg)
failf (data, "Error while processing content unencoding: %s",
z->msg);
else
failf (data, "Error while processing content unencoding: "
"Unknown failure within decompression software.");
return CURLE_BAD_CONTENT_ENCODING;
}
static CURLcode
exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
{
inflateEnd(z);
*zlib_init = ZLIB_UNINIT;
return result;
}
static CURLcode
inflate_stream(struct SessionHandle *data,
struct Curl_transfer_keeper *k)
{
z_stream *z = &k->z;
int status;
CURLcode result = CURLE_OK;
char *decomp;
decomp = (char*)malloc(DSIZ);
if (decomp == NULL) {
return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
}
for (;;) {
z->next_out = (Bytef *)decomp;
z->avail_out = DSIZ;
status = inflate(z, Z_SYNC_FLUSH);
if (status == Z_OK || status == Z_STREAM_END) {
if(DSIZ - z->avail_out) {
result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
DSIZ - z->avail_out);
if (result) {
free(decomp);
return exit_zlib(z, &k->zlib_init, result);
}
}
if (status == Z_STREAM_END) {
free(decomp);
if (inflateEnd(z) == Z_OK)
return exit_zlib(z, &k->zlib_init, result);
else
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
}
if (status == Z_OK && z->avail_in == 0) {
free(decomp);
return result;
}
}
else {
free(decomp);
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
}
}
}
CURLcode
Curl_unencode_deflate_write(struct SessionHandle *data,
struct Curl_transfer_keeper *k,
ssize_t nread)
{
z_stream *z = &k->z;
if (k->zlib_init == ZLIB_UNINIT) {
z->zalloc = (alloc_func)Z_NULL;
z->zfree = (free_func)Z_NULL;
z->opaque = 0;
z->next_in = NULL;
z->avail_in = 0;
if (inflateInit(z) != Z_OK)
return process_zlib_error(data, z);
k->zlib_init = ZLIB_INIT;
}
z->next_in = (Bytef *)k->str;
z->avail_in = (uInt)nread;
return inflate_stream(data, k);
}
#ifdef OLD_ZLIB_SUPPORT
static enum {
GZIP_OK,
GZIP_BAD,
GZIP_UNDERFLOW
} check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
{
int method, flags;
const ssize_t totallen = len;
if (len < 10)
return GZIP_UNDERFLOW;
if ((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
return GZIP_BAD;
method = data[2];
flags = data[3];
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
return GZIP_BAD;
}
len -= 10;
data += 10;
if (flags & EXTRA_FIELD) {
ssize_t extra_len;
if (len < 2)
return GZIP_UNDERFLOW;
extra_len = (data[1] << 8) | data[0];
if (len < (extra_len+2))
return GZIP_UNDERFLOW;
len -= (extra_len + 2);
data += (extra_len + 2);
}
if (flags & ORIG_NAME) {
while (len && *data) {
--len;
++data;
}
if (!len || *data)
return GZIP_UNDERFLOW;
--len;
++data;
}
if (flags & COMMENT) {
while (len && *data) {
--len;
++data;
}
if (!len || *data)
return GZIP_UNDERFLOW;
--len;
++data;
}
if (flags & HEAD_CRC) {
if (len < 2)
return GZIP_UNDERFLOW;
len -= 2;
data += 2;
}
*headerlen = totallen - len;
return GZIP_OK;
}
#endif
CURLcode
Curl_unencode_gzip_write(struct SessionHandle *data,
struct Curl_transfer_keeper *k,
ssize_t nread)
{
z_stream *z = &k->z;
if (k->zlib_init == ZLIB_UNINIT) {
z->zalloc = (alloc_func)Z_NULL;
z->zfree = (free_func)Z_NULL;
z->opaque = 0;
z->next_in = NULL;
z->avail_in = 0;
if (strcmp(zlibVersion(), "1.2.0.4") >= 0) {
if (inflateInit2(z, MAX_WBITS+32) != Z_OK) {
return process_zlib_error(data, z);
}
k->zlib_init = ZLIB_INIT_GZIP;
} else {
if (inflateInit2(z, -MAX_WBITS) != Z_OK) {
return process_zlib_error(data, z);
}
k->zlib_init = ZLIB_INIT;
}
}
if (k->zlib_init == ZLIB_INIT_GZIP) {
z->next_in = (Bytef *)k->str;
z->avail_in = (uInt)nread;
return inflate_stream(data, k);
}
#ifndef OLD_ZLIB_SUPPORT
return exit_zlib(z, &k->zlib_init, CURLE_FUNCTION_NOT_FOUND);
#else
switch (k->zlib_init) {
case ZLIB_INIT:
{
ssize_t hlen;
switch (check_gzip_header((unsigned char *)k->str, nread, &hlen)) {
case GZIP_OK:
z->next_in = (Bytef *)k->str + hlen;
z->avail_in = (uInt)(nread - hlen);
k->zlib_init = ZLIB_GZIP_INFLATING;
break;
case GZIP_UNDERFLOW:
z->avail_in = (uInt)nread;
z->next_in = malloc(z->avail_in);
if (z->next_in == NULL) {
return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
}
memcpy(z->next_in, k->str, z->avail_in);
k->zlib_init = ZLIB_GZIP_HEADER;
return CURLE_OK;
case GZIP_BAD:
default:
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
}
}
break;
case ZLIB_GZIP_HEADER:
{
ssize_t hlen;
unsigned char *oldblock = z->next_in;
z->avail_in += nread;
z->next_in = realloc(z->next_in, z->avail_in);
if (z->next_in == NULL) {
free(oldblock);
return exit_zlib(z, &k->zlib_init, CURLE_OUT_OF_MEMORY);
}
memcpy(z->next_in + z->avail_in - nread, k->str, nread);
switch (check_gzip_header(z->next_in, z->avail_in, &hlen)) {
case GZIP_OK:
free(z->next_in);
z->next_in = (Bytef *)k->str + hlen + nread - z->avail_in;
z->avail_in = (uInt)(z->avail_in - hlen);
k->zlib_init = ZLIB_GZIP_INFLATING;
break;
case GZIP_UNDERFLOW:
return CURLE_OK;
case GZIP_BAD:
default:
free(z->next_in);
return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
}
}
break;
case ZLIB_GZIP_INFLATING:
default:
z->next_in = (Bytef *)k->str;
z->avail_in = (uInt)nread;
break;
}
if (z->avail_in == 0) {
return CURLE_OK;
}
return inflate_stream(data, k);
#endif
}
#endif