Skip to content

Commit

Permalink
Add error handling to scrambling (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
FakeShemp authored Oct 15, 2023
1 parent 089ba52 commit fd04357
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions DVD/Sector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Aaru.CommonTypes.Enums;
using Aaru.Helpers;

namespace Aaru.Decoders.DVD;
Expand Down Expand Up @@ -204,38 +205,50 @@ bool TestSeed(in byte[] sector, ushort seed)
/// </summary>
/// <param name="sector">Buffer of the scrambled sector</param>
/// <param name="cipher">Buffer of the scrambling cipher</param>
/// <returns>The unscrambled sector.</returns>
static byte[] UnscrambleSector(byte[] sector, IReadOnlyList<byte> cipher)
/// <param name="scrambled">Buffer of unscrambled sector data</param>
/// <returns>The Error.</returns>
static ErrorNumber UnscrambleSector(byte[] sector, IReadOnlyList<byte> cipher, out byte[] scrambled)
{
var scrambled = new byte[sector.Length];
scrambled = new byte[sector.Length];
Array.Copy(sector, 0, scrambled, 0, sector.Length);

for(var i = 0; i < 2048; i++)
scrambled[i + 12] = (byte)(sector[i + 12] ^ cipher[i]);

return ComputeEdc(0, scrambled, 2060) != BigEndianBitConverter.ToUInt32(sector, 2060) ? sector : scrambled;
return ComputeEdc(0, scrambled, 2060) != BigEndianBitConverter.ToUInt32(sector, 2060)
? ErrorNumber.NotVerifiable
: ErrorNumber.NoError;
}

public byte[] Scramble(byte[] sector)
public ErrorNumber Scramble(byte[] sector, out byte[] scrambled)
{
scrambled = new byte[sector.Length];

if(sector is not { Length: 2064 })
return sector;
return ErrorNumber.NotSupported;

byte[]? cipher = GetSeed(sector);

return cipher == null ? sector : UnscrambleSector(sector, cipher);
return cipher == null ? ErrorNumber.UnrecognizedFormat : UnscrambleSector(sector, cipher, out scrambled);
}

public byte[] Scramble(byte[] sector, uint transferLength)
public ErrorNumber Scramble(byte[] sector, uint transferLength, out byte[] scrambled)
{
var scrambled = new byte[sector.Length];
scrambled = new byte[sector.Length];

if(sector.Length % 2064 != 0 || sector.Length / 2064 != transferLength)
return sector;
return ErrorNumber.NotSupported;

for(uint i = 0; i < transferLength; i++)
Array.Copy(Scramble(sector.Skip((int)(i * 2064)).Take(2064).ToArray()), 0, scrambled, i * 2064, 2064);
{
ErrorNumber error = Scramble(sector.Skip((int)(i * 2064)).Take(2064).ToArray(), out byte[]? currentSector);

if(error != ErrorNumber.NoError)
return error;

Array.Copy(currentSector, 0, scrambled, i * 2064, 2064);
}

return scrambled;
return ErrorNumber.NoError;
}
}

0 comments on commit fd04357

Please sign in to comment.