From b9455e243fdc03232a3a20bbd5968b54f4af2f61 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 16 Nov 2018 14:52:49 +0000 Subject: [PATCH] util: Add write_copy and read_copy to BitStream --- .gitignore | 1 + src/util/BitStream.cpp | 44 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4376a2d..2dc49e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Visual Studio/Code .vs/ +CMakeSettings.json # Prerequisites *.d diff --git a/src/util/BitStream.cpp b/src/util/BitStream.cpp index 7040eaf..597b3e7 100644 --- a/src/util/BitStream.cpp +++ b/src/util/BitStream.cpp @@ -19,6 +19,11 @@ namespace ki set_bit(cp.m_bit); } + intmax_t BitStream::stream_pos::as_bits() const + { + return (m_byte * 8) + m_bit; + } + intmax_t BitStream::stream_pos::get_byte() const { return m_byte; @@ -165,10 +170,47 @@ namespace ki return m_buffer; } + void BitStream::write_copy(uint8_t *src, const std::size_t bitsize) + { + // Copy all whole bytes + const auto bytes = bitsize / 8; + auto written_bytes = 0; + while (written_bytes < bytes) + { + write(src[written_bytes]); + written_bytes++; + } + + // Copy left over bits + const auto bits = bitsize % 8; + if (bits > 0) + write(src[bytes + 1], bits); + } + + void BitStream::read_copy(uint8_t *dst, const std::size_t bitsize) + { + // Copy all whole bytes + const auto bytes = bitsize / 8; + auto read_bytes = 0; + while (read_bytes < bytes) + { + dst[read_bytes] = read(); + read_bytes++; + } + + // Copy left over bits + const auto bits = bitsize % 8; + if (bits > 0) + dst[bytes + 1] = read(bits); + } + void BitStream::expand_buffer() { // Work out a new buffer size - auto new_size = (2 << (uint64_t)std::log2(m_position.get_byte()) + 1) + 2; + const auto minimum_bits = static_cast( + std::log2(m_position.get_byte()) + ) + 1; + auto new_size = (2 << minimum_bits) + 2; if (new_size < m_buffer_size) new_size = std::numeric_limits::max();