/* * Copyright (c) 2020, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include 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 as_u16; }; }; static_assert(sizeof(ZlibHeader) == sizeof(u16)); class ZlibDecompressor : public Stream { public: static ErrorOr> create(MaybeOwned); virtual ErrorOr read_some(Bytes) override; virtual ErrorOr write_some(ReadonlyBytes) override; virtual bool is_eof() const override; virtual bool is_open() const override; virtual void close() override; private: ZlibDecompressor(ZlibHeader, NonnullOwnPtr); ZlibHeader m_header; NonnullOwnPtr m_stream; }; class ZlibCompressor : public Stream { public: static ErrorOr> construct(MaybeOwned, ZlibCompressionLevel = ZlibCompressionLevel::Default); ~ZlibCompressor(); virtual ErrorOr read_some(Bytes) override; virtual ErrorOr write_some(ReadonlyBytes) override; virtual bool is_eof() const override; virtual bool is_open() const override; virtual void close() override; ErrorOr finish(); static ErrorOr compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default); private: ZlibCompressor(MaybeOwned stream, NonnullOwnPtr compressor_stream); ErrorOr write_header(ZlibCompressionMethod, ZlibCompressionLevel); bool m_finished { false }; MaybeOwned m_output_stream; NonnullOwnPtr m_compressor; Crypto::Checksum::Adler32 m_adler32_checksum; }; } template<> struct AK::Traits : public AK::DefaultTraits { static constexpr bool is_trivially_serializable() { return true; } };