From 482644af8570d7e992c4e35d34a8990794b4fbed Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 24 Oct 2019 16:09:43 -0400 Subject: [PATCH] Fix Memory Issues (#4) * Fix a couple of protection scans (possible mem issues) * Don't open the file contents on path scan for antimodchip * IS-CAB intermediate filtering to reduce scan times * Update NuGet version --- BurnOutSharp/BurnOutSharp.nuspec | 2 +- BurnOutSharp/ProtectionFind.cs | 64 ++++++++++++------- .../ProtectionType/CactusDataShield.cs | 4 +- BurnOutSharp/ProtectionType/PSXAntiModchip.cs | 28 +++----- 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/BurnOutSharp/BurnOutSharp.nuspec b/BurnOutSharp/BurnOutSharp.nuspec index 6403a26a..ad76f496 100644 --- a/BurnOutSharp/BurnOutSharp.nuspec +++ b/BurnOutSharp/BurnOutSharp.nuspec @@ -2,7 +2,7 @@ BurnOutSharp - 1.03.8.0 + 1.03.8.1 BurnOutSharp Matt Nadareski, Gernot Knippen Matt Nadareski, Gernot Knippen diff --git a/BurnOutSharp/ProtectionFind.cs b/BurnOutSharp/ProtectionFind.cs index a23eee4a..bfb96744 100644 --- a/BurnOutSharp/ProtectionFind.cs +++ b/BurnOutSharp/ProtectionFind.cs @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading; using BurnOutSharp.ProtectionType; using LibMSPackN; @@ -436,36 +437,51 @@ namespace BurnOutSharp // InstallShield CAB else if (magic.StartsWith("ISc")) { - try + // Get the name of the first cabinet file or header + string directory = Path.GetDirectoryName(file); + string noExtension = Path.GetFileNameWithoutExtension(file); + string filenamePattern = Path.Combine(directory, noExtension); + filenamePattern = new Regex(@"\d+$").Replace(filenamePattern, string.Empty); + + bool cabinetHeaderExists = File.Exists(Path.Combine(directory, filenamePattern + "1.hdr")); + bool shouldScanCabinet = cabinetHeaderExists + ? file.Equals(Path.Combine(directory, filenamePattern + "1.hdr"), StringComparison.OrdinalIgnoreCase) + : file.Equals(Path.Combine(directory, filenamePattern + "1.cab"), StringComparison.OrdinalIgnoreCase); + + // If we have the first file + if (shouldScanCabinet) { - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - UnshieldCabinet cabfile = UnshieldCabinet.Open(file); - for (int i = 0; i < cabfile.FileCount; i++) - { - string tempFileName = Path.Combine(tempPath, cabfile.FileName(i)); - if (cabfile.FileSave(i, tempFileName)) - { - string protection = ScanInFile(tempFileName); - try - { - File.Delete(tempFileName); - } - catch { } - - if (!string.IsNullOrEmpty(protection)) - protections.Add(protection); - } - } - try { - Directory.Delete(tempPath, true); + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(tempPath); + + UnshieldCabinet cabfile = UnshieldCabinet.Open(file); + for (int i = 0; i < cabfile.FileCount; i++) + { + string tempFileName = Path.Combine(tempPath, cabfile.FileName(i)); + if (cabfile.FileSave(i, tempFileName)) + { + string protection = ScanInFile(tempFileName); + try + { + File.Delete(tempFileName); + } + catch { } + + if (!string.IsNullOrEmpty(protection)) + protections.Add(protection); + } + } + + try + { + Directory.Delete(tempPath, true); + } + catch { } } catch { } } - catch { } } // Microsoft CAB diff --git a/BurnOutSharp/ProtectionType/CactusDataShield.cs b/BurnOutSharp/ProtectionType/CactusDataShield.cs index 7e4830a8..d283a2b9 100644 --- a/BurnOutSharp/ProtectionType/CactusDataShield.cs +++ b/BurnOutSharp/ProtectionType/CactusDataShield.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; namespace BurnOutSharp.ProtectionType { @@ -11,8 +12,7 @@ namespace BurnOutSharp.ProtectionType { if (Path.GetFileName(file) == "CDSPlayer.app") { - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (var sr = new StreamReader(fs)) + using (var sr = new StreamReader(file, Encoding.Default)) { return "Cactus Data Shield " + sr.ReadLine().Substring(3) + "(" + sr.ReadLine() + ")"; } diff --git a/BurnOutSharp/ProtectionType/PSXAntiModchip.cs b/BurnOutSharp/ProtectionType/PSXAntiModchip.cs index e2402e56..85dffc5b 100644 --- a/BurnOutSharp/ProtectionType/PSXAntiModchip.cs +++ b/BurnOutSharp/ProtectionType/PSXAntiModchip.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; namespace BurnOutSharp.ProtectionType { @@ -26,28 +27,19 @@ namespace BurnOutSharp.ProtectionType { foreach (string file in files) { - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (var sr = new StreamReader(fs)) + // Load the current file content + string fileContent = null; + using (StreamReader sr = new StreamReader(file, Encoding.Default)) { - string fileContent = sr.ReadToEnd(); - string protection = CheckContents(path, fileContent); - if (!string.IsNullOrWhiteSpace(protection)) - return protection; + fileContent = sr.ReadToEnd(); } + + string protection = CheckContents(path, fileContent); + if (!string.IsNullOrWhiteSpace(protection)) + return protection; } } - } - else - { - using (var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (var sr = new StreamReader(fs)) - { - string fileContent = sr.ReadToEnd(); - string protection = CheckContents(path, fileContent); - if (!string.IsNullOrWhiteSpace(protection)) - return protection; - } - } + } return null; }