diff options
author | Jack Lloyd <[email protected]> | 2016-04-12 23:03:14 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-04-21 09:18:54 -0400 |
commit | 8b85b7805151ab8fce5ac9d214c71c4eeb3d6075 (patch) | |
tree | 40cbc2af481dfc2f84e32330308523a5e8f68e44 /src/lib/compression/compression.cpp | |
parent | a4358c96a0de1ab7afc0b437ab79bfc35f2e1824 (diff) |
Remove Transform base class
With sufficient squinting, Transform provided an abstract base
interface that covered both cipher modes and compression algorithms.
However it mapped on neither of them particularly well. In addition
this API had the same problem that has made me dislike the Pipe/Filter
API: given a Transform&, what does it do when you put bits in? Maybe
it encrypts. Maybe it compresses. It's a floor wax and a dessert topping!
Currently the Cipher_Mode interface is left mostly unchanged, with the
APIs previously on Transform just moved down the type hierarchy. I
think there are some definite improvements possible here, wrt handling
of in-place encryption, but left for a later commit.
The compression API is split into two types, Compression_Algorithm and
Decompression_Algorithm. Compression_Algorithm's start() call takes
the compression level, allowing varying compressions with a single
object. And flushing the compression state is moved to a bool param on
`Compression_Algorithm::update`. All the nonsense WRT compression
algorithms having zero length nonces, input granularity rules, etc
as a result of using the Transform interface goes away.
Diffstat (limited to 'src/lib/compression/compression.cpp')
-rw-r--r-- | src/lib/compression/compression.cpp | 69 |
1 files changed, 25 insertions, 44 deletions
diff --git a/src/lib/compression/compression.cpp b/src/lib/compression/compression.cpp index 54faec7b8..f289aa79f 100644 --- a/src/lib/compression/compression.cpp +++ b/src/lib/compression/compression.cpp @@ -54,9 +54,7 @@ void Compression_Alloc_Info::do_free(void* ptr) } } -namespace { - -Compressor_Transform* do_make_compressor(const std::string& type, const std::string& suffix) +Compression_Algorithm* make_compressor(const std::string& type) { const std::map<std::string, std::string> trans{ {"zlib", "Zlib"}, @@ -73,31 +71,29 @@ Compressor_Transform* do_make_compressor(const std::string& type, const std::str if(i == trans.end()) return nullptr; - const std::string t_name = i->second + suffix; - - std::unique_ptr<Transform> t(get_transform(t_name)); - - if(!t) - return nullptr; - - Compressor_Transform* r = dynamic_cast<Compressor_Transform*>(t.get()); - if(!r) - throw Exception("Bad cast of compression object " + t_name); - - t.release(); - return r; + const SCAN_Name t_name(i->second + "_Compression"); + return Algo_Registry<Compression_Algorithm>::global_registry().make(t_name); } -} - -Compressor_Transform* make_compressor(const std::string& type, size_t level) +Decompression_Algorithm* make_decompressor(const std::string& type) { - return do_make_compressor(type, "_Compression(" + std::to_string(level) + ")"); - } + const std::map<std::string, std::string> trans{ + {"zlib", "Zlib"}, + {"deflate", "Deflate"}, + {"gzip", "Gzip"}, + {"gz", "Gzip"}, + {"bzip2", "Bzip2"}, + {"bz2", "Bzip2"}, + {"lzma", "LZMA"}, + {"xz", "LZMA"}}; -Compressor_Transform* make_decompressor(const std::string& type) - { - return do_make_compressor(type, "_Decompression"); + auto i = trans.find(type); + + if(i == trans.end()) + return nullptr; + + const SCAN_Name t_name(i->second + "_Decompression"); + return Algo_Registry<Decompression_Algorithm>::global_registry().make(t_name); } void Stream_Compression::clear() @@ -105,13 +101,9 @@ void Stream_Compression::clear() m_stream.reset(); } -secure_vector<byte> Stream_Compression::start_raw(const byte[], size_t nonce_len) +void Stream_Compression::start(size_t level) { - if(!valid_nonce_length(nonce_len)) - throw Invalid_IV_Length(name(), nonce_len); - - m_stream.reset(make_stream()); - return secure_vector<byte>(); + m_stream.reset(make_stream(level)); } void Stream_Compression::process(secure_vector<byte>& buf, size_t offset, u32bit flags) @@ -154,16 +146,10 @@ void Stream_Compression::process(secure_vector<byte>& buf, size_t offset, u32bit buf.swap(m_buffer); } -void Stream_Compression::update(secure_vector<byte>& buf, size_t offset) - { - BOTAN_ASSERT(m_stream, "Initialized"); - process(buf, offset, m_stream->run_flag()); - } - -void Stream_Compression::flush(secure_vector<byte>& buf, size_t offset) +void Stream_Compression::update(secure_vector<byte>& buf, size_t offset, bool flush) { BOTAN_ASSERT(m_stream, "Initialized"); - process(buf, offset, m_stream->flush_flag()); + process(buf, offset, flush ? m_stream->flush_flag() : m_stream->run_flag()); } void Stream_Compression::finish(secure_vector<byte>& buf, size_t offset) @@ -178,14 +164,9 @@ void Stream_Decompression::clear() m_stream.reset(); } -secure_vector<byte> Stream_Decompression::start_raw(const byte[], size_t nonce_len) +void Stream_Decompression::start() { - if(!valid_nonce_length(nonce_len)) - throw Invalid_IV_Length(name(), nonce_len); - m_stream.reset(make_stream()); - - return secure_vector<byte>(); } void Stream_Decompression::process(secure_vector<byte>& buf, size_t offset, u32bit flags) |