mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:37:35 +00:00
LibCompress: Port ZlibDecompressor
to AK::Stream
This commit is contained in:
parent
90780d9ade
commit
8a853278d0
7 changed files with 96 additions and 68 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BitStream.h>
|
||||
#include <AK/IntegralMath.h>
|
||||
#include <AK/MemoryStream.h>
|
||||
#include <AK/Span.h>
|
||||
|
@ -14,59 +15,53 @@
|
|||
|
||||
namespace Compress {
|
||||
|
||||
constexpr static size_t Adler32Size = sizeof(u32);
|
||||
|
||||
Optional<ZlibDecompressor> ZlibDecompressor::try_create(ReadonlyBytes data)
|
||||
ErrorOr<NonnullOwnPtr<ZlibDecompressor>> ZlibDecompressor::create(MaybeOwned<Stream> stream)
|
||||
{
|
||||
if (data.size() < (sizeof(ZlibHeader) + Adler32Size))
|
||||
return {};
|
||||
|
||||
ZlibHeader header { .as_u16 = data.at(0) << 8 | data.at(1) };
|
||||
auto header = TRY(stream->read_value<ZlibHeader>());
|
||||
|
||||
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)
|
||||
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)
|
||||
return {}; // error correction code doesn't match
|
||||
return Error::from_string_literal("Zlib error correction code does not match");
|
||||
|
||||
ZlibDecompressor zlib { header, data };
|
||||
zlib.m_data_bytes = data.slice(2, data.size() - sizeof(ZlibHeader) - Adler32Size);
|
||||
return zlib;
|
||||
auto bit_stream = make<LittleEndianInputBitStream>(move(stream));
|
||||
auto deflate_stream = TRY(Compress::DeflateDecompressor::construct(move(bit_stream)));
|
||||
|
||||
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_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);
|
||||
if (buffer_or_error.is_error())
|
||||
return {};
|
||||
return buffer_or_error.release_value();
|
||||
return m_stream->read_some(bytes);
|
||||
}
|
||||
|
||||
Optional<ByteBuffer> ZlibDecompressor::decompress_all(ReadonlyBytes bytes)
|
||||
ErrorOr<size_t> ZlibDecompressor::write_some(ReadonlyBytes)
|
||||
{
|
||||
auto zlib = try_create(bytes);
|
||||
if (!zlib.has_value())
|
||||
return {};
|
||||
return zlib->decompress();
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
u32 ZlibDecompressor::checksum()
|
||||
bool ZlibDecompressor::is_eof() const
|
||||
{
|
||||
if (!m_checksum) {
|
||||
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_stream->is_eof();
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue