WIACompression.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2020 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstddef>
  5. #include <memory>
  6. #include <optional>
  7. #include <vector>
  8. #include <bzlib.h>
  9. #include <lzma.h>
  10. #include <zstd.h>
  11. #include "Common/CommonTypes.h"
  12. #include "Common/Crypto/SHA1.h"
  13. #include "DiscIO/LaggedFibonacciGenerator.h"
  14. namespace DiscIO
  15. {
  16. struct DecompressionBuffer
  17. {
  18. std::vector<u8> data;
  19. size_t bytes_written = 0;
  20. };
  21. struct PurgeSegment
  22. {
  23. u32 offset;
  24. u32 size;
  25. };
  26. static_assert(sizeof(PurgeSegment) == 0x08, "Wrong size for WIA purge segment");
  27. class Decompressor
  28. {
  29. public:
  30. virtual ~Decompressor();
  31. virtual bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  32. size_t* in_bytes_read) = 0;
  33. virtual bool Done() const { return m_done; }
  34. protected:
  35. bool m_done = false;
  36. };
  37. class NoneDecompressor final : public Decompressor
  38. {
  39. public:
  40. bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  41. size_t* in_bytes_read) override;
  42. };
  43. // This class assumes that more bytes won't be added to in once in.bytes_written == in.data.size()
  44. // and that *in_bytes_read initially will be equal to the size of the exception lists
  45. class PurgeDecompressor final : public Decompressor
  46. {
  47. public:
  48. PurgeDecompressor(u64 decompressed_size);
  49. bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  50. size_t* in_bytes_read) override;
  51. private:
  52. const u64 m_decompressed_size;
  53. PurgeSegment m_segment = {};
  54. size_t m_bytes_read = 0;
  55. size_t m_segment_bytes_written = 0;
  56. size_t m_out_bytes_written = 0;
  57. bool m_started = false;
  58. std::unique_ptr<Common::SHA1::Context> m_sha1_context;
  59. };
  60. class Bzip2Decompressor final : public Decompressor
  61. {
  62. public:
  63. ~Bzip2Decompressor();
  64. bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  65. size_t* in_bytes_read) override;
  66. private:
  67. bz_stream m_stream = {};
  68. bool m_started = false;
  69. };
  70. class LZMADecompressor final : public Decompressor
  71. {
  72. public:
  73. LZMADecompressor(bool lzma2, const u8* filter_options, size_t filter_options_size);
  74. ~LZMADecompressor();
  75. bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  76. size_t* in_bytes_read) override;
  77. private:
  78. lzma_stream m_stream = LZMA_STREAM_INIT;
  79. lzma_options_lzma m_options = {};
  80. lzma_filter m_filters[2];
  81. bool m_started = false;
  82. bool m_error_occurred = false;
  83. };
  84. class ZstdDecompressor final : public Decompressor
  85. {
  86. public:
  87. ZstdDecompressor();
  88. ~ZstdDecompressor();
  89. bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  90. size_t* in_bytes_read) override;
  91. private:
  92. ZSTD_DStream* m_stream;
  93. };
  94. class RVZPackDecompressor final : public Decompressor
  95. {
  96. public:
  97. RVZPackDecompressor(std::unique_ptr<Decompressor> decompressor, DecompressionBuffer decompressed,
  98. u64 data_offset, u32 rvz_packed_size);
  99. bool Decompress(const DecompressionBuffer& in, DecompressionBuffer* out,
  100. size_t* in_bytes_read) override;
  101. bool Done() const override;
  102. private:
  103. bool IncrementBytesRead(size_t x);
  104. std::optional<bool> ReadToDecompressed(const DecompressionBuffer& in, size_t* in_bytes_read,
  105. size_t decompressed_bytes_read, size_t bytes_to_read);
  106. std::unique_ptr<Decompressor> m_decompressor;
  107. DecompressionBuffer m_decompressed;
  108. size_t m_decompressed_bytes_read = 0;
  109. size_t m_bytes_read;
  110. u64 m_data_offset;
  111. u32 m_rvz_packed_size;
  112. u32 m_size = 0;
  113. bool m_junk = false;
  114. LaggedFibonacciGenerator m_lfg;
  115. };
  116. class Compressor
  117. {
  118. public:
  119. virtual ~Compressor();
  120. // First call Start, then AddDataOnlyForPurgeHashing/Compress any number of times,
  121. // then End, then GetData/GetSize any number of times.
  122. virtual bool Start(std::optional<u64> size) = 0;
  123. virtual bool AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) { return true; }
  124. virtual bool Compress(const u8* data, size_t size) = 0;
  125. virtual bool End() = 0;
  126. virtual const u8* GetData() const = 0;
  127. virtual size_t GetSize() const = 0;
  128. };
  129. class PurgeCompressor final : public Compressor
  130. {
  131. public:
  132. PurgeCompressor();
  133. ~PurgeCompressor();
  134. bool Start(std::optional<u64> size) override;
  135. bool AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) override;
  136. bool Compress(const u8* data, size_t size) override;
  137. bool End() override;
  138. const u8* GetData() const override;
  139. size_t GetSize() const override;
  140. private:
  141. std::vector<u8> m_buffer;
  142. size_t m_bytes_written = 0;
  143. std::unique_ptr<Common::SHA1::Context> m_sha1_context;
  144. };
  145. class Bzip2Compressor final : public Compressor
  146. {
  147. public:
  148. Bzip2Compressor(int compression_level);
  149. ~Bzip2Compressor();
  150. bool Start(std::optional<u64> size) override;
  151. bool Compress(const u8* data, size_t size) override;
  152. bool End() override;
  153. const u8* GetData() const override;
  154. size_t GetSize() const override;
  155. private:
  156. void ExpandBuffer(size_t bytes_to_add);
  157. bz_stream m_stream = {};
  158. std::vector<u8> m_buffer;
  159. int m_compression_level;
  160. };
  161. class LZMACompressor final : public Compressor
  162. {
  163. public:
  164. LZMACompressor(bool lzma2, int compression_level, u8 compressor_data_out[7],
  165. u8* compressor_data_size_out);
  166. ~LZMACompressor();
  167. bool Start(std::optional<u64> size) override;
  168. bool Compress(const u8* data, size_t size) override;
  169. bool End() override;
  170. const u8* GetData() const override;
  171. size_t GetSize() const override;
  172. private:
  173. void ExpandBuffer(size_t bytes_to_add);
  174. lzma_stream m_stream = LZMA_STREAM_INIT;
  175. lzma_options_lzma m_options = {};
  176. lzma_filter m_filters[2];
  177. std::vector<u8> m_buffer;
  178. bool m_initialization_failed = false;
  179. };
  180. class ZstdCompressor final : public Compressor
  181. {
  182. public:
  183. ZstdCompressor(int compression_level);
  184. ~ZstdCompressor();
  185. bool Start(std::optional<u64> size) override;
  186. bool Compress(const u8* data, size_t size) override;
  187. bool End() override;
  188. const u8* GetData() const override { return m_buffer.data(); }
  189. size_t GetSize() const override { return m_out_buffer.pos; }
  190. private:
  191. void ExpandBuffer(size_t bytes_to_add);
  192. ZSTD_CStream* m_stream;
  193. ZSTD_outBuffer m_out_buffer{};
  194. std::vector<u8> m_buffer;
  195. };
  196. } // namespace DiscIO