2018-10-19 23:09:50 +00:00
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
2018-11-18 17:10:21 +00:00
|
|
|
#include <sstream>
|
2018-10-26 13:42:00 +00:00
|
|
|
#include "ki/util/BitTypes.h"
|
2018-10-19 23:09:50 +00:00
|
|
|
|
|
|
|
#define KI_BITSTREAM_DEFAULT_BUFFER_SIZE 0x2000
|
|
|
|
|
|
|
|
namespace ki
|
|
|
|
{
|
|
|
|
/**
|
2018-11-18 17:10:21 +00:00
|
|
|
* An abstract base class that provides a common interface for
|
|
|
|
* writing to, reading from, and querying bit streams.
|
2018-10-19 23:09:50 +00:00
|
|
|
*/
|
2018-11-18 17:10:21 +00:00
|
|
|
class BitStreamBase
|
2018-10-19 23:09:50 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Represents a position in a BitStream's buffer.
|
|
|
|
*/
|
|
|
|
struct stream_pos
|
|
|
|
{
|
|
|
|
explicit stream_pos(intmax_t byte = 0, int bit = 0);
|
|
|
|
stream_pos(const stream_pos &cp);
|
|
|
|
|
2018-11-16 14:53:46 +00:00
|
|
|
intmax_t as_bits() const;
|
2018-10-19 23:09:50 +00:00
|
|
|
intmax_t get_byte() const;
|
|
|
|
uint8_t get_bit() const;
|
|
|
|
|
|
|
|
stream_pos operator +(const stream_pos &rhs) const;
|
|
|
|
stream_pos operator -(const stream_pos &rhs) const;
|
|
|
|
stream_pos operator +(const int &rhs) const;
|
|
|
|
stream_pos operator -(const int &rhs) const;
|
|
|
|
stream_pos &operator +=(stream_pos lhs);
|
|
|
|
stream_pos &operator -=(stream_pos lhs);
|
|
|
|
stream_pos &operator +=(int bits);
|
|
|
|
stream_pos &operator -=(int bits);
|
|
|
|
stream_pos &operator ++();
|
|
|
|
stream_pos &operator --();
|
2018-10-20 19:08:17 +00:00
|
|
|
stream_pos operator ++(int increment);
|
|
|
|
stream_pos operator --(int increment);
|
2018-10-19 23:09:50 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
intmax_t m_byte;
|
|
|
|
uint8_t m_bit;
|
|
|
|
|
|
|
|
void set_bit(int bit);
|
|
|
|
};
|
|
|
|
|
2018-11-18 17:10:21 +00:00
|
|
|
virtual ~BitStreamBase() {}
|
2018-10-19 23:09:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-11-18 17:10:21 +00:00
|
|
|
* @returns The stream's current position.
|
2018-10-19 23:09:50 +00:00
|
|
|
*/
|
2018-11-18 17:10:21 +00:00
|
|
|
virtual stream_pos tell() const = 0;
|
2018-10-19 23:09:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the position of the stream.
|
|
|
|
* @param position The new position of the stream.
|
|
|
|
*/
|
2018-11-18 17:10:21 +00:00
|
|
|
virtual void seek(stream_pos position) = 0;
|
2018-10-19 23:09:50 +00:00
|
|
|
|
2018-10-20 17:17:35 +00:00
|
|
|
/**
|
2018-11-18 17:10:21 +00:00
|
|
|
* @returns The current size of the internal buffer.
|
2018-10-20 17:17:35 +00:00
|
|
|
*/
|
2018-11-18 17:10:21 +00:00
|
|
|
virtual std::size_t capacity() const = 0;
|
2018-10-20 17:17:35 +00:00
|
|
|
|
2018-10-19 23:09:50 +00:00
|
|
|
/**
|
2018-11-18 17:10:21 +00:00
|
|
|
* @returns A pointer to the start of the internal buffer.
|
2018-10-19 23:09:50 +00:00
|
|
|
*/
|
2018-11-18 17:10:21 +00:00
|
|
|
virtual const uint8_t *data() const = 0;
|
2018-10-19 23:09:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a value from the buffer given a defined number of bits.
|
2018-11-18 17:10:21 +00:00
|
|
|
* @param[in] bits The number of bits to read. Defaults to the bitsize of IntegerT.
|
|
|
|
* @returns The value read from the buffer.
|
2018-10-19 23:09:50 +00:00
|
|
|
*/
|
|
|
|
template <
|
|
|
|
typename IntegerT,
|
2018-10-26 13:42:00 +00:00
|
|
|
typename = std::enable_if<is_integral<IntegerT>::value>
|
2018-10-19 23:09:50 +00:00
|
|
|
>
|
2018-10-26 13:42:00 +00:00
|
|
|
IntegerT read(const uint8_t bits = bitsizeof<IntegerT>::value)
|
2018-10-19 23:09:50 +00:00
|
|
|
{
|
2018-11-18 17:10:21 +00:00
|
|
|
return static_cast<IntegerT>(read(bits));
|
2018-10-19 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a value to the buffer that occupies a defined number of bits.
|
2018-11-18 17:10:21 +00:00
|
|
|
* @tparam IntegerT The type of value (must be an integral type).
|
|
|
|
* @param[in] value The value to write.
|
|
|
|
* @param[in] bits The number of bits to use. Defaults to the bitsize of IntegerT.
|
2018-10-19 23:09:50 +00:00
|
|
|
*/
|
|
|
|
template <
|
|
|
|
typename IntegerT,
|
2018-10-26 13:42:00 +00:00
|
|
|
typename = std::enable_if<is_integral<IntegerT>::value>
|
2018-10-19 23:09:50 +00:00
|
|
|
>
|
2018-10-26 13:42:00 +00:00
|
|
|
void write(IntegerT value, const uint8_t bits = bitsizeof<IntegerT>::value)
|
2018-10-19 23:09:50 +00:00
|
|
|
{
|
2018-11-18 17:10:21 +00:00
|
|
|
write(static_cast<uint64_t>(value), bits);
|
2018-10-19 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 14:53:46 +00:00
|
|
|
/**
|
|
|
|
* Copy memory from an external buffer into the bitstream's buffer from the current position.
|
2018-11-18 17:10:21 +00:00
|
|
|
* @param[in] src The buffer to copy data from.
|
|
|
|
* @param[in] bitsize The number of bits to copy from the src buffer.
|
2018-11-16 14:53:46 +00:00
|
|
|
*/
|
|
|
|
void write_copy(uint8_t *src, std::size_t bitsize);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy memory from the bitstream's buffer into an external buffer.
|
2018-11-18 17:10:21 +00:00
|
|
|
* @param[out] dst The destination buffer to copy data to.
|
|
|
|
* @param[in] bitsize The number of bits to copy into the dst buffer.
|
2018-11-16 14:53:46 +00:00
|
|
|
*/
|
|
|
|
void read_copy(uint8_t *dst, std::size_t bitsize);
|
|
|
|
|
2018-11-18 17:10:21 +00:00
|
|
|
protected:
|
|
|
|
virtual uint64_t read(uint8_t bits) = 0;
|
|
|
|
virtual void write(uint64_t value, uint8_t bits) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A read/write-able stream of bits.
|
|
|
|
*/
|
|
|
|
class BitStream : public BitStreamBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit BitStream(std::size_t buffer_size = KI_BITSTREAM_DEFAULT_BUFFER_SIZE);
|
|
|
|
virtual ~BitStream();
|
|
|
|
|
|
|
|
stream_pos tell() const override;
|
|
|
|
void seek(stream_pos position) override;
|
|
|
|
std::size_t capacity() const override;
|
|
|
|
const uint8_t *data() const override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc BitStreamBase::read<IntegerT>(uint8_t)
|
|
|
|
* @throws ki::runtime_error Not enough data available to read the specified number of bits.
|
|
|
|
*/
|
|
|
|
template <
|
|
|
|
typename IntegerT,
|
|
|
|
typename = std::enable_if<is_integral<IntegerT>::value>
|
|
|
|
>
|
|
|
|
IntegerT read(const uint8_t bits = bitsizeof<IntegerT>::value)
|
|
|
|
{
|
|
|
|
return BitStreamBase::read<IntegerT>(bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename IntegerT,
|
|
|
|
typename = std::enable_if<is_integral<IntegerT>::value>
|
|
|
|
>
|
|
|
|
void write(IntegerT value, const uint8_t bits = bitsizeof<IntegerT>::value)
|
|
|
|
{
|
|
|
|
BitStreamBase::write<IntegerT>(value, bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
uint64_t read(uint8_t bits) override;
|
|
|
|
void write(uint64_t value, uint8_t bits) override;
|
|
|
|
|
2018-10-19 23:09:50 +00:00
|
|
|
private:
|
2018-11-18 17:10:21 +00:00
|
|
|
uint8_t * m_buffer;
|
2018-10-20 00:21:06 +00:00
|
|
|
std::size_t m_buffer_size;
|
2018-10-19 23:09:50 +00:00
|
|
|
stream_pos m_position;
|
|
|
|
|
|
|
|
void expand_buffer();
|
|
|
|
void validate_buffer();
|
|
|
|
};
|
2018-11-18 17:10:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Documentation
|
|
|
|
*/
|
|
|
|
class BitStreamSection : public BitStreamBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit BitStreamSection(BitStreamBase &stream, std::size_t size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
BitStreamBase &m_stream;
|
|
|
|
stream_pos m_position;
|
|
|
|
std::size_t m_size;
|
|
|
|
};
|
2018-10-19 23:09:50 +00:00
|
|
|
}
|