diff options
author | lloyd <[email protected]> | 2014-11-18 13:42:36 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-11-18 13:42:36 +0000 |
commit | dec2e0d9e493b53c48ca26b47028ab309d3cc586 (patch) | |
tree | 2242f756ece5e1a9cc728dc208c154e694314d5a /src/lib/compression/bzip2 | |
parent | b5e2a1ce3044084e43ff523f148b0cc8e95ce283 (diff) |
Convert compression filters to in-place transforms and refactor
to minimize the amount of logic needed in the files specific to each
library.
Diffstat (limited to 'src/lib/compression/bzip2')
-rw-r--r-- | src/lib/compression/bzip2/bzip2.cpp | 112 | ||||
-rw-r--r-- | src/lib/compression/bzip2/bzip2.h | 50 | ||||
-rw-r--r-- | src/lib/compression/bzip2/info.txt | 7 |
3 files changed, 169 insertions, 0 deletions
diff --git a/src/lib/compression/bzip2/bzip2.cpp b/src/lib/compression/bzip2/bzip2.cpp new file mode 100644 index 000000000..440c21477 --- /dev/null +++ b/src/lib/compression/bzip2/bzip2.cpp @@ -0,0 +1,112 @@ +/* +* Bzip Compressor +* (C) 2001 Peter J Jones +* 2001-2007,2014 Jack Lloyd +* 2006 Matt Johnston +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bzip2.h> +#include <botan/internal/comp_util.h> + +#define BZ_NO_STDIO +#include <bzlib.h> + +namespace Botan { + +namespace { + +class Bzip_Stream : public Zlib_Style_Stream<bz_stream, char> + { + public: + Bzip_Stream() + { + streamp()->opaque = alloc(); + streamp()->bzalloc = Compression_Alloc_Info::malloc<int>; + streamp()->bzfree = Compression_Alloc_Info::free; + } + + u32bit run_flag() const override { return BZ_RUN; } + u32bit flush_flag() const override { return BZ_FLUSH; } + u32bit finish_flag() const override { return BZ_FINISH; } + }; + +class Bzip_Compression_Stream : public Bzip_Stream + { + public: + Bzip_Compression_Stream(size_t level) + { + int rc = BZ2_bzCompressInit(streamp(), level, 0, 0); + + if(rc == BZ_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != BZ_OK) + throw std::runtime_error("bzip compress initialization failed"); + } + + ~Bzip_Compression_Stream() + { + BZ2_bzCompressEnd(streamp()); + } + + bool run(u32bit flags) override + { + int rc = BZ2_bzCompress(streamp(), flags); + + if(rc == BZ_MEM_ERROR) + throw std::bad_alloc(); + else if(rc < 0) + throw std::runtime_error("bzip compress error"); + + return (rc == BZ_STREAM_END); + } + + private: + size_t m_level; + }; + +class Bzip_Decompression_Stream : public Bzip_Stream + { + public: + Bzip_Decompression_Stream() + { + int rc = BZ2_bzDecompressInit(streamp(), 0, 0); + + if(rc == BZ_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != BZ_OK) + throw std::runtime_error("bzip decompress initialization failed"); + } + + ~Bzip_Decompression_Stream() + { + BZ2_bzDecompressEnd(streamp()); + } + + bool run(u32bit flags) override + { + int rc = BZ2_bzDecompress(streamp()); + + if(rc == BZ_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != BZ_OK && rc != BZ_STREAM_END) + throw std::runtime_error("bzip decompress error"); + + return (rc == BZ_STREAM_END); + } + }; + +} + +Compression_Stream* Bzip_Compression::make_stream() const + { + return new Bzip_Compression_Stream(m_level); + } + +Compression_Stream* Bzip_Decompression::make_stream() const + { + return new Bzip_Decompression_Stream; + } + +} diff --git a/src/lib/compression/bzip2/bzip2.h b/src/lib/compression/bzip2/bzip2.h new file mode 100644 index 000000000..945f6a051 --- /dev/null +++ b/src/lib/compression/bzip2/bzip2.h @@ -0,0 +1,50 @@ +/* +* Bzip Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BZIP2_H__ +#define BOTAN_BZIP2_H__ + +#include <botan/compression.h> + +namespace Botan { + +/** +* Bzip Compression +*/ +class BOTAN_DLL Bzip_Compression : 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 + */ + Bzip_Compression(size_t level = 6) : m_level(level) {} + + std::string name() const override { return "Bzip_Compression"; } + + private: + Compression_Stream* make_stream() const; + + const size_t m_level; + }; + +/** +* Bzip Deccompression +*/ +class BOTAN_DLL Bzip_Decompression : public Stream_Decompression + { + public: + std::string name() const override { return "Bzip_Decompression"; } + private: + Compression_Stream* make_stream() const; + }; + +} + +#endif diff --git a/src/lib/compression/bzip2/info.txt b/src/lib/compression/bzip2/info.txt new file mode 100644 index 000000000..a0f8d82ee --- /dev/null +++ b/src/lib/compression/bzip2/info.txt @@ -0,0 +1,7 @@ +define BZIP2_TRANSFORM 20141118 + +load_on request + +<libs> +all -> bz2 +</libs> |