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.)
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 zlib_base::do_init.
11 #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED
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>
35 # pragma warning(push)
36 # pragma warning(disable:4251 4231 4660)
38 #include <boost/config/abi_prefix.hpp>
43 namespace boost { namespace iostreams {
49 typedef void* (*alloc_func)(void*, int, int);
50 typedef void (*free_func)(void*, void*);
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;
71 BOOST_IOSTREAMS_DECL extern const int finish;
72 BOOST_IOSTREAMS_DECL extern const int run;
76 const int default_block_size = 9;
77 const int default_work_factor = 30;
78 const bool default_small = false;
80 } // End namespace bzip2.
83 // Class name: bzip2_params.
84 // Description: Encapsulates the parameters passed to deflateInit2
85 // to customize compression.
89 // Non-explicit constructor for compression.
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_)
95 // Constructor for decompression.
96 bzip2_params(bool small)
97 : small(small), work_factor(0)
101 int block_size; // For compression.
102 bool small; // For decompression.
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.
112 class BOOST_IOSTREAMS_DECL bzip2_error : public BOOST_IOSTREAMS_FAILURE {
114 explicit bzip2_error(int error);
115 int error() const { return error_; }
116 static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
123 template<typename Alloc>
124 struct bzip2_allocator_traits {
125 #ifndef BOOST_NO_STD_ALLOCATOR
126 #if defined(BOOST_NO_CXX11_ALLOCATOR)
127 typedef typename Alloc::template rebind<char>::other type;
129 typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
132 typedef std::allocator<char> type;
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 {
141 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
142 typedef typename Base::size_type size_type;
144 typedef typename std::allocator_traits<Base>::size_type size_type;
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);
154 class BOOST_IOSTREAMS_DECL bzip2_base {
156 typedef char char_type;
158 bzip2_base(const bzip2_params& params);
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 )
166 bool custom = bzip2_allocator<Alloc>::custom;
168 custom ? bzip2_allocator<Alloc>::allocate : 0,
169 custom ? bzip2_allocator<Alloc>::deallocate : 0,
170 custom ? &alloc : 0 );
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);
178 int end(bool compress, std::nothrow_t);
179 void end(bool compress);
181 void do_init( bool compress,
185 bzip2_params params_;
186 void* stream_; // Actual type: bz_stream*.
191 // Template name: bzip2_compressor_impl
192 // Description: Model of SymmetricFilter implementing compression by
193 // delegating to the libbzip2 function BZ_bzCompress.
195 template<typename Alloc = std::allocator<char> >
196 class bzip2_compressor_impl
198 #if BOOST_WORKAROUND(__BORLANDC__, < 0x600)
201 bzip2_allocator<Alloc>
204 bzip2_compressor_impl(const bzip2_params&);
205 ~bzip2_compressor_impl();
206 bool filter( const char*& src_begin, const char* src_end,
207 char*& dest_begin, char* dest_end, bool flush );
211 bool eof_; // Guard to make sure filter() isn't called after it returns false.
215 // Template name: bzip2_compressor
216 // Description: Model of SymmetricFilter implementing decompression by
217 // delegating to the libbzip2 function BZ_bzDecompress.
219 template<typename Alloc = std::allocator<char> >
220 class bzip2_decompressor_impl
222 #if BOOST_WORKAROUND(__BORLANDC__, < 0x600)
225 bzip2_allocator<Alloc>
228 bzip2_decompressor_impl(bool small = bzip2::default_small);
229 ~bzip2_decompressor_impl();
230 bool filter( const char*& begin_in, const char* end_in,
231 char*& begin_out, char* end_out, bool flush );
235 bool eof_; // Guard to make sure filter() isn't called after it returns false.
238 } // End namespace detail.
241 // Template name: bzip2_compressor
242 // Description: Model of InputFilter and OutputFilter implementing
243 // compression using libbzip2.
245 template<typename Alloc = std::allocator<char> >
246 struct basic_bzip2_compressor
247 : symmetric_filter<detail::bzip2_compressor_impl<Alloc>, Alloc>
250 typedef detail::bzip2_compressor_impl<Alloc> impl_type;
251 typedef symmetric_filter<impl_type, Alloc> base_type;
253 typedef typename base_type::char_type char_type;
254 typedef typename base_type::category category;
255 basic_bzip2_compressor( const bzip2_params& = bzip2::default_block_size,
256 std::streamsize buffer_size = default_device_buffer_size );
258 BOOST_IOSTREAMS_PIPABLE(basic_bzip2_compressor, 1)
260 typedef basic_bzip2_compressor<> bzip2_compressor;
263 // Template name: bzip2_decompressor
264 // Description: Model of InputFilter and OutputFilter implementing
265 // decompression using libbzip2.
267 template<typename Alloc = std::allocator<char> >
268 struct basic_bzip2_decompressor
269 : symmetric_filter<detail::bzip2_decompressor_impl<Alloc>, Alloc>
272 typedef detail::bzip2_decompressor_impl<Alloc> impl_type;
273 typedef symmetric_filter<impl_type, Alloc> base_type;
275 typedef typename base_type::char_type char_type;
276 typedef typename base_type::category category;
277 basic_bzip2_decompressor( bool small = bzip2::default_small,
278 std::streamsize buffer_size = default_device_buffer_size );
280 BOOST_IOSTREAMS_PIPABLE(basic_bzip2_decompressor, 1)
282 typedef basic_bzip2_decompressor<> bzip2_decompressor;
284 //----------------------------------------------------------------------------//
286 //------------------Implementation of bzip2_allocator-------------------------//
290 template<typename Alloc, typename Base>
291 void* bzip2_allocator<Alloc, Base>::allocate(void* self, int items, int size)
293 size_type len = items * size;
295 static_cast<allocator_type*>(self)->allocate
296 (len + sizeof(size_type)
297 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
301 *reinterpret_cast<size_type*>(ptr) = len;
302 return ptr + sizeof(size_type);
305 template<typename Alloc, typename Base>
306 void bzip2_allocator<Alloc, Base>::deallocate(void* self, void* address)
308 char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
309 size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
310 static_cast<allocator_type*>(self)->deallocate(ptr, len);
313 //------------------Implementation of bzip2_compressor_impl-------------------//
315 template<typename Alloc>
316 bzip2_compressor_impl<Alloc>::bzip2_compressor_impl(const bzip2_params& p)
317 : bzip2_base(p), eof_(false) { }
319 template<typename Alloc>
320 bzip2_compressor_impl<Alloc>::~bzip2_compressor_impl()
321 { (void) bzip2_base::end(true, std::nothrow); }
323 template<typename Alloc>
324 bool bzip2_compressor_impl<Alloc>::filter
325 ( const char*& src_begin, const char* src_end,
326 char*& dest_begin, char* dest_end, bool flush )
328 if (!ready()) init();
329 if (eof_) return false;
330 before(src_begin, src_end, dest_begin, dest_end);
331 int result = compress(flush ? bzip2::finish : bzip2::run);
332 after(src_begin, dest_begin);
333 bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
334 return !(eof_ = result == bzip2::stream_end);
337 template<typename Alloc>
338 void bzip2_compressor_impl<Alloc>::close()
349 template<typename Alloc>
350 inline void bzip2_compressor_impl<Alloc>::init()
351 { bzip2_base::init(true, static_cast<bzip2_allocator<Alloc>&>(*this)); }
353 //------------------Implementation of bzip2_decompressor_impl-----------------//
355 template<typename Alloc>
356 bzip2_decompressor_impl<Alloc>::bzip2_decompressor_impl(bool small)
357 : bzip2_base(bzip2_params(small)), eof_(false) { }
359 template<typename Alloc>
360 bzip2_decompressor_impl<Alloc>::~bzip2_decompressor_impl()
361 { (void) bzip2_base::end(false, std::nothrow); }
363 template<typename Alloc>
364 bool bzip2_decompressor_impl<Alloc>::filter
365 ( const char*& src_begin, const char* src_end,
366 char*& dest_begin, char* dest_end, bool flush )
370 // reset the stream if there are more characters
371 if(src_begin == src_end)
378 before(src_begin, src_end, dest_begin, dest_end);
379 int result = decompress();
380 if(result == bzip2::ok && flush)
381 result = check_end(src_begin, dest_begin);
382 after(src_begin, dest_begin);
383 bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
384 eof_ = result == bzip2::stream_end;
385 } while (eof_ && src_begin != src_end && dest_begin != dest_end);
389 template<typename Alloc>
390 void bzip2_decompressor_impl<Alloc>::close()
401 template<typename Alloc>
402 inline void bzip2_decompressor_impl<Alloc>::init()
403 { bzip2_base::init(false, static_cast<bzip2_allocator<Alloc>&>(*this)); }
404 } // End namespace detail.
406 //------------------Implementation of bzip2_decompressor----------------------//
408 template<typename Alloc>
409 basic_bzip2_compressor<Alloc>::basic_bzip2_compressor
410 (const bzip2_params& p, std::streamsize buffer_size)
411 : base_type(buffer_size, p)
414 //------------------Implementation of bzip2_decompressor----------------------//
416 template<typename Alloc>
417 basic_bzip2_decompressor<Alloc>::basic_bzip2_decompressor
418 (bool small, std::streamsize buffer_size)
419 : base_type(buffer_size, small)
422 //----------------------------------------------------------------------------//
424 } } // End namespaces iostreams, boost.
426 #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
428 # pragma warning(pop)
431 #endif // #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED