2 // Copyright (c) 2016-2019 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 // Official repository: https://github.com/boostorg/beast
9 // This is a derivative work based on Zlib, copyright below:
11 Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
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.
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:
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.
29 Jean-loup Gailly Mark Adler
30 jloup@gzip.org madler@alumni.caltech.edu
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).
37 #ifndef BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
38 #define BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
40 #include <boost/assert.hpp>
51 using value_type = std::uint32_t;
57 // returns the number of bits in the reservoir
68 BOOST_ASSERT(n <= n_);
69 n_ -= static_cast<unsigned>(n);
81 // flush to the next byte boundary
88 // ensure at least n bits
91 fill(std::size_t n, FwdIt& first, FwdIt const& last);
93 // fill 8 bits, unchecked
98 // fill 16 bits, unchecked
104 template<class Unsigned>
106 peek(Unsigned& value, std::size_t n);
108 // return everything in the reservoir
115 // return n bits, and consume
116 template<class Unsigned>
118 read(Unsigned& value, std::size_t n);
120 // rewind by the number of whole bytes stored (unchecked)
121 template<class BidirIt>
126 template<class FwdIt>
129 fill(std::size_t n, FwdIt& first, FwdIt const& last)
135 v_ += static_cast<value_type>(*first++) << n_;
141 template<class FwdIt>
146 v_ += static_cast<value_type>(*it++) << n_;
150 template<class FwdIt>
155 v_ += static_cast<value_type>(*it++) << n_;
157 v_ += static_cast<value_type>(*it++) << n_;
161 template<class Unsigned>
164 peek(Unsigned& value, std::size_t n)
166 BOOST_ASSERT(n <= sizeof(value)*8);
167 BOOST_ASSERT(n <= n_);
168 value = static_cast<Unsigned>(
169 v_ & ((1ULL << n) - 1));
172 template<class Unsigned>
175 read(Unsigned& value, std::size_t n)
177 BOOST_ASSERT(n < sizeof(v_)*8);
178 BOOST_ASSERT(n <= n_);
179 value = static_cast<Unsigned>(
180 v_ & ((1ULL << n) - 1));
182 n_ -= static_cast<unsigned>(n);
185 template<class BidirIt>
191 it = std::prev(it, len);
193 v_ &= (1U << n_) - 1;