2004-09-02 20:22:51 +02:00
|
|
|
/* Copyright 2004 Roger Dingledine */
|
2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
|
2004-09-02 20:22:51 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char torgzip_c_id[] =
|
|
|
|
"$Id$";
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file torgzip.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief A simple in-memory gzip implementation.
|
2004-09-02 20:22:51 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2004-11-04 05:01:19 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#include "..\..\contrib\zlib\zlib.h"
|
|
|
|
#else
|
2004-09-02 20:22:51 +02:00
|
|
|
#include <zlib.h>
|
2004-11-04 05:01:19 +01:00
|
|
|
#endif
|
2005-01-20 00:07:43 +01:00
|
|
|
#include <string.h>
|
2005-01-22 01:35:09 +01:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2005-01-20 21:15:14 +01:00
|
|
|
#include <netinet/in.h>
|
2005-01-22 01:35:09 +01:00
|
|
|
#endif
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "torgzip.h"
|
|
|
|
|
|
|
|
static int gzip_is_supported = -1;
|
|
|
|
|
2005-08-29 20:01:38 +02:00
|
|
|
/** Return true iff we support gzip-based compression. Otherwise, we need to
|
|
|
|
* use zlib. */
|
2004-09-02 20:22:51 +02:00
|
|
|
int
|
|
|
|
is_gzip_supported(void)
|
|
|
|
{
|
|
|
|
if (gzip_is_supported >= 0)
|
|
|
|
return gzip_is_supported;
|
|
|
|
|
|
|
|
if (!strcmpstart(ZLIB_VERSION, "0.") ||
|
|
|
|
!strcmpstart(ZLIB_VERSION, "1.0") ||
|
|
|
|
!strcmpstart(ZLIB_VERSION, "1.1"))
|
|
|
|
gzip_is_supported = 0;
|
|
|
|
else
|
|
|
|
gzip_is_supported = 1;
|
|
|
|
|
|
|
|
return gzip_is_supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE int
|
|
|
|
method_bits(compress_method_t method)
|
|
|
|
{
|
|
|
|
/* Bits+16 means "use gzip" in zlib >= 1.2 */
|
|
|
|
return method == GZIP_METHOD ? 15+16 : 15;
|
|
|
|
}
|
|
|
|
|
2005-08-29 20:01:38 +02:00
|
|
|
/** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
|
|
|
|
* allocated buffer, using the method described in <b>method</b>. Store the
|
|
|
|
* compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
2004-09-02 20:22:51 +02:00
|
|
|
int
|
|
|
|
tor_gzip_compress(char **out, size_t *out_len,
|
2004-10-14 06:24:42 +02:00
|
|
|
const char *in, size_t in_len,
|
2004-09-02 20:22:51 +02:00
|
|
|
compress_method_t method)
|
|
|
|
{
|
|
|
|
struct z_stream_s *stream = NULL;
|
|
|
|
size_t out_size;
|
|
|
|
off_t offset;
|
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(out);
|
|
|
|
tor_assert(out_len);
|
|
|
|
tor_assert(in);
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
if (method == GZIP_METHOD && !is_gzip_supported()) {
|
|
|
|
/* Old zlib version don't support gzip in deflateInit2 */
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Gzip not supported with zlib %s", ZLIB_VERSION);
|
2004-09-02 20:22:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = NULL;
|
|
|
|
|
|
|
|
stream = tor_malloc_zero(sizeof(struct z_stream_s));
|
|
|
|
stream->zalloc = Z_NULL;
|
|
|
|
stream->zfree = Z_NULL;
|
|
|
|
stream->opaque = NULL;
|
|
|
|
stream->next_in = (unsigned char*) in;
|
|
|
|
stream->avail_in = in_len;
|
|
|
|
|
|
|
|
if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
|
2004-10-14 06:24:42 +02:00
|
|
|
method_bits(method),
|
|
|
|
8, Z_DEFAULT_STRATEGY) != Z_OK) {
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Error from deflateInit2: %s",
|
2004-10-14 06:24:42 +02:00
|
|
|
stream->msg?stream->msg:"<no message>");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Guess 50% compression. */
|
|
|
|
out_size = in_len / 2;
|
|
|
|
if (out_size < 1024) out_size = 1024;
|
|
|
|
*out = tor_malloc(out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)*out;
|
2004-09-02 20:22:51 +02:00
|
|
|
stream->avail_out = out_size;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
switch (deflate(stream, Z_FINISH))
|
|
|
|
{
|
|
|
|
case Z_STREAM_END:
|
|
|
|
goto done;
|
|
|
|
case Z_OK:
|
|
|
|
/* In case zlib doesn't work as I think .... */
|
|
|
|
if (stream->avail_out >= stream->avail_in+16)
|
|
|
|
break;
|
|
|
|
case Z_BUF_ERROR:
|
2004-10-14 06:24:42 +02:00
|
|
|
offset = stream->next_out - ((unsigned char*)*out);
|
|
|
|
out_size *= 2;
|
|
|
|
*out = tor_realloc(*out, out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)(*out + offset);
|
2004-10-14 06:24:42 +02:00
|
|
|
stream->avail_out = out_size - offset;
|
|
|
|
break;
|
2004-09-02 20:22:51 +02:00
|
|
|
default:
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Gzip compression didn't finish: %s",
|
2004-09-02 20:22:51 +02:00
|
|
|
stream->msg ? stream->msg : "<no message>");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
*out_len = stream->total_out;
|
|
|
|
if (deflateEnd(stream)!=Z_OK) {
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Error freeing gzip structures");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
tor_free(stream);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
if (stream) {
|
|
|
|
deflateEnd(stream);
|
|
|
|
tor_free(stream);
|
|
|
|
}
|
|
|
|
if (*out) {
|
|
|
|
tor_free(*out);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-08-29 20:01:38 +02:00
|
|
|
/** Given or more zlib-compressed or gzip-compressed strings of total length
|
|
|
|
* <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
|
|
|
|
* buffer, using the method described in <b>method</b>. Store the uncompressed
|
|
|
|
* string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
|
|
|
|
* success, -1 on failure.
|
|
|
|
*/
|
2004-09-02 20:22:51 +02:00
|
|
|
int
|
|
|
|
tor_gzip_uncompress(char **out, size_t *out_len,
|
2004-10-14 06:24:42 +02:00
|
|
|
const char *in, size_t in_len,
|
2005-10-14 00:48:09 +02:00
|
|
|
compress_method_t method,
|
|
|
|
int complete_only)
|
2004-09-02 20:22:51 +02:00
|
|
|
{
|
|
|
|
struct z_stream_s *stream = NULL;
|
|
|
|
size_t out_size;
|
|
|
|
off_t offset;
|
2005-07-13 07:26:43 +02:00
|
|
|
int r;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(out);
|
|
|
|
tor_assert(out_len);
|
|
|
|
tor_assert(in);
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
if (method == GZIP_METHOD && !is_gzip_supported()) {
|
|
|
|
/* Old zlib version don't support gzip in inflateInit2 */
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Gzip not supported with zlib %s", ZLIB_VERSION);
|
2004-09-02 20:22:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = NULL;
|
|
|
|
|
|
|
|
stream = tor_malloc_zero(sizeof(struct z_stream_s));
|
|
|
|
stream->zalloc = Z_NULL;
|
|
|
|
stream->zfree = Z_NULL;
|
|
|
|
stream->opaque = NULL;
|
|
|
|
stream->next_in = (unsigned char*) in;
|
|
|
|
stream->avail_in = in_len;
|
|
|
|
|
|
|
|
if (inflateInit2(stream,
|
|
|
|
method_bits(method)) != Z_OK) {
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Error from inflateInit2: %s",
|
2004-10-14 06:24:42 +02:00
|
|
|
stream->msg?stream->msg:"<no message>");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_size = in_len * 2; /* guess 50% compression. */
|
|
|
|
if (out_size < 1024) out_size = 1024;
|
|
|
|
|
|
|
|
*out = tor_malloc(out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)*out;
|
2004-09-02 20:22:51 +02:00
|
|
|
stream->avail_out = out_size;
|
|
|
|
|
|
|
|
while (1) {
|
2005-10-14 00:48:09 +02:00
|
|
|
switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH))
|
2004-09-02 20:22:51 +02:00
|
|
|
{
|
|
|
|
case Z_STREAM_END:
|
2005-08-29 20:01:38 +02:00
|
|
|
if (stream->avail_in == 0)
|
|
|
|
goto done;
|
2005-10-14 00:48:09 +02:00
|
|
|
/* There may be more compressed data here. */
|
2005-08-29 20:01:38 +02:00
|
|
|
if (inflateInit2(stream, method_bits(method)) != Z_OK) {
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Error from inflateInit2: %s",
|
2005-08-29 20:01:38 +02:00
|
|
|
stream->msg?stream->msg:"<no message>");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2004-09-02 20:22:51 +02:00
|
|
|
case Z_OK:
|
2005-10-14 00:48:09 +02:00
|
|
|
if (!complete_only && stream->avail_in == 0)
|
|
|
|
goto done;
|
2004-10-14 06:24:42 +02:00
|
|
|
/* In case zlib doesn't work as I think.... */
|
|
|
|
if (stream->avail_out >= stream->avail_in+16)
|
|
|
|
break;
|
2004-09-02 20:22:51 +02:00
|
|
|
case Z_BUF_ERROR:
|
2005-10-14 00:48:09 +02:00
|
|
|
if (stream->avail_out > 0) {
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_PROTOCOL, "possible truncated or corrupt zlib data");
|
2005-10-14 00:48:09 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2005-08-29 20:01:38 +02:00
|
|
|
offset = stream->next_out - (unsigned char*)*out;
|
2004-10-14 06:24:42 +02:00
|
|
|
out_size *= 2;
|
|
|
|
*out = tor_realloc(*out, out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)(*out + offset);
|
2004-10-14 06:24:42 +02:00
|
|
|
stream->avail_out = out_size - offset;
|
|
|
|
break;
|
2004-09-02 20:22:51 +02:00
|
|
|
default:
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Gzip decompression returned an error: %s",
|
2004-10-14 06:24:42 +02:00
|
|
|
stream->msg ? stream->msg : "<no message>");
|
|
|
|
goto err;
|
2004-09-02 20:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2005-08-29 20:01:38 +02:00
|
|
|
*out_len = stream->next_out - (unsigned char*)*out;
|
2005-07-13 07:26:43 +02:00
|
|
|
r = inflateEnd(stream);
|
|
|
|
tor_free(stream);
|
|
|
|
if (r != Z_OK) {
|
2005-10-18 23:58:19 +02:00
|
|
|
warn(LD_GENERAL, "Error freeing gzip structures");
|
2005-07-13 07:26:43 +02:00
|
|
|
goto err;
|
2004-09-02 20:22:51 +02:00
|
|
|
}
|
|
|
|
|
2004-09-08 08:47:33 +02:00
|
|
|
/* NUL-terminate output. */
|
|
|
|
if (out_size == *out_len)
|
|
|
|
*out = tor_realloc(*out, out_size + 1);
|
|
|
|
(*out)[*out_len] = '\0';
|
|
|
|
|
2004-09-02 20:22:51 +02:00
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
if (stream) {
|
|
|
|
inflateEnd(stream);
|
|
|
|
tor_free(stream);
|
|
|
|
}
|
|
|
|
if (*out) {
|
|
|
|
tor_free(*out);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-19 23:40:33 +01:00
|
|
|
/** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
|
|
|
|
* to be compressed or not. If it is, return the likeliest compression method.
|
|
|
|
* Otherwise, return 0.
|
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
detect_compression_method(const char *in, size_t in_len)
|
2005-01-19 23:40:33 +01:00
|
|
|
{
|
2005-01-20 00:07:43 +01:00
|
|
|
if (in_len > 2 && !memcmp(in, "\x1f\x8b", 2)) {
|
2005-01-19 23:40:33 +01:00
|
|
|
return GZIP_METHOD;
|
|
|
|
} else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
|
2005-01-20 20:02:35 +01:00
|
|
|
(ntohs(get_uint16(in)) % 31) == 0) {
|
2005-01-19 23:40:33 +01:00
|
|
|
return ZLIB_METHOD;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|