2020-08-01 20:01:39 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-08-01 20:01:39 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 20:01:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-26 12:22:25 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2023-02-09 02:11:50 +00:00
|
|
|
#include <AK/Endian.h>
|
2023-01-22 03:24:18 +00:00
|
|
|
#include <AK/MaybeOwned.h>
|
2021-05-16 16:59:28 +00:00
|
|
|
#include <AK/Optional.h>
|
2022-06-17 07:26:35 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-08-01 20:01:39 +00:00
|
|
|
#include <AK/Span.h>
|
2023-02-09 02:11:50 +00:00
|
|
|
#include <AK/Stream.h>
|
2020-08-01 20:01:39 +00:00
|
|
|
#include <AK/Types.h>
|
2022-06-17 07:26:35 +00:00
|
|
|
#include <LibCrypto/Checksum/Adler32.h>
|
2020-08-01 20:01:39 +00:00
|
|
|
|
|
|
|
namespace Compress {
|
2020-08-26 12:22:25 +00:00
|
|
|
|
2022-06-17 07:24:28 +00:00
|
|
|
enum class ZlibCompressionMethod : u8 {
|
|
|
|
Deflate = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ZlibCompressionLevel : u8 {
|
|
|
|
Fastest,
|
|
|
|
Fast,
|
|
|
|
Default,
|
|
|
|
Best,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ZlibHeader {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
ZlibCompressionMethod compression_method : 4;
|
|
|
|
u8 compression_info : 4;
|
|
|
|
|
|
|
|
u8 check_bits : 5;
|
|
|
|
bool present_dictionary : 1;
|
|
|
|
ZlibCompressionLevel compression_level : 2;
|
|
|
|
};
|
|
|
|
NetworkOrdered<u16> as_u16;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ZlibHeader) == sizeof(u16));
|
|
|
|
|
2023-06-08 17:35:44 +00:00
|
|
|
class ZlibDecompressor : public Stream {
|
2020-08-01 20:01:39 +00:00
|
|
|
public:
|
2023-06-08 17:35:44 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<ZlibDecompressor>> create(MaybeOwned<Stream>);
|
2020-08-01 20:01:39 +00:00
|
|
|
|
2023-06-08 17:35:44 +00:00
|
|
|
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;
|
2020-08-26 12:22:25 +00:00
|
|
|
|
2020-08-01 20:01:39 +00:00
|
|
|
private:
|
2024-11-18 18:16:55 +00:00
|
|
|
ZlibDecompressor(MaybeOwned<Stream>);
|
2021-03-15 15:03:42 +00:00
|
|
|
|
2024-11-18 18:16:55 +00:00
|
|
|
bool m_has_seen_header { false };
|
|
|
|
MaybeOwned<Stream> m_stream;
|
2020-08-01 20:01:39 +00:00
|
|
|
};
|
2020-08-18 18:49:59 +00:00
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
class ZlibCompressor : public Stream {
|
2022-06-17 07:26:35 +00:00
|
|
|
public:
|
2023-02-10 00:00:18 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(MaybeOwned<Stream>, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
2022-06-17 07:26:35 +00:00
|
|
|
~ZlibCompressor();
|
|
|
|
|
2023-02-24 21:38:01 +00:00
|
|
|
virtual ErrorOr<Bytes> read_some(Bytes) override;
|
|
|
|
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
|
2022-12-26 17:42:39 +00:00
|
|
|
virtual bool is_eof() const override;
|
|
|
|
virtual bool is_open() const override;
|
|
|
|
virtual void close() override;
|
|
|
|
ErrorOr<void> finish();
|
2022-06-17 07:26:35 +00:00
|
|
|
|
2022-12-26 14:58:42 +00:00
|
|
|
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
2022-06-17 07:26:35 +00:00
|
|
|
|
|
|
|
private:
|
2023-02-10 00:00:18 +00:00
|
|
|
ZlibCompressor(MaybeOwned<Stream> stream, NonnullOwnPtr<Stream> compressor_stream);
|
2022-12-26 17:42:39 +00:00
|
|
|
ErrorOr<void> write_header(ZlibCompressionMethod, ZlibCompressionLevel);
|
2022-06-17 07:26:35 +00:00
|
|
|
|
|
|
|
bool m_finished { false };
|
2023-02-10 00:00:18 +00:00
|
|
|
MaybeOwned<Stream> m_output_stream;
|
|
|
|
NonnullOwnPtr<Stream> m_compressor;
|
2022-06-17 07:26:35 +00:00
|
|
|
Crypto::Checksum::Adler32 m_adler32_checksum;
|
|
|
|
};
|
|
|
|
|
2020-08-01 20:01:39 +00:00
|
|
|
}
|
2023-06-08 17:35:44 +00:00
|
|
|
|
|
|
|
template<>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct AK::Traits<Compress::ZlibHeader> : public AK::DefaultTraits<Compress::ZlibHeader> {
|
2023-06-08 17:35:44 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
|
|
|
};
|