Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame] | 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 NDNBOOST_IOSTREAMS_BZIP2_HPP_INCLUDED |
| 12 | #define NDNBOOST_IOSTREAMS_BZIP2_HPP_INCLUDED |
| 13 | |
| 14 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 15 | # pragma once |
| 16 | #endif |
| 17 | |
| 18 | #include <cassert> |
| 19 | #include <memory> // allocator. |
| 20 | #include <new> // bad_alloc. |
| 21 | #include <ndnboost/config.hpp> // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM. |
| 22 | #include <ndnboost/detail/workaround.hpp> |
| 23 | #include <ndnboost/iostreams/constants.hpp> // buffer size. |
| 24 | #include <ndnboost/iostreams/detail/config/auto_link.hpp> |
| 25 | #include <ndnboost/iostreams/detail/config/bzip2.hpp> |
| 26 | #include <ndnboost/iostreams/detail/config/dyn_link.hpp> |
| 27 | #include <ndnboost/iostreams/detail/config/wide_streams.hpp> |
| 28 | #include <ndnboost/iostreams/detail/ios.hpp> // failure, streamsize. |
| 29 | #include <ndnboost/iostreams/filter/symmetric.hpp> |
| 30 | #include <ndnboost/iostreams/pipeline.hpp> |
| 31 | #include <ndnboost/type_traits/is_same.hpp> |
| 32 | |
| 33 | // Must come last. |
| 34 | #ifdef NDNBOOST_MSVC |
| 35 | # pragma warning(push) |
| 36 | # pragma warning(disable:4251 4231 4660) |
| 37 | #endif |
| 38 | #include <ndnboost/config/abi_prefix.hpp> |
| 39 | |
| 40 | // Temporary fix. |
| 41 | #undef small |
| 42 | |
| 43 | namespace ndnboost { 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 | NDNBOOST_IOSTREAMS_DECL extern const int ok; |
| 55 | NDNBOOST_IOSTREAMS_DECL extern const int run_ok; |
| 56 | NDNBOOST_IOSTREAMS_DECL extern const int flush_ok; |
| 57 | NDNBOOST_IOSTREAMS_DECL extern const int finish_ok; |
| 58 | NDNBOOST_IOSTREAMS_DECL extern const int stream_end; |
| 59 | NDNBOOST_IOSTREAMS_DECL extern const int sequence_error; |
| 60 | NDNBOOST_IOSTREAMS_DECL extern const int param_error; |
| 61 | NDNBOOST_IOSTREAMS_DECL extern const int mem_error; |
| 62 | NDNBOOST_IOSTREAMS_DECL extern const int data_error; |
| 63 | NDNBOOST_IOSTREAMS_DECL extern const int data_error_magic; |
| 64 | NDNBOOST_IOSTREAMS_DECL extern const int io_error; |
| 65 | NDNBOOST_IOSTREAMS_DECL extern const int unexpected_eof; |
| 66 | NDNBOOST_IOSTREAMS_DECL extern const int outbuff_full; |
| 67 | NDNBOOST_IOSTREAMS_DECL extern const int config_error; |
| 68 | |
| 69 | // Action codes |
| 70 | |
| 71 | NDNBOOST_IOSTREAMS_DECL extern const int finish; |
| 72 | NDNBOOST_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 NDNBOOST_IOSTREAMS_DECL bzip2_error : public NDNBOOST_IOSTREAMS_FAILURE { |
| 113 | public: |
| 114 | explicit bzip2_error(int error); |
| 115 | int error() const { return error_; } |
| 116 | static void check NDNBOOST_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 NDNBOOST_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 | NDNBOOST_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 | NDNBOOST_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 NDNBOOST_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 | #if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1300) |
| 161 | custom ? bzip2_allocator<Alloc>::allocate : 0, |
| 162 | custom ? bzip2_allocator<Alloc>::deallocate : 0, |
| 163 | #endif |
| 164 | custom ? &alloc : 0 ); |
| 165 | } |
| 166 | void before( const char*& src_begin, const char* src_end, |
| 167 | char*& dest_begin, char* dest_end ); |
| 168 | void after(const char*& src_begin, char*& dest_begin); |
| 169 | int check_end(const char* src_begin, const char* dest_begin); |
| 170 | int compress(int action); |
| 171 | int decompress(); |
| 172 | void end(bool compress); |
| 173 | private: |
| 174 | void do_init( bool compress, |
| 175 | #if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1300) |
| 176 | bzip2::alloc_func, |
| 177 | bzip2::free_func, |
| 178 | #endif |
| 179 | void* derived ); |
| 180 | bzip2_params params_; |
| 181 | void* stream_; // Actual type: bz_stream*. |
| 182 | bool ready_; |
| 183 | }; |
| 184 | |
| 185 | // |
| 186 | // Template name: bzip2_compressor_impl |
| 187 | // Description: Model of SymmetricFilter implementing compression by |
| 188 | // delegating to the libbzip2 function BZ_bzCompress. |
| 189 | // |
| 190 | template<typename Alloc = std::allocator<char> > |
| 191 | class bzip2_compressor_impl |
| 192 | : public bzip2_base, |
| 193 | #if NDNBOOST_WORKAROUND(__BORLANDC__, < 0x600) |
| 194 | public |
| 195 | #endif |
| 196 | bzip2_allocator<Alloc> |
| 197 | { |
| 198 | public: |
| 199 | bzip2_compressor_impl(const bzip2_params&); |
| 200 | bool filter( const char*& src_begin, const char* src_end, |
| 201 | char*& dest_begin, char* dest_end, bool flush ); |
| 202 | void close(); |
| 203 | private: |
| 204 | void init(); |
| 205 | bool eof_; // Guard to make sure filter() isn't called after it returns false. |
| 206 | }; |
| 207 | |
| 208 | // |
| 209 | // Template name: bzip2_compressor |
| 210 | // Description: Model of SymmetricFilter implementing decompression by |
| 211 | // delegating to the libbzip2 function BZ_bzDecompress. |
| 212 | // |
| 213 | template<typename Alloc = std::allocator<char> > |
| 214 | class bzip2_decompressor_impl |
| 215 | : public bzip2_base, |
| 216 | #if NDNBOOST_WORKAROUND(__BORLANDC__, < 0x600) |
| 217 | public |
| 218 | #endif |
| 219 | bzip2_allocator<Alloc> |
| 220 | { |
| 221 | public: |
| 222 | bzip2_decompressor_impl(bool small = bzip2::default_small); |
| 223 | bool filter( const char*& begin_in, const char* end_in, |
| 224 | char*& begin_out, char* end_out, bool flush ); |
| 225 | void close(); |
| 226 | private: |
| 227 | void init(); |
| 228 | bool eof_; // Guard to make sure filter() isn't called after it returns false. |
| 229 | }; |
| 230 | |
| 231 | } // End namespace detail. |
| 232 | |
| 233 | // |
| 234 | // Template name: bzip2_compressor |
| 235 | // Description: Model of InputFilter and OutputFilter implementing |
| 236 | // compression using libbzip2. |
| 237 | // |
| 238 | template<typename Alloc = std::allocator<char> > |
| 239 | struct basic_bzip2_compressor |
| 240 | : symmetric_filter<detail::bzip2_compressor_impl<Alloc>, Alloc> |
| 241 | { |
| 242 | private: |
| 243 | typedef detail::bzip2_compressor_impl<Alloc> impl_type; |
| 244 | typedef symmetric_filter<impl_type, Alloc> base_type; |
| 245 | public: |
| 246 | typedef typename base_type::char_type char_type; |
| 247 | typedef typename base_type::category category; |
| 248 | basic_bzip2_compressor( const bzip2_params& = bzip2::default_block_size, |
| 249 | int buffer_size = default_device_buffer_size ); |
| 250 | }; |
| 251 | NDNBOOST_IOSTREAMS_PIPABLE(basic_bzip2_compressor, 1) |
| 252 | |
| 253 | typedef basic_bzip2_compressor<> bzip2_compressor; |
| 254 | |
| 255 | // |
| 256 | // Template name: bzip2_decompressor |
| 257 | // Description: Model of InputFilter and OutputFilter implementing |
| 258 | // decompression using libbzip2. |
| 259 | // |
| 260 | template<typename Alloc = std::allocator<char> > |
| 261 | struct basic_bzip2_decompressor |
| 262 | : symmetric_filter<detail::bzip2_decompressor_impl<Alloc>, Alloc> |
| 263 | { |
| 264 | private: |
| 265 | typedef detail::bzip2_decompressor_impl<Alloc> impl_type; |
| 266 | typedef symmetric_filter<impl_type, Alloc> base_type; |
| 267 | public: |
| 268 | typedef typename base_type::char_type char_type; |
| 269 | typedef typename base_type::category category; |
| 270 | basic_bzip2_decompressor( bool small = bzip2::default_small, |
| 271 | int buffer_size = default_device_buffer_size ); |
| 272 | }; |
| 273 | NDNBOOST_IOSTREAMS_PIPABLE(basic_bzip2_decompressor, 1) |
| 274 | |
| 275 | typedef basic_bzip2_decompressor<> bzip2_decompressor; |
| 276 | |
| 277 | //----------------------------------------------------------------------------// |
| 278 | |
| 279 | //------------------Implementation of bzip2_allocator-------------------------// |
| 280 | |
| 281 | namespace detail { |
| 282 | |
| 283 | template<typename Alloc, typename Base> |
| 284 | void* bzip2_allocator<Alloc, Base>::allocate(void* self, int items, int size) |
| 285 | { |
| 286 | size_type len = items * size; |
| 287 | char* ptr = |
| 288 | static_cast<allocator_type*>(self)->allocate |
| 289 | (len + sizeof(size_type) |
| 290 | #if NDNBOOST_WORKAROUND(NDNBOOST_DINKUMWARE_STDLIB, == 1) |
| 291 | , (char*)0 |
| 292 | #endif |
| 293 | ); |
| 294 | *reinterpret_cast<size_type*>(ptr) = len; |
| 295 | return ptr + sizeof(size_type); |
| 296 | } |
| 297 | |
| 298 | template<typename Alloc, typename Base> |
| 299 | void bzip2_allocator<Alloc, Base>::deallocate(void* self, void* address) |
| 300 | { |
| 301 | char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type); |
| 302 | size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type); |
| 303 | static_cast<allocator_type*>(self)->deallocate(ptr, len); |
| 304 | } |
| 305 | |
| 306 | //------------------Implementation of bzip2_compressor_impl-------------------// |
| 307 | |
| 308 | template<typename Alloc> |
| 309 | bzip2_compressor_impl<Alloc>::bzip2_compressor_impl(const bzip2_params& p) |
| 310 | : bzip2_base(p), eof_(false) { } |
| 311 | |
| 312 | template<typename Alloc> |
| 313 | bool bzip2_compressor_impl<Alloc>::filter |
| 314 | ( const char*& src_begin, const char* src_end, |
| 315 | char*& dest_begin, char* dest_end, bool flush ) |
| 316 | { |
| 317 | if (!ready()) init(); |
| 318 | if (eof_) return false; |
| 319 | before(src_begin, src_end, dest_begin, dest_end); |
| 320 | int result = compress(flush ? bzip2::finish : bzip2::run); |
| 321 | after(src_begin, dest_begin); |
| 322 | bzip2_error::check NDNBOOST_PREVENT_MACRO_SUBSTITUTION(result); |
| 323 | return !(eof_ = result == bzip2::stream_end); |
| 324 | } |
| 325 | |
| 326 | template<typename Alloc> |
| 327 | void bzip2_compressor_impl<Alloc>::close() |
| 328 | { |
| 329 | try { |
| 330 | end(true); |
| 331 | } catch (...) { |
| 332 | eof_ = false; |
| 333 | throw; |
| 334 | } |
| 335 | eof_ = false; |
| 336 | } |
| 337 | |
| 338 | template<typename Alloc> |
| 339 | inline void bzip2_compressor_impl<Alloc>::init() |
| 340 | { bzip2_base::init(true, static_cast<bzip2_allocator<Alloc>&>(*this)); } |
| 341 | |
| 342 | //------------------Implementation of bzip2_decompressor_impl-----------------// |
| 343 | |
| 344 | template<typename Alloc> |
| 345 | bzip2_decompressor_impl<Alloc>::bzip2_decompressor_impl(bool small) |
| 346 | : bzip2_base(bzip2_params(small)), eof_(false) { } |
| 347 | |
| 348 | template<typename Alloc> |
| 349 | bool bzip2_decompressor_impl<Alloc>::filter |
| 350 | ( const char*& src_begin, const char* src_end, |
| 351 | char*& dest_begin, char* dest_end, bool flush ) |
| 352 | { |
| 353 | if (eof_) { |
| 354 | // reset the stream if there are more characters |
| 355 | if(src_begin == src_end) |
| 356 | return false; |
| 357 | else |
| 358 | close(); |
| 359 | } |
| 360 | if (!ready()) |
| 361 | init(); |
| 362 | before(src_begin, src_end, dest_begin, dest_end); |
| 363 | int result = decompress(); |
| 364 | if(result == bzip2::ok && flush) |
| 365 | result = check_end(src_begin, dest_begin); |
| 366 | after(src_begin, dest_begin); |
| 367 | bzip2_error::check NDNBOOST_PREVENT_MACRO_SUBSTITUTION(result); |
| 368 | eof_ = result == bzip2::stream_end; |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | template<typename Alloc> |
| 373 | void bzip2_decompressor_impl<Alloc>::close() |
| 374 | { |
| 375 | try { |
| 376 | end(false); |
| 377 | } catch (...) { |
| 378 | eof_ = false; |
| 379 | throw; |
| 380 | } |
| 381 | eof_ = false; |
| 382 | } |
| 383 | |
| 384 | template<typename Alloc> |
| 385 | inline void bzip2_decompressor_impl<Alloc>::init() |
| 386 | { bzip2_base::init(false, static_cast<bzip2_allocator<Alloc>&>(*this)); } |
| 387 | } // End namespace detail. |
| 388 | |
| 389 | //------------------Implementation of bzip2_decompressor----------------------// |
| 390 | |
| 391 | template<typename Alloc> |
| 392 | basic_bzip2_compressor<Alloc>::basic_bzip2_compressor |
| 393 | (const bzip2_params& p, int buffer_size) |
| 394 | : base_type(buffer_size, p) |
| 395 | { } |
| 396 | |
| 397 | //------------------Implementation of bzip2_decompressor----------------------// |
| 398 | |
| 399 | template<typename Alloc> |
| 400 | basic_bzip2_decompressor<Alloc>::basic_bzip2_decompressor |
| 401 | (bool small, int buffer_size) |
| 402 | : base_type(buffer_size, small) |
| 403 | { } |
| 404 | |
| 405 | //----------------------------------------------------------------------------// |
| 406 | |
| 407 | } } // End namespaces iostreams, boost. |
| 408 | |
| 409 | #include <ndnboost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas. |
| 410 | #ifdef NDNBOOST_MSVC |
| 411 | # pragma warning(pop) |
| 412 | #endif |
| 413 | |
| 414 | #endif // #ifndef NDNBOOST_IOSTREAMS_BZIP2_HPP_INCLUDED |