Remove tor_compress_memory_level().

This patch splits up `tor_compress_memory_level()` into static functions
in the individual compression backends, which allows us to tune the
values per compression backend rather than globally.

See: https://bugs.torproject.org/21662
This commit is contained in:
Alexander Færøy 2017-04-24 14:20:16 +02:00 committed by Nick Mathewson
parent 69a41e8bc6
commit cf912259ba
5 changed files with 44 additions and 23 deletions

View File

@ -56,20 +56,6 @@ tor_compress_is_compression_bomb(size_t size_in, size_t size_out)
return (size_out / size_in > MAX_UNCOMPRESSION_FACTOR);
}
/** 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;
}
}
/** 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>.

View File

@ -46,9 +46,6 @@ tor_uncompress(char **out, size_t *out_len,
compress_method_t detect_compression_method(const char *in, size_t in_len);
int
tor_compress_memory_level(compression_level_t level);
int
tor_compress_is_compression_bomb(size_t size_in, size_t size_out);

View File

@ -26,6 +26,18 @@
static size_t total_lzma_allocation = 0;
#ifdef HAVE_LZMA
/** Given <b>level</b> return the memory level. */
static int
memory_level(compression_level_t level)
{
switch (level) {
default:
case HIGH_COMPRESSION: return 9;
case MEDIUM_COMPRESSION: return 6;
case LOW_COMPRESSION: return 3;
}
}
/** Convert a given <b>error</b> to a human readable error string. */
static const char *
lzma_error_str(lzma_ret error)
@ -124,7 +136,7 @@ tor_lzma_compress(char **out, size_t *out_len,
stream.avail_in = in_len;
lzma_lzma_preset(&stream_options,
tor_compress_memory_level(HIGH_COMPRESSION));
memory_level(HIGH_COMPRESSION));
retval = lzma_alone_encoder(&stream, &stream_options);
@ -432,7 +444,7 @@ tor_lzma_compress_new(int compress,
if (compress) {
lzma_lzma_preset(&stream_options,
tor_compress_memory_level(compression_level));
memory_level(compression_level));
retval = lzma_alone_encoder(&result->stream, &stream_options);

View File

@ -50,6 +50,18 @@ static size_t tor_zlib_state_size_precalc(int inflate,
/** Total number of bytes allocated for zlib state */
static size_t total_zlib_allocation = 0;
/** Given <b>level</b> return the memory level. */
static int
memory_level(compression_level_t level)
{
switch (level) {
default:
case HIGH_COMPRESSION: return 8;
case MEDIUM_COMPRESSION: return 7;
case LOW_COMPRESSION: return 6;
}
}
/** Return the 'bits' value to tell zlib to use <b>method</b>.*/
static inline int
method_bits(compress_method_t method, compression_level_t level)
@ -120,7 +132,7 @@ tor_zlib_compress(char **out, size_t *out_len,
if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
method_bits(method, HIGH_COMPRESSION),
tor_compress_memory_level(HIGH_COMPRESSION),
memory_level(HIGH_COMPRESSION),
Z_DEFAULT_STRATEGY) != Z_OK) {
//LCOV_EXCL_START -- we can only provoke failure by giving junk arguments.
log_warn(LD_GENERAL, "Error from deflateInit2: %s",
@ -413,7 +425,7 @@ tor_zlib_compress_new(int compress,
out->stream.opaque = NULL;
out->compress = compress;
bits = method_bits(method, compression_level);
memlevel = tor_compress_memory_level(compression_level);
memlevel = memory_level(compression_level);
if (compress) {
if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
bits, memlevel,

View File

@ -26,6 +26,20 @@
/** Total number of bytes allocated for Zstandard state. */
static size_t total_zstd_allocation = 0;
#ifdef HAVE_ZSTD
/** Given <b>level</b> return the memory level. */
static int
memory_level(compression_level_t level)
{
switch (level) {
default:
case HIGH_COMPRESSION: return 9;
case MEDIUM_COMPRESSION: return 8;
case LOW_COMPRESSION: return 7;
}
}
#endif // HAVE_ZSTD.
/** Return 1 if Zstandard compression is supported; otherwise 0. */
int
tor_zstd_method_supported(void)
@ -104,7 +118,7 @@ tor_zstd_compress(char **out, size_t *out_len,
}
retval = ZSTD_initCStream(stream,
tor_compress_memory_level(HIGH_COMPRESSION));
memory_level(HIGH_COMPRESSION));
if (ZSTD_isError(retval)) {
log_warn(LD_GENERAL, "Zstandard stream initialization error: %s",
@ -408,7 +422,7 @@ tor_zstd_compress_new(int compress,
}
retval = ZSTD_initCStream(result->u.compress_stream,
tor_compress_memory_level(compression_level));
memory_level(compression_level));
if (ZSTD_isError(retval)) {
log_warn(LD_GENERAL, "Zstandard stream initialization error: %s",