123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #ifndef BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
- #define BOOST_BEAST_ZLIB_DETAIL_BITSTREAM_HPP
- #include <boost/assert.hpp>
- #include <cstdint>
- #include <iterator>
- namespace boost {
- namespace beast {
- namespace zlib {
- namespace detail {
- class bitstream
- {
- using value_type = std::uint32_t;
- value_type v_ = 0;
- unsigned n_ = 0;
- public:
-
- unsigned
- size() const
- {
- return n_;
- }
-
- void
- drop(std::size_t n)
- {
- BOOST_ASSERT(n <= n_);
- n_ -= static_cast<unsigned>(n);
- v_ >>= n;
- }
-
- void
- flush()
- {
- n_ = 0;
- v_ = 0;
- }
-
- void
- flush_byte()
- {
- drop(n_ % 8);
- }
-
- template<class FwdIt>
- bool
- fill(std::size_t n, FwdIt& first, FwdIt const& last);
-
- template<class FwdIt>
- void
- fill_8(FwdIt& it);
-
- template<class FwdIt>
- void
- fill_16(FwdIt& it);
-
- template<class Unsigned>
- void
- peek(Unsigned& value, std::size_t n);
-
- value_type
- peek_fast() const
- {
- return v_;
- }
-
- template<class Unsigned>
- void
- read(Unsigned& value, std::size_t n);
-
- template<class BidirIt>
- void
- rewind(BidirIt& it);
- };
- template<class FwdIt>
- bool
- bitstream::
- fill(std::size_t n, FwdIt& first, FwdIt const& last)
- {
- while(n_ < n)
- {
- if(first == last)
- return false;
- v_ += static_cast<value_type>(*first++) << n_;
- n_ += 8;
- }
- return true;
- }
- template<class FwdIt>
- void
- bitstream::
- fill_8(FwdIt& it)
- {
- v_ += static_cast<value_type>(*it++) << n_;
- n_ += 8;
- }
- template<class FwdIt>
- void
- bitstream::
- fill_16(FwdIt& it)
- {
- v_ += static_cast<value_type>(*it++) << n_;
- n_ += 8;
- v_ += static_cast<value_type>(*it++) << n_;
- n_ += 8;
- }
- template<class Unsigned>
- void
- bitstream::
- peek(Unsigned& value, std::size_t n)
- {
- BOOST_ASSERT(n <= sizeof(value)*8);
- BOOST_ASSERT(n <= n_);
- value = static_cast<Unsigned>(
- v_ & ((1ULL << n) - 1));
- }
- template<class Unsigned>
- void
- bitstream::
- read(Unsigned& value, std::size_t n)
- {
- BOOST_ASSERT(n < sizeof(v_)*8);
- BOOST_ASSERT(n <= n_);
- value = static_cast<Unsigned>(
- v_ & ((1ULL << n) - 1));
- v_ >>= n;
- n_ -= static_cast<unsigned>(n);
- }
- template<class BidirIt>
- void
- bitstream::
- rewind(BidirIt& it)
- {
- auto len = n_ >> 3;
- it = std::prev(it, len);
- n_ &= 7;
- v_ &= (1U << n_) - 1;
- }
- }
- }
- }
- }
- #endif
|