Browse Source

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
Alexander Færøy 7 years ago
parent
commit
1c77d8690c

+ 19 - 0
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

+ 5 - 1
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);
 

+ 11 - 0
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 *

+ 3 - 0
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);
 

+ 10 - 0
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 *

+ 3 - 0
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);
 

+ 11 - 0
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 *

+ 3 - 0
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);
 

+ 15 - 0
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.
 }