2007-12-12 21:09:01 +00:00
|
|
|
/* Copyright (c) 2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 16:13:17 -04:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2004-09-02 18:22:51 +00:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
2017-04-18 03:21:53 +02:00
|
|
|
* \file compress.c
|
|
|
|
* \brief Common compression API.
|
2004-09-02 18:22:51 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2005-01-19 23:07:43 +00:00
|
|
|
#include <string.h>
|
2011-08-30 20:45:49 -04:00
|
|
|
#include "torint.h"
|
|
|
|
|
2005-01-22 00:35:09 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2005-01-20 20:15:14 +00:00
|
|
|
#include <netinet/in.h>
|
2005-01-22 00:35:09 +00:00
|
|
|
#endif
|
2004-09-02 18:22:51 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
2010-07-09 21:52:20 -04:00
|
|
|
#include "torlog.h"
|
2017-04-18 03:21:53 +02:00
|
|
|
#include "compress.h"
|
2017-04-18 14:30:44 +02:00
|
|
|
#include "compress_lzma.h"
|
2017-04-18 03:14:36 +02:00
|
|
|
#include "compress_zlib.h"
|
2017-04-18 22:19:29 +02:00
|
|
|
#include "compress_zstd.h"
|
2004-09-02 18:22:51 +00:00
|
|
|
|
2011-03-16 17:05:37 -04:00
|
|
|
/** @{ */
|
2011-01-03 15:54:23 -05:00
|
|
|
/* These macros define the maximum allowable compression factor. Anything of
|
2011-01-15 12:12:10 -05:00
|
|
|
* size greater than CHECK_FOR_COMPRESSION_BOMB_AFTER is not allowed to
|
2011-01-03 15:54:23 -05:00
|
|
|
* have an uncompression factor (uncompressed size:compressed size ratio) of
|
2011-01-15 12:12:10 -05: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 15:54:23 -05:00
|
|
|
#define MAX_UNCOMPRESSION_FACTOR 25
|
|
|
|
#define CHECK_FOR_COMPRESSION_BOMB_AFTER (1024*64)
|
2011-03-16 17:05:37 -04:00
|
|
|
/** @} */
|
2011-01-03 15:54:23 -05:00
|
|
|
|
2017-04-18 03:12:19 +02: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. */
|
|
|
|
int
|
|
|
|
tor_compress_is_compression_bomb(size_t size_in, size_t size_out)
|
2011-01-03 15:54:23 -05:00
|
|
|
{
|
|
|
|
if (size_in == 0 || size_out < CHECK_FOR_COMPRESSION_BOMB_AFTER)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (size_out / size_in > MAX_UNCOMPRESSION_FACTOR);
|
|
|
|
}
|
|
|
|
|
2017-04-18 02:23:25 +02:00
|
|
|
/** Given <b>level</b> return the memory level. The memory level is needed for
|
|
|
|
* the various compression backends used in Tor.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
tor_compress_memory_level(compression_level_t level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
default:
|
|
|
|
case HIGH_COMPRESSION: return 8;
|
|
|
|
case MEDIUM_COMPRESSION: return 7;
|
|
|
|
case LOW_COMPRESSION: return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-29 18:01:38 +00: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 18:22:51 +00:00
|
|
|
int
|
2017-04-17 14:29:10 +02:00
|
|
|
tor_compress(char **out, size_t *out_len,
|
|
|
|
const char *in, size_t in_len,
|
|
|
|
compress_method_t method)
|
2004-09-02 18:22:51 +00:00
|
|
|
{
|
2017-04-18 03:14:36 +02:00
|
|
|
if (method == GZIP_METHOD || method == ZLIB_METHOD)
|
|
|
|
return tor_zlib_compress(out, out_len, in, in_len, method);
|
2004-09-02 18:22:51 +00:00
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
if (method == LZMA_METHOD)
|
|
|
|
return tor_lzma_compress(out, out_len, in, in_len, method);
|
|
|
|
|
2017-04-18 22:19:29 +02:00
|
|
|
if (method == ZSTD_METHOD)
|
|
|
|
return tor_zstd_compress(out, out_len, in, in_len, method);
|
|
|
|
|
2004-09-02 18:22:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-02-12 23:34:03 +00:00
|
|
|
/** Given zero or more zlib-compressed or gzip-compressed strings of
|
|
|
|
* total length
|
2005-08-29 18:01:38 +00: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 18:13:37 +00: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 18:01:38 +00:00
|
|
|
*/
|
2004-09-02 18:22:51 +00:00
|
|
|
int
|
2017-04-17 14:29:10 +02:00
|
|
|
tor_uncompress(char **out, size_t *out_len,
|
|
|
|
const char *in, size_t in_len,
|
|
|
|
compress_method_t method,
|
|
|
|
int complete_only,
|
|
|
|
int protocol_warn_level)
|
2004-09-02 18:22:51 +00:00
|
|
|
{
|
2017-04-18 03:14:36 +02:00
|
|
|
if (method == GZIP_METHOD || method == ZLIB_METHOD)
|
|
|
|
return tor_zlib_uncompress(out, out_len, in, in_len,
|
|
|
|
method,
|
|
|
|
complete_only,
|
|
|
|
protocol_warn_level);
|
2004-09-02 18:22:51 +00:00
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
if (method == LZMA_METHOD)
|
|
|
|
return tor_lzma_uncompress(out, out_len, in, in_len,
|
|
|
|
method,
|
|
|
|
complete_only,
|
|
|
|
protocol_warn_level);
|
|
|
|
|
2017-04-18 22:19:29 +02:00
|
|
|
if (method == ZSTD_METHOD)
|
|
|
|
return tor_zstd_uncompress(out, out_len, in, in_len,
|
|
|
|
method,
|
|
|
|
complete_only,
|
|
|
|
protocol_warn_level);
|
|
|
|
|
2004-09-02 18:22:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-19 22:40:33 +00: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 02:35:51 +00:00
|
|
|
* Otherwise, return UNKNOWN_METHOD.
|
2005-01-19 22:40:33 +00:00
|
|
|
*/
|
2006-10-09 02:35:51 +00:00
|
|
|
compress_method_t
|
2005-09-30 01:09:52 +00:00
|
|
|
detect_compression_method(const char *in, size_t in_len)
|
2005-01-19 22:40:33 +00:00
|
|
|
{
|
2011-05-10 16:58:38 -04:00
|
|
|
if (in_len > 2 && fast_memeq(in, "\x1f\x8b", 2)) {
|
2005-01-19 22:40:33 +00:00
|
|
|
return GZIP_METHOD;
|
|
|
|
} else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
|
2005-01-20 19:02:35 +00:00
|
|
|
(ntohs(get_uint16(in)) % 31) == 0) {
|
2005-01-19 22:40:33 +00:00
|
|
|
return ZLIB_METHOD;
|
2017-04-18 14:30:44 +02:00
|
|
|
} else if (in_len > 3 &&
|
|
|
|
fast_memeq(in, "\x5d\x00\x00\x00", 4)) {
|
|
|
|
return LZMA_METHOD;
|
2017-04-18 22:19:29 +02:00
|
|
|
} else if (in_len > 3 &&
|
|
|
|
fast_memeq(in, "\x28\xb5\x2f\xfd", 4)) {
|
|
|
|
return ZSTD_METHOD;
|
2005-01-19 22:40:33 +00:00
|
|
|
} else {
|
2006-10-09 02:35:51 +00:00
|
|
|
return UNKNOWN_METHOD;
|
2005-01-19 22:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-09 19:03:31 +00:00
|
|
|
|
2017-04-20 15:33:13 +02:00
|
|
|
/** Return the approximate number of bytes allocated for all
|
|
|
|
* supported compression schemas. */
|
|
|
|
size_t
|
|
|
|
tor_compress_get_total_allocation(void)
|
|
|
|
{
|
|
|
|
return tor_zlib_get_total_allocation() +
|
|
|
|
tor_lzma_get_total_allocation() +
|
|
|
|
tor_zstd_get_total_allocation();
|
|
|
|
}
|
|
|
|
|
2017-04-17 14:57:37 +02:00
|
|
|
/** Internal state for an incremental compression/decompression. The body of
|
|
|
|
* this struct is not exposed. */
|
|
|
|
struct tor_compress_state_t {
|
2017-04-18 03:14:36 +02:00
|
|
|
compress_method_t method; /**< The compression method. */
|
2011-01-03 15:54:23 -05:00
|
|
|
|
2017-04-18 03:14:36 +02:00
|
|
|
union {
|
|
|
|
tor_zlib_compress_state_t *zlib_state;
|
2017-04-18 14:30:44 +02:00
|
|
|
tor_lzma_compress_state_t *lzma_state;
|
2017-04-18 22:19:29 +02:00
|
|
|
tor_zstd_compress_state_t *zstd_state;
|
2017-04-18 03:14:36 +02:00
|
|
|
} u; /**< Compression backend state. */
|
2006-06-18 07:24:29 +00:00
|
|
|
};
|
|
|
|
|
2017-04-17 14:57:37 +02:00
|
|
|
/** Construct and return a tor_compress_state_t object using <b>method</b>. If
|
|
|
|
* <b>compress</b>, it's for compression; otherwise it's for decompression. */
|
|
|
|
tor_compress_state_t *
|
2017-04-18 03:14:36 +02:00
|
|
|
tor_compress_new(int compress, compress_method_t method,
|
2017-04-17 14:57:37 +02:00
|
|
|
compression_level_t compression_level)
|
2006-06-18 07:24:29 +00:00
|
|
|
{
|
2017-04-18 03:14:36 +02:00
|
|
|
tor_compress_state_t *state;
|
|
|
|
|
|
|
|
state = tor_malloc_zero(sizeof(tor_compress_state_t));
|
|
|
|
state->method = method;
|
|
|
|
|
2017-04-18 03:39:43 +02:00
|
|
|
switch (method) {
|
|
|
|
case GZIP_METHOD:
|
|
|
|
case ZLIB_METHOD: {
|
|
|
|
tor_zlib_compress_state_t *zlib_state =
|
|
|
|
tor_zlib_compress_new(compress, method, compression_level);
|
2017-04-18 03:14:36 +02:00
|
|
|
|
2017-04-18 03:39:43 +02:00
|
|
|
if (zlib_state == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
state->u.zlib_state = zlib_state;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-18 14:30:44 +02:00
|
|
|
case LZMA_METHOD: {
|
|
|
|
tor_lzma_compress_state_t *lzma_state =
|
|
|
|
tor_lzma_compress_new(compress, method, compression_level);
|
|
|
|
|
|
|
|
if (lzma_state == NULL)
|
|
|
|
goto err;
|
2017-04-18 03:14:36 +02:00
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
state->u.lzma_state = lzma_state;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-18 22:19:29 +02:00
|
|
|
case ZSTD_METHOD: {
|
|
|
|
tor_zstd_compress_state_t *zstd_state =
|
|
|
|
tor_zstd_compress_new(compress, method, compression_level);
|
|
|
|
|
|
|
|
if (zstd_state == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
state->u.zstd_state = zstd_state;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-18 03:39:43 +02:00
|
|
|
case NO_METHOD:
|
|
|
|
case UNKNOWN_METHOD:
|
|
|
|
goto err;
|
2017-04-18 03:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2006-06-18 07:24:29 +00:00
|
|
|
|
|
|
|
err:
|
2017-04-18 03:14:36 +02:00
|
|
|
tor_free(state);
|
|
|
|
return NULL;
|
2006-06-18 07:24:29 +00:00
|
|
|
}
|
|
|
|
|
2009-05-27 16:35:03 -04:00
|
|
|
/** Compress/decompress some bytes using <b>state</b>. Read up to
|
2006-09-29 18:13:37 +00: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.
|
|
|
|
*
|
2017-04-17 14:57:37 +02:00
|
|
|
* Return TOR_COMPRESS_DONE if we've finished the entire
|
|
|
|
* compression/decompression.
|
|
|
|
* Return TOR_COMPRESS_OK if we're processed everything from the input.
|
|
|
|
* Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
|
|
|
|
* Return TOR_COMPRESS_ERROR if the stream is corrupt.
|
2006-09-29 18:13:37 +00:00
|
|
|
*/
|
2017-04-17 14:57:37 +02:00
|
|
|
tor_compress_output_t
|
|
|
|
tor_compress_process(tor_compress_state_t *state,
|
|
|
|
char **out, size_t *out_len,
|
|
|
|
const char **in, size_t *in_len,
|
|
|
|
int finish)
|
2006-06-18 07:24:29 +00:00
|
|
|
{
|
2017-04-18 03:14:36 +02:00
|
|
|
tor_assert(state != NULL);
|
2011-01-03 15:54:23 -05:00
|
|
|
|
2017-04-18 03:39:43 +02:00
|
|
|
switch (state->method) {
|
|
|
|
case GZIP_METHOD:
|
|
|
|
case ZLIB_METHOD:
|
|
|
|
return tor_zlib_compress_process(state->u.zlib_state,
|
|
|
|
out, out_len, in, in_len,
|
|
|
|
finish);
|
2017-04-18 14:30:44 +02:00
|
|
|
case LZMA_METHOD:
|
|
|
|
return tor_lzma_compress_process(state->u.lzma_state,
|
|
|
|
out, out_len, in, in_len,
|
|
|
|
finish);
|
2017-04-18 22:19:29 +02:00
|
|
|
case ZSTD_METHOD:
|
|
|
|
return tor_zstd_compress_process(state->u.zstd_state,
|
|
|
|
out, out_len, in, in_len,
|
|
|
|
finish);
|
2017-04-18 03:39:43 +02:00
|
|
|
case NO_METHOD:
|
|
|
|
case UNKNOWN_METHOD:
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2017-04-18 03:14:36 +02:00
|
|
|
return TOR_COMPRESS_ERROR;
|
2006-06-18 07:24:29 +00:00
|
|
|
}
|
|
|
|
|
2006-09-29 18:13:37 +00:00
|
|
|
/** Deallocate <b>state</b>. */
|
2006-06-18 07:24:29 +00:00
|
|
|
void
|
2017-04-17 14:57:37 +02:00
|
|
|
tor_compress_free(tor_compress_state_t *state)
|
2006-06-18 07:24:29 +00:00
|
|
|
{
|
2017-04-18 03:14:36 +02:00
|
|
|
if (state == NULL)
|
2009-09-28 16:37:01 +02:00
|
|
|
return;
|
2006-06-18 07:24:29 +00:00
|
|
|
|
2017-04-18 03:39:43 +02:00
|
|
|
switch (state->method) {
|
|
|
|
case GZIP_METHOD:
|
|
|
|
case ZLIB_METHOD:
|
|
|
|
tor_zlib_compress_free(state->u.zlib_state);
|
|
|
|
break;
|
2017-04-18 14:30:44 +02:00
|
|
|
case LZMA_METHOD:
|
|
|
|
tor_lzma_compress_free(state->u.lzma_state);
|
|
|
|
break;
|
2017-04-18 22:19:29 +02:00
|
|
|
case ZSTD_METHOD:
|
|
|
|
tor_zstd_compress_free(state->u.zstd_state);
|
|
|
|
break;
|
2017-04-18 03:39:43 +02:00
|
|
|
case NO_METHOD:
|
|
|
|
case UNKNOWN_METHOD:
|
|
|
|
break;
|
|
|
|
}
|
2006-06-18 07:24:29 +00:00
|
|
|
|
|
|
|
tor_free(state);
|
|
|
|
}
|
|
|
|
|
2014-08-19 10:59:15 -04:00
|
|
|
/** Return the approximate number of bytes allocated for <b>state</b>. */
|
|
|
|
size_t
|
2017-04-17 14:57:37 +02:00
|
|
|
tor_compress_state_size(const tor_compress_state_t *state)
|
2014-08-19 10:59:15 -04:00
|
|
|
{
|
2017-04-18 03:14:36 +02:00
|
|
|
tor_assert(state != NULL);
|
2014-08-19 10:59:15 -04:00
|
|
|
|
2017-04-18 03:39:43 +02:00
|
|
|
switch (state->method) {
|
|
|
|
case GZIP_METHOD:
|
|
|
|
case ZLIB_METHOD:
|
|
|
|
return tor_zlib_compress_state_size(state->u.zlib_state);
|
2017-04-18 14:30:44 +02:00
|
|
|
case LZMA_METHOD:
|
|
|
|
return tor_lzma_compress_state_size(state->u.lzma_state);
|
2017-04-18 22:19:29 +02:00
|
|
|
case ZSTD_METHOD:
|
|
|
|
return tor_zstd_compress_state_size(state->u.zstd_state);
|
2017-04-18 03:39:43 +02:00
|
|
|
case NO_METHOD:
|
|
|
|
case UNKNOWN_METHOD:
|
|
|
|
goto err;
|
|
|
|
}
|
2017-04-18 03:14:36 +02:00
|
|
|
|
2017-04-18 03:39:43 +02:00
|
|
|
err:
|
2017-04-18 03:14:36 +02:00
|
|
|
return 0;
|
2014-08-19 10:59:15 -04:00
|
|
|
}
|
2014-08-24 13:32:39 -04:00
|
|
|
|