summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2023-01-03 18:59:27 +0100
committerAndreas Kling <kling@serenityos.org>2023-01-10 10:28:26 +0100
commitf4afee4278f681ad788f781e24df73ab22baa1f4 (patch)
treee4f7813bdc010096ec20ac1f7c49535d16f30c7f
parent8cd2cf2b7787a5c0fa844f96a96ea8c67a354bba (diff)
downloadserenity-f4afee4278f681ad788f781e24df73ab22baa1f4.zip
LibCompress: Switch `DeflateCompressor` to a fallible constructor
-rw-r--r--Userland/Libraries/LibCompress/Deflate.cpp17
-rw-r--r--Userland/Libraries/LibCompress/Deflate.h4
-rw-r--r--Userland/Libraries/LibCompress/Gzip.cpp6
-rw-r--r--Userland/Libraries/LibCompress/Zlib.cpp2
4 files changed, 19 insertions, 10 deletions
diff --git a/Userland/Libraries/LibCompress/Deflate.cpp b/Userland/Libraries/LibCompress/Deflate.cpp
index cc0f786913..5beeb55ea6 100644
--- a/Userland/Libraries/LibCompress/Deflate.cpp
+++ b/Userland/Libraries/LibCompress/Deflate.cpp
@@ -438,10 +438,17 @@ ErrorOr<void> DeflateDecompressor::decode_codes(CanonicalCode& literal_code, Opt
return {};
}
-DeflateCompressor::DeflateCompressor(Core::Stream::Handle<Core::Stream::Stream> stream, CompressionLevel compression_level)
+ErrorOr<NonnullOwnPtr<DeflateCompressor>> DeflateCompressor::construct(Core::Stream::Handle<Core::Stream::Stream> stream, CompressionLevel compression_level)
+{
+ auto bit_stream = TRY(Core::Stream::LittleEndianOutputBitStream::construct(move(stream)));
+ auto deflate_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateCompressor(move(bit_stream), compression_level)));
+ return deflate_compressor;
+}
+
+DeflateCompressor::DeflateCompressor(NonnullOwnPtr<Core::Stream::LittleEndianOutputBitStream> stream, CompressionLevel compression_level)
: m_compression_level(compression_level)
, m_compression_constants(compression_constants[static_cast<int>(m_compression_level)])
- , m_output_stream(Core::Stream::LittleEndianOutputBitStream::construct(move(stream)).release_value_but_fixme_should_propagate_errors())
+ , m_output_stream(move(stream))
{
m_symbol_frequencies.fill(0);
m_distance_frequencies.fill(0);
@@ -1002,10 +1009,10 @@ ErrorOr<void> DeflateCompressor::final_flush()
ErrorOr<ByteBuffer> DeflateCompressor::compress_all(ReadonlyBytes bytes, CompressionLevel compression_level)
{
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
- DeflateCompressor deflate_stream { Core::Stream::Handle<Core::Stream::Stream>(*output_stream), compression_level };
+ auto deflate_stream = TRY(DeflateCompressor::construct(Core::Stream::Handle<Core::Stream::Stream>(*output_stream), compression_level));
- TRY(deflate_stream.write_entire_buffer(bytes));
- TRY(deflate_stream.final_flush());
+ TRY(deflate_stream->write_entire_buffer(bytes));
+ TRY(deflate_stream->final_flush());
auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size()));
TRY(output_stream->read_entire_buffer(buffer));
diff --git a/Userland/Libraries/LibCompress/Deflate.h b/Userland/Libraries/LibCompress/Deflate.h
index 8cdfaee287..22b60730df 100644
--- a/Userland/Libraries/LibCompress/Deflate.h
+++ b/Userland/Libraries/LibCompress/Deflate.h
@@ -139,7 +139,7 @@ public:
BEST // WARNING: this one can take an unreasonable amount of time!
};
- DeflateCompressor(Core::Stream::Handle<Core::Stream::Stream>, CompressionLevel = CompressionLevel::GOOD);
+ static ErrorOr<NonnullOwnPtr<DeflateCompressor>> construct(Core::Stream::Handle<Core::Stream::Stream>, CompressionLevel = CompressionLevel::GOOD);
~DeflateCompressor();
virtual ErrorOr<Bytes> read(Bytes) override;
@@ -152,6 +152,8 @@ public:
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, CompressionLevel = CompressionLevel::GOOD);
private:
+ DeflateCompressor(NonnullOwnPtr<Core::Stream::LittleEndianOutputBitStream>, CompressionLevel = CompressionLevel::GOOD);
+
Bytes pending_block() { return { m_rolling_window + block_size, block_size }; }
// LZ77 Compression
diff --git a/Userland/Libraries/LibCompress/Gzip.cpp b/Userland/Libraries/LibCompress/Gzip.cpp
index 5675b30eb4..0a6f7dd978 100644
--- a/Userland/Libraries/LibCompress/Gzip.cpp
+++ b/Userland/Libraries/LibCompress/Gzip.cpp
@@ -194,9 +194,9 @@ ErrorOr<size_t> GzipCompressor::write(ReadonlyBytes bytes)
header.extra_flags = 3; // DEFLATE sets 2 for maximum compression and 4 for minimum compression
header.operating_system = 3; // unix
TRY(m_output_stream->write_entire_buffer({ &header, sizeof(header) }));
- DeflateCompressor compressed_stream { Core::Stream::Handle(*m_output_stream) };
- TRY(compressed_stream.write_entire_buffer(bytes));
- TRY(compressed_stream.final_flush());
+ auto compressed_stream = TRY(DeflateCompressor::construct(Core::Stream::Handle(*m_output_stream)));
+ TRY(compressed_stream->write_entire_buffer(bytes));
+ TRY(compressed_stream->final_flush());
Crypto::Checksum::CRC32 crc32;
crc32.update(bytes);
LittleEndian<u32> digest = crc32.digest();
diff --git a/Userland/Libraries/LibCompress/Zlib.cpp b/Userland/Libraries/LibCompress/Zlib.cpp
index 83924b8d5d..c731191e9b 100644
--- a/Userland/Libraries/LibCompress/Zlib.cpp
+++ b/Userland/Libraries/LibCompress/Zlib.cpp
@@ -83,7 +83,7 @@ ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(Core::Stream::H
ZlibCompressor::ZlibCompressor(Core::Stream::Handle<Core::Stream::Stream> stream, ZlibCompressionLevel compression_level)
: m_output_stream(move(stream))
// FIXME: Find a way to compress with Deflate's "Best" compression level.
- , m_compressor(make<DeflateCompressor>(Core::Stream::Handle(*m_output_stream), static_cast<DeflateCompressor::CompressionLevel>(compression_level)))
+ , m_compressor(DeflateCompressor::construct(Core::Stream::Handle(*m_output_stream), static_cast<DeflateCompressor::CompressionLevel>(compression_level)).release_value_but_fixme_should_propagate_errors())
{
}