2015-06-08 22:09:09 +02:00
|
|
|
#ifndef IOUTILITIES_BITREADER_H
|
|
|
|
#define IOUTILITIES_BITREADER_H
|
|
|
|
|
2015-09-06 20:19:09 +02:00
|
|
|
#include "../conversion/types.h"
|
|
|
|
#include "../application/global.h"
|
2015-06-10 21:00:44 +02:00
|
|
|
|
|
|
|
#include <ios>
|
2015-06-08 22:09:09 +02:00
|
|
|
|
2015-07-13 00:59:21 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2015-06-08 22:09:09 +02:00
|
|
|
namespace IoUtilities {
|
|
|
|
|
2015-06-10 21:00:44 +02:00
|
|
|
class LIB_EXPORT BitReader
|
2015-06-08 22:09:09 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-06-10 21:00:44 +02:00
|
|
|
BitReader(const char *buffer, std::size_t bufferSize);
|
|
|
|
BitReader(const char *buffer, const char *end);
|
|
|
|
|
|
|
|
template<typename intType> intType readBits(byte bitCount);
|
2015-08-08 01:18:24 +02:00
|
|
|
byte readBit();
|
2015-07-31 01:23:42 +02:00
|
|
|
template<typename intType> intType showBits(byte bitCount);
|
2015-06-10 21:00:44 +02:00
|
|
|
void skipBits(std::size_t bitCount);
|
2015-07-31 01:23:42 +02:00
|
|
|
void align();
|
2015-06-10 21:00:44 +02:00
|
|
|
std::size_t bitsAvailable();
|
2015-07-07 00:50:03 +02:00
|
|
|
void reset(const char *buffer, std::size_t bufferSize);
|
|
|
|
void reset(const char *buffer, const char *end);
|
2015-06-10 21:00:44 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
const byte *m_buffer;
|
|
|
|
const byte *m_end;
|
|
|
|
byte m_bitsAvail;
|
2015-06-08 22:09:09 +02:00
|
|
|
};
|
|
|
|
|
2015-06-10 21:00:44 +02:00
|
|
|
/*!
|
|
|
|
* \brief Constructs a new BitReader.
|
2015-07-13 00:59:21 +02:00
|
|
|
* \remarks
|
|
|
|
* - Does not take ownership over the specified \a buffer.
|
|
|
|
* - bufferSize must be equal or greather than 1.
|
2015-06-10 21:00:44 +02:00
|
|
|
*/
|
|
|
|
inline BitReader::BitReader(const char *buffer, std::size_t bufferSize) :
|
2015-07-13 00:59:21 +02:00
|
|
|
BitReader(buffer, buffer + bufferSize)
|
2015-06-10 21:00:44 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new BitReader.
|
2015-07-13 00:59:21 +02:00
|
|
|
* \remarks
|
|
|
|
* - Does not take ownership over the specified \a buffer.
|
|
|
|
* - \a end must be greather than \a buffer.
|
2015-06-10 21:00:44 +02:00
|
|
|
*/
|
|
|
|
inline BitReader::BitReader(const char *buffer, const char *end) :
|
|
|
|
m_buffer(reinterpret_cast<const byte *>(buffer)),
|
|
|
|
m_end(reinterpret_cast<const byte *>(end)),
|
|
|
|
m_bitsAvail(8)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/*!
|
2015-07-31 01:23:42 +02:00
|
|
|
* \brief Reads the specified number of bits from the buffer advancing the current position by \a bitCount bits.
|
2015-06-10 21:00:44 +02:00
|
|
|
* \param bitCount Specifies the number of bits read.
|
2015-07-27 23:16:49 +02:00
|
|
|
* \tparam intType Specifies the type of the returned value.
|
2015-06-10 21:00:44 +02:00
|
|
|
* \remarks Does not check whether intType is big enough to hold result.
|
|
|
|
* \throws Throws ios_base::failure if the end of the buffer is exceeded.
|
|
|
|
* The reader becomes invalid in that case.
|
|
|
|
*/
|
|
|
|
template<typename intType>
|
|
|
|
intType BitReader::readBits(byte bitCount)
|
|
|
|
{
|
|
|
|
intType val = 0;
|
|
|
|
for(byte readAtOnce; bitCount; bitCount -= readAtOnce) {
|
|
|
|
if(!m_bitsAvail) {
|
|
|
|
if(++m_buffer >= m_end) {
|
|
|
|
throw std::ios_base::failure("end of buffer exceeded");
|
|
|
|
}
|
2015-07-27 23:16:49 +02:00
|
|
|
m_bitsAvail = 8;
|
2015-06-10 21:00:44 +02:00
|
|
|
}
|
2015-07-13 00:59:21 +02:00
|
|
|
readAtOnce = std::min(bitCount, m_bitsAvail);
|
|
|
|
val = (val << readAtOnce) | (((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)));
|
2015-06-10 21:00:44 +02:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-08-08 01:18:24 +02:00
|
|
|
/*!
|
|
|
|
* \brief Reads the one bit from the buffer advancing the current position by one bit.
|
|
|
|
* \throws Throws ios_base::failure if the end of the buffer is exceeded.
|
|
|
|
* The reader becomes invalid in that case.
|
|
|
|
*/
|
|
|
|
inline byte BitReader::readBit()
|
|
|
|
{
|
|
|
|
return readBits<byte>(1) == 1;
|
|
|
|
}
|
|
|
|
|
2015-07-31 01:23:42 +02:00
|
|
|
/*!
|
|
|
|
* \brief Reads the specified number of bits from the buffer without advancing the current position.
|
|
|
|
*/
|
|
|
|
template<typename intType>
|
|
|
|
intType BitReader::showBits(byte bitCount)
|
|
|
|
{
|
2016-01-27 01:13:30 +01:00
|
|
|
auto tmp = *this;
|
|
|
|
return tmp.readBits<intType>(bitCount);
|
2015-07-31 01:23:42 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 21:00:44 +02:00
|
|
|
/*!
|
|
|
|
* \brief Returns the number of bits which are still available to read.
|
|
|
|
*/
|
|
|
|
inline std::size_t BitReader::bitsAvailable()
|
|
|
|
{
|
|
|
|
return ((m_end - m_buffer) * 8) + m_bitsAvail;
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:50:03 +02:00
|
|
|
/*!
|
|
|
|
* \brief Resets the reader.
|
2015-07-27 23:16:49 +02:00
|
|
|
* - Does not take ownership over the specified \a buffer.
|
|
|
|
* - bufferSize must be equal or greather than 1.
|
2015-07-07 00:50:03 +02:00
|
|
|
*/
|
|
|
|
inline void BitReader::reset(const char *buffer, size_t bufferSize)
|
|
|
|
{
|
|
|
|
m_buffer = reinterpret_cast<const byte *>(buffer);
|
|
|
|
m_end = reinterpret_cast<const byte *>(buffer + bufferSize);
|
|
|
|
m_bitsAvail = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Resets the reader.
|
2015-07-27 23:16:49 +02:00
|
|
|
* - Does not take ownership over the specified \a buffer.
|
|
|
|
* - \a end must be greather than \a buffer.
|
2015-07-07 00:50:03 +02:00
|
|
|
*/
|
|
|
|
inline void BitReader::reset(const char *buffer, const char *end)
|
|
|
|
{
|
|
|
|
m_buffer = reinterpret_cast<const byte *>(buffer);
|
|
|
|
m_end = reinterpret_cast<const byte *>(end);
|
|
|
|
m_bitsAvail = 8;
|
|
|
|
}
|
|
|
|
|
2015-07-31 01:23:42 +02:00
|
|
|
/*!
|
|
|
|
* \brief Re-establishes alignment.
|
|
|
|
*/
|
|
|
|
inline void BitReader::align()
|
|
|
|
{
|
|
|
|
skipBits(m_bitsAvail);
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:09:09 +02:00
|
|
|
} // namespace IoUtilities
|
|
|
|
|
|
|
|
#endif // IOUTILITIES_BITREADER_H
|