LaggedFibonacciGenerator.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-License-Identifier: CC0-1.0
  2. #pragma once
  3. #include <array>
  4. #include <cstddef>
  5. #include "Common/CommonTypes.h"
  6. namespace DiscIO
  7. {
  8. class LaggedFibonacciGenerator
  9. {
  10. public:
  11. static constexpr size_t SEED_SIZE = 17;
  12. // Reconstructs a seed and writes it to seed_out, then returns the number of bytes which can
  13. // be reconstructed using that seed. Can return any number between 0 and size, inclusive.
  14. // data - data_offset must be 4-byte aligned.
  15. static size_t GetSeed(const u8* data, size_t size, size_t data_offset, u32 seed_out[SEED_SIZE]);
  16. // SetSeed must be called before using the functions below
  17. void SetSeed(const u32 seed[SEED_SIZE]);
  18. void SetSeed(const u8 seed[SEED_SIZE * sizeof(u32)]);
  19. // Outputs a number of bytes and advances the internal state by the same amount.
  20. void GetBytes(size_t count, u8* out);
  21. u8 GetByte();
  22. // Advances the internal state like GetBytes, but without outputting data. O(N), like GetBytes.
  23. void Forward(size_t count);
  24. private:
  25. static bool GetSeed(const u32* data, size_t size, size_t data_offset,
  26. LaggedFibonacciGenerator* lfg, u32 seed_out[SEED_SIZE]);
  27. void Forward();
  28. void Backward(size_t start_word = 0, size_t end_word = LFG_K);
  29. bool Reinitialize(u32 seed_out[SEED_SIZE]);
  30. bool Initialize(bool check_existing_data);
  31. static constexpr size_t LFG_K = 521;
  32. static constexpr size_t LFG_J = 32;
  33. std::array<u32, LFG_K> m_buffer{};
  34. size_t m_position_bytes = 0;
  35. };
  36. } // namespace DiscIO