diff options
author | lloyd <[email protected]> | 2015-01-28 04:32:10 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-01-28 04:32:10 +0000 |
commit | 7b56f1bd570dc684ffd7c945dee0d9b5480354ff (patch) | |
tree | 0c50ad534280a292a1b76daee9a19b34cfd96367 /src/lib/compression | |
parent | b8fa304ec981d273c45d7ef31705d65ccfb00cc1 (diff) |
Add a runtime map of string->func() which when called return
Transforms and BlockCiphers. Registration for all types is done at
startup but is very cheap as just a std::function and a std::map entry
are created, no actual objects are created until needed. This is a
huge improvement over Algorithm_Factory which used T::clone() as the
function and thus kept a prototype object of each type in memory.
Replace existing lookup mechanisms for ciphers, AEADs, and compression
to use the transform lookup. The existing Engine framework remains in
place for BlockCipher, but the engines now just call to the registry
instead of having hardcoded lookups.
s/Transformation/Transform/ with typedefs for compatability.
Remove lib/selftest code (for runtime selftesting): not the right approach.
Diffstat (limited to 'src/lib/compression')
-rw-r--r-- | src/lib/compression/bzip2/bzip2.cpp | 2 | ||||
-rw-r--r-- | src/lib/compression/comp_util.h | 5 | ||||
-rw-r--r-- | src/lib/compression/compression.cpp | 59 | ||||
-rw-r--r-- | src/lib/compression/compression.h | 10 | ||||
-rw-r--r-- | src/lib/compression/lzma/lzma.cpp | 2 | ||||
-rw-r--r-- | src/lib/compression/zlib/zlib.cpp | 4 |
6 files changed, 35 insertions, 47 deletions
diff --git a/src/lib/compression/bzip2/bzip2.cpp b/src/lib/compression/bzip2/bzip2.cpp index 471635e3e..2d1617bce 100644 --- a/src/lib/compression/bzip2/bzip2.cpp +++ b/src/lib/compression/bzip2/bzip2.cpp @@ -15,6 +15,8 @@ namespace Botan { +BOTAN_REGISTER_COMPRESSION(Bzip2_Compression, Bzip2_Decompression); + namespace { class Bzip2_Stream : public Zlib_Style_Stream<bz_stream, char> diff --git a/src/lib/compression/comp_util.h b/src/lib/compression/comp_util.h index 6e1ee1671..963eae642 100644 --- a/src/lib/compression/comp_util.h +++ b/src/lib/compression/comp_util.h @@ -9,6 +9,7 @@ #define BOTAN_COMPRESSION_UTILS_H__ #include <botan/compression.h> +#include <botan/algo_registry.h> #include <memory> #include <unordered_map> @@ -84,6 +85,10 @@ class Zlib_Style_Stream : public Compression_Stream std::unique_ptr<Compression_Alloc_Info> m_allocs; }; +#define BOTAN_REGISTER_COMPRESSION(C, D) \ + BOTAN_REGISTER_T_1LEN(Transform, C, 9) \ + BOTAN_REGISTER_T_NOARGS(Transform, D) + } #endif diff --git a/src/lib/compression/compression.cpp b/src/lib/compression/compression.cpp index 428271e9a..e5221aba6 100644 --- a/src/lib/compression/compression.cpp +++ b/src/lib/compression/compression.cpp @@ -6,67 +6,42 @@ */ #include <botan/compression.h> - -#if defined(BOTAN_HAS_ZLIB_TRANSFORM) - #include <botan/zlib.h> -#endif - -#if defined(BOTAN_HAS_BZIP2_TRANSFORM) - #include <botan/bzip2.h> -#endif - -#if defined(BOTAN_HAS_LZMA_TRANSFORM) - #include <botan/lzma.h> -#endif +#include <botan/algo_registry.h> namespace Botan { -Compressor_Transformation* make_compressor(const std::string& type, size_t level) +Transform* make_compressor(const std::string& type, size_t level) { -#if defined(BOTAN_HAS_ZLIB_TRANSFORM) + const std::string comp_suffix = "_Compression(" + std::to_string(level) + ")"; + if(type == "zlib") - return new Zlib_Compression(level); + return get_transform("Zlib" + comp_suffix); if(type == "deflate") - return new Deflate_Compression(level); + return get_transform("Deflate" + comp_suffix); if(type == "gzip" || type == "gz") - return new Gzip_Compression(level); -#endif - -#if defined(BOTAN_HAS_BZIP2_TRANSFORM) + return get_transform("Gzip" + comp_suffix); if(type == "bzip2" || type == "bz2") - return new Bzip2_Compression(level); -#endif - -#if defined(BOTAN_HAS_LZMA_TRANSFORM) + return get_transform("Bzip2", comp_suffix); if(type == "lzma" || type == "xz") - return new LZMA_Compression(level); -#endif + return get_transform("LZMA", comp_suffix); - throw std::runtime_error("Unknown compression type " + type); + return nullptr; } -Compressor_Transformation* make_decompressor(const std::string& type) +Transform* make_decompressor(const std::string& type) { -#if defined(BOTAN_HAS_ZLIB_TRANSFORM) if(type == "zlib") - return new Zlib_Decompression; + return get_transform("Zlib_Decompression"); if(type == "deflate") - return new Deflate_Decompression; + return get_transform("Deflate_Decompression"); if(type == "gzip" || type == "gz") - return new Gzip_Decompression; -#endif - -#if defined(BOTAN_HAS_BZIP2_TRANSFORM) + return get_transform("Gzip_Decompression"); if(type == "bzip2" || type == "bz2") - return new Bzip2_Decompression; -#endif - -#if defined(BOTAN_HAS_LZMA_TRANSFORM) + return get_transform("Bzip2_Decompression"); if(type == "lzma" || type == "xz") - return new LZMA_Decompression; -#endif + return get_transform("LZMA_Decompression"); - throw std::runtime_error("Unknown compression type " + type); + return nullptr; } void Stream_Compression::clear() diff --git a/src/lib/compression/compression.h b/src/lib/compression/compression.h index b38d94f64..f70252cbe 100644 --- a/src/lib/compression/compression.h +++ b/src/lib/compression/compression.h @@ -12,7 +12,7 @@ namespace Botan { -class BOTAN_DLL Compressor_Transformation : public Transformation +class BOTAN_DLL Compressor_Transform : public Transform { public: size_t update_granularity() const override { return 1; } @@ -32,8 +32,8 @@ class BOTAN_DLL Compressor_Transformation : public Transformation } }; -BOTAN_DLL Compressor_Transformation* make_compressor(const std::string& type, size_t level); -BOTAN_DLL Compressor_Transformation* make_decompressor(const std::string& type); +BOTAN_DLL Transform* make_compressor(const std::string& type, size_t level); +BOTAN_DLL Transform* make_decompressor(const std::string& type); class Compression_Stream { @@ -55,7 +55,7 @@ class Compression_Stream virtual bool run(u32bit flags) = 0; }; -class BOTAN_DLL Stream_Compression : public Compressor_Transformation +class BOTAN_DLL Stream_Compression : public Compressor_Transform { public: void update(secure_vector<byte>& buf, size_t offset = 0) override; @@ -76,7 +76,7 @@ class BOTAN_DLL Stream_Compression : public Compressor_Transformation std::unique_ptr<Compression_Stream> m_stream; }; -class BOTAN_DLL Stream_Decompression : public Compressor_Transformation +class BOTAN_DLL Stream_Decompression : public Compressor_Transform { public: void update(secure_vector<byte>& buf, size_t offset = 0) override; diff --git a/src/lib/compression/lzma/lzma.cpp b/src/lib/compression/lzma/lzma.cpp index 0aadf1513..69d73a3a1 100644 --- a/src/lib/compression/lzma/lzma.cpp +++ b/src/lib/compression/lzma/lzma.cpp @@ -14,6 +14,8 @@ namespace Botan { +BOTAN_REGISTER_COMPRESSION(LZMA_Compression, LZMA_Decompression); + namespace { class LZMA_Stream : public Zlib_Style_Stream<lzma_stream, byte> diff --git a/src/lib/compression/zlib/zlib.cpp b/src/lib/compression/zlib/zlib.cpp index 8c94e4331..24e8721e3 100644 --- a/src/lib/compression/zlib/zlib.cpp +++ b/src/lib/compression/zlib/zlib.cpp @@ -14,6 +14,10 @@ namespace Botan { +BOTAN_REGISTER_COMPRESSION(Zlib_Compression, Zlib_Decompression); +BOTAN_REGISTER_COMPRESSION(Gzip_Compression, Gzip_Decompression); +BOTAN_REGISTER_COMPRESSION(Deflate_Compression, Deflate_Decompression); + namespace { class Zlib_Stream : public Zlib_Style_Stream<z_stream, Bytef> |