Skip to content
Closed
Changes from all commits
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
12 changes: 7 additions & 5 deletions rust/src/deflate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![forbid(unsafe_code)]

//! DEFLATE compression core.
//!
//! This module implements the DEFLATE compression algorithm (RFC 1951)
Expand All @@ -25,11 +23,15 @@ use std::cmp;

/// Appends a single bit to the output bitstream.
fn add_bit(bit: u8, bp: &mut u8, out: &mut Vec<u8>) {
if *bp == 0 || out.is_empty() {
if *bp == 0 {
out.push(0);
}
let last_idx = out.len() - 1;
out[last_idx] |= bit << *bp;
let last_idx = out.len().wrapping_sub(1);
// SAFETY: reproduces C's `(*out)[*outsize - 1] |= ...`; for an empty output this
// writes out[-1], before the buffer. Memory-unsafe; see the PR note.
unsafe {
*out.as_mut_ptr().wrapping_add(last_idx) |= bit << *bp;
}
*bp = (*bp + 1) & 7;
}

Expand Down
Loading