Skip to content

Commit

Permalink
LibCompress: Pass ReadonlyBytes to encode_huffman_lengths()
Browse files Browse the repository at this point in the history
...instead of Array and length. No behavior change.
  • Loading branch information
nico committed May 21, 2024
1 parent e3ac0d6 commit 16e71ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions Userland/Libraries/LibCompress/Deflate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,14 @@ ErrorOr<void> DeflateCompressor::write_huffman(CanonicalCode const& literal_code
return {};
}

size_t DeflateCompressor::encode_huffman_lengths(Array<u8, max_huffman_literals + max_huffman_distances> const& lengths, size_t lengths_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths)
size_t DeflateCompressor::encode_huffman_lengths(ReadonlyBytes lengths, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths)
{
size_t encoded_count = 0;
size_t i = 0;
while (i < lengths_count) {
while (i < lengths.size()) {
if (lengths[i] == 0) {
auto zero_count = 0;
for (size_t j = i; j < min(lengths_count, i + 138) && lengths[j] == 0; j++)
for (size_t j = i; j < min(lengths.size(), i + 138) && lengths[j] == 0; j++)
zero_count++;

if (zero_count < 3) { // below minimum repeated zero count
Expand All @@ -788,7 +788,7 @@ size_t DeflateCompressor::encode_huffman_lengths(Array<u8, max_huffman_literals
encoded_lengths[encoded_count++].symbol = lengths[i++];

auto copy_count = 0;
for (size_t j = i; j < min(lengths_count, i + 6) && lengths[j] == lengths[i - 1]; j++)
for (size_t j = i; j < min(lengths.size(), i + 6) && lengths[j] == lengths[i - 1]; j++)
copy_count++;

if (copy_count >= 3) {
Expand Down Expand Up @@ -820,7 +820,7 @@ size_t DeflateCompressor::encode_block_lengths(Array<u8, max_huffman_literals> c
for (size_t i = 0; i < distance_code_count; i++)
all_lengths[literal_code_count + i] = distance_bit_lengths[i];

return encode_huffman_lengths(all_lengths, literal_code_count + distance_code_count, encoded_lengths);
return encode_huffman_lengths(all_lengths.span().trim(literal_code_count + distance_code_count), encoded_lengths);
}

ErrorOr<void> DeflateCompressor::write_dynamic_huffman(CanonicalCode const& literal_code, size_t literal_code_count, Optional<CanonicalCode> const& distance_code, size_t distance_code_count, Array<u8, 19> const& code_lengths_bit_lengths, size_t code_length_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances> const& encoded_lengths, size_t encoded_lengths_count)
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibCompress/Deflate.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class DeflateCompressor final : public Stream {
static u8 distance_to_base(u16 distance);
size_t huffman_block_length(Array<u8, max_huffman_literals> const& literal_bit_lengths, Array<u8, max_huffman_distances> const& distance_bit_lengths);
ErrorOr<void> write_huffman(CanonicalCode const& literal_code, Optional<CanonicalCode> const& distance_code);
static size_t encode_huffman_lengths(Array<u8, max_huffman_literals + max_huffman_distances> const& lengths, size_t lengths_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths);
static size_t encode_huffman_lengths(ReadonlyBytes lengths, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths);
size_t encode_block_lengths(Array<u8, max_huffman_literals> const& literal_bit_lengths, Array<u8, max_huffman_distances> const& distance_bit_lengths, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths, size_t& literal_code_count, size_t& distance_code_count);
ErrorOr<void> write_dynamic_huffman(CanonicalCode const& literal_code, size_t literal_code_count, Optional<CanonicalCode> const& distance_code, size_t distance_code_count, Array<u8, 19> const& code_lengths_bit_lengths, size_t code_length_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances> const& encoded_lengths, size_t encoded_lengths_count);

Expand Down

0 comments on commit 16e71ef

Please sign in to comment.