2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2016-02-27 18:48:19 +01:00
|
|
|
* Copyright (c) 2007-2016, The Tor Project, Inc. */
|
2004-09-02 20:22:51 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \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>
|
2005-01-20 00:07:43 +01:00
|
|
|
#include <string.h>
|
2011-08-31 02:45:49 +02:00
|
|
|
#include "torint.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"
|
2010-07-10 03:52:20 +02:00
|
|
|
#include "torlog.h"
|
2004-09-02 20:22:51 +02:00
|
|
|
#include "torgzip.h"
|
|
|
|
|
2010-06-09 00:10:20 +02:00
|
|
|
/* zlib 1.2.4 and 1.2.5 do some "clever" things with macros. Instead of
|
|
|
|
saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
|
|
|
|
that nobody will care if the compile outputs a no-such-identifier warning.
|
|
|
|
|
|
|
|
Sorry, but we like -Werror over here, so I guess we need to define these.
|
|
|
|
I hope that zlib 1.2.6 doesn't break these too.
|
|
|
|
*/
|
|
|
|
#ifndef _LARGEFILE64_SOURCE
|
|
|
|
#define _LARGEFILE64_SOURCE 0
|
|
|
|
#endif
|
|
|
|
#ifndef _LFS64_LARGEFILE
|
|
|
|
#define _LFS64_LARGEFILE 0
|
|
|
|
#endif
|
|
|
|
#ifndef _FILE_OFFSET_BITS
|
|
|
|
#define _FILE_OFFSET_BITS 0
|
|
|
|
#endif
|
|
|
|
#ifndef off64_t
|
|
|
|
#define off64_t int64_t
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2016-06-09 00:14:51 +02:00
|
|
|
#if defined ZLIB_VERNUM && ZLIB_VERNUM < 0x1200
|
|
|
|
#error "We require zlib version 1.2 or later."
|
|
|
|
#endif
|
|
|
|
|
2014-08-19 16:59:15 +02:00
|
|
|
static size_t tor_zlib_state_size_precalc(int inflate,
|
|
|
|
int windowbits, int memlevel);
|
|
|
|
|
|
|
|
/** Total number of bytes allocated for zlib state */
|
|
|
|
static size_t total_zlib_allocation = 0;
|
|
|
|
|
2013-09-01 18:38:01 +02:00
|
|
|
/** Return a string representation of the version of the currently running
|
|
|
|
* version of zlib. */
|
|
|
|
const char *
|
|
|
|
tor_zlib_get_version_str(void)
|
|
|
|
{
|
|
|
|
return zlibVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a string representation of the version of the version of zlib
|
|
|
|
* used at compilation. */
|
|
|
|
const char *
|
|
|
|
tor_zlib_get_header_version_str(void)
|
|
|
|
{
|
|
|
|
return ZLIB_VERSION;
|
|
|
|
}
|
|
|
|
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Return the 'bits' value to tell zlib to use <b>method</b>.*/
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2014-11-17 17:43:50 +01:00
|
|
|
method_bits(compress_method_t method, zlib_compression_level_t level)
|
2004-09-02 20:22:51 +02:00
|
|
|
{
|
|
|
|
/* Bits+16 means "use gzip" in zlib >= 1.2 */
|
2014-11-17 17:43:50 +01:00
|
|
|
const int flag = method == GZIP_METHOD ? 16 : 0;
|
|
|
|
switch (level) {
|
|
|
|
default:
|
|
|
|
case HIGH_COMPRESSION: return flag + 15;
|
|
|
|
case MEDIUM_COMPRESSION: return flag + 13;
|
|
|
|
case LOW_COMPRESSION: return flag + 11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2014-11-17 17:43:50 +01:00
|
|
|
get_memlevel(zlib_compression_level_t level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
default:
|
|
|
|
case HIGH_COMPRESSION: return 8;
|
|
|
|
case MEDIUM_COMPRESSION: return 7;
|
|
|
|
case LOW_COMPRESSION: return 6;
|
|
|
|
}
|
2004-09-02 20:22:51 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
2011-01-03 21:54:23 +01:00
|
|
|
/* These macros define the maximum allowable compression factor. Anything of
|
2011-01-15 18:12:10 +01:00
|
|
|
* size greater than CHECK_FOR_COMPRESSION_BOMB_AFTER is not allowed to
|
2011-01-03 21:54:23 +01:00
|
|
|
* have an uncompression factor (uncompressed size:compressed size ratio) of
|
2011-01-15 18:12:10 +01:00
|
|
|
* any greater than MAX_UNCOMPRESSION_FACTOR.
|
|
|
|
*
|
|
|
|
* Picking a value for MAX_UNCOMPRESSION_FACTOR is a trade-off: we want it to
|
|
|
|
* be small to limit the attack multiplier, but we also want it to be large
|
|
|
|
* enough so that no legitimate document --even ones we might invent in the
|
|
|
|
* future -- ever compresses by a factor of greater than
|
|
|
|
* MAX_UNCOMPRESSION_FACTOR. Within those parameters, there's a reasonably
|
|
|
|
* large range of possible values. IMO, anything over 8 is probably safe; IMO
|
|
|
|
* anything under 50 is probably sufficient.
|
|
|
|
*/
|
2011-01-03 21:54:23 +01:00
|
|
|
#define MAX_UNCOMPRESSION_FACTOR 25
|
|
|
|
#define CHECK_FOR_COMPRESSION_BOMB_AFTER (1024*64)
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2011-01-03 21:54:23 +01:00
|
|
|
|
|
|
|
/** Return true if uncompressing an input of size <b>in_size</b> to an input
|
|
|
|
* of size at least <b>size_out</b> looks like a compression bomb. */
|
|
|
|
static int
|
|
|
|
is_compression_bomb(size_t size_in, size_t size_out)
|
|
|
|
{
|
|
|
|
if (size_in == 0 || size_out < CHECK_FOR_COMPRESSION_BOMB_AFTER)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (size_out / size_in > MAX_UNCOMPRESSION_FACTOR);
|
|
|
|
}
|
|
|
|
|
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;
|
2008-02-19 23:05:49 +01:00
|
|
|
size_t out_size, old_size;
|
2004-09-02 20:22:51 +02:00
|
|
|
off_t offset;
|
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(out);
|
|
|
|
tor_assert(out_len);
|
|
|
|
tor_assert(in);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(in_len < UINT_MAX);
|
2004-09-02 20:22:51 +02:00
|
|
|
|
2009-01-13 15:43:51 +01:00
|
|
|
*out = NULL;
|
|
|
|
|
2004-09-02 20:22:51 +02:00
|
|
|
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;
|
2008-02-21 22:57:47 +01:00
|
|
|
stream->avail_in = (unsigned int)in_len;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
|
2014-11-17 17:43:50 +01:00
|
|
|
method_bits(method, HIGH_COMPRESSION),
|
|
|
|
get_memlevel(HIGH_COMPRESSION),
|
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK) {
|
2016-06-09 00:34:33 +02:00
|
|
|
//LCOV_EXCL_START -- we can only provoke failure by giving junk arguments.
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_GENERAL, "Error from deflateInit2: %s",
|
|
|
|
stream->msg?stream->msg:"<no message>");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
2016-06-09 00:34:33 +02:00
|
|
|
//LCOV_EXCL_STOP
|
2004-09-02 20:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2008-02-21 22:57:47 +01:00
|
|
|
stream->avail_out = (unsigned int)out_size;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
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);
|
2008-02-19 23:05:49 +01:00
|
|
|
old_size = out_size;
|
2004-10-14 06:24:42 +02:00
|
|
|
out_size *= 2;
|
2008-02-19 23:05:49 +01:00
|
|
|
if (out_size < old_size) {
|
|
|
|
log_warn(LD_GENERAL, "Size overflow in compression.");
|
|
|
|
goto err;
|
|
|
|
}
|
2004-10-14 06:24:42 +02:00
|
|
|
*out = tor_realloc(*out, out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)(*out + offset);
|
2006-11-14 02:07:52 +01:00
|
|
|
if (out_size - offset > UINT_MAX) {
|
|
|
|
log_warn(LD_BUG, "Ran over unsigned int limit of zlib while "
|
|
|
|
"uncompressing.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
stream->avail_out = (unsigned int)(out_size - offset);
|
2004-10-14 06:24:42 +02:00
|
|
|
break;
|
2004-09-02 20:22:51 +02:00
|
|
|
default:
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_GENERAL, "Gzip compression didn't finish: %s",
|
|
|
|
stream->msg ? stream->msg : "<no message>");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
*out_len = stream->total_out;
|
2009-01-05 00:15:42 +01:00
|
|
|
#ifdef OPENBSD
|
|
|
|
/* "Hey Rocky! Watch me change an unsigned field to a signed field in a
|
|
|
|
* third-party API!"
|
|
|
|
* "Oh, that trick will just make people do unsafe casts to the unsigned
|
|
|
|
* type in their cross-platform code!"
|
|
|
|
* "Don't be foolish. I'm _sure_ they'll have the good sense to make sure
|
|
|
|
* the newly unsigned field isn't negative." */
|
|
|
|
tor_assert(stream->total_out >= 0);
|
|
|
|
#endif
|
2004-09-02 20:22:51 +02:00
|
|
|
if (deflateEnd(stream)!=Z_OK) {
|
2016-06-09 00:34:33 +02:00
|
|
|
// LCOV_EXCL_START -- unreachable if we handled the zlib structure right
|
|
|
|
tor_assert_nonfatal_unreached();
|
2006-09-30 00:33:34 +02:00
|
|
|
log_warn(LD_BUG, "Error freeing gzip structures");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
2016-06-09 00:34:33 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2004-09-02 20:22:51 +02:00
|
|
|
}
|
|
|
|
tor_free(stream);
|
|
|
|
|
2011-01-03 21:54:23 +01:00
|
|
|
if (is_compression_bomb(*out_len, in_len)) {
|
|
|
|
log_warn(LD_BUG, "We compressed something and got an insanely high "
|
|
|
|
"compression factor; other Tors would think this was a zlib bomb.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-09-02 20:22:51 +02:00
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
if (stream) {
|
|
|
|
deflateEnd(stream);
|
|
|
|
tor_free(stream);
|
|
|
|
}
|
2009-12-12 08:07:59 +01:00
|
|
|
tor_free(*out);
|
2004-09-02 20:22:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-02-13 00:34:03 +01:00
|
|
|
/** Given zero or more zlib-compressed or gzip-compressed strings of
|
|
|
|
* total length
|
2005-08-29 20:01:38 +02:00
|
|
|
* <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.
|
2006-09-29 20:13:37 +02:00
|
|
|
*
|
|
|
|
* If <b>complete_only</b> is true, we consider a truncated input as a
|
|
|
|
* failure; otherwise we decompress as much as we can. Warn about truncated
|
|
|
|
* or corrupt inputs at <b>protocol_warn_level</b>.
|
2005-08-29 20:01:38 +02:00
|
|
|
*/
|
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,
|
2006-02-13 00:44:02 +01:00
|
|
|
int complete_only,
|
|
|
|
int protocol_warn_level)
|
2004-09-02 20:22:51 +02:00
|
|
|
{
|
|
|
|
struct z_stream_s *stream = NULL;
|
2008-02-19 23:05:49 +01:00
|
|
|
size_t out_size, old_size;
|
2004-09-02 20:22:51 +02:00
|
|
|
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);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(in_len < UINT_MAX);
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
*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;
|
2008-02-21 22:57:47 +01:00
|
|
|
stream->avail_in = (unsigned int)in_len;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
if (inflateInit2(stream,
|
2014-11-17 17:43:50 +01:00
|
|
|
method_bits(method, HIGH_COMPRESSION)) != Z_OK) {
|
2016-06-09 00:34:33 +02:00
|
|
|
// LCOV_EXCL_START -- can only hit this if we give bad inputs.
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_GENERAL, "Error from inflateInit2: %s",
|
|
|
|
stream->msg?stream->msg:"<no message>");
|
2004-09-02 20:22:51 +02:00
|
|
|
goto err;
|
2016-06-09 00:34:33 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2004-09-02 20:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out_size = in_len * 2; /* guess 50% compression. */
|
|
|
|
if (out_size < 1024) out_size = 1024;
|
2011-01-05 18:42:34 +01:00
|
|
|
if (out_size >= SIZE_T_CEILING || out_size > UINT_MAX)
|
2008-02-21 22:57:47 +01:00
|
|
|
goto err;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
*out = tor_malloc(out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)*out;
|
2008-02-21 22:57:47 +01:00
|
|
|
stream->avail_out = (unsigned int)out_size;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
|
|
|
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. */
|
2006-03-21 21:31:27 +01:00
|
|
|
if ((r = inflateEnd(stream)) != Z_OK) {
|
2006-09-30 00:33:34 +02:00
|
|
|
log_warn(LD_BUG, "Error freeing gzip structures");
|
2006-03-21 21:31:27 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-11-17 17:43:50 +01:00
|
|
|
if (inflateInit2(stream,
|
|
|
|
method_bits(method,HIGH_COMPRESSION)) != Z_OK) {
|
2006-09-30 00:51:47 +02:00
|
|
|
log_warn(LD_GENERAL, "Error from second inflateInit2: %s",
|
2006-02-13 09:01:59 +01:00
|
|
|
stream->msg?stream->msg:"<no message>");
|
2005-08-29 20:01:38 +02:00
|
|
|
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) {
|
2006-02-13 00:44:02 +01:00
|
|
|
log_fn(protocol_warn_level, 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;
|
2008-02-19 23:05:49 +01:00
|
|
|
old_size = out_size;
|
2004-10-14 06:24:42 +02:00
|
|
|
out_size *= 2;
|
2008-02-19 23:05:49 +01:00
|
|
|
if (out_size < old_size) {
|
2011-01-03 21:54:23 +01:00
|
|
|
log_warn(LD_GENERAL, "Size overflow in uncompression.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (is_compression_bomb(in_len, out_size)) {
|
2011-01-15 18:12:10 +01:00
|
|
|
log_warn(LD_GENERAL, "Input looks like a possible zlib bomb; "
|
2011-01-03 21:54:23 +01:00
|
|
|
"not proceeding.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (out_size >= SIZE_T_CEILING) {
|
|
|
|
log_warn(LD_BUG, "Hit SIZE_T_CEILING limit while uncompressing.");
|
2008-02-19 23:05:49 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2004-10-14 06:24:42 +02:00
|
|
|
*out = tor_realloc(*out, out_size);
|
2005-05-07 07:55:06 +02:00
|
|
|
stream->next_out = (unsigned char*)(*out + offset);
|
2006-11-14 02:07:52 +01:00
|
|
|
if (out_size - offset > UINT_MAX) {
|
|
|
|
log_warn(LD_BUG, "Ran over unsigned int limit of zlib while "
|
|
|
|
"uncompressing.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
stream->avail_out = (unsigned int)(out_size - offset);
|
2004-10-14 06:24:42 +02:00
|
|
|
break;
|
2004-09-02 20:22:51 +02:00
|
|
|
default:
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_GENERAL, "Gzip decompression returned an error: %s",
|
|
|
|
stream->msg ? stream->msg : "<no message>");
|
2004-10-14 06:24:42 +02:00
|
|
|
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) {
|
2006-09-30 00:33:34 +02:00
|
|
|
log_warn(LD_BUG, "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.
|
2006-10-09 04:35:51 +02:00
|
|
|
* Otherwise, return UNKNOWN_METHOD.
|
2005-01-19 23:40:33 +01:00
|
|
|
*/
|
2006-10-09 04:35:51 +02:00
|
|
|
compress_method_t
|
2005-09-30 03:09:52 +02:00
|
|
|
detect_compression_method(const char *in, size_t in_len)
|
2005-01-19 23:40:33 +01:00
|
|
|
{
|
2011-05-10 22:58:38 +02:00
|
|
|
if (in_len > 2 && fast_memeq(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 {
|
2006-10-09 04:35:51 +02:00
|
|
|
return UNKNOWN_METHOD;
|
2005-01-19 23:40:33 +01:00
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Internal state for an incremental zlib compression/decompression. The
|
|
|
|
* body of this struct is not exposed. */
|
2006-06-18 09:24:29 +02:00
|
|
|
struct tor_zlib_state_t {
|
2011-03-16 22:05:37 +01:00
|
|
|
struct z_stream_s stream; /**< The zlib stream */
|
|
|
|
int compress; /**< True if we are compressing; false if we are inflating */
|
2011-01-03 21:54:23 +01:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Number of bytes read so far. Used to detect zlib bombs. */
|
2011-01-03 21:54:23 +01:00
|
|
|
size_t input_so_far;
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Number of bytes written so far. Used to detect zlib bombs. */
|
2011-01-03 21:54:23 +01:00
|
|
|
size_t output_so_far;
|
2014-08-19 16:59:15 +02:00
|
|
|
|
|
|
|
/** Approximate number of bytes allocated for this object. */
|
|
|
|
size_t allocation;
|
2006-06-18 09:24:29 +02:00
|
|
|
};
|
|
|
|
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Construct and return a tor_zlib_state_t object using <b>method</b>. If
|
|
|
|
* <b>compress</b>, it's for compression; otherwise it's for
|
2007-02-24 08:50:38 +01:00
|
|
|
* decompression. */
|
2006-06-18 09:24:29 +02:00
|
|
|
tor_zlib_state_t *
|
2016-07-28 16:22:10 +02:00
|
|
|
tor_zlib_new(int compress_, compress_method_t method,
|
2014-11-17 17:43:50 +01:00
|
|
|
zlib_compression_level_t compression_level)
|
2006-06-18 09:24:29 +02:00
|
|
|
{
|
|
|
|
tor_zlib_state_t *out;
|
2014-11-17 17:43:50 +01:00
|
|
|
int bits, memlevel;
|
2006-06-18 09:24:29 +02:00
|
|
|
|
2016-07-28 16:22:10 +02:00
|
|
|
if (! compress_) {
|
2014-11-17 17:43:50 +01:00
|
|
|
/* use this setting for decompression, since we might have the
|
|
|
|
* max number of window bits */
|
|
|
|
compression_level = HIGH_COMPRESSION;
|
|
|
|
}
|
|
|
|
|
2006-06-18 09:24:29 +02:00
|
|
|
out = tor_malloc_zero(sizeof(tor_zlib_state_t));
|
|
|
|
out->stream.zalloc = Z_NULL;
|
|
|
|
out->stream.zfree = Z_NULL;
|
|
|
|
out->stream.opaque = NULL;
|
2016-07-28 16:22:10 +02:00
|
|
|
out->compress = compress_;
|
2014-11-17 17:43:50 +01:00
|
|
|
bits = method_bits(method, compression_level);
|
|
|
|
memlevel = get_memlevel(compression_level);
|
2016-07-28 16:22:10 +02:00
|
|
|
if (compress_) {
|
2006-06-18 09:24:29 +02:00
|
|
|
if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
|
2014-11-17 17:43:50 +01:00
|
|
|
bits, memlevel,
|
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK)
|
2016-06-09 00:34:33 +02:00
|
|
|
goto err; // LCOV_EXCL_LINE
|
2006-06-18 09:24:29 +02:00
|
|
|
} else {
|
2014-08-19 16:59:15 +02:00
|
|
|
if (inflateInit2(&out->stream, bits) != Z_OK)
|
2016-06-09 00:34:33 +02:00
|
|
|
goto err; // LCOV_EXCL_LINE
|
2006-06-18 09:24:29 +02:00
|
|
|
}
|
2016-07-28 16:22:10 +02:00
|
|
|
out->allocation = tor_zlib_state_size_precalc(!compress_, bits, memlevel);
|
2014-08-19 16:59:15 +02:00
|
|
|
|
|
|
|
total_zlib_allocation += out->allocation;
|
|
|
|
|
2006-06-18 09:24:29 +02:00
|
|
|
return out;
|
|
|
|
|
|
|
|
err:
|
|
|
|
tor_free(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-27 22:35:03 +02:00
|
|
|
/** Compress/decompress some bytes using <b>state</b>. Read up to
|
2006-09-29 20:13:37 +02:00
|
|
|
* *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
|
|
|
|
* to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
|
|
|
|
* we've reached the end of the input.
|
|
|
|
*
|
|
|
|
* Return TOR_ZLIB_DONE if we've finished the entire compression/decompression.
|
|
|
|
* Return TOR_ZLIB_OK if we're processed everything from the input.
|
|
|
|
* Return TOR_ZLIB_BUF_FULL if we're out of space on <b>out</b>.
|
|
|
|
* Return TOR_ZLIB_ERR if the stream is corrupt.
|
|
|
|
*/
|
2006-06-18 09:24:29 +02:00
|
|
|
tor_zlib_output_t
|
|
|
|
tor_zlib_process(tor_zlib_state_t *state,
|
|
|
|
char **out, size_t *out_len,
|
|
|
|
const char **in, size_t *in_len,
|
|
|
|
int finish)
|
|
|
|
{
|
|
|
|
int err;
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(*in_len <= UINT_MAX);
|
|
|
|
tor_assert(*out_len <= UINT_MAX);
|
2006-06-18 09:24:29 +02:00
|
|
|
state->stream.next_in = (unsigned char*) *in;
|
2008-02-21 22:57:47 +01:00
|
|
|
state->stream.avail_in = (unsigned int)*in_len;
|
2006-06-18 09:24:29 +02:00
|
|
|
state->stream.next_out = (unsigned char*) *out;
|
2008-02-21 22:57:47 +01:00
|
|
|
state->stream.avail_out = (unsigned int)*out_len;
|
2006-06-18 09:24:29 +02:00
|
|
|
|
|
|
|
if (state->compress) {
|
2014-05-07 16:27:30 +02:00
|
|
|
err = deflate(&state->stream, finish ? Z_FINISH : Z_NO_FLUSH);
|
2006-06-18 09:24:29 +02:00
|
|
|
} else {
|
|
|
|
err = inflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
|
|
|
|
}
|
|
|
|
|
2011-01-03 21:54:23 +01:00
|
|
|
state->input_so_far += state->stream.next_in - ((unsigned char*)*in);
|
|
|
|
state->output_so_far += state->stream.next_out - ((unsigned char*)*out);
|
|
|
|
|
2006-06-18 09:24:29 +02:00
|
|
|
*out = (char*) state->stream.next_out;
|
|
|
|
*out_len = state->stream.avail_out;
|
|
|
|
*in = (const char *) state->stream.next_in;
|
|
|
|
*in_len = state->stream.avail_in;
|
|
|
|
|
2011-01-03 21:54:23 +01:00
|
|
|
if (! state->compress &&
|
|
|
|
is_compression_bomb(state->input_so_far, state->output_so_far)) {
|
|
|
|
log_warn(LD_DIR, "Possible zlib bomb; abandoning stream.");
|
|
|
|
return TOR_ZLIB_ERR;
|
|
|
|
}
|
|
|
|
|
2006-06-18 09:24:29 +02:00
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case Z_STREAM_END:
|
|
|
|
return TOR_ZLIB_DONE;
|
|
|
|
case Z_BUF_ERROR:
|
2014-06-04 02:38:00 +02:00
|
|
|
if (state->stream.avail_in == 0 && !finish)
|
2006-09-29 20:13:37 +02:00
|
|
|
return TOR_ZLIB_OK;
|
2006-06-18 09:24:29 +02:00
|
|
|
return TOR_ZLIB_BUF_FULL;
|
|
|
|
case Z_OK:
|
2006-06-29 13:17:36 +02:00
|
|
|
if (state->stream.avail_out == 0 || finish)
|
2006-06-18 09:24:29 +02:00
|
|
|
return TOR_ZLIB_BUF_FULL;
|
|
|
|
return TOR_ZLIB_OK;
|
|
|
|
default:
|
|
|
|
log_warn(LD_GENERAL, "Gzip returned an error: %s",
|
|
|
|
state->stream.msg ? state->stream.msg : "<no message>");
|
|
|
|
return TOR_ZLIB_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Deallocate <b>state</b>. */
|
2006-06-18 09:24:29 +02:00
|
|
|
void
|
|
|
|
tor_zlib_free(tor_zlib_state_t *state)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!state)
|
|
|
|
return;
|
2006-06-18 09:24:29 +02:00
|
|
|
|
2014-08-19 16:59:15 +02:00
|
|
|
total_zlib_allocation -= state->allocation;
|
|
|
|
|
2006-06-18 09:24:29 +02:00
|
|
|
if (state->compress)
|
|
|
|
deflateEnd(&state->stream);
|
|
|
|
else
|
|
|
|
inflateEnd(&state->stream);
|
|
|
|
|
|
|
|
tor_free(state);
|
|
|
|
}
|
|
|
|
|
2014-08-19 16:59:15 +02:00
|
|
|
/** Return an approximate number of bytes used in RAM to hold a state with
|
|
|
|
* window bits <b>windowBits</b> and compression level 'memlevel' */
|
|
|
|
static size_t
|
2016-07-28 16:22:10 +02:00
|
|
|
tor_zlib_state_size_precalc(int inflate_, int windowbits, int memlevel)
|
2014-08-19 16:59:15 +02:00
|
|
|
{
|
|
|
|
windowbits &= 15;
|
|
|
|
|
|
|
|
#define A_FEW_KILOBYTES 2048
|
|
|
|
|
2016-07-28 16:22:10 +02:00
|
|
|
if (inflate_) {
|
2014-08-19 16:59:15 +02:00
|
|
|
/* From zconf.h:
|
|
|
|
|
|
|
|
"The memory requirements for inflate are (in bytes) 1 << windowBits
|
|
|
|
that is, 32K for windowBits=15 (default value) plus a few kilobytes
|
|
|
|
for small objects."
|
|
|
|
*/
|
|
|
|
return sizeof(tor_zlib_state_t) + sizeof(struct z_stream_s) +
|
|
|
|
(1 << 15) + A_FEW_KILOBYTES;
|
|
|
|
} else {
|
|
|
|
/* Also from zconf.h:
|
|
|
|
|
|
|
|
"The memory requirements for deflate are (in bytes):
|
|
|
|
(1 << (windowBits+2)) + (1 << (memLevel+9))
|
|
|
|
... plus a few kilobytes for small objects."
|
|
|
|
*/
|
|
|
|
return sizeof(tor_zlib_state_t) + sizeof(struct z_stream_s) +
|
|
|
|
(1 << (windowbits + 2)) + (1 << (memlevel + 9)) + A_FEW_KILOBYTES;
|
|
|
|
}
|
|
|
|
#undef A_FEW_KILOBYTES
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the approximate number of bytes allocated for <b>state</b>. */
|
|
|
|
size_t
|
|
|
|
tor_zlib_state_size(const tor_zlib_state_t *state)
|
|
|
|
{
|
|
|
|
return state->allocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the approximate number of bytes allocated for all zlib states. */
|
|
|
|
size_t
|
|
|
|
tor_zlib_get_total_allocation(void)
|
|
|
|
{
|
|
|
|
return total_zlib_allocation;
|
|
|
|
}
|
2014-08-24 19:32:39 +02:00
|
|
|
|