123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- ====================BSL-1.0====================
- * 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.)
- ====================BSL-1.0====================
- * 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.)
- ====================BSL-1.0====================
- ### License
- Distributed under the [Boost Software License, Version 1.0](http://www.boost.org/LICENSE_1_0.txt).
- ====================BSL-1.0====================
- * Submit your patches as pull requests against **develop** branch. Note that by submitting patches you agree to license your modifications under the [Boost Software License, Version 1.0](http://www.boost.org/LICENSE_1_0.txt).
- ====================BSL-1.0====================
- // 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.)
- ====================BSL-1.0====================
- Boost Software License - Version 1.0 - August 17th, 2003
- Permission is hereby granted, free of charge, to any person or organization
- obtaining a copy of the software and accompanying documentation covered by
- this license (the "Software") to use, reproduce, display, distribute,
- execute, and transmit the Software, and to prepare derivative works of the
- Software, and to permit third-parties to whom the Software is furnished to
- do so, all subject to the following:
- The copyright notices in the Software and this entire statement, including
- the above license grant, this restriction and the following disclaimer,
- must be included in all copies of the Software, in whole or in part, and
- all derivative works of the Software, unless such copies or derivative
- works are solely in the form of machine-executable object code generated by
- a source language processor.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
- SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
- FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- DEALINGS IN THE SOFTWARE.
- ====================COPYRIGHT====================
- * Date: Sun Jan 06 12:57:30 MST 2008
- * Copyright: 2008 CodeRage, LLC
- 2004-2007 Jonathan Turkanis
- * Author: Jonathan Turkanis
- * Contact: turkanis at coderage dot com
- ====================COPYRIGHT====================
- * Date: Sun Jun 22 14:23:12 MDT 2008
- * Copyright: 2008 CodeRage, LLC
- * Author: Jonathan Turkanis
- * Contact: turkanis at coderage dot com
- ====================COPYRIGHT====================
- * Date: Thu Dec 06 13:21:54 MST 2007
- * Copyright: 2007-2008 CodeRage, LLC
- * Author: Jonathan Turkanis
- * Contact: turkanis at coderage dot com
- ====================COPYRIGHT====================
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2003-2007 Jonathan Turkanis
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2004-2007 Jonathan Turkanis
- // (C) Copyright 2002, 2003 Beman Dawes Boost.Filesystem
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2004-2007 Jonathan Turkanis
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2005-2007 Jonathan Turkanis
- // (C) Copyright David Abrahams 2004.
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2005-2007 Jonathan Turkanis
- // (C) Copyright Jonathan Graehl 2004.
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2005-2007 Jonathan Turkanis
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright 2010 Daniel James
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright Craig Henderson 2002 'boost/memmap.hpp' from sandbox
- // (C) Copyright Jonathan Turkanis 2004.
- // (C) Copyright Jonathan Graehl 2004.
- // (C) Copyright Jorge Lodos 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.)
- ====================COPYRIGHT====================
- // (C) Copyright Jorge Lodos 2008.
- // (C) Copyright Jonathan Turkanis 2003.
- // (C) Copyright Craig Henderson 2002. 'boost/memmap.hpp' from sandbox
- // 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.)
- ====================COPYRIGHT====================
- // (C) Copyright Milan Svoboda 2008.
- // Originally developed under the fusecompress project.
- // 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.)
- ====================COPYRIGHT====================
- // (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.)
- ====================COPYRIGHT====================
- // Inspired by fdstream.hpp, (C) Copyright Nicolai M. Josuttis 2001,
- // available at http://www.josuttis.com/cppcode/fdstream.html.
- ====================File: include/boost/iostreams/filter/zstd.hpp====================
- // (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 <cassert>
- #include <iosfwd> // streamsize.
- #include <memory> // allocator, bad_alloc.
- #include <new>
- #include <boost/config.hpp> // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM.
- #include <boost/detail/workaround.hpp>
- #include <boost/iostreams/constants.hpp> // buffer size.
- #include <boost/iostreams/detail/config/auto_link.hpp>
- #include <boost/iostreams/detail/config/dyn_link.hpp>
- #include <boost/iostreams/detail/config/wide_streams.hpp>
- #include <boost/iostreams/detail/ios.hpp> // failure, streamsize.
- #include <boost/iostreams/filter/symmetric.hpp>
- #include <boost/iostreams/pipeline.hpp>
- #include <boost/type_traits/is_same.hpp>
- // Must come last.
- #ifdef BOOST_MSVC
- # pragma warning(push)
- # pragma warning(disable:4251 4231 4660) // Dependencies not exported.
- #endif
- #include <boost/config/abi_prefix.hpp>
- 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<typename Alloc>
- struct zstd_allocator_traits {
- #ifndef BOOST_NO_STD_ALLOCATOR
- #if defined(BOOST_NO_CXX11_ALLOCATOR)
- typedef typename Alloc::template rebind<char>::other type;
- #else
- typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
- #endif
- #else
- typedef std::allocator<char> type;
- #endif
- };
- template< typename Alloc,
- typename Base = // VC6 workaround (C2516)
- BOOST_DEDUCED_TYPENAME zstd_allocator_traits<Alloc>::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<Base>::size_type size_type;
- #endif
- public:
- BOOST_STATIC_CONSTANT(bool, custom =
- (!is_same<std::allocator<char>, Base>::value));
- typedef typename zstd_allocator_traits<Alloc>::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<typename Alloc>
- void init( const zstd_params& p,
- bool compress,
- zstd_allocator<Alloc>& zalloc )
- {
- bool custom = zstd_allocator<Alloc>::custom;
- do_init( p, compress,
- custom ? zstd_allocator<Alloc>::allocate : 0,
- custom ? zstd_allocator<Alloc>::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<typename Alloc = std::allocator<char> >
- class zstd_compressor_impl : public zstd_base, public zstd_allocator<Alloc> {
- 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_decompressor_impl
- // Description: Model of C-Style Filter implementing decompression by
- // delegating to the zstd function inflate.
- //
- template<typename Alloc = std::allocator<char> >
- class zstd_decompressor_impl : public zstd_base, public zstd_allocator<Alloc> {
- 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<typename Alloc = std::allocator<char> >
- struct basic_zstd_compressor
- : symmetric_filter<detail::zstd_compressor_impl<Alloc>, Alloc>
- {
- private:
- typedef detail::zstd_compressor_impl<Alloc> impl_type;
- typedef symmetric_filter<impl_type, Alloc> 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<typename Alloc = std::allocator<char> >
- struct basic_zstd_decompressor
- : symmetric_filter<detail::zstd_decompressor_impl<Alloc>, Alloc>
- {
- private:
- typedef detail::zstd_decompressor_impl<Alloc> impl_type;
- typedef symmetric_filter<impl_type, Alloc> 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<typename Alloc, typename Base>
- void* zstd_allocator<Alloc, Base>::allocate
- (void* self, size_t items, size_t size)
- {
- size_type len = items * size;
- char* ptr =
- static_cast<allocator_type*>(self)->allocate
- (len + sizeof(size_type)
- #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
- , (char*)0
- #endif
- );
- *reinterpret_cast<size_type*>(ptr) = len;
- return ptr + sizeof(size_type);
- }
- template<typename Alloc, typename Base>
- void zstd_allocator<Alloc, Base>::deallocate(void* self, void* address)
- {
- char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
- size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
- static_cast<allocator_type*>(self)->deallocate(ptr, len);
- }
- //------------------Implementation of zstd_compressor_impl--------------------//
- template<typename Alloc>
- zstd_compressor_impl<Alloc>::zstd_compressor_impl(const zstd_params& p)
- { init(p, true, static_cast<zstd_allocator<Alloc>&>(*this)); }
- template<typename Alloc>
- zstd_compressor_impl<Alloc>::~zstd_compressor_impl()
- { reset(true, false); }
- template<typename Alloc>
- bool zstd_compressor_impl<Alloc>::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<typename Alloc>
- void zstd_compressor_impl<Alloc>::close() { reset(true, true); }
- //------------------Implementation of zstd_decompressor_impl------------------//
- template<typename Alloc>
- zstd_decompressor_impl<Alloc>::zstd_decompressor_impl(const zstd_params& p)
- { init(p, false, static_cast<zstd_allocator<Alloc>&>(*this)); }
- template<typename Alloc>
- zstd_decompressor_impl<Alloc>::~zstd_decompressor_impl()
- { reset(false, false); }
- template<typename Alloc>
- zstd_decompressor_impl<Alloc>::zstd_decompressor_impl()
- {
- zstd_params p;
- init(p, false, static_cast<zstd_allocator<Alloc>&>(*this));
- }
- template<typename Alloc>
- bool zstd_decompressor_impl<Alloc>::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<typename Alloc>
- void zstd_decompressor_impl<Alloc>::close() { reset(false, true); }
- } // End namespace detail.
- //------------------Implementation of zstd_compressor-----------------------//
- template<typename Alloc>
- basic_zstd_compressor<Alloc>::basic_zstd_compressor
- (const zstd_params& p, std::streamsize buffer_size)
- : base_type(buffer_size, p) { }
- //------------------Implementation of zstd_decompressor-----------------------//
- template<typename Alloc>
- basic_zstd_decompressor<Alloc>::basic_zstd_decompressor
- (std::streamsize buffer_size)
- : base_type(buffer_size) { }
- template<typename Alloc>
- basic_zstd_decompressor<Alloc>::basic_zstd_decompressor
- (const zstd_params& p, std::streamsize buffer_size)
- : base_type(buffer_size, p) { }
- //----------------------------------------------------------------------------//
- } } // End namespaces iostreams, boost.
- #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
- #ifdef BOOST_MSVC
- # pragma warning(pop)
- #endif
- #endif // #ifndef BOOST_IOSTREAMS_ZSTD_HPP_INCLUDED
|