2022-12-28 22:54:56 -08:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2023-03-07 16:59:14 -05:00
|
|
|
using BinaryObjectScanner.Compression;
|
2023-03-09 14:04:31 -05:00
|
|
|
using BinaryObjectScanner.Interfaces;
|
2022-12-28 22:54:56 -08:00
|
|
|
|
2023-03-10 13:48:24 -05:00
|
|
|
namespace BinaryObjectScanner.FileType
|
2022-12-28 22:54:56 -08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Microsoft LZ-compressed Files (LZ32)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>This is treated like an archive type due to the packing style</remarks>
|
2023-03-09 15:07:35 -05:00
|
|
|
public class MicrosoftLZ : IExtractable
|
2022-12-28 22:54:56 -08:00
|
|
|
{
|
2023-03-09 14:04:31 -05:00
|
|
|
/// <inheritdoc/>
|
2023-03-09 17:16:39 -05:00
|
|
|
public string Extract(string file, bool includeDebug)
|
2023-03-09 14:04:31 -05:00
|
|
|
{
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
return Extract(fs, file, includeDebug);
|
2023-03-09 14:04:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2023-03-09 17:16:39 -05:00
|
|
|
public string Extract(Stream stream, string file, bool includeDebug)
|
2023-03-09 14:04:31 -05:00
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Create a temp output directory
|
|
|
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2023-03-09 14:39:26 -05:00
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
byte[] data = LZ.Decompress(stream);
|
2023-03-09 14:39:26 -05:00
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
// 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);
|
2023-03-09 14:39:26 -05:00
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
// 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);
|
|
|
|
|
}
|
2023-03-09 14:39:26 -05:00
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
return tempPath;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2023-03-09 14:39:26 -05:00
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
|
|
|
return null;
|
2023-03-09 14:39:26 -05:00
|
|
|
}
|
2023-03-09 14:04:31 -05:00
|
|
|
}
|
2022-12-28 22:54:56 -08:00
|
|
|
}
|
|
|
|
|
}
|