Skip to content

Commit 023c640

Browse files
timschumiawesomekling
authored andcommitted
LibCompress: Use the correct LZMA repetition offset in all cases
1 parent 9ccb0fc commit 023c640

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Userland/Libraries/LibCompress/Lzma.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ ErrorOr<void> LzmaDecompressor::decode_literal_to_output_buffer()
321321
// Testing `(State > 7)` with actual test files yields errors, so the reference implementation appears to be the correct one.
322322
if (m_state >= 7) {
323323
u8 matched_byte = 0;
324-
auto read_bytes = TRY(m_dictionary->read_with_seekback({ &matched_byte, sizeof(matched_byte) }, m_rep0 + 1));
324+
auto read_bytes = TRY(m_dictionary->read_with_seekback({ &matched_byte, sizeof(matched_byte) }, current_repetition_offset()));
325325
VERIFY(read_bytes.size() == sizeof(matched_byte));
326326

327327
do {
@@ -455,6 +455,16 @@ ErrorOr<u32> LzmaDecompressor::decode_normalized_match_distance(u16 normalized_m
455455
return (distance_prefix << number_of_alignment_bits) | TRY(decode_symbol_using_reverse_bit_tree(number_of_alignment_bits, m_alignment_bit_probabilities));
456456
}
457457

458+
u32 LzmaDecompressor::current_repetition_offset() const
459+
{
460+
// LZMA never needs to read at offset 0 (i.e. the actual read head of the buffer).
461+
// Instead, the values are remapped so that the rep-value n starts reading n + 1 bytes back.
462+
// The special rep-value 0xFFFFFFFF is reserved for marking the end of the stream,
463+
// so this should never overflow.
464+
VERIFY(m_rep0 < NumericLimits<u32>::max());
465+
return m_rep0 + 1;
466+
}
467+
458468
ErrorOr<Bytes> LzmaDecompressor::read_some(Bytes bytes)
459469
{
460470
while (m_dictionary->used_space() < bytes.size() && m_dictionary->empty_space() != 0) {
@@ -517,7 +527,7 @@ ErrorOr<Bytes> LzmaDecompressor::read_some(Bytes bytes)
517527
}
518528

519529
u8 byte;
520-
auto read_bytes = TRY(m_dictionary->read_with_seekback({ &byte, sizeof(byte) }, m_rep0 + 1));
530+
auto read_bytes = TRY(m_dictionary->read_with_seekback({ &byte, sizeof(byte) }, current_repetition_offset()));
521531
VERIFY(read_bytes.size() == sizeof(byte));
522532

523533
auto written_bytes = m_dictionary->write({ &byte, sizeof(byte) });
@@ -600,7 +610,7 @@ ErrorOr<Bytes> LzmaDecompressor::read_some(Bytes bytes)
600610

601611
// "Also the decoder must check that "rep0" value is not larger than dictionary size
602612
// and is not larger than the number of already decoded bytes."
603-
if (m_rep0 > m_dictionary->seekback_limit())
613+
if (current_repetition_offset() > m_dictionary->seekback_limit())
604614
return Error::from_string_literal("rep0 value is larger than the possible lookback size");
605615

606616
// "Then the decoder must copy match bytes as described in

Userland/Libraries/LibCompress/Lzma.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class LzmaDecompressor : public Stream {
147147
u32 m_rep1 { 0 };
148148
u32 m_rep2 { 0 };
149149
u32 m_rep3 { 0 };
150+
u32 current_repetition_offset() const;
150151

151152
static constexpr size_t maximum_number_of_position_bits = 4;
152153
static constexpr size_t number_of_states = 12;

0 commit comments

Comments
 (0)