Gzip.cpp 8.1 KB

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