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
|
|
|
|
|
2022-06-17 07:26:35 +00:00
|
|
|
#include <AK/BitStream.h>
|
2020-08-26 12:22:25 +00:00
|
|
|
#include <AK/ByteBuffer.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>
|
|
|
|
#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));
|
|
|
|
|
2020-08-01 20:01:39 +00:00
|
|
|
class Zlib {
|
|
|
|
public:
|
2020-09-08 14:49:05 +00:00
|
|
|
Optional<ByteBuffer> decompress();
|
2020-08-01 20:01:39 +00:00
|
|
|
u32 checksum();
|
|
|
|
|
2021-03-15 15:03:42 +00:00
|
|
|
static Optional<Zlib> 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-06-17 07:26:04 +00:00
|
|
|
Zlib(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
|
|
|
|
2022-06-17 07:26:35 +00:00
|
|
|
class ZlibCompressor : public OutputStream {
|
|
|
|
public:
|
|
|
|
ZlibCompressor(OutputStream&, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
|
|
|
~ZlibCompressor();
|
|
|
|
|
|
|
|
size_t write(ReadonlyBytes) override;
|
|
|
|
bool write_or_error(ReadonlyBytes) override;
|
|
|
|
void finish();
|
|
|
|
|
|
|
|
static Optional<ByteBuffer> compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void write_header(ZlibCompressionMethod, ZlibCompressionLevel);
|
|
|
|
|
|
|
|
bool m_finished { false };
|
|
|
|
OutputBitStream m_output_stream;
|
|
|
|
OwnPtr<OutputStream> m_compressor;
|
|
|
|
Crypto::Checksum::Adler32 m_adler32_checksum;
|
|
|
|
};
|
|
|
|
|
2020-08-01 20:01:39 +00:00
|
|
|
}
|