Skip to content

Commit 482644a

Browse files
authored
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
1 parent 45661f3 commit 482644a

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

BurnOutSharp/BurnOutSharp.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>BurnOutSharp</id>
5-
<version>1.03.8.0</version>
5+
<version>1.03.8.1</version>
66
<title>BurnOutSharp</title>
77
<authors>Matt Nadareski, Gernot Knippen</authors>
88
<owners>Matt Nadareski, Gernot Knippen</owners>

BurnOutSharp/ProtectionFind.cs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.IO;
2222
using System.Linq;
2323
using System.Text;
24+
using System.Text.RegularExpressions;
2425
using System.Threading;
2526
using BurnOutSharp.ProtectionType;
2627
using LibMSPackN;
@@ -436,36 +437,51 @@ public static string ScanInFile(string file)
436437
// InstallShield CAB
437438
else if (magic.StartsWith("ISc"))
438439
{
439-
try
440+
// Get the name of the first cabinet file or header
441+
string directory = Path.GetDirectoryName(file);
442+
string noExtension = Path.GetFileNameWithoutExtension(file);
443+
string filenamePattern = Path.Combine(directory, noExtension);
444+
filenamePattern = new Regex(@"\d+$").Replace(filenamePattern, string.Empty);
445+
446+
bool cabinetHeaderExists = File.Exists(Path.Combine(directory, filenamePattern + "1.hdr"));
447+
bool shouldScanCabinet = cabinetHeaderExists
448+
? file.Equals(Path.Combine(directory, filenamePattern + "1.hdr"), StringComparison.OrdinalIgnoreCase)
449+
: file.Equals(Path.Combine(directory, filenamePattern + "1.cab"), StringComparison.OrdinalIgnoreCase);
450+
451+
// If we have the first file
452+
if (shouldScanCabinet)
440453
{
441-
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
442-
Directory.CreateDirectory(tempPath);
443-
444-
UnshieldCabinet cabfile = UnshieldCabinet.Open(file);
445-
for (int i = 0; i < cabfile.FileCount; i++)
454+
try
446455
{
447-
string tempFileName = Path.Combine(tempPath, cabfile.FileName(i));
448-
if (cabfile.FileSave(i, tempFileName))
456+
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
457+
Directory.CreateDirectory(tempPath);
458+
459+
UnshieldCabinet cabfile = UnshieldCabinet.Open(file);
460+
for (int i = 0; i < cabfile.FileCount; i++)
449461
{
450-
string protection = ScanInFile(tempFileName);
451-
try
462+
string tempFileName = Path.Combine(tempPath, cabfile.FileName(i));
463+
if (cabfile.FileSave(i, tempFileName))
452464
{
453-
File.Delete(tempFileName);
465+
string protection = ScanInFile(tempFileName);
466+
try
467+
{
468+
File.Delete(tempFileName);
469+
}
470+
catch { }
471+
472+
if (!string.IsNullOrEmpty(protection))
473+
protections.Add(protection);
454474
}
455-
catch { }
456-
457-
if (!string.IsNullOrEmpty(protection))
458-
protections.Add(protection);
459475
}
460-
}
461476

462-
try
463-
{
464-
Directory.Delete(tempPath, true);
477+
try
478+
{
479+
Directory.Delete(tempPath, true);
480+
}
481+
catch { }
465482
}
466483
catch { }
467484
}
468-
catch { }
469485
}
470486

471487
// Microsoft CAB

BurnOutSharp/ProtectionType/CactusDataShield.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Text;
56

67
namespace BurnOutSharp.ProtectionType
78
{
@@ -11,8 +12,7 @@ public static string CheckContents(string file)
1112
{
1213
if (Path.GetFileName(file) == "CDSPlayer.app")
1314
{
14-
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
15-
using (var sr = new StreamReader(fs))
15+
using (var sr = new StreamReader(file, Encoding.Default))
1616
{
1717
return "Cactus Data Shield " + sr.ReadLine().Substring(3) + "(" + sr.ReadLine() + ")";
1818
}

BurnOutSharp/ProtectionType/PSXAntiModchip.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
4+
using System.Text;
45

56
namespace BurnOutSharp.ProtectionType
67
{
@@ -26,28 +27,19 @@ public static string CheckPath(string path, IEnumerable<string> files, bool isDi
2627
{
2728
foreach (string file in files)
2829
{
29-
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
30-
using (var sr = new StreamReader(fs))
30+
// Load the current file content
31+
string fileContent = null;
32+
using (StreamReader sr = new StreamReader(file, Encoding.Default))
3133
{
32-
string fileContent = sr.ReadToEnd();
33-
string protection = CheckContents(path, fileContent);
34-
if (!string.IsNullOrWhiteSpace(protection))
35-
return protection;
34+
fileContent = sr.ReadToEnd();
3635
}
36+
37+
string protection = CheckContents(path, fileContent);
38+
if (!string.IsNullOrWhiteSpace(protection))
39+
return protection;
3740
}
3841
}
39-
}
40-
else
41-
{
42-
using (var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
43-
using (var sr = new StreamReader(fs))
44-
{
45-
string fileContent = sr.ReadToEnd();
46-
string protection = CheckContents(path, fileContent);
47-
if (!string.IsNullOrWhiteSpace(protection))
48-
return protection;
49-
}
50-
}
42+
}
5143

5244
return null;
5345
}

0 commit comments

Comments
 (0)