2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // This is a derivative work based on Zlib, copyright below:
9 Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
11 This software is provided 'as-is', without any express or implied
12 warranty. In no event will the authors be held liable for any damages
13 arising from the use of this software.
15 Permission is granted to anyone to use this software for any purpose,
16 including commercial applications, and to alter it and redistribute it
17 freely, subject to the following restrictions:
19 1. The origin of this software must not be misrepresented; you must not
20 claim that you wrote the original software. If you use this software
21 in a product, an acknowledgment in the product documentation would be
22 appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and must not be
24 misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source distribution.
27 Jean-loup Gailly Mark Adler
28 jloup@gzip.org madler@alumni.caltech.edu
30 The data format used by the zlib library is described by RFCs (Request for
31 Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
32 (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
35 #ifndef BEAST_ZLIB_DETAIL_WINDOW_HPP
36 #define BEAST_ZLIB_DETAIL_WINDOW_HPP
38 #include <boost/assert.hpp>
49 std::unique_ptr<std::uint8_t[]> p_;
51 std::uint16_t size_ = 0;
52 std::uint16_t capacity_ = 0;
53 std::uint8_t bits_ = 0;
78 read(std::uint8_t* out, std::size_t pos, std::size_t n);
80 template<class = void>
82 write(std::uint8_t const* in, std::size_t n);
93 bits_ = static_cast<std::uint8_t>(bits);
94 capacity_ = 1U << bits_;
103 read(std::uint8_t* out, std::size_t pos, std::size_t n)
107 // window is contiguous
108 std::memcpy(out, &p_[i_ - pos], n);
111 auto i = ((i_ - pos) + capacity_) % capacity_;
112 auto m = capacity_ - i;
115 std::memcpy(out, &p_[i], n);
118 std::memcpy(out, &p_[i], m);
120 std::memcpy(out, &p_[0], n - m);
126 write(std::uint8_t const* in, std::size_t n)
129 p_.reset(new std::uint8_t[capacity_]);
134 std::memcpy(&p_[0], in + (n - size_), size_);
137 if(i_ + n <= capacity_)
139 std::memcpy(&p_[i_], in, n);
140 if(size_ >= capacity_ - n)
143 size_ = static_cast<std::uint16_t>(size_ + n);
145 i_ = static_cast<std::uint16_t>(
146 (i_ + n) % capacity_);
149 auto m = capacity_ - i_;
150 std::memcpy(&p_[i_], in, m);
152 i_ = static_cast<std::uint16_t>(n - m);
153 std::memcpy(&p_[0], in, i_);