From fed9dbefdffe93390c0f7beb3ca526efffdd92ff Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 11 Apr 2026 19:47:44 +0300 Subject: [PATCH] LibDEFLATE: Allow decompression from multiple byte spans Before we required the compressed data to live in a single contiguous chunch of memory. --- .../LibDEFLATE/include/LibDEFLATE/BitStream.h | 32 +++++++++++++++---- .../include/LibDEFLATE/Decompressor.h | 5 ++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/userspace/libraries/LibDEFLATE/include/LibDEFLATE/BitStream.h b/userspace/libraries/LibDEFLATE/include/LibDEFLATE/BitStream.h index dbde3cad..1e15a6f6 100644 --- a/userspace/libraries/LibDEFLATE/include/LibDEFLATE/BitStream.h +++ b/userspace/libraries/LibDEFLATE/include/LibDEFLATE/BitStream.h @@ -3,6 +3,8 @@ #include #include +#include + namespace LibDEFLATE { @@ -10,6 +12,10 @@ namespace LibDEFLATE { public: BitInputStream(BAN::ConstByteSpan data) + : m_data_wrapper(data) + , m_data({ &m_data_wrapper, 1 }) + { } + BitInputStream(BAN::Span data) : m_data(data) { } @@ -19,11 +25,14 @@ namespace LibDEFLATE while (m_bit_buffer_len < count) { + while (!m_data.empty() && m_data[0].empty()) + m_data = m_data.slice(1); if (m_data.empty()) return BAN::Error::from_errno(ENOBUFS); - m_bit_buffer |= m_data[0] << m_bit_buffer_len; + + m_bit_buffer |= m_data[0][0] << m_bit_buffer_len; m_bit_buffer_len += 8; - m_data = m_data.slice(1); + m_data[0] = m_data[0].slice(1); } return m_bit_buffer & ((1 << count) - 1); @@ -49,10 +58,18 @@ namespace LibDEFLATE bytes--; } - if (bytes > m_data.size()) - return BAN::Error::from_errno(EINVAL); - memcpy(output, m_data.data(), bytes); - m_data = m_data.slice(bytes); + while (bytes) + { + while (!m_data.empty() && m_data[0].empty()) + m_data = m_data.slice(1); + if (m_data.empty()) + return BAN::Error::from_errno(ENOBUFS); + const size_t to_copy = BAN::Math::min(m_data[0].size(), bytes); + memcpy(output, m_data[0].data(), to_copy); + m_data[0] = m_data[0].slice(to_copy); + output += to_copy; + bytes -= to_copy; + } return {}; } @@ -65,7 +82,8 @@ namespace LibDEFLATE } private: - BAN::ConstByteSpan m_data; + BAN::ConstByteSpan m_data_wrapper; + BAN::Span m_data; uint32_t m_bit_buffer { 0 }; uint8_t m_bit_buffer_len { 0 }; }; diff --git a/userspace/libraries/LibDEFLATE/include/LibDEFLATE/Decompressor.h b/userspace/libraries/LibDEFLATE/include/LibDEFLATE/Decompressor.h index f293a475..b4a7dd2a 100644 --- a/userspace/libraries/LibDEFLATE/include/LibDEFLATE/Decompressor.h +++ b/userspace/libraries/LibDEFLATE/include/LibDEFLATE/Decompressor.h @@ -1,6 +1,5 @@ #pragma once - #include #include #include @@ -22,6 +21,10 @@ namespace LibDEFLATE : m_type(type) , m_stream(data) { } + Decompressor(BAN::Span data, StreamType type) + : m_type(type) + , m_stream(data) + { } BAN::ErrorOr> decompress();