]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) |
2 | // (C) Copyright 2003-2007 Jonathan Turkanis | |
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 zlib_base::do_init. | |
10 | ||
11 | #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED | |
12 | #define BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED | |
13 | ||
14 | #if defined(_MSC_VER) | |
15 | # pragma once | |
16 | #endif | |
17 | ||
18 | #include <cassert> | |
19 | #include <memory> // allocator. | |
20 | #include <new> // bad_alloc. | |
21 | #include <boost/config.hpp> // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM. | |
22 | #include <boost/detail/workaround.hpp> | |
23 | #include <boost/iostreams/constants.hpp> // buffer size. | |
24 | #include <boost/iostreams/detail/config/auto_link.hpp> | |
25 | #include <boost/iostreams/detail/config/bzip2.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) | |
37 | #endif | |
38 | #include <boost/config/abi_prefix.hpp> | |
39 | ||
40 | // Temporary fix. | |
41 | #undef small | |
42 | ||
43 | namespace boost { namespace iostreams { | |
44 | ||
45 | namespace bzip2 { | |
46 | ||
47 | // Typedefs. | |
48 | ||
49 | typedef void* (*alloc_func)(void*, int, int); | |
50 | typedef void (*free_func)(void*, void*); | |
51 | ||
52 | // Status codes | |
53 | ||
54 | BOOST_IOSTREAMS_DECL extern const int ok; | |
55 | BOOST_IOSTREAMS_DECL extern const int run_ok; | |
56 | BOOST_IOSTREAMS_DECL extern const int flush_ok; | |
57 | BOOST_IOSTREAMS_DECL extern const int finish_ok; | |
58 | BOOST_IOSTREAMS_DECL extern const int stream_end; | |
59 | BOOST_IOSTREAMS_DECL extern const int sequence_error; | |
60 | BOOST_IOSTREAMS_DECL extern const int param_error; | |
61 | BOOST_IOSTREAMS_DECL extern const int mem_error; | |
62 | BOOST_IOSTREAMS_DECL extern const int data_error; | |
63 | BOOST_IOSTREAMS_DECL extern const int data_error_magic; | |
64 | BOOST_IOSTREAMS_DECL extern const int io_error; | |
65 | BOOST_IOSTREAMS_DECL extern const int unexpected_eof; | |
66 | BOOST_IOSTREAMS_DECL extern const int outbuff_full; | |
67 | BOOST_IOSTREAMS_DECL extern const int config_error; | |
68 | ||
69 | // Action codes | |
70 | ||
71 | BOOST_IOSTREAMS_DECL extern const int finish; | |
72 | BOOST_IOSTREAMS_DECL extern const int run; | |
73 | ||
74 | // Default values | |
75 | ||
76 | const int default_block_size = 9; | |
77 | const int default_work_factor = 30; | |
78 | const bool default_small = false; | |
79 | ||
80 | } // End namespace bzip2. | |
81 | ||
82 | // | |
83 | // Class name: bzip2_params. | |
84 | // Description: Encapsulates the parameters passed to deflateInit2 | |
85 | // to customize compression. | |
86 | // | |
87 | struct bzip2_params { | |
88 | ||
89 | // Non-explicit constructor for compression. | |
b32b8144 FG |
90 | bzip2_params( int block_size_ = bzip2::default_block_size, |
91 | int work_factor_ = bzip2::default_work_factor ) | |
92 | : block_size(block_size_), work_factor(work_factor_) | |
7c673cae FG |
93 | { } |
94 | ||
95 | // Constructor for decompression. | |
96 | bzip2_params(bool small) | |
97 | : small(small), work_factor(0) | |
98 | { } | |
99 | ||
100 | union { | |
101 | int block_size; // For compression. | |
102 | bool small; // For decompression. | |
103 | }; | |
104 | int work_factor; | |
105 | }; | |
106 | ||
107 | // | |
108 | // Class name: bzip2_error. | |
109 | // Description: Subclass of std::ios_base::failure thrown to indicate | |
110 | // bzip2 errors other than out-of-memory conditions. | |
111 | // | |
112 | class BOOST_IOSTREAMS_DECL bzip2_error : public BOOST_IOSTREAMS_FAILURE { | |
113 | public: | |
114 | explicit bzip2_error(int error); | |
115 | int error() const { return error_; } | |
116 | static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error); | |
117 | private: | |
118 | int error_; | |
119 | }; | |
120 | ||
121 | namespace detail { | |
122 | ||
123 | template<typename Alloc> | |
124 | struct bzip2_allocator_traits { | |
125 | #ifndef BOOST_NO_STD_ALLOCATOR | |
11fdf7f2 | 126 | #if defined(BOOST_NO_CXX11_ALLOCATOR) |
7c673cae | 127 | typedef typename Alloc::template rebind<char>::other type; |
11fdf7f2 TL |
128 | #else |
129 | typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type; | |
130 | #endif | |
7c673cae FG |
131 | #else |
132 | typedef std::allocator<char> type; | |
133 | #endif | |
134 | }; | |
135 | ||
136 | template< typename Alloc, | |
137 | typename Base = // VC6 workaround (C2516) | |
138 | BOOST_DEDUCED_TYPENAME bzip2_allocator_traits<Alloc>::type > | |
139 | struct bzip2_allocator : private Base { | |
140 | private: | |
11fdf7f2 | 141 | #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR) |
7c673cae | 142 | typedef typename Base::size_type size_type; |
11fdf7f2 TL |
143 | #else |
144 | typedef typename std::allocator_traits<Base>::size_type size_type; | |
145 | #endif | |
7c673cae FG |
146 | public: |
147 | BOOST_STATIC_CONSTANT(bool, custom = | |
148 | (!is_same<std::allocator<char>, Base>::value)); | |
149 | typedef typename bzip2_allocator_traits<Alloc>::type allocator_type; | |
150 | static void* allocate(void* self, int items, int size); | |
151 | static void deallocate(void* self, void* address); | |
152 | }; | |
153 | ||
154 | class BOOST_IOSTREAMS_DECL bzip2_base { | |
155 | public: | |
156 | typedef char char_type; | |
157 | protected: | |
158 | bzip2_base(const bzip2_params& params); | |
159 | ~bzip2_base(); | |
160 | bzip2_params& params() { return params_; } | |
161 | bool& ready() { return ready_; } | |
162 | template<typename Alloc> | |
163 | void init( bool compress, | |
164 | bzip2_allocator<Alloc>& alloc ) | |
165 | { | |
166 | bool custom = bzip2_allocator<Alloc>::custom; | |
167 | do_init( compress, | |
168 | custom ? bzip2_allocator<Alloc>::allocate : 0, | |
169 | custom ? bzip2_allocator<Alloc>::deallocate : 0, | |
170 | custom ? &alloc : 0 ); | |
171 | } | |
172 | void before( const char*& src_begin, const char* src_end, | |
173 | char*& dest_begin, char* dest_end ); | |
174 | void after(const char*& src_begin, char*& dest_begin); | |
175 | int check_end(const char* src_begin, const char* dest_begin); | |
176 | int compress(int action); | |
177 | int decompress(); | |
178 | void end(bool compress); | |
179 | private: | |
180 | void do_init( bool compress, | |
181 | bzip2::alloc_func, | |
182 | bzip2::free_func, | |
183 | void* derived ); | |
184 | bzip2_params params_; | |
185 | void* stream_; // Actual type: bz_stream*. | |
186 | bool ready_; | |
187 | }; | |
188 | ||
189 | // | |
190 | // Template name: bzip2_compressor_impl | |
191 | // Description: Model of SymmetricFilter implementing compression by | |
192 | // delegating to the libbzip2 function BZ_bzCompress. | |
193 | // | |
194 | template<typename Alloc = std::allocator<char> > | |
195 | class bzip2_compressor_impl | |
196 | : public bzip2_base, | |
197 | #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) | |
198 | public | |
199 | #endif | |
200 | bzip2_allocator<Alloc> | |
201 | { | |
202 | public: | |
203 | bzip2_compressor_impl(const bzip2_params&); | |
204 | bool filter( const char*& src_begin, const char* src_end, | |
205 | char*& dest_begin, char* dest_end, bool flush ); | |
206 | void close(); | |
207 | private: | |
208 | void init(); | |
209 | bool eof_; // Guard to make sure filter() isn't called after it returns false. | |
210 | }; | |
211 | ||
212 | // | |
213 | // Template name: bzip2_compressor | |
214 | // Description: Model of SymmetricFilter implementing decompression by | |
215 | // delegating to the libbzip2 function BZ_bzDecompress. | |
216 | // | |
217 | template<typename Alloc = std::allocator<char> > | |
218 | class bzip2_decompressor_impl | |
219 | : public bzip2_base, | |
220 | #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) | |
221 | public | |
222 | #endif | |
223 | bzip2_allocator<Alloc> | |
224 | { | |
225 | public: | |
226 | bzip2_decompressor_impl(bool small = bzip2::default_small); | |
227 | bool filter( const char*& begin_in, const char* end_in, | |
228 | char*& begin_out, char* end_out, bool flush ); | |
229 | void close(); | |
230 | private: | |
231 | void init(); | |
232 | bool eof_; // Guard to make sure filter() isn't called after it returns false. | |
233 | }; | |
234 | ||
235 | } // End namespace detail. | |
236 | ||
237 | // | |
238 | // Template name: bzip2_compressor | |
239 | // Description: Model of InputFilter and OutputFilter implementing | |
240 | // compression using libbzip2. | |
241 | // | |
242 | template<typename Alloc = std::allocator<char> > | |
243 | struct basic_bzip2_compressor | |
244 | : symmetric_filter<detail::bzip2_compressor_impl<Alloc>, Alloc> | |
245 | { | |
246 | private: | |
247 | typedef detail::bzip2_compressor_impl<Alloc> impl_type; | |
248 | typedef symmetric_filter<impl_type, Alloc> base_type; | |
249 | public: | |
250 | typedef typename base_type::char_type char_type; | |
251 | typedef typename base_type::category category; | |
252 | basic_bzip2_compressor( const bzip2_params& = bzip2::default_block_size, | |
b32b8144 | 253 | std::streamsize buffer_size = default_device_buffer_size ); |
7c673cae FG |
254 | }; |
255 | BOOST_IOSTREAMS_PIPABLE(basic_bzip2_compressor, 1) | |
256 | ||
257 | typedef basic_bzip2_compressor<> bzip2_compressor; | |
258 | ||
259 | // | |
260 | // Template name: bzip2_decompressor | |
261 | // Description: Model of InputFilter and OutputFilter implementing | |
262 | // decompression using libbzip2. | |
263 | // | |
264 | template<typename Alloc = std::allocator<char> > | |
265 | struct basic_bzip2_decompressor | |
266 | : symmetric_filter<detail::bzip2_decompressor_impl<Alloc>, Alloc> | |
267 | { | |
268 | private: | |
269 | typedef detail::bzip2_decompressor_impl<Alloc> impl_type; | |
270 | typedef symmetric_filter<impl_type, Alloc> base_type; | |
271 | public: | |
272 | typedef typename base_type::char_type char_type; | |
273 | typedef typename base_type::category category; | |
274 | basic_bzip2_decompressor( bool small = bzip2::default_small, | |
b32b8144 | 275 | std::streamsize buffer_size = default_device_buffer_size ); |
7c673cae FG |
276 | }; |
277 | BOOST_IOSTREAMS_PIPABLE(basic_bzip2_decompressor, 1) | |
278 | ||
279 | typedef basic_bzip2_decompressor<> bzip2_decompressor; | |
280 | ||
281 | //----------------------------------------------------------------------------// | |
282 | ||
283 | //------------------Implementation of bzip2_allocator-------------------------// | |
284 | ||
285 | namespace detail { | |
286 | ||
287 | template<typename Alloc, typename Base> | |
288 | void* bzip2_allocator<Alloc, Base>::allocate(void* self, int items, int size) | |
289 | { | |
290 | size_type len = items * size; | |
291 | char* ptr = | |
292 | static_cast<allocator_type*>(self)->allocate | |
293 | (len + sizeof(size_type) | |
294 | #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) | |
295 | , (char*)0 | |
296 | #endif | |
297 | ); | |
298 | *reinterpret_cast<size_type*>(ptr) = len; | |
299 | return ptr + sizeof(size_type); | |
300 | } | |
301 | ||
302 | template<typename Alloc, typename Base> | |
303 | void bzip2_allocator<Alloc, Base>::deallocate(void* self, void* address) | |
304 | { | |
305 | char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type); | |
306 | size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type); | |
307 | static_cast<allocator_type*>(self)->deallocate(ptr, len); | |
308 | } | |
309 | ||
310 | //------------------Implementation of bzip2_compressor_impl-------------------// | |
311 | ||
312 | template<typename Alloc> | |
313 | bzip2_compressor_impl<Alloc>::bzip2_compressor_impl(const bzip2_params& p) | |
314 | : bzip2_base(p), eof_(false) { } | |
315 | ||
316 | template<typename Alloc> | |
317 | bool bzip2_compressor_impl<Alloc>::filter | |
318 | ( const char*& src_begin, const char* src_end, | |
319 | char*& dest_begin, char* dest_end, bool flush ) | |
320 | { | |
321 | if (!ready()) init(); | |
322 | if (eof_) return false; | |
323 | before(src_begin, src_end, dest_begin, dest_end); | |
324 | int result = compress(flush ? bzip2::finish : bzip2::run); | |
325 | after(src_begin, dest_begin); | |
326 | bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result); | |
327 | return !(eof_ = result == bzip2::stream_end); | |
328 | } | |
329 | ||
330 | template<typename Alloc> | |
331 | void bzip2_compressor_impl<Alloc>::close() | |
332 | { | |
333 | try { | |
334 | end(true); | |
335 | } catch (...) { | |
336 | eof_ = false; | |
337 | throw; | |
338 | } | |
339 | eof_ = false; | |
340 | } | |
341 | ||
342 | template<typename Alloc> | |
343 | inline void bzip2_compressor_impl<Alloc>::init() | |
344 | { bzip2_base::init(true, static_cast<bzip2_allocator<Alloc>&>(*this)); } | |
345 | ||
346 | //------------------Implementation of bzip2_decompressor_impl-----------------// | |
347 | ||
348 | template<typename Alloc> | |
349 | bzip2_decompressor_impl<Alloc>::bzip2_decompressor_impl(bool small) | |
350 | : bzip2_base(bzip2_params(small)), eof_(false) { } | |
351 | ||
352 | template<typename Alloc> | |
353 | bool bzip2_decompressor_impl<Alloc>::filter | |
354 | ( const char*& src_begin, const char* src_end, | |
355 | char*& dest_begin, char* dest_end, bool flush ) | |
356 | { | |
357 | if (eof_) { | |
358 | // reset the stream if there are more characters | |
359 | if(src_begin == src_end) | |
360 | return false; | |
361 | else | |
362 | close(); | |
363 | } | |
364 | if (!ready()) | |
365 | init(); | |
366 | before(src_begin, src_end, dest_begin, dest_end); | |
367 | int result = decompress(); | |
368 | if(result == bzip2::ok && flush) | |
369 | result = check_end(src_begin, dest_begin); | |
370 | after(src_begin, dest_begin); | |
371 | bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result); | |
372 | eof_ = result == bzip2::stream_end; | |
373 | return true; | |
374 | } | |
375 | ||
376 | template<typename Alloc> | |
377 | void bzip2_decompressor_impl<Alloc>::close() | |
378 | { | |
379 | try { | |
380 | end(false); | |
381 | } catch (...) { | |
382 | eof_ = false; | |
383 | throw; | |
384 | } | |
385 | eof_ = false; | |
386 | } | |
387 | ||
388 | template<typename Alloc> | |
389 | inline void bzip2_decompressor_impl<Alloc>::init() | |
390 | { bzip2_base::init(false, static_cast<bzip2_allocator<Alloc>&>(*this)); } | |
391 | } // End namespace detail. | |
392 | ||
393 | //------------------Implementation of bzip2_decompressor----------------------// | |
394 | ||
395 | template<typename Alloc> | |
396 | basic_bzip2_compressor<Alloc>::basic_bzip2_compressor | |
b32b8144 | 397 | (const bzip2_params& p, std::streamsize buffer_size) |
7c673cae FG |
398 | : base_type(buffer_size, p) |
399 | { } | |
400 | ||
401 | //------------------Implementation of bzip2_decompressor----------------------// | |
402 | ||
403 | template<typename Alloc> | |
404 | basic_bzip2_decompressor<Alloc>::basic_bzip2_decompressor | |
b32b8144 | 405 | (bool small, std::streamsize buffer_size) |
7c673cae FG |
406 | : base_type(buffer_size, small) |
407 | { } | |
408 | ||
409 | //----------------------------------------------------------------------------// | |
410 | ||
411 | } } // End namespaces iostreams, boost. | |
412 | ||
413 | #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas. | |
414 | #ifdef BOOST_MSVC | |
415 | # pragma warning(pop) | |
416 | #endif | |
417 | ||
418 | #endif // #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED |