Treat the identity transformation as another kind of compression.

This will allow us to treat NO_METHOD as a real compression method,
and to simplify code that currently does

   if (compressing) {
      compress
   } else {
      copy
   }
This commit is contained in:
Nick Mathewson 2017-04-27 10:00:41 -04:00
parent 49deb1e1b8
commit 1bc21111d8
4 changed files with 89 additions and 3 deletions

View File

@ -24,6 +24,7 @@
#include "torlog.h"
#include "compress.h"
#include "compress_lzma.h"
#include "compress_none.h"
#include "compress_zlib.h"
#include "compress_zstd.h"
@ -67,8 +68,12 @@ guess_compress_size(int compress, compress_method_t method,
size_t in_len)
{
// ignore these for now.
(void)method;
(void)compression_level;
if (method == NO_METHOD) {
/* Guess that we'll need an extra byte, to avoid a needless realloc
* for nul-termination */
return (in_len < SIZE_MAX) ? in_len + 1 : in_len;
}
/* Always guess a factor of 2. */
if (compress) {
@ -279,6 +284,7 @@ tor_compress_supports_method(compress_method_t method)
case ZSTD_METHOD:
return tor_zstd_method_supported();
case NO_METHOD:
return 1;
case UNKNOWN_METHOD:
default:
return 0;
@ -436,7 +442,9 @@ tor_compress_new(int compress, compress_method_t method,
state->u.zstd_state = zstd_state;
break;
}
case NO_METHOD:
case NO_METHOD: {
break;
}
case UNKNOWN_METHOD:
goto err;
}
@ -484,6 +492,8 @@ tor_compress_process(tor_compress_state_t *state,
out, out_len, in, in_len,
finish);
case NO_METHOD:
return tor_cnone_compress_process(out, out_len, in, in_len,
finish);
case UNKNOWN_METHOD:
goto err;
}
@ -511,6 +521,7 @@ tor_compress_free(tor_compress_state_t *state)
tor_zstd_compress_free(state->u.zstd_state);
break;
case NO_METHOD:
break;
case UNKNOWN_METHOD:
break;
}

View File

@ -0,0 +1,53 @@
/* 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 identity compression.
*
* We actually define this backend so that we can treat the identity transform
* as another case of compression.
*
* 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_none.h"
/** Transfer some bytes using the identity transformation. 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_cnone_compress_process(char **out, size_t *out_len,
const char **in, size_t *in_len,
int finish)
{
size_t n_to_copy = MIN(*in_len, *out_len);
memcpy(*out, *in, n_to_copy);
*out += n_to_copy;
*in += n_to_copy;
*out_len -= n_to_copy;
*in_len -= n_to_copy;
if (*in_len == 0) {
return finish ? TOR_COMPRESS_DONE : TOR_COMPRESS_OK;
} else {
return TOR_COMPRESS_BUFFER_FULL;
}
}

View File

@ -0,0 +1,20 @@
/* Copyright (c) 2003, 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_none.h
* \brief Header for compress_none.c
**/
#ifndef TOR_COMPRESS_NONE_H
#define TOR_COMPRESS_NONE_H
tor_compress_output_t
tor_cnone_compress_process(char **out, size_t *out_len,
const char **in, size_t *in_len,
int finish);
#endif // TOR_COMPRESS_NONE_H.

View File

@ -107,6 +107,7 @@ LIBOR_CRYPTO_A_SRC = \
src/common/aes.c \
src/common/compress.c \
src/common/compress_lzma.c \
src/common/compress_none.c \
src/common/compress_zlib.c \
src/common/compress_zstd.c \
src/common/crypto.c \
@ -148,8 +149,9 @@ COMMONHEADERS = \
src/common/compat_openssl.h \
src/common/compat_threads.h \
src/common/compat_time.h \
src/common/compress.h \
src/common/compress.h \
src/common/compress_lzma.h \
src/common/compress_none.h \
src/common/compress_zlib.h \
src/common/compress_zstd.h \
src/common/confline.h \