aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/compression
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-04-12 23:03:14 -0400
committerJack Lloyd <[email protected]>2016-04-21 09:18:54 -0400
commit8b85b7805151ab8fce5ac9d214c71c4eeb3d6075 (patch)
tree40cbc2af481dfc2f84e32330308523a5e8f68e44 /src/lib/compression
parenta4358c96a0de1ab7afc0b437ab79bfc35f2e1824 (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')
-rw-r--r--src/lib/compression/bzip2/bzip2.cpp12
-rw-r--r--src/lib/compression/bzip2/bzip2.h14
-rw-r--r--src/lib/compression/bzip2/info.txt2
-rw-r--r--src/lib/compression/compress_utils.h4
-rw-r--r--src/lib/compression/compression.cpp69
-rw-r--r--src/lib/compression/compression.h92
-rw-r--r--src/lib/compression/lzma/info.txt2
-rw-r--r--src/lib/compression/lzma/lzma.cpp9
-rw-r--r--src/lib/compression/lzma/lzma.h11
-rw-r--r--src/lib/compression/zlib/info.txt2
-rw-r--r--src/lib/compression/zlib/zlib.cpp31
-rw-r--r--src/lib/compression/zlib/zlib.h41
12 files changed, 138 insertions, 151 deletions
diff --git a/src/lib/compression/bzip2/bzip2.cpp b/src/lib/compression/bzip2/bzip2.cpp
index d9ada84f6..565eb09fc 100644
--- a/src/lib/compression/bzip2/bzip2.cpp
+++ b/src/lib/compression/bzip2/bzip2.cpp
@@ -39,6 +39,14 @@ class Bzip2_Compression_Stream : public Bzip2_Stream
public:
explicit Bzip2_Compression_Stream(size_t block_size)
{
+ /*
+ * Defaults to 900k blocks as the computation cost of
+ * compression is not overly affected by the size, though
+ * more memory is required.
+ */
+ if(block_size == 0 || block_size >= 9)
+ block_size = 9;
+
int rc = BZ2_bzCompressInit(streamp(), block_size, 0, 0);
if(rc == BZ_MEM_ERROR)
@@ -98,9 +106,9 @@ class Bzip2_Decompression_Stream : public Bzip2_Stream
}
-Compression_Stream* Bzip2_Compression::make_stream() const
+Compression_Stream* Bzip2_Compression::make_stream(size_t comp_level) const
{
- return new Bzip2_Compression_Stream(m_block_size);
+ return new Bzip2_Compression_Stream(comp_level);
}
Compression_Stream* Bzip2_Decompression::make_stream() const
diff --git a/src/lib/compression/bzip2/bzip2.h b/src/lib/compression/bzip2/bzip2.h
index 06c80cb8e..18216f7eb 100644
--- a/src/lib/compression/bzip2/bzip2.h
+++ b/src/lib/compression/bzip2/bzip2.h
@@ -19,21 +19,9 @@ namespace Botan {
class BOTAN_DLL Bzip2_Compression final : public Stream_Compression
{
public:
- /**
- * @param block_size in 1024 KiB increments, in range from 1 to 9.
- *
- * Lowering this does not noticably modify the compression or
- * decompression speed, though less memory is required for both
- * compression and decompression.
- */
- Bzip2_Compression(size_t block_size = 9) : m_block_size(block_size) {}
-
std::string name() const override { return "Bzip2_Compression"; }
-
private:
- Compression_Stream* make_stream() const override;
-
- const size_t m_block_size;
+ Compression_Stream* make_stream(size_t comp_level) const override;
};
/**
diff --git a/src/lib/compression/bzip2/info.txt b/src/lib/compression/bzip2/info.txt
index ea2efa6f1..bc8d780be 100644
--- a/src/lib/compression/bzip2/info.txt
+++ b/src/lib/compression/bzip2/info.txt
@@ -1,4 +1,4 @@
-define BZIP2_TRANSFORM 20141118
+define BZIP2 20160412
load_on vendor
diff --git a/src/lib/compression/compress_utils.h b/src/lib/compression/compress_utils.h
index 2a830ac4e..a60ae3c22 100644
--- a/src/lib/compression/compress_utils.h
+++ b/src/lib/compression/compress_utils.h
@@ -86,8 +86,8 @@ class Zlib_Style_Stream : public Compression_Stream
};
#define BOTAN_REGISTER_COMPRESSION(C, D) \
- BOTAN_REGISTER_T_1LEN(Transform, C, 9); \
- BOTAN_REGISTER_T_NOARGS(Transform, D)
+ BOTAN_REGISTER_T_NOARGS(Compression_Algorithm, C); \
+ BOTAN_REGISTER_T_NOARGS(Decompression_Algorithm, D)
}
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)
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);
diff --git a/src/lib/compression/lzma/info.txt b/src/lib/compression/lzma/info.txt
index 443a96919..7a712338e 100644
--- a/src/lib/compression/lzma/info.txt
+++ b/src/lib/compression/lzma/info.txt
@@ -1,4 +1,4 @@
-define LZMA_TRANSFORM 20141118
+define LZMA 20160412
load_on vendor
diff --git a/src/lib/compression/lzma/lzma.cpp b/src/lib/compression/lzma/lzma.cpp
index 3cc03a098..18701278d 100644
--- a/src/lib/compression/lzma/lzma.cpp
+++ b/src/lib/compression/lzma/lzma.cpp
@@ -58,6 +58,11 @@ class LZMA_Compression_Stream : public LZMA_Stream
public:
explicit LZMA_Compression_Stream(size_t level)
{
+ if(level == 0)
+ level = 6; // default
+ else if(level > 9)
+ level = 9; // clamp to maximum allowed value
+
lzma_ret rc = ::lzma_easy_encoder(streamp(), level, LZMA_CHECK_CRC64);
if(rc == LZMA_MEM_ERROR)
@@ -84,9 +89,9 @@ class LZMA_Decompression_Stream : public LZMA_Stream
}
-Compression_Stream* LZMA_Compression::make_stream() const
+Compression_Stream* LZMA_Compression::make_stream(size_t level) const
{
- return new LZMA_Compression_Stream(m_level);
+ return new LZMA_Compression_Stream(level);
}
Compression_Stream* LZMA_Decompression::make_stream() const
diff --git a/src/lib/compression/lzma/lzma.h b/src/lib/compression/lzma/lzma.h
index d9ea10091..63722b064 100644
--- a/src/lib/compression/lzma/lzma.h
+++ b/src/lib/compression/lzma/lzma.h
@@ -20,19 +20,10 @@ namespace Botan {
class BOTAN_DLL LZMA_Compression final : public Stream_Compression
{
public:
- /**
- * @param level how much effort to use on compressing (0 to 9);
- * higher levels are slower but tend to give better
- * compression
- */
- LZMA_Compression(size_t level = 6) : m_level(level) {}
-
std::string name() const override { return "LZMA_Compression"; }
private:
- Compression_Stream* make_stream() const override;
-
- const size_t m_level;
+ Compression_Stream* make_stream(size_t level) const override;
};
/**
diff --git a/src/lib/compression/zlib/info.txt b/src/lib/compression/zlib/info.txt
index 8b722350f..6c82c49a3 100644
--- a/src/lib/compression/zlib/info.txt
+++ b/src/lib/compression/zlib/info.txt
@@ -1,4 +1,4 @@
-define ZLIB_TRANSFORM 20141118
+define ZLIB 20160412
load_on vendor
diff --git a/src/lib/compression/zlib/zlib.cpp b/src/lib/compression/zlib/zlib.cpp
index 6df5ee931..836925a68 100644
--- a/src/lib/compression/zlib/zlib.cpp
+++ b/src/lib/compression/zlib/zlib.cpp
@@ -50,20 +50,25 @@ class Zlib_Compression_Stream : public Zlib_Stream
{
wbits = compute_window_bits(wbits, wbits_offset);
- int rc = deflateInit2(streamp(), level, Z_DEFLATED, wbits,
- 8, Z_DEFAULT_STRATEGY);
+ if(level >= 9)
+ level = 9;
+ else if(level == 0)
+ level = 6;
+
+ int rc = ::deflateInit2(streamp(), level, Z_DEFLATED, wbits, 8, Z_DEFAULT_STRATEGY);
+
if(rc != Z_OK)
throw Exception("zlib deflate initialization failed");
}
~Zlib_Compression_Stream()
{
- deflateEnd(streamp());
+ ::deflateEnd(streamp());
}
bool run(u32bit flags) override
{
- int rc = deflate(streamp(), flags);
+ int rc = ::deflate(streamp(), flags);
if(rc == Z_MEM_ERROR)
throw Exception("zlib memory allocation failure");
@@ -79,7 +84,7 @@ class Zlib_Decompression_Stream : public Zlib_Stream
public:
Zlib_Decompression_Stream(int wbits, int wbits_offset = 0)
{
- int rc = inflateInit2(streamp(), compute_window_bits(wbits, wbits_offset));
+ int rc = ::inflateInit2(streamp(), compute_window_bits(wbits, wbits_offset));
if(rc == Z_MEM_ERROR)
throw Exception("zlib memory allocation failure");
@@ -89,12 +94,12 @@ class Zlib_Decompression_Stream : public Zlib_Stream
~Zlib_Decompression_Stream()
{
- inflateEnd(streamp());
+ ::inflateEnd(streamp());
}
bool run(u32bit flags) override
{
- int rc = inflate(streamp(), flags);
+ int rc = ::inflate(streamp(), flags);
if(rc == Z_MEM_ERROR)
throw Exception("zlib memory allocation failure");
@@ -145,9 +150,9 @@ class Gzip_Decompression_Stream : public Zlib_Decompression_Stream
}
-Compression_Stream* Zlib_Compression::make_stream() const
+Compression_Stream* Zlib_Compression::make_stream(size_t level) const
{
- return new Zlib_Compression_Stream(m_level, 15);
+ return new Zlib_Compression_Stream(level, 15);
}
Compression_Stream* Zlib_Decompression::make_stream() const
@@ -155,9 +160,9 @@ Compression_Stream* Zlib_Decompression::make_stream() const
return new Zlib_Decompression_Stream(15);
}
-Compression_Stream* Deflate_Compression::make_stream() const
+Compression_Stream* Deflate_Compression::make_stream(size_t level) const
{
- return new Deflate_Compression_Stream(m_level, 15);
+ return new Deflate_Compression_Stream(level, 15);
}
Compression_Stream* Deflate_Decompression::make_stream() const
@@ -165,9 +170,9 @@ Compression_Stream* Deflate_Decompression::make_stream() const
return new Deflate_Decompression_Stream(15);
}
-Compression_Stream* Gzip_Compression::make_stream() const
+Compression_Stream* Gzip_Compression::make_stream(size_t level) const
{
- return new Gzip_Compression_Stream(m_level, 15, m_os_code);
+ return new Gzip_Compression_Stream(level, 15, m_os_code);
}
Compression_Stream* Gzip_Decompression::make_stream() const
diff --git a/src/lib/compression/zlib/zlib.h b/src/lib/compression/zlib/zlib.h
index 6a8cead14..0cedb1eab 100644
--- a/src/lib/compression/zlib/zlib.h
+++ b/src/lib/compression/zlib/zlib.h
@@ -19,20 +19,9 @@ namespace Botan {
class BOTAN_DLL Zlib_Compression final : public Stream_Compression
{
public:
- /**
- * @param level how much effort to use on compressing (0 to 9);
- * higher levels are slower but tend to give better
- * compression
- */
-
- Zlib_Compression(size_t level = 6) : m_level(level) {}
-
std::string name() const override { return "Zlib_Compression"; }
-
private:
- Compression_Stream* make_stream() const override;
-
- const size_t m_level;
+ Compression_Stream* make_stream(size_t level) const override;
};
/**
@@ -42,7 +31,6 @@ class BOTAN_DLL Zlib_Decompression final : public Stream_Decompression
{
public:
std::string name() const override { return "Zlib_Decompression"; }
-
private:
Compression_Stream* make_stream() const override;
};
@@ -53,19 +41,9 @@ class BOTAN_DLL Zlib_Decompression final : public Stream_Decompression
class BOTAN_DLL Deflate_Compression final : public Stream_Compression
{
public:
- /**
- * @param level how much effort to use on compressing (0 to 9);
- * higher levels are slower but tend to give better
- * compression
- */
- Deflate_Compression(size_t level = 6) : m_level(level) {}
-
std::string name() const override { return "Deflate_Compression"; }
-
private:
- Compression_Stream* make_stream() const override;
-
- const size_t m_level;
+ Compression_Stream* make_stream(size_t level) const override;
};
/**
@@ -75,7 +53,6 @@ class BOTAN_DLL Deflate_Decompression final : public Stream_Decompression
{
public:
std::string name() const override { return "Deflate_Decompression"; }
-
private:
Compression_Stream* make_stream() const override;
};
@@ -86,20 +63,11 @@ class BOTAN_DLL Deflate_Decompression final : public Stream_Decompression
class BOTAN_DLL Gzip_Compression final : public Stream_Compression
{
public:
- /**
- * @param level how much effort to use on compressing (0 to 9);
- * higher levels are slower but tend to give better
- * compression
- */
- Gzip_Compression(size_t level = 6, byte os_code = 255) :
- m_level(level), m_os_code(os_code) {}
+ Gzip_Compression(byte os_code = 255) : m_os_code(os_code) {}
std::string name() const override { return "Gzip_Compression"; }
-
private:
- Compression_Stream* make_stream() const override;
-
- const size_t m_level;
+ Compression_Stream* make_stream(size_t level) const override;
const byte m_os_code;
};
@@ -110,7 +78,6 @@ class BOTAN_DLL Gzip_Decompression final : public Stream_Decompression
{
public:
std::string name() const override { return "Gzip_Decompression"; }
-
private:
Compression_Stream* make_stream() const override;
};