Skip to content

Commit

Permalink
Fix more issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Nov 14, 2024
1 parent 15961e7 commit ce54eb2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion NDecrypt.Core/NDecrypt.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<PackageReference Include="SabreTools.Hashing" Version="1.4.0" />
<PackageReference Include="SabreTools.IO" Version="1.5.0" />
<PackageReference Include="SabreTools.Models" Version="1.5.1" />
<PackageReference Include="SabreTools.Serialization" Version="1.7.3" />
<PackageReference Include="SabreTools.Serialization" Version="1.7.4" />
</ItemGroup>

</Project>
52 changes: 35 additions & 17 deletions NDecrypt.Core/ThreeDSTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SabreTools.Models.N3DS;
using SabreTools.Serialization.Wrappers;
using static NDecrypt.Core.CommonOperations;
using static SabreTools.Models.N3DS.Constants;

namespace NDecrypt.Core
{
Expand Down Expand Up @@ -123,13 +124,21 @@ private void DecryptAllPartitions(N3DS cart, bool force, Stream input, Stream ou
// Iterate over all 8 NCCH partitions
for (int p = 0; p < 8; p++)
{
// Check the partition exists
if (cart.Partitions[p] == null)
var partition = cart.Partitions[p];
if (partition == null || partition.MagicID != NCCHMagicNumber)
{
Console.WriteLine($"Partition {p} Not found... Skipping...");
continue;
}

// Check the partition has data
var partitionEntry = cart.PartitionsTable[p];
if (partitionEntry == null || partitionEntry.Length == 0)
{
Console.WriteLine($"Partition {p} No data... Skipping...");
continue;
}

// Decrypt the partition, if possible
if (ShouldDecryptPartition(cart, p, force))
DecryptPartition(cart, p, input, output);
Expand Down Expand Up @@ -213,22 +222,22 @@ private bool DecryptExtendedHeader(N3DS cart, int index, Stream input, Stream ou
uint partitionOffset = cart.GetPartitionOffset(index);
if (partitionOffset == 0 || partitionOffset > input.Length)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
Console.WriteLine($"Partition {index} No Data... Skipping...");
return false;
}

uint extHeaderSize = cart.GetExtendedHeaderSize(index);
if (extHeaderSize == 0)
{
Console.WriteLine($"Partition {index} RomFS: No Extended Header... Skipping...");
Console.WriteLine($"Partition {index} No Extended Header... Skipping...");
return false;
}

// Seek to the extended header
input.Seek(partitionOffset + 0x200, SeekOrigin.Begin);
output.Seek(partitionOffset + 0x200, SeekOrigin.Begin);

Console.WriteLine($"Partition {index} ExeFS: Decrypting: ExHeader");
Console.WriteLine($"Partition {index}: Decrypting - ExHeader");

// Create the Plain AES cipher for this partition
var cipher = CreateAESDecryptionCipher(KeysMap[index].NormalKey2C, cart.PlainIV(index));
Expand Down Expand Up @@ -289,7 +298,7 @@ private bool DecryptExeFS(N3DS cart, int index, Stream input, Stream output)
cipher,
input,
output,
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Decrypting: {s}"));
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Decrypting - {s}"));

return true;
}
Expand All @@ -315,7 +324,7 @@ private void DecryptExeFSFilenameTable(N3DS cart, int index, Stream input, Strea
input.Seek(exeFsOffset, SeekOrigin.Begin);
output.Seek(exeFsOffset, SeekOrigin.Begin);

Console.WriteLine($"Partition {index} ExeFS: Decrypting: ExeFS Filename Table");
Console.WriteLine($"Partition {index} ExeFS: Decrypting - ExeFS Filename Table");

// Create the ExeFS AES cipher for this partition
var cipher = CreateAESDecryptionCipher(KeysMap[index].NormalKey2C, cart.ExeFSIV(index));
Expand Down Expand Up @@ -386,7 +395,7 @@ private void DecryptExeFSFileEntries(N3DS cart, int index, Stream input, Stream
secondCipher,
input,
output,
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Decrypting: {fileHeader.FileName}...{s}"));
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Decrypting - {fileHeader.FileName}...{s}"));
}
}

Expand Down Expand Up @@ -426,7 +435,7 @@ private bool DecryptRomFS(N3DS cart, int index, Stream input, Stream output)
cipher,
input,
output,
(string s) => Console.WriteLine($"\rPartition {index} RomFS: Decrypting: {s}"));
(string s) => Console.WriteLine($"\rPartition {index} RomFS: Decrypting - {s}"));

return true;
}
Expand Down Expand Up @@ -484,12 +493,21 @@ private void EncryptAllPartitions(N3DS cart, bool force, Stream input, Stream ou
for (int p = 0; p < 8; p++)
{
// Check the partition exists
if (cart.Partitions[p] == null)
var partition = cart.Partitions[p];
if (partition == null || partition.MagicID != NCCHMagicNumber)
{
Console.WriteLine($"Partition {p} Not found... Skipping...");
continue;
}

// Check the partition has data
var partitionEntry = cart.PartitionsTable[p];
if (partitionEntry == null || partitionEntry.Length == 0)
{
Console.WriteLine($"Partition {p} No data... Skipping...");
continue;
}

// Encrypt the partition, if possible
if (ShouldEncryptPartition(cart, p, force))
EncryptPartition(cart, p, input, output);
Expand Down Expand Up @@ -578,22 +596,22 @@ private bool EncryptExtendedHeader(N3DS cart, int index, Stream input, Stream ou
uint partitionOffset = cart.GetPartitionOffset(index);
if (partitionOffset == 0 || partitionOffset > input.Length)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
Console.WriteLine($"Partition {index} No Data... Skipping...");
return false;
}

uint extHeaderSize = cart.GetExtendedHeaderSize(index);
if (extHeaderSize == 0)
{
Console.WriteLine($"Partition {index} RomFS: No Extended Header... Skipping...");
Console.WriteLine($"Partition {index} No Extended Header... Skipping...");
return false;
}

// Seek to the extended header
input.Seek(partitionOffset + 0x200, SeekOrigin.Begin);
output.Seek(partitionOffset + 0x200, SeekOrigin.Begin);

Console.WriteLine($"Partition {index} ExeFS: Encrypting: ExHeader");
Console.WriteLine($"Partition {index}: Encrypting - ExHeader");

// Create the Plain AES cipher for this partition
var cipher = CreateAESEncryptionCipher(KeysMap[index].NormalKey2C, cart.PlainIV(index));
Expand Down Expand Up @@ -659,7 +677,7 @@ private bool EncryptExeFS(N3DS cart, int index, Stream input, Stream output)
cipher,
input,
output,
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Encrypting: {s}"));
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Encrypting - {s}"));

return true;
}
Expand All @@ -685,7 +703,7 @@ private void EncryptExeFSFilenameTable(N3DS cart, int index, Stream input, Strea
input.Seek(exeFsOffset, SeekOrigin.Begin);
output.Seek(exeFsOffset, SeekOrigin.Begin);

Console.WriteLine($"Partition {index} ExeFS: Encrypting: ExeFS Filename Table");
Console.WriteLine($"Partition {index} ExeFS: Encrypting - ExeFS Filename Table");

// Create the ExeFS AES cipher for this partition
var cipher = CreateAESEncryptionCipher(KeysMap[index].NormalKey2C, cart.ExeFSIV(index));
Expand Down Expand Up @@ -757,7 +775,7 @@ private void EncryptExeFSFileEntries(N3DS cart, int index, Stream input, Stream
secondCipher,
input,
output,
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Encrypting: {fileHeader.FileName}...{s}"));
(string s) => Console.WriteLine($"\rPartition {index} ExeFS: Encrypting - {fileHeader.FileName}...{s}"));
}
}

Expand Down Expand Up @@ -804,7 +822,7 @@ private bool EncryptRomFS(N3DS cart, int index, Stream input, Stream output)
cipher,
input,
output,
(string s) => Console.WriteLine($"\rPartition {index} RomFS: Encrypting: {s}"));
(string s) => Console.WriteLine($"\rPartition {index} RomFS: Encrypting - {s}"));

return true;
}
Expand Down

0 comments on commit ce54eb2

Please sign in to comment.