aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlexander Færøy <ahf@torproject.org>2017-04-20 15:56:38 +0200
committerNick Mathewson <nickm@torproject.org>2017-04-25 08:10:09 -0400
commit1c77d8690caa1ad80b9d9581cac8eddae95e82d1 (patch)
tree87cb3f07a2919f11fef5eafe671bd97f8c825710 /src
parent04682d302aeeb0c2a0d9859bc0c1feee38daf16a (diff)
downloadtor-1c77d8690caa1ad80b9d9581cac8eddae95e82d1.tar.gz
tor-1c77d8690caa1ad80b9d9581cac8eddae95e82d1.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/common/compress.c19
-rw-r--r--src/common/compress.h6
-rw-r--r--src/common/compress_lzma.c11
-rw-r--r--src/common/compress_lzma.h3
-rw-r--r--src/common/compress_zlib.c10
-rw-r--r--src/common/compress_zlib.h3
-rw-r--r--src/common/compress_zstd.c11
-rw-r--r--src/common/compress_zstd.h3
-rw-r--r--src/test/test_util.c15
9 files changed, 80 insertions, 1 deletions
diff --git a/src/common/compress.c b/src/common/compress.c
index 52ff13258a..725dde53e0 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 1fd4325b7e..87306f5fb9 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 e1a7a66b0c..c45cb5eb4f 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 2cc3a00660..801e22ddd4 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 2c9aba32ee..e1a68c2aa1 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 0862678da3..0b1aad8b9c 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 e2eb292d5d..dca4dbdab5 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 ec83ba961e..b2297bd1df 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 6a7db3137a..1e33de82ae 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.
}