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));
|
|
|
|
|
2022-12-26 14:51:51 +00:00
|
|
|
class ZlibDecompressor {
|
2020-08-01 20:01:39 +00:00
|
|
|
public:
|
2020-09-08 14:49:05 +00:00
|
|
|
Optional<ByteBuffer> decompress();
|
2020-08-01 20:01:39 +00:00
|
|
|
u32 checksum();
|
|
|
|
|
2022-12-26 14:51:51 +00:00
|
|
|
static Optional<ZlibDecompressor> try_create(ReadonlyBytes data);
|
2020-09-08 14:49:05 +00:00
|
|
|
static Optional<ByteBuffer> decompress_all(ReadonlyBytes);
|
2020-08-26 12:22:25 +00:00
|
|
|
|
2020-08-01 20:01:39 +00:00
|
|
|
private:
|
2022-12-26 14:51:51 +00:00
|
|
|
ZlibDecompressor(ZlibHeader, ReadonlyBytes data);
|
2021-03-15 15:03:42 +00:00
|
|
|
|
2022-06-17 07:26:04 +00:00
|
|
|
ZlibHeader m_header;
|
2020-08-01 20:01:39 +00:00
|
|
|
|
2021-03-15 15:03:42 +00:00
|
|
|
u32 m_checksum { 0 };
|
2020-08-01 20:01:39 +00:00
|
|
|
ReadonlyBytes m_input_data;
|
|
|
|
ReadonlyBytes m_data_bytes;
|
|
|
|
};
|
2020-08-18 18:49:59 +00:00
|
|
|
|
2023-01-22 04:09:11 +00:00
|
|
|
class ZlibCompressor : public AK::Stream {
|
2022-06-17 07:26:35 +00:00
|
|
|
public:
|
2023-01-22 04:09:11 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(MaybeOwned<AK::Stream>, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
2022-06-17 07:26:35 +00:00
|
|
|
~ZlibCompressor();
|
|
|
|
|
2022-12-26 17:42:39 +00:00
|
|
|
virtual ErrorOr<Bytes> read(Bytes) override;
|
|
|
|
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
|
|
|
|
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-01-22 04:09:11 +00:00
|
|
|
ZlibCompressor(MaybeOwned<AK::Stream> stream, NonnullOwnPtr<AK::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-01-22 04:09:11 +00:00
|
|
|
MaybeOwned<AK::Stream> m_output_stream;
|
|
|
|
NonnullOwnPtr<AK::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
|
|
|
}
|