diff --git a/AK/CircularBuffer.cpp b/AK/CircularBuffer.cpp index 6ffe7ede668..075ffd4e25f 100644 --- a/AK/CircularBuffer.cpp +++ b/AK/CircularBuffer.cpp @@ -206,4 +206,20 @@ ErrorOr CircularBuffer::fill_from_stream(Stream& stream) return bytes.size(); } +ErrorOr CircularBuffer::copy_from_seekback(size_t distance, size_t length) +{ + if (distance > m_seekback_limit) + return Error::from_string_literal("Tried a seekback copy beyond the seekback limit"); + + while (length > 0) { + auto next_span = next_read_span_with_seekback(distance); + if (next_span.size() == 0) + break; + + length -= write(next_span.trim(length)); + } + + return {}; +} + } diff --git a/AK/CircularBuffer.h b/AK/CircularBuffer.h index ffeb887319d..be23b791e47 100644 --- a/AK/CircularBuffer.h +++ b/AK/CircularBuffer.h @@ -33,6 +33,8 @@ public: /// before the current write pointer and allows for reading already-read data. ErrorOr read_with_seekback(Bytes bytes, size_t distance); + ErrorOr copy_from_seekback(size_t distance, size_t length); + [[nodiscard]] size_t empty_space() const; [[nodiscard]] size_t used_space() const; [[nodiscard]] size_t capacity() const; diff --git a/Userland/Libraries/LibCompress/Deflate.cpp b/Userland/Libraries/LibCompress/Deflate.cpp index 04d26eba021..44d010b6f0e 100644 --- a/Userland/Libraries/LibCompress/Deflate.cpp +++ b/Userland/Libraries/LibCompress/Deflate.cpp @@ -207,9 +207,7 @@ ErrorOr DeflateDecompressor::CompressedBlock::try_read_more() m_decompressor.m_output_buffer.write({ &byte, sizeof(byte) }); } } else { - Array buffer; - auto bytes = TRY(m_decompressor.m_output_buffer.read_with_seekback({ buffer.data(), length }, distance)); - m_decompressor.m_output_buffer.write(bytes); + TRY(m_decompressor.m_output_buffer.copy_from_seekback(distance, length)); } return true;