From 6d43afb258f8e2120bfcfb2d6d7a233997596759 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 14 Dec 2022 23:01:06 -0800 Subject: [PATCH] Use wrapper in BFPK scans --- BurnOutSharp/FileType/BFPK.cs | 70 ++++------------------------------- 1 file changed, 7 insertions(+), 63 deletions(-) diff --git a/BurnOutSharp/FileType/BFPK.cs b/BurnOutSharp/FileType/BFPK.cs index 5d1039b5..ab6b06c2 100644 --- a/BurnOutSharp/FileType/BFPK.cs +++ b/BurnOutSharp/FileType/BFPK.cs @@ -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