2017-04-18 14:30:44 +02:00
|
|
|
/* Copyright (c) 2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file compress_lzma.c
|
|
|
|
* \brief Compression backend for LZMA.
|
|
|
|
*
|
|
|
|
* This module should never be invoked directly. Use the compress module
|
|
|
|
* instead.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "torlog.h"
|
|
|
|
#include "compress.h"
|
|
|
|
#include "compress_lzma.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
#include <lzma.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-27 20:09:20 +02:00
|
|
|
/** The maximum amount of memory we allow the LZMA decoder to use, in bytes. */
|
|
|
|
#define MEMORY_LIMIT (16 * 1024 * 1024)
|
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
/** Total number of bytes allocated for LZMA state. */
|
2017-04-25 16:29:07 +02:00
|
|
|
static atomic_counter_t total_lzma_allocation;
|
2017-04-18 14:30:44 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_LZMA
|
2017-04-24 14:20:16 +02:00
|
|
|
/** Given <b>level</b> return the memory level. */
|
|
|
|
static int
|
|
|
|
memory_level(compression_level_t level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
default:
|
2017-04-25 15:54:02 +02:00
|
|
|
case BEST_COMPRESSION:
|
2017-04-27 20:09:20 +02:00
|
|
|
case HIGH_COMPRESSION: return 6;
|
|
|
|
case MEDIUM_COMPRESSION: return 4;
|
|
|
|
case LOW_COMPRESSION: return 2;
|
2017-04-24 14:20:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
/** Convert a given <b>error</b> to a human readable error string. */
|
|
|
|
static const char *
|
|
|
|
lzma_error_str(lzma_ret error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case LZMA_OK:
|
|
|
|
return "Operation completed successfully";
|
|
|
|
case LZMA_STREAM_END:
|
|
|
|
return "End of stream";
|
|
|
|
case LZMA_NO_CHECK:
|
|
|
|
return "Input stream lacks integrity check";
|
|
|
|
case LZMA_UNSUPPORTED_CHECK:
|
|
|
|
return "Unable to calculate integrity check";
|
|
|
|
case LZMA_GET_CHECK:
|
|
|
|
return "Integrity check available";
|
|
|
|
case LZMA_MEM_ERROR:
|
|
|
|
return "Unable to allocate memory";
|
|
|
|
case LZMA_MEMLIMIT_ERROR:
|
|
|
|
return "Memory limit reached";
|
|
|
|
case LZMA_FORMAT_ERROR:
|
|
|
|
return "Unknown file format";
|
|
|
|
case LZMA_OPTIONS_ERROR:
|
|
|
|
return "Unsupported options";
|
|
|
|
case LZMA_DATA_ERROR:
|
|
|
|
return "Corrupt input data";
|
|
|
|
case LZMA_BUF_ERROR:
|
|
|
|
return "Unable to progress";
|
|
|
|
case LZMA_PROG_ERROR:
|
|
|
|
return "Programming error";
|
|
|
|
default:
|
|
|
|
return "Unknown LZMA error";
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LZMA) */
|
2017-04-18 14:30:44 +02:00
|
|
|
|
2017-04-20 15:56:38 +02:00
|
|
|
/** Return 1 if LZMA compression is supported; otherwise 0. */
|
|
|
|
int
|
|
|
|
tor_lzma_method_supported(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
/** Return a string representation of the version of the currently running
|
2017-04-20 16:20:14 +02:00
|
|
|
* version of liblzma. Returns NULL if LZMA is unsupported. */
|
2017-04-18 14:30:44 +02:00
|
|
|
const char *
|
|
|
|
tor_lzma_get_version_str(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
return lzma_version_string();
|
|
|
|
#else
|
2017-04-20 16:20:14 +02:00
|
|
|
return NULL;
|
2017-04-18 14:30:44 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:20:14 +02:00
|
|
|
/** Return a string representation of the version of liblzma used at
|
|
|
|
* compilation time. Returns NULL if LZMA is unsupported. */
|
2017-04-18 14:30:44 +02:00
|
|
|
const char *
|
|
|
|
tor_lzma_get_header_version_str(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
return LZMA_VERSION_STRING;
|
|
|
|
#else
|
2017-04-20 16:20:14 +02:00
|
|
|
return NULL;
|
2017-04-18 14:30:44 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Internal LZMA state for incremental compression/decompression.
|
|
|
|
* The body of this struct is not exposed. */
|
|
|
|
struct tor_lzma_compress_state_t {
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
lzma_stream stream; /**< The LZMA stream. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int compress; /**< True if we are compressing; false if we are inflating */
|
|
|
|
|
|
|
|
/** Number of bytes read so far. Used to detect compression bombs. */
|
|
|
|
size_t input_so_far;
|
|
|
|
/** Number of bytes written so far. Used to detect compression bombs. */
|
|
|
|
size_t output_so_far;
|
|
|
|
|
|
|
|
/** Approximate number of bytes allocated for this object. */
|
|
|
|
size_t allocation;
|
|
|
|
};
|
|
|
|
|
2017-04-26 03:03:29 +02:00
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
/** Return an approximate number of bytes stored in memory to hold the LZMA
|
|
|
|
* encoder/decoder state. */
|
|
|
|
static size_t
|
|
|
|
tor_lzma_state_size_precalc(int compress, compression_level_t level)
|
|
|
|
{
|
|
|
|
uint64_t memory_usage;
|
|
|
|
|
|
|
|
if (compress)
|
|
|
|
memory_usage = lzma_easy_encoder_memusage(memory_level(level));
|
|
|
|
else
|
|
|
|
memory_usage = lzma_easy_decoder_memusage(memory_level(level));
|
|
|
|
|
|
|
|
if (memory_usage == UINT64_MAX) {
|
2017-05-17 14:52:07 +02:00
|
|
|
// LCOV_EXCL_START
|
2017-04-26 03:03:29 +02:00
|
|
|
log_warn(LD_GENERAL, "Unsupported compression level passed to LZMA %s",
|
|
|
|
compress ? "encoder" : "decoder");
|
|
|
|
goto err;
|
2017-05-17 14:52:07 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2017-04-26 03:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (memory_usage + sizeof(tor_lzma_compress_state_t) > SIZE_MAX)
|
|
|
|
memory_usage = SIZE_MAX;
|
|
|
|
else
|
|
|
|
memory_usage += sizeof(tor_lzma_compress_state_t);
|
|
|
|
|
|
|
|
return (size_t)memory_usage;
|
|
|
|
|
|
|
|
err:
|
2017-05-17 14:52:07 +02:00
|
|
|
return 0; // LCOV_EXCL_LINE
|
2017-04-26 03:03:29 +02:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LZMA) */
|
2017-04-26 03:03:29 +02:00
|
|
|
|
2017-04-18 14:30:44 +02:00
|
|
|
/** Construct and return a tor_lzma_compress_state_t object using
|
|
|
|
* <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
|
|
|
|
* decompression. */
|
|
|
|
tor_lzma_compress_state_t *
|
|
|
|
tor_lzma_compress_new(int compress,
|
|
|
|
compress_method_t method,
|
2017-04-26 03:03:29 +02:00
|
|
|
compression_level_t level)
|
2017-04-18 14:30:44 +02:00
|
|
|
{
|
|
|
|
tor_assert(method == LZMA_METHOD);
|
|
|
|
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
tor_lzma_compress_state_t *result;
|
|
|
|
lzma_ret retval;
|
|
|
|
lzma_options_lzma stream_options;
|
|
|
|
|
|
|
|
// Note that we do not explicitly initialize the lzma_stream object here,
|
|
|
|
// since the LZMA_STREAM_INIT "just" initializes all members to 0, which is
|
|
|
|
// also what `tor_malloc_zero()` does.
|
|
|
|
result = tor_malloc_zero(sizeof(tor_lzma_compress_state_t));
|
|
|
|
result->compress = compress;
|
2017-04-26 03:03:29 +02:00
|
|
|
result->allocation = tor_lzma_state_size_precalc(compress, level);
|
2017-04-18 14:30:44 +02:00
|
|
|
|
|
|
|
if (compress) {
|
2017-04-26 03:03:29 +02:00
|
|
|
lzma_lzma_preset(&stream_options, memory_level(level));
|
2017-04-18 14:30:44 +02:00
|
|
|
|
|
|
|
retval = lzma_alone_encoder(&result->stream, &stream_options);
|
|
|
|
|
|
|
|
if (retval != LZMA_OK) {
|
2017-05-17 14:52:07 +02:00
|
|
|
// LCOV_EXCL_START
|
2017-04-18 14:30:44 +02:00
|
|
|
log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).",
|
|
|
|
lzma_error_str(retval), retval);
|
|
|
|
goto err;
|
2017-05-17 14:52:07 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2017-04-18 14:30:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-27 20:09:20 +02:00
|
|
|
retval = lzma_alone_decoder(&result->stream, MEMORY_LIMIT);
|
2017-04-18 14:30:44 +02:00
|
|
|
|
|
|
|
if (retval != LZMA_OK) {
|
2017-05-17 14:52:07 +02:00
|
|
|
// LCOV_EXCL_START
|
2017-04-18 14:30:44 +02:00
|
|
|
log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
|
|
|
|
lzma_error_str(retval), retval);
|
|
|
|
goto err;
|
2017-05-17 14:52:07 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2017-04-18 14:30:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 16:29:07 +02:00
|
|
|
atomic_counter_add(&total_lzma_allocation, result->allocation);
|
2017-04-18 14:30:44 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
err:
|
2017-05-17 14:52:07 +02:00
|
|
|
tor_free(result); // LCOV_EXCL_LINE
|
2017-04-18 14:30:44 +02:00
|
|
|
return NULL;
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_LZMA)) */
|
2017-04-18 14:30:44 +02:00
|
|
|
(void)compress;
|
|
|
|
(void)method;
|
2017-04-26 21:00:40 +02:00
|
|
|
(void)level;
|
2017-04-18 14:30:44 +02:00
|
|
|
|
|
|
|
return NULL;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LZMA) */
|
2017-04-18 14:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Compress/decompress some bytes using <b>state</b>. Read up to
|
|
|
|
* *<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_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.
|
|
|
|
*/
|
|
|
|
tor_compress_output_t
|
|
|
|
tor_lzma_compress_process(tor_lzma_compress_state_t *state,
|
|
|
|
char **out, size_t *out_len,
|
|
|
|
const char **in, size_t *in_len,
|
|
|
|
int finish)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
lzma_ret retval;
|
|
|
|
lzma_action action;
|
|
|
|
|
|
|
|
tor_assert(state != NULL);
|
|
|
|
tor_assert(*in_len <= UINT_MAX);
|
|
|
|
tor_assert(*out_len <= UINT_MAX);
|
|
|
|
|
|
|
|
state->stream.next_in = (unsigned char *)*in;
|
|
|
|
state->stream.avail_in = *in_len;
|
|
|
|
state->stream.next_out = (unsigned char *)*out;
|
|
|
|
state->stream.avail_out = *out_len;
|
|
|
|
|
|
|
|
action = finish ? LZMA_FINISH : LZMA_RUN;
|
|
|
|
|
|
|
|
retval = lzma_code(&state->stream, action);
|
|
|
|
|
|
|
|
state->input_so_far += state->stream.next_in - ((unsigned char *)*in);
|
|
|
|
state->output_so_far += state->stream.next_out - ((unsigned char *)*out);
|
|
|
|
|
|
|
|
*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;
|
|
|
|
|
|
|
|
if (! state->compress &&
|
|
|
|
tor_compress_is_compression_bomb(state->input_so_far,
|
|
|
|
state->output_so_far)) {
|
|
|
|
log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
|
|
|
|
return TOR_COMPRESS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (retval) {
|
|
|
|
case LZMA_OK:
|
|
|
|
if (state->stream.avail_out == 0 || finish)
|
|
|
|
return TOR_COMPRESS_BUFFER_FULL;
|
|
|
|
|
|
|
|
return TOR_COMPRESS_OK;
|
|
|
|
|
|
|
|
case LZMA_BUF_ERROR:
|
|
|
|
if (state->stream.avail_in == 0 && !finish)
|
|
|
|
return TOR_COMPRESS_OK;
|
|
|
|
|
|
|
|
return TOR_COMPRESS_BUFFER_FULL;
|
|
|
|
|
|
|
|
case LZMA_STREAM_END:
|
|
|
|
return TOR_COMPRESS_DONE;
|
|
|
|
|
|
|
|
// We list all the possible values of `lzma_ret` here to silence the
|
|
|
|
// `switch-enum` warning and to detect if a new member was added.
|
|
|
|
case LZMA_NO_CHECK:
|
|
|
|
case LZMA_UNSUPPORTED_CHECK:
|
|
|
|
case LZMA_GET_CHECK:
|
|
|
|
case LZMA_MEM_ERROR:
|
|
|
|
case LZMA_MEMLIMIT_ERROR:
|
|
|
|
case LZMA_FORMAT_ERROR:
|
|
|
|
case LZMA_OPTIONS_ERROR:
|
|
|
|
case LZMA_DATA_ERROR:
|
|
|
|
case LZMA_PROG_ERROR:
|
|
|
|
default:
|
|
|
|
log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.",
|
|
|
|
state->compress ? "compression" : "decompression",
|
|
|
|
lzma_error_str(retval));
|
|
|
|
return TOR_COMPRESS_ERROR;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_LZMA)) */
|
2017-04-18 14:30:44 +02:00
|
|
|
(void)state;
|
|
|
|
(void)out;
|
|
|
|
(void)out_len;
|
|
|
|
(void)in;
|
|
|
|
(void)in_len;
|
|
|
|
(void)finish;
|
|
|
|
return TOR_COMPRESS_ERROR;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LZMA) */
|
2017-04-18 14:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Deallocate <b>state</b>. */
|
|
|
|
void
|
|
|
|
tor_lzma_compress_free(tor_lzma_compress_state_t *state)
|
|
|
|
{
|
|
|
|
if (state == NULL)
|
|
|
|
return;
|
|
|
|
|
2017-04-25 16:29:07 +02:00
|
|
|
atomic_counter_sub(&total_lzma_allocation, state->allocation);
|
2017-04-18 14:30:44 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_LZMA
|
|
|
|
lzma_end(&state->stream);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tor_free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the approximate number of bytes allocated for <b>state</b>. */
|
|
|
|
size_t
|
|
|
|
tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state)
|
|
|
|
{
|
|
|
|
tor_assert(state != NULL);
|
|
|
|
return state->allocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the approximate number of bytes allocated for all LZMA states. */
|
|
|
|
size_t
|
|
|
|
tor_lzma_get_total_allocation(void)
|
|
|
|
{
|
2017-04-25 16:29:07 +02:00
|
|
|
return atomic_counter_get(&total_lzma_allocation);
|
2017-04-18 14:30:44 +02:00
|
|
|
}
|
|
|
|
|
2017-04-25 16:29:07 +02:00
|
|
|
/** Initialize the lzma module */
|
|
|
|
void
|
|
|
|
tor_lzma_init(void)
|
|
|
|
{
|
|
|
|
atomic_counter_init(&total_lzma_allocation);
|
|
|
|
}
|
2017-04-25 16:54:34 +02:00
|
|
|
|