Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:44:30

0001 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
0002 // (C) Copyright 2003-2007 Jonathan Turkanis
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
0005 
0006 // See http://www.boost.org/libs/iostreams for documentation.
0007 
0008 // Note: custom allocators are not supported on VC6, since that compiler
0009 // had trouble finding the function zlib_base::do_init.
0010 
0011 #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED
0012 #define BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED
0013 
0014 #if defined(_MSC_VER)
0015 # pragma once
0016 #endif
0017                    
0018 #include <cassert>                            
0019 #include <memory>            // allocator.
0020 #include <new>               // bad_alloc.
0021 #include <boost/config.hpp>  // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM.
0022 #include <boost/detail/workaround.hpp>
0023 #include <boost/iostreams/constants.hpp>   // buffer size.
0024 #include <boost/iostreams/detail/config/auto_link.hpp>
0025 #include <boost/iostreams/detail/config/bzip2.hpp>
0026 #include <boost/iostreams/detail/config/dyn_link.hpp>
0027 #include <boost/iostreams/detail/config/wide_streams.hpp>
0028 #include <boost/iostreams/detail/ios.hpp>  // failure, streamsize.
0029 #include <boost/iostreams/filter/symmetric.hpp>               
0030 #include <boost/iostreams/pipeline.hpp>       
0031 #include <boost/type_traits/is_same.hpp>     
0032 
0033 // Must come last.
0034 #ifdef BOOST_MSVC
0035 # pragma warning(push)
0036 # pragma warning(disable:4251 4231 4660)
0037 #endif
0038 #include <boost/config/abi_prefix.hpp>           
0039 
0040 // Temporary fix.
0041 #undef small
0042 
0043 namespace boost { namespace iostreams {
0044 
0045 namespace bzip2 {
0046 
0047                     // Typedefs.
0048 
0049 typedef void* (*alloc_func)(void*, int, int);
0050 typedef void (*free_func)(void*, void*);
0051 
0052                     // Status codes
0053 
0054 BOOST_IOSTREAMS_DECL extern const int ok;
0055 BOOST_IOSTREAMS_DECL extern const int run_ok;
0056 BOOST_IOSTREAMS_DECL extern const int flush_ok;
0057 BOOST_IOSTREAMS_DECL extern const int finish_ok;
0058 BOOST_IOSTREAMS_DECL extern const int stream_end;    
0059 BOOST_IOSTREAMS_DECL extern const int sequence_error;
0060 BOOST_IOSTREAMS_DECL extern const int param_error;
0061 BOOST_IOSTREAMS_DECL extern const int mem_error;
0062 BOOST_IOSTREAMS_DECL extern const int data_error;
0063 BOOST_IOSTREAMS_DECL extern const int data_error_magic;
0064 BOOST_IOSTREAMS_DECL extern const int io_error;
0065 BOOST_IOSTREAMS_DECL extern const int unexpected_eof;
0066 BOOST_IOSTREAMS_DECL extern const int outbuff_full;
0067 BOOST_IOSTREAMS_DECL extern const int config_error;
0068 
0069                     // Action codes
0070 
0071 BOOST_IOSTREAMS_DECL extern const int finish;
0072 BOOST_IOSTREAMS_DECL extern const int run;
0073 
0074                     // Default values
0075 
0076 const int default_block_size   = 9;
0077 const int default_work_factor  = 30;
0078 const bool default_small       = false;
0079 
0080 } // End namespace bzip2. 
0081 
0082 //
0083 // Class name: bzip2_params.
0084 // Description: Encapsulates the parameters passed to deflateInit2
0085 //      to customize compression.
0086 //
0087 struct bzip2_params {
0088 
0089     // Non-explicit constructor for compression.
0090     bzip2_params( int block_size_  = bzip2::default_block_size,
0091                   int work_factor_ = bzip2::default_work_factor )
0092         : block_size(block_size_), work_factor(work_factor_)
0093         { }
0094 
0095     // Constructor for decompression.
0096     bzip2_params(bool small)
0097         : small(small), work_factor(0)
0098         { }
0099 
0100     union {
0101         int   block_size;    // For compression.
0102         bool  small;         // For decompression.
0103     };
0104     int       work_factor;
0105 };
0106 
0107 //
0108 // Class name: bzip2_error.
0109 // Description: Subclass of std::ios_base::failure thrown to indicate
0110 //     bzip2 errors other than out-of-memory conditions.
0111 //
0112 class BOOST_IOSTREAMS_DECL bzip2_error : public BOOST_IOSTREAMS_FAILURE {
0113 public:
0114     explicit bzip2_error(int error);
0115     int error() const { return error_; }
0116     static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
0117 private:
0118     int error_;
0119 };
0120 
0121 namespace detail {
0122 
0123 template<typename Alloc>
0124 struct bzip2_allocator_traits {
0125 #ifndef BOOST_NO_STD_ALLOCATOR
0126 #if defined(BOOST_NO_CXX11_ALLOCATOR)
0127     typedef typename Alloc::template rebind<char>::other type;
0128 #else
0129     typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
0130 #endif
0131 #else
0132     typedef std::allocator<char> type;
0133 #endif
0134 };
0135 
0136 template< typename Alloc,
0137           typename Base = // VC6 workaround (C2516)
0138               BOOST_DEDUCED_TYPENAME bzip2_allocator_traits<Alloc>::type >
0139 struct bzip2_allocator : private Base {
0140 private:
0141 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
0142     typedef typename Base::size_type size_type;
0143 #else
0144     typedef typename std::allocator_traits<Base>::size_type size_type;
0145 #endif
0146 public:
0147     BOOST_STATIC_CONSTANT(bool, custom = 
0148         (!is_same<std::allocator<char>, Base>::value));
0149     typedef typename bzip2_allocator_traits<Alloc>::type allocator_type;
0150     static void* allocate(void* self, int items, int size);
0151     static void deallocate(void* self, void* address);
0152 };
0153 
0154 class BOOST_IOSTREAMS_DECL bzip2_base  { 
0155 public:
0156     typedef char char_type;
0157 protected:
0158     bzip2_base(const bzip2_params& params);
0159     ~bzip2_base();
0160     bzip2_params& params() { return params_; }
0161     bool& ready() { return ready_; }
0162     template<typename Alloc> 
0163     void init( bool compress,
0164                bzip2_allocator<Alloc>& alloc )
0165         {
0166             bool custom = bzip2_allocator<Alloc>::custom;
0167             do_init( compress,
0168                      custom ? bzip2_allocator<Alloc>::allocate : 0,
0169                      custom ? bzip2_allocator<Alloc>::deallocate : 0,
0170                      custom ? &alloc : 0 );
0171         }
0172     void before( const char*& src_begin, const char* src_end,
0173                  char*& dest_begin, char* dest_end );
0174     void after(const char*& src_begin, char*& dest_begin);
0175     int check_end(const char* src_begin, const char* dest_begin);
0176     int compress(int action);
0177     int decompress();
0178     int end(bool compress, std::nothrow_t);
0179     void end(bool compress);
0180 private:
0181     void do_init( bool compress, 
0182                   bzip2::alloc_func,
0183                   bzip2::free_func, 
0184                   void* derived );
0185     bzip2_params  params_;
0186     void*         stream_; // Actual type: bz_stream*.
0187     bool          ready_;
0188 };
0189 
0190 //
0191 // Template name: bzip2_compressor_impl
0192 // Description: Model of SymmetricFilter implementing compression by
0193 //      delegating to the libbzip2 function BZ_bzCompress.
0194 //
0195 template<typename Alloc = std::allocator<char> >
0196 class bzip2_compressor_impl 
0197     : public bzip2_base, 
0198       #if BOOST_WORKAROUND(BOOST_BORLANDC, < 0x600)
0199           public
0200       #endif
0201       bzip2_allocator<Alloc> 
0202 {
0203 public: 
0204     bzip2_compressor_impl(const bzip2_params&);
0205     ~bzip2_compressor_impl();
0206     bool filter( const char*& src_begin, const char* src_end,
0207                  char*& dest_begin, char* dest_end, bool flush );
0208     void close();
0209 private:
0210     void init();
0211     bool eof_; // Guard to make sure filter() isn't called after it returns false.
0212 };
0213 
0214 //
0215 // Template name: bzip2_compressor
0216 // Description: Model of SymmetricFilter implementing decompression by
0217 //      delegating to the libbzip2 function BZ_bzDecompress.
0218 //
0219 template<typename Alloc = std::allocator<char> >
0220 class bzip2_decompressor_impl 
0221     : public bzip2_base, 
0222       #if BOOST_WORKAROUND(BOOST_BORLANDC, < 0x600)
0223           public
0224       #endif
0225       bzip2_allocator<Alloc> 
0226 { 
0227 public:
0228     bzip2_decompressor_impl(bool small = bzip2::default_small);
0229     ~bzip2_decompressor_impl();
0230     bool filter( const char*& begin_in, const char* end_in,
0231                  char*& begin_out, char* end_out, bool flush );
0232     void close();
0233 private:
0234     void init();
0235     bool eof_; // Guard to make sure filter() isn't called after it returns false.
0236 };
0237 
0238 } // End namespace detail.
0239 
0240 //
0241 // Template name: bzip2_compressor
0242 // Description: Model of InputFilter and OutputFilter implementing
0243 //      compression using libbzip2.
0244 //
0245 template<typename Alloc = std::allocator<char> >
0246 struct basic_bzip2_compressor 
0247     : symmetric_filter<detail::bzip2_compressor_impl<Alloc>, Alloc> 
0248 {
0249 private:
0250     typedef detail::bzip2_compressor_impl<Alloc>        impl_type;
0251     typedef symmetric_filter<impl_type, Alloc>  base_type;
0252 public:
0253     typedef typename base_type::char_type               char_type;
0254     typedef typename base_type::category                category;
0255     basic_bzip2_compressor( const bzip2_params& = bzip2::default_block_size, 
0256                             std::streamsize buffer_size =  default_device_buffer_size );
0257 };
0258 BOOST_IOSTREAMS_PIPABLE(basic_bzip2_compressor, 1)
0259 
0260 typedef basic_bzip2_compressor<> bzip2_compressor;
0261 
0262 //
0263 // Template name: bzip2_decompressor
0264 // Description: Model of InputFilter and OutputFilter implementing
0265 //      decompression using libbzip2.
0266 //
0267 template<typename Alloc = std::allocator<char> >
0268 struct basic_bzip2_decompressor 
0269     : symmetric_filter<detail::bzip2_decompressor_impl<Alloc>, Alloc> 
0270 {
0271 private:
0272     typedef detail::bzip2_decompressor_impl<Alloc>      impl_type;
0273     typedef symmetric_filter<impl_type, Alloc>  base_type;
0274 public:
0275     typedef typename base_type::char_type               char_type;
0276     typedef typename base_type::category                category;
0277     basic_bzip2_decompressor( bool small = bzip2::default_small,
0278                               std::streamsize buffer_size = default_device_buffer_size );
0279 };
0280 BOOST_IOSTREAMS_PIPABLE(basic_bzip2_decompressor, 1)
0281 
0282 typedef basic_bzip2_decompressor<> bzip2_decompressor;
0283 
0284 //----------------------------------------------------------------------------//
0285 
0286 //------------------Implementation of bzip2_allocator-------------------------//
0287 
0288 namespace detail {
0289 
0290 template<typename Alloc, typename Base>
0291 void* bzip2_allocator<Alloc, Base>::allocate(void* self, int items, int size)
0292 { 
0293     size_type len = items * size;
0294     char* ptr = 
0295         static_cast<allocator_type*>(self)->allocate
0296             (len + sizeof(size_type)
0297             #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
0298                 , (char*)0
0299             #endif
0300             );
0301     *reinterpret_cast<size_type*>(ptr) = len;
0302     return ptr + sizeof(size_type);
0303 }
0304 
0305 template<typename Alloc, typename Base>
0306 void bzip2_allocator<Alloc, Base>::deallocate(void* self, void* address)
0307 { 
0308     char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
0309     size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
0310     static_cast<allocator_type*>(self)->deallocate(ptr, len); 
0311 }
0312 
0313 //------------------Implementation of bzip2_compressor_impl-------------------//
0314 
0315 template<typename Alloc>
0316 bzip2_compressor_impl<Alloc>::bzip2_compressor_impl(const bzip2_params& p)
0317     : bzip2_base(p), eof_(false) { }
0318 
0319 template<typename Alloc>
0320 bzip2_compressor_impl<Alloc>::~bzip2_compressor_impl()
0321 { (void) bzip2_base::end(true, std::nothrow); }
0322 
0323 template<typename Alloc>
0324 bool bzip2_compressor_impl<Alloc>::filter
0325     ( const char*& src_begin, const char* src_end,
0326       char*& dest_begin, char* dest_end, bool flush )
0327 {
0328     if (!ready()) init();
0329     if (eof_) return false;
0330     before(src_begin, src_end, dest_begin, dest_end);
0331     int result = compress(flush ? bzip2::finish : bzip2::run);
0332     after(src_begin, dest_begin);
0333     bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
0334     return !(eof_ = result == bzip2::stream_end);
0335 }
0336 
0337 template<typename Alloc>
0338 void bzip2_compressor_impl<Alloc>::close() 
0339 { 
0340     try {
0341         end(true);
0342     } catch (...) { 
0343         eof_ = false; 
0344         throw;
0345     }
0346     eof_ = false;
0347 }
0348 
0349 template<typename Alloc>
0350 inline void bzip2_compressor_impl<Alloc>::init() 
0351 { bzip2_base::init(true, static_cast<bzip2_allocator<Alloc>&>(*this)); }
0352 
0353 //------------------Implementation of bzip2_decompressor_impl-----------------//
0354 
0355 template<typename Alloc>
0356 bzip2_decompressor_impl<Alloc>::bzip2_decompressor_impl(bool small)
0357     : bzip2_base(bzip2_params(small)), eof_(false) { }
0358 
0359 template<typename Alloc>
0360 bzip2_decompressor_impl<Alloc>::~bzip2_decompressor_impl()
0361 { (void) bzip2_base::end(false, std::nothrow); }
0362 
0363 template<typename Alloc>
0364 bool bzip2_decompressor_impl<Alloc>::filter
0365     ( const char*& src_begin, const char* src_end,
0366       char*& dest_begin, char* dest_end, bool flush )
0367 {
0368     do {
0369         if (eof_) {
0370             // reset the stream if there are more characters
0371             if(src_begin == src_end)
0372                 return false;
0373             else
0374                 close();
0375         }
0376         if (!ready()) 
0377             init();
0378         before(src_begin, src_end, dest_begin, dest_end);
0379         int result = decompress();
0380         if(result == bzip2::ok && flush)
0381             result = check_end(src_begin, dest_begin);
0382         after(src_begin, dest_begin);
0383         bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
0384         eof_ = result == bzip2::stream_end;
0385     } while (eof_ && src_begin != src_end && dest_begin != dest_end);
0386     return true; 
0387 }
0388 
0389 template<typename Alloc>
0390 void bzip2_decompressor_impl<Alloc>::close() 
0391 { 
0392     try {
0393         end(false);
0394     } catch (...) { 
0395         eof_ = false; 
0396         throw;
0397     }
0398     eof_ = false;
0399 }
0400 
0401 template<typename Alloc>
0402 inline void bzip2_decompressor_impl<Alloc>::init()
0403 { bzip2_base::init(false, static_cast<bzip2_allocator<Alloc>&>(*this)); }
0404 } // End namespace detail.
0405 
0406 //------------------Implementation of bzip2_decompressor----------------------//
0407 
0408 template<typename Alloc>
0409 basic_bzip2_compressor<Alloc>::basic_bzip2_compressor
0410         (const bzip2_params& p, std::streamsize buffer_size) 
0411     : base_type(buffer_size, p) 
0412     { }
0413 
0414 //------------------Implementation of bzip2_decompressor----------------------//
0415 
0416 template<typename Alloc>
0417 basic_bzip2_decompressor<Alloc>::basic_bzip2_decompressor
0418         (bool small, std::streamsize buffer_size) 
0419     : base_type(buffer_size, small)
0420     { }
0421 
0422 //----------------------------------------------------------------------------//
0423 
0424 } } // End namespaces iostreams, boost.
0425 
0426 #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
0427 #ifdef BOOST_MSVC
0428 # pragma warning(pop)
0429 #endif
0430 
0431 #endif // #ifndef BOOST_IOSTREAMS_BZIP2_HPP_INCLUDED