Serenity Operating System
at master 247 lines 8.2 kB view raw
1/* 2 * Copyright (c) 2020-2022, the SerenityOS developers. 3 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibCompress/Gzip.h> 9 10#include <AK/DeprecatedString.h> 11#include <AK/MemoryStream.h> 12#include <LibCore/DateTime.h> 13 14namespace Compress { 15 16bool GzipDecompressor::is_likely_compressed(ReadonlyBytes bytes) 17{ 18 return bytes.size() >= 2 && bytes[0] == gzip_magic_1 && bytes[1] == gzip_magic_2; 19} 20 21bool BlockHeader::valid_magic_number() const 22{ 23 return identification_1 == gzip_magic_1 && identification_2 == gzip_magic_2; 24} 25 26bool BlockHeader::supported_by_implementation() const 27{ 28 if (compression_method != 0x08) { 29 // RFC 1952 does not define any compression methods other than deflate. 30 return false; 31 } 32 33 if (flags > Flags::MAX) { 34 // RFC 1952 does not define any more flags. 35 return false; 36 } 37 38 return true; 39} 40 41ErrorOr<NonnullOwnPtr<GzipDecompressor::Member>> GzipDecompressor::Member::construct(BlockHeader header, Stream& stream) 42{ 43 auto deflate_stream = TRY(DeflateDecompressor::construct(MaybeOwned<Stream>(stream))); 44 return TRY(adopt_nonnull_own_or_enomem(new (nothrow) Member(header, move(deflate_stream)))); 45} 46 47GzipDecompressor::Member::Member(BlockHeader header, NonnullOwnPtr<DeflateDecompressor> stream) 48 : m_header(header) 49 , m_stream(move(stream)) 50{ 51} 52 53GzipDecompressor::GzipDecompressor(NonnullOwnPtr<Stream> stream) 54 : m_input_stream(move(stream)) 55{ 56} 57 58GzipDecompressor::~GzipDecompressor() 59{ 60 m_current_member.clear(); 61} 62 63ErrorOr<Bytes> GzipDecompressor::read_some(Bytes bytes) 64{ 65 size_t total_read = 0; 66 while (total_read < bytes.size()) { 67 if (is_eof()) 68 break; 69 70 auto slice = bytes.slice(total_read); 71 72 if (m_current_member) { 73 auto current_slice = TRY(current_member().m_stream->read_some(slice)); 74 current_member().m_checksum.update(current_slice); 75 current_member().m_nread += current_slice.size(); 76 77 if (current_slice.size() < slice.size()) { 78 u32 crc32 = TRY(m_input_stream->read_value<LittleEndian<u32>>()); 79 u32 input_size = TRY(m_input_stream->read_value<LittleEndian<u32>>()); 80 81 if (crc32 != current_member().m_checksum.digest()) 82 return Error::from_string_literal("Stored CRC32 does not match the calculated CRC32 of the current member"); 83 84 if (input_size != current_member().m_nread) 85 return Error::from_string_literal("Input size does not match the number of read bytes"); 86 87 m_current_member.clear(); 88 89 total_read += current_slice.size(); 90 continue; 91 } 92 93 total_read += current_slice.size(); 94 continue; 95 } else { 96 auto current_partial_header_slice = Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset); 97 auto current_partial_header_data = TRY(m_input_stream->read_some(current_partial_header_slice)); 98 m_partial_header_offset += current_partial_header_data.size(); 99 100 if (is_eof()) 101 break; 102 103 if (m_partial_header_offset < sizeof(BlockHeader)) { 104 break; // partial header read 105 } 106 m_partial_header_offset = 0; 107 108 BlockHeader header = *(reinterpret_cast<BlockHeader*>(m_partial_header)); 109 110 if (!header.valid_magic_number()) 111 return Error::from_string_literal("Header does not have a valid magic number"); 112 113 if (!header.supported_by_implementation()) 114 return Error::from_string_literal("Header is not supported by implementation"); 115 116 if (header.flags & Flags::FEXTRA) { 117 u16 subfield_id = TRY(m_input_stream->read_value<LittleEndian<u16>>()); 118 u16 length = TRY(m_input_stream->read_value<LittleEndian<u16>>()); 119 TRY(m_input_stream->discard(length)); 120 (void)subfield_id; 121 } 122 123 auto discard_string = [&]() -> ErrorOr<void> { 124 char next_char; 125 do { 126 next_char = TRY(m_input_stream->read_value<char>()); 127 } while (next_char); 128 129 return {}; 130 }; 131 132 if (header.flags & Flags::FNAME) 133 TRY(discard_string()); 134 135 if (header.flags & Flags::FCOMMENT) 136 TRY(discard_string()); 137 138 if (header.flags & Flags::FHCRC) { 139 u16 crc = TRY(m_input_stream->read_value<LittleEndian<u16>>()); 140 // FIXME: we should probably verify this instead of just assuming it matches 141 (void)crc; 142 } 143 144 m_current_member = TRY(Member::construct(header, *m_input_stream)); 145 continue; 146 } 147 } 148 return bytes.slice(0, total_read); 149} 150 151Optional<DeprecatedString> GzipDecompressor::describe_header(ReadonlyBytes bytes) 152{ 153 if (bytes.size() < sizeof(BlockHeader)) 154 return {}; 155 156 auto& header = *(reinterpret_cast<BlockHeader const*>(bytes.data())); 157 if (!header.valid_magic_number() || !header.supported_by_implementation()) 158 return {}; 159 160 LittleEndian<u32> original_size = *reinterpret_cast<u32 const*>(bytes.offset(bytes.size() - sizeof(u32))); 161 return DeprecatedString::formatted("last modified: {}, original size {}", Core::DateTime::from_timestamp(header.modification_time).to_deprecated_string(), (u32)original_size); 162} 163 164ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes) 165{ 166 auto memory_stream = TRY(try_make<FixedMemoryStream>(bytes)); 167 auto gzip_stream = make<GzipDecompressor>(move(memory_stream)); 168 AllocatingMemoryStream output_stream; 169 170 auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); 171 while (!gzip_stream->is_eof()) { 172 auto const data = TRY(gzip_stream->read_some(buffer)); 173 TRY(output_stream.write_until_depleted(data)); 174 } 175 176 auto output_buffer = TRY(ByteBuffer::create_uninitialized(output_stream.used_buffer_size())); 177 TRY(output_stream.read_until_filled(output_buffer)); 178 return output_buffer; 179} 180 181bool GzipDecompressor::is_eof() const { return m_input_stream->is_eof(); } 182 183ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes) 184{ 185 return Error::from_errno(EBADF); 186} 187 188GzipCompressor::GzipCompressor(MaybeOwned<Stream> stream) 189 : m_output_stream(move(stream)) 190{ 191} 192 193ErrorOr<Bytes> GzipCompressor::read_some(Bytes) 194{ 195 return Error::from_errno(EBADF); 196} 197 198ErrorOr<size_t> GzipCompressor::write_some(ReadonlyBytes bytes) 199{ 200 BlockHeader header; 201 header.identification_1 = 0x1f; 202 header.identification_2 = 0x8b; 203 header.compression_method = 0x08; 204 header.flags = 0; 205 header.modification_time = 0; 206 header.extra_flags = 3; // DEFLATE sets 2 for maximum compression and 4 for minimum compression 207 header.operating_system = 3; // unix 208 TRY(m_output_stream->write_until_depleted({ &header, sizeof(header) })); 209 auto compressed_stream = TRY(DeflateCompressor::construct(MaybeOwned(*m_output_stream))); 210 TRY(compressed_stream->write_until_depleted(bytes)); 211 TRY(compressed_stream->final_flush()); 212 Crypto::Checksum::CRC32 crc32; 213 crc32.update(bytes); 214 LittleEndian<u32> digest = crc32.digest(); 215 LittleEndian<u32> size = bytes.size(); 216 TRY(m_output_stream->write_until_depleted(digest.bytes())); 217 TRY(m_output_stream->write_until_depleted(size.bytes())); 218 return bytes.size(); 219} 220 221bool GzipCompressor::is_eof() const 222{ 223 return true; 224} 225 226bool GzipCompressor::is_open() const 227{ 228 return m_output_stream->is_open(); 229} 230 231void GzipCompressor::close() 232{ 233} 234 235ErrorOr<ByteBuffer> GzipCompressor::compress_all(ReadonlyBytes bytes) 236{ 237 auto output_stream = TRY(try_make<AllocatingMemoryStream>()); 238 GzipCompressor gzip_stream { MaybeOwned<Stream>(*output_stream) }; 239 240 TRY(gzip_stream.write_until_depleted(bytes)); 241 242 auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size())); 243 TRY(output_stream->read_until_filled(buffer.bytes())); 244 return buffer; 245} 246 247}