Files
BinaryObjectScanner/BurnOutSharp/FileType/BFPK.cs

141 lines
5.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
2022-05-01 17:41:50 -07:00
using BurnOutSharp.Interfaces;
2021-08-25 15:09:42 -07:00
using BurnOutSharp.Tools;
using SharpCompress.Compressors;
using SharpCompress.Compressors.Deflate;
namespace BurnOutSharp.FileType
{
public class BFPK : IScannable
{
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
{
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0x42, 0x46, 0x50, 0x4b }))
return true;
return false;
}
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
2021-02-26 09:26:23 -08:00
{
if (!File.Exists(file))
return null;
using (var fs = File.OpenRead(file))
{
return Scan(scanner, fs, file);
}
}
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
// If the BFPK file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true))
{
br.ReadBytes(4); // Skip magic number
int version = br.ReadInt32();
int files = br.ReadInt32();
long current = br.BaseStream.Position;
for (int i = 0; i < files; i++)
{
br.BaseStream.Seek(current, SeekOrigin.Begin);
int nameSize = br.ReadInt32();
string name = new string(br.ReadChars(nameSize));
uint uncompressedSize = br.ReadUInt32();
int offset = br.ReadInt32();
current = br.BaseStream.Position;
br.BaseStream.Seek(offset, SeekOrigin.Begin);
uint compressedSize = br.ReadUInt32();
2022-05-15 20:58:27 -07:00
// Some files can lack the length prefix
if (compressedSize > br.BaseStream.Length)
{
br.BaseStream.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(br.ReadBytes((int)uncompressedSize), 0, (int)uncompressedSize);
}
}
else
{
using (FileStream fs = File.OpenWrite(tempFile))
{
try
{
ZlibStream zs = new ZlibStream(br.BaseStream, CompressionMode.Decompress);
zs.CopyTo(fs);
}
catch (ZlibException)
{
br.BaseStream.Seek(offset + 4, SeekOrigin.Begin);
fs.Write(br.ReadBytes((int)compressedSize), 0, (int)compressedSize);
}
}
}
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
br.BaseStream.Seek(current, SeekOrigin.Begin);
}
}
2020-10-28 12:17:10 -07:00
// Collect and format all found protections
2020-10-31 14:00:31 -07:00
var protections = scanner.GetProtections(tempPath);
2020-10-28 12:17:10 -07:00
// If temp directory cleanup fails
try
{
Directory.Delete(tempPath, true);
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
return null;
}
}
}