Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:36

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 // This is a derivative work based on Zlib, copyright below:
0010 /*
0011     Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
0012 
0013     This software is provided 'as-is', without any express or implied
0014     warranty.  In no event will the authors be held liable for any damages
0015     arising from the use of this software.
0016 
0017     Permission is granted to anyone to use this software for any purpose,
0018     including commercial applications, and to alter it and redistribute it
0019     freely, subject to the following restrictions:
0020 
0021     1. The origin of this software must not be misrepresented; you must not
0022        claim that you wrote the original software. If you use this software
0023        in a product, an acknowledgment in the product documentation would be
0024        appreciated but is not required.
0025     2. Altered source versions must be plainly marked as such, and must not be
0026        misrepresented as being the original software.
0027     3. This notice may not be removed or altered from any source distribution.
0028 
0029     Jean-loup Gailly        Mark Adler
0030     jloup@gzip.org          madler@alumni.caltech.edu
0031 
0032     The data format used by the zlib library is described by RFCs (Request for
0033     Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
0034     (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
0035 */
0036 
0037 #ifndef BOOST_BEAST_ZLIB_DETAIL_WINDOW_HPP
0038 #define BOOST_BEAST_ZLIB_DETAIL_WINDOW_HPP
0039 
0040 #include <boost/assert.hpp>
0041 #include <boost/make_unique.hpp>
0042 #include <cstdint>
0043 #include <cstring>
0044 #include <memory>
0045 
0046 namespace boost {
0047 namespace beast {
0048 namespace zlib {
0049 namespace detail {
0050 
0051 class window
0052 {
0053     std::unique_ptr<std::uint8_t[]> p_;
0054     std::uint16_t i_ = 0;
0055     std::uint16_t size_ = 0;
0056     std::uint16_t capacity_ = 0;
0057     std::uint8_t bits_ = 0;
0058 
0059 public:
0060     int
0061     bits() const
0062     {
0063         return bits_;
0064     }
0065 
0066     unsigned
0067     capacity() const
0068     {
0069         return capacity_;
0070     }
0071 
0072     unsigned
0073     size() const
0074     {
0075         return size_;
0076     }
0077 
0078     void
0079     reset(int bits)
0080     {
0081         if(bits_ != bits)
0082         {
0083             p_.reset();
0084             bits_ = static_cast<std::uint8_t>(bits);
0085             capacity_ = 1U << bits_;
0086         }
0087         i_ = 0;
0088         size_ = 0;
0089     }
0090 
0091     void
0092     read(std::uint8_t* out, std::size_t pos, std::size_t n)
0093     {
0094         if(i_ >= size_)
0095         {
0096             // window is contiguous
0097             std::memcpy(out, &p_[i_ - pos], n);
0098             return;
0099         }
0100         auto i = ((i_ - pos) + capacity_) % capacity_;
0101         auto m = capacity_ - i;
0102         if(n <= m)
0103         {
0104             std::memcpy(out, &p_[i], n);
0105             return;
0106         }
0107         std::memcpy(out, &p_[i], m);
0108         out += m;
0109         std::memcpy(out, &p_[0], n - m);
0110     }
0111 
0112     void
0113     write(std::uint8_t const* in, std::size_t n)
0114     {
0115         if(! p_)
0116             p_ = boost::make_unique<
0117                 std::uint8_t[]>(capacity_);
0118         if(n >= capacity_)
0119         {
0120             i_ = 0;
0121             size_ = capacity_;
0122             std::memcpy(&p_[0], in + (n - size_), size_);
0123             return;
0124         }
0125         if(i_ + n <= capacity_)
0126         {
0127             std::memcpy(&p_[i_], in, n);
0128             if(size_ >= capacity_ - n)
0129                 size_ = capacity_;
0130             else
0131                 size_ = static_cast<std::uint16_t>(size_ + n);
0132 
0133             i_ = static_cast<std::uint16_t>(
0134                 (i_ + n) % capacity_);
0135             return;
0136         }
0137         auto m = capacity_ - i_;
0138         std::memcpy(&p_[i_], in, m);
0139         in += m;
0140         i_ = static_cast<std::uint16_t>(n - m);
0141         std::memcpy(&p_[0], in, i_);
0142         size_ = capacity_;
0143     }
0144 };
0145 
0146 } // detail
0147 } // zlib
0148 } // beast
0149 } // boost
0150 
0151 #endif