11 #include <botan/lzma.h>
12 #include <botan/internal/compress_utils.h>
13 #include <botan/exceptn.h>
20 class LZMA_Stream :
public Zlib_Style_Stream<lzma_stream, uint8_t>
25 auto a = new ::lzma_allocator;
27 a->alloc = Compression_Alloc_Info::malloc<size_t>;
29 streamp()->allocator = a;
34 ::lzma_end(streamp());
35 delete streamp()->allocator;
38 bool run(uint32_t
flags)
override
40 lzma_ret rc = ::lzma_code(streamp(), static_cast<lzma_action>(flags));
42 if(rc == LZMA_MEM_ERROR)
43 throw Exception(
"lzma memory allocation failed");
44 else if (rc != LZMA_OK && rc != LZMA_STREAM_END)
45 throw Exception(
"Lzma error");
47 return (rc == LZMA_STREAM_END);
50 uint32_t run_flag()
const override {
return LZMA_RUN; }
51 uint32_t flush_flag()
const override {
return LZMA_FULL_FLUSH; }
52 uint32_t finish_flag()
const override {
return LZMA_FINISH; }
55 class LZMA_Compression_Stream :
public LZMA_Stream
58 explicit LZMA_Compression_Stream(
size_t level)
65 lzma_ret rc = ::lzma_easy_encoder(streamp(), level, LZMA_CHECK_CRC64);
67 if(rc == LZMA_MEM_ERROR)
68 throw Exception(
"lzma memory allocation failed");
69 else if(rc != LZMA_OK)
70 throw Exception(
"lzma compress initialization failed");
74 class LZMA_Decompression_Stream :
public LZMA_Stream
77 LZMA_Decompression_Stream()
79 lzma_ret rc = ::lzma_stream_decoder(streamp(), UINT64_MAX,
80 LZMA_TELL_UNSUPPORTED_CHECK);
82 if(rc == LZMA_MEM_ERROR)
83 throw Exception(
"lzma memory allocation failed");
84 else if(rc != LZMA_OK)
85 throw Exception(
"Bad setting in lzma_stream_decoder");
91 Compression_Stream* LZMA_Compression::make_stream(
size_t level)
const
93 return new LZMA_Compression_Stream(level);
96 Compression_Stream* LZMA_Decompression::make_stream()
const
98 return new LZMA_Decompression_Stream;
static void free(void *self, void *ptr)