Botan  1.11.15
src/lib/compression/compression.h
Go to the documentation of this file.
00001 /*
00002 * Compression Transform
00003 * (C) 2014 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_COMPRESSION_TRANSFORM_H__
00009 #define BOTAN_COMPRESSION_TRANSFORM_H__
00010 
00011 #include <botan/transform.h>
00012 
00013 namespace Botan {
00014 
00015 class BOTAN_DLL Compressor_Transform : public Transform
00016    {
00017    public:
00018       size_t update_granularity() const override { return 1; }
00019 
00020       size_t minimum_final_size() const override { return 0; }
00021 
00022       size_t default_nonce_length() const override { return 0; }
00023 
00024       bool valid_nonce_length(size_t nonce_len) const override
00025          { return nonce_len == 0; }
00026 
00027       virtual void flush(secure_vector<byte>& buf, size_t offset = 0) { update(buf, offset); }
00028 
00029       size_t output_length(size_t) const override
00030          {
00031          throw std::runtime_error(name() + " output length indeterminate");
00032          }
00033    };
00034 
00035 BOTAN_DLL Transform* make_compressor(const std::string& type, size_t level);
00036 BOTAN_DLL Transform* make_decompressor(const std::string& type);
00037 
00038 class Compression_Stream
00039    {
00040    public:
00041       virtual ~Compression_Stream() {}
00042 
00043       virtual void next_in(byte* b, size_t len) = 0;
00044 
00045       virtual void next_out(byte* b, size_t len) = 0;
00046 
00047       virtual size_t avail_in() const = 0;
00048 
00049       virtual size_t avail_out() const = 0;
00050 
00051       virtual u32bit run_flag() const = 0;
00052       virtual u32bit flush_flag() const = 0;
00053       virtual u32bit finish_flag() const = 0;
00054 
00055       virtual bool run(u32bit flags) = 0;
00056    };
00057 
00058 class BOTAN_DLL Stream_Compression : public Compressor_Transform
00059    {
00060    public:
00061       void update(secure_vector<byte>& buf, size_t offset = 0) override;
00062 
00063       void flush(secure_vector<byte>& buf, size_t offset = 0) override;
00064 
00065       void finish(secure_vector<byte>& buf, size_t offset = 0) override;
00066 
00067       void clear() override;
00068    private:
00069       secure_vector<byte> start_raw(const byte[], size_t) override;
00070 
00071       void process(secure_vector<byte>& buf, size_t offset, u32bit flags);
00072 
00073       virtual Compression_Stream* make_stream() const = 0;
00074 
00075       secure_vector<byte> m_buffer;
00076       std::unique_ptr<Compression_Stream> m_stream;
00077    };
00078 
00079 class BOTAN_DLL Stream_Decompression : public Compressor_Transform
00080    {
00081    public:
00082       void update(secure_vector<byte>& buf, size_t offset = 0) override;
00083 
00084       void finish(secure_vector<byte>& buf, size_t offset = 0) override;
00085 
00086       void clear() override;
00087 
00088    private:
00089       secure_vector<byte> start_raw(const byte[], size_t) override;
00090 
00091       void process(secure_vector<byte>& buf, size_t offset, u32bit flags);
00092 
00093       virtual Compression_Stream* make_stream() const = 0;
00094 
00095       secure_vector<byte> m_buffer;
00096       std::unique_ptr<Compression_Stream> m_stream;
00097    };
00098 
00099 }
00100 
00101 #endif