2020-08-28 15:53:57 +00:00
|
|
|
/*
|
2022-02-26 16:06:40 +00:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2021-04-22 20:40:43 +00:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2020-08-28 15:53:57 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-28 15:53:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCompress/Gzip.h>
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-01-25 19:19:05 +00:00
|
|
|
#include <AK/MemoryStream.h>
|
2021-05-07 20:40:02 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2020-08-28 15:53:57 +00:00
|
|
|
|
|
|
|
namespace Compress {
|
|
|
|
|
2021-03-03 21:54:07 +00:00
|
|
|
bool GzipDecompressor::is_likely_compressed(ReadonlyBytes bytes)
|
|
|
|
{
|
|
|
|
return bytes.size() >= 2 && bytes[0] == gzip_magic_1 && bytes[1] == gzip_magic_2;
|
|
|
|
}
|
|
|
|
|
2021-03-12 23:26:44 +00:00
|
|
|
bool BlockHeader::valid_magic_number() const
|
2020-08-28 15:53:57 +00:00
|
|
|
{
|
2021-03-03 21:54:07 +00:00
|
|
|
return identification_1 == gzip_magic_1 && identification_2 == gzip_magic_2;
|
2020-08-28 15:53:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 23:26:44 +00:00
|
|
|
bool BlockHeader::supported_by_implementation() const
|
2020-08-28 15:53:57 +00:00
|
|
|
{
|
|
|
|
if (compression_method != 0x08) {
|
|
|
|
// RFC 1952 does not define any compression methods other than deflate.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags > Flags::MAX) {
|
|
|
|
// RFC 1952 does not define any more flags.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<GzipDecompressor::Member>> GzipDecompressor::Member::construct(BlockHeader header, Stream& stream)
|
2022-12-31 23:38:05 +00:00
|
|
|
{
|
2023-02-10 00:00:18 +00:00
|
|
|
auto deflate_stream = TRY(DeflateDecompressor::construct(MaybeOwned<Stream>(stream)));
|
2022-12-31 23:38:05 +00:00
|
|
|
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) Member(header, move(deflate_stream))));
|
|
|
|
}
|
|
|
|
|
|
|
|
GzipDecompressor::Member::Member(BlockHeader header, NonnullOwnPtr<DeflateDecompressor> stream)
|
|
|
|
: m_header(header)
|
|
|
|
, m_stream(move(stream))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
GzipDecompressor::GzipDecompressor(NonnullOwnPtr<Stream> stream)
|
2022-11-29 14:43:31 +00:00
|
|
|
: m_input_stream(move(stream))
|
2020-08-28 15:53:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GzipDecompressor::~GzipDecompressor()
|
|
|
|
{
|
|
|
|
m_current_member.clear();
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:38:01 +00:00
|
|
|
ErrorOr<Bytes> GzipDecompressor::read_some(Bytes bytes)
|
2020-08-28 15:53:57 +00:00
|
|
|
{
|
2021-03-21 13:13:19 +00:00
|
|
|
size_t total_read = 0;
|
|
|
|
while (total_read < bytes.size()) {
|
2022-11-29 14:43:31 +00:00
|
|
|
if (is_eof())
|
2021-03-21 13:13:19 +00:00
|
|
|
break;
|
2020-09-05 14:39:56 +00:00
|
|
|
|
2021-03-21 13:13:19 +00:00
|
|
|
auto slice = bytes.slice(total_read);
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2022-12-31 23:38:05 +00:00
|
|
|
if (m_current_member) {
|
2023-02-24 21:38:01 +00:00
|
|
|
auto current_slice = TRY(current_member().m_stream->read_some(slice));
|
2022-12-02 21:01:44 +00:00
|
|
|
current_member().m_checksum.update(current_slice);
|
|
|
|
current_member().m_nread += current_slice.size();
|
2020-09-05 11:18:01 +00:00
|
|
|
|
2022-12-02 21:01:44 +00:00
|
|
|
if (current_slice.size() < slice.size()) {
|
2023-02-24 21:38:01 +00:00
|
|
|
// FIXME: This should read the entire span.
|
2021-03-21 13:13:19 +00:00
|
|
|
LittleEndian<u32> crc32, input_size;
|
2023-02-24 21:38:01 +00:00
|
|
|
TRY(m_input_stream->read_some(crc32.bytes()));
|
|
|
|
TRY(m_input_stream->read_some(input_size.bytes()));
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
if (crc32 != current_member().m_checksum.digest())
|
|
|
|
return Error::from_string_literal("Stored CRC32 does not match the calculated CRC32 of the current member");
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
if (input_size != current_member().m_nread)
|
|
|
|
return Error::from_string_literal("Input size does not match the number of read bytes");
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2021-03-21 13:13:19 +00:00
|
|
|
m_current_member.clear();
|
2020-09-13 10:24:17 +00:00
|
|
|
|
2022-12-02 21:01:44 +00:00
|
|
|
total_read += current_slice.size();
|
2021-03-21 13:13:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-16 15:39:50 +00:00
|
|
|
|
2022-12-02 21:01:44 +00:00
|
|
|
total_read += current_slice.size();
|
2021-03-21 13:13:19 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2022-11-29 14:43:31 +00:00
|
|
|
auto current_partial_header_slice = Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset);
|
2023-02-24 21:38:01 +00:00
|
|
|
auto current_partial_header_data = TRY(m_input_stream->read_some(current_partial_header_slice));
|
2022-11-29 14:43:31 +00:00
|
|
|
m_partial_header_offset += current_partial_header_data.size();
|
2021-03-16 15:39:50 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
if (is_eof())
|
2021-03-21 13:13:19 +00:00
|
|
|
break;
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2021-03-21 13:13:19 +00:00
|
|
|
if (m_partial_header_offset < sizeof(BlockHeader)) {
|
|
|
|
break; // partial header read
|
|
|
|
}
|
|
|
|
m_partial_header_offset = 0;
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2021-03-21 13:13:19 +00:00
|
|
|
BlockHeader header = *(reinterpret_cast<BlockHeader*>(m_partial_header));
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
if (!header.valid_magic_number())
|
|
|
|
return Error::from_string_literal("Header does not have a valid magic number");
|
|
|
|
|
|
|
|
if (!header.supported_by_implementation())
|
|
|
|
return Error::from_string_literal("Header is not supported by implementation");
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2021-03-21 13:13:19 +00:00
|
|
|
if (header.flags & Flags::FEXTRA) {
|
2023-02-24 21:38:01 +00:00
|
|
|
// FIXME: This should read the entire span.
|
2021-03-21 13:13:19 +00:00
|
|
|
LittleEndian<u16> subfield_id, length;
|
2023-02-24 21:38:01 +00:00
|
|
|
TRY(m_input_stream->read_some(subfield_id.bytes()));
|
|
|
|
TRY(m_input_stream->read_some(length.bytes()));
|
2022-11-29 14:43:31 +00:00
|
|
|
TRY(m_input_stream->discard(length));
|
2021-03-21 13:13:19 +00:00
|
|
|
}
|
2021-03-03 21:54:07 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
auto discard_string = [&]() -> ErrorOr<void> {
|
2021-05-17 21:26:16 +00:00
|
|
|
char next_char;
|
|
|
|
do {
|
2023-02-24 21:38:01 +00:00
|
|
|
// FIXME: This should read the entire span.
|
|
|
|
TRY(m_input_stream->read_some({ &next_char, sizeof(next_char) }));
|
2021-05-17 21:26:16 +00:00
|
|
|
} while (next_char);
|
2022-11-29 14:43:31 +00:00
|
|
|
|
|
|
|
return {};
|
2021-05-17 21:26:16 +00:00
|
|
|
};
|
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
if (header.flags & Flags::FNAME)
|
|
|
|
TRY(discard_string());
|
2021-03-21 13:13:19 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
if (header.flags & Flags::FCOMMENT)
|
|
|
|
TRY(discard_string());
|
2021-03-21 13:13:19 +00:00
|
|
|
|
|
|
|
if (header.flags & Flags::FHCRC) {
|
2023-02-24 21:38:01 +00:00
|
|
|
// FIXME: This should read the entire span.
|
2021-03-21 13:13:19 +00:00
|
|
|
LittleEndian<u16> crc16;
|
2023-02-24 21:38:01 +00:00
|
|
|
TRY(m_input_stream->read_some(crc16.bytes()));
|
2021-03-21 13:13:19 +00:00
|
|
|
// FIXME: we should probably verify this instead of just assuming it matches
|
|
|
|
}
|
|
|
|
|
2022-12-31 23:38:05 +00:00
|
|
|
m_current_member = TRY(Member::construct(header, *m_input_stream));
|
2021-03-21 13:13:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-28 15:53:57 +00:00
|
|
|
}
|
2022-11-29 14:43:31 +00:00
|
|
|
return bytes.slice(0, total_read);
|
2020-08-28 15:53:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Optional<DeprecatedString> GzipDecompressor::describe_header(ReadonlyBytes bytes)
|
2021-05-07 20:40:02 +00:00
|
|
|
{
|
|
|
|
if (bytes.size() < sizeof(BlockHeader))
|
|
|
|
return {};
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
auto& header = *(reinterpret_cast<BlockHeader const*>(bytes.data()));
|
2021-05-07 20:40:02 +00:00
|
|
|
if (!header.valid_magic_number() || !header.supported_by_implementation())
|
|
|
|
return {};
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
LittleEndian<u32> original_size = *reinterpret_cast<u32 const*>(bytes.offset(bytes.size() - sizeof(u32)));
|
2022-12-06 01:12:49 +00:00
|
|
|
return DeprecatedString::formatted("last modified: {}, original size {}", Core::DateTime::from_timestamp(header.modification_time).to_deprecated_string(), (u32)original_size);
|
2021-05-07 20:40:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
|
2020-08-28 15:53:57 +00:00
|
|
|
{
|
2023-01-30 10:05:43 +00:00
|
|
|
auto memory_stream = TRY(try_make<FixedMemoryStream>(bytes));
|
2022-11-29 14:43:31 +00:00
|
|
|
auto gzip_stream = make<GzipDecompressor>(move(memory_stream));
|
2023-01-25 19:19:05 +00:00
|
|
|
AllocatingMemoryStream output_stream;
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
|
|
|
while (!gzip_stream->is_eof()) {
|
2023-02-24 21:38:01 +00:00
|
|
|
auto const data = TRY(gzip_stream->read_some(buffer));
|
2022-12-31 23:55:23 +00:00
|
|
|
TRY(output_stream.write_entire_buffer(data));
|
2020-08-28 15:53:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-31 23:55:23 +00:00
|
|
|
auto output_buffer = TRY(ByteBuffer::create_uninitialized(output_stream.used_buffer_size()));
|
|
|
|
TRY(output_stream.read_entire_buffer(output_buffer));
|
|
|
|
return output_buffer;
|
2020-08-28 15:53:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
bool GzipDecompressor::is_eof() const { return m_input_stream->is_eof(); }
|
2020-08-28 15:53:57 +00:00
|
|
|
|
2023-02-24 21:38:01 +00:00
|
|
|
ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes)
|
2021-03-16 13:20:46 +00:00
|
|
|
{
|
2022-12-14 13:30:58 +00:00
|
|
|
return Error::from_errno(EBADF);
|
2021-03-16 13:20:46 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
GzipCompressor::GzipCompressor(MaybeOwned<Stream> stream)
|
2022-12-27 10:40:23 +00:00
|
|
|
: m_output_stream(move(stream))
|
2021-03-12 23:26:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:38:01 +00:00
|
|
|
ErrorOr<Bytes> GzipCompressor::read_some(Bytes)
|
2022-12-27 10:40:23 +00:00
|
|
|
{
|
|
|
|
return Error::from_errno(EBADF);
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:38:01 +00:00
|
|
|
ErrorOr<size_t> GzipCompressor::write_some(ReadonlyBytes bytes)
|
2021-03-12 23:26:44 +00:00
|
|
|
{
|
|
|
|
BlockHeader header;
|
|
|
|
header.identification_1 = 0x1f;
|
|
|
|
header.identification_2 = 0x8b;
|
|
|
|
header.compression_method = 0x08;
|
|
|
|
header.flags = 0;
|
|
|
|
header.modification_time = 0;
|
|
|
|
header.extra_flags = 3; // DEFLATE sets 2 for maximum compression and 4 for minimum compression
|
|
|
|
header.operating_system = 3; // unix
|
2022-12-27 10:40:23 +00:00
|
|
|
TRY(m_output_stream->write_entire_buffer({ &header, sizeof(header) }));
|
2023-01-22 03:24:18 +00:00
|
|
|
auto compressed_stream = TRY(DeflateCompressor::construct(MaybeOwned(*m_output_stream)));
|
2023-01-03 17:59:27 +00:00
|
|
|
TRY(compressed_stream->write_entire_buffer(bytes));
|
|
|
|
TRY(compressed_stream->final_flush());
|
2021-03-12 23:26:44 +00:00
|
|
|
Crypto::Checksum::CRC32 crc32;
|
|
|
|
crc32.update(bytes);
|
|
|
|
LittleEndian<u32> digest = crc32.digest();
|
|
|
|
LittleEndian<u32> size = bytes.size();
|
2022-12-27 10:40:23 +00:00
|
|
|
TRY(m_output_stream->write_entire_buffer(digest.bytes()));
|
|
|
|
TRY(m_output_stream->write_entire_buffer(size.bytes()));
|
2021-03-12 23:26:44 +00:00
|
|
|
return bytes.size();
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:40:23 +00:00
|
|
|
bool GzipCompressor::is_eof() const
|
2021-03-12 23:26:44 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:40:23 +00:00
|
|
|
bool GzipCompressor::is_open() const
|
2021-03-12 23:26:44 +00:00
|
|
|
{
|
2022-12-27 10:40:23 +00:00
|
|
|
return m_output_stream->is_open();
|
|
|
|
}
|
2021-03-12 23:26:44 +00:00
|
|
|
|
2022-12-27 10:40:23 +00:00
|
|
|
void GzipCompressor::close()
|
|
|
|
{
|
|
|
|
}
|
2021-03-12 23:26:44 +00:00
|
|
|
|
2022-12-27 10:40:23 +00:00
|
|
|
ErrorOr<ByteBuffer> GzipCompressor::compress_all(ReadonlyBytes bytes)
|
|
|
|
{
|
2023-01-25 19:19:05 +00:00
|
|
|
auto output_stream = TRY(try_make<AllocatingMemoryStream>());
|
2023-02-10 00:00:18 +00:00
|
|
|
GzipCompressor gzip_stream { MaybeOwned<Stream>(*output_stream) };
|
2021-03-12 23:26:44 +00:00
|
|
|
|
2022-12-27 10:40:23 +00:00
|
|
|
TRY(gzip_stream.write_entire_buffer(bytes));
|
|
|
|
|
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size()));
|
|
|
|
TRY(output_stream->read_entire_buffer(buffer.bytes()));
|
|
|
|
return buffer;
|
2021-03-12 23:26:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 15:53:57 +00:00
|
|
|
}
|