Almost there...

This commit is contained in:
Matt Nadareski
2024-10-13 23:05:34 -04:00
parent 4a35d490d5
commit 5a4e747329
2 changed files with 86 additions and 26 deletions

View File

@@ -71,8 +71,9 @@ namespace NDecrypt.N3DS
return false;
}
// Process all NCCH partitions
ProcessAllPartitions(cia, encrypt, force, input, output);
// Process all 8 NCCH partitions
if (encrypt) EncryptAllPartitions(cia, force, input, output);
else DecryptAllPartitions(cia, force, input, output);
return false;
}
@@ -84,16 +85,18 @@ namespace NDecrypt.N3DS
}
}
#endregion
#region Decrypt
/// <summary>
/// Process all partitions in the content file data of a CIA header
/// Decrypt all partitions in the content file data of a CIA header
/// </summary>
/// <param name="cia">CIA representing the 3DS CIA file</param>
/// <param name="encrypt">Indicates if the file should be encrypted or decrypted</param>
/// <param name="force">Indicates if the operation should be forced</param>
/// <param name="input">Stream representing the input</param>
/// <param name="output">Stream representing the output</param>
private void ProcessAllPartitions(CIA cia,
bool encrypt,
private void DecryptAllPartitions(CIA cia,
bool force,
Stream input,
Stream output)
@@ -116,18 +119,12 @@ namespace NDecrypt.N3DS
continue;
}
// Process the partition, if possible
if (encrypt && ShouldEncryptPartition(cia, p, force))
EncryptPartition(header, p, input, output);
else if (!encrypt && ShouldDecryptPartition(cia, p, force))
// Decrypt the partition, if possible
if (ShouldDecryptPartition(cia, p, force))
DecryptPartition(header, p, input, output);
}
}
#endregion
#region Decrypt
/// <summary>
/// Determine if the current partition should be decrypted
/// </summary>
@@ -482,6 +479,39 @@ namespace NDecrypt.N3DS
#region Encrypt
/// <summary>
/// Encrypt all partitions in the content file data of a CIA header
/// </summary>
/// <param name="cia">CIA representing the 3DS CIA file</param>
/// <param name="force">Indicates if the operation should be forced</param>
/// <param name="input">Stream representing the input</param>
/// <param name="output">Stream representing the output</param>
private void EncryptAllPartitions(CIA cia, bool force, Stream input, Stream output)
{
// Check the partitions table
if (cia.Partitions == null)
{
Console.WriteLine("Invalid partitions table!");
return;
}
// Iterate over all 8 NCCH partitions
for (int p = 0; p < cia.Partitions.Length; p++)
{
// Check the partition exists
var header = cia.Partitions[0];
if (header == null)
{
Console.WriteLine($"Partition {p} Not found... Skipping...");
continue;
}
// Encrypt the partition, if possible
if (ShouldEncryptPartition(cia, p, force))
EncryptPartition(header, p, input, output);
}
}
/// <summary>
/// Determine if the current partition should be encrypted
/// </summary>

View File

@@ -70,7 +70,8 @@ namespace NDecrypt.N3DS
}
// Process all 8 NCCH partitions
ProcessAllPartitions(cart, encrypt, force, input, output);
if (encrypt) EncryptAllPartitions(cart, force, input, output);
else DecryptAllPartitions(cart, force, input, output);
return true;
}
@@ -82,15 +83,18 @@ namespace NDecrypt.N3DS
}
}
#endregion
#region Decrypt
/// <summary>
/// Process all partitions in the partition table of an NCSD header
/// Decrypt all partitions in the partition table of an NCSD header
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="encrypt">Indicates if the file should be encrypted or decrypted</param>
/// <param name="force">Indicates if the operation should be forced</param>
/// <param name="input">Stream representing the input</param>
/// <param name="output">Stream representing the output</param>
private void ProcessAllPartitions(Cart cart, bool encrypt, bool force, Stream input, Stream output)
private void DecryptAllPartitions(Cart cart, bool force, Stream input, Stream output)
{
// Check the partitions table
if (cart.Header?.PartitionsTable == null || cart.Partitions == null)
@@ -109,18 +113,12 @@ namespace NDecrypt.N3DS
continue;
}
// Process the partition, if possible
if (encrypt && ShouldEncryptPartition(cart, p, force))
EncryptPartition(cart, p, input, output);
else if (!encrypt && ShouldDecryptPartition(cart, p, force))
// Decrypt the partition, if possible
if (ShouldDecryptPartition(cart, p, force))
DecryptPartition(cart, p, input, output);
}
}
#endregion
#region Decrypt
/// <summary>
/// Determine if the current partition should be decrypted
/// </summary>s
@@ -448,6 +446,38 @@ namespace NDecrypt.N3DS
#region Encrypt
/// <summary>
/// Encrypt all partitions in the partition table of an NCSD header
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="force">Indicates if the operation should be forced</param>
/// <param name="input">Stream representing the input</param>
/// <param name="output">Stream representing the output</param>
private void EncryptAllPartitions(Cart cart, bool force, Stream input, Stream output)
{
// Check the partitions table
if (cart.Header?.PartitionsTable == null || cart.Partitions == null)
{
Console.WriteLine("Invalid partitions table!");
return;
}
// Iterate over all 8 NCCH partitions
for (int p = 0; p < 8; p++)
{
// Check the partition exists
if (cart.Partitions[p] == null)
{
Console.WriteLine($"Partition {p} Not found... Skipping...");
continue;
}
// Encrypt the partition, if possible
if (ShouldEncryptPartition(cart, p, force))
EncryptPartition(cart, p, input, output);
}
}
/// <summary>
/// Determine if the current partition should be encrypted
/// </summary>