1#ifndef IOUTILITIES_BITREADER_H
2#define IOUTILITIES_BITREADER_H
15 BitReader(
const char *buffer, std::size_t bufferSize);
16 BitReader(
const char *buffer,
const char *end);
18 template <
typename intType> intType readBits(std::uint8_t bitCount);
19 std::uint8_t readBit();
20 template <
typename intType> intType readUnsignedExpGolombCodedBits();
21 template <
typename intType> intType readSignedExpGolombCodedBits();
22 template <
typename intType> intType showBits(std::uint8_t bitCount);
23 void skipBits(std::size_t bitCount);
25 std::size_t bitsAvailable();
26 void reset(
const char *buffer, std::size_t bufferSize);
27 void reset(
const char *buffer,
const char *end);
30 const std::uint8_t *m_buffer;
31 const std::uint8_t *m_end;
32 std::uint8_t m_bitsAvail;
53 : m_buffer(reinterpret_cast<const
std::uint8_t *>(buffer))
54 , m_end(reinterpret_cast<const
std::uint8_t *>(end))
70 for (std::uint8_t readAtOnce; bitCount; bitCount -= readAtOnce) {
72 if (++m_buffer >= m_end) {
73 throw std::ios_base::failure(
"end of buffer exceeded");
77 readAtOnce = std::min(bitCount, m_bitsAvail);
78 val =
static_cast<intType
>(
79 (val << readAtOnce) | static_cast<intType>(((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce))));
104 std::uint8_t count = 0;
108 return count ?
static_cast<intType
>(((1 << count) |
readBits<intType>(count)) - 1) : 0;
121 auto value = readUnsignedExpGolombCodedBits<typename std::make_unsigned<intType>::type>();
122 return (value % 2) ?
static_cast<intType
>((value + 1) / 2) : (-
static_cast<intType
>(value / 2));
131 return tmp.readBits<intType>(bitCount);
139 return m_buffer != m_end ?
static_cast<std::size_t
>(((m_end - m_buffer - 1) * 8) + m_bitsAvail) :
static_cast<std::size_t
>(0);
150 m_buffer =
reinterpret_cast<const std::uint8_t *
>(buffer);
151 m_end =
reinterpret_cast<const std::uint8_t *
>(buffer + bufferSize);
163 m_buffer =
reinterpret_cast<const std::uint8_t *
>(buffer);
164 m_end =
reinterpret_cast<const std::uint8_t *
>(end);
The BitReader class provides bitwise reading of buffered data.
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
std::uint8_t readBit()
Reads the one bit from the buffer advancing the current position by one bit.
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
void align()
Re-establishes alignment.
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
BitReader(const char *buffer, std::size_t bufferSize)
Constructs a new BitReader.
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
intType readBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits.
intType showBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.