Back to home page

EIC code displayed by LXR

 
 

    


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

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_ZLIB_HPP_INCLUDED
0012 #define BOOST_IOSTREAMS_ZLIB_HPP_INCLUDED
0013 
0014 #if defined(_MSC_VER)
0015 # pragma once
0016 #endif              
0017 
0018 #include <cassert>                            
0019 #include <iosfwd>            // streamsize.                 
0020 #include <memory>            // allocator, bad_alloc.
0021 #include <new>          
0022 #include <boost/config.hpp>  // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM.
0023 #include <boost/cstdint.hpp> // uint*_t
0024 #include <boost/detail/workaround.hpp>
0025 #include <boost/iostreams/constants.hpp>   // buffer size.
0026 #include <boost/iostreams/detail/config/auto_link.hpp>
0027 #include <boost/iostreams/detail/config/dyn_link.hpp>
0028 #include <boost/iostreams/detail/config/wide_streams.hpp>
0029 #include <boost/iostreams/detail/config/zlib.hpp>
0030 #include <boost/iostreams/detail/ios.hpp>  // failure, streamsize.
0031 #include <boost/iostreams/filter/symmetric.hpp>                
0032 #include <boost/iostreams/pipeline.hpp>                
0033 #include <boost/type_traits/is_same.hpp>
0034 
0035 // Must come last.
0036 #ifdef BOOST_MSVC
0037 # pragma warning(push)
0038 # pragma warning(disable:4251 4275 4231 4660)         // Dependencies not exported.
0039 #endif
0040 #include <boost/config/abi_prefix.hpp>           
0041 
0042 namespace boost { namespace iostreams {
0043 
0044 namespace zlib {
0045                     // Typedefs
0046 
0047 typedef uint32_t uint;
0048 typedef uint8_t byte;
0049 typedef uint32_t ulong;
0050 
0051 // Prefix 'x' prevents symbols from being redefined when Z_PREFIX is defined
0052 typedef void* (*xalloc_func)(void*, zlib::uint, zlib::uint);
0053 typedef void (*xfree_func)(void*, void*);
0054 
0055                     // Compression levels
0056 
0057 BOOST_IOSTREAMS_DECL extern const int no_compression;
0058 BOOST_IOSTREAMS_DECL extern const int best_speed;
0059 BOOST_IOSTREAMS_DECL extern const int best_compression;
0060 BOOST_IOSTREAMS_DECL extern const int default_compression;
0061 
0062                     // Compression methods
0063 
0064 BOOST_IOSTREAMS_DECL extern const int deflated;
0065 
0066                     // Compression strategies
0067 
0068 BOOST_IOSTREAMS_DECL extern const int default_strategy;
0069 BOOST_IOSTREAMS_DECL extern const int filtered;
0070 BOOST_IOSTREAMS_DECL extern const int huffman_only;
0071 
0072                     // Status codes
0073 
0074 BOOST_IOSTREAMS_DECL extern const int okay;
0075 BOOST_IOSTREAMS_DECL extern const int stream_end;
0076 BOOST_IOSTREAMS_DECL extern const int stream_error;
0077 BOOST_IOSTREAMS_DECL extern const int version_error;
0078 BOOST_IOSTREAMS_DECL extern const int data_error;
0079 BOOST_IOSTREAMS_DECL extern const int mem_error;
0080 BOOST_IOSTREAMS_DECL extern const int buf_error;
0081 
0082                     // Flush codes
0083 
0084 BOOST_IOSTREAMS_DECL extern const int finish;
0085 BOOST_IOSTREAMS_DECL extern const int no_flush;
0086 BOOST_IOSTREAMS_DECL extern const int sync_flush;
0087 
0088                     // Code for current OS
0089 
0090 //BOOST_IOSTREAMS_DECL extern const int os_code;
0091 
0092                     // Null pointer constant.
0093 
0094 const int null                               = 0;
0095 
0096                     // Default values
0097 
0098 const int default_window_bits                = 15;
0099 const int default_mem_level                  = 8;
0100 const bool default_crc                       = false;
0101 const bool default_noheader                  = false;
0102 
0103 } // End namespace zlib. 
0104 
0105 //
0106 // Class name: zlib_params.
0107 // Description: Encapsulates the parameters passed to deflateInit2
0108 //      and inflateInit2 to customize compression and decompression.
0109 //
0110 struct zlib_params {
0111 
0112     // Non-explicit constructor.
0113     zlib_params( int level_          = zlib::default_compression,
0114                  int method_         = zlib::deflated,
0115                  int window_bits_    = zlib::default_window_bits, 
0116                  int mem_level_      = zlib::default_mem_level, 
0117                  int strategy_       = zlib::default_strategy,
0118                  bool noheader_      = zlib::default_noheader,
0119                  bool calculate_crc_ = zlib::default_crc )
0120         : level(level_), method(method_), window_bits(window_bits_),
0121           mem_level(mem_level_), strategy(strategy_),  
0122           noheader(noheader_), calculate_crc(calculate_crc_)
0123         { }
0124     int level;
0125     int method;
0126     int window_bits;
0127     int mem_level;
0128     int strategy;
0129     bool noheader;
0130     bool calculate_crc;
0131 };
0132 
0133 //
0134 // Class name: zlib_error.
0135 // Description: Subclass of std::ios::failure thrown to indicate
0136 //     zlib errors other than out-of-memory conditions.
0137 //
0138 class BOOST_IOSTREAMS_DECL zlib_error : public BOOST_IOSTREAMS_FAILURE {
0139 public:
0140     explicit zlib_error(int error);
0141     int error() const { return error_; }
0142     static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
0143 private:
0144     int error_;
0145 };
0146 
0147 namespace detail {
0148 
0149 template<typename Alloc>
0150 struct zlib_allocator_traits {
0151 #ifndef BOOST_NO_STD_ALLOCATOR
0152 #if defined(BOOST_NO_CXX11_ALLOCATOR)
0153     typedef typename Alloc::template rebind<char>::other type;
0154 #else
0155     typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
0156 #endif
0157 #else
0158     typedef std::allocator<char> type;
0159 #endif
0160 };
0161 
0162 template< typename Alloc,
0163           typename Base = // VC6 workaround (C2516)
0164               BOOST_DEDUCED_TYPENAME zlib_allocator_traits<Alloc>::type >
0165 struct zlib_allocator : private Base {
0166 private:
0167 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
0168     typedef typename Base::size_type size_type;
0169 #else
0170     typedef typename std::allocator_traits<Base>::size_type size_type;
0171 #endif
0172 public:
0173     BOOST_STATIC_CONSTANT(bool, custom = 
0174         (!is_same<std::allocator<char>, Base>::value));
0175     typedef typename zlib_allocator_traits<Alloc>::type allocator_type;
0176     static void* allocate(void* self, zlib::uint items, zlib::uint size);
0177     static void deallocate(void* self, void* address);
0178 };
0179 
0180 class BOOST_IOSTREAMS_DECL zlib_base { 
0181 public:
0182     typedef char char_type;
0183 protected:
0184     zlib_base();
0185     ~zlib_base();
0186     void* stream() { return stream_; }
0187     template<typename Alloc> 
0188     void init( const zlib_params& p, 
0189                bool compress,
0190                zlib_allocator<Alloc>& zalloc )
0191         {
0192             bool custom = zlib_allocator<Alloc>::custom;
0193             do_init( p, compress,
0194                      custom ? zlib_allocator<Alloc>::allocate : 0,
0195                      custom ? zlib_allocator<Alloc>::deallocate : 0,
0196                      &zalloc );
0197         }
0198     void before( const char*& src_begin, const char* src_end,
0199                  char*& dest_begin, char* dest_end );
0200     void after( const char*& src_begin, char*& dest_begin, 
0201                 bool compress );
0202     int xdeflate(int flush);  // Prefix 'x' prevents symbols from being 
0203     int xinflate(int flush);  // redefined when Z_PREFIX is defined
0204     void reset(bool compress, bool realloc);
0205 public:
0206     zlib::ulong crc() const { return crc_; }
0207     int total_in() const { return total_in_; }
0208     int total_out() const { return total_out_; }
0209 private:
0210     void do_init( const zlib_params& p, bool compress, 
0211                   zlib::xalloc_func,
0212                   zlib::xfree_func, 
0213                   void* derived );
0214     void*        stream_;         // Actual type: z_stream*.
0215     bool         calculate_crc_;
0216     zlib::ulong  crc_;
0217     zlib::ulong  crc_imp_;
0218     int          total_in_;
0219     int          total_out_;
0220 };
0221 
0222 //
0223 // Template name: zlib_compressor_impl
0224 // Description: Model of C-Style Filte implementing compression by
0225 //      delegating to the zlib function deflate.
0226 //
0227 template<typename Alloc = std::allocator<char> >
0228 class zlib_compressor_impl : public zlib_base, public zlib_allocator<Alloc> { 
0229 public: 
0230     zlib_compressor_impl(const zlib_params& = zlib::default_compression);
0231     ~zlib_compressor_impl();
0232     bool filter( const char*& src_begin, const char* src_end,
0233                  char*& dest_begin, char* dest_end, bool flush );
0234     void close();
0235 };
0236 
0237 //
0238 // Template name: zlib_compressor
0239 // Description: Model of C-Style Filte implementing decompression by
0240 //      delegating to the zlib function inflate.
0241 //
0242 template<typename Alloc = std::allocator<char> >
0243 class zlib_decompressor_impl : public zlib_base, public zlib_allocator<Alloc> {
0244 public:
0245     zlib_decompressor_impl(const zlib_params&);
0246     zlib_decompressor_impl(int window_bits = zlib::default_window_bits);
0247     ~zlib_decompressor_impl();
0248     bool filter( const char*& begin_in, const char* end_in,
0249                  char*& begin_out, char* end_out, bool flush );
0250     void close();
0251     bool eof() const { return eof_; }
0252 private:
0253     bool eof_;
0254 };
0255 
0256 } // End namespace detail.
0257 
0258 //
0259 // Template name: zlib_compressor
0260 // Description: Model of InputFilter and OutputFilter implementing
0261 //      compression using zlib.
0262 //
0263 template<typename Alloc = std::allocator<char> >
0264 struct basic_zlib_compressor 
0265     : symmetric_filter<detail::zlib_compressor_impl<Alloc>, Alloc> 
0266 {
0267 private:
0268     typedef detail::zlib_compressor_impl<Alloc>         impl_type;
0269     typedef symmetric_filter<impl_type, Alloc>  base_type;
0270 public:
0271     typedef typename base_type::char_type               char_type;
0272     typedef typename base_type::category                category;
0273     basic_zlib_compressor( const zlib_params& = zlib::default_compression, 
0274                            std::streamsize buffer_size = default_device_buffer_size );
0275     zlib::ulong crc() { return this->filter().crc(); }
0276     int total_in() {  return this->filter().total_in(); }
0277 };
0278 BOOST_IOSTREAMS_PIPABLE(basic_zlib_compressor, 1)
0279 
0280 typedef basic_zlib_compressor<> zlib_compressor;
0281 
0282 //
0283 // Template name: zlib_decompressor
0284 // Description: Model of InputFilter and OutputFilter implementing
0285 //      decompression using zlib.
0286 //
0287 template<typename Alloc = std::allocator<char> >
0288 struct basic_zlib_decompressor 
0289     : symmetric_filter<detail::zlib_decompressor_impl<Alloc>, Alloc> 
0290 {
0291 private:
0292     typedef detail::zlib_decompressor_impl<Alloc>       impl_type;
0293     typedef symmetric_filter<impl_type, Alloc>  base_type;
0294 public:
0295     typedef typename base_type::char_type               char_type;
0296     typedef typename base_type::category                category;
0297     basic_zlib_decompressor( int window_bits = zlib::default_window_bits,
0298                              std::streamsize buffer_size = default_device_buffer_size );
0299     basic_zlib_decompressor( const zlib_params& p,
0300                              std::streamsize buffer_size = default_device_buffer_size );
0301     zlib::ulong crc() { return this->filter().crc(); }
0302     int total_out() {  return this->filter().total_out(); }
0303     bool eof() { return this->filter().eof(); }
0304 };
0305 BOOST_IOSTREAMS_PIPABLE(basic_zlib_decompressor, 1)
0306 
0307 typedef basic_zlib_decompressor<> zlib_decompressor;
0308 
0309 //----------------------------------------------------------------------------//
0310 
0311 //------------------Implementation of zlib_allocator--------------------------//
0312 
0313 namespace detail {
0314 
0315 template<typename Alloc, typename Base>
0316 void* zlib_allocator<Alloc, Base>::allocate
0317     (void* self, zlib::uint items, zlib::uint size)
0318 { 
0319     size_type len = items * size;
0320     char* ptr = 
0321         static_cast<allocator_type*>(self)->allocate
0322             (len + sizeof(size_type)
0323             #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
0324                 , (char*)0
0325             #endif
0326             );
0327     *reinterpret_cast<size_type*>(ptr) = len;
0328     return ptr + sizeof(size_type);
0329 }
0330 
0331 template<typename Alloc, typename Base>
0332 void zlib_allocator<Alloc, Base>::deallocate(void* self, void* address)
0333 { 
0334     char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
0335     size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
0336     static_cast<allocator_type*>(self)->deallocate(ptr, len); 
0337 }
0338 
0339 //------------------Implementation of zlib_compressor_impl--------------------//
0340 
0341 template<typename Alloc>
0342 zlib_compressor_impl<Alloc>::zlib_compressor_impl(const zlib_params& p)
0343 { init(p, true, static_cast<zlib_allocator<Alloc>&>(*this)); }
0344 
0345 template<typename Alloc>
0346 zlib_compressor_impl<Alloc>::~zlib_compressor_impl()
0347 { reset(true, false); }
0348 
0349 template<typename Alloc>
0350 bool zlib_compressor_impl<Alloc>::filter
0351     ( const char*& src_begin, const char* src_end,
0352       char*& dest_begin, char* dest_end, bool flush )
0353 {
0354     before(src_begin, src_end, dest_begin, dest_end);
0355     int result = xdeflate(flush ? zlib::finish : zlib::no_flush);
0356     after(src_begin, dest_begin, true);
0357     zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
0358     return result != zlib::stream_end;
0359 }
0360 
0361 template<typename Alloc>
0362 void zlib_compressor_impl<Alloc>::close() { reset(true, true); }
0363 
0364 //------------------Implementation of zlib_decompressor_impl------------------//
0365 
0366 template<typename Alloc>
0367 zlib_decompressor_impl<Alloc>::zlib_decompressor_impl(const zlib_params& p)
0368   : eof_(false)
0369 { init(p, false, static_cast<zlib_allocator<Alloc>&>(*this)); }
0370 
0371 template<typename Alloc>
0372 zlib_decompressor_impl<Alloc>::~zlib_decompressor_impl()
0373 { reset(false, false); }
0374 
0375 template<typename Alloc>
0376 zlib_decompressor_impl<Alloc>::zlib_decompressor_impl(int window_bits)
0377 { 
0378     zlib_params p;
0379     p.window_bits = window_bits;
0380     init(p, false, static_cast<zlib_allocator<Alloc>&>(*this)); 
0381 }
0382 
0383 template<typename Alloc>
0384 bool zlib_decompressor_impl<Alloc>::filter
0385     ( const char*& src_begin, const char* src_end,
0386       char*& dest_begin, char* dest_end, bool /* flush */ )
0387 {
0388     before(src_begin, src_end, dest_begin, dest_end);
0389     int result = xinflate(zlib::sync_flush);
0390     after(src_begin, dest_begin, false);
0391     zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
0392     eof_ = result == zlib::stream_end;
0393     return !eof_;
0394 }
0395 
0396 template<typename Alloc>
0397 void zlib_decompressor_impl<Alloc>::close() {
0398     eof_ = false;
0399     reset(false, true);
0400 }
0401 
0402 } // End namespace detail.
0403 
0404 //------------------Implementation of zlib_decompressor-----------------------//
0405 
0406 template<typename Alloc>
0407 basic_zlib_compressor<Alloc>::basic_zlib_compressor
0408     (const zlib_params& p, std::streamsize buffer_size) 
0409     : base_type(buffer_size, p) { }
0410 
0411 //------------------Implementation of zlib_decompressor-----------------------//
0412 
0413 template<typename Alloc>
0414 basic_zlib_decompressor<Alloc>::basic_zlib_decompressor
0415     (int window_bits, std::streamsize buffer_size) 
0416     : base_type(buffer_size, window_bits) { }
0417 
0418 template<typename Alloc>
0419 basic_zlib_decompressor<Alloc>::basic_zlib_decompressor
0420     (const zlib_params& p, std::streamsize buffer_size) 
0421     : base_type(buffer_size, p) { }
0422 
0423 //----------------------------------------------------------------------------//
0424 
0425 } } // End namespaces iostreams, boost.
0426 
0427 #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
0428 #ifdef BOOST_MSVC
0429 # pragma warning(pop)
0430 #endif
0431 
0432 #endif // #ifndef BOOST_IOSTREAMS_ZLIB_HPP_INCLUDED