From 0274ea749a6a1456d905878730c226985db183ad Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 25 Apr 2017 14:51:44 -0400 Subject: Function to convert compression methods to/from strings. --- src/common/compress.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/common/compress.c') diff --git a/src/common/compress.c b/src/common/compress.c index 5a3359aa05..72669391bb 100644 --- a/src/common/compress.c +++ b/src/common/compress.c @@ -277,6 +277,49 @@ tor_compress_supports_method(compress_method_t method) } } +/** Table of compression method names. These should have an "x-" prefix, + * if they are not listed in the IANA content coding registry. */ +static const struct { + const char *name; + compress_method_t method; +} compression_method_names[] = { + { "gzip", GZIP_METHOD }, + { "deflate", ZLIB_METHOD }, + { "x-lzma2", LZMA_METHOD }, + { "x-zstd" , ZSTD_METHOD }, + { "identity", NO_METHOD }, + + /* Later entries in this table are not canonical; these are recognized but + * not emitted. */ + { "x-gzip", GZIP_METHOD }, +}; + +/** Return the canonical string representation of the compression method + * method, or NULL if the method isn't recognized. */ +const char * +compression_method_get_name(compress_method_t method) +{ + unsigned i; + for (i = 0; i < ARRAY_LENGTH(compression_method_names); ++i) { + if (method == compression_method_names[i].method) + return compression_method_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 +compression_method_get_by_name(const char *name) +{ + unsigned i; + for (i = 0; i < ARRAY_LENGTH(compression_method_names); ++i) { + if (!strcmp(compression_method_names[i].name, name)) + return compression_method_names[i].method; + } + return UNKNOWN_METHOD; +} + /** Return a string representation of the version of the library providing the * compression method given in method. Returns NULL if method is * unknown or unsupported. */ -- cgit v1.2.3-54-g00ecf