ladybird/Userland/Libraries/LibCompress/Zlib.h
Tim Schumacher d5871f5717 AK: Rename Stream::{read,write} to Stream::{read_some,write_some}
Similar to POSIX read, the basic read and write functions of AK::Stream
do not have a lower limit of how much data they read or write (apart
from "none at all").

Rename the functions to "read some [data]" and "write some [data]" (with
"data" being omitted, since everything here is reading and writing data)
to make them sufficiently distinct from the functions that ensure to
use the entire buffer (which should be the go-to function for most
usages).

No functional changes, just a lot of new FIXMEs.
2023-03-13 15:16:20 +00:00

89 lines
2.2 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Endian.h>
#include <AK/MaybeOwned.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <AK/Span.h>
#include <AK/Stream.h>
#include <AK/Types.h>
#include <LibCrypto/Checksum/Adler32.h>
namespace Compress {
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));
class ZlibDecompressor {
public:
Optional<ByteBuffer> decompress();
u32 checksum();
static Optional<ZlibDecompressor> try_create(ReadonlyBytes data);
static Optional<ByteBuffer> decompress_all(ReadonlyBytes);
private:
ZlibDecompressor(ZlibHeader, ReadonlyBytes data);
ZlibHeader m_header;
u32 m_checksum { 0 };
ReadonlyBytes m_input_data;
ReadonlyBytes m_data_bytes;
};
class ZlibCompressor : public Stream {
public:
static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(MaybeOwned<Stream>, ZlibCompressionLevel = ZlibCompressionLevel::Default);
~ZlibCompressor();
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;
ErrorOr<void> finish();
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default);
private:
ZlibCompressor(MaybeOwned<Stream> stream, NonnullOwnPtr<Stream> compressor_stream);
ErrorOr<void> write_header(ZlibCompressionMethod, ZlibCompressionLevel);
bool m_finished { false };
MaybeOwned<Stream> m_output_stream;
NonnullOwnPtr<Stream> m_compressor;
Crypto::Checksum::Adler32 m_adler32_checksum;
};
}