Lzma2.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/CircularBuffer.h>
  8. #include <AK/MaybeOwned.h>
  9. #include <AK/Stream.h>
  10. #include <LibCompress/Lzma.h>
  11. namespace Compress {
  12. // This is based on the human-language description of the LZMA2 format on the English Wikipedia.
  13. // https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Markov_chain_algorithm#LZMA2_format
  14. class Lzma2Decompressor : public Stream {
  15. public:
  16. /// Creates a decompressor that does not require the leading byte indicating the dictionary size.
  17. static ErrorOr<NonnullOwnPtr<Lzma2Decompressor>> create_from_raw_stream(MaybeOwned<Stream>, u32 dictionary_size);
  18. virtual ErrorOr<Bytes> read_some(Bytes) override;
  19. virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
  20. virtual bool is_eof() const override;
  21. virtual bool is_open() const override;
  22. virtual void close() override;
  23. private:
  24. Lzma2Decompressor(MaybeOwned<Stream>, CircularBuffer dictionary);
  25. MaybeOwned<Stream> m_stream;
  26. CircularBuffer m_dictionary;
  27. // Our dictionary is always initialized, but LZMA2 requires that the first chunk resets the dictionary.
  28. bool m_dictionary_initialized { false };
  29. bool m_found_end_of_stream { false };
  30. Optional<MaybeOwned<Stream>> m_current_chunk_stream;
  31. bool m_in_uncompressed_chunk { false };
  32. Optional<NonnullOwnPtr<LzmaDecompressor>> m_last_lzma_stream;
  33. Optional<LzmaDecompressorOptions> m_last_lzma_options;
  34. };
  35. }