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.h | |
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.h')
-rw-r--r-- | src/lib/compression/compression.h | 92 |
1 files changed, 67 insertions, 25 deletions
diff --git a/src/lib/compression/compression.h b/src/lib/compression/compression.h index 66aaacdc4..81b863dcf 100644 --- a/src/lib/compression/compression.h +++ b/src/lib/compression/compression.h @@ -8,32 +8,76 @@ #ifndef BOTAN_COMPRESSION_TRANSFORM_H__ #define BOTAN_COMPRESSION_TRANSFORM_H__ -#include <botan/transform.h> +#include <botan/secmem.h> +#include <botan/scan_name.h> namespace Botan { -class BOTAN_DLL Compressor_Transform : public Transform +class BOTAN_DLL Compression_Algorithm { public: - size_t update_granularity() const override final { return 1; } + typedef SCAN_Name Spec; + + /** + * Begin compressing. Most compression algorithms offer a tunable + * time/compression tradeoff parameter generally represented by + * an integer in the range of 1 to 9. + * + * If 0 or a value out of range is provided, a compression algorithm + * specific default is used. + */ + virtual void start(size_t comp_level = 0) = 0; + + /** + * Process some data. Input must be in size update_granularity() byte blocks. + * @param blocks in/out parameter which will possibly be resized or swapped + * @param offset an offset into blocks to begin processing + * @param flush if true the compressor will be told to flush state + */ + virtual void update(secure_vector<byte>& buf, size_t offset = 0, bool flush = false) = 0; + + /** + * Finish compressing + * + * @param final_block in/out parameter + * @param offset an offset into final_block to begin processing + */ + virtual void finish(secure_vector<byte>& final_block, size_t offset = 0) = 0; + + virtual std::string name() const = 0; + + /** + * Reset the state and abort the current message; start can be + * called again to process a new message. + */ + virtual void clear() = 0; + + virtual ~Compression_Algorithm() {} + }; + +class BOTAN_DLL Decompression_Algorithm + { + public: + typedef SCAN_Name Spec; - size_t minimum_final_size() const override final { return 0; } + /** + * Decompression does not support levels + */ + virtual void start() = 0; - size_t default_nonce_length() const override final { return 0; } + virtual void update(secure_vector<byte>& buf, size_t offset = 0) = 0; - bool valid_nonce_length(size_t nonce_len) const override final - { return nonce_len == 0; } + virtual void finish(secure_vector<byte>& final_block, size_t offset = 0) = 0; - virtual void flush(secure_vector<byte>& buf, size_t offset = 0) { update(buf, offset); } + virtual std::string name() const = 0; - size_t output_length(size_t) const override final - { - throw Exception(name() + " output length indeterminate"); - } + virtual void clear() = 0; + + virtual ~Decompression_Algorithm() {} }; -BOTAN_DLL Compressor_Transform* make_compressor(const std::string& type, size_t level); -BOTAN_DLL Compressor_Transform* make_decompressor(const std::string& type); +BOTAN_DLL Compression_Algorithm* make_compressor(const std::string& type); +BOTAN_DLL Decompression_Algorithm* make_decompressor(const std::string& type); class Compression_Stream { @@ -55,39 +99,37 @@ class Compression_Stream virtual bool run(u32bit flags) = 0; }; -class BOTAN_DLL Stream_Compression : public Compressor_Transform +class Stream_Compression : public Compression_Algorithm { public: - void update(secure_vector<byte>& buf, size_t offset = 0) final override; - - void flush(secure_vector<byte>& buf, size_t offset = 0) final override; + void update(secure_vector<byte>& buf, size_t offset, bool flush) final override; - void finish(secure_vector<byte>& buf, size_t offset = 0) final override; + void finish(secure_vector<byte>& buf, size_t offset) final override; void clear() final override; private: - secure_vector<byte> start_raw(const byte[], size_t) final override; + void start(size_t level) final override; void process(secure_vector<byte>& buf, size_t offset, u32bit flags); - virtual Compression_Stream* make_stream() const = 0; + virtual Compression_Stream* make_stream(size_t level) const = 0; secure_vector<byte> m_buffer; std::unique_ptr<Compression_Stream> m_stream; }; -class BOTAN_DLL Stream_Decompression : public Compressor_Transform +class Stream_Decompression : public Decompression_Algorithm { public: - void update(secure_vector<byte>& buf, size_t offset = 0) final override; + void update(secure_vector<byte>& buf, size_t offset) final override; - void finish(secure_vector<byte>& buf, size_t offset = 0) final override; + void finish(secure_vector<byte>& buf, size_t offset) final override; void clear() final override; private: - secure_vector<byte> start_raw(const byte[], size_t) final override; + void start() final override; void process(secure_vector<byte>& buf, size_t offset, u32bit flags); |