]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/zlib/detail/bitstream.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / zlib / detail / bitstream.hpp
1 //
2 // Copyright (c) 2016-2017 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_BITSTREAM_HPP
38 #define BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
39
40 #include <boost/assert.hpp>
41 #include <cstdint>
42 #include <iterator>
43
44 namespace boost {
45 namespace beast {
46 namespace zlib {
47 namespace detail {
48
49 class bitstream
50 {
51 using value_type = std::uint32_t;
52
53 value_type v_ = 0;
54 unsigned n_ = 0;
55
56 public:
57 // returns the number of bits in the reservoir
58 unsigned
59 size() const
60 {
61 return n_;
62 }
63
64 // discard n bits
65 void
66 drop(std::size_t n)
67 {
68 BOOST_ASSERT(n <= n_);
69 n_ -= static_cast<unsigned>(n);
70 v_ >>= n;
71 }
72
73 // flush everything
74 void
75 flush()
76 {
77 n_ = 0;
78 v_ = 0;
79 }
80
81 // flush to the next byte boundary
82 void
83 flush_byte()
84 {
85 drop(n_ % 8);
86 }
87
88 // ensure at least n bits
89 template<class FwdIt>
90 bool
91 fill(std::size_t n, FwdIt& first, FwdIt const& last);
92
93 // fill 8 bits, unchecked
94 template<class FwdIt>
95 void
96 fill_8(FwdIt& it);
97
98 // fill 16 bits, unchecked
99 template<class FwdIt>
100 void
101 fill_16(FwdIt& it);
102
103 // return n bits
104 template<class Unsigned>
105 void
106 peek(Unsigned& value, std::size_t n);
107
108 // return everything in the reservoir
109 value_type
110 peek_fast() const
111 {
112 return v_;
113 }
114
115 // return n bits, and consume
116 template<class Unsigned>
117 void
118 read(Unsigned& value, std::size_t n);
119
120 // rewind by the number of whole bytes stored (unchecked)
121 template<class BidirIt>
122 void
123 rewind(BidirIt& it);
124 };
125
126 template<class FwdIt>
127 inline
128 bool
129 bitstream::
130 fill(std::size_t n, FwdIt& first, FwdIt const& last)
131 {
132 while(n_ < n)
133 {
134 if(first == last)
135 return false;
136 v_ += static_cast<value_type>(*first++) << n_;
137 n_ += 8;
138 }
139 return true;
140 }
141
142 template<class FwdIt>
143 inline
144 void
145 bitstream::
146 fill_8(FwdIt& it)
147 {
148 v_ += static_cast<value_type>(*it++) << n_;
149 n_ += 8;
150 }
151
152 template<class FwdIt>
153 inline
154 void
155 bitstream::
156 fill_16(FwdIt& it)
157 {
158 v_ += static_cast<value_type>(*it++) << n_;
159 n_ += 8;
160 v_ += static_cast<value_type>(*it++) << n_;
161 n_ += 8;
162 }
163
164 template<class Unsigned>
165 inline
166 void
167 bitstream::
168 peek(Unsigned& value, std::size_t n)
169 {
170 BOOST_ASSERT(n <= sizeof(value)*8);
171 BOOST_ASSERT(n <= n_);
172 value = static_cast<Unsigned>(
173 v_ & ((1ULL << n) - 1));
174 }
175
176 template<class Unsigned>
177 inline
178 void
179 bitstream::
180 read(Unsigned& value, std::size_t n)
181 {
182 BOOST_ASSERT(n < sizeof(v_)*8);
183 BOOST_ASSERT(n <= n_);
184 value = static_cast<Unsigned>(
185 v_ & ((1ULL << n) - 1));
186 v_ >>= n;
187 n_ -= static_cast<unsigned>(n);
188 }
189
190 template<class BidirIt>
191 inline
192 void
193 bitstream::
194 rewind(BidirIt& it)
195 {
196 auto len = n_ >> 3;
197 it = std::prev(it, len);
198 n_ &= 7;
199 v_ &= (1U << n_) - 1;
200 }
201
202 } // detail
203 } // zlib
204 } // beast
205 } // boost
206
207 #endif