Browse Source

Merge branch 'compress_none_v2_squashed'

Nick Mathewson 7 years ago
parent
commit
584ab1f29b

+ 13 - 2
src/common/compress.c

@@ -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;
@@ -458,7 +464,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;
   }
@@ -506,6 +514,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;
   }
@@ -533,6 +543,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;
   }

+ 53 - 0
src/common/compress_none.c

@@ -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;
+  }
+}
+

+ 20 - 0
src/common/compress_none.h

@@ -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.
+

+ 3 - 1
src/common/include.am

@@ -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				\

+ 7 - 1
src/test/test_buffers.c

@@ -611,7 +611,11 @@ test_buffers_compress_fin_at_chunk_end_impl(compress_method_t method,
 
   tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
 
-  tt_uint_op(in_len, OP_GT, headerjunk);
+  if (method == NO_METHOD) {
+    tt_uint_op(in_len, OP_EQ, headerjunk);
+  } else {
+    tt_uint_op(in_len, OP_GT, headerjunk);
+  }
 
   tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
                                      contents + headerjunk,
@@ -855,6 +859,8 @@ struct testcase_t buffer_tests[] = {
     &passthrough_setup, (char*)"x-zstd" },
   { "compress/lzma", test_buffers_compress, TT_FORK,
     &passthrough_setup, (char*)"x-tor-lzma" },
+  { "compress/none", test_buffers_compress, TT_FORK,
+    &passthrough_setup, (char*)"identity" },
 
   END_OF_TESTCASES
 };

+ 22 - 7
src/test/test_util.c

@@ -2255,8 +2255,15 @@ test_util_compress_impl(compress_method_t method)
 
   tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1, method));
   tt_assert(buf2 != NULL);
-  tt_int_op(len1, OP_LT, strlen(buf1));
-  tt_int_op(detect_compression_method(buf2, len1), OP_EQ, method);
+  if (method == NO_METHOD) {
+    // The identity transform doesn't actually compress, and it isn't
+    // detectable as "the identity transform."
+    tt_int_op(len1, OP_EQ, strlen(buf1)+1);
+    tt_int_op(detect_compression_method(buf2, len1), OP_EQ, UNKNOWN_METHOD);
+  } else {
+    tt_int_op(len1, OP_LT, strlen(buf1));
+    tt_int_op(detect_compression_method(buf2, len1), OP_EQ, method);
+  }
 
   tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1, method, 1, LOG_INFO));
   tt_assert(buf3 != NULL);
@@ -2300,11 +2307,14 @@ test_util_compress_impl(compress_method_t method)
   tt_assert(fast_memeq(buf1, buf3, len2));
   tt_int_op(buf3[len2], OP_EQ, 0);
 
-  /* when we demand a complete output, this must fail. */
+  /* when we demand a complete output from a real compression method, this
+   * must fail. */
   tor_free(buf3);
-  tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
-                           method, 1, LOG_INFO));
-  tt_assert(buf3 == NULL);
+  if (method != NO_METHOD) {
+    tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
+                             method, 1, LOG_INFO));
+    tt_assert(buf3 == NULL);
+  }
 
  done:
   tor_free(buf1);
@@ -2337,7 +2347,11 @@ test_util_compress_stream_impl(compress_method_t method,
   tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 1),
             OP_EQ, TOR_COMPRESS_DONE);
   tt_int_op(0, OP_EQ, len2);
-  tt_assert(cp1 > cp2); /* Make sure we really added something. */
+  if (method == NO_METHOD) {
+    tt_ptr_op(cp1, OP_EQ, cp2);
+  } else {
+    tt_assert(cp1 > cp2); /* Make sure we really added something. */
+  }
 
   tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
                             method, 1, LOG_WARN));
@@ -5755,6 +5769,7 @@ struct testcase_t util_tests[] = {
   COMPRESS(gzip, "gzip"),
   COMPRESS(lzma, "x-tor-lzma"),
   COMPRESS(zstd, "x-zstd"),
+  COMPRESS(none, "identity"),
   UTIL_TEST(gzip_compression_bomb, TT_FORK),
   UTIL_LEGACY(datadir),
   UTIL_LEGACY(memarea),