1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

LibCompress: Order branches in Deflate's decode_codes() numerically

deflate_special_code_length_copy has value 16, so it should be
before the two zero-filling branches for codes 17 and 18.

Also, the initial if also refers to deflate_special_code_length_copy
as well, so if it's repeated right in the next else, one has to keep
it on the mental stack for shorter when reading this code.

No behavior change.
This commit is contained in:
Nico Weber 2023-04-04 09:17:57 -04:00 committed by Andreas Kling
parent 72d6a30e08
commit 26230f2ffd

View file

@ -437,23 +437,21 @@ ErrorOr<void> DeflateDecompressor::decode_codes(CanonicalCode& literal_code, Opt
if (symbol < deflate_special_code_length_copy) { if (symbol < deflate_special_code_length_copy) {
code_lengths.append(static_cast<u8>(symbol)); code_lengths.append(static_cast<u8>(symbol));
} else if (symbol == deflate_special_code_length_copy) {
if (code_lengths.is_empty())
return Error::from_string_literal("Found no codes to copy before a copy block");
auto nrepeat = 3 + TRY(m_input_stream->read_bits(2));
for (size_t j = 0; j < nrepeat; ++j)
code_lengths.append(code_lengths.last());
} else if (symbol == deflate_special_code_length_zeros) { } else if (symbol == deflate_special_code_length_zeros) {
auto nrepeat = 3 + TRY(m_input_stream->read_bits(3)); auto nrepeat = 3 + TRY(m_input_stream->read_bits(3));
for (size_t j = 0; j < nrepeat; ++j) for (size_t j = 0; j < nrepeat; ++j)
code_lengths.append(0); code_lengths.append(0);
} else if (symbol == deflate_special_code_length_long_zeros) { } else {
VERIFY(symbol == deflate_special_code_length_long_zeros);
auto nrepeat = 11 + TRY(m_input_stream->read_bits(7)); auto nrepeat = 11 + TRY(m_input_stream->read_bits(7));
for (size_t j = 0; j < nrepeat; ++j) for (size_t j = 0; j < nrepeat; ++j)
code_lengths.append(0); code_lengths.append(0);
} else {
VERIFY(symbol == deflate_special_code_length_copy);
if (code_lengths.is_empty())
return Error::from_string_literal("Found no codes to copy before a copy block");
auto nrepeat = 3 + TRY(m_input_stream->read_bits(2));
for (size_t j = 0; j < nrepeat; ++j)
code_lengths.append(code_lengths.last());
} }
} }