Implement unused extractions

This commit is contained in:
Matt Nadareski
2023-03-09 14:39:26 -05:00
parent 3c064bad55
commit 1dca51988a
25 changed files with 442 additions and 46 deletions

View File

@@ -12,7 +12,10 @@ namespace BinaryObjectScanner.Interfaces
/// </summary>
/// <param name="file">Path to the input file</param>
/// <returns>Path to extracted files, null on error</returns>
/// <remarks>Ideally, this should just point to the other extract implementation</remarks>
/// <remarks>
/// Ideally, this should just point to the other extract implementation.
/// It is expected that the calling method will provide exception handling.
/// </remarks>
string Extract(string file);
/// <summary>
@@ -21,6 +24,7 @@ namespace BinaryObjectScanner.Interfaces
/// <param name="stream">Stream representing the input file</param>
/// <param name="file">Path to the input file</param>
/// <returns>Path to extracted files, null on error</returns>
/// <remarks>It is expected that the calling method will provide exception handling.</remarks>
string Extract(Stream stream, string file);
}
}

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.BFPK bfpk = BinaryObjectScanner.Wrappers.BFPK.Create(stream);
if (bfpk == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Extract all files
bfpk.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Concurrent;
using System.IO;
using BurnOutSharp.Interfaces;
using BinaryObjectScanner.Interfaces;
using static BinaryObjectScanner.Utilities.Dictionary;
namespace BurnOutSharp.FileType
@@ -9,8 +10,39 @@ namespace BurnOutSharp.FileType
/// <summary>
/// Half-Life Level
/// </summary>
public class BSP : IScannable
public class BSP : IExtractable, IScannable
{
/// <inheritdoc/>
public string Extract(string file)
{
if (!File.Exists(file))
return null;
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return Extract(fs, file);
}
}
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Create the wrapper
BinaryObjectScanner.Wrappers.BSP bsp = BinaryObjectScanner.Wrappers.BSP.Create(stream);
if (bsp == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
bsp.ExtractAllLumps(tempPath);
bsp.ExtractAllTextures(tempPath);
return tempPath;
}
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
{

View File

@@ -29,8 +29,20 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (BZip2Stream bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true))
{
string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
using (FileStream fs = File.OpenWrite(tempFile))
{
bz2File.CopyTo(fs);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,46 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (CompoundFile msi = new CompoundFile(stream, CFSUpdateMode.ReadOnly, CFSConfiguration.Default))
{
msi.RootStorage.VisitEntries((e) =>
{
if (!e.IsStream)
return;
var str = msi.RootStorage.GetStream(e.Name);
if (str == null)
return;
byte[] strData = str.GetData();
if (strData == null)
return;
string decoded = DecodeStreamName(e.Name).TrimEnd('\0');
byte[] nameBytes = Encoding.UTF8.GetBytes(e.Name);
// UTF-8 encoding of 0x4840.
if (nameBytes[0] == 0xe4 && nameBytes[1] == 0xa1 && nameBytes[2] == 0x80)
decoded = decoded.Substring(3);
foreach (char c in Path.GetInvalidFileNameChars())
{
decoded = decoded.Replace(c, '_');
}
string filename = Path.Combine(tempPath, decoded);
using (Stream fs = File.OpenWrite(filename))
{
fs.Write(strData, 0, strData.Length);
}
}, recursive: true);
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.GCF gcf = BinaryObjectScanner.Wrappers.GCF.Create(stream);
if (gcf == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
gcf.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,24 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (GZipArchive zipFile = GZipArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,28 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
UnshieldSharp.Archive.InstallShieldArchiveV3 archive = new UnshieldSharp.Archive.InstallShieldArchiveV3(file);
foreach (CompressedFile cfile in archive.Files.Select(kvp => kvp.Value))
{
string tempFile = Path.Combine(tempPath, cfile.FullPath);
if (!Directory.Exists(Path.GetDirectoryName(tempFile)))
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
(byte[] fileContents, string error) = archive.Extract(cfile.FullPath);
if (!string.IsNullOrWhiteSpace(error))
continue;
using (FileStream fs = File.OpenWrite(tempFile))
{
fs.Write(fileContents, 0, fileContents.Length);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,32 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
InstallShieldCabinet cabfile = InstallShieldCabinet.Open(file);
for (int i = 0; i < cabfile.FileCount; i++)
{
// Check if the file is valid first
if (!cabfile.FileIsValid(i))
continue;
string tempFile;
try
{
string filename = cabfile.FileName(i);
tempFile = Path.Combine(tempPath, filename);
}
catch
{
tempFile = Path.Combine(tempPath, $"BAD_FILENAME{i}");
}
cabfile.FileSave(i, tempFile);
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,11 +27,48 @@ namespace BurnOutSharp.FileType
}
}
// TODO: Add stream opening support
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
#if NET6_0_OR_GREATER
// Not supported for .NET 6.0 due to Windows DLL requirements
return null;
#else
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (MpqArchive mpqArchive = new MpqArchive(file, FileAccess.Read))
{
// Try to open the listfile
string listfile = null;
MpqFileStream listStream = mpqArchive.OpenFile("(listfile)");
// If we can't read the listfile, we just return
if (!listStream.CanRead)
return null;
// Read the listfile in for processing
using (StreamReader sr = new StreamReader(listStream))
{
listfile = sr.ReadToEnd();
}
// Split the listfile by newlines
string[] listfileLines = listfile.Replace("\r\n", "\n").Split('\n');
// Loop over each entry
foreach (string sub in listfileLines)
{
string tempFile = Path.Combine(tempPath, sub);
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
mpqArchive.ExtractFile(sub, tempFile);
}
}
return tempPath;
#endif
}
/// <inheritdoc/>

View File

@@ -30,8 +30,21 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Open the cab file
var cabFile = MicrosoftCabinet.Create(stream);
if (cabFile == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// If entry extraction fails
bool success = cabFile.ExtractAll(tempPath);
if (!success)
return null;
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,33 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
byte[] data = LZ.Decompress(stream);
// Create the temp filename
string tempFile = "temp.bin";
if (!string.IsNullOrEmpty(file))
{
string expandedFilePath = LZ.GetExpandedName(file, out _);
tempFile = Path.GetFileName(expandedFilePath).TrimEnd('\0');
if (tempFile.EndsWith(".ex"))
tempFile += "e";
else if (tempFile.EndsWith(".dl"))
tempFile += "l";
}
tempFile = Path.Combine(tempPath, tempFile);
// Write the file data to a temp file
using (Stream tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
tempStream.Write(data, 0, data.Length);
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.PAK pak = BinaryObjectScanner.Wrappers.PAK.Create(stream);
if (pak == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
pak.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.PFF pff = BinaryObjectScanner.Wrappers.PFF.Create(stream);
if (pff == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Extract all files
pff.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,25 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (ZipArchive zipFile = ZipArchive.Open(stream))
{
foreach (var entry in zipFile.Entries)
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
entry.WriteToFile(tempFile);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,24 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (RarArchive rarFile = RarArchive.Open(stream))
{
foreach (var entry in rarFile.Entries)
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -28,7 +28,6 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
}

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.SGA sga = BinaryObjectScanner.Wrappers.SGA.Create(stream);
if (sga == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
sga.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,24 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (SevenZipArchive sevenZipFile = SevenZipArchive.Open(stream))
{
foreach (var entry in sevenZipFile.Entries)
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -29,8 +29,24 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (TarArchive tarFile = TarArchive.Open(stream))
{
foreach (var entry in tarFile.Entries)
{
// If we have a directory, skip it
if (entry.IsDirectory)
continue;
string tempFile = Path.Combine(tempPath, entry.Key);
entry.WriteToFile(tempFile);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.VBSP vbsp = BinaryObjectScanner.Wrappers.VBSP.Create(stream);
if (vbsp == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
vbsp.ExtractAllLumps(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.VPK vpk = BinaryObjectScanner.Wrappers.VPK.Create(stream);
if (vpk == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
vpk.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.WAD wad = BinaryObjectScanner.Wrappers.WAD.Create(stream);
if (wad == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
wad.ExtractAllLumps(tempPath);
return tempPath;
}
/// <inheritdoc/>

View File

@@ -28,8 +28,20 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (XZStream xzFile = new XZStream(stream))
{
string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
using (FileStream fs = File.OpenWrite(tempFile))
{
xzFile.CopyTo(fs);
}
}
return tempPath;
}
/// <inheritdoc/>

View File

@@ -27,8 +27,19 @@ namespace BurnOutSharp.FileType
/// <inheritdoc/>
public string Extract(Stream stream, string file)
{
// Implement from existing Scan
return null;
// Create the wrapper
BinaryObjectScanner.Wrappers.XZP xzp = BinaryObjectScanner.Wrappers.XZP.Create(stream);
if (xzp == null)
return null;
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Loop through and extract all files
xzp.ExtractAll(tempPath);
return tempPath;
}
/// <inheritdoc/>