1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:37:37 +00:00

LibCompress: Port ZlibDecompressor to AK::Stream

This commit is contained in:
Tim Schumacher 2023-06-08 19:35:44 +02:00 committed by Sam Atkins
parent 90780d9ade
commit 8a853278d0
7 changed files with 96 additions and 68 deletions

View file

@ -4,11 +4,18 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/MemoryStream.h>
#include <LibCompress/Zlib.h> #include <LibCompress/Zlib.h>
#include <stdio.h> #include <stdio.h>
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{ {
(void)Compress::ZlibDecompressor::decompress_all(ReadonlyBytes { data, size }); auto stream = make<FixedMemoryStream>(ReadonlyBytes { data, size });
auto decompressor_or_error = Compress::ZlibDecompressor::create(move(stream));
if (decompressor_or_error.is_error())
return 0;
auto decompressor = decompressor_or_error.release_value();
(void)decompressor->read_until_eof();
return 0; return 0;
} }

View file

@ -7,6 +7,7 @@
#include <LibTest/TestCase.h> #include <LibTest/TestCase.h>
#include <AK/Array.h> #include <AK/Array.h>
#include <AK/MemoryStream.h>
#include <LibCompress/Zlib.h> #include <LibCompress/Zlib.h>
TEST_CASE(zlib_decompress_simple) TEST_CASE(zlib_decompress_simple)
@ -20,8 +21,10 @@ TEST_CASE(zlib_decompress_simple)
const u8 uncompressed[] = "This is a simple text file :)"; const u8 uncompressed[] = "This is a simple text file :)";
auto const decompressed = Compress::ZlibDecompressor::decompress_all(compressed); auto stream = make<FixedMemoryStream>(compressed);
EXPECT(decompressed.value().bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 })); auto decompressor = TRY_OR_FAIL(Compress::ZlibDecompressor::create(move(stream)));
auto decompressed = TRY_OR_FAIL(decompressor->read_until_eof());
EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));
} }
TEST_CASE(zlib_compress_simple) TEST_CASE(zlib_compress_simple)
@ -58,7 +61,7 @@ TEST_CASE(zlib_decompress_with_missing_end_bits)
0x17, 0x17, 0x08, 0x43, 0xC5, 0xC9, 0x05, 0xA8, 0x4B, 0x50, 0x50, 0x50, 0x17, 0x17, 0x08, 0x43, 0xC5, 0xC9, 0x05, 0xA8, 0x4B, 0x50, 0x50, 0x50,
0xC4, 0xD1, 0x45, 0x50, 0x80, 0x01, 0x06, 0x00, 0xB6, 0x1F, 0x15, 0xEF 0xC4, 0xD1, 0x45, 0x50, 0x80, 0x01, 0x06, 0x00, 0xB6, 0x1F, 0x15, 0xEF
}; };
Array<u8, 144> const decompressed { Array<u8, 144> const uncompressed {
0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x10, 0x00, 0x32, 0x22, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x10, 0x00, 0x32, 0x22,
0x22, 0x22, 0x22, 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52, 0x22, 0x22, 0x22, 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52,
0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52, 0x22, 0x22, 0x10, 0x22, 0x22, 0x10, 0x00, 0x32, 0x55, 0x22, 0x25, 0x52, 0x22, 0x22, 0x10,
@ -73,7 +76,8 @@ TEST_CASE(zlib_decompress_with_missing_end_bits)
0x44, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x44, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; };
auto const maybe_decompressed = Compress::ZlibDecompressor::decompress_all(compressed); auto stream = make<FixedMemoryStream>(compressed);
EXPECT(maybe_decompressed.has_value()); auto decompressor = TRY_OR_FAIL(Compress::ZlibDecompressor::create(move(stream)));
EXPECT_EQ(maybe_decompressed.value().span(), decompressed.span()); auto decompressed = TRY_OR_FAIL(decompressor->read_until_eof());
EXPECT_EQ(decompressed.span(), uncompressed.span());
} }

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/BitStream.h>
#include <AK/IntegralMath.h> #include <AK/IntegralMath.h>
#include <AK/MemoryStream.h> #include <AK/MemoryStream.h>
#include <AK/Span.h> #include <AK/Span.h>
@ -14,59 +15,53 @@
namespace Compress { namespace Compress {
constexpr static size_t Adler32Size = sizeof(u32); ErrorOr<NonnullOwnPtr<ZlibDecompressor>> ZlibDecompressor::create(MaybeOwned<Stream> stream)
Optional<ZlibDecompressor> ZlibDecompressor::try_create(ReadonlyBytes data)
{ {
if (data.size() < (sizeof(ZlibHeader) + Adler32Size)) auto header = TRY(stream->read_value<ZlibHeader>());
return {};
ZlibHeader header { .as_u16 = data.at(0) << 8 | data.at(1) };
if (header.compression_method != ZlibCompressionMethod::Deflate || header.compression_info > 7) if (header.compression_method != ZlibCompressionMethod::Deflate || header.compression_info > 7)
return {}; // non-deflate compression return Error::from_string_literal("Non-DEFLATE compression inside Zlib is not supported");
if (header.present_dictionary) if (header.present_dictionary)
return {}; // we dont support pre-defined dictionaries return Error::from_string_literal("Zlib compression with a pre-defined dictionary is currently not supported");
if (header.as_u16 % 31 != 0) if (header.as_u16 % 31 != 0)
return {}; // error correction code doesn't match return Error::from_string_literal("Zlib error correction code does not match");
ZlibDecompressor zlib { header, data }; auto bit_stream = make<LittleEndianInputBitStream>(move(stream));
zlib.m_data_bytes = data.slice(2, data.size() - sizeof(ZlibHeader) - Adler32Size); auto deflate_stream = TRY(Compress::DeflateDecompressor::construct(move(bit_stream)));
return zlib;
return adopt_nonnull_own_or_enomem(new (nothrow) ZlibDecompressor(header, move(deflate_stream)));
} }
ZlibDecompressor::ZlibDecompressor(ZlibHeader header, ReadonlyBytes data) ZlibDecompressor::ZlibDecompressor(ZlibHeader header, NonnullOwnPtr<Stream> stream)
: m_header(header) : m_header(header)
, m_input_data(data) , m_stream(move(stream))
{ {
} }
Optional<ByteBuffer> ZlibDecompressor::decompress() ErrorOr<Bytes> ZlibDecompressor::read_some(Bytes bytes)
{ {
auto buffer_or_error = DeflateDecompressor::decompress_all(m_data_bytes); return m_stream->read_some(bytes);
if (buffer_or_error.is_error())
return {};
return buffer_or_error.release_value();
} }
Optional<ByteBuffer> ZlibDecompressor::decompress_all(ReadonlyBytes bytes) ErrorOr<size_t> ZlibDecompressor::write_some(ReadonlyBytes)
{ {
auto zlib = try_create(bytes); return Error::from_errno(EBADF);
if (!zlib.has_value())
return {};
return zlib->decompress();
} }
u32 ZlibDecompressor::checksum() bool ZlibDecompressor::is_eof() const
{ {
if (!m_checksum) { return m_stream->is_eof();
auto bytes = m_input_data.slice_from_end(Adler32Size); }
m_checksum = bytes.at(0) << 24 | bytes.at(1) << 16 | bytes.at(2) << 8 || bytes.at(3);
}
return m_checksum; bool ZlibDecompressor::is_open() const
{
return m_stream->is_open();
}
void ZlibDecompressor::close()
{
} }
ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(MaybeOwned<Stream> stream, ZlibCompressionLevel compression_level) ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(MaybeOwned<Stream> stream, ZlibCompressionLevel compression_level)

View file

@ -44,22 +44,21 @@ struct ZlibHeader {
}; };
static_assert(sizeof(ZlibHeader) == sizeof(u16)); static_assert(sizeof(ZlibHeader) == sizeof(u16));
class ZlibDecompressor { class ZlibDecompressor : public Stream {
public: public:
Optional<ByteBuffer> decompress(); static ErrorOr<NonnullOwnPtr<ZlibDecompressor>> create(MaybeOwned<Stream>);
u32 checksum();
static Optional<ZlibDecompressor> try_create(ReadonlyBytes data); virtual ErrorOr<Bytes> read_some(Bytes) override;
static Optional<ByteBuffer> decompress_all(ReadonlyBytes); virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
private: private:
ZlibDecompressor(ZlibHeader, ReadonlyBytes data); ZlibDecompressor(ZlibHeader, NonnullOwnPtr<Stream>);
ZlibHeader m_header; ZlibHeader m_header;
NonnullOwnPtr<Stream> m_stream;
u32 m_checksum { 0 };
ReadonlyBytes m_input_data;
ReadonlyBytes m_data_bytes;
}; };
class ZlibCompressor : public Stream { class ZlibCompressor : public Stream {
@ -87,3 +86,8 @@ private:
}; };
} }
template<>
struct AK::Traits<Compress::ZlibHeader> : public AK::GenericTraits<Compress::ZlibHeader> {
static constexpr bool is_trivially_serializable() { return true; }
};

View file

@ -6,6 +6,7 @@
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/IntegralMath.h> #include <AK/IntegralMath.h>
#include <AK/MemoryStream.h>
#include <LibCompress/Zlib.h> #include <LibCompress/Zlib.h>
#include <LibGfx/Font/OpenType/Font.h> #include <LibGfx/Font/OpenType/Font.h>
#include <LibGfx/Font/WOFF/Font.h> #include <LibGfx/Font/WOFF/Font.h>
@ -120,12 +121,12 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(Readonl
if (font_buffer_offset + orig_length > font_buffer.size()) if (font_buffer_offset + orig_length > font_buffer.size())
return Error::from_string_literal("Uncompressed WOFF table too big"); return Error::from_string_literal("Uncompressed WOFF table too big");
if (comp_length < orig_length) { if (comp_length < orig_length) {
auto decompressed = Compress::ZlibDecompressor::decompress_all(buffer.slice(offset, comp_length)); auto compressed_data_stream = make<FixedMemoryStream>(buffer.slice(offset, comp_length));
if (!decompressed.has_value()) auto decompressor = TRY(Compress::ZlibDecompressor::create(move(compressed_data_stream)));
return Error::from_string_literal("Could not decompress WOFF table"); auto decompressed = TRY(decompressor->read_until_eof());
if (orig_length != decompressed->size()) if (orig_length != decompressed.size())
return Error::from_string_literal("Invalid decompressed WOFF table length"); return Error::from_string_literal("Invalid decompressed WOFF table length");
font_buffer.overwrite(font_buffer_offset, decompressed->data(), orig_length); font_buffer.overwrite(font_buffer_offset, decompressed.data(), orig_length);
} else { } else {
if (comp_length != orig_length) if (comp_length != orig_length)
return Error::from_string_literal("Invalid uncompressed WOFF table length"); return Error::from_string_literal("Invalid uncompressed WOFF table length");

View file

@ -7,6 +7,7 @@
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/Endian.h> #include <AK/Endian.h>
#include <AK/MemoryStream.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCompress/Zlib.h> #include <LibCompress/Zlib.h>
#include <LibGfx/ImageFormats/PNGLoader.h> #include <LibGfx/ImageFormats/PNGLoader.h>
@ -852,12 +853,19 @@ static ErrorOr<void> decode_png_bitmap(PNGLoadingContext& context)
if (context.color_type == PNG::ColorType::IndexedColor && context.palette_data.is_empty()) if (context.color_type == PNG::ColorType::IndexedColor && context.palette_data.is_empty())
return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see a PLTE chunk for a palletized image, or it was empty."); return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see a PLTE chunk for a palletized image, or it was empty.");
auto result = Compress::ZlibDecompressor::decompress_all(context.compressed_data.span()); auto compressed_data_stream = make<FixedMemoryStream>(context.compressed_data.span());
if (!result.has_value()) { auto decompressor_or_error = Compress::ZlibDecompressor::create(move(compressed_data_stream));
if (decompressor_or_error.is_error()) {
context.state = PNGLoadingContext::State::Error; context.state = PNGLoadingContext::State::Error;
return Error::from_string_literal("PNGImageDecoderPlugin: Decompression failed"); return decompressor_or_error.release_error();
} }
auto& decompression_buffer = result.value(); auto decompressor = decompressor_or_error.release_value();
auto result_or_error = decompressor->read_until_eof();
if (result_or_error.is_error()) {
context.state = PNGLoadingContext::State::Error;
return result_or_error.release_error();
}
auto decompression_buffer = result_or_error.release_value();
context.compressed_data.clear(); context.compressed_data.clear();
context.scanlines.ensure_capacity(context.height); context.scanlines.ensure_capacity(context.height);
@ -887,11 +895,9 @@ static ErrorOr<RefPtr<Bitmap>> decode_png_animation_frame_bitmap(PNGLoadingConte
auto frame_rect = animation_frame.rect(); auto frame_rect = animation_frame.rect();
auto frame_context = context.create_subimage_context(frame_rect.width(), frame_rect.height()); auto frame_context = context.create_subimage_context(frame_rect.width(), frame_rect.height());
auto result = Compress::ZlibDecompressor::decompress_all(animation_frame.compressed_data.span()); auto compressed_data_stream = make<FixedMemoryStream>(animation_frame.compressed_data.span());
if (!result.has_value()) auto decompressor = TRY(Compress::ZlibDecompressor::create(move(compressed_data_stream)));
return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of animation frame failed"); auto decompression_buffer = TRY(decompressor->read_until_eof());
auto& decompression_buffer = result.value();
frame_context.compressed_data.clear(); frame_context.compressed_data.clear();
frame_context.scanlines.ensure_capacity(frame_context.height); frame_context.scanlines.ensure_capacity(frame_context.height);
@ -1443,12 +1449,19 @@ ErrorOr<Optional<ReadonlyBytes>> PNGImageDecoderPlugin::icc_data()
if (m_context->embedded_icc_profile.has_value()) { if (m_context->embedded_icc_profile.has_value()) {
if (!m_context->decompressed_icc_profile.has_value()) { if (!m_context->decompressed_icc_profile.has_value()) {
auto result = Compress::ZlibDecompressor::decompress_all(m_context->embedded_icc_profile->compressed_data); auto compressed_data_stream = make<FixedMemoryStream>(m_context->embedded_icc_profile->compressed_data);
if (!result.has_value()) { auto decompressor_or_error = Compress::ZlibDecompressor::create(move(compressed_data_stream));
if (decompressor_or_error.is_error()) {
m_context->embedded_icc_profile.clear(); m_context->embedded_icc_profile.clear();
return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of ICC profile failed"); return decompressor_or_error.release_error();
} }
m_context->decompressed_icc_profile = move(*result); auto decompressor = decompressor_or_error.release_value();
auto result_or_error = decompressor->read_until_eof();
if (result_or_error.is_error()) {
m_context->embedded_icc_profile.clear();
return result_or_error.release_error();
}
m_context->decompressed_icc_profile = result_or_error.release_value();
} }
return m_context->decompressed_icc_profile.value(); return m_context->decompressed_icc_profile.value();

View file

@ -51,13 +51,17 @@ static ErrorOr<ByteBuffer> handle_content_encoding(ByteBuffer const& buf, Deprec
// Even though the content encoding is "deflate", it's actually deflate with the zlib wrapper. // Even though the content encoding is "deflate", it's actually deflate with the zlib wrapper.
// https://tools.ietf.org/html/rfc7230#section-4.2.2 // https://tools.ietf.org/html/rfc7230#section-4.2.2
auto uncompressed = Compress::ZlibDecompressor::decompress_all(buf); auto memory_stream = make<FixedMemoryStream>(buf);
if (!uncompressed.has_value()) { auto zlib_decompressor = Compress::ZlibDecompressor::create(move(memory_stream));
Optional<ByteBuffer> uncompressed;
if (zlib_decompressor.is_error()) {
// From the RFC: // From the RFC:
// "Note: Some non-conformant implementations send the "deflate" // "Note: Some non-conformant implementations send the "deflate"
// compressed data without the zlib wrapper." // compressed data without the zlib wrapper."
dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: ZlibDecompressor::decompress_all() failed. Trying DeflateDecompressor::decompress_all()"); dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: ZlibDecompressor::decompress_all() failed. Trying DeflateDecompressor::decompress_all()");
uncompressed = TRY(Compress::DeflateDecompressor::decompress_all(buf)); uncompressed = TRY(Compress::DeflateDecompressor::decompress_all(buf));
} else {
uncompressed = TRY(zlib_decompressor.value()->read_until_eof());
} }
if constexpr (JOB_DEBUG) { if constexpr (JOB_DEBUG) {