Use wrapper in BFPK scans

This commit is contained in:
Matt Nadareski
2022-12-14 23:01:06 -08:00
parent a47c778b0e
commit 6d43afb258

View File

@@ -35,71 +35,15 @@ namespace BurnOutSharp.FileType
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
stream.ReadBytes(4); // Skip magic number
// Create the wrapper
Wrappers.BFPK bfpk = Wrappers.BFPK.Create(stream);
if (bfpk == null)
return null;
int version = stream.ReadInt32();
int files = stream.ReadInt32();
long current = stream.Position;
for (int i = 0; i < files; i++)
// Loop through and extract all files
for (int i = 0; i < bfpk.Files; i++)
{
stream.Seek(current, SeekOrigin.Begin);
int nameSize = stream.ReadInt32();
string name = new string(stream.ReadBytes(nameSize).Select(b => (char)b).ToArray());
uint uncompressedSize = stream.ReadUInt32();
int offset = stream.ReadInt32();
current = stream.Position;
stream.Seek(offset, SeekOrigin.Begin);
uint compressedSize = stream.ReadUInt32();
// Some files can lack the length prefix
if (compressedSize > stream.Length)
{
stream.Seek(-4, SeekOrigin.Current);
compressedSize = uncompressedSize;
}
// If an individual entry fails
try
{
string tempFile = Path.Combine(tempPath, name);
if (!Directory.Exists(Path.GetDirectoryName(tempFile)))
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
if (compressedSize == uncompressedSize)
{
using (FileStream fs = File.OpenWrite(tempFile))
{
fs.Write(stream.ReadBytes((int)uncompressedSize), 0, (int)uncompressedSize);
}
}
else
{
using (FileStream fs = File.OpenWrite(tempFile))
{
try
{
ZlibStream zs = new ZlibStream(stream, CompressionMode.Decompress);
zs.CopyTo(fs);
}
catch (ZlibException)
{
stream.Seek(offset + 4, SeekOrigin.Begin);
fs.Write(stream.ReadBytes((int)compressedSize), 0, (int)compressedSize);
}
}
}
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
stream.Seek(current, SeekOrigin.Begin);
bfpk.ExtractFile(i, tempPath);
}
// Collect and format all found protections