2020-09-10 21:10:32 -07:00
|
|
|
|
using System;
|
2021-07-18 09:44:23 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using System.IO;
|
2022-05-01 17:41:50 -07:00
|
|
|
|
using BurnOutSharp.Interfaces;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using SharpCompress.Compressors;
|
|
|
|
|
|
using SharpCompress.Compressors.BZip2;
|
2022-12-15 00:13:24 -08:00
|
|
|
|
using static BurnOutSharp.Utilities.Dictionary;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
|
|
|
|
|
namespace BurnOutSharp.FileType
|
|
|
|
|
|
{
|
2022-12-08 21:32:52 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// bzip2 archive
|
|
|
|
|
|
/// </summary>
|
2021-09-10 16:10:15 -07:00
|
|
|
|
public class BZip2 : IScannable
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2021-02-26 09:26:23 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2021-02-26 09:26:23 -08:00
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2022-12-22 22:03:32 -08:00
|
|
|
|
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
|
2021-02-26 09:26:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
return Scan(scanner, fs, file);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
|
2021-02-26 09:26:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
// If the BZip2 file itself fails
|
2020-09-10 21:10:32 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
|
|
|
|
Directory.CreateDirectory(tempPath);
|
|
|
|
|
|
|
|
|
|
|
|
using (BZip2Stream bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true))
|
|
|
|
|
|
{
|
|
|
|
|
|
// If an individual entry fails
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-10-28 12:05:48 -07:00
|
|
|
|
string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
|
|
|
|
|
|
using (FileStream fs = File.OpenWrite(tempFile))
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
bz2File.CopyTo(fs);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-10-31 13:20:54 -07:00
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2020-10-31 13:20:54 -07:00
|
|
|
|
// Collect and format all found protections
|
2020-10-31 14:00:31 -07:00
|
|
|
|
var protections = scanner.GetProtections(tempPath);
|
2020-10-31 13:20:54 -07:00
|
|
|
|
|
|
|
|
|
|
// If temp directory cleanup fails
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(tempPath, true);
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-10-31 13:20:54 -07:00
|
|
|
|
|
2020-10-31 14:14:35 -07:00
|
|
|
|
// Remove temporary path references
|
2022-12-15 00:13:24 -08:00
|
|
|
|
StripFromKeys(protections, tempPath);
|
2020-10-31 14:14:35 -07:00
|
|
|
|
|
2020-10-31 13:20:54 -07:00
|
|
|
|
return protections;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2020-10-31 13:20:54 -07:00
|
|
|
|
return null;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|