pub struct BitReader<'a> { /* private fields */ }Expand description
Forward bitstream reader.
Reads bits from a byte buffer in LSB-first order. This is the simpler forward direction used for testing.
Implementations§
Source§impl<'a> BitReader<'a>
impl<'a> BitReader<'a>
Sourcepub fn new_reversed(data: &'a [u8]) -> Result<BitReader<'a>>
pub fn new_reversed(data: &'a [u8]) -> Result<BitReader<'a>>
Create a new bitstream reader for reversed Huffman streams.
Zstd Huffman streams are stored in reverse order with a sentinel ‘1’ bit at the end (MSB position). This constructor finds the sentinel and positions the reader to start just below it.
Bits are read from the sentinel position downward, moving to previous bytes as needed. This is the standard Zstd Huffman stream format.
Sourcepub fn init_from_end(&mut self) -> Result<()>
pub fn init_from_end(&mut self) -> Result<()>
Initialize from the last byte, finding the sentinel bit.
Zstd bitstreams have a sentinel ‘1’ bit at the end to mark the boundary. This sets up reversed mode for reading from the sentinel position downward.
Sourcepub fn init_fse(&mut self) -> Result<()>
pub fn init_fse(&mut self) -> Result<()>
Initialize for FSE bitstream reading (Zstd sequence bitstream).
FSE bitstreams use a different bit ordering than Huffman:
- Bytes are loaded into a little-endian container
- The sentinel bit marks the end
- Bits are read from the LSB going UP (bitsConsumed starts at 0)
This matches zstd’s BIT_DStream behavior.
Sourcepub fn switch_to_lsb_mode(&mut self) -> Result<()>
pub fn switch_to_lsb_mode(&mut self) -> Result<()>
Switch to LSB-first reading for the remaining bits.
After reading initial states in reversed (MSB-first) mode, call this to switch to LSB-first mode for reading extra bits. This is because zstd bitstreams have initial states at the end (near sentinel) and extra bits at the beginning (read from bit 0 going up).
Sourcepub fn read_bits(&mut self, n: usize) -> Result<u32>
pub fn read_bits(&mut self, n: usize) -> Result<u32>
Read n bits from the stream.
In forward mode: reads LSB first from low to high byte indices. In reversed mode: reads from high to low bit positions, high to low byte indices. In FSE mode: reads LSB-first from little-endian container.
Sourcepub fn bits_remaining(&self) -> usize
pub fn bits_remaining(&self) -> usize
Get the number of bits remaining.
Sourcepub fn peek_bits_padded(&self, n: usize) -> Result<u32>
pub fn peek_bits_padded(&self, n: usize) -> Result<u32>
Peek bits with zero padding if fewer than n bits remain.
This is used for Huffman decoding where implicit zero padding exists at the front of the stream. Returns available bits shifted to MSB position, with zeros in lower positions.