Gzip.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #pragma once
  8. #include <LibCompress/Deflate.h>
  9. #include <LibCore/Stream.h>
  10. #include <LibCrypto/Checksum/CRC32.h>
  11. namespace Compress {
  12. constexpr u8 gzip_magic_1 = 0x1f;
  13. constexpr u8 gzip_magic_2 = 0x8b;
  14. struct [[gnu::packed]] BlockHeader {
  15. u8 identification_1;
  16. u8 identification_2;
  17. u8 compression_method;
  18. u8 flags;
  19. LittleEndian<u32> modification_time;
  20. u8 extra_flags;
  21. u8 operating_system;
  22. bool valid_magic_number() const;
  23. bool supported_by_implementation() const;
  24. };
  25. struct Flags {
  26. static constexpr u8 FTEXT = 1 << 0;
  27. static constexpr u8 FHCRC = 1 << 1;
  28. static constexpr u8 FEXTRA = 1 << 2;
  29. static constexpr u8 FNAME = 1 << 3;
  30. static constexpr u8 FCOMMENT = 1 << 4;
  31. static constexpr u8 MAX = FTEXT | FHCRC | FEXTRA | FNAME | FCOMMENT;
  32. };
  33. class GzipDecompressor final : public Core::Stream::Stream {
  34. public:
  35. GzipDecompressor(NonnullOwnPtr<Core::Stream::Stream>);
  36. ~GzipDecompressor();
  37. virtual ErrorOr<Bytes> read(Bytes) override;
  38. virtual ErrorOr<size_t> write(ReadonlyBytes) override;
  39. virtual bool is_eof() const override;
  40. virtual bool is_open() const override { return true; }
  41. virtual void close() override {};
  42. static ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes);
  43. static Optional<DeprecatedString> describe_header(ReadonlyBytes);
  44. static bool is_likely_compressed(ReadonlyBytes bytes);
  45. private:
  46. class Member {
  47. public:
  48. Member(BlockHeader header, Core::Stream::Stream& stream)
  49. : m_header(header)
  50. , m_adapted_ak_stream(make<Core::Stream::WrapInAKInputStream>(stream))
  51. , m_stream(*m_adapted_ak_stream)
  52. {
  53. }
  54. BlockHeader m_header;
  55. NonnullOwnPtr<InputStream> m_adapted_ak_stream;
  56. DeflateDecompressor m_stream;
  57. Crypto::Checksum::CRC32 m_checksum;
  58. size_t m_nread { 0 };
  59. };
  60. Member const& current_member() const { return m_current_member.value(); }
  61. Member& current_member() { return m_current_member.value(); }
  62. NonnullOwnPtr<Core::Stream::Stream> m_input_stream;
  63. u8 m_partial_header[sizeof(BlockHeader)];
  64. size_t m_partial_header_offset { 0 };
  65. Optional<Member> m_current_member;
  66. bool m_eof { false };
  67. };
  68. class GzipCompressor final : public OutputStream {
  69. public:
  70. GzipCompressor(OutputStream&);
  71. ~GzipCompressor() = default;
  72. size_t write(ReadonlyBytes) override;
  73. bool write_or_error(ReadonlyBytes) override;
  74. static Optional<ByteBuffer> compress_all(ReadonlyBytes bytes);
  75. private:
  76. OutputStream& m_output_stream;
  77. };
  78. }