]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/iostreams/filter/lzma.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / iostreams / filter / lzma.hpp
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.)
5
6 // See http://www.boost.org/libs/iostreams for documentation.
7
8 // Note: custom allocators are not supported on VC6, since that compiler
9 // had trouble finding the function lzma_base::do_init.
10
11 #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
13
14 #if defined(_MSC_VER)
15 # pragma once
16 #endif
17
18 #include <cassert>
19 #include <iosfwd> // streamsize.
20 #include <memory> // allocator, bad_alloc.
21 #include <new>
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>
32
33 // Must come last.
34 #ifdef BOOST_MSVC
35 # pragma warning(push)
36 # pragma warning(disable:4251 4231 4660) // Dependencies not exported.
37 #endif
38 #include <boost/config/abi_prefix.hpp>
39
40 namespace boost { namespace iostreams {
41
42 namespace lzma {
43
44 typedef void* (*alloc_func)(void*, size_t, size_t);
45 typedef void (*free_func)(void*, void*);
46
47 // Compression levels
48
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;
53
54 // Status codes
55
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;
64
65 // Flush codes
66
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;
71
72 // Code for current OS
73
74 // Null pointer constant.
75
76 const int null = 0;
77
78 // Default values
79
80 } // End namespace lzma.
81
82 //
83 // Class name: lzma_params.
84 // Description: Encapsulates the parameters passed to lzmadec_init
85 // to customize compression and decompression.
86 //
87 struct lzma_params {
88
89 // Non-explicit constructor.
90 lzma_params( uint32_t level = lzma::default_compression, uint32_t threads = 1 )
91 : level(level)
92 , threads(threads)
93 { }
94 uint32_t level;
95 uint32_t threads;
96 };
97
98 //
99 // Class name: lzma_error.
100 // Description: Subclass of std::ios::failure thrown to indicate
101 // lzma errors other than out-of-memory conditions.
102 //
103 class BOOST_IOSTREAMS_DECL lzma_error : public BOOST_IOSTREAMS_FAILURE {
104 public:
105 explicit lzma_error(int error);
106 int error() const { return error_; }
107 static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
108 private:
109 int error_;
110 };
111
112 namespace detail {
113
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;
119 #else
120 typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
121 #endif
122 #else
123 typedef std::allocator<char> type;
124 #endif
125 };
126
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 {
131 private:
132 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
133 typedef typename Base::size_type size_type;
134 #else
135 typedef typename std::allocator_traits<Base>::size_type size_type;
136 #endif
137 public:
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);
143 };
144
145 class BOOST_IOSTREAMS_DECL lzma_base {
146 public:
147 typedef char char_type;
148 protected:
149 lzma_base();
150 ~lzma_base();
151 void* stream() { return stream_; }
152 template<typename Alloc>
153 void init( const lzma_params& p,
154 bool compress,
155 lzma_allocator<Alloc>& zalloc )
156 {
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,
161 &zalloc );
162 }
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,
166 bool compress );
167 int deflate(int action);
168 int inflate(int action);
169 void reset(bool compress, bool realloc);
170 private:
171 void do_init( const lzma_params& p, bool compress,
172 lzma::alloc_func,
173 lzma::free_func,
174 void* derived );
175 void init_stream(bool compress);
176 void* stream_; // Actual type: lzma_stream*.
177 uint32_t level_;
178 uint32_t threads_;
179 };
180
181 //
182 // Template name: lzma_compressor_impl
183 // Description: Model of C-Style Filter implementing compression by
184 // delegating to the lzma function deflate.
185 //
186 template<typename Alloc = std::allocator<char> >
187 class lzma_compressor_impl : public lzma_base, public lzma_allocator<Alloc> {
188 public:
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 );
193 void close();
194 };
195
196 //
197 // Template name: lzma_compressor_impl
198 // Description: Model of C-Style Filte implementing decompression by
199 // delegating to the lzma function inflate.
200 //
201 template<typename Alloc = std::allocator<char> >
202 class lzma_decompressor_impl : public lzma_base, public lzma_allocator<Alloc> {
203 public:
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 );
209 void close();
210 };
211
212 } // End namespace detail.
213
214 //
215 // Template name: lzma_compressor
216 // Description: Model of InputFilter and OutputFilter implementing
217 // compression using lzma.
218 //
219 template<typename Alloc = std::allocator<char> >
220 struct basic_lzma_compressor
221 : symmetric_filter<detail::lzma_compressor_impl<Alloc>, Alloc>
222 {
223 private:
224 typedef detail::lzma_compressor_impl<Alloc> impl_type;
225 typedef symmetric_filter<impl_type, Alloc> base_type;
226 public:
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 );
231 };
232 BOOST_IOSTREAMS_PIPABLE(basic_lzma_compressor, 1)
233
234 typedef basic_lzma_compressor<> lzma_compressor;
235
236 //
237 // Template name: lzma_decompressor
238 // Description: Model of InputFilter and OutputFilter implementing
239 // decompression using lzma.
240 //
241 template<typename Alloc = std::allocator<char> >
242 struct basic_lzma_decompressor
243 : symmetric_filter<detail::lzma_decompressor_impl<Alloc>, Alloc>
244 {
245 private:
246 typedef detail::lzma_decompressor_impl<Alloc> impl_type;
247 typedef symmetric_filter<impl_type, Alloc> base_type;
248 public:
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 );
254 };
255 BOOST_IOSTREAMS_PIPABLE(basic_lzma_decompressor, 1)
256
257 typedef basic_lzma_decompressor<> lzma_decompressor;
258
259 //----------------------------------------------------------------------------//
260
261 //------------------Implementation of lzma_allocator--------------------------//
262
263 namespace detail {
264
265 template<typename Alloc, typename Base>
266 void* lzma_allocator<Alloc, Base>::allocate
267 (void* self, size_t items, size_t size)
268 {
269 size_type len = items * size;
270 char* ptr =
271 static_cast<allocator_type*>(self)->allocate
272 (len + sizeof(size_type)
273 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
274 , (char*)0
275 #endif
276 );
277 *reinterpret_cast<size_type*>(ptr) = len;
278 return ptr + sizeof(size_type);
279 }
280
281 template<typename Alloc, typename Base>
282 void lzma_allocator<Alloc, Base>::deallocate(void* self, void* address)
283 {
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);
287 }
288
289 //------------------Implementation of lzma_compressor_impl--------------------//
290
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)); }
294
295 template<typename Alloc>
296 lzma_compressor_impl<Alloc>::~lzma_compressor_impl()
297 { reset(true, false); }
298
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 )
303 {
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;
309 }
310
311 template<typename Alloc>
312 void lzma_compressor_impl<Alloc>::close() { reset(true, true); }
313
314 //------------------Implementation of lzma_decompressor_impl------------------//
315
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)); }
319
320 template<typename Alloc>
321 lzma_decompressor_impl<Alloc>::~lzma_decompressor_impl()
322 { reset(false, false); }
323
324 template<typename Alloc>
325 lzma_decompressor_impl<Alloc>::lzma_decompressor_impl()
326 {
327 lzma_params p;
328 init(p, false, static_cast<lzma_allocator<Alloc>&>(*this));
329 }
330
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 )
335 {
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;
341 }
342
343 template<typename Alloc>
344 void lzma_decompressor_impl<Alloc>::close() { reset(false, true); }
345
346 } // End namespace detail.
347
348 //------------------Implementation of lzma_compressor-----------------------//
349
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) { }
354
355 //------------------Implementation of lzma_decompressor-----------------------//
356
357 template<typename Alloc>
358 basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
359 (std::streamsize buffer_size)
360 : base_type(buffer_size) { }
361
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) { }
366
367 //----------------------------------------------------------------------------//
368
369 } } // End namespaces iostreams, boost.
370
371 #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
372 #ifdef BOOST_MSVC
373 # pragma warning(pop)
374 #endif
375
376 #endif // #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED