Reduce per-framework complexity

This commit is contained in:
Matt Nadareski
2024-10-31 21:46:40 -04:00
parent 5eab12946f
commit 99a64942ea
6 changed files with 68 additions and 163 deletions

View File

@@ -6,12 +6,11 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
#if NET40_OR_GREATER || NETCOREAPP
using System.Threading.Tasks;
#endif
using BinaryObjectScanner.Interfaces;
using BinaryObjectScanner.Utilities;
using SabreTools.IO.Extensions;
using SabreTools.Serialization.Wrappers;
namespace BinaryObjectScanner.FileType
@@ -146,44 +145,40 @@ namespace BinaryObjectScanner.FileType
if (wrapper == null)
return null;
// Create the internal queue
#if NET20 || NET35
var protections = new Queue<string>();
#else
var protections = new ConcurrentQueue<string>();
#endif
// Create the internal list
var protections = new List<string>();
// Only use generic content checks if we're in debug mode
if (includeDebug)
{
var subProtections = RunContentChecks(file, stream, includeDebug);
if (subProtections != null)
protections.AddRange(subProtections.Values.ToArray());
protections.AddRange(subProtections.Values);
}
if (wrapper is MSDOS mz)
{
var subProtections = RunMSDOSExecutableChecks(file, stream, mz, includeDebug);
if (subProtections != null)
protections.AddRange(subProtections.Values.ToArray());
protections.AddRange(subProtections.Values);
}
else if (wrapper is LinearExecutable lex)
{
var subProtections = RunLinearExecutableChecks(file, stream, lex, includeDebug);
if (subProtections != null)
protections.AddRange(subProtections.Values.ToArray());
protections.AddRange(subProtections.Values);
}
else if (wrapper is NewExecutable nex)
{
var subProtections = RunNewExecutableChecks(file, stream, nex, includeDebug);
if (subProtections != null)
protections.AddRange(subProtections.Values.ToArray());
protections.AddRange(subProtections.Values);
}
else if (wrapper is PortableExecutable pex)
{
var subProtections = RunPortableExecutableChecks(file, stream, pex, includeDebug);
if (subProtections != null)
protections.AddRange(subProtections.Values.ToArray());
protections.AddRange(subProtections.Values);
}
return string.Join(";", [.. protections]);
@@ -198,11 +193,7 @@ namespace BinaryObjectScanner.FileType
/// <param name="stream">Stream to scan the contents of</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public Dictionary<IContentCheck, string>? RunContentChecks(string? file, Stream stream, bool includeDebug)
#else
public ConcurrentDictionary<IContentCheck, string>? RunContentChecks(string? file, Stream stream, bool includeDebug)
#endif
public IDictionary<IContentCheck, string>? RunContentChecks(string? file, Stream stream, bool includeDebug)
{
// If we have an invalid file
if (string.IsNullOrEmpty(file))
@@ -214,12 +205,7 @@ namespace BinaryObjectScanner.FileType
byte[] fileContent = [];
try
{
#if NET20 || NET35 || NET40
using var br = new BinaryReader(stream, Encoding.Default);
#else
using var br = new BinaryReader(stream, Encoding.Default, true);
#endif
fileContent = br.ReadBytes((int)stream.Length);
fileContent = stream.ReadBytes((int)stream.Length);
if (fileContent == null)
return null;
}
@@ -286,11 +272,7 @@ namespace BinaryObjectScanner.FileType
/// <param name="lex">Executable to scan</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public Dictionary<ILinearExecutableCheck, string> RunLinearExecutableChecks(string file, Stream stream, LinearExecutable lex, bool includeDebug)
#else
public ConcurrentDictionary<ILinearExecutableCheck, string> RunLinearExecutableChecks(string file, Stream stream, LinearExecutable lex, bool includeDebug)
#endif
public IDictionary<ILinearExecutableCheck, string> RunLinearExecutableChecks(string file, Stream stream, LinearExecutable lex, bool includeDebug)
{
// Create the output dictionary
#if NET20 || NET35
@@ -349,11 +331,7 @@ namespace BinaryObjectScanner.FileType
/// <param name="mz">Executable to scan</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public Dictionary<IMSDOSExecutableCheck, string> RunMSDOSExecutableChecks(string file, Stream stream, MSDOS mz, bool includeDebug)
#else
public ConcurrentDictionary<IMSDOSExecutableCheck, string> RunMSDOSExecutableChecks(string file, Stream stream, MSDOS mz, bool includeDebug)
#endif
public IDictionary<IMSDOSExecutableCheck, string> RunMSDOSExecutableChecks(string file, Stream stream, MSDOS mz, bool includeDebug)
{
// Create the output dictionary
#if NET20 || NET35
@@ -412,11 +390,7 @@ namespace BinaryObjectScanner.FileType
/// <param name="nex">Executable to scan</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public Dictionary<INewExecutableCheck, string> RunNewExecutableChecks(string file, Stream stream, NewExecutable nex, bool includeDebug)
#else
public ConcurrentDictionary<INewExecutableCheck, string> RunNewExecutableChecks(string file, Stream stream, NewExecutable nex, bool includeDebug)
#endif
public IDictionary<INewExecutableCheck, string> RunNewExecutableChecks(string file, Stream stream, NewExecutable nex, bool includeDebug)
{
// Create the output dictionary
#if NET20 || NET35
@@ -475,11 +449,7 @@ namespace BinaryObjectScanner.FileType
/// <param name="pex">Executable to scan</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public Dictionary<IPortableExecutableCheck, string> RunPortableExecutableChecks(string file, Stream stream, PortableExecutable pex, bool includeDebug)
#else
public ConcurrentDictionary<IPortableExecutableCheck, string> RunPortableExecutableChecks(string file, Stream stream, PortableExecutable pex, bool includeDebug)
#endif
public IDictionary<IPortableExecutableCheck, string> RunPortableExecutableChecks(string file, Stream stream, PortableExecutable pex, bool includeDebug)
{
// Create the output dictionary
#if NET20 || NET35

View File

@@ -87,11 +87,7 @@ namespace BinaryObjectScanner
/// <param name="stream">Stream to scan the contents of</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Queue<string>? HandleDetectable(IDetectable impl, string fileName, Stream stream, bool includeDebug)
#else
public static ConcurrentQueue<string>? HandleDetectable(IDetectable impl, string fileName, Stream stream, bool includeDebug)
#endif
public static List<string>? HandleDetectable(IDetectable impl, string fileName, Stream stream, bool includeDebug)
{
var protection = impl.Detect(stream, fileName, includeDebug);
return ProcessProtectionString(protection);
@@ -129,8 +125,8 @@ namespace BinaryObjectScanner
}
// Prepare the returned protections
subProtections.StripFromKeys(tempPath);
subProtections.PrependToKeys(fileName);
subProtections?.StripFromKeys(tempPath);
subProtections?.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
@@ -173,8 +169,8 @@ namespace BinaryObjectScanner
}
// Prepare the returned protections
subProtections.StripFromKeys(tempPath);
subProtections.PrependToKeys(fileName);
subProtections?.StripFromKeys(tempPath);
subProtections?.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
@@ -217,8 +213,8 @@ namespace BinaryObjectScanner
}
// Prepare the returned protections
subProtections.StripFromKeys(tempPath);
subProtections.PrependToKeys(fileName);
subProtections?.StripFromKeys(tempPath);
subProtections?.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
@@ -261,8 +257,8 @@ namespace BinaryObjectScanner
}
// Prepare the returned protections
subProtections.StripFromKeys(tempPath);
subProtections.PrependToKeys(fileName);
subProtections?.StripFromKeys(tempPath);
subProtections?.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
@@ -305,8 +301,8 @@ namespace BinaryObjectScanner
}
// Prepare the returned protections
subProtections.StripFromKeys(tempPath);
subProtections.PrependToKeys(fileName);
subProtections?.StripFromKeys(tempPath);
subProtections?.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
@@ -323,22 +319,14 @@ namespace BinaryObjectScanner
/// <param name="impl">IPathCheck class representing the file type</param>
/// <param name="path">Path of the file or directory to check</param>
/// <returns>Set of protections in path, null on error</returns>
#if NET20 || NET35
private static Queue<string>? PerformCheck(this IPathCheck impl, string? path, IEnumerable<string>? files)
#else
private static ConcurrentQueue<string>? PerformCheck(this IPathCheck impl, string? path, IEnumerable<string>? files)
#endif
private static List<string>? PerformCheck(this IPathCheck impl, string? path, IEnumerable<string>? files)
{
// If we have an invalid path
if (string.IsNullOrEmpty(path))
return null;
// Setup the output dictionary
#if NET20 || NET35
var protections = new Queue<string>();
#else
var protections = new ConcurrentQueue<string>();
#endif
// Setup the list
var protections = new List<string>();
// If we have a file path
if (File.Exists(path))
@@ -413,22 +401,14 @@ namespace BinaryObjectScanner
/// </summary>
/// <param name="protection">Protection string to process</param>
/// <returns>Set of protections parsed, null on error</returns>
#if NET20 || NET35
private static Queue<string>? ProcessProtectionString(string? protection)
#else
private static ConcurrentQueue<string>? ProcessProtectionString(string? protection)
#endif
private static List<string>? ProcessProtectionString(string? protection)
{
// If we have an invalid protection string
if (string.IsNullOrEmpty(protection))
return null;
// Setup the output queue
#if NET20 || NET35
var protections = new Queue<string>();
#else
var protections = new ConcurrentQueue<string>();
#endif
var protections = new List<string>();
// If we have an indicator of multiple protections
if (protection!.Contains(";"))
@@ -438,7 +418,7 @@ namespace BinaryObjectScanner
}
else
{
protections.Enqueue(protection);
protections.Add(protection);
}
return protections;

View File

@@ -1,9 +1,8 @@
using System;
#if NET20 || NET35
using System.Collections.Generic;
#else
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BinaryObjectScanner.Utilities;
@@ -27,13 +26,7 @@ namespace BinaryObjectScanner
if (string.IsNullOrEmpty(value))
return;
#if NET20 || NET35
var values = new Queue<string>();
#else
var values = new ConcurrentQueue<string>();
#endif
values.Enqueue(value);
Append(key, values);
Append(key, [value]);
}
/// <summary>
@@ -47,12 +40,7 @@ namespace BinaryObjectScanner
key ??= "NO FILENAME";
// Add the key if needed and then append the lists
#if NET20 || NET35
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
EnsureKey(key);
this[key].AddRange(values);
}
@@ -61,22 +49,13 @@ namespace BinaryObjectScanner
/// </summary>
/// <param name="key">Key to add information to</param>
/// <param name="value">String value to add</param>
#if NET20 || NET35
public void Append(string key, Queue<string> values)
#else
public void Append(string key, ConcurrentQueue<string> values)
#endif
public void Append(string key, IEnumerable<string> values)
{
// Use a placeholder value if the key is null
key ??= "NO FILENAME";
// Add the key if needed and then append the lists
#if NET20 || NET35
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
EnsureKey(key);
this[key].AddRange(values);
}
@@ -93,12 +72,7 @@ namespace BinaryObjectScanner
// Loop through each of the addition keys and add accordingly
foreach (string key in addition.Keys)
{
#if NET20 || NET35
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
EnsureKey(key);
this[key].AddRange(addition[key]);
}
}
@@ -189,5 +163,18 @@ namespace BinaryObjectScanner
#endif
}
}
/// <summary>
/// Ensure the collection for the given key exists
/// </summary>
private void EnsureKey(string key)
{
#if NET20 || NET35
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
}
}
}

View File

@@ -1,7 +1,4 @@
using System;
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -13,7 +10,6 @@ using System.Threading.Tasks;
#endif
using BinaryObjectScanner.FileType;
using BinaryObjectScanner.Interfaces;
using BinaryObjectScanner.Utilities;
using SabreTools.IO.Extensions;
using SabreTools.Serialization.Interfaces;
using SabreTools.Serialization.Wrappers;
@@ -390,7 +386,7 @@ namespace BinaryObjectScanner
{
var subProtections = executable.RunContentChecks(fileName, stream, IncludeDebug);
if (subProtections != null)
protections.Append(fileName, subProtections.Values.ToArray());
protections.Append(fileName, subProtections.Values);
}
if (wrapper is MSDOS mz)
@@ -400,7 +396,7 @@ namespace BinaryObjectScanner
return protections;
// Append the returned values
protections.Append(fileName, subProtections.Values.ToArray());
protections.Append(fileName, subProtections.Values);
// If we have any extractable packers
var extractedProtections = HandleExtractableProtections(subProtections.Keys, fileName, mz);
@@ -414,7 +410,7 @@ namespace BinaryObjectScanner
return protections;
// Append the returned values
protections.Append(fileName, subProtections.Values.ToArray());
protections.Append(fileName, subProtections.Values);
// If we have any extractable packers
var extractedProtections = HandleExtractableProtections(subProtections.Keys, fileName, lex);
@@ -428,7 +424,7 @@ namespace BinaryObjectScanner
return protections;
// Append the returned values
protections.Append(fileName, subProtections.Values.ToArray());
protections.Append(fileName, subProtections.Values);
// If we have any extractable packers
var extractedProtections = HandleExtractableProtections(subProtections.Keys, fileName, nex);
@@ -442,7 +438,7 @@ namespace BinaryObjectScanner
return protections;
// Append the returned values
protections.Append(fileName, subProtections.Values.ToArray());
protections.Append(fileName, subProtections.Values);
// If we have any extractable packers
var extractedProtections = HandleExtractableProtections(subProtections.Keys, fileName, pex);
@@ -460,11 +456,7 @@ namespace BinaryObjectScanner
/// <param name="fileName">Name of the source file of the stream, for tracking</param>
/// <param name="mz">MSDOS to scan the contents of</param>
/// <returns>Set of protections found from extraction, null on error</returns>
#if NET20 || NET35
private ProtectionDictionary? HandleExtractableProtections<T>(Dictionary<T, string>.KeyCollection? classes, string fileName, MSDOS mz)
#else
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<object>? classes, string fileName, MSDOS mz)
#endif
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<IMSDOSExecutableCheck>? classes, string fileName, MSDOS mz)
{
// If we have an invalid set of classes
if (classes == null || !classes.Any())
@@ -509,11 +501,7 @@ namespace BinaryObjectScanner
/// <param name="fileName">Name of the source file of the stream, for tracking</param>
/// <param name="lex">LinearExecutable to scan the contents of</param>
/// <returns>Set of protections found from extraction, null on error</returns>
#if NET20 || NET35
private ProtectionDictionary? HandleExtractableProtections<T>(Dictionary<T, string>.KeyCollection? classes, string fileName, LinearExecutable lex)
#else
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<object>? classes, string fileName, LinearExecutable lex)
#endif
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<ILinearExecutableCheck>? classes, string fileName, LinearExecutable lex)
{
// If we have an invalid set of classes
if (classes == null || !classes.Any())
@@ -558,11 +546,7 @@ namespace BinaryObjectScanner
/// <param name="fileName">Name of the source file of the stream, for tracking</param>
/// <param name="nex">NewExecutable to scan the contents of</param>
/// <returns>Set of protections found from extraction, null on error</returns>
#if NET20 || NET35
private ProtectionDictionary? HandleExtractableProtections<T>(Dictionary<T, string>.KeyCollection? classes, string fileName, NewExecutable nex)
#else
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<object>? classes, string fileName, NewExecutable nex)
#endif
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<INewExecutableCheck>? classes, string fileName, NewExecutable nex)
{
// If we have an invalid set of classes
if (classes == null || !classes.Any())
@@ -607,11 +591,7 @@ namespace BinaryObjectScanner
/// <param name="fileName">Name of the source file of the stream, for tracking</param>
/// <param name="pex">PortableExecutable to scan the contents of</param>
/// <returns>Set of protections found from extraction, null on error</returns>
#if NET20 || NET35
private ProtectionDictionary? HandleExtractableProtections<T>(Dictionary<T, string>.KeyCollection? classes, string fileName, PortableExecutable pex)
#else
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<object>? classes, string fileName, PortableExecutable pex)
#endif
private ProtectionDictionary? HandleExtractableProtections(IEnumerable<IPortableExecutableCheck>? classes, string fileName, PortableExecutable pex)
{
// If we have an invalid set of classes
if (classes == null || !classes.Any())

View File

@@ -1,8 +1,8 @@
#if NET20 || NET35
using System.Collections.Generic;
#else
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
using System.Linq;
namespace BinaryObjectScanner.Utilities
{
@@ -36,27 +36,20 @@ namespace BinaryObjectScanner.Utilities
/// <param name="original">Queue to add data to</param>
/// <param name="values">Queue to get data from</param>
#if NET20 || NET35
public static void AddRange(this Queue<string> original, Queue<string> values)
public static void AddRange(this Queue<string> original, IEnumerable<string> values)
#else
public static void AddRange(this ConcurrentQueue<string> original, ConcurrentQueue<string> values)
public static void AddRange(this ConcurrentQueue<string> original, IEnumerable<string> values)
#endif
{
if (values == null || values.Count == 0)
if (values == null || !values.Any())
return;
while (values.Count > 0)
foreach (string value in values)
{
#if NET20 || NET35
original.Enqueue(values.Dequeue());
#else
if (!values.TryDequeue(out var value))
return;
original.Enqueue(value);
#endif
}
}
#endregion
#endregion
}
}

View File

@@ -1,9 +1,4 @@
using System;
#if NET20 || NET35
using System.Collections.Generic;
#else
using System.Collections.Concurrent;
#endif
using System.IO;
using System.Linq;
using BinaryObjectScanner;