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/lzma | |
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/lzma')
-rw-r--r-- | src/lib/compression/lzma/info.txt | 7 | ||||
-rw-r--r-- | src/lib/compression/lzma/lzma.cpp | 94 | ||||
-rw-r--r-- | src/lib/compression/lzma/lzma.h | 51 |
3 files changed, 152 insertions, 0 deletions
diff --git a/src/lib/compression/lzma/info.txt b/src/lib/compression/lzma/info.txt new file mode 100644 index 000000000..1ec668ca8 --- /dev/null +++ b/src/lib/compression/lzma/info.txt @@ -0,0 +1,7 @@ +define LZMA_TRANSFORM 20141118 + +load_on request + +<libs> +all -> lzma +</libs> diff --git a/src/lib/compression/lzma/lzma.cpp b/src/lib/compression/lzma/lzma.cpp new file mode 100644 index 000000000..d145ed305 --- /dev/null +++ b/src/lib/compression/lzma/lzma.cpp @@ -0,0 +1,94 @@ +/* +* Lzma Compressor +* (C) 2001 Peter J Jones +* 2001-2007,2014 Jack Lloyd +* 2006 Matt Johnston +* 2012 Vojtech Kral +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/lzma.h> +#include <botan/internal/comp_util.h> +#include <lzma.h> + +namespace Botan { + +namespace { + +class LZMA_Stream : public Zlib_Style_Stream<lzma_stream, byte> + { + public: + LZMA_Stream() + { + streamp()->allocator = new ::lzma_allocator; + streamp()->allocator->opaque = alloc(); + streamp()->allocator->alloc = Compression_Alloc_Info::malloc<size_t>; + streamp()->allocator->free = Compression_Alloc_Info::free; + } + + ~LZMA_Stream() + { + ::lzma_end(streamp()); + delete streamp()->allocator; + } + + bool run(u32bit flags) override + { + lzma_ret rc = ::lzma_code(streamp(), static_cast<lzma_action>(flags)); + + if(rc == LZMA_MEM_ERROR) + throw std::bad_alloc(); + else if (rc != LZMA_OK && rc != LZMA_STREAM_END) + throw std::runtime_error("Lzma error"); + + return (rc == LZMA_STREAM_END); + } + + u32bit run_flag() const override { return LZMA_RUN; } + u32bit flush_flag() const override { return LZMA_FULL_FLUSH; } + u32bit finish_flag() const override { return LZMA_FINISH; } + }; + +class LZMA_Compression_Stream : public LZMA_Stream + { + public: + LZMA_Compression_Stream(size_t level) + { + lzma_ret rc = ::lzma_easy_encoder(streamp(), level, LZMA_CHECK_CRC64); + + if(rc == LZMA_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != LZMA_OK) + throw std::runtime_error("lzma compress initialization failed"); + } + }; + +class LZMA_Decompression_Stream : public LZMA_Stream + { + public: + LZMA_Decompression_Stream() + { + lzma_ret rc = ::lzma_stream_decoder(streamp(), UINT64_MAX, + LZMA_TELL_UNSUPPORTED_CHECK); + + if(rc == LZMA_MEM_ERROR) + throw std::bad_alloc(); + else if(rc != LZMA_OK) + throw std::runtime_error("Bad setting in lzma_stream_decoder"); + } + }; + +} + +Compression_Stream* LZMA_Compression::make_stream() const + { + return new LZMA_Compression_Stream(m_level); + } + +Compression_Stream* LZMA_Decompression::make_stream() const + { + return new LZMA_Decompression_Stream; + } + +} diff --git a/src/lib/compression/lzma/lzma.h b/src/lib/compression/lzma/lzma.h new file mode 100644 index 000000000..0761a8194 --- /dev/null +++ b/src/lib/compression/lzma/lzma.h @@ -0,0 +1,51 @@ +/* +* Lzma Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* 2012 Vojtech Kral +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LZMA_H__ +#define BOTAN_LZMA_H__ + +#include <botan/compression.h> + +namespace Botan { + +/** +* LZMA Compression +*/ +class BOTAN_DLL LZMA_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 + */ + LZMA_Compression(size_t level = 6) : m_level(level) {} + + std::string name() const override { return "LZMA_Compression"; } + + private: + Compression_Stream* make_stream() const; + + const size_t m_level; + }; + +/** +* LZMA Deccompression +*/ +class BOTAN_DLL LZMA_Decompression : public Stream_Decompression + { + public: + std::string name() const override { return "LZMA_Decompression"; } + private: + Compression_Stream* make_stream() const; + }; + +} + +#endif |