2014-12-11 10:50:15 +03:00
|
|
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
|
2014-12-09 15:21:51 +03:00
|
|
|
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
|
2013-10-03 05:51:24 +04:00
|
|
|
|
2015-01-25 19:44:15 +03:00
|
|
|
#ifndef COMPRESSION_HH_INCLUDED
|
|
|
|
#define COMPRESSION_HH_INCLUDED
|
2013-10-03 05:51:24 +04:00
|
|
|
|
2013-10-08 03:17:15 +04:00
|
|
|
#include "sptr.hh"
|
2013-10-09 02:22:51 +04:00
|
|
|
#include "ex.hh"
|
2013-10-28 03:47:46 +04:00
|
|
|
#include "nocopy.hh"
|
2015-01-23 14:32:40 +03:00
|
|
|
#include "config.hh"
|
2013-10-09 02:22:51 +04:00
|
|
|
|
|
|
|
namespace Compression {
|
|
|
|
|
|
|
|
DEF_EX( Ex, "Compression exception", std::exception )
|
2015-01-20 17:55:58 +03:00
|
|
|
DEF_EX_STR( exUnsupportedCompressionMethod, "Unsupported compression method:", Ex )
|
2013-10-03 05:51:24 +04:00
|
|
|
|
|
|
|
// used for encoding or decoding
|
2013-10-28 03:47:46 +04:00
|
|
|
class EnDecoder: NoCopy
|
2013-10-08 03:17:15 +04:00
|
|
|
{
|
2013-10-03 05:51:24 +04:00
|
|
|
protected:
|
|
|
|
EnDecoder();
|
|
|
|
public:
|
|
|
|
virtual ~EnDecoder();
|
|
|
|
|
|
|
|
// encoder can read up to size bytes from data
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual void setInput ( const void * data, size_t size ) = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
// how many bytes of the last input haven't been used, yet?
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual size_t getAvailableInput() = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
|
|
|
|
// encoder can write up to size bytes to output
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual void setOutput( void * data, size_t size ) = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
// how many bytes of free space are remaining in the output buffer
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual size_t getAvailableOutput() = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
|
|
|
|
// process some bytes
|
|
|
|
// finish: will you pass more data to the encoder via setOutput?
|
|
|
|
// NOTE You must eventually set finish to true.
|
|
|
|
// returns, whether all output bytes have been written
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual bool process( bool finish ) = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// compression method
|
2013-10-09 02:22:51 +04:00
|
|
|
class CompressionMethod
|
2013-10-08 03:17:15 +04:00
|
|
|
{
|
2013-10-03 05:51:24 +04:00
|
|
|
public:
|
2013-10-09 02:22:51 +04:00
|
|
|
virtual ~CompressionMethod();
|
2013-10-03 05:51:24 +04:00
|
|
|
|
|
|
|
// returns name of compression method
|
|
|
|
// This name is saved in the file header of the compressed file.
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual std::string getName() const = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
|
2015-01-23 14:32:40 +03:00
|
|
|
virtual sptr< EnDecoder > createEncoder( Config const & ) const = 0;
|
2015-01-20 17:55:58 +03:00
|
|
|
virtual sptr< EnDecoder > createEncoder() const = 0;
|
|
|
|
virtual sptr< EnDecoder > createDecoder() const = 0;
|
2013-10-03 05:51:24 +04:00
|
|
|
|
|
|
|
// find a compression by name
|
2013-10-09 02:22:51 +04:00
|
|
|
// If optional is false, it will either return a valid CompressionMethod
|
2013-10-08 03:17:15 +04:00
|
|
|
// object or abort the program. If optional is true, it will return
|
|
|
|
// NULL, if it cannot find the a compression with that name.
|
2015-01-20 17:55:58 +03:00
|
|
|
static const_sptr< CompressionMethod > findCompression(
|
|
|
|
const std::string & name, bool optional = false );
|
|
|
|
|
|
|
|
static const_sptr< CompressionMethod > selectedCompression;
|
2013-10-03 05:51:24 +04:00
|
|
|
|
2015-01-20 17:55:58 +03:00
|
|
|
static const_sptr< CompressionMethod > const compressions[];
|
2013-10-03 11:32:58 +04:00
|
|
|
|
2013-10-08 03:17:15 +04:00
|
|
|
class iterator
|
|
|
|
{
|
2013-10-09 02:22:51 +04:00
|
|
|
friend class CompressionMethod;
|
2013-10-03 12:24:47 +04:00
|
|
|
|
2015-01-20 17:55:58 +03:00
|
|
|
const const_sptr< CompressionMethod > * ptr;
|
|
|
|
iterator( const const_sptr< CompressionMethod > * ptr );
|
2013-10-03 11:32:58 +04:00
|
|
|
public:
|
2015-01-20 17:55:58 +03:00
|
|
|
iterator( const iterator & it );
|
|
|
|
iterator & operator =( const iterator & it );
|
2013-10-03 11:32:58 +04:00
|
|
|
|
2015-01-20 17:55:58 +03:00
|
|
|
bool operator == ( const iterator & other ) const;
|
|
|
|
bool operator != ( const iterator & other ) const;
|
2013-10-03 11:32:58 +04:00
|
|
|
|
2013-10-09 01:18:39 +04:00
|
|
|
bool atEnd() const;
|
2013-10-03 11:32:58 +04:00
|
|
|
|
2015-01-20 17:55:58 +03:00
|
|
|
iterator & operator ++();
|
2013-10-03 11:32:58 +04:00
|
|
|
|
2015-01-20 17:55:58 +03:00
|
|
|
const_sptr< CompressionMethod > operator * ();
|
2013-10-03 11:32:58 +04:00
|
|
|
};
|
|
|
|
static iterator begin();
|
|
|
|
static iterator end();
|
2013-10-03 05:51:24 +04:00
|
|
|
};
|
|
|
|
|
2013-10-09 02:22:51 +04:00
|
|
|
}
|
|
|
|
|
2013-10-03 05:51:24 +04:00
|
|
|
#endif
|