Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions libbz2-rs-sys/src/bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,35 @@ mod stream {
unsafe { Allocator::from_bz_stream(self) }
}

#[must_use]
#[inline(always)]
pub(crate) fn pull_u32(
&mut self,
mut bit_buffer: u32,
bits_used: i32,
) -> Option<(u32, i32)> {
if self.avail_in < 4 {
return None;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a debug assertion that no more than 7 bytes are requested? And no more than 1 byte in the case of pull_u8.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well I added an assert that we can at least consume 1 additional byte, in other words that we don't ask for more input if we don't have space in the bit buffer.

The check you comment on is a bounds check, so we just skip this function if there are not 8 additional bytes in the input, there is nothing to assert there right?

// of course this uses big endian values
let read = unsafe { self.next_in.cast::<u32>().read_unaligned().to_be() };

// because of the endianness, we can only shift in whole bytes.
let increment_bytes = (31 - bits_used) / 8;
let increment_bits = 8 * increment_bytes;

bit_buffer <<= increment_bits;
bit_buffer |= read >> (32 - increment_bits);

self.next_in = unsafe { (self.next_in).add(increment_bytes as usize) };
self.avail_in -= increment_bytes as u32;

// skips updating `self.total_in`: the caller is responsible for keeping it updated

Some((bit_buffer, bits_used + increment_bits))
}

#[must_use]
#[inline(always)]
pub(crate) fn read_byte_fast(&mut self) -> Option<u8> {
Expand Down
5 changes: 4 additions & 1 deletion libbz2-rs-sys/src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ pub(crate) fn decompress(
break v;
}

if let Some(next_byte) = strm.read_byte_fast() {
if let Some((bit_buffer, bits_used)) = strm.pull_u32($s.bsBuff, $s.bsLive) {
$s.bsBuff = bit_buffer;
$s.bsLive = bits_used;
} else if let Some(next_byte) = strm.read_byte_fast() {
$s.bsBuff = $s.bsBuff << 8 | next_byte as u32;
$s.bsLive += 8;
} else {
Expand Down