Gzip.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <AK/MemoryStream.h>
  10. #include <LibCore/DateTime.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, Stream& stream)
  33. {
  34. auto deflate_stream = TRY(DeflateDecompressor::construct(MaybeOwned<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<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_some(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_some(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. // FIXME: This should read the entire span.
  63. LittleEndian<u32> crc32, input_size;
  64. TRY(m_input_stream->read_some(crc32.bytes()));
  65. TRY(m_input_stream->read_some(input_size.bytes()));
  66. if (crc32 != current_member().m_checksum.digest())
  67. return Error::from_string_literal("Stored CRC32 does not match the calculated CRC32 of the current member");
  68. if (input_size != current_member().m_nread)
  69. return Error::from_string_literal("Input size does not match the number of read bytes");
  70. m_current_member.clear();
  71. total_read += current_slice.size();
  72. continue;
  73. }
  74. total_read += current_slice.size();
  75. continue;
  76. } else {
  77. auto current_partial_header_slice = Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset);
  78. auto current_partial_header_data = TRY(m_input_stream->read_some(current_partial_header_slice));
  79. m_partial_header_offset += current_partial_header_data.size();
  80. if (is_eof())
  81. break;
  82. if (m_partial_header_offset < sizeof(BlockHeader)) {
  83. break; // partial header read
  84. }
  85. m_partial_header_offset = 0;
  86. BlockHeader header = *(reinterpret_cast<BlockHeader*>(m_partial_header));
  87. if (!header.valid_magic_number())
  88. return Error::from_string_literal("Header does not have a valid magic number");
  89. if (!header.supported_by_implementation())
  90. return Error::from_string_literal("Header is not supported by implementation");
  91. if (header.flags & Flags::FEXTRA) {
  92. // FIXME: This should read the entire span.
  93. LittleEndian<u16> subfield_id, length;
  94. TRY(m_input_stream->read_some(subfield_id.bytes()));
  95. TRY(m_input_stream->read_some(length.bytes()));
  96. TRY(m_input_stream->discard(length));
  97. }
  98. auto discard_string = [&]() -> ErrorOr<void> {
  99. char next_char;
  100. do {
  101. // FIXME: This should read the entire span.
  102. TRY(m_input_stream->read_some({ &next_char, sizeof(next_char) }));
  103. } while (next_char);
  104. return {};
  105. };
  106. if (header.flags & Flags::FNAME)
  107. TRY(discard_string());
  108. if (header.flags & Flags::FCOMMENT)
  109. TRY(discard_string());
  110. if (header.flags & Flags::FHCRC) {
  111. // FIXME: This should read the entire span.
  112. LittleEndian<u16> crc16;
  113. TRY(m_input_stream->read_some(crc16.bytes()));
  114. // FIXME: we should probably verify this instead of just assuming it matches
  115. }
  116. m_current_member = TRY(Member::construct(header, *m_input_stream));
  117. continue;
  118. }
  119. }
  120. return bytes.slice(0, total_read);
  121. }
  122. Optional<DeprecatedString> GzipDecompressor::describe_header(ReadonlyBytes bytes)
  123. {
  124. if (bytes.size() < sizeof(BlockHeader))
  125. return {};
  126. auto& header = *(reinterpret_cast<BlockHeader const*>(bytes.data()));
  127. if (!header.valid_magic_number() || !header.supported_by_implementation())
  128. return {};
  129. LittleEndian<u32> original_size = *reinterpret_cast<u32 const*>(bytes.offset(bytes.size() - sizeof(u32)));
  130. return DeprecatedString::formatted("last modified: {}, original size {}", Core::DateTime::from_timestamp(header.modification_time).to_deprecated_string(), (u32)original_size);
  131. }
  132. ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
  133. {
  134. auto memory_stream = TRY(try_make<FixedMemoryStream>(bytes));
  135. auto gzip_stream = make<GzipDecompressor>(move(memory_stream));
  136. AllocatingMemoryStream output_stream;
  137. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  138. while (!gzip_stream->is_eof()) {
  139. auto const data = TRY(gzip_stream->read_some(buffer));
  140. TRY(output_stream.write_until_depleted(data));
  141. }
  142. auto output_buffer = TRY(ByteBuffer::create_uninitialized(output_stream.used_buffer_size()));
  143. TRY(output_stream.read_until_filled(output_buffer));
  144. return output_buffer;
  145. }
  146. bool GzipDecompressor::is_eof() const { return m_input_stream->is_eof(); }
  147. ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes)
  148. {
  149. return Error::from_errno(EBADF);
  150. }
  151. GzipCompressor::GzipCompressor(MaybeOwned<Stream> stream)
  152. : m_output_stream(move(stream))
  153. {
  154. }
  155. ErrorOr<Bytes> GzipCompressor::read_some(Bytes)
  156. {
  157. return Error::from_errno(EBADF);
  158. }
  159. ErrorOr<size_t> GzipCompressor::write_some(ReadonlyBytes bytes)
  160. {
  161. BlockHeader header;
  162. header.identification_1 = 0x1f;
  163. header.identification_2 = 0x8b;
  164. header.compression_method = 0x08;
  165. header.flags = 0;
  166. header.modification_time = 0;
  167. header.extra_flags = 3; // DEFLATE sets 2 for maximum compression and 4 for minimum compression
  168. header.operating_system = 3; // unix
  169. TRY(m_output_stream->write_until_depleted({ &header, sizeof(header) }));
  170. auto compressed_stream = TRY(DeflateCompressor::construct(MaybeOwned(*m_output_stream)));
  171. TRY(compressed_stream->write_until_depleted(bytes));
  172. TRY(compressed_stream->final_flush());
  173. Crypto::Checksum::CRC32 crc32;
  174. crc32.update(bytes);
  175. LittleEndian<u32> digest = crc32.digest();
  176. LittleEndian<u32> size = bytes.size();
  177. TRY(m_output_stream->write_until_depleted(digest.bytes()));
  178. TRY(m_output_stream->write_until_depleted(size.bytes()));
  179. return bytes.size();
  180. }
  181. bool GzipCompressor::is_eof() const
  182. {
  183. return true;
  184. }
  185. bool GzipCompressor::is_open() const
  186. {
  187. return m_output_stream->is_open();
  188. }
  189. void GzipCompressor::close()
  190. {
  191. }
  192. ErrorOr<ByteBuffer> GzipCompressor::compress_all(ReadonlyBytes bytes)
  193. {
  194. auto output_stream = TRY(try_make<AllocatingMemoryStream>());
  195. GzipCompressor gzip_stream { MaybeOwned<Stream>(*output_stream) };
  196. TRY(gzip_stream.write_until_depleted(bytes));
  197. auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size()));
  198. TRY(output_stream->read_until_filled(buffer.bytes()));
  199. return buffer;
  200. }
  201. }