]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add function to check if a given compression method is supported.
authorAlexander Færøy <ahf@torproject.org>
Thu, 20 Apr 2017 13:56:38 +0000 (15:56 +0200)
committerNick Mathewson <nickm@torproject.org>
Tue, 25 Apr 2017 12:10:09 +0000 (08:10 -0400)
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
src/common/compress.h
src/common/compress_lzma.c
src/common/compress_lzma.h
src/common/compress_zlib.c
src/common/compress_zlib.h
src/common/compress_zstd.c
src/common/compress_zstd.h
src/test/test_util.c

index 52ff13258a818b59538e20d5fc831c310b4cab61..725dde53e01ae516e3b1f5397ba477af645a1176 100644 (file)
@@ -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
index 1fd4325b7ef0230f0f0aa2fe8c5340122cd857ec..87306f5fb9b3249182dec3fb6b8d8efad8e3a7ae 100644 (file)
@@ -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);
 
index e1a7a66b0cf0e904fef9acdf2b69fed26a3138fb..c45cb5eb4fb8048abc66555c40c61276e2aa5dae 100644 (file)
@@ -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 *
index 2cc3a00660f2e627b82a36731ed0d09190008e01..801e22ddd4e5c169dfbf780a2137de8d67615383 100644 (file)
@@ -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);
 
index 2c9aba32ee1f3369cdbc948222716c33ebee7157..e1a68c2aa1905eddf7b7e20a0b6f2b27738b413c 100644 (file)
@@ -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 *
index 0862678da32197941ea06bb64a21feaf38d35bff..0b1aad8b9ccb2ca87da61d900041a0b2e10f3eb2 100644 (file)
@@ -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);
 
index e2eb292d5d605ee5dc117c934e3c40ca43911773..dca4dbdab5b966632cb641b4ea4512ea7bdb29b4 100644 (file)
 /** 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 *
index ec83ba961ed0c6befe3d8eb00fb5bde7d0d273ec..b2297bd1df3637f82d925c5c2ad2aa799cb19571 100644 (file)
@@ -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);
 
index 6a7db3137a6105ae7fbb4d648121787abc43ba93..1e33de82ae28c3a53f0922433775e39573309bb1 100644 (file)
@@ -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.
 }