Skip to content
Draft
Show file tree
Hide file tree
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
32 changes: 2 additions & 30 deletions src/decoding/decodebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,7 @@ impl DecodeBuffer {
// We need to copy in chunks.
self.repeat_in_chunks(offset, match_length, start_idx);
} else {
// can just copy parts of the existing buffer
// SAFETY: Requirements checked:
// 1. start_idx + match_length must be <= self.buffer.len()
// We know that:
// 1. start_idx = self.buffer.len() - offset
// 2. end_idx = start_idx + match_length
// 3. end_idx <= self.buffer.len()
// Thus follows: start_idx + match_length <= self.buffer.len()
//
// 2. explicitly reserved enough memory for the whole match_length
unsafe {
self.buffer
.extend_from_within_unchecked(start_idx, match_length)
};
self.buffer.extend_from_within(start_idx, match_length);
}

self.total_output_counter += match_length as u64;
Expand All @@ -137,22 +124,7 @@ impl DecodeBuffer {
while copied_counter_left > 0 {
let chunksize = usize::min(offset, copied_counter_left);

// SAFETY: Requirements checked:
// 1. start_idx + chunksize must be <= self.buffer.len()
// We know that:
// 1. start_idx starts at buffer.len() - offset
// 2. chunksize <= offset (== offset for each iteration but the last, and match_length modulo offset in the last iteration)
// 3. the buffer grows by offset many bytes each iteration but the last
// 4. start_idx is increased by the same amount as the buffer grows each iteration
//
// Thus follows: start_idx + chunksize == self.buffer.len() in each iteration but the last, where match_length modulo offset == chunksize < offset
// Meaning: start_idx + chunksize <= self.buffer.len()
//
// 2. explicitly reserved enough memory for the whole match_length
unsafe {
self.buffer
.extend_from_within_unchecked(start_idx, chunksize)
};
self.buffer.extend_from_within(start_idx, chunksize);
copied_counter_left -= chunksize;
start_idx += chunksize;
}
Expand Down
Loading
Loading