diff options
author | lloyd <[email protected]> | 2015-05-10 02:33:00 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-05-10 02:33:00 +0000 |
commit | a08c16ef5f5fa85ab8b46c2fcbeca2c1b40fa339 (patch) | |
tree | 7623a51e07a9bcf3fe57cdb4536a983c4a4ebb15 /src | |
parent | 9dfc6fdb2adc00ec1aac3099d60883d473943bb7 (diff) |
Change `make_compressor` and `make_decompressor` to return a
compression type instead of the base transform class. Add some final
annotations.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/compression/compression.cpp | 74 | ||||
-rw-r--r-- | src/lib/compression/compression.h | 14 |
2 files changed, 49 insertions, 39 deletions
diff --git a/src/lib/compression/compression.cpp b/src/lib/compression/compression.cpp index 8e976d572..89d3a68e7 100644 --- a/src/lib/compression/compression.cpp +++ b/src/lib/compression/compression.cpp @@ -36,38 +36,48 @@ void Compression_Alloc_Info::do_free(void* ptr) } } -Transform* make_compressor(const std::string& type, size_t level) - { - const std::string comp_suffix = "_Compression(" + std::to_string(level) + ")"; - - if(type == "zlib") - return get_transform("Zlib" + comp_suffix); - if(type == "deflate") - return get_transform("Deflate" + comp_suffix); - if(type == "gzip" || type == "gz") - return get_transform("Gzip" + comp_suffix); - if(type == "bzip2" || type == "bz2") - return get_transform("Bzip2" + comp_suffix); - if(type == "lzma" || type == "xz") - return get_transform("LZMA" + comp_suffix); - - return nullptr; - } - -Transform* make_decompressor(const std::string& type) - { - if(type == "zlib") - return get_transform("Zlib_Decompression"); - if(type == "deflate") - return get_transform("Deflate_Decompression"); - if(type == "gzip" || type == "gz") - return get_transform("Gzip_Decompression"); - if(type == "bzip2" || type == "bz2") - return get_transform("Bzip2_Decompression"); - if(type == "lzma" || type == "xz") - return get_transform("LZMA_Decompression"); - - return nullptr; +namespace { + +Compressor_Transform* do_make_compressor(const std::string& type, const std::string suffix) + { + const std::map<std::string, std::string> trans{ + {"zlib", "Zlib"}, + {"deflate", "Deflate"}, + {"gzip", "Gzip"}, + {"gz", "Gzip"}, + {"bzip2", "Bzip2"}, + {"bz2", "Bzip2"}, + {"lzma", "LZMA"}, + {"xz", "LZMA"}}; + + auto i = trans.find(type); + + if(i == trans.end()) + return nullptr; + + const std::string t_name = i->second + suffix; + + std::unique_ptr<Transform> t(get_transform(t_name)); + + Compressor_Transform* r = dynamic_cast<Compressor_Transform*>(t.get()); + + if(!r) + throw std::runtime_error("Bad cast of compression object " + t_name); + + t.release(); + return r; + } + +} + +Compressor_Transform* make_compressor(const std::string& type, size_t level) + { + return do_make_compressor(type, "_Compression(" + std::to_string(level) + ")"); + } + +Compressor_Transform* make_decompressor(const std::string& type) + { + return do_make_compressor(type, "_Decompression"); } void Stream_Compression::clear() diff --git a/src/lib/compression/compression.h b/src/lib/compression/compression.h index f70252cbe..62525dcc9 100644 --- a/src/lib/compression/compression.h +++ b/src/lib/compression/compression.h @@ -15,25 +15,25 @@ namespace Botan { class BOTAN_DLL Compressor_Transform : public Transform { public: - size_t update_granularity() const override { return 1; } + size_t update_granularity() const override final { return 1; } - size_t minimum_final_size() const override { return 0; } + size_t minimum_final_size() const override final { return 0; } - size_t default_nonce_length() const override { return 0; } + size_t default_nonce_length() const override final { return 0; } - bool valid_nonce_length(size_t nonce_len) const override + bool valid_nonce_length(size_t nonce_len) const override final { return nonce_len == 0; } virtual void flush(secure_vector<byte>& buf, size_t offset = 0) { update(buf, offset); } - size_t output_length(size_t) const override + size_t output_length(size_t) const override final { throw std::runtime_error(name() + " output length indeterminate"); } }; -BOTAN_DLL Transform* make_compressor(const std::string& type, size_t level); -BOTAN_DLL Transform* make_decompressor(const std::string& type); +BOTAN_DLL Compressor_Transform* make_compressor(const std::string& type, size_t level); +BOTAN_DLL Compressor_Transform* make_decompressor(const std::string& type); class Compression_Stream { |