| 1 | // SPDX-License-Identifier: CC0-1.0 |
| 2 | |
| 3 | #include "DiscIO/LaggedFibonacciGenerator.h" |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <cstddef> |
| 7 | #include <cstring> |
| 8 | |
| 9 | #include "Common/Align.h" |
| 10 | #include "Common/Assert.h" |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/Swap.h" |
| 13 | |
| 14 | namespace DiscIO |
| 15 | { |
| 16 | void LaggedFibonacciGenerator::SetSeed(const u32 seed[SEED_SIZE]) |
| 17 | { |
| 18 | SetSeed(reinterpret_cast<const u8*>(seed)); |
| 19 | } |
| 20 | |
| 21 | void LaggedFibonacciGenerator::SetSeed(const u8 seed[SEED_SIZE * sizeof(u32)]) |
| 22 | { |
| 23 | m_position_bytes = 0; |
| 24 | |
| 25 | for (size_t i = 0; i < SEED_SIZE; ++i) |
| 26 | m_buffer[i] = Common::swap32(seed + i * sizeof(u32)); |
| 27 | |
| 28 | Initialize(false); |
| 29 | } |
| 30 | |
| 31 | size_t LaggedFibonacciGenerator::GetSeed(const u8* data, size_t size, size_t data_offset, |
| 32 | u32 seed_out[SEED_SIZE]) |
| 33 | { |
| 34 | if ((reinterpret_cast<uintptr_t>(data) - data_offset) % alignof(u32) != 0) |
| 35 | { |
| 36 | ASSERT(false); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | // For code simplicity, only include whole u32 words when regenerating the seed. It would be |
| 41 | // possible to get rid of this restriction and use a few additional bytes, but it's probably more |
| 42 | // effort than it's worth considering that junk data often starts or ends on 4-byte offsets. |
| 43 | const size_t bytes_to_skip = Common::AlignUp(data_offset, sizeof(u32)) - data_offset; |
| 44 | const u32* u32_data = reinterpret_cast<const u32*>(data + bytes_to_skip); |
| 45 | const size_t u32_size = (size - bytes_to_skip) / sizeof(u32); |
| 46 | const size_t u32_data_offset = (data_offset + bytes_to_skip) / sizeof(u32); |
| 47 | |
| 48 | LaggedFibonacciGenerator lfg; |
| 49 | if (!GetSeed(u32_data, u32_size, u32_data_offset, &lfg, seed_out)) |
| 50 | return false; |
| 51 | |
| 52 | lfg.m_position_bytes = data_offset % (LFG_K * sizeof(u32)); |
| 53 | |
| 54 | const u8* end = data + size; |
| 55 | size_t reconstructed_bytes = 0; |
| 56 | while (data < end && lfg.GetByte() == *data) |
| 57 | { |
| 58 | ++reconstructed_bytes; |
| 59 | ++data; |
| 60 | } |
| 61 | return reconstructed_bytes; |
| 62 | } |
| 63 | |
| 64 | bool LaggedFibonacciGenerator::GetSeed(const u32* data, size_t size, size_t data_offset, |
| 65 | LaggedFibonacciGenerator* lfg, u32 seed_out[SEED_SIZE]) |
| 66 | { |
| 67 | if (size < LFG_K) |
| 68 | return false; |
| 69 | |
| 70 | // If the data doesn't look like something we can regenerate, return early to save time |
| 71 | if (!std::all_of(data, data + LFG_K, [](u32 x) { |
| 72 | return (Common::swap32(x) & 0x00C00000) == (Common::swap32(x) >> 2 & 0x00C00000); |
| 73 | })) |
| 74 | { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | const size_t data_offset_mod_k = data_offset % LFG_K; |
| 79 | const size_t data_offset_div_k = data_offset / LFG_K; |
| 80 | |
| 81 | std::copy_n(data, LFG_K - data_offset_mod_k, lfg->m_buffer.data() + data_offset_mod_k); |
| 82 | std::copy_n(data + LFG_K - data_offset_mod_k, data_offset_mod_k, lfg->m_buffer.data()); |
| 83 | |
| 84 | lfg->Backward(0, data_offset_mod_k); |
| 85 | |
| 86 | for (size_t i = 0; i < data_offset_div_k; ++i) |
| 87 | lfg->Backward(); |
| 88 | |
| 89 | if (!lfg->Reinitialize(seed_out)) |
| 90 | return false; |
| 91 | |
| 92 | for (size_t i = 0; i < data_offset_div_k; ++i) |
| 93 | lfg->Forward(); |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void LaggedFibonacciGenerator::GetBytes(size_t count, u8* out) |
| 99 | { |
| 100 | while (count > 0) |
| 101 | { |
| 102 | const size_t length = std::min(count, LFG_K * sizeof(u32) - m_position_bytes); |
| 103 | |
| 104 | std::memcpy(out, reinterpret_cast<u8*>(m_buffer.data()) + m_position_bytes, length); |
| 105 | |
| 106 | m_position_bytes += length; |
| 107 | count -= length; |
| 108 | out += length; |
| 109 | |
| 110 | if (m_position_bytes == LFG_K * sizeof(u32)) |
| 111 | { |
| 112 | Forward(); |
| 113 | m_position_bytes = 0; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | u8 LaggedFibonacciGenerator::GetByte() |
| 119 | { |
| 120 | const u8 result = reinterpret_cast<u8*>(m_buffer.data())[m_position_bytes]; |
| 121 | |
| 122 | ++m_position_bytes; |
| 123 | |
| 124 | if (m_position_bytes == LFG_K * sizeof(u32)) |
| 125 | { |
| 126 | Forward(); |
| 127 | m_position_bytes = 0; |
| 128 | } |
| 129 | |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | void LaggedFibonacciGenerator::Forward(size_t count) |
| 134 | { |
| 135 | m_position_bytes += count; |
| 136 | while (m_position_bytes >= LFG_K * sizeof(u32)) |
| 137 | { |
| 138 | Forward(); |
| 139 | m_position_bytes -= LFG_K * sizeof(u32); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void LaggedFibonacciGenerator::Forward() |
| 144 | { |
| 145 | for (size_t i = 0; i < LFG_J; ++i) |
| 146 | m_buffer[i] ^= m_buffer[i + LFG_K - LFG_J]; |
| 147 | |
| 148 | for (size_t i = LFG_J; i < LFG_K; ++i) |
| 149 | m_buffer[i] ^= m_buffer[i - LFG_J]; |
| 150 | } |
| 151 | |
| 152 | void LaggedFibonacciGenerator::Backward(size_t start_word, size_t end_word) |
| 153 | { |
| 154 | const size_t loop_end = std::max(LFG_J, start_word); |
| 155 | for (size_t i = std::min(end_word, LFG_K); i > loop_end; --i) |
| 156 | m_buffer[i - 1] ^= m_buffer[i - 1 - LFG_J]; |
| 157 | |
| 158 | for (size_t i = std::min(end_word, LFG_J); i > start_word; --i) |
| 159 | m_buffer[i - 1] ^= m_buffer[i - 1 + LFG_K - LFG_J]; |
| 160 | } |
| 161 | |
| 162 | bool LaggedFibonacciGenerator::Reinitialize(u32 seed_out[SEED_SIZE]) |
| 163 | { |
| 164 | for (size_t i = 0; i < 4; ++i) |
| 165 | Backward(); |
| 166 | |
| 167 | for (u32& x : m_buffer) |
| 168 | x = Common::swap32(x); |
| 169 | |
| 170 | // Reconstruct the bits which are missing due to the output code shifting by 18 instead of 16. |
| 171 | // Unfortunately we can't reconstruct bits 16 and 17 (counting LSB as 0) for the first word, |
| 172 | // but the observable result (when shifting by 18 instead of 16) is not affected by this. |
| 173 | for (size_t i = 0; i < SEED_SIZE; ++i) |
| 174 | { |
| 175 | m_buffer[i] = (m_buffer[i] & 0xFF00FFFF) | (m_buffer[i] << 2 & 0x00FC0000) | |
| 176 | ((m_buffer[i + 16] ^ m_buffer[i + 15]) << 9 & 0x00030000); |
| 177 | } |
| 178 | |
| 179 | for (size_t i = 0; i < SEED_SIZE; ++i) |
| 180 | seed_out[i] = Common::swap32(m_buffer[i]); |
| 181 | |
| 182 | return Initialize(true); |
| 183 | } |
| 184 | |
| 185 | bool LaggedFibonacciGenerator::Initialize(bool check_existing_data) |
| 186 | { |
| 187 | for (size_t i = SEED_SIZE; i < LFG_K; ++i) |
| 188 | { |
| 189 | const u32 calculated = (m_buffer[i - 17] << 23) ^ (m_buffer[i - 16] >> 9) ^ m_buffer[i - 1]; |
| 190 | |
| 191 | if (check_existing_data) |
| 192 | { |
| 193 | const u32 actual = (m_buffer[i] & 0xFF00FFFF) | (m_buffer[i] << 2 & 0x00FC0000); |
| 194 | if ((calculated & 0xFFFCFFFF) != actual) |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | m_buffer[i] = calculated; |
| 199 | } |
| 200 | |
| 201 | // Instead of doing the "shift by 18 instead of 16" oddity when actually outputting the data, |
| 202 | // we can do the shifting (and byteswapping) at this point to make the output code simpler. |
| 203 | for (u32& x : m_buffer) |
| 204 | x = Common::swap32((x & 0xFF00FFFF) | ((x >> 2) & 0x00FF0000)); |
| 205 | |
| 206 | for (size_t i = 0; i < 4; ++i) |
| 207 | Forward(); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | } // namespace DiscIO |