7 Commits
Author SHA1 Message Date
Deterous 5ce81f9663 Bump version 2024-02-18 17:28:21 +09:00
Deterous f74220efea Read ISO in larger chunks 2024-02-18 16:59:04 +09:00
Deterous 6e529f0050 Reuse ISO hash if searching redump 2024-02-16 11:11:51 +09:00
Deterous b862057123 Only print warning for non-contiguous files 2024-02-11 12:39:13 +09:00
Deterous cf19596ea7 Alphabetical order, key file path 2024-02-08 09:38:46 +09:00
Deterous 6af41a9bb3 Include dependencies in nupkg 2024-02-07 15:43:23 +09:00
Deterous 89c23622bd Remove DiscUtils as a public dependency 2024-02-07 14:41:14 +09:00
6 changed files with 125 additions and 79 deletions
+5 -5
View File
@@ -9,7 +9,7 @@
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>0.4.1</Version>
<Version>0.5.0</Version>
<!-- Package Properties -->
<Authors>Deterous</Authors>
@@ -27,11 +27,11 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibIRD\LibIRD.csproj" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Core\DiscUtils.Core.csproj" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Iso9660\DiscUtils.Iso9660.csproj" />
<ProjectReference Include="..\LibIRD\LibIRD.csproj" PrivateAssets="All" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Core\DiscUtils.Core.csproj" PrivateAssets="All" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Iso9660\DiscUtils.Iso9660.csproj" PrivateAssets="All" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="SabreTools.RedumpLib" Version="1.3.2" />
<PackageReference Include="SabreTools.RedumpLib" Version="1.3.3" />
</ItemGroup>
</Project>
+18 -9
View File
@@ -159,11 +159,13 @@ namespace IRDKit
}
// Determine output IRD folder
string outputPath = Path.GetDirectoryName(opt.IRDPath);
string outputPath = opt.IRDPath;
if (File.Exists(opt.IRDPath))
outputPath = Path.GetDirectoryName(opt.IRDPath);
// Create an IRD file for all ISO files found
foreach (string file in isoFiles)
ISO2IRD(file, irdPath: outputPath, verbose: opt.Verbose);
foreach (string file in isoFiles.OrderBy(x => x))
ISO2IRD(file, irdPath: outputPath, keyPath: opt.KeyFile, verbose: opt.Verbose);
}
else
{
@@ -719,14 +721,19 @@ namespace IRDKit
{
try
{
// If key directory was given, append filename and .key
if (Directory.Exists(keyPath))
{
keyPath = Path.Combine(keyPath, Path.ChangeExtension(Path.GetFileName(isoPath), ".key"));
}
// Read key from .key file
byte[] discKey = File.ReadAllBytes(keyPath);
if (discKey == null || discKey.Length != 16)
Console.Error.WriteLine($"{hexKey} is not a valid key, detecting key automatically...");
else
{
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
Console.WriteLine($"Creating {irdPath} with Key: {Convert.ToHexString(discKey)}");
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
@@ -778,13 +785,13 @@ namespace IRDKit
try
{
// Read key from .key file
byte[] discKey = File.ReadAllBytes(keyPath);
byte[] discKey = File.ReadAllBytes(keyfilePath);
if (discKey == null || discKey.Length != 16)
Console.Error.WriteLine($"{hexKey} is not a valid key, detecting key automatically...");
else
{
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
Console.WriteLine($"Creating {irdPath} with Key: {Convert.ToHexString(discKey)}");
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
@@ -811,8 +818,8 @@ namespace IRDKit
// Found .getkey.log file, check it is valid
try
{
Console.WriteLine($"Creating {irdPath} with key from: {getKeyLog}");
IRD ird1 = new ReIRD(isoPath, getKeyLog);
Console.WriteLine($"Creating {irdPath} with key from: {logfilePath}");
IRD ird1 = new ReIRD(isoPath, logfilePath);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
@@ -836,11 +843,13 @@ namespace IRDKit
// Compute CRC32 hash
byte[] crc32;
uint crc32UInt;
using (FileStream fs = File.OpenRead(isoPath))
{
Crc32 hasher = new();
hasher.Append(fs);
crc32 = hasher.GetCurrentHash();
crc32UInt = BitConverter.ToUInt32(crc32);
// Change endianness
Array.Reverse(crc32);
}
@@ -898,7 +907,7 @@ namespace IRDKit
Console.WriteLine($"Creating {irdPath} with Key from redump.org: {Convert.ToHexString(key)}");
try
{
IRD ird = new ReIRD(isoPath, key, layerbreak);
IRD ird = new ReIRD(isoPath, key, layerbreak, crc32UInt);
ird.Write(irdPath);
if (verbose)
ird.Print();
+85 -51
View File
@@ -270,7 +270,7 @@ namespace LibIRD
/// <summary>
/// MD5 hashes for all decrypted files in the image
/// </summary>
/// <remarks><see cref="FileHashes"/> files, 16-bytes per hash, alternating with each <see cref="FileHashes"/> entry</remarks>
/// <remarks><see cref="FileCount"/> files, 16-bytes per hash, alternating with each <see cref="FileHashes"/> entry</remarks>
public byte[][] FileHashes { get; private set; }
/// <summary>
@@ -715,7 +715,6 @@ namespace LibIRD
HashFiles(fs, reader, rootDir);
if (FileCount != fileCount)
{
Console.WriteLine($"{isoPath} contains split files: detected {FileCount} out of {fileCount} expected files");
long[] tempFileKeys = FileKeys;
Array.Resize(ref tempFileKeys, (int)FileCount);
FileKeys = tempFileKeys;
@@ -891,8 +890,8 @@ namespace LibIRD
RegionEnd[^1] = (UpdateEnd / SectorSize) - 1;
// Determine MD5 hashes for each region
using MD5 md5 = MD5.Create();
byte[] buf = new byte[SectorSize];
int bufSectors = 1024;
byte[] buf = new byte[bufSectors * SectorSize];
for (int i = 0; i < RegionCount; i++)
{
// Start reading data from first sector of region
@@ -900,19 +899,32 @@ namespace LibIRD
// Compute MD5 hash for just the region portion of the ISO file
int numBytes;
for (long j = RegionStart[i]; j <= RegionEnd[i]; j++)
using MD5 md5 = MD5.Create();
int regionSectors = (int)(RegionEnd[i] - RegionStart[i]) + 1;
for (int j = bufSectors; j <= regionSectors; j += bufSectors)
{
// Read one sector at a time
// Read into buffer
numBytes = fs.Read(buf, 0, buf.Length);
// Check that an entire sector was read
// TODO: Process partial buffer if non-zero is returned
if (numBytes < buf.Length)
throw new InvalidFileSystemException("Disc region ended unexpectedly");
// Process MD5 sum one sector at a time
// Process MD5 sum
md5.TransformBlock(buf, 0, buf.Length, null, 0);
}
// Read any remaining sectors
int bufRemainder = (int)(SectorSize * (regionSectors % bufSectors));
if (bufRemainder != 0)
{
numBytes = fs.Read(buf, 0, bufRemainder);
// TODO: Process partial buffer if non-zero is returned
if (numBytes < bufRemainder)
throw new InvalidFileSystemException("Disc region ended unexpectedly");
}
// Compute and store MD5 hash of region
md5.TransformFinalBlock(buf, 0, 0);
md5.TransformFinalBlock(buf, 0, bufRemainder);
RegionHashes[i] = md5.Hash;
}
}
@@ -938,11 +950,15 @@ namespace LibIRD
// Determine smallest file offset as first sector
long smallestOffset = fileClusters[0].Offset;
bool nonContiguous = false;
for (int i = 1; i < fileClusters.Length; i++)
{
if (fileClusters[i] == null)
throw new InvalidFileSystemException($"Unexpected file extents for {filePath}");
if (fileClusters[i].Offset * SectorSize != fileClusters[i - 1].Offset * SectorSize + fileClusters[i - 1].Count)
nonContiguous = true;
if (fileClusters[i].Offset < smallestOffset)
smallestOffset = fileClusters[i].Offset;
}
@@ -950,9 +966,8 @@ namespace LibIRD
// If already encountered file offset, skip this file
if (Array.Exists(FileKeys, element => element == smallestOffset))
continue;
if (fileClusters.Length > 1)
Console.WriteLine($"Split file detected: {filePath}");
else if (nonContiguous)
Console.WriteLine($"Non-contiguous file found: {filePath}");
// Add file offset to keys
FileKeys[FileCount] = smallestOffset;
@@ -968,39 +983,47 @@ namespace LibIRD
}
}
// Hash each non-contiguous portion of the ISO file
byte[] buf = new byte[SectorSize];
MD5 md5 = MD5.Create();
// Read one sector at a time for small files
int bufSectors = (fileClusters[0].Count < 1024 * SectorSize) ? 1 : 1024;
byte[] buf = new byte[bufSectors * SectorSize];
// Hash each non-contiguous portion of the file
using MD5 md5 = MD5.Create();
for (int i = 0; i < fileClusters.Length; i++)
{
// Start reading data from the beginning of the ISO file
// Start reading data from the beginning of the current extent
fs.Seek(fileClusters[i].Offset * SectorSize, SeekOrigin.Begin);
int numBytes;
// Read all data before the first data sector
for (int j = 0; j < (fileClusters[i].Count / SectorSize); j++)
// Read file in buffers
long fileSectors = fileClusters[i].Count / SectorSize;
for (int j = 0; j <= fileSectors - bufSectors; j += bufSectors)
{
numBytes = fs.Read(buf, 0, buf.Length);
// Check that an entire sector was read
if (numBytes < buf.Length)
// TODO: Process partial buffer if non-zero is returned
if (numBytes != buf.Length)
throw new InvalidFileSystemException("Disc region ended unexpectedly");
// Decrypt sector if necessary
// Decrypt buffer if necessary
if (encrypted)
buf = DecryptSector(buf, (int)fileClusters[i].Offset + j);
DecryptSectors(ref buf, (int)fileClusters[i].Offset + j);
// Hash sector
md5.TransformBlock(buf, 0, numBytes, null, 0);
md5.TransformBlock(buf, 0, buf.Length, null, 0);
}
// Read remaining partial sector
if (fileClusters[i].Count % SectorSize != 0)
// Read remaining partial buffer
if ((fileClusters[i].Count % buf.Length) > 0)
{
numBytes = fs.Read(buf, 0, buf.Length);
// Check that an entire sector was read
if (numBytes < buf.Length)
// TODO: Process partial buffer if non-zero is returned
if (numBytes != buf.Length)
throw new InvalidFileSystemException("Disc region ended unexpectedly");
// Decrypt partial sector if necessary
// Decrypt buffer if necessary
if (encrypted)
buf = DecryptSector(buf, (int)fileClusters[i].Offset + (int)(fileClusters[i].Count / SectorSize));
// Hash partial sector
md5.TransformBlock(buf, 0, (int)(fileClusters[i].Count % SectorSize), null, 0);
DecryptSectors(ref buf, (int)fileClusters[i].Offset + bufSectors * (int)(fileClusters[i].Count / buf.Length));
// Hash partial buffer
md5.TransformBlock(buf, 0, (int)(fileClusters[i].Count % buf.Length), null, 0);
}
}
@@ -1018,38 +1041,49 @@ namespace LibIRD
}
/// <summary>
/// Decrypts a given sector byte array
/// Decrypts a given byte array of sector(s)
/// </summary>
/// <param name="sector">Byte array to be decrypted</param>
/// <param name="buffer">Byte array to be decrypted</param>
/// <param name="offset">Number of bytes to decrypt</param>
/// <exception cref="InvalidOperationException"></exception>
private protected byte[] DecryptSector(byte[] sector, int sectorNumber)
private protected void DecryptSectors(ref byte[] buffer, int sectorNumber)
{
ArgumentNullException.ThrowIfNull(buffer);
if (buffer.Length == 0 || buffer.Length % SectorSize != 0)
throw new ArgumentException("Encrypted buffer must be multiple of SectorSize");
// Setup AES decryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = DiscKey;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Determine Initial Value based on sector number
byte[] iv = new byte[16];
for (int i = 0; i < 16; i++)
// Decrypt buffer one sector at a time
for (int i = 0; i < buffer.Length; i += (int)SectorSize)
{
byte a = (byte)(sectorNumber & 0xFF);
iv[16 - i - 1] = (byte)(sectorNumber & 0xFF);
sectorNumber >>= 8;
// Determine AES Initial Value based on sector number
byte[] iv = new byte[16];
int tempNum = sectorNumber;
for (int j = 0; j < 16; j++)
{
iv[16 - j - 1] = (byte)(tempNum & 0xFF);
tempNum >>= 8;
}
aes.IV = iv;
// Perform AES decryption
using MemoryStream stream = new();
using CryptoStream cs = new(stream, aes.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(buffer, i, (int)SectorSize);
cs.FlushFinalBlock();
// Write decrypted sector to output
stream.ToArray().CopyTo(buffer, i);
sectorNumber++;
}
aes.IV = iv;
// Perform AES decryption
using MemoryStream stream = new();
using ICryptoTransform dec = aes.CreateDecryptor();
using CryptoStream cs = new(stream, dec, CryptoStreamMode.Write);
cs.Write(sector, 0, sector.Length);
cs.FlushFinalBlock();
return stream.ToArray();
return;
}
/// <summary>
+13 -4
View File
@@ -6,7 +6,7 @@
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>0.4.1</Version>
<Version>0.5.0</Version>
<PackageOutputPath>../nupkg</PackageOutputPath>
<!-- Package Properties -->
@@ -26,10 +26,19 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Core\DiscUtils.Core.csproj" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Iso9660\DiscUtils.Iso9660.csproj" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Streams\DiscUtils.Streams.csproj" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Core\DiscUtils.Core.csproj" PrivateAssets="All" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Iso9660\DiscUtils.Iso9660.csproj" PrivateAssets="All" />
<ProjectReference Include="..\DiscUtils\Library\DiscUtils.Streams\DiscUtils.Streams.csproj" PrivateAssets="All" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
</ItemGroup>
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('PrivateAssets', 'All'))" />
</ItemGroup>
</Target>
</Project>
-5
View File
@@ -43,13 +43,8 @@ namespace LibIRD
/// Constructor using a PARAM.SFO file path
/// </summary>
/// <param name="sfoPath">Full file path to the PARAM.SFO file</param>
/// <exception cref="ArgumentNullException"></exception>
public ParamSFO(string sfoPath)
{
// Validate file path
if (sfoPath == null || sfoPath.Length <= 0)
throw new ArgumentNullException(nameof(sfoPath));
// Read file as a stream, and parse file
using FileStream fs = new(sfoPath, FileMode.Open, FileAccess.Read);
Parse(fs);
+4 -5
View File
@@ -59,7 +59,6 @@ namespace LibIRD
/// </summary>
/// <param name="isoPath">Path to the ISO</param>
/// <param name="getKeyLog">Path to the GetKey log file</param>
/// <exception cref="InvalidDataException"></exception>
public ReIRD(string isoPath, string getKeyLog) : base(isoPath, getKeyLog, true)
{
// Generate Unique Identifier using ISO CRC32
@@ -72,11 +71,12 @@ namespace LibIRD
/// <param name="isoPath">Path to the ISO</param>
/// <param name="key">Disc Key, redump-style (AES encrypted Data 1)</param>
/// <param name="layerbreak">Layerbreak value, in sectors</param>
/// <param name="uid">Unique ID, crc32 hash of ISO</param>
/// <param name="region">Disc Region</param>
public ReIRD(string isoPath, byte[] key, long? layerbreak = null, Region region = Region.NONE) : base()
public ReIRD(string isoPath, byte[] key, long? layerbreak = null, uint? uid = null, Region region = Region.NONE) : base()
{
// Generate Unique Identifier using ISO CRC32
UID = GenerateUID(isoPath);
UID = uid == null ? GenerateUID(isoPath) : (uint)uid;
// Determine ISO file size
long size = CalculateSize(isoPath);
@@ -240,7 +240,6 @@ namespace LibIRD
/// Generates the UID field by computing the CRC32 hash of the ISO
/// </summary>
/// <param name="isoPath">Path to the ISO</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="FileNotFoundException"></exception>
private static uint GenerateUID(string isoPath)
{
@@ -274,7 +273,7 @@ namespace LibIRD
/// Calculates ISO file size
/// </summary>
/// <param name="isoPath">Path to the ISO</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="FileNotFoundException"></exception>
private static long CalculateSize(string isoPath)
{