scrambler.hpp 754 B

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. namespace nall::CD::Scrambler {
  3. //polynomial(x) = x^15 + x + 1
  4. inline auto polynomial(uint x) -> uint8_t {
  5. static uint8_t lookup[2340]{};
  6. static bool once = false;
  7. if(!once) { once = true;
  8. uint16_t shift = 0x0001;
  9. for(uint n : range(2340)) {
  10. lookup[n] = shift;
  11. for(uint b : range(8)) {
  12. bool carry = shift & 1 ^ shift >> 1 & 1;
  13. shift = (carry << 15 | shift) >> 1;
  14. }
  15. }
  16. }
  17. return lookup[x];
  18. }
  19. //
  20. inline auto transform(array_span<uint8_t> sector) -> bool {
  21. if(sector.size() == 2352) sector += 12; //header is not scrambled
  22. if(sector.size() != 2340) return false; //F1 frames only
  23. for(uint index : range(2340)) {
  24. sector[index] ^= polynomial(index);
  25. }
  26. return true;
  27. }
  28. }