Truly independent web browser
LibCompress: Remove the unused Deflate and zlib compressors
Shannon Booth9618db4924ae
diff --git a/Libraries/LibCompress/Deflate.cpp b/Libraries/LibCompress/Deflate.cppindex 80ee89463b1..4268af7e44e 100644--- a/Libraries/LibCompress/Deflate.cpp+++ b/Libraries/LibCompress/Deflate.cpp@@ -11,8 +11,6 @@#include <LibCompress/Deflate.h>#include <LibCompress/DeflateTables.h>-#include <zlib.h>-namespace Compress {ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)@@ -132,28 +130,4 @@ ErrorOr<u32> CanonicalCode::read_symbol(LittleEndianInputBitStream& stream) consreturn Error::from_string_literal("Symbol exceeds maximum symbol number");}-ErrorOr<NonnullOwnPtr<DeflateDecompressor>> DeflateDecompressor::create(MaybeOwned<Stream> stream)-{- auto buffer = TRY(AK::FixedArray<u8>::create(16 * 1024));- auto zstream = TRY(GenericZlibDecompressor::new_z_stream(-MAX_WBITS));- return adopt_nonnull_own_or_enomem(new (nothrow) DeflateDecompressor(move(buffer), move(stream), zstream));-}--ErrorOr<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)-{- return ::Compress::decompress_all<DeflateDecompressor>(bytes);-}--ErrorOr<NonnullOwnPtr<DeflateCompressor>> DeflateCompressor::create(MaybeOwned<Stream> stream, GenericZlibCompressionLevel compression_level)-{- auto buffer = TRY(AK::FixedArray<u8>::create(16 * 1024));- auto zstream = TRY(GenericZlibCompressor::new_z_stream(-MAX_WBITS, compression_level));- return adopt_nonnull_own_or_enomem(new (nothrow) DeflateCompressor(move(buffer), move(stream), zstream));-}--ErrorOr<ByteBuffer> DeflateCompressor::compress_all(ReadonlyBytes bytes, GenericZlibCompressionLevel compression_level)-{- return ::Compress::compress_all<DeflateCompressor>(bytes, compression_level);-}-}diff --git a/Libraries/LibCompress/Deflate.h b/Libraries/LibCompress/Deflate.hindex 7db9f26df0c..b9672aef1bb 100644--- a/Libraries/LibCompress/Deflate.h+++ b/Libraries/LibCompress/Deflate.h@@ -8,9 +8,10 @@#pragma once+#include <AK/Array.h>#include <AK/BitStream.h>#include <AK/Stream.h>-#include <LibCompress/GenericZlib.h>+#include <AK/Vector.h>namespace Compress {@@ -52,28 +53,4 @@ ALWAYS_INLINE ErrorOr<void> CanonicalCode::write_symbol(LittleEndianOutputBitStrreturn {};}-class DeflateDecompressor final : public GenericZlibDecompressor {-public:- static ErrorOr<NonnullOwnPtr<DeflateDecompressor>> create(MaybeOwned<Stream>);- static ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes);--private:- DeflateDecompressor(AK::FixedArray<u8> buffer, MaybeOwned<Stream> stream, z_stream* zstream)- : GenericZlibDecompressor(move(buffer), move(stream), zstream)- {- }-};--class DeflateCompressor final : public GenericZlibCompressor {-public:- static ErrorOr<NonnullOwnPtr<DeflateCompressor>> create(MaybeOwned<Stream>, GenericZlibCompressionLevel = GenericZlibCompressionLevel::Default);- static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes, GenericZlibCompressionLevel = GenericZlibCompressionLevel::Default);--private:- DeflateCompressor(AK::FixedArray<u8> buffer, MaybeOwned<Stream> stream, z_stream* zstream)- : GenericZlibCompressor(move(buffer), move(stream), zstream)- {- }-};-}diff --git a/Libraries/LibCompress/Forward.h b/Libraries/LibCompress/Forward.hindex 1d586996e32..2a0e8bc884d 100644--- a/Libraries/LibCompress/Forward.h+++ b/Libraries/LibCompress/Forward.h@@ -8,9 +8,6 @@namespace Compress {-class DeflateCompressor;-class DeflateDecompressor;-class ZlibCompressor;class ZlibDecompressor;}diff --git a/Libraries/LibCompress/GenericZlib.cpp b/Libraries/LibCompress/GenericZlib.cppindex 6a49950b894..6030310d2d1 100644--- a/Libraries/LibCompress/GenericZlib.cpp+++ b/Libraries/LibCompress/GenericZlib.cpp@@ -116,113 +116,4 @@ void GenericZlibDecompressor::close(){}-GenericZlibCompressor::GenericZlibCompressor(AK::FixedArray<u8> buffer, MaybeOwned<Stream> stream, z_stream* zstream)- : m_stream(move(stream))- , m_zstream(zstream)- , m_buffer(move(buffer))-{-}--ErrorOr<z_stream*> GenericZlibCompressor::new_z_stream(int window_bits, GenericZlibCompressionLevel compression_level)-{- auto zstream = new (nothrow) z_stream {};- if (!zstream)- return Error::from_errno(ENOMEM);-- // The fields zalloc, zfree and opaque must be initialized before by the caller.- zstream->zalloc = nullptr;- zstream->zfree = nullptr;- zstream->opaque = nullptr;-- int level = [&] {- switch (compression_level) {- case GenericZlibCompressionLevel::Fastest:- return Z_BEST_SPEED;- case GenericZlibCompressionLevel::Default:- return Z_DEFAULT_COMPRESSION;- case GenericZlibCompressionLevel::Best:- return Z_BEST_COMPRESSION;- default:- VERIFY_NOT_REACHED();- }- }();-- if (auto ret = deflateInit2(zstream, level, Z_DEFLATED, window_bits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); ret != Z_OK)- return handle_zlib_error(ret);-- return zstream;-}--GenericZlibCompressor::~GenericZlibCompressor()-{- deflateEnd(m_zstream);- delete m_zstream;-}--ErrorOr<Bytes> GenericZlibCompressor::read_some(Bytes)-{- return Error::from_errno(EBADF);-}--ErrorOr<size_t> GenericZlibCompressor::write_some(ReadonlyBytes bytes)-{- m_zstream->avail_in = bytes.size();- m_zstream->next_in = const_cast<u8*>(bytes.data());-- // If deflate returns with avail_out == 0, this function must be called again with the same value of the flush parameter- // and more output space (updated avail_out), until the flush is complete (deflate returns with non-zero avail_out).- do {- m_zstream->avail_out = m_buffer.size();- m_zstream->next_out = m_buffer.data();-- auto ret = deflate(m_zstream, Z_NO_FLUSH);- if (ret != Z_OK && ret != Z_BUF_ERROR)- return handle_zlib_error(ret);-- auto have = m_buffer.size() - m_zstream->avail_out;- TRY(m_stream->write_until_depleted(m_buffer.span().slice(0, have)));- } while (m_zstream->avail_out == 0);-- VERIFY(m_zstream->avail_in == 0);- return bytes.size();-}--bool GenericZlibCompressor::is_eof() const-{- return false;-}--bool GenericZlibCompressor::is_open() const-{- return m_stream->is_open();-}--void GenericZlibCompressor::close()-{-}--ErrorOr<void> GenericZlibCompressor::finish()-{- VERIFY(m_zstream->avail_in == 0);-- // If the parameter flush is set to Z_FINISH, pending input is processed, pending output is flushed and deflate returns with Z_STREAM_END- // if there was enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this function must be called again with Z_FINISH- // and more output space (updated avail_out) but no more input data, until it returns with Z_STREAM_END or an error.- while (true) {- m_zstream->avail_out = m_buffer.size();- m_zstream->next_out = m_buffer.data();-- auto ret = deflate(m_zstream, Z_FINISH);- if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || ret == Z_OK) {- auto have = m_buffer.size() - m_zstream->avail_out;- TRY(m_stream->write_until_depleted(m_buffer.span().slice(0, have)));-- if (ret == Z_STREAM_END)- return {};- } else {- return handle_zlib_error(ret);- }- }-}-}diff --git a/Libraries/LibCompress/GenericZlib.h b/Libraries/LibCompress/GenericZlib.hindex 71346832bb7..f2d32d37129 100644--- a/Libraries/LibCompress/GenericZlib.h+++ b/Libraries/LibCompress/GenericZlib.h@@ -18,12 +18,6 @@ typedef struct z_stream_s z_stream;namespace Compress {-enum class GenericZlibCompressionLevel : u8 {- Fastest,- Default,- Best,-};-class GenericZlibDecompressor : public Stream {AK_MAKE_NONCOPYABLE(GenericZlibDecompressor);@@ -50,31 +44,6 @@ private:AK::FixedArray<u8> m_buffer;};-class GenericZlibCompressor : public Stream {- AK_MAKE_NONCOPYABLE(GenericZlibCompressor);--public:- ~GenericZlibCompressor() override;-- virtual ErrorOr<Bytes> read_some(Bytes) override;- virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;- virtual bool is_eof() const override;- virtual bool is_open() const override;- virtual void close() override;- ErrorOr<void> finish();--protected:- GenericZlibCompressor(AK::FixedArray<u8>, MaybeOwned<Stream>, z_stream*);-- static ErrorOr<z_stream*> new_z_stream(int window_bits, GenericZlibCompressionLevel compression_level);--private:- MaybeOwned<Stream> m_stream;- z_stream* m_zstream;-- AK::FixedArray<u8> m_buffer;-};-template<class T>ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes bytes){@@ -83,19 +52,4 @@ ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes bytes)return TRY(deflate_stream->read_until_eof(4096));}-template<class T, class CompressionLevel>-ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, CompressionLevel compression_level)-{- auto output_stream = TRY(try_make<AllocatingMemoryStream>());- auto gzip_stream = TRY(T::create(MaybeOwned { *output_stream }, compression_level));-- TRY(gzip_stream->write_until_depleted(bytes));- TRY(gzip_stream->finish());-- auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size()));- TRY(output_stream->read_until_filled(buffer.bytes()));-- return buffer;-}-}diff --git a/Libraries/LibCompress/Zlib.cpp b/Libraries/LibCompress/Zlib.cppindex 2e88eb3ad48..1305f4bcc74 100644--- a/Libraries/LibCompress/Zlib.cpp+++ b/Libraries/LibCompress/Zlib.cpp@@ -23,16 +23,4 @@ ErrorOr<ByteBuffer> ZlibDecompressor::decompress_all(ReadonlyBytes bytes)return ::Compress::decompress_all<ZlibDecompressor>(bytes);}-ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::create(MaybeOwned<Stream> stream, GenericZlibCompressionLevel compression_level)-{- auto buffer = TRY(AK::FixedArray<u8>::create(16 * 1024));- auto zstream = TRY(GenericZlibCompressor::new_z_stream(MAX_WBITS, compression_level));- return adopt_nonnull_own_or_enomem(new (nothrow) ZlibCompressor(move(buffer), move(stream), zstream));-}--ErrorOr<ByteBuffer> ZlibCompressor::compress_all(ReadonlyBytes bytes, GenericZlibCompressionLevel compression_level)-{- return ::Compress::compress_all<ZlibCompressor>(bytes, compression_level);-}-}diff --git a/Libraries/LibCompress/Zlib.h b/Libraries/LibCompress/Zlib.hindex 0c2a3224b3f..9ec457d7c8f 100644--- a/Libraries/LibCompress/Zlib.h+++ b/Libraries/LibCompress/Zlib.h@@ -26,16 +26,4 @@ private:}};-class ZlibCompressor final : public GenericZlibCompressor {-public:- static ErrorOr<NonnullOwnPtr<ZlibCompressor>> create(MaybeOwned<Stream>, GenericZlibCompressionLevel = GenericZlibCompressionLevel::Default);- static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes, GenericZlibCompressionLevel = GenericZlibCompressionLevel::Default);--private:- ZlibCompressor(AK::FixedArray<u8> buffer, MaybeOwned<Stream> stream, z_stream* zstream)- : GenericZlibCompressor(move(buffer), move(stream), zstream)- {- }-};-}diff --git a/Libraries/LibGfx/ImageFormats/WebPWriterLossless.cpp b/Libraries/LibGfx/ImageFormats/WebPWriterLossless.cppindex f348ef9da32..acd05c97aa7 100644--- a/Libraries/LibGfx/ImageFormats/WebPWriterLossless.cpp+++ b/Libraries/LibGfx/ImageFormats/WebPWriterLossless.cpp@@ -7,6 +7,7 @@// Lossless format: https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification#include <AK/BitStream.h>+#include <AK/ByteBuffer.h>#include <AK/Debug.h>#include <AK/HashMap.h>#include <AK/HashTable.h>diff --git a/Tests/LibCompress/TestDeflate.cpp b/Tests/LibCompress/TestDeflate.cppindex 9fd6ce77e2a..48273bb89d3 100644--- a/Tests/LibCompress/TestDeflate.cpp+++ b/Tests/LibCompress/TestDeflate.cpp@@ -7,7 +7,6 @@#include <AK/Array.h>#include <AK/BitStream.h>#include <AK/MemoryStream.h>-#include <AK/Random.h>#include <LibCompress/Deflate.h>#include <LibTest/TestCase.h>@@ -59,99 +58,3 @@ TEST_CASE(invalid_canonical_code)code.fill(0x08);EXPECT(Compress::CanonicalCode::from_bytes(code).is_error());}--TEST_CASE(deflate_decompress_compressed_block)-{- Array<u8, 28> const compressed {- 0x0B, 0xC9, 0xC8, 0x2C, 0x56, 0x00, 0xA2, 0x44, 0x85, 0xE2, 0xCC, 0xDC,- 0x82, 0x9C, 0x54, 0x85, 0x92, 0xD4, 0x8A, 0x12, 0x85, 0xB4, 0x4C, 0x20,- 0xCB, 0x4A, 0x13, 0x00- };-- u8 const uncompressed[] = "This is a simple text file :)";-- auto const decompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(decompressed.bytes() == ReadonlyBytes({ uncompressed, sizeof(uncompressed) - 1 }));-}--TEST_CASE(deflate_decompress_uncompressed_block)-{- Array<u8, 18> const compressed {- 0x01, 0x0d, 0x00, 0xf2, 0xff, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,- 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21- };-- u8 const uncompressed[] = "Hello, World!";-- auto const decompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));-}--TEST_CASE(deflate_decompress_multiple_blocks)-{- Array<u8, 72> const compressed {- 0x00, 0x1f, 0x00, 0xe0, 0xff, 0x54, 0x68, 0x65, 0x20, 0x66, 0x69, 0x72,- 0x73, 0x74, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x69, 0x73, 0x20,- 0x75, 0x6e, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64,- 0x53, 0x48, 0xcc, 0x4b, 0x51, 0x28, 0xc9, 0x48, 0x55, 0x28, 0x4e, 0x4d,- 0xce, 0x07, 0x32, 0x93, 0x72, 0xf2, 0x93, 0xb3, 0x15, 0x32, 0x8b, 0x15,- 0x92, 0xf3, 0x73, 0x0b, 0x8a, 0x52, 0x8b, 0x8b, 0x53, 0x53, 0xf4, 0x00- };-- u8 const uncompressed[] = "The first block is uncompressed and the second block is compressed.";-- auto const decompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));-}--TEST_CASE(deflate_decompress_zeroes)-{- Array<u8, 20> const compressed {- 0xed, 0xc1, 0x01, 0x0d, 0x00, 0x00, 0x00, 0xc2, 0xa0, 0xf7, 0x4f, 0x6d,- 0x0f, 0x07, 0x14, 0x00, 0x00, 0x00, 0xf0, 0x6e- };-- Array<u8, 4096> const uncompressed { 0 };-- auto const decompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(uncompressed == decompressed.bytes());-}--TEST_CASE(deflate_round_trip_store)-{- auto original = ByteBuffer::create_uninitialized(1024).release_value();- fill_with_random(original);- auto compressed = TRY_OR_FAIL(Compress::DeflateCompressor::compress_all(original));- auto uncompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(uncompressed == original);-}--TEST_CASE(deflate_round_trip_compress)-{- auto original = ByteBuffer::create_zeroed(2048).release_value();- fill_with_random(original.bytes().trim(1024)); // we pre-filled the second half with 0s to make sure we test back references as well- // Since the different levels just change how much time is spent looking for better matches, just use fast here to reduce test time- auto compressed = TRY_OR_FAIL(Compress::DeflateCompressor::compress_all(original));- auto uncompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(uncompressed == original);-}--TEST_CASE(deflate_round_trip_compress_large)-{- // Compress a buffer larger than the maximum block size to test the sliding window mechanism- auto original = TRY_OR_FAIL(ByteBuffer::create_uninitialized((32 * KiB - 1) * 2));- fill_with_random(original);-- // Since the different levels just change how much time is spent looking for better matches, just use fast here to reduce test time- auto compressed = TRY_OR_FAIL(Compress::DeflateCompressor::compress_all(original));- auto uncompressed = TRY_OR_FAIL(Compress::DeflateDecompressor::decompress_all(compressed));- EXPECT(uncompressed.span() == original.span().slice(0, uncompressed.size()));- EXPECT(uncompressed == original);-}--TEST_CASE(deflate_compress_literals)-{- // This byte array is known to not produce any back references with our lz77 implementation even at the highest compression settings- Array<u8, 0x13> test { 0, 0, 0, 0, 0x72, 0, 0, 0xee, 0, 0, 0, 0x26, 0, 0, 0, 0x28, 0, 0, 0x72 };- auto compressed = TRY_OR_FAIL(Compress::DeflateCompressor::compress_all(test));-}diff --git a/Tests/LibCompress/TestZlib.cpp b/Tests/LibCompress/TestZlib.cppindex 09e1eb02cb8..1722edcdcd5 100644--- a/Tests/LibCompress/TestZlib.cpp+++ b/Tests/LibCompress/TestZlib.cpp@@ -44,39 +44,6 @@ TEST_CASE(zlib_decompress_stream)EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));}-TEST_CASE(zlib_round_trip_simple_default)-{- u8 const uncompressed[] = "This is a simple text file :)";-- auto const freshly_pressed = TRY_OR_FAIL(Compress::ZlibCompressor::compress_all({ uncompressed, sizeof(uncompressed) - 1 }, Compress::GenericZlibCompressionLevel::Default));- EXPECT(freshly_pressed.span().slice(0, 2) == ReadonlyBytes { { 0x78, 0x9C } });-- auto const decompressed = TRY_OR_FAIL(Compress::ZlibDecompressor::decompress_all(freshly_pressed));- EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));-}--TEST_CASE(zlib_round_trip_simple_best)-{- u8 const uncompressed[] = "This is a simple text file :)";-- auto const freshly_pressed = TRY_OR_FAIL(Compress::ZlibCompressor::compress_all({ uncompressed, sizeof(uncompressed) - 1 }, Compress::GenericZlibCompressionLevel::Best));- EXPECT(freshly_pressed.span().slice(0, 2) == ReadonlyBytes { { 0x78, 0xDA } });-- auto const decompressed = TRY_OR_FAIL(Compress::ZlibDecompressor::decompress_all(freshly_pressed));- EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));-}--TEST_CASE(zlib_round_trip_simple_fastest)-{- u8 const uncompressed[] = "This is a simple text file :)";-- auto const freshly_pressed = TRY_OR_FAIL(Compress::ZlibCompressor::compress_all({ uncompressed, sizeof(uncompressed) - 1 }, Compress::GenericZlibCompressionLevel::Fastest));- EXPECT(freshly_pressed.span().slice(0, 2) == ReadonlyBytes { { 0x78, 0x01 } });-- auto const decompressed = TRY_OR_FAIL(Compress::ZlibDecompressor::decompress_all(freshly_pressed));- EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));-}-TEST_CASE(zlib_decompress_with_missing_end_bits){// This test case has been extracted from compressed PNG data of `/res/icons/16x16/app-masterword.png`.