]>
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. | |
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) | |
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 | |
126 | typedef typename Alloc::template rebind<char>::other type; | |
127 | #else | |
128 | typedef std::allocator<char> type; | |
129 | #endif | |
130 | }; | |
131 | ||
132 | template< typename Alloc, | |
133 | typename Base = // VC6 workaround (C2516) | |
134 | BOOST_DEDUCED_TYPENAME bzip2_allocator_traits<Alloc>::type > | |
135 | struct bzip2_allocator : private Base { | |
136 | private: | |
137 | typedef typename Base::size_type size_type; | |
138 | public: | |
139 | BOOST_STATIC_CONSTANT(bool, custom = | |
140 | (!is_same<std::allocator<char>, Base>::value)); | |
141 | typedef typename bzip2_allocator_traits<Alloc>::type allocator_type; | |
142 | static void* allocate(void* self, int items, int size); | |
143 | static void deallocate(void* self, void* address); | |
144 | }; | |
145 | ||
146 | class BOOST_IOSTREAMS_DECL bzip2_base { | |
147 | public: | |
148 | typedef char char_type; | |
149 | protected: | |
150 | bzip2_base(const bzip2_params& params); | |
151 | ~bzip2_base(); | |
152 | bzip2_params& params() { return params_; } | |
153 | bool& ready() { return ready_; } | |
154 | template<typename Alloc> | |
155 | void init( bool compress, | |
156 | bzip2_allocator<Alloc>& alloc ) | |
157 | { | |
158 | bool custom = bzip2_allocator<Alloc>::custom; | |
159 | do_init( compress, | |
160 | custom ? bzip2_allocator<Alloc>::allocate : 0, | |
161 | custom ? bzip2_allocator<Alloc>::deallocate : 0, | |
162 | custom ? &alloc : 0 ); | |
163 | } | |
164 | void before( const char*& src_begin, const char* src_end, | |
165 | char*& dest_begin, char* dest_end ); | |
166 | void after(const char*& src_begin, char*& dest_begin); | |
167 | int check_end(const char* src_begin, const char* dest_begin); | |
168 | int compress(int action); | |
169 | int decompress(); | |
170 | void end(bool compress); | |
171 | private: | |
172 | void do_init( bool compress, | |
173 | bzip2::alloc_func, | |
174 | bzip2::free_func, | |
175 | void* derived ); | |
176 | bzip2_params params_; | |
177 | void* stream_; // Actual type: bz_stream*. | |
178 | bool ready_; | |
179 | }; | |
180 | ||
181 | // | |
182 | // Template name: bzip2_compressor_impl | |
183 | // Description: Model of SymmetricFilter implementing compression by | |
184 | // delegating to the libbzip2 function BZ_bzCompress. | |
185 | // | |
186 | template<typename Alloc = std::allocator<char> > | |
187 | class bzip2_compressor_impl | |
188 | : public bzip2_base, | |
189 | #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) | |
190 | public | |
191 | #endif | |
192 | bzip2_allocator<Alloc> | |
193 | { | |
194 | public: | |
195 | bzip2_compressor_impl(const bzip2_params&); | |
196 | bool filter( const char*& src_begin, const char* src_end, | |
197 | char*& dest_begin, char* dest_end, bool flush ); | |
198 | void close(); | |
199 | private: | |
200 | void init(); | |
201 | bool eof_; // Guard to make sure filter() isn't called after it returns false. | |
202 | }; | |
203 | ||
204 | // | |
205 | // Template name: bzip2_compressor | |
206 | // Description: Model of SymmetricFilter implementing decompression by | |
207 | // delegating to the libbzip2 function BZ_bzDecompress. | |
208 | // | |
209 | template<typename Alloc = std::allocator<char> > | |
210 | class bzip2_decompressor_impl | |
211 | : public bzip2_base, | |
212 | #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) | |
213 | public | |
214 | #endif | |
215 | bzip2_allocator<Alloc> | |
216 | { | |
217 | public: | |
218 | bzip2_decompressor_impl(bool small = bzip2::default_small); | |
219 | bool filter( const char*& begin_in, const char* end_in, | |
220 | char*& begin_out, char* end_out, bool flush ); | |
221 | void close(); | |
222 | private: | |
223 | void init(); | |
224 | bool eof_; // Guard to make sure filter() isn't called after it returns false. | |
225 | }; | |
226 | ||
227 | } // End namespace detail. | |
228 | ||
229 | // | |
230 | // Template name: bzip2_compressor | |
231 | // Description: Model of InputFilter and OutputFilter implementing | |
232 | // compression using libbzip2. | |
233 | // | |
234 | template<typename Alloc = std::allocator<char> > | |
235 | struct basic_bzip2_compressor | |
236 | : symmetric_filter<detail::bzip2_compressor_impl<Alloc>, Alloc> | |
237 | { | |
238 | private: | |
239 | typedef detail::bzip2_compressor_impl<Alloc> impl_type; | |
240 | typedef symmetric_filter<impl_type, Alloc> base_type; | |
241 | public: | |
242 | typedef typename base_type::char_type char_type; | |
243 | typedef typename base_type::category category; | |
244 | basic_bzip2_compressor( const bzip2_params& = bzip2::default_block_size, | |
245 | int buffer_size = default_device_buffer_size ); | |
246 | }; | |
247 | BOOST_IOSTREAMS_PIPABLE(basic_bzip2_compressor, 1) | |
248 | ||
249 | typedef basic_bzip2_compressor<> bzip2_compressor; | |
250 | ||
251 | // | |
252 | // Template name: bzip2_decompressor | |
253 | // Description: Model of InputFilter and OutputFilter implementing | |
254 | // decompression using libbzip2. | |
255 | // | |
256 | template<typename Alloc = std::allocator<char> > | |
257 | struct basic_bzip2_decompressor | |
258 | : symmetric_filter<detail::bzip2_decompressor_impl<Alloc>, Alloc> | |
259 | { | |
260 | private: | |
261 | typedef detail::bzip2_decompressor_impl<Alloc> impl_type; | |
262 | typedef symmetric_filter<impl_type, Alloc> base_type; | |
263 | public: | |
264 | typedef typename base_type::char_type char_type; | |
265 | typedef typename base_type::category category; | |
266 | basic_bzip2_decompressor( bool small = bzip2::default_small, | |
267 | int buffer_size = default_device_buffer_size ); | |
268 | }; | |
269 | BOOST_IOSTREAMS_PIPABLE(basic_bzip2_decompressor, 1) | |
270 | ||
271 | typedef basic_bzip2_decompressor<> bzip2_decompressor; | |
272 | ||
273 | //----------------------------------------------------------------------------// | |
274 | ||
275 | //------------------Implementation of bzip2_allocator-------------------------// | |
276 | ||
277 | namespace detail { | |
278 | ||
279 | template<typename Alloc, typename Base> | |
280 | void* bzip2_allocator<Alloc, Base>::allocate(void* self, int items, int size) | |
281 | { | |
282 | size_type len = items * size; | |
283 | char* ptr = | |
284 | static_cast<allocator_type*>(self)->allocate | |
285 | (len + sizeof(size_type) | |
286 | #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) | |
287 | , (char*)0 | |
288 | #endif | |
289 | ); | |
290 | *reinterpret_cast<size_type*>(ptr) = len; | |
291 | return ptr + sizeof(size_type); | |
292 | } | |
293 | ||
294 | template<typename Alloc, typename Base> | |
295 | void bzip2_allocator<Alloc, Base>::deallocate(void* self, void* address) | |
296 | { | |
297 | char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type); | |
298 | size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type); | |
299 | static_cast<allocator_type*>(self)->deallocate(ptr, len); | |
300 | } | |
301 | ||
302 | //------------------Implementation of bzip2_compressor_impl-------------------// | |
303 | ||
304 | template<typename Alloc> | |
305 | bzip2_compressor_impl<Alloc>::bzip2_compressor_impl(const bzip2_params& p) | |
306 | : bzip2_base(p), eof_(false) { } | |
307 | ||
308 | template<typename Alloc> | |
309 | bool bzip2_compressor_impl<Alloc>::filter | |
310 | ( const char*& src_begin, const char* src_end, | |
311 | char*& dest_begin, char* dest_end, bool flush ) | |
312 | { | |
313 | if (!ready()) init(); | |
314 | if (eof_) return false; | |
315 | before(src_begin, src_end, dest_begin, dest_end); | |
316 | int result = compress(flush ? bzip2::finish : bzip2::run); | |
317 | after(src_begin, dest_begin); | |
318 | bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result); | |
319 | return !(eof_ = result == bzip2::stream_end); | |
320 | } | |
321 | ||
322 | template<typename Alloc> | |
323 | void bzip2_compressor_impl<Alloc>::close() | |
324 | { | |
325 | try { | |
326 | end(true); | |
327 | } catch (...) { | |
328 | eof_ = false; | |
329 | throw; | |
330 | } | |
331 | eof_ = false; | |
332 | } | |
333 | ||
334 | template<typename Alloc> | |
335 | inline void bzip2_compressor_impl<Alloc>::init() | |
336 | { bzip2_base::init(true, static_cast<bzip2_allocator<Alloc>&>(*this)); } | |
337 | ||
338 | //------------------Implementation of bzip2_decompressor_impl-----------------// | |
339 | ||
340 | template<typename Alloc> | |
341 | bzip2_decompressor_impl<Alloc>::bzip2_decompressor_impl(bool small) | |
342 | : bzip2_base(bzip2_params(small)), eof_(false) { } | |
343 | ||
344 | template<typename Alloc> | |
345 | bool bzip2_decompressor_impl<Alloc>::filter | |
346 | ( const char*& src_begin, const char* src_end, | |
347 | char*& dest_begin, char* dest_end, bool flush ) | |
348 | { | |
349 | if (eof_) { | |
350 | // reset the stream if there are more characters | |
351 | if(src_begin == src_end) | |
352 | return false; | |
353 | else | |
354 | close(); | |
355 | } | |
356 | if (!ready()) | |
357 | init(); | |
358 | before(src_begin, src_end, dest_begin, dest_end); | |
359 | int result = decompress(); | |
360 | if(result == bzip2::ok && flush) | |
361 | result = check_end(src_begin, dest_begin); | |
362 | after(src_begin, dest_begin); | |
363 | bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result); | |
364 | eof_ = result == bzip2::stream_end; | |
365 | return true; | |
366 | } | |
367 | ||
368 | template<typename Alloc> | |
369 | void bzip2_decompressor_impl<Alloc>::close() | |
370 | { | |
371 | try { | |
372 | end(false); | |
373 | } catch (...) { | |
374 | eof_ = false; | |
375 | throw; | |
376 | } | |
377 | eof_ = false; | |
378 | } | |
379 | ||
380 | template<typename Alloc> | |
381 | inline void bzip2_decompressor_impl<Alloc>::init() | |
382 | { bzip2_base::init(false, static_cast<bzip2_allocator<Alloc>&>(*this)); } | |
383 | } // End namespace detail. | |
384 | ||
385 | //------------------Implementation of bzip2_decompressor----------------------// | |
386 | ||
387 | template<typename Alloc> | |
388 | basic_bzip2_compressor<Alloc>::basic_bzip2_compressor | |
389 | (const bzip2_params& p, int buffer_size) | |
390 | : base_type(buffer_size, p) | |
391 | { } | |
392 | ||
393 | //------------------Implementation of bzip2_decompressor----------------------// | |
394 | ||
395 | template<typename Alloc> | |
396 | basic_bzip2_decompressor<Alloc>::basic_bzip2_decompressor | |
397 | (bool small, int buffer_size) | |
398 | : base_type(buffer_size, small) | |
399 | { } | |
400 | ||
401 | //----------------------------------------------------------------------------// | |
402 | ||
403 | } } // End namespaces iostreams, boost. | |
404 | ||
405 | #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas. | |
406 | #ifdef BOOST_MSVC | |
407 | # pragma warning(pop) | |
408 | #endif | |
409 | ||
410 | #endif // #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED |