2020-08-01 22:01:39 +02:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2021-04-22 23:40:43 +03:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2025-03-01 17:41:22 +01:00
|
|
|
* Copyright (c) 2025, Altomani Gianluca <altomanigianluca@gmail.com>
|
2020-08-01 22:01:39 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 22:01:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-05-08 18:57:53 -04:00
|
|
|
#include <AK/BitStream.h>
|
2023-02-09 03:11:50 +01:00
|
|
|
#include <AK/Stream.h>
|
2025-03-01 17:41:22 +01:00
|
|
|
#include <LibCompress/Zlib.h>
|
2020-08-01 22:01:39 +02:00
|
|
|
|
|
|
|
namespace Compress {
|
|
|
|
|
2020-09-10 14:06:50 +02:00
|
|
|
class CanonicalCode {
|
|
|
|
public:
|
|
|
|
CanonicalCode() = default;
|
2023-01-25 20:06:16 +01:00
|
|
|
ErrorOr<u32> read_symbol(LittleEndianInputBitStream&) const;
|
|
|
|
ErrorOr<void> write_symbol(LittleEndianOutputBitStream&, u32) const;
|
2020-08-01 22:01:39 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static CanonicalCode const& fixed_literal_codes();
|
|
|
|
static CanonicalCode const& fixed_distance_codes();
|
2020-08-01 22:01:39 +02:00
|
|
|
|
2023-04-01 19:52:38 -04:00
|
|
|
static ErrorOr<CanonicalCode> from_bytes(ReadonlyBytes);
|
2020-08-31 11:41:44 +02:00
|
|
|
|
2020-09-10 14:06:50 +02:00
|
|
|
private:
|
2023-03-28 14:45:20 -04:00
|
|
|
static constexpr size_t max_allowed_prefixed_code_length = 8;
|
|
|
|
|
|
|
|
struct PrefixTableEntry {
|
|
|
|
u16 symbol_value { 0 };
|
|
|
|
u16 code_length { 0 };
|
|
|
|
};
|
|
|
|
|
2021-03-13 01:17:18 +02:00
|
|
|
// Decompression - indexed by code
|
2023-03-30 14:01:07 -04:00
|
|
|
Vector<u16, 286> m_symbol_codes;
|
|
|
|
Vector<u16, 286> m_symbol_values;
|
2021-03-13 01:17:18 +02:00
|
|
|
|
2023-03-28 14:45:20 -04:00
|
|
|
Array<PrefixTableEntry, 1 << max_allowed_prefixed_code_length> m_prefix_table {};
|
|
|
|
size_t m_max_prefixed_code_length { 0 };
|
|
|
|
|
2021-03-13 01:17:18 +02:00
|
|
|
// Compression - indexed by symbol
|
2023-04-04 11:04:54 -04:00
|
|
|
// Deflate uses a maximum of 288 symbols (maximum of 32 for distances),
|
|
|
|
// but this is also used by webp, which can use up to 256 + 24 + (1 << 11) == 2328 symbols.
|
|
|
|
Vector<u16, 288> m_bit_codes {};
|
|
|
|
Vector<u16, 288> m_bit_code_lengths {};
|
2020-09-10 14:06:50 +02:00
|
|
|
};
|
2020-08-01 22:01:39 +02:00
|
|
|
|
2024-05-08 18:57:53 -04:00
|
|
|
ALWAYS_INLINE ErrorOr<void> CanonicalCode::write_symbol(LittleEndianOutputBitStream& stream, u32 symbol) const
|
|
|
|
{
|
|
|
|
auto code = symbol < m_bit_codes.size() ? m_bit_codes[symbol] : 0u;
|
|
|
|
auto length = symbol < m_bit_code_lengths.size() ? m_bit_code_lengths[symbol] : 0u;
|
|
|
|
TRY(stream.write_bits(code, length));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2025-03-01 17:41:22 +01:00
|
|
|
class DeflateDecompressor final : public GenericZlibDecompressor {
|
2020-08-26 14:22:25 +02:00
|
|
|
public:
|
2025-03-01 17:41:22 +01:00
|
|
|
static ErrorOr<NonnullOwnPtr<DeflateDecompressor>> create(MaybeOwned<Stream>);
|
2022-12-02 22:01:44 +01:00
|
|
|
static ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes);
|
2020-08-26 14:22:25 +02:00
|
|
|
|
|
|
|
private:
|
2025-03-01 17:41:22 +01:00
|
|
|
DeflateDecompressor(AK::FixedArray<u8> buffer, MaybeOwned<Stream> stream, z_stream* zstream)
|
|
|
|
: GenericZlibDecompressor(move(buffer), move(stream), zstream)
|
|
|
|
{
|
|
|
|
}
|
2021-03-13 01:17:18 +02:00
|
|
|
};
|
|
|
|
|
2025-03-01 17:41:22 +01:00
|
|
|
class DeflateCompressor final : public GenericZlibCompressor {
|
2021-03-13 01:17:18 +02:00
|
|
|
public:
|
2025-03-01 17:41:22 +01:00
|
|
|
static ErrorOr<NonnullOwnPtr<DeflateCompressor>> create(MaybeOwned<Stream>, GenericZlibCompressionLevel = GenericZlibCompressionLevel::Default);
|
|
|
|
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes, GenericZlibCompressionLevel = GenericZlibCompressionLevel::Default);
|
2021-03-13 01:17:18 +02:00
|
|
|
|
|
|
|
private:
|
2025-03-01 17:41:22 +01:00
|
|
|
DeflateCompressor(AK::FixedArray<u8> buffer, MaybeOwned<Stream> stream, z_stream* zstream)
|
|
|
|
: GenericZlibCompressor(move(buffer), move(stream), zstream)
|
|
|
|
{
|
|
|
|
}
|
2020-08-01 22:01:39 +02:00
|
|
|
};
|
2020-08-18 20:49:59 +02:00
|
|
|
|
2020-08-01 22:01:39 +02:00
|
|
|
}
|