File indexing completed on 2025-01-18 09:29:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
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
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 }
0147 }
0148 }
0149 }
0150
0151 #endif