From 3a05687c6d4c1ed11707e53b5dbec1b81e2a9c0a Mon Sep 17 00:00:00 2001 From: Alexander Færøy Date: Fri, 12 May 2017 12:45:00 +0200 Subject: Add API for getting human readable descriptions of a compress_method_t See: https://bugs.torproject.org/21667 --- src/common/compress.c | 26 ++++++++++++++++++++++++++ src/common/compress.h | 1 + 2 files changed, 27 insertions(+) diff --git a/src/common/compress.c b/src/common/compress.c index 6fe4569868..6513029f9c 100644 --- a/src/common/compress.c +++ b/src/common/compress.c @@ -343,6 +343,32 @@ compression_method_get_name(compress_method_t method) return NULL; } +/** Table of compression human readable method names. */ +static const struct { + compress_method_t method; + const char *name; +} compression_method_human_names[] = { + { NO_METHOD, "uncompressed" }, + { GZIP_METHOD, "gzipped" }, + { ZLIB_METHOD, "deflated" }, + { LZMA_METHOD, "LZMA compressed" }, + { ZSTD_METHOD, "Zstandard compressed" }, + { UNKNOWN_METHOD, "unknown encoding" }, +}; + +/** Return a human readable string representation of the compression method + * method, or NULL if the method isn't recognized. */ +const char * +compression_method_get_human_name(compress_method_t method) +{ + unsigned i; + for (i = 0; i < ARRAY_LENGTH(compression_method_human_names); ++i) { + if (method == compression_method_human_names[i].method) + return compression_method_human_names[i].name; + } + return NULL; +} + /** Return the compression method represented by the string name, or * UNKNOWN_METHOD if the string isn't recognized. */ compress_method_t diff --git a/src/common/compress.h b/src/common/compress.h index 5b47c5d458..7c0dc14061 100644 --- a/src/common/compress.h +++ b/src/common/compress.h @@ -50,6 +50,7 @@ int tor_compress_is_compression_bomb(size_t size_in, size_t size_out); int tor_compress_supports_method(compress_method_t method); unsigned tor_compress_get_supported_method_bitmask(void); const char * compression_method_get_name(compress_method_t method); +const char *compression_method_get_human_name(compress_method_t method); compress_method_t compression_method_get_by_name(const char *name); const char *tor_compress_version_str(compress_method_t method); -- cgit v1.2.3-54-g00ecf