// (C) Copyright Reimar Döffinger 2018. // Based on zstd.hpp by: // (C) Copyright Milan Svoboda 2008. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) // See http://www.boost.org/libs/iostreams for documentation. #ifndef BOOST_IOSTREAMS_ZSTD_HPP_INCLUDED #define BOOST_IOSTREAMS_ZSTD_HPP_INCLUDED #if defined(_MSC_VER) # pragma once #endif #include #include // streamsize. #include // allocator, bad_alloc. #include #include // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM. #include #include // buffer size. #include #include #include #include // failure, streamsize. #include #include #include // Must come last. #ifdef BOOST_MSVC # pragma warning(push) # pragma warning(disable:4251 4231 4660) // Dependencies not exported. #endif #include namespace boost { namespace iostreams { namespace zstd { typedef void* (*alloc_func)(void*, size_t, size_t); typedef void (*free_func)(void*, void*); // Compression levels BOOST_IOSTREAMS_DECL extern const uint32_t best_speed; BOOST_IOSTREAMS_DECL extern const uint32_t best_compression; BOOST_IOSTREAMS_DECL extern const uint32_t default_compression; // Status codes BOOST_IOSTREAMS_DECL extern const int okay; BOOST_IOSTREAMS_DECL extern const int stream_end; // Flush codes BOOST_IOSTREAMS_DECL extern const int finish; BOOST_IOSTREAMS_DECL extern const int flush; BOOST_IOSTREAMS_DECL extern const int run; // Code for current OS // Null pointer constant. const int null = 0; // Default values } // End namespace zstd. // // Class name: zstd_params. // Description: Encapsulates the parameters passed to zstddec_init // to customize compression and decompression. // struct zstd_params { // Non-explicit constructor. zstd_params( uint32_t level = zstd::default_compression ) : level(level) { } uint32_t level; }; // // Class name: zstd_error. // Description: Subclass of std::ios::failure thrown to indicate // zstd errors other than out-of-memory conditions. // class BOOST_IOSTREAMS_DECL zstd_error : public BOOST_IOSTREAMS_FAILURE { public: explicit zstd_error(size_t error); int error() const { return error_; } static void check BOOST_PREVENT_MACRO_SUBSTITUTION(size_t error); private: size_t error_; }; namespace detail { template struct zstd_allocator_traits { #ifndef BOOST_NO_STD_ALLOCATOR #if defined(BOOST_NO_CXX11_ALLOCATOR) typedef typename Alloc::template rebind::other type; #else typedef typename std::allocator_traits::template rebind_alloc type; #endif #else typedef std::allocator type; #endif }; template< typename Alloc, typename Base = // VC6 workaround (C2516) BOOST_DEDUCED_TYPENAME zstd_allocator_traits::type > struct zstd_allocator : private Base { private: #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR) typedef typename Base::size_type size_type; #else typedef typename std::allocator_traits::size_type size_type; #endif public: BOOST_STATIC_CONSTANT(bool, custom = (!is_same, Base>::value)); typedef typename zstd_allocator_traits::type allocator_type; static void* allocate(void* self, size_t items, size_t size); static void deallocate(void* self, void* address); }; class BOOST_IOSTREAMS_DECL zstd_base { public: typedef char char_type; protected: zstd_base(); ~zstd_base(); template void init( const zstd_params& p, bool compress, zstd_allocator& zalloc ) { bool custom = zstd_allocator::custom; do_init( p, compress, custom ? zstd_allocator::allocate : 0, custom ? zstd_allocator::deallocate : 0, &zalloc ); } void before( const char*& src_begin, const char* src_end, char*& dest_begin, char* dest_end ); void after( const char*& src_begin, char*& dest_begin, bool compress ); int deflate(int action); int inflate(int action); void reset(bool compress, bool realloc); private: void do_init( const zstd_params& p, bool compress, zstd::alloc_func, zstd::free_func, void* derived ); void* cstream_; // Actual type: ZSTD_CStream * void* dstream_; // Actual type: ZSTD_DStream * void* in_; // Actual type: ZSTD_inBuffer * void* out_; // Actual type: ZSTD_outBuffer * int eof_; uint32_t level; }; // // Template name: zstd_compressor_impl // Description: Model of C-Style Filter implementing compression by // delegating to the zstd function deflate. // template > class zstd_compressor_impl : public zstd_base, public zstd_allocator { public: zstd_compressor_impl(const zstd_params& = zstd::default_compression); ~zstd_compressor_impl(); bool filter( const char*& src_begin, const char* src_end, char*& dest_begin, char* dest_end, bool flush ); void close(); }; // // Template name: zstd_compressor_impl // Description: Model of C-Style Filte implementing decompression by // delegating to the zstd function inflate. // template > class zstd_decompressor_impl : public zstd_base, public zstd_allocator { public: zstd_decompressor_impl(const zstd_params&); zstd_decompressor_impl(); ~zstd_decompressor_impl(); bool filter( const char*& begin_in, const char* end_in, char*& begin_out, char* end_out, bool flush ); void close(); }; } // End namespace detail. // // Template name: zstd_compressor // Description: Model of InputFilter and OutputFilter implementing // compression using zstd. // template > struct basic_zstd_compressor : symmetric_filter, Alloc> { private: typedef detail::zstd_compressor_impl impl_type; typedef symmetric_filter base_type; public: typedef typename base_type::char_type char_type; typedef typename base_type::category category; basic_zstd_compressor( const zstd_params& = zstd::default_compression, std::streamsize buffer_size = default_device_buffer_size ); }; BOOST_IOSTREAMS_PIPABLE(basic_zstd_compressor, 1) typedef basic_zstd_compressor<> zstd_compressor; // // Template name: zstd_decompressor // Description: Model of InputFilter and OutputFilter implementing // decompression using zstd. // template > struct basic_zstd_decompressor : symmetric_filter, Alloc> { private: typedef detail::zstd_decompressor_impl impl_type; typedef symmetric_filter base_type; public: typedef typename base_type::char_type char_type; typedef typename base_type::category category; basic_zstd_decompressor( std::streamsize buffer_size = default_device_buffer_size ); basic_zstd_decompressor( const zstd_params& p, std::streamsize buffer_size = default_device_buffer_size ); }; BOOST_IOSTREAMS_PIPABLE(basic_zstd_decompressor, 1) typedef basic_zstd_decompressor<> zstd_decompressor; //----------------------------------------------------------------------------// //------------------Implementation of zstd_allocator--------------------------// namespace detail { template void* zstd_allocator::allocate (void* self, size_t items, size_t size) { size_type len = items * size; char* ptr = static_cast(self)->allocate (len + sizeof(size_type) #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) , (char*)0 #endif ); *reinterpret_cast(ptr) = len; return ptr + sizeof(size_type); } template void zstd_allocator::deallocate(void* self, void* address) { char* ptr = reinterpret_cast(address) - sizeof(size_type); size_type len = *reinterpret_cast(ptr) + sizeof(size_type); static_cast(self)->deallocate(ptr, len); } //------------------Implementation of zstd_compressor_impl--------------------// template zstd_compressor_impl::zstd_compressor_impl(const zstd_params& p) { init(p, true, static_cast&>(*this)); } template zstd_compressor_impl::~zstd_compressor_impl() { reset(true, false); } template bool zstd_compressor_impl::filter ( const char*& src_begin, const char* src_end, char*& dest_begin, char* dest_end, bool flush ) { before(src_begin, src_end, dest_begin, dest_end); int result = deflate(flush ? zstd::finish : zstd::run); after(src_begin, dest_begin, true); return result != zstd::stream_end; } template void zstd_compressor_impl::close() { reset(true, true); } //------------------Implementation of zstd_decompressor_impl------------------// template zstd_decompressor_impl::zstd_decompressor_impl(const zstd_params& p) { init(p, false, static_cast&>(*this)); } template zstd_decompressor_impl::~zstd_decompressor_impl() { reset(false, false); } template zstd_decompressor_impl::zstd_decompressor_impl() { zstd_params p; init(p, false, static_cast&>(*this)); } template bool zstd_decompressor_impl::filter ( const char*& src_begin, const char* src_end, char*& dest_begin, char* dest_end, bool flush ) { before(src_begin, src_end, dest_begin, dest_end); int result = inflate(flush ? zstd::finish : zstd::run); after(src_begin, dest_begin, false); return result != zstd::stream_end; } template void zstd_decompressor_impl::close() { reset(false, true); } } // End namespace detail. //------------------Implementation of zstd_compressor-----------------------// template basic_zstd_compressor::basic_zstd_compressor (const zstd_params& p, std::streamsize buffer_size) : base_type(buffer_size, p) { } //------------------Implementation of zstd_decompressor-----------------------// template basic_zstd_decompressor::basic_zstd_decompressor (std::streamsize buffer_size) : base_type(buffer_size) { } template basic_zstd_decompressor::basic_zstd_decompressor (const zstd_params& p, std::streamsize buffer_size) : base_type(buffer_size, p) { } //----------------------------------------------------------------------------// } } // End namespaces iostreams, boost. #include // Pops abi_suffix.hpp pragmas. #ifdef BOOST_MSVC # pragma warning(pop) #endif #endif // #ifndef BOOST_IOSTREAMS_ZSTD_HPP_INCLUDED