2020-09-10 21:10:32 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
2023-03-09 14:04:31 -05:00
|
|
|
|
using BinaryObjectScanner.Interfaces;
|
2021-07-21 13:33:52 -07:00
|
|
|
|
using UnshieldSharp.Cabinet;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2023-03-10 13:48:24 -05:00
|
|
|
|
namespace BinaryObjectScanner.FileType
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2022-12-08 21:32:52 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// InstallShield cabinet file
|
|
|
|
|
|
/// </summary>
|
2023-03-09 15:07:35 -05:00
|
|
|
|
public class InstallShieldCAB : IExtractable
|
2020-09-10 21:10:32 -07: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 14:50:24 -05:00
|
|
|
|
// Get the name of the first cabinet file or header
|
|
|
|
|
|
string directory = Path.GetDirectoryName(file);
|
|
|
|
|
|
string noExtension = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
|
string filenamePattern = Path.Combine(directory, noExtension);
|
|
|
|
|
|
filenamePattern = new Regex(@"\d+$").Replace(filenamePattern, string.Empty);
|
|
|
|
|
|
|
|
|
|
|
|
bool cabinetHeaderExists = File.Exists(Path.Combine(directory, filenamePattern + "1.hdr"));
|
|
|
|
|
|
bool shouldScanCabinet = cabinetHeaderExists
|
|
|
|
|
|
? file.Equals(Path.Combine(directory, filenamePattern + "1.hdr"), StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
: file.Equals(Path.Combine(directory, filenamePattern + "1.cab"), StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have anything but the first file
|
|
|
|
|
|
if (!shouldScanCabinet)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
|
try
|
2023-03-09 14:39:26 -05:00
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
|
// 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
|
|
|
|
InstallShieldCabinet cabfile = InstallShieldCabinet.Open(file);
|
|
|
|
|
|
for (int i = 0; i < cabfile.FileCount; i++)
|
2023-03-09 14:39:26 -05:00
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2023-03-09 14:39:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
|
return tempPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
return null;
|
2023-03-09 14:39:26 -05:00
|
|
|
|
}
|
2023-03-09 14:04:31 -05:00
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|