bitstream.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. This software is provided 'as-is', without any express or implied
  13. warranty. In no event will the authors be held liable for any damages
  14. 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:
  18. 1. The origin of this software must not be misrepresented; you must not
  19. claim that you wrote the original software. If you use this software
  20. in a product, an acknowledgment in the product documentation would be
  21. appreciated but is not required.
  22. 2. Altered source versions must be plainly marked as such, and must not be
  23. misrepresented as being the original software.
  24. 3. This notice may not be removed or altered from any source distribution.
  25. Jean-loup Gailly Mark Adler
  26. jloup@gzip.org madler@alumni.caltech.edu
  27. The data format used by the zlib library is described by RFCs (Request for
  28. Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
  29. (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
  30. */
  31. #ifndef BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
  32. #define BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
  33. #include <boost/assert.hpp>
  34. #include <cstdint>
  35. #include <iterator>
  36. namespace boost {
  37. namespace beast {
  38. namespace zlib {
  39. namespace detail {
  40. class bitstream
  41. {
  42. using value_type = std::uint32_t;
  43. value_type v_ = 0;
  44. unsigned n_ = 0;
  45. public:
  46. // returns the number of bits in the reservoir
  47. unsigned
  48. size() const
  49. {
  50. return n_;
  51. }
  52. // discard n bits
  53. void
  54. drop(std::size_t n)
  55. {
  56. BOOST_ASSERT(n <= n_);
  57. n_ -= static_cast<unsigned>(n);
  58. v_ >>= n;
  59. }
  60. // flush everything
  61. void
  62. flush()
  63. {
  64. n_ = 0;
  65. v_ = 0;
  66. }
  67. // flush to the next byte boundary
  68. void
  69. flush_byte()
  70. {
  71. drop(n_ % 8);
  72. }
  73. // ensure at least n bits
  74. template<class FwdIt>
  75. bool
  76. fill(std::size_t n, FwdIt& first, FwdIt const& last);
  77. // fill 8 bits, unchecked
  78. template<class FwdIt>
  79. void
  80. fill_8(FwdIt& it);
  81. // fill 16 bits, unchecked
  82. template<class FwdIt>
  83. void
  84. fill_16(FwdIt& it);
  85. // return n bits
  86. template<class Unsigned>
  87. void
  88. peek(Unsigned& value, std::size_t n);
  89. // return everything in the reservoir
  90. value_type
  91. peek_fast() const
  92. {
  93. return v_;
  94. }
  95. // return n bits, and consume
  96. template<class Unsigned>
  97. void
  98. read(Unsigned& value, std::size_t n);
  99. // rewind by the number of whole bytes stored (unchecked)
  100. template<class BidirIt>
  101. void
  102. rewind(BidirIt& it);
  103. };
  104. template<class FwdIt>
  105. bool
  106. bitstream::
  107. fill(std::size_t n, FwdIt& first, FwdIt const& last)
  108. {
  109. while(n_ < n)
  110. {
  111. if(first == last)
  112. return false;
  113. v_ += static_cast<value_type>(*first++) << n_;
  114. n_ += 8;
  115. }
  116. return true;
  117. }
  118. template<class FwdIt>
  119. void
  120. bitstream::
  121. fill_8(FwdIt& it)
  122. {
  123. v_ += static_cast<value_type>(*it++) << n_;
  124. n_ += 8;
  125. }
  126. template<class FwdIt>
  127. void
  128. bitstream::
  129. fill_16(FwdIt& it)
  130. {
  131. v_ += static_cast<value_type>(*it++) << n_;
  132. n_ += 8;
  133. v_ += static_cast<value_type>(*it++) << n_;
  134. n_ += 8;
  135. }
  136. template<class Unsigned>
  137. void
  138. bitstream::
  139. peek(Unsigned& value, std::size_t n)
  140. {
  141. BOOST_ASSERT(n <= sizeof(value)*8);
  142. BOOST_ASSERT(n <= n_);
  143. value = static_cast<Unsigned>(
  144. v_ & ((1ULL << n) - 1));
  145. }
  146. template<class Unsigned>
  147. void
  148. bitstream::
  149. read(Unsigned& value, std::size_t n)
  150. {
  151. BOOST_ASSERT(n < sizeof(v_)*8);
  152. BOOST_ASSERT(n <= n_);
  153. value = static_cast<Unsigned>(
  154. v_ & ((1ULL << n) - 1));
  155. v_ >>= n;
  156. n_ -= static_cast<unsigned>(n);
  157. }
  158. template<class BidirIt>
  159. void
  160. bitstream::
  161. rewind(BidirIt& it)
  162. {
  163. auto len = n_ >> 3;
  164. it = std::prev(it, len);
  165. n_ &= 7;
  166. v_ &= (1U << n_) - 1;
  167. }
  168. } // detail
  169. } // zlib
  170. } // beast
  171. } // boost
  172. #endif