]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/src/lzma.cpp
1 // (C) Copyright Milan Svoboda 2008.
2 // Originally developed under the fusecompress project.
3 // Based on bzip2.cpp by:
4 // (C) Copyright Jonathan Turkanis 2003.
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
8 // See http://www.boost.org/libs/iostreams for documentation.
10 // Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
11 // knows that we are building the library (possibly exporting code), rather
12 // than using it (possibly importing code).
13 #define BOOST_IOSTREAMS_SOURCE
17 #include <boost/throw_exception.hpp>
18 #include <boost/iostreams/detail/config/dyn_link.hpp>
19 #include <boost/iostreams/filter/lzma.hpp>
21 namespace boost
{ namespace iostreams
{
27 const uint32_t no_compression
= 0;
28 const uint32_t best_speed
= 1;
29 const uint32_t best_compression
= 9;
30 const uint32_t default_compression
= 2;
34 const int okay
= LZMA_OK
;
35 const int stream_end
= LZMA_STREAM_END
;
36 const int unsupported_check
= LZMA_UNSUPPORTED_CHECK
;
37 const int mem_error
= LZMA_MEM_ERROR
;
38 const int options_error
= LZMA_OPTIONS_ERROR
;
39 const int data_error
= LZMA_DATA_ERROR
;
40 const int buf_error
= LZMA_BUF_ERROR
;
41 const int prog_error
= LZMA_PROG_ERROR
;
45 const int finish
= LZMA_FINISH
;
46 const int full_flush
= LZMA_FULL_FLUSH
;
47 const int sync_flush
= LZMA_SYNC_FLUSH
;
48 const int run
= LZMA_RUN
;
50 // Code for current OS
52 } // End namespace lzma.
54 //------------------Implementation of lzma_error------------------------------//
56 lzma_error::lzma_error(int error
)
57 : BOOST_IOSTREAMS_FAILURE("lzma error"), error_(error
)
60 void lzma_error::check
BOOST_PREVENT_MACRO_SUBSTITUTION(int error
)
67 boost::throw_exception(std::bad_alloc());
69 boost::throw_exception(lzma_error(error
));
73 //------------------Implementation of lzma_base-------------------------------//
77 lzma_base::lzma_base()
78 : stream_(new lzma_stream
)
81 lzma_base::~lzma_base() { delete static_cast<lzma_stream
*>(stream_
); }
83 void lzma_base::before( const char*& src_begin
, const char* src_end
,
84 char*& dest_begin
, char* dest_end
)
86 lzma_stream
* s
= static_cast<lzma_stream
*>(stream_
);
87 s
->next_in
= reinterpret_cast<uint8_t*>(const_cast<char*>(src_begin
));
88 s
->avail_in
= static_cast<size_t>(src_end
- src_begin
);
89 s
->next_out
= reinterpret_cast<uint8_t*>(dest_begin
);
90 s
->avail_out
= static_cast<size_t>(dest_end
- dest_begin
);
93 void lzma_base::after(const char*& src_begin
, char*& dest_begin
, bool)
95 lzma_stream
* s
= static_cast<lzma_stream
*>(stream_
);
96 src_begin
= const_cast<const char*>(reinterpret_cast<const char*>(s
->next_in
));
97 dest_begin
= reinterpret_cast<char*>(s
->next_out
);
100 int lzma_base::deflate(int action
)
102 return lzma_code(static_cast<lzma_stream
*>(stream_
), static_cast<lzma_action
>(action
));
105 int lzma_base::inflate(int action
)
107 return lzma_code(static_cast<lzma_stream
*>(stream_
), static_cast<lzma_action
>(action
));
110 void lzma_base::reset(bool compress
, bool realloc
)
112 lzma_stream
* s
= static_cast<lzma_stream
*>(stream_
);
116 memset(s
, 0, sizeof(*s
));
118 lzma_error::check
BOOST_PREVENT_MACRO_SUBSTITUTION(
120 lzma_easy_encoder(s
, level
, LZMA_CHECK_CRC32
) :
121 lzma_stream_decoder(s
, 100 * 1024 * 1024, LZMA_CONCATENATED
)
126 void lzma_base::do_init
127 ( const lzma_params
& p
, bool compress
,
128 lzma::alloc_func
, lzma::free_func
,
131 lzma_stream
* s
= static_cast<lzma_stream
*>(stream_
);
133 memset(s
, 0, sizeof(*s
));
136 lzma_error::check
BOOST_PREVENT_MACRO_SUBSTITUTION(
138 lzma_easy_encoder(s
, p
.level
, LZMA_CHECK_CRC32
) :
139 lzma_stream_decoder(s
, 100 * 1024 * 1024, LZMA_CONCATENATED
)
143 } // End namespace detail.
145 //----------------------------------------------------------------------------//
147 } } // End namespaces iostreams, boost.