From 229d19de63e934947cdb450a90e129be2a535b3b Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Sat, 24 Feb 2024 02:56:52 +1100 Subject: [PATCH] Revert "util/bitstream.cpp: Fixed cases where bits would be dropped when reading and writing. (#12057)" This reverts commit 69c3cd7daba9e8dd130af167c27ecd8b4131074f. This causes CHD SHA1 digests to change. Either it's buggy, or CHD SHA1 digests depend on the representation rather than the data itself. --- src/lib/util/bitstream.h | 58 +++++++++------------------------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/src/lib/util/bitstream.h b/src/lib/util/bitstream.h index 2866d28c20d..4f3817afe6f 100644 --- a/src/lib/util/bitstream.h +++ b/src/lib/util/bitstream.h @@ -40,11 +40,10 @@ public: private: // internal state uint32_t m_buffer; // current bit accumulator - int m_bits; // number of bits in the accumulator + int m_bits; // number of bits in the accumulator const uint8_t * m_read; // read pointer uint32_t m_doffset; // byte offset within the data uint32_t m_dlength; // length of the data - int m_dbitoffs; // bit offset within current read pointer }; @@ -65,7 +64,7 @@ public: private: // internal state uint32_t m_buffer; // current bit accumulator - int m_bits; // number of bits in the accumulator + int m_bits; // number of bits in the accumulator uint8_t * m_write; // write pointer uint32_t m_doffset; // byte offset within the data uint32_t m_dlength; // length of the data @@ -86,8 +85,7 @@ inline bitstream_in::bitstream_in(const void *src, uint32_t srclength) m_bits(0), m_read(reinterpret_cast(src)), m_doffset(0), - m_dlength(srclength), - m_dbitoffs(0) + m_dlength(srclength) { } @@ -105,31 +103,12 @@ inline uint32_t bitstream_in::peek(int numbits) // fetch data if we need more if (numbits > m_bits) { - while (m_bits < 32) + while (m_bits <= 24) { - uint32_t newbits = 0; - if (m_doffset < m_dlength) - { - // adjust current data to discard any previously read partial bits - newbits = (m_read[m_doffset] << m_dbitoffs) & 0xff; - } - - if (m_bits + 8 > 32) - { - // take only what can be used to fill out the rest of the buffer - m_dbitoffs = 32 - m_bits; - newbits >>= 8 - m_dbitoffs; - m_buffer |= newbits; - m_bits += m_dbitoffs; - } - else - { - m_buffer |= newbits << (24 - m_bits); - m_bits += 8 - m_dbitoffs; - m_dbitoffs = 0; - m_doffset++; - } + m_buffer |= m_read[m_doffset] << (24 - m_bits); + m_doffset++; + m_bits += 8; } } @@ -217,11 +196,8 @@ inline bitstream_out::bitstream_out(void *dest, uint32_t destlength) inline void bitstream_out::write(uint32_t newbits, int numbits) { - newbits <<= 32 - numbits; - // flush the buffer if we're going to overflow it - while (m_bits + numbits >= 32 && numbits > 0) - { + if (m_bits + numbits > 32) while (m_bits >= 8) { if (m_doffset < m_dlength) @@ -231,19 +207,11 @@ inline void bitstream_out::write(uint32_t newbits, int numbits) m_bits -= 8; } - // offload more bits if it'll still overflow the buffer - if (m_bits + numbits >= 32) - { - const int rem = std::min(32 - m_bits, numbits); - m_buffer |= newbits >> m_bits; - m_bits += rem; - newbits <<= rem; - numbits -= rem; - } - } - - if (numbits <= 0) - return; + // shift the bits to the top + if (numbits == 0) + newbits = 0; + else + newbits <<= 32 - numbits; // now shift it down to account for the number of bits we already have and OR them in m_buffer |= newbits >> m_bits;