Add scanner to all archive signatures (nw)

This commit is contained in:
Matt Nadareski
2020-10-31 00:06:41 -07:00
parent 0dd71d72ca
commit 5b980e138a
14 changed files with 237 additions and 116 deletions

View File

@@ -18,7 +18,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -28,6 +28,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true))
{
br.ReadBytes(4); // Skip magic number

View File

@@ -17,7 +17,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -27,6 +27,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (BZip2Stream bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true))
{
// If an individual entry fails

View File

@@ -17,7 +17,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -27,6 +27,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (GZipArchive zipFile = GZipArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)

View File

@@ -18,7 +18,7 @@ namespace BurnOutSharp.FileType
}
// TODO: Add stream opening support
public static List<string> Scan(string file, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, string file, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -42,6 +42,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
UnshieldCabinet cabfile = UnshieldCabinet.Open(file);
for (int i = 0; i < cabfile.FileCount; i++)
{

View File

@@ -17,7 +17,7 @@ namespace BurnOutSharp.FileType
}
// TODO: Add stream opening support
public static List<string> Scan(string file, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, string file, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -27,6 +27,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (MpqArchive mpqArchive = new MpqArchive(file, FileAccess.Read))
{
string listfile = null;

View File

@@ -21,7 +21,7 @@ namespace BurnOutSharp.FileType
}
// TODO: Add stream opening support
public static List<string> Scan(string file, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, string file, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -31,6 +31,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (Database msidb = new Database(file, DatabaseOpenMode.ReadOnly))
{
msidb.ExportAll(tempPath);

View File

@@ -17,7 +17,7 @@ namespace BurnOutSharp.FileType
}
// TODO: Add stream opening support
public static List<string> Scan(string file, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, string file, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -27,6 +27,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (MSCabinet cabfile = new MSCabinet(file))
{
foreach (var sub in cabfile.GetFiles())

View File

@@ -26,7 +26,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -36,6 +36,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (ZipArchive zipFile = ZipArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)

View File

@@ -22,7 +22,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -32,6 +32,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (RarArchive zipFile = RarArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)

View File

@@ -17,7 +17,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -27,6 +27,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (SevenZipArchive sevenZipFile = SevenZipArchive.Open(stream))
{
foreach (var entry in sevenZipFile.Entries)

View File

@@ -20,7 +20,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -30,6 +30,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (TarArchive tarFile = TarArchive.Open(stream))
{
foreach (var entry in tarFile.Entries)

View File

@@ -34,13 +34,21 @@ namespace BurnOutSharp.FileType
}
// TODO: Add stream opening support
public static List<string> Scan(string file, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, string file, bool includePosition = false)
{
List<string> protections = new List<string>();
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
string[] args = new string[]
{
"-p", file,

View File

@@ -16,7 +16,7 @@ namespace BurnOutSharp.FileType
return false;
}
public static List<string> Scan(Stream stream, bool includePosition = false)
public static List<string> Scan(Scanner parentScanner, Stream stream, bool includePosition = false)
{
List<string> protections = new List<string>();
@@ -26,6 +26,14 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create a new scanner for the new temp path
Scanner subScanner = new Scanner(tempPath, parentScanner.FileProgress)
{
IncludePosition = parentScanner.IncludePosition,
ScanAllFiles = parentScanner.ScanAllFiles,
ScanArchives = parentScanner.ScanArchives,
};
using (XZStream xzFile = new XZStream(stream))
{
// If an individual entry fails

View File

@@ -48,10 +48,22 @@ namespace BurnOutSharp
FileProgress = fileProgress;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="paths">Paths to create a scanner for</param>
/// <param name="fileProgress">Optional progress callback</param>
public Scanner(List<string> paths, IProgress<FileProtection> fileProgress = null)
{
Paths = paths;
FileProgress = fileProgress;
}
/// <summary>
/// Scan the list of paths and get all found protections
/// </summary>
/// <returns>Dictionary of list of strings representing the found protections</returns>
/// TODO: Should this populate an internal field instead of returning?
public Dictionary<string, List<string>> GetProtections()
{
// If we have no paths, we can't scan
@@ -158,7 +170,7 @@ namespace BurnOutSharp
/// <param name="path">Path of the directory or file to scan</param>
/// <param name="files">Files contained within if the path is a directory</param>
/// <returns>Dictionary of list of strings representing the found protections</returns>
public Dictionary<string, List<string>> GetPathProtections(string path, List<string> files = null)
private Dictionary<string, List<string>> GetPathProtections(string path, List<string> files = null)
{
List<string> protections = new List<string>();
string protection;
@@ -446,134 +458,139 @@ namespace BurnOutSharp
#region Archive File Types
// 7-Zip archive
if (SevenZip.ShouldScan(magic))
// If we're scanning archives, we have a few to try out
// TODO: All archives should return a dictionary instead of a list
if (ScanArchives)
{
var subProtections = SevenZip.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// 7-Zip archive
if (SevenZip.ShouldScan(magic))
{
var subProtections = SevenZip.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// BFPK archive
if (BFPK.ShouldScan(magic))
{
var subProtections = BFPK.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// BFPK archive
if (BFPK.ShouldScan(magic))
{
var subProtections = BFPK.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// BZip2
if (BZip2.ShouldScan(magic))
{
var subProtections = BZip2.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// BZip2
if (BZip2.ShouldScan(magic))
{
var subProtections = BZip2.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// GZIP
if (GZIP.ShouldScan(magic))
{
var subProtections = GZIP.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// GZIP
if (GZIP.ShouldScan(magic))
{
var subProtections = GZIP.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// InstallShield Cabinet
if (file != null && InstallShieldCAB.ShouldScan(magic))
{
var subProtections = InstallShieldCAB.Scan(file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// InstallShield Cabinet
if (file != null && InstallShieldCAB.ShouldScan(magic))
{
var subProtections = InstallShieldCAB.Scan(this, file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// Microsoft Cabinet
if (file != null && MicrosoftCAB.ShouldScan(magic))
{
var subProtections = MicrosoftCAB.Scan(file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// Microsoft Cabinet
if (file != null && MicrosoftCAB.ShouldScan(magic))
{
var subProtections = MicrosoftCAB.Scan(this, file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// MSI
if (file != null && MSI.ShouldScan(magic))
{
var subProtections = MSI.Scan(file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// MSI
if (file != null && MSI.ShouldScan(magic))
{
var subProtections = MSI.Scan(this, file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// MPQ archive
if (file != null && MPQ.ShouldScan(magic))
{
var subProtections = MPQ.Scan(file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// MPQ archive
if (file != null && MPQ.ShouldScan(magic))
{
var subProtections = MPQ.Scan(this, file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// PKZIP archive (and derivatives)
if (PKZIP.ShouldScan(magic))
{
var subProtections = PKZIP.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// PKZIP archive (and derivatives)
if (PKZIP.ShouldScan(magic))
{
var subProtections = PKZIP.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// RAR archive
if (RAR.ShouldScan(magic))
{
var subProtections = RAR.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// RAR archive
if (RAR.ShouldScan(magic))
{
var subProtections = RAR.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// Tape Archive
if (TapeArchive.ShouldScan(magic))
{
var subProtections = TapeArchive.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// Tape Archive
if (TapeArchive.ShouldScan(magic))
{
var subProtections = TapeArchive.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// Valve archive formats
if (file != null && Valve.ShouldScan(magic))
{
var subProtections = Valve.Scan(file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// Valve archive formats
if (file != null && Valve.ShouldScan(magic))
{
var subProtections = Valve.Scan(this, file, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
}
protections[file] = subProtections;
}
// XZ
if (XZ.ShouldScan(magic))
{
var subProtections = XZ.Scan(fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
// XZ
if (XZ.ShouldScan(magic))
{
var subProtections = XZ.Scan(this, fs, IncludePosition);
if (!protections.ContainsKey(file))
protections[file] = new List<string>();
protections[file] = subProtections;
protections[file] = subProtections;
}
}
#endregion