]>
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_ZLIB_HPP_INCLUDED | |
12 | #define BOOST_IOSTREAMS_ZLIB_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/cstdint.hpp> // uint*_t | |
24 | #include <boost/detail/workaround.hpp> | |
25 | #include <boost/iostreams/constants.hpp> // buffer size. | |
26 | #include <boost/iostreams/detail/config/auto_link.hpp> | |
27 | #include <boost/iostreams/detail/config/dyn_link.hpp> | |
28 | #include <boost/iostreams/detail/config/wide_streams.hpp> | |
29 | #include <boost/iostreams/detail/config/zlib.hpp> | |
30 | #include <boost/iostreams/detail/ios.hpp> // failure, streamsize. | |
31 | #include <boost/iostreams/filter/symmetric.hpp> | |
32 | #include <boost/iostreams/pipeline.hpp> | |
33 | #include <boost/type_traits/is_same.hpp> | |
34 | ||
35 | // Must come last. | |
36 | #ifdef BOOST_MSVC | |
37 | # pragma warning(push) | |
38 | # pragma warning(disable:4251 4275 4231 4660) // Dependencies not exported. | |
39 | #endif | |
40 | #include <boost/config/abi_prefix.hpp> | |
41 | ||
42 | namespace boost { namespace iostreams { | |
43 | ||
44 | namespace zlib { | |
45 | // Typedefs | |
46 | ||
47 | typedef uint32_t uint; | |
48 | typedef uint8_t byte; | |
49 | typedef uint32_t ulong; | |
50 | ||
51 | // Prefix 'x' prevents symbols from being redefined when Z_PREFIX is defined | |
52 | typedef void* (*xalloc_func)(void*, zlib::uint, zlib::uint); | |
53 | typedef void (*xfree_func)(void*, void*); | |
54 | ||
55 | // Compression levels | |
56 | ||
57 | BOOST_IOSTREAMS_DECL extern const int no_compression; | |
58 | BOOST_IOSTREAMS_DECL extern const int best_speed; | |
59 | BOOST_IOSTREAMS_DECL extern const int best_compression; | |
60 | BOOST_IOSTREAMS_DECL extern const int default_compression; | |
61 | ||
62 | // Compression methods | |
63 | ||
64 | BOOST_IOSTREAMS_DECL extern const int deflated; | |
65 | ||
66 | // Compression strategies | |
67 | ||
68 | BOOST_IOSTREAMS_DECL extern const int default_strategy; | |
69 | BOOST_IOSTREAMS_DECL extern const int filtered; | |
70 | BOOST_IOSTREAMS_DECL extern const int huffman_only; | |
71 | ||
72 | // Status codes | |
73 | ||
74 | BOOST_IOSTREAMS_DECL extern const int okay; | |
75 | BOOST_IOSTREAMS_DECL extern const int stream_end; | |
76 | BOOST_IOSTREAMS_DECL extern const int stream_error; | |
77 | BOOST_IOSTREAMS_DECL extern const int version_error; | |
78 | BOOST_IOSTREAMS_DECL extern const int data_error; | |
79 | BOOST_IOSTREAMS_DECL extern const int mem_error; | |
80 | BOOST_IOSTREAMS_DECL extern const int buf_error; | |
81 | ||
82 | // Flush codes | |
83 | ||
84 | BOOST_IOSTREAMS_DECL extern const int finish; | |
85 | BOOST_IOSTREAMS_DECL extern const int no_flush; | |
86 | BOOST_IOSTREAMS_DECL extern const int sync_flush; | |
87 | ||
88 | // Code for current OS | |
89 | ||
90 | //BOOST_IOSTREAMS_DECL extern const int os_code; | |
91 | ||
92 | // Null pointer constant. | |
93 | ||
94 | const int null = 0; | |
95 | ||
96 | // Default values | |
97 | ||
98 | const int default_window_bits = 15; | |
99 | const int default_mem_level = 8; | |
100 | const bool default_crc = false; | |
101 | const bool default_noheader = false; | |
102 | ||
103 | } // End namespace zlib. | |
104 | ||
105 | // | |
106 | // Class name: zlib_params. | |
107 | // Description: Encapsulates the parameters passed to deflateInit2 | |
108 | // and inflateInit2 to customize compression and decompression. | |
109 | // | |
110 | struct zlib_params { | |
111 | ||
112 | // Non-explicit constructor. | |
113 | zlib_params( int level = zlib::default_compression, | |
114 | int method = zlib::deflated, | |
115 | int window_bits = zlib::default_window_bits, | |
116 | int mem_level = zlib::default_mem_level, | |
117 | int strategy = zlib::default_strategy, | |
118 | bool noheader = zlib::default_noheader, | |
119 | bool calculate_crc = zlib::default_crc ) | |
120 | : level(level), method(method), window_bits(window_bits), | |
121 | mem_level(mem_level), strategy(strategy), | |
122 | noheader(noheader), calculate_crc(calculate_crc) | |
123 | { } | |
124 | int level; | |
125 | int method; | |
126 | int window_bits; | |
127 | int mem_level; | |
128 | int strategy; | |
129 | bool noheader; | |
130 | bool calculate_crc; | |
131 | }; | |
132 | ||
133 | // | |
134 | // Class name: zlib_error. | |
135 | // Description: Subclass of std::ios::failure thrown to indicate | |
136 | // zlib errors other than out-of-memory conditions. | |
137 | // | |
138 | class BOOST_IOSTREAMS_DECL zlib_error : public BOOST_IOSTREAMS_FAILURE { | |
139 | public: | |
140 | explicit zlib_error(int error); | |
141 | int error() const { return error_; } | |
142 | static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error); | |
143 | private: | |
144 | int error_; | |
145 | }; | |
146 | ||
147 | namespace detail { | |
148 | ||
149 | template<typename Alloc> | |
150 | struct zlib_allocator_traits { | |
151 | #ifndef BOOST_NO_STD_ALLOCATOR | |
152 | typedef typename Alloc::template rebind<char>::other type; | |
153 | #else | |
154 | typedef std::allocator<char> type; | |
155 | #endif | |
156 | }; | |
157 | ||
158 | template< typename Alloc, | |
159 | typename Base = // VC6 workaround (C2516) | |
160 | BOOST_DEDUCED_TYPENAME zlib_allocator_traits<Alloc>::type > | |
161 | struct zlib_allocator : private Base { | |
162 | private: | |
163 | typedef typename Base::size_type size_type; | |
164 | public: | |
165 | BOOST_STATIC_CONSTANT(bool, custom = | |
166 | (!is_same<std::allocator<char>, Base>::value)); | |
167 | typedef typename zlib_allocator_traits<Alloc>::type allocator_type; | |
168 | static void* allocate(void* self, zlib::uint items, zlib::uint size); | |
169 | static void deallocate(void* self, void* address); | |
170 | }; | |
171 | ||
172 | class BOOST_IOSTREAMS_DECL zlib_base { | |
173 | public: | |
174 | typedef char char_type; | |
175 | protected: | |
176 | zlib_base(); | |
177 | ~zlib_base(); | |
178 | void* stream() { return stream_; } | |
179 | template<typename Alloc> | |
180 | void init( const zlib_params& p, | |
181 | bool compress, | |
182 | zlib_allocator<Alloc>& zalloc ) | |
183 | { | |
184 | bool custom = zlib_allocator<Alloc>::custom; | |
185 | do_init( p, compress, | |
186 | custom ? zlib_allocator<Alloc>::allocate : 0, | |
187 | custom ? zlib_allocator<Alloc>::deallocate : 0, | |
188 | &zalloc ); | |
189 | } | |
190 | void before( const char*& src_begin, const char* src_end, | |
191 | char*& dest_begin, char* dest_end ); | |
192 | void after( const char*& src_begin, char*& dest_begin, | |
193 | bool compress ); | |
194 | int xdeflate(int flush); // Prefix 'x' prevents symbols from being | |
195 | int xinflate(int flush); // redefined when Z_PREFIX is defined | |
196 | void reset(bool compress, bool realloc); | |
197 | public: | |
198 | zlib::ulong crc() const { return crc_; } | |
199 | int total_in() const { return total_in_; } | |
200 | int total_out() const { return total_out_; } | |
201 | private: | |
202 | void do_init( const zlib_params& p, bool compress, | |
203 | zlib::xalloc_func, | |
204 | zlib::xfree_func, | |
205 | void* derived ); | |
206 | void* stream_; // Actual type: z_stream*. | |
207 | bool calculate_crc_; | |
208 | zlib::ulong crc_; | |
209 | zlib::ulong crc_imp_; | |
210 | int total_in_; | |
211 | int total_out_; | |
212 | }; | |
213 | ||
214 | // | |
215 | // Template name: zlib_compressor_impl | |
216 | // Description: Model of C-Style Filte implementing compression by | |
217 | // delegating to the zlib function deflate. | |
218 | // | |
219 | template<typename Alloc = std::allocator<char> > | |
220 | class zlib_compressor_impl : public zlib_base, public zlib_allocator<Alloc> { | |
221 | public: | |
222 | zlib_compressor_impl(const zlib_params& = zlib::default_compression); | |
223 | ~zlib_compressor_impl(); | |
224 | bool filter( const char*& src_begin, const char* src_end, | |
225 | char*& dest_begin, char* dest_end, bool flush ); | |
226 | void close(); | |
227 | }; | |
228 | ||
229 | // | |
230 | // Template name: zlib_compressor | |
231 | // Description: Model of C-Style Filte implementing decompression by | |
232 | // delegating to the zlib function inflate. | |
233 | // | |
234 | template<typename Alloc = std::allocator<char> > | |
235 | class zlib_decompressor_impl : public zlib_base, public zlib_allocator<Alloc> { | |
236 | public: | |
237 | zlib_decompressor_impl(const zlib_params&); | |
238 | zlib_decompressor_impl(int window_bits = zlib::default_window_bits); | |
239 | ~zlib_decompressor_impl(); | |
240 | bool filter( const char*& begin_in, const char* end_in, | |
241 | char*& begin_out, char* end_out, bool flush ); | |
242 | void close(); | |
243 | bool eof() const { return eof_; } | |
244 | private: | |
245 | bool eof_; | |
246 | }; | |
247 | ||
248 | } // End namespace detail. | |
249 | ||
250 | // | |
251 | // Template name: zlib_compressor | |
252 | // Description: Model of InputFilter and OutputFilter implementing | |
253 | // compression using zlib. | |
254 | // | |
255 | template<typename Alloc = std::allocator<char> > | |
256 | struct basic_zlib_compressor | |
257 | : symmetric_filter<detail::zlib_compressor_impl<Alloc>, Alloc> | |
258 | { | |
259 | private: | |
260 | typedef detail::zlib_compressor_impl<Alloc> impl_type; | |
261 | typedef symmetric_filter<impl_type, Alloc> base_type; | |
262 | public: | |
263 | typedef typename base_type::char_type char_type; | |
264 | typedef typename base_type::category category; | |
265 | basic_zlib_compressor( const zlib_params& = zlib::default_compression, | |
266 | int buffer_size = default_device_buffer_size ); | |
267 | zlib::ulong crc() { return this->filter().crc(); } | |
268 | int total_in() { return this->filter().total_in(); } | |
269 | }; | |
270 | BOOST_IOSTREAMS_PIPABLE(basic_zlib_compressor, 1) | |
271 | ||
272 | typedef basic_zlib_compressor<> zlib_compressor; | |
273 | ||
274 | // | |
275 | // Template name: zlib_decompressor | |
276 | // Description: Model of InputFilter and OutputFilter implementing | |
277 | // decompression using zlib. | |
278 | // | |
279 | template<typename Alloc = std::allocator<char> > | |
280 | struct basic_zlib_decompressor | |
281 | : symmetric_filter<detail::zlib_decompressor_impl<Alloc>, Alloc> | |
282 | { | |
283 | private: | |
284 | typedef detail::zlib_decompressor_impl<Alloc> impl_type; | |
285 | typedef symmetric_filter<impl_type, Alloc> base_type; | |
286 | public: | |
287 | typedef typename base_type::char_type char_type; | |
288 | typedef typename base_type::category category; | |
289 | basic_zlib_decompressor( int window_bits = zlib::default_window_bits, | |
290 | int buffer_size = default_device_buffer_size ); | |
291 | basic_zlib_decompressor( const zlib_params& p, | |
292 | int buffer_size = default_device_buffer_size ); | |
293 | zlib::ulong crc() { return this->filter().crc(); } | |
294 | int total_out() { return this->filter().total_out(); } | |
295 | bool eof() { return this->filter().eof(); } | |
296 | }; | |
297 | BOOST_IOSTREAMS_PIPABLE(basic_zlib_decompressor, 1) | |
298 | ||
299 | typedef basic_zlib_decompressor<> zlib_decompressor; | |
300 | ||
301 | //----------------------------------------------------------------------------// | |
302 | ||
303 | //------------------Implementation of zlib_allocator--------------------------// | |
304 | ||
305 | namespace detail { | |
306 | ||
307 | template<typename Alloc, typename Base> | |
308 | void* zlib_allocator<Alloc, Base>::allocate | |
309 | (void* self, zlib::uint items, zlib::uint size) | |
310 | { | |
311 | size_type len = items * size; | |
312 | char* ptr = | |
313 | static_cast<allocator_type*>(self)->allocate | |
314 | (len + sizeof(size_type) | |
315 | #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) | |
316 | , (char*)0 | |
317 | #endif | |
318 | ); | |
319 | *reinterpret_cast<size_type*>(ptr) = len; | |
320 | return ptr + sizeof(size_type); | |
321 | } | |
322 | ||
323 | template<typename Alloc, typename Base> | |
324 | void zlib_allocator<Alloc, Base>::deallocate(void* self, void* address) | |
325 | { | |
326 | char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type); | |
327 | size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type); | |
328 | static_cast<allocator_type*>(self)->deallocate(ptr, len); | |
329 | } | |
330 | ||
331 | //------------------Implementation of zlib_compressor_impl--------------------// | |
332 | ||
333 | template<typename Alloc> | |
334 | zlib_compressor_impl<Alloc>::zlib_compressor_impl(const zlib_params& p) | |
335 | { init(p, true, static_cast<zlib_allocator<Alloc>&>(*this)); } | |
336 | ||
337 | template<typename Alloc> | |
338 | zlib_compressor_impl<Alloc>::~zlib_compressor_impl() | |
339 | { reset(true, false); } | |
340 | ||
341 | template<typename Alloc> | |
342 | bool zlib_compressor_impl<Alloc>::filter | |
343 | ( const char*& src_begin, const char* src_end, | |
344 | char*& dest_begin, char* dest_end, bool flush ) | |
345 | { | |
346 | before(src_begin, src_end, dest_begin, dest_end); | |
347 | int result = xdeflate(flush ? zlib::finish : zlib::no_flush); | |
348 | after(src_begin, dest_begin, true); | |
349 | zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result); | |
350 | return result != zlib::stream_end; | |
351 | } | |
352 | ||
353 | template<typename Alloc> | |
354 | void zlib_compressor_impl<Alloc>::close() { reset(true, true); } | |
355 | ||
356 | //------------------Implementation of zlib_decompressor_impl------------------// | |
357 | ||
358 | template<typename Alloc> | |
359 | zlib_decompressor_impl<Alloc>::zlib_decompressor_impl(const zlib_params& p) | |
360 | : eof_(false) | |
361 | { init(p, false, static_cast<zlib_allocator<Alloc>&>(*this)); } | |
362 | ||
363 | template<typename Alloc> | |
364 | zlib_decompressor_impl<Alloc>::~zlib_decompressor_impl() | |
365 | { reset(false, false); } | |
366 | ||
367 | template<typename Alloc> | |
368 | zlib_decompressor_impl<Alloc>::zlib_decompressor_impl(int window_bits) | |
369 | { | |
370 | zlib_params p; | |
371 | p.window_bits = window_bits; | |
372 | init(p, false, static_cast<zlib_allocator<Alloc>&>(*this)); | |
373 | } | |
374 | ||
375 | template<typename Alloc> | |
376 | bool zlib_decompressor_impl<Alloc>::filter | |
377 | ( const char*& src_begin, const char* src_end, | |
378 | char*& dest_begin, char* dest_end, bool /* flush */ ) | |
379 | { | |
380 | before(src_begin, src_end, dest_begin, dest_end); | |
381 | int result = xinflate(zlib::sync_flush); | |
382 | after(src_begin, dest_begin, false); | |
383 | zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result); | |
384 | return !(eof_ = result == zlib::stream_end); | |
385 | } | |
386 | ||
387 | template<typename Alloc> | |
388 | void zlib_decompressor_impl<Alloc>::close() { | |
389 | eof_ = false; | |
390 | reset(false, true); | |
391 | } | |
392 | ||
393 | } // End namespace detail. | |
394 | ||
395 | //------------------Implementation of zlib_decompressor-----------------------// | |
396 | ||
397 | template<typename Alloc> | |
398 | basic_zlib_compressor<Alloc>::basic_zlib_compressor | |
399 | (const zlib_params& p, int buffer_size) | |
400 | : base_type(buffer_size, p) { } | |
401 | ||
402 | //------------------Implementation of zlib_decompressor-----------------------// | |
403 | ||
404 | template<typename Alloc> | |
405 | basic_zlib_decompressor<Alloc>::basic_zlib_decompressor | |
406 | (int window_bits, int buffer_size) | |
407 | : base_type(buffer_size, window_bits) { } | |
408 | ||
409 | template<typename Alloc> | |
410 | basic_zlib_decompressor<Alloc>::basic_zlib_decompressor | |
411 | (const zlib_params& p, int buffer_size) | |
412 | : base_type(buffer_size, p) { } | |
413 | ||
414 | //----------------------------------------------------------------------------// | |
415 | ||
416 | } } // End namespaces iostreams, boost. | |
417 | ||
418 | #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas. | |
419 | #ifdef BOOST_MSVC | |
420 | # pragma warning(pop) | |
421 | #endif | |
422 | ||
423 | #endif // #ifndef BOOST_IOSTREAMS_ZLIB_HPP_INCLUDED |