Minor cleanup

This commit is contained in:
Deterous
2023-11-01 20:46:01 +13:00
parent fd18b425d1
commit be713935b6
3 changed files with 129 additions and 130 deletions
+69 -69
View File
@@ -131,75 +131,7 @@ namespace LibIRD
#endregion #endregion
#region Generate Keys #region Helper Functions
/// <summary>
/// Generates Data 1, via AES-128 CBC decryption of a Disc Key
/// </summary>
/// <param name="key">Byte array containing AES encrypted Disc Key</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD1(byte[] key)
{
// Validate key
if (key == null)
throw new ArgumentNullException(nameof(key));
if (key.Length != 16)
throw new ArgumentException("Disc Key must be a byte array of length 16", nameof(key));
// Setup AES decryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D1AesKey;
aes.IV = D1AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES decryption
using MemoryStream stream = new();
using ICryptoTransform dec = aes.CreateDecryptor();
using CryptoStream cs = new(stream, dec, CryptoStreamMode.Write);
cs.Write(key, 0, 16);
cs.FlushFinalBlock();
// Save decrypted key to field
Data1Key = stream.ToArray();
}
/// <summary>
/// Generates Data 2, via AES-128 CBC encryption of a Disc ID
/// </summary>
/// <param name="id">Byte array containing AES decrypted Disc ID</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD2(byte[] id)
{
// Validate id
if (id == null)
throw new ArgumentNullException(nameof(id));
if (id.Length != 16)
throw new ArgumentException("Disc ID must be a byte array of length 16", nameof(id));
// Setup AES encryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D2AesKey;
aes.IV = D2AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES encryption
using MemoryStream stream = new();
using ICryptoTransform enc = aes.CreateEncryptor();
using CryptoStream cs = new(stream, enc, CryptoStreamMode.Write);
cs.Write(id, 0, 16);
cs.FlushFinalBlock();
// Save encrypted key to field
Data2Key = stream.ToArray();
}
#endregion #endregion
@@ -315,6 +247,74 @@ namespace LibIRD
#region Methods #region Methods
/// <summary>
/// Generates Data 1, via AES-128 CBC decryption of a Disc Key
/// </summary>
/// <param name="key">Byte array containing AES encrypted Disc Key</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD1(byte[] key)
{
// Validate key
if (key == null)
throw new ArgumentNullException(nameof(key));
if (key.Length != 16)
throw new ArgumentException("Disc Key must be a byte array of length 16", nameof(key));
// Setup AES decryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D1AesKey;
aes.IV = D1AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES decryption
using MemoryStream stream = new();
using ICryptoTransform dec = aes.CreateDecryptor();
using CryptoStream cs = new(stream, dec, CryptoStreamMode.Write);
cs.Write(key, 0, 16);
cs.FlushFinalBlock();
// Save decrypted key to field
Data1Key = stream.ToArray();
}
/// <summary>
/// Generates Data 2, via AES-128 CBC encryption of a Disc ID
/// </summary>
/// <param name="id">Byte array containing AES decrypted Disc ID</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD2(byte[] id)
{
// Validate id
if (id == null)
throw new ArgumentNullException(nameof(id));
if (id.Length != 16)
throw new ArgumentException("Disc ID must be a byte array of length 16", nameof(id));
// Setup AES encryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D2AesKey;
aes.IV = D2AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES encryption
using MemoryStream stream = new();
using ICryptoTransform enc = aes.CreateEncryptor();
using CryptoStream cs = new(stream, enc, CryptoStreamMode.Write);
cs.Write(id, 0, 16);
cs.FlushFinalBlock();
// Save encrypted key to field
Data2Key = stream.ToArray();
}
/// <summary> /// <summary>
/// Write IRD data to file /// Write IRD data to file
/// </summary> /// </summary>
+59 -60
View File
@@ -126,8 +126,8 @@ namespace LibIRD
throw new ArgumentNullException(nameof(sfoPath)); throw new ArgumentNullException(nameof(sfoPath));
// Read file as a stream, and parse file // Read file as a stream, and parse file
using (FileStream fs = new FileStream(sfoPath, FileMode.Open, FileAccess.Read)) using FileStream fs = new(sfoPath, FileMode.Open, FileAccess.Read);
Parse(fs); Parse(fs);
} }
/// <summary> /// <summary>
@@ -138,67 +138,66 @@ namespace LibIRD
private void Parse(Stream sfoStream) private void Parse(Stream sfoStream)
{ {
// Read binary stream // Read binary stream
using (BinaryReader br = new BinaryReader(sfoStream)) using BinaryReader br = new(sfoStream);
// Check file signature is correct
string magic = Encoding.ASCII.GetString(br.ReadBytes(4));
if (magic != ParamSFO.Magic)
throw new FileLoadException("Not a valid PARAM.SFO file");
// Parse header
Version = br.ReadUInt32();
KeyTableStart = br.ReadUInt32();
DataTableStart = br.ReadUInt32();
ParamCount = br.ReadUInt32();
// Parse parameter metadata
Params = new Param[ParamCount];
for (int i = 0; i < ParamCount; i++)
{ {
// Check file signature is correct Params[i] = new Param
string magic = Encoding.ASCII.GetString(br.ReadBytes(4));
if (magic != ParamSFO.Magic)
throw new FileLoadException("Not a valid PARAM.SFO file");
// Parse header
Version = br.ReadUInt32();
KeyTableStart = br.ReadUInt32();
DataTableStart = br.ReadUInt32();
ParamCount = br.ReadUInt32();
// Parse parameter metadata
Params = new Param[ParamCount];
for (int i = 0; i < ParamCount; i++)
{ {
Params[i] = new Param KeyOffset = br.ReadUInt16(),
{ DataFormat = br.ReadUInt16(),
KeyOffset = br.ReadUInt16(), DataLength = br.ReadUInt32(),
DataFormat = br.ReadUInt16(), DataTotal = br.ReadUInt32(),
DataLength = br.ReadUInt32(), DataOffset = br.ReadUInt32()
DataTotal = br.ReadUInt32(), };
DataOffset = br.ReadUInt32() }
};
}
// Parse parameters // Parse parameters
for (int i = 0; i < ParamCount; i++) for (int i = 0; i < ParamCount; i++)
{
// Move stream to ith key
sfoStream.Position = KeyTableStart + Params[i].KeyOffset;
// Determine ith key length
uint keyLen = ((i == ParamCount - 1) ? DataTableStart - KeyTableStart : Params[i + 1].KeyOffset)
- Params[i].KeyOffset;
// Read ith key name
Params[i].Name = Encoding.ASCII.GetString(br.ReadBytes((int) keyLen)).TrimEnd('\0');
// Move stream to ith data
sfoStream.Position = DataTableStart + Params[i].DataOffset;
// Read ith data, based on data format
switch (Params[i].DataFormat)
{ {
// Move stream to ith key case 0x0400: // Non-null-terminated UTF-8 String
sfoStream.Position = KeyTableStart + Params[i].KeyOffset; Params[i].StringValue = Encoding.UTF8.GetString(br.ReadBytes((int)Params[i].DataLength));
break;
// Determine ith key length case 0x0402: // Null-terminated UTF-8 String
uint keyLen = ((i == ParamCount - 1) ? DataTableStart - KeyTableStart : Params[i + 1].KeyOffset) Params[i].StringValue = Encoding.UTF8.GetString(br.ReadBytes((int)Params[i].DataLength)).TrimEnd('\0');
- Params[i].KeyOffset; break;
case 0x0404: // Integer
// Read ith key name //if (Params[i].DataLength != 4)
Params[i].Name = Encoding.ASCII.GetString(br.ReadBytes((int) keyLen)).TrimEnd('\0'); //throw new ArgumentException("Integer parameter not 4 bytes?");
Params[i].IntValue = br.ReadInt32();
// Move stream to ith data break;
sfoStream.Position = DataTableStart + Params[i].DataOffset; default: // Unknown data format, assume null-terminated string
Params[i].StringValue = Encoding.UTF8.GetString(br.ReadBytes((int)Params[i].DataLength)).TrimEnd('\0');
// Read ith data, based on data format break;
switch (Params[i].DataFormat)
{
case 0x0400: // Non-null-terminated UTF-8 String
Params[i].StringValue = Encoding.UTF8.GetString(br.ReadBytes((int)Params[i].DataLength));
break;
case 0x0402: // Null-terminated UTF-8 String
Params[i].StringValue = Encoding.UTF8.GetString(br.ReadBytes((int)Params[i].DataLength)).TrimEnd('\0');
break;
case 0x0404: // Integer
//if (Params[i].DataLength != 4)
//throw new ArgumentException("Integer parameter not 4 bytes?");
Params[i].IntValue = br.ReadInt32();
break;
default: // Unknown data format, assume null-terminated string
Params[i].StringValue = Encoding.UTF8.GetString(br.ReadBytes((int)Params[i].DataLength)).TrimEnd('\0');
break;
}
} }
} }
} }
@@ -209,7 +208,7 @@ namespace LibIRD
public void Print() public void Print()
{ {
// Build string from parameters // Build string from parameters
StringBuilder print = new StringBuilder("PARAM.SFO Contents:\n====================\n"); StringBuilder print = new("PARAM.SFO Contents:\n====================\n");
// Loop through all parameters in PARAM.SFO // Loop through all parameters in PARAM.SFO
for (int i = 0; i < ParamCount; i++) for (int i = 0; i < ParamCount; i++)
+1 -1
View File
@@ -170,7 +170,7 @@ namespace LibIRD
byte[] crc32; byte[] crc32;
using (FileStream fs = File.OpenRead(isoPath)) using (FileStream fs = File.OpenRead(isoPath))
{ {
Crc32 hasher = new Crc32(); Crc32 hasher = new();
hasher.Append(fs); hasher.Append(fs);
crc32 = hasher.GetCurrentHash(); crc32 = hasher.GetCurrentHash();
Array.Reverse(crc32); Array.Reverse(crc32);