| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | // This is a derivative work based on Zlib, copyright below: |
| 10 | /* |
| 11 | Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler |
| 12 | |
| 13 | This software is provided 'as-is', without any express or implied |
| 14 | warranty. In no event will the authors be held liable for any damages |
| 15 | arising from the use of this software. |
| 16 | |
| 17 | Permission is granted to anyone to use this software for any purpose, |
| 18 | including commercial applications, and to alter it and redistribute it |
| 19 | freely, subject to the following restrictions: |
| 20 | |
| 21 | 1. The origin of this software must not be misrepresented; you must not |
| 22 | claim that you wrote the original software. If you use this software |
| 23 | in a product, an acknowledgment in the product documentation would be |
| 24 | appreciated but is not required. |
| 25 | 2. Altered source versions must be plainly marked as such, and must not be |
| 26 | misrepresented as being the original software. |
| 27 | 3. This notice may not be removed or altered from any source distribution. |
| 28 | |
| 29 | Jean-loup Gailly Mark Adler |
| 30 | jloup@gzip.org madler@alumni.caltech.edu |
| 31 | |
| 32 | The data format used by the zlib library is described by RFCs (Request for |
| 33 | Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 |
| 34 | (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). |
| 35 | */ |
| 36 | |
| 37 | #ifndef BOOST_BEAST_ZLIB_DETAIL_WINDOW_HPP |
| 38 | #define BOOST_BEAST_ZLIB_DETAIL_WINDOW_HPP |
| 39 | |
| 40 | #include <boost/assert.hpp> |
| 41 | #include <boost/make_unique.hpp> |
| 42 | #include <cstdint> |
| 43 | #include <cstring> |
| 44 | #include <memory> |
| 45 | |
| 46 | namespace boost { |
| 47 | namespace beast { |
| 48 | namespace zlib { |
| 49 | namespace detail { |
| 50 | |
| 51 | class window |
| 52 | { |
| 53 | std::unique_ptr<std::uint8_t[]> p_; |
| 54 | std::uint16_t i_ = 0; |
| 55 | std::uint16_t size_ = 0; |
| 56 | std::uint16_t capacity_ = 0; |
| 57 | std::uint8_t bits_ = 0; |
| 58 | |
| 59 | public: |
| 60 | int |
| 61 | bits() const |
| 62 | { |
| 63 | return bits_; |
| 64 | } |
| 65 | |
| 66 | unsigned |
| 67 | capacity() const |
| 68 | { |
| 69 | return capacity_; |
| 70 | } |
| 71 | |
| 72 | unsigned |
| 73 | size() const |
| 74 | { |
| 75 | return size_; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | reset(int bits) |
| 80 | { |
| 81 | if(bits_ != bits) |
| 82 | { |
| 83 | p_.reset(); |
| 84 | bits_ = static_cast<std::uint8_t>(bits); |
| 85 | capacity_ = 1U << bits_; |
| 86 | } |
| 87 | i_ = 0; |
| 88 | size_ = 0; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | read(std::uint8_t* out, std::size_t pos, std::size_t n) |
| 93 | { |
| 94 | if(i_ >= size_) |
| 95 | { |
| 96 | // window is contiguous |
| 97 | std::memcpy(dest: out, src: &p_[i_ - pos], n: n); |
| 98 | return; |
| 99 | } |
| 100 | auto i = ((i_ - pos) + capacity_) % capacity_; |
| 101 | auto m = capacity_ - i; |
| 102 | if(n <= m) |
| 103 | { |
| 104 | std::memcpy(dest: out, src: &p_[i], n: n); |
| 105 | return; |
| 106 | } |
| 107 | std::memcpy(dest: out, src: &p_[i], n: m); |
| 108 | out += m; |
| 109 | std::memcpy(dest: out, src: &p_[0], n: n - m); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | write(std::uint8_t const* in, std::size_t n) |
| 114 | { |
| 115 | if(! p_) |
| 116 | p_ = boost::make_unique< |
| 117 | std::uint8_t[]>(size: capacity_); |
| 118 | if(n >= capacity_) |
| 119 | { |
| 120 | i_ = 0; |
| 121 | size_ = capacity_; |
| 122 | std::memcpy(dest: &p_[0], src: in + (n - size_), n: size_); |
| 123 | return; |
| 124 | } |
| 125 | if(i_ + n <= capacity_) |
| 126 | { |
| 127 | std::memcpy(dest: &p_[i_], src: in, n: n); |
| 128 | if(size_ >= capacity_ - n) |
| 129 | size_ = capacity_; |
| 130 | else |
| 131 | size_ = static_cast<std::uint16_t>(size_ + n); |
| 132 | |
| 133 | i_ = static_cast<std::uint16_t>( |
| 134 | (i_ + n) % capacity_); |
| 135 | return; |
| 136 | } |
| 137 | auto m = capacity_ - i_; |
| 138 | std::memcpy(dest: &p_[i_], src: in, n: m); |
| 139 | in += m; |
| 140 | i_ = static_cast<std::uint16_t>(n - m); |
| 141 | std::memcpy(dest: &p_[0], src: in, n: i_); |
| 142 | size_ = capacity_; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | } // detail |
| 147 | } // zlib |
| 148 | } // beast |
| 149 | } // boost |
| 150 | |
| 151 | #endif |
| 152 | |