diff options
Diffstat (limited to 'src/lib/compression/zlib')
-rw-r--r-- | src/lib/compression/zlib/info.txt | 7 | ||||
-rw-r--r-- | src/lib/compression/zlib/zlib.cpp | 106 | ||||
-rw-r--r-- | src/lib/compression/zlib/zlib.h | 61 |
3 files changed, 174 insertions, 0 deletions
diff --git a/src/lib/compression/zlib/info.txt b/src/lib/compression/zlib/info.txt new file mode 100644 index 000000000..ba5abd4ec --- /dev/null +++ b/src/lib/compression/zlib/info.txt @@ -0,0 +1,7 @@ +define ZLIB_TRANSFORM 20141118 + +load_on request + +<libs> +all -> z +</libs> diff --git a/src/lib/compression/zlib/zlib.cpp b/src/lib/compression/zlib/zlib.cpp new file mode 100644 index 000000000..b18621640 --- /dev/null +++ b/src/lib/compression/zlib/zlib.cpp @@ -0,0 +1,106 @@ +/* +* Zlib Compressor +* (C) 2001 Peter J Jones +* 2001-2007,2014 Jack Lloyd +* 2006 Matt Johnston +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/zlib.h> +#include <botan/internal/comp_util.h> +#include <zlib.h> + +namespace Botan { + +namespace { + +class Zlib_Stream : public Zlib_Style_Stream<z_stream, Bytef> + { + public: + Zlib_Stream() + { + streamp()->opaque = alloc(); + streamp()->zalloc = Compression_Alloc_Info::malloc<unsigned int>; + streamp()->zfree = Compression_Alloc_Info::free; + } + + u32bit run_flag() const override { return Z_NO_FLUSH; } + u32bit flush_flag() const override { return Z_FULL_FLUSH; } + u32bit finish_flag() const override { return Z_FINISH; } + }; + +class Zlib_Compression_Stream : public Zlib_Stream + { + public: + Zlib_Compression_Stream(size_t level, bool raw_deflate) + { + // FIXME: allow specifiying memLevel and strategy + int rc = deflateInit2(streamp(), level, Z_DEFLATED, + (raw_deflate ? -15 : 15), 8, Z_DEFAULT_STRATEGY); + if(rc != Z_OK) + throw std::runtime_error("zlib deflate initialization failed"); + } + + ~Zlib_Compression_Stream() + { + deflateEnd(streamp()); + } + + bool run(u32bit flags) override + { + int rc = deflate(streamp(), flags); + + if(rc == Z_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != Z_OK && rc != Z_STREAM_END) + throw std::runtime_error("zlib deflate error"); + + return (rc == Z_STREAM_END); + } + }; + +class Zlib_Decompression_Stream : public Zlib_Stream + { + public: + Zlib_Decompression_Stream(bool raw_deflate) + { + int rc = inflateInit2(streamp(), (raw_deflate ? -15 : 15)); + + if(rc == Z_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != Z_OK) + throw std::runtime_error("zlib inflate initialization failed"); + } + + ~Zlib_Decompression_Stream() + { + inflateEnd(streamp()); + } + + bool run(u32bit flags) override + { + int rc = inflate(streamp(), flags); + + if(rc == Z_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != Z_OK && rc != Z_STREAM_END) + throw std::runtime_error("zlib deflate error"); + + return (rc == Z_STREAM_END); + } + }; + +} + +Compression_Stream* Zlib_Compression::make_stream() const + { + return new Zlib_Compression_Stream(m_level, m_raw_deflate); + } + +Compression_Stream* Zlib_Decompression::make_stream() const + { + return new Zlib_Decompression_Stream(m_raw_deflate); + } + +} diff --git a/src/lib/compression/zlib/zlib.h b/src/lib/compression/zlib/zlib.h new file mode 100644 index 000000000..55da47a0d --- /dev/null +++ b/src/lib/compression/zlib/zlib.h @@ -0,0 +1,61 @@ +/* +* Zlib Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ZLIB_H__ +#define BOTAN_ZLIB_H__ + +#include <botan/compression.h> + +namespace Botan { + +/** +* Zlib Compression +*/ +class BOTAN_DLL Zlib_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 + * @param raw_deflate if true no zlib header/trailer will be used + */ + Zlib_Compression(size_t level = 6, bool raw_deflate = false) : + m_level(level), m_raw_deflate(raw_deflate) {} + + std::string name() const override { return "Zlib_Compression"; } + + private: + Compression_Stream* make_stream() const; + + const size_t m_level; + const bool m_raw_deflate; + }; + +/** +* Zlib Deccompression +*/ +class BOTAN_DLL Zlib_Decompression : public Stream_Decompression + { + public: + /** + * @param raw_deflate if true no zlib header/trailer will be used + */ + Zlib_Decompression( bool raw_deflate = false) : m_raw_deflate(raw_deflate) {} + + std::string name() const override { return "Zlib_Decompression"; } + + private: + Compression_Stream* make_stream() const; + + const bool m_raw_deflate; + }; + +} + +#endif |