bit_reader_c: fix get_bit_position/get_remaining_bits for empty buffers

One side affect of the fixed bug was that trying to identify/mux an
empty file (file with a size of 0) resulted in the following error
message:

> Error: buffer_c: num > m_filled. Should not have happened. Please
> file a bug report.
This commit is contained in:
Moritz Bunkus 2018-06-14 21:22:49 +02:00
parent 1c7ad896a1
commit c8cb86b8fb
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85
2 changed files with 11 additions and 2 deletions

View File

@ -194,11 +194,11 @@ public:
}
int get_bit_position() const {
return (m_byte_position - m_start_of_data) * 8 + 8 - m_bits_valid;
return (m_byte_position - m_start_of_data) * 8 + (m_bits_valid ? 8 - m_bits_valid : 0);
}
int get_remaining_bits() const {
return (m_end_of_data - m_byte_position) * 8 - 8 + m_bits_valid;
return (m_end_of_data - m_byte_position) * 8 - (m_bits_valid ? 8 - m_bits_valid : 0);
}
void skip_bits(std::size_t num) {

View File

@ -20,6 +20,15 @@ TEST(BitReader, Initialization) {
EXPECT_FALSE(b.eof());
}
TEST(BitReader, InitializationEmpty) {
unsigned char value{};
auto b = mtx::bits::reader_c{&value, 0};
EXPECT_EQ(0, b.get_bit_position());
EXPECT_EQ(0, b.get_remaining_bits());
EXPECT_TRUE(b.eof());
}
TEST(BitReader, GetBit) {
unsigned char value[4];
put_uint32_be(value, 0xf7234a81);