Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // (C) Copyright Milan Svoboda 2008.
0002 // Originally developed under the fusecompress project.
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 lzma_base::do_init.
0010 
0011 #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
0012 #define BOOST_IOSTREAMS_LZMA_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/detail/workaround.hpp>
0024 #include <boost/iostreams/constants.hpp>   // buffer size.
0025 #include <boost/iostreams/detail/config/auto_link.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)         // Dependencies not exported.
0037 #endif
0038 #include <boost/config/abi_prefix.hpp>
0039 
0040 namespace boost { namespace iostreams {
0041 
0042 namespace lzma {
0043 
0044 typedef void* (*alloc_func)(void*, size_t, size_t);
0045 typedef void (*free_func)(void*, void*);
0046 
0047                     // Compression levels
0048 
0049 BOOST_IOSTREAMS_DECL extern const uint32_t no_compression;
0050 BOOST_IOSTREAMS_DECL extern const uint32_t best_speed;
0051 BOOST_IOSTREAMS_DECL extern const uint32_t best_compression;
0052 BOOST_IOSTREAMS_DECL extern const uint32_t default_compression;
0053 
0054                     // Status codes
0055 
0056 BOOST_IOSTREAMS_DECL extern const int okay;
0057 BOOST_IOSTREAMS_DECL extern const int stream_end;
0058 BOOST_IOSTREAMS_DECL extern const int unsupported_check;
0059 BOOST_IOSTREAMS_DECL extern const int mem_error;
0060 BOOST_IOSTREAMS_DECL extern const int options_error;
0061 BOOST_IOSTREAMS_DECL extern const int data_error;
0062 BOOST_IOSTREAMS_DECL extern const int buf_error;
0063 BOOST_IOSTREAMS_DECL extern const int prog_error;
0064 
0065                     // Flush codes
0066 
0067 BOOST_IOSTREAMS_DECL extern const int finish;
0068 BOOST_IOSTREAMS_DECL extern const int full_flush;
0069 BOOST_IOSTREAMS_DECL extern const int sync_flush;
0070 BOOST_IOSTREAMS_DECL extern const int run;
0071 
0072                     // Code for current OS
0073 
0074                     // Null pointer constant.
0075 
0076 const int null                               = 0;
0077 
0078                     // Default values
0079 
0080 } // End namespace lzma.
0081 
0082 //
0083 // Class name: lzma_params.
0084 // Description: Encapsulates the parameters passed to lzmadec_init
0085 //      to customize compression and decompression.
0086 //
0087 struct lzma_params {
0088 
0089     // Non-explicit constructor.
0090     lzma_params( uint32_t level = lzma::default_compression, uint32_t threads = 1 )
0091         : level(level)
0092         , threads(threads)
0093         { }
0094     uint32_t level;
0095     uint32_t threads;
0096 };
0097 
0098 //
0099 // Class name: lzma_error.
0100 // Description: Subclass of std::ios::failure thrown to indicate
0101 //     lzma errors other than out-of-memory conditions.
0102 //
0103 class BOOST_IOSTREAMS_DECL lzma_error : public BOOST_IOSTREAMS_FAILURE {
0104 public:
0105     explicit lzma_error(int error);
0106     int error() const { return error_; }
0107     static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
0108 private:
0109     int error_;
0110 };
0111 
0112 namespace detail {
0113 
0114 template<typename Alloc>
0115 struct lzma_allocator_traits {
0116 #ifndef BOOST_NO_STD_ALLOCATOR
0117 #if defined(BOOST_NO_CXX11_ALLOCATOR)
0118     typedef typename Alloc::template rebind<char>::other type;
0119 #else
0120     typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
0121 #endif
0122 #else
0123     typedef std::allocator<char> type;
0124 #endif
0125 };
0126 
0127 template< typename Alloc,
0128           typename Base = // VC6 workaround (C2516)
0129               BOOST_DEDUCED_TYPENAME lzma_allocator_traits<Alloc>::type >
0130 struct lzma_allocator : private Base {
0131 private:
0132 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
0133     typedef typename Base::size_type size_type;
0134 #else
0135     typedef typename std::allocator_traits<Base>::size_type size_type;
0136 #endif
0137 public:
0138     BOOST_STATIC_CONSTANT(bool, custom =
0139         (!is_same<std::allocator<char>, Base>::value));
0140     typedef typename lzma_allocator_traits<Alloc>::type allocator_type;
0141     static void* allocate(void* self, size_t items, size_t size);
0142     static void deallocate(void* self, void* address);
0143 };
0144 
0145 class BOOST_IOSTREAMS_DECL lzma_base {
0146 public:
0147     typedef char char_type;
0148 protected:
0149     lzma_base();
0150     ~lzma_base();
0151     void* stream() { return stream_; }
0152     template<typename Alloc>
0153     void init( const lzma_params& p,
0154                bool compress,
0155                lzma_allocator<Alloc>& zalloc )
0156         {
0157             bool custom = lzma_allocator<Alloc>::custom;
0158             do_init( p, compress,
0159                      custom ? lzma_allocator<Alloc>::allocate : 0,
0160                      custom ? lzma_allocator<Alloc>::deallocate : 0,
0161                      &zalloc );
0162         }
0163     void before( const char*& src_begin, const char* src_end,
0164                  char*& dest_begin, char* dest_end );
0165     void after( const char*& src_begin, char*& dest_begin,
0166                 bool compress );
0167     int deflate(int action);
0168     int inflate(int action);
0169     void reset(bool compress, bool realloc);
0170 private:
0171     void do_init( const lzma_params& p, bool compress,
0172                   lzma::alloc_func,
0173                   lzma::free_func,
0174                   void* derived );
0175     void init_stream(bool compress);
0176     void*    stream_;         // Actual type: lzma_stream*.
0177     uint32_t level_;
0178     uint32_t threads_;
0179 };
0180 
0181 //
0182 // Template name: lzma_compressor_impl
0183 // Description: Model of C-Style Filter implementing compression by
0184 //      delegating to the lzma function deflate.
0185 //
0186 template<typename Alloc = std::allocator<char> >
0187 class lzma_compressor_impl : public lzma_base, public lzma_allocator<Alloc> {
0188 public:
0189     lzma_compressor_impl(const lzma_params& = lzma_params());
0190     ~lzma_compressor_impl();
0191     bool filter( const char*& src_begin, const char* src_end,
0192                  char*& dest_begin, char* dest_end, bool flush );
0193     void close();
0194 };
0195 
0196 //
0197 // Template name: lzma_compressor_impl
0198 // Description: Model of C-Style Filte implementing decompression by
0199 //      delegating to the lzma function inflate.
0200 //
0201 template<typename Alloc = std::allocator<char> >
0202 class lzma_decompressor_impl : public lzma_base, public lzma_allocator<Alloc> {
0203 public:
0204     lzma_decompressor_impl(const lzma_params&);
0205     lzma_decompressor_impl();
0206     ~lzma_decompressor_impl();
0207     bool filter( const char*& begin_in, const char* end_in,
0208                  char*& begin_out, char* end_out, bool flush );
0209     void close();
0210 };
0211 
0212 } // End namespace detail.
0213 
0214 //
0215 // Template name: lzma_compressor
0216 // Description: Model of InputFilter and OutputFilter implementing
0217 //      compression using lzma.
0218 //
0219 template<typename Alloc = std::allocator<char> >
0220 struct basic_lzma_compressor
0221     : symmetric_filter<detail::lzma_compressor_impl<Alloc>, Alloc>
0222 {
0223 private:
0224     typedef detail::lzma_compressor_impl<Alloc> impl_type;
0225     typedef symmetric_filter<impl_type, Alloc>  base_type;
0226 public:
0227     typedef typename base_type::char_type               char_type;
0228     typedef typename base_type::category                category;
0229     basic_lzma_compressor( const lzma_params& = lzma_params(),
0230                            std::streamsize buffer_size = default_device_buffer_size );
0231 };
0232 BOOST_IOSTREAMS_PIPABLE(basic_lzma_compressor, 1)
0233 
0234 typedef basic_lzma_compressor<> lzma_compressor;
0235 
0236 //
0237 // Template name: lzma_decompressor
0238 // Description: Model of InputFilter and OutputFilter implementing
0239 //      decompression using lzma.
0240 //
0241 template<typename Alloc = std::allocator<char> >
0242 struct basic_lzma_decompressor
0243     : symmetric_filter<detail::lzma_decompressor_impl<Alloc>, Alloc>
0244 {
0245 private:
0246     typedef detail::lzma_decompressor_impl<Alloc> impl_type;
0247     typedef symmetric_filter<impl_type, Alloc>    base_type;
0248 public:
0249     typedef typename base_type::char_type               char_type;
0250     typedef typename base_type::category                category;
0251     basic_lzma_decompressor( std::streamsize buffer_size = default_device_buffer_size );
0252     basic_lzma_decompressor( const lzma_params& p,
0253                              std::streamsize buffer_size = default_device_buffer_size );
0254 };
0255 BOOST_IOSTREAMS_PIPABLE(basic_lzma_decompressor, 1)
0256 
0257 typedef basic_lzma_decompressor<> lzma_decompressor;
0258 
0259 //----------------------------------------------------------------------------//
0260 
0261 //------------------Implementation of lzma_allocator--------------------------//
0262 
0263 namespace detail {
0264 
0265 template<typename Alloc, typename Base>
0266 void* lzma_allocator<Alloc, Base>::allocate
0267     (void* self, size_t items, size_t size)
0268 {
0269     size_type len = items * size;
0270     char* ptr =
0271         static_cast<allocator_type*>(self)->allocate
0272             (len + sizeof(size_type)
0273             #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
0274                 , (char*)0
0275             #endif
0276             );
0277     *reinterpret_cast<size_type*>(ptr) = len;
0278     return ptr + sizeof(size_type);
0279 }
0280 
0281 template<typename Alloc, typename Base>
0282 void lzma_allocator<Alloc, Base>::deallocate(void* self, void* address)
0283 {
0284     char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
0285     size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
0286     static_cast<allocator_type*>(self)->deallocate(ptr, len);
0287 }
0288 
0289 //------------------Implementation of lzma_compressor_impl--------------------//
0290 
0291 template<typename Alloc>
0292 lzma_compressor_impl<Alloc>::lzma_compressor_impl(const lzma_params& p)
0293 { init(p, true, static_cast<lzma_allocator<Alloc>&>(*this)); }
0294 
0295 template<typename Alloc>
0296 lzma_compressor_impl<Alloc>::~lzma_compressor_impl()
0297 { reset(true, false); }
0298 
0299 template<typename Alloc>
0300 bool lzma_compressor_impl<Alloc>::filter
0301     ( const char*& src_begin, const char* src_end,
0302       char*& dest_begin, char* dest_end, bool flush )
0303 {
0304     before(src_begin, src_end, dest_begin, dest_end);
0305     int result = deflate(flush ? lzma::finish : lzma::run);
0306     after(src_begin, dest_begin, true);
0307     lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
0308     return result != lzma::stream_end;
0309 }
0310 
0311 template<typename Alloc>
0312 void lzma_compressor_impl<Alloc>::close() { reset(true, true); }
0313 
0314 //------------------Implementation of lzma_decompressor_impl------------------//
0315 
0316 template<typename Alloc>
0317 lzma_decompressor_impl<Alloc>::lzma_decompressor_impl(const lzma_params& p)
0318 { init(p, false, static_cast<lzma_allocator<Alloc>&>(*this)); }
0319 
0320 template<typename Alloc>
0321 lzma_decompressor_impl<Alloc>::~lzma_decompressor_impl()
0322 { reset(false, false); }
0323 
0324 template<typename Alloc>
0325 lzma_decompressor_impl<Alloc>::lzma_decompressor_impl()
0326 {
0327     lzma_params p;
0328     init(p, false, static_cast<lzma_allocator<Alloc>&>(*this));
0329 }
0330 
0331 template<typename Alloc>
0332 bool lzma_decompressor_impl<Alloc>::filter
0333     ( const char*& src_begin, const char* src_end,
0334       char*& dest_begin, char* dest_end, bool flush )
0335 {
0336     before(src_begin, src_end, dest_begin, dest_end);
0337     int result = inflate(flush ? lzma::finish : lzma::run);
0338     after(src_begin, dest_begin, false);
0339     lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
0340     return result != lzma::stream_end;
0341 }
0342 
0343 template<typename Alloc>
0344 void lzma_decompressor_impl<Alloc>::close() { reset(false, true); }
0345 
0346 } // End namespace detail.
0347 
0348 //------------------Implementation of lzma_compressor-----------------------//
0349 
0350 template<typename Alloc>
0351 basic_lzma_compressor<Alloc>::basic_lzma_compressor
0352     (const lzma_params& p, std::streamsize buffer_size)
0353     : base_type(buffer_size, p) { }
0354 
0355 //------------------Implementation of lzma_decompressor-----------------------//
0356 
0357 template<typename Alloc>
0358 basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
0359     (std::streamsize buffer_size)
0360     : base_type(buffer_size) { }
0361 
0362 template<typename Alloc>
0363 basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
0364     (const lzma_params& p, std::streamsize buffer_size)
0365     : base_type(buffer_size, p) { }
0366 
0367 //----------------------------------------------------------------------------//
0368 
0369 } } // End namespaces iostreams, boost.
0370 
0371 #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
0372 #ifdef BOOST_MSVC
0373 # pragma warning(pop)
0374 #endif
0375 
0376 #endif // #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED