From 6ae797a6ebac68965060a9448cec81437ce87cd1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 6 Sep 2025 18:14:39 -0400 Subject: [PATCH] Copy inputs to outputs for processing, optionally --- NDecrypt.Core/DSTool.cs | 26 +++++++++++++++++++------- NDecrypt.Core/ThreeDSTool.cs | 28 ++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/NDecrypt.Core/DSTool.cs b/NDecrypt.Core/DSTool.cs index 2b4d499..3896106 100644 --- a/NDecrypt.Core/DSTool.cs +++ b/NDecrypt.Core/DSTool.cs @@ -25,8 +25,14 @@ namespace NDecrypt.Core { try { - // Open the read and write on the same file for inplace processing - using var reader = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + // If the output is provided, copy the input file + if (output != null) + File.Copy(input, output); + else + output = input; + + // Open the output file for processing + using var reader = File.Open(output, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Deserialize the cart information var nitro = Nitro.Create(reader); @@ -47,7 +53,7 @@ namespace NDecrypt.Core nitro.EncryptSecureArea(_decryptArgs.NitroEncryptionData, force); // Write the encrypted secure area - using var writer = File.Open(input, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + using var writer = File.Open(output, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); writer.Seek(0x4000, SeekOrigin.Begin); writer.Write(nitro.SecureArea); writer.Flush(); @@ -56,7 +62,7 @@ namespace NDecrypt.Core } catch { - Console.WriteLine($"An error has occurred. {input} may be corrupted if it was partially processed."); + Console.WriteLine($"An error has occurred. {output} may be corrupted if it was partially processed."); Console.WriteLine("Please check that the file was a valid DS or DSi file and try again."); return false; } @@ -71,8 +77,14 @@ namespace NDecrypt.Core { try { + // If the output is provided, copy the input file + if (output != null) + File.Copy(input, output); + else + output = input; + // Open the read and write on the same file for inplace processing - using var reader = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var reader = File.Open(output, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Deserialize the cart information var nitro = Nitro.Create(reader); @@ -93,7 +105,7 @@ namespace NDecrypt.Core nitro.DecryptSecureArea(_decryptArgs.NitroEncryptionData, force); // Write the decrypted secure area - using var writer = File.Open(input, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + using var writer = File.Open(output, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); writer.Seek(0x4000, SeekOrigin.Begin); writer.Write(nitro.SecureArea); writer.Flush(); @@ -102,7 +114,7 @@ namespace NDecrypt.Core } catch { - Console.WriteLine($"An error has occurred. {input} may be corrupted if it was partially processed."); + Console.WriteLine($"An error has occurred. {output} may be corrupted if it was partially processed."); Console.WriteLine("Please check that the file was a valid DS or DSi file and try again."); return false; } diff --git a/NDecrypt.Core/ThreeDSTool.cs b/NDecrypt.Core/ThreeDSTool.cs index 1cce755..4abf317 100644 --- a/NDecrypt.Core/ThreeDSTool.cs +++ b/NDecrypt.Core/ThreeDSTool.cs @@ -46,9 +46,15 @@ namespace NDecrypt.Core try { - // Open the read and write on the same file for inplace processing - using var reader = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - using var writer = File.Open(input, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + // If the output is provided, copy the input file + if (output != null) + File.Copy(input, output); + else + output = input; + + // Open the output file for processing + using var reader = File.Open(output, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var writer = File.Open(output, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); // Deserialize the cart information var cart = N3DS.Create(reader); @@ -64,7 +70,7 @@ namespace NDecrypt.Core } catch { - Console.WriteLine($"An error has occurred. {input} may be corrupted if it was partially processed."); + Console.WriteLine($"An error has occurred. {output} may be corrupted if it was partially processed."); Console.WriteLine("Please check that the file was a valid 3DS or New 3DS cart image and try again."); return false; } @@ -459,9 +465,15 @@ namespace NDecrypt.Core try { - // Open the read and write on the same file for inplace processing - using var reader = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - using var writer = File.Open(input, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + // If the output is provided, copy the input file + if (output != null) + File.Copy(input, output); + else + output = input; + + // Open the output file for processing + using var reader = File.Open(output, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var writer = File.Open(output, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); // Deserialize the cart information var cart = N3DS.Create(reader); @@ -477,7 +489,7 @@ namespace NDecrypt.Core } catch { - Console.WriteLine($"An error has occurred. {input} may be corrupted if it was partially processed."); + Console.WriteLine($"An error has occurred. {output} may be corrupted if it was partially processed."); Console.WriteLine("Please check that the file was a valid 3DS or New 3DS cart image and try again."); return false; }