[tor-commits] [tor/master] Add function to check if a given compression method is supported.

nickm at torproject.org nickm at torproject.org
Tue Apr 25 12:18:24 UTC 2017


commit 1c77d8690caa1ad80b9d9581cac8eddae95e82d1
Author: Alexander Færøy <ahf at torproject.org>
Date:   Thu Apr 20 15:56:38 2017 +0200

    Add function to check if a given compression method is supported.
    
    This patch adds support for checking if a given `compress_method_t` is
    supported by the currently running Tor instance using
    `tor_compress_supports_method()`.
    
    See: https://bugs.torproject.org/21662
---
 src/common/compress.c      | 19 +++++++++++++++++++
 src/common/compress.h      |  6 +++++-
 src/common/compress_lzma.c | 11 +++++++++++
 src/common/compress_lzma.h |  3 +++
 src/common/compress_zlib.c | 10 ++++++++++
 src/common/compress_zlib.h |  3 +++
 src/common/compress_zstd.c | 11 +++++++++++
 src/common/compress_zstd.h |  3 +++
 src/test/test_util.c       | 15 +++++++++++++++
 9 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/src/common/compress.c b/src/common/compress.c
index 52ff132..725dde5 100644
--- a/src/common/compress.c
+++ b/src/common/compress.c
@@ -154,6 +154,25 @@ detect_compression_method(const char *in, size_t in_len)
   }
 }
 
+/** Return 1 if a given <b>method</b> is supported; otherwise 0. */
+int
+tor_compress_supports_method(compress_method_t method)
+{
+  switch (method) {
+    case GZIP_METHOD:
+    case ZLIB_METHOD:
+      return tor_zlib_method_supported();
+    case LZMA_METHOD:
+      return tor_lzma_method_supported();
+    case ZSTD_METHOD:
+      return tor_zstd_method_supported();
+    case NO_METHOD:
+    case UNKNOWN_METHOD:
+    default:
+      return 0;
+  }
+}
+
 /** Return the approximate number of bytes allocated for all
  * supported compression schemas. */
 size_t
diff --git a/src/common/compress.h b/src/common/compress.h
index 1fd4325..87306f5 100644
--- a/src/common/compress.h
+++ b/src/common/compress.h
@@ -13,7 +13,8 @@
 
 /** Enumeration of what kind of compression to use.  Only ZLIB_METHOD and
  * GZIP_METHOD is guaranteed to be supported by the compress/uncompress
- * functions here. */
+ * functions here. Call tor_compress_supports_method() to check if a given
+ * compression schema is supported by Tor. */
 typedef enum {
   NO_METHOD=0,
   GZIP_METHOD=1,
@@ -51,6 +52,9 @@ tor_compress_memory_level(compression_level_t level);
 int
 tor_compress_is_compression_bomb(size_t size_in, size_t size_out);
 
+int
+tor_compress_supports_method(compress_method_t method);
+
 size_t
 tor_compress_get_total_allocation(void);
 
diff --git a/src/common/compress_lzma.c b/src/common/compress_lzma.c
index e1a7a66..c45cb5e 100644
--- a/src/common/compress_lzma.c
+++ b/src/common/compress_lzma.c
@@ -61,6 +61,17 @@ lzma_error_str(lzma_ret error)
 }
 #endif // HAVE_LZMA.
 
+/** Return 1 if LZMA compression is supported; otherwise 0. */
+int
+tor_lzma_method_supported(void)
+{
+#ifdef HAVE_LZMA
+  return 1;
+#else
+  return 0;
+#endif
+}
+
 /** Return a string representation of the version of the currently running
  * version of liblzma. */
 const char *
diff --git a/src/common/compress_lzma.h b/src/common/compress_lzma.h
index 2cc3a00..801e22d 100644
--- a/src/common/compress_lzma.h
+++ b/src/common/compress_lzma.h
@@ -11,6 +11,9 @@
 #ifndef TOR_COMPRESS_LZMA_H
 #define TOR_COMPRESS_LZMA_H
 
+int
+tor_lzma_method_supported(void);
+
 const char *
 tor_lzma_get_version_str(void);
 
diff --git a/src/common/compress_zlib.c b/src/common/compress_zlib.c
index 2c9aba3..e1a68c2 100644
--- a/src/common/compress_zlib.c
+++ b/src/common/compress_zlib.c
@@ -64,6 +64,16 @@ method_bits(compress_method_t method, compression_level_t level)
   }
 }
 
+/** Return 1 if zlib/gzip compression is supported; otherwise 0. */
+int
+tor_zlib_method_supported(void)
+{
+  /* We currently always support zlib/gzip, but we keep this function around in
+   * case we some day decide to deprecate zlib/gzip support.
+   */
+  return 1;
+}
+
 /** Return a string representation of the version of the currently running
  * version of zlib. */
 const char *
diff --git a/src/common/compress_zlib.h b/src/common/compress_zlib.h
index 0862678..0b1aad8 100644
--- a/src/common/compress_zlib.h
+++ b/src/common/compress_zlib.h
@@ -11,6 +11,9 @@
 #ifndef TOR_COMPRESS_ZLIB_H
 #define TOR_COMPRESS_ZLIB_H
 
+int
+tor_zlib_method_supported(void);
+
 const char *
 tor_zlib_get_version_str(void);
 
diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c
index e2eb292..dca4dbd 100644
--- a/src/common/compress_zstd.c
+++ b/src/common/compress_zstd.c
@@ -26,6 +26,17 @@
 /** Total number of bytes allocated for Zstandard state. */
 static size_t total_zstd_allocation = 0;
 
+/** Return 1 if Zstandard compression is supported; otherwise 0. */
+int
+tor_zstd_method_supported(void)
+{
+#ifdef HAVE_ZSTD
+  return 1;
+#else
+  return 0;
+#endif
+}
+
 /** Return a string representation of the version of the currently running
  * version of libzstd. */
 const char *
diff --git a/src/common/compress_zstd.h b/src/common/compress_zstd.h
index ec83ba9..b2297bd 100644
--- a/src/common/compress_zstd.h
+++ b/src/common/compress_zstd.h
@@ -11,6 +11,9 @@
 #ifndef TOR_COMPRESS_ZSTD_H
 #define TOR_COMPRESS_ZSTD_H
 
+int
+tor_zstd_method_supported(void);
+
 const char *
 tor_zstd_get_version_str(void);
 
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 6a7db31..1e33de8 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -2252,6 +2252,9 @@ test_util_gzip(void *arg)
   tor_compress_state_t *state = NULL;
 
   (void)arg;
+  tt_assert(tor_compress_supports_method(GZIP_METHOD));
+  tt_assert(tor_compress_supports_method(ZLIB_METHOD));
+
   buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
   tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
 
@@ -2414,6 +2417,8 @@ test_util_lzma(void *arg)
   tor_compress_state_t *state = NULL;
 
   (void)arg;
+  tt_assert(tor_compress_supports_method(LZMA_METHOD));
+
   buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
   tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
 
@@ -2507,6 +2512,10 @@ test_util_lzma(void *arg)
   tor_free(buf1);
 #else
   (void)arg;
+  tt_assert(! tor_compress_supports_method(LZMA_METHOD));
+
+ done:
+  ;
 #endif // HAVE_LZMA.
 }
 
@@ -2520,6 +2529,8 @@ test_util_zstd(void *arg)
   tor_compress_state_t *state = NULL;
 
   (void)arg;
+  tt_assert(tor_compress_supports_method(ZSTD_METHOD));
+
   buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
   tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
 
@@ -2613,6 +2624,10 @@ test_util_zstd(void *arg)
   tor_free(buf1);
 #else
   (void)arg;
+  tt_assert(! tor_compress_supports_method(ZSTD_METHOD));
+
+ done:
+  ;
 #endif // HAVE_ZSTD.
 }
 





More information about the tor-commits mailing list