1 // (C) Copyright Milan Svoboda 2008.
2 // Originally developed under the fusecompress project.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 // Note: custom allocators are not supported on VC6, since that compiler
9 // had trouble finding the function lzma_base::do_init.
11 #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
19 #include <iosfwd> // streamsize.
20 #include <memory> // allocator, bad_alloc.
22 #include <boost/config.hpp> // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM.
23 #include <boost/detail/workaround.hpp>
24 #include <boost/iostreams/constants.hpp> // buffer size.
25 #include <boost/iostreams/detail/config/auto_link.hpp>
26 #include <boost/iostreams/detail/config/dyn_link.hpp>
27 #include <boost/iostreams/detail/config/wide_streams.hpp>
28 #include <boost/iostreams/detail/ios.hpp> // failure, streamsize.
29 #include <boost/iostreams/filter/symmetric.hpp>
30 #include <boost/iostreams/pipeline.hpp>
31 #include <boost/type_traits/is_same.hpp>
35 # pragma warning(push)
36 # pragma warning(disable:4251 4231 4660) // Dependencies not exported.
38 #include <boost/config/abi_prefix.hpp>
40 namespace boost { namespace iostreams {
44 typedef void* (*alloc_func)(void*, size_t, size_t);
45 typedef void (*free_func)(void*, void*);
49 BOOST_IOSTREAMS_DECL extern const uint32_t no_compression;
50 BOOST_IOSTREAMS_DECL extern const uint32_t best_speed;
51 BOOST_IOSTREAMS_DECL extern const uint32_t best_compression;
52 BOOST_IOSTREAMS_DECL extern const uint32_t default_compression;
56 BOOST_IOSTREAMS_DECL extern const int okay;
57 BOOST_IOSTREAMS_DECL extern const int stream_end;
58 BOOST_IOSTREAMS_DECL extern const int unsupported_check;
59 BOOST_IOSTREAMS_DECL extern const int mem_error;
60 BOOST_IOSTREAMS_DECL extern const int options_error;
61 BOOST_IOSTREAMS_DECL extern const int data_error;
62 BOOST_IOSTREAMS_DECL extern const int buf_error;
63 BOOST_IOSTREAMS_DECL extern const int prog_error;
67 BOOST_IOSTREAMS_DECL extern const int finish;
68 BOOST_IOSTREAMS_DECL extern const int full_flush;
69 BOOST_IOSTREAMS_DECL extern const int sync_flush;
70 BOOST_IOSTREAMS_DECL extern const int run;
72 // Code for current OS
74 // Null pointer constant.
80 } // End namespace lzma.
83 // Class name: lzma_params.
84 // Description: Encapsulates the parameters passed to lzmadec_init
85 // to customize compression and decompression.
89 // Non-explicit constructor.
90 lzma_params( uint32_t level = lzma::default_compression, uint32_t threads = 1 )
99 // Class name: lzma_error.
100 // Description: Subclass of std::ios::failure thrown to indicate
101 // lzma errors other than out-of-memory conditions.
103 class BOOST_IOSTREAMS_DECL lzma_error : public BOOST_IOSTREAMS_FAILURE {
105 explicit lzma_error(int error);
106 int error() const { return error_; }
107 static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
114 template<typename Alloc>
115 struct lzma_allocator_traits {
116 #ifndef BOOST_NO_STD_ALLOCATOR
117 #if defined(BOOST_NO_CXX11_ALLOCATOR)
118 typedef typename Alloc::template rebind<char>::other type;
120 typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
123 typedef std::allocator<char> type;
127 template< typename Alloc,
128 typename Base = // VC6 workaround (C2516)
129 BOOST_DEDUCED_TYPENAME lzma_allocator_traits<Alloc>::type >
130 struct lzma_allocator : private Base {
132 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
133 typedef typename Base::size_type size_type;
135 typedef typename std::allocator_traits<Base>::size_type size_type;
138 BOOST_STATIC_CONSTANT(bool, custom =
139 (!is_same<std::allocator<char>, Base>::value));
140 typedef typename lzma_allocator_traits<Alloc>::type allocator_type;
141 static void* allocate(void* self, size_t items, size_t size);
142 static void deallocate(void* self, void* address);
145 class BOOST_IOSTREAMS_DECL lzma_base {
147 typedef char char_type;
151 void* stream() { return stream_; }
152 template<typename Alloc>
153 void init( const lzma_params& p,
155 lzma_allocator<Alloc>& zalloc )
157 bool custom = lzma_allocator<Alloc>::custom;
158 do_init( p, compress,
159 custom ? lzma_allocator<Alloc>::allocate : 0,
160 custom ? lzma_allocator<Alloc>::deallocate : 0,
163 void before( const char*& src_begin, const char* src_end,
164 char*& dest_begin, char* dest_end );
165 void after( const char*& src_begin, char*& dest_begin,
167 int deflate(int action);
168 int inflate(int action);
169 void reset(bool compress, bool realloc);
171 void do_init( const lzma_params& p, bool compress,
175 void init_stream(bool compress);
176 void* stream_; // Actual type: lzma_stream*.
182 // Template name: lzma_compressor_impl
183 // Description: Model of C-Style Filter implementing compression by
184 // delegating to the lzma function deflate.
186 template<typename Alloc = std::allocator<char> >
187 class lzma_compressor_impl : public lzma_base, public lzma_allocator<Alloc> {
189 lzma_compressor_impl(const lzma_params& = lzma_params());
190 ~lzma_compressor_impl();
191 bool filter( const char*& src_begin, const char* src_end,
192 char*& dest_begin, char* dest_end, bool flush );
197 // Template name: lzma_compressor_impl
198 // Description: Model of C-Style Filte implementing decompression by
199 // delegating to the lzma function inflate.
201 template<typename Alloc = std::allocator<char> >
202 class lzma_decompressor_impl : public lzma_base, public lzma_allocator<Alloc> {
204 lzma_decompressor_impl(const lzma_params&);
205 lzma_decompressor_impl();
206 ~lzma_decompressor_impl();
207 bool filter( const char*& begin_in, const char* end_in,
208 char*& begin_out, char* end_out, bool flush );
212 } // End namespace detail.
215 // Template name: lzma_compressor
216 // Description: Model of InputFilter and OutputFilter implementing
217 // compression using lzma.
219 template<typename Alloc = std::allocator<char> >
220 struct basic_lzma_compressor
221 : symmetric_filter<detail::lzma_compressor_impl<Alloc>, Alloc>
224 typedef detail::lzma_compressor_impl<Alloc> impl_type;
225 typedef symmetric_filter<impl_type, Alloc> base_type;
227 typedef typename base_type::char_type char_type;
228 typedef typename base_type::category category;
229 basic_lzma_compressor( const lzma_params& = lzma_params(),
230 std::streamsize buffer_size = default_device_buffer_size );
232 BOOST_IOSTREAMS_PIPABLE(basic_lzma_compressor, 1)
234 typedef basic_lzma_compressor<> lzma_compressor;
237 // Template name: lzma_decompressor
238 // Description: Model of InputFilter and OutputFilter implementing
239 // decompression using lzma.
241 template<typename Alloc = std::allocator<char> >
242 struct basic_lzma_decompressor
243 : symmetric_filter<detail::lzma_decompressor_impl<Alloc>, Alloc>
246 typedef detail::lzma_decompressor_impl<Alloc> impl_type;
247 typedef symmetric_filter<impl_type, Alloc> base_type;
249 typedef typename base_type::char_type char_type;
250 typedef typename base_type::category category;
251 basic_lzma_decompressor( std::streamsize buffer_size = default_device_buffer_size );
252 basic_lzma_decompressor( const lzma_params& p,
253 std::streamsize buffer_size = default_device_buffer_size );
255 BOOST_IOSTREAMS_PIPABLE(basic_lzma_decompressor, 1)
257 typedef basic_lzma_decompressor<> lzma_decompressor;
259 //----------------------------------------------------------------------------//
261 //------------------Implementation of lzma_allocator--------------------------//
265 template<typename Alloc, typename Base>
266 void* lzma_allocator<Alloc, Base>::allocate
267 (void* self, size_t items, size_t size)
269 size_type len = items * size;
271 static_cast<allocator_type*>(self)->allocate
272 (len + sizeof(size_type)
273 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
277 *reinterpret_cast<size_type*>(ptr) = len;
278 return ptr + sizeof(size_type);
281 template<typename Alloc, typename Base>
282 void lzma_allocator<Alloc, Base>::deallocate(void* self, void* address)
284 char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
285 size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
286 static_cast<allocator_type*>(self)->deallocate(ptr, len);
289 //------------------Implementation of lzma_compressor_impl--------------------//
291 template<typename Alloc>
292 lzma_compressor_impl<Alloc>::lzma_compressor_impl(const lzma_params& p)
293 { init(p, true, static_cast<lzma_allocator<Alloc>&>(*this)); }
295 template<typename Alloc>
296 lzma_compressor_impl<Alloc>::~lzma_compressor_impl()
297 { reset(true, false); }
299 template<typename Alloc>
300 bool lzma_compressor_impl<Alloc>::filter
301 ( const char*& src_begin, const char* src_end,
302 char*& dest_begin, char* dest_end, bool flush )
304 before(src_begin, src_end, dest_begin, dest_end);
305 int result = deflate(flush ? lzma::finish : lzma::run);
306 after(src_begin, dest_begin, true);
307 lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
308 return result != lzma::stream_end;
311 template<typename Alloc>
312 void lzma_compressor_impl<Alloc>::close() { reset(true, true); }
314 //------------------Implementation of lzma_decompressor_impl------------------//
316 template<typename Alloc>
317 lzma_decompressor_impl<Alloc>::lzma_decompressor_impl(const lzma_params& p)
318 { init(p, false, static_cast<lzma_allocator<Alloc>&>(*this)); }
320 template<typename Alloc>
321 lzma_decompressor_impl<Alloc>::~lzma_decompressor_impl()
322 { reset(false, false); }
324 template<typename Alloc>
325 lzma_decompressor_impl<Alloc>::lzma_decompressor_impl()
328 init(p, false, static_cast<lzma_allocator<Alloc>&>(*this));
331 template<typename Alloc>
332 bool lzma_decompressor_impl<Alloc>::filter
333 ( const char*& src_begin, const char* src_end,
334 char*& dest_begin, char* dest_end, bool flush )
336 before(src_begin, src_end, dest_begin, dest_end);
337 int result = inflate(flush ? lzma::finish : lzma::run);
338 after(src_begin, dest_begin, false);
339 lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
340 return result != lzma::stream_end;
343 template<typename Alloc>
344 void lzma_decompressor_impl<Alloc>::close() { reset(false, true); }
346 } // End namespace detail.
348 //------------------Implementation of lzma_compressor-----------------------//
350 template<typename Alloc>
351 basic_lzma_compressor<Alloc>::basic_lzma_compressor
352 (const lzma_params& p, std::streamsize buffer_size)
353 : base_type(buffer_size, p) { }
355 //------------------Implementation of lzma_decompressor-----------------------//
357 template<typename Alloc>
358 basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
359 (std::streamsize buffer_size)
360 : base_type(buffer_size) { }
362 template<typename Alloc>
363 basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
364 (const lzma_params& p, std::streamsize buffer_size)
365 : base_type(buffer_size, p) { }
367 //----------------------------------------------------------------------------//
369 } } // End namespaces iostreams, boost.
371 #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
373 # pragma warning(pop)
376 #endif // #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED