mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-21 09:49:21 +00:00
LibCompress: Refactor zlib de/compressor using zlib
This commit is contained in:
parent
c4b76bea13
commit
dafbe32626
Notes:
github-actions[bot]
2025-03-19 12:48:52 +00:00
Author: https://github.com/devgianlu
Commit: dafbe32626
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3755
Reviewed-by: https://github.com/gmta ✅
Reviewed-by: https://github.com/trflynn89
4 changed files with 63 additions and 244 deletions
|
@ -4,11 +4,11 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/MemoryStream.h>
|
||||
#include <LibCompress/Zlib.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
TEST_CASE(zlib_decompress_simple)
|
||||
{
|
||||
|
@ -21,9 +21,7 @@ TEST_CASE(zlib_decompress_simple)
|
|||
|
||||
u8 const uncompressed[] = "This is a simple text file :)";
|
||||
|
||||
auto stream = make<FixedMemoryStream>(compressed);
|
||||
auto decompressor = TRY_OR_FAIL(Compress::ZlibDecompressor::create(move(stream)));
|
||||
auto decompressed = TRY_OR_FAIL(decompressor->read_until_eof());
|
||||
auto decompressed = TRY_OR_FAIL(Compress::ZlibDecompressor::decompress_all(compressed));
|
||||
EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));
|
||||
}
|
||||
|
||||
|
@ -46,31 +44,43 @@ TEST_CASE(zlib_decompress_stream)
|
|||
EXPECT(decompressed.bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));
|
||||
}
|
||||
|
||||
TEST_CASE(zlib_compress_simple)
|
||||
TEST_CASE(zlib_round_trip_simple_default)
|
||||
{
|
||||
// Note: This is just the output of our compression function from an arbitrary point in time.
|
||||
// This test is intended to ensure that the decompression doesn't change unintentionally,
|
||||
// it does not make any guarantees for correctness.
|
||||
|
||||
Array<u8, 37> const compressed {
|
||||
0x78, 0x9C, 0x0B, 0xC9, 0xC8, 0x2C, 0x56, 0xC8, 0x2C, 0x56, 0x48, 0x54,
|
||||
0x28, 0xCE, 0xCC, 0x2D, 0xC8, 0x49, 0x55, 0x28, 0x49, 0xAD, 0x28, 0x51,
|
||||
0x48, 0xCB, 0xCC, 0x49, 0x55, 0xB0, 0xD2, 0x04, 0x00, 0x99, 0x5E, 0x09,
|
||||
0xE8
|
||||
};
|
||||
|
||||
u8 const uncompressed[] = "This is a simple text file :)";
|
||||
|
||||
auto const freshly_pressed = Compress::ZlibCompressor::compress_all({ uncompressed, sizeof(uncompressed) - 1 });
|
||||
EXPECT(freshly_pressed.value().bytes() == compressed.span());
|
||||
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`.
|
||||
// The decompression results have been confirmed using the `zlib-flate` tool.
|
||||
// Note: It is unconfirmed whether there are actually bits missing.
|
||||
// However, our decompressor implementation ends up in a weird state nonetheless.
|
||||
|
||||
Array<u8, 72> const compressed {
|
||||
0x08, 0xD7, 0x63, 0x30, 0x86, 0x00, 0x01, 0x06, 0x23, 0x25, 0x30, 0x00,
|
||||
|
@ -95,8 +105,6 @@ TEST_CASE(zlib_decompress_with_missing_end_bits)
|
|||
0x44, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
auto stream = make<FixedMemoryStream>(compressed);
|
||||
auto decompressor = TRY_OR_FAIL(Compress::ZlibDecompressor::create(move(stream)));
|
||||
auto decompressed = TRY_OR_FAIL(decompressor->read_until_eof());
|
||||
auto decompressed = TRY_OR_FAIL(Compress::ZlibDecompressor::decompress_all(compressed));
|
||||
EXPECT_EQ(decompressed.span(), uncompressed.span());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue