diff --git a/BinaryObjectScanner/FileType/Executable.cs b/BinaryObjectScanner/FileType/Executable.cs index 3e2ebb3e..84722b38 100644 --- a/BinaryObjectScanner/FileType/Executable.cs +++ b/BinaryObjectScanner/FileType/Executable.cs @@ -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(); -#else - var protections = new ConcurrentQueue(); -#endif + // Create the internal list + var protections = new List(); // 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 /// Stream to scan the contents of /// True to include debug data, false otherwise /// Set of protections in file, null on error -#if NET20 || NET35 - public Dictionary? RunContentChecks(string? file, Stream stream, bool includeDebug) -#else - public ConcurrentDictionary? RunContentChecks(string? file, Stream stream, bool includeDebug) -#endif + public IDictionary? 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 /// Executable to scan /// True to include debug data, false otherwise /// Set of protections in file, null on error -#if NET20 || NET35 - public Dictionary RunLinearExecutableChecks(string file, Stream stream, LinearExecutable lex, bool includeDebug) -#else - public ConcurrentDictionary RunLinearExecutableChecks(string file, Stream stream, LinearExecutable lex, bool includeDebug) -#endif + public IDictionary RunLinearExecutableChecks(string file, Stream stream, LinearExecutable lex, bool includeDebug) { // Create the output dictionary #if NET20 || NET35 @@ -349,11 +331,7 @@ namespace BinaryObjectScanner.FileType /// Executable to scan /// True to include debug data, false otherwise /// Set of protections in file, null on error -#if NET20 || NET35 - public Dictionary RunMSDOSExecutableChecks(string file, Stream stream, MSDOS mz, bool includeDebug) -#else - public ConcurrentDictionary RunMSDOSExecutableChecks(string file, Stream stream, MSDOS mz, bool includeDebug) -#endif + public IDictionary RunMSDOSExecutableChecks(string file, Stream stream, MSDOS mz, bool includeDebug) { // Create the output dictionary #if NET20 || NET35 @@ -412,11 +390,7 @@ namespace BinaryObjectScanner.FileType /// Executable to scan /// True to include debug data, false otherwise /// Set of protections in file, null on error -#if NET20 || NET35 - public Dictionary RunNewExecutableChecks(string file, Stream stream, NewExecutable nex, bool includeDebug) -#else - public ConcurrentDictionary RunNewExecutableChecks(string file, Stream stream, NewExecutable nex, bool includeDebug) -#endif + public IDictionary RunNewExecutableChecks(string file, Stream stream, NewExecutable nex, bool includeDebug) { // Create the output dictionary #if NET20 || NET35 @@ -475,11 +449,7 @@ namespace BinaryObjectScanner.FileType /// Executable to scan /// True to include debug data, false otherwise /// Set of protections in file, null on error -#if NET20 || NET35 - public Dictionary RunPortableExecutableChecks(string file, Stream stream, PortableExecutable pex, bool includeDebug) -#else - public ConcurrentDictionary RunPortableExecutableChecks(string file, Stream stream, PortableExecutable pex, bool includeDebug) -#endif + public IDictionary RunPortableExecutableChecks(string file, Stream stream, PortableExecutable pex, bool includeDebug) { // Create the output dictionary #if NET20 || NET35 diff --git a/BinaryObjectScanner/Handler.cs b/BinaryObjectScanner/Handler.cs index a4d8fc38..b0ccc104 100644 --- a/BinaryObjectScanner/Handler.cs +++ b/BinaryObjectScanner/Handler.cs @@ -87,11 +87,7 @@ namespace BinaryObjectScanner /// Stream to scan the contents of /// True to include debug data, false otherwise /// Set of protections in file, null on error -#if NET20 || NET35 - public static Queue? HandleDetectable(IDetectable impl, string fileName, Stream stream, bool includeDebug) -#else - public static ConcurrentQueue? HandleDetectable(IDetectable impl, string fileName, Stream stream, bool includeDebug) -#endif + public static List? 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 /// IPathCheck class representing the file type /// Path of the file or directory to check /// Set of protections in path, null on error -#if NET20 || NET35 - private static Queue? PerformCheck(this IPathCheck impl, string? path, IEnumerable? files) -#else - private static ConcurrentQueue? PerformCheck(this IPathCheck impl, string? path, IEnumerable? files) -#endif + private static List? PerformCheck(this IPathCheck impl, string? path, IEnumerable? files) { // If we have an invalid path if (string.IsNullOrEmpty(path)) return null; - // Setup the output dictionary -#if NET20 || NET35 - var protections = new Queue(); -#else - var protections = new ConcurrentQueue(); -#endif + // Setup the list + var protections = new List(); // If we have a file path if (File.Exists(path)) @@ -413,22 +401,14 @@ namespace BinaryObjectScanner /// /// Protection string to process /// Set of protections parsed, null on error -#if NET20 || NET35 - private static Queue? ProcessProtectionString(string? protection) -#else - private static ConcurrentQueue? ProcessProtectionString(string? protection) -#endif + private static List? 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(); -#else - var protections = new ConcurrentQueue(); -#endif + var protections = new List(); // 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; diff --git a/BinaryObjectScanner/ProtectionDictionary.cs b/BinaryObjectScanner/ProtectionDictionary.cs index 550a6f87..d0ec9c89 100644 --- a/BinaryObjectScanner/ProtectionDictionary.cs +++ b/BinaryObjectScanner/ProtectionDictionary.cs @@ -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(); -#else - var values = new ConcurrentQueue(); -#endif - values.Enqueue(value); - Append(key, values); + Append(key, [value]); } /// @@ -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(); -#else - TryAdd(key, new ConcurrentQueue()); -#endif + EnsureKey(key); this[key].AddRange(values); } @@ -61,22 +49,13 @@ namespace BinaryObjectScanner /// /// Key to add information to /// String value to add -#if NET20 || NET35 - public void Append(string key, Queue values) -#else - public void Append(string key, ConcurrentQueue values) -#endif + public void Append(string key, IEnumerable 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(); -#else - TryAdd(key, new ConcurrentQueue()); -#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(); -#else - TryAdd(key, new ConcurrentQueue()); -#endif + EnsureKey(key); this[key].AddRange(addition[key]); } } @@ -189,5 +163,18 @@ namespace BinaryObjectScanner #endif } } + + /// + /// Ensure the collection for the given key exists + /// + private void EnsureKey(string key) + { +#if NET20 || NET35 + if (!ContainsKey(key)) + this[key] = new Queue(); +#else + TryAdd(key, new ConcurrentQueue()); +#endif + } } } \ No newline at end of file diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index e35af71d..9cfcd6d1 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -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 /// Name of the source file of the stream, for tracking /// MSDOS to scan the contents of /// Set of protections found from extraction, null on error -#if NET20 || NET35 - private ProtectionDictionary? HandleExtractableProtections(Dictionary.KeyCollection? classes, string fileName, MSDOS mz) -#else - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, MSDOS mz) -#endif + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, MSDOS mz) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -509,11 +501,7 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// LinearExecutable to scan the contents of /// Set of protections found from extraction, null on error -#if NET20 || NET35 - private ProtectionDictionary? HandleExtractableProtections(Dictionary.KeyCollection? classes, string fileName, LinearExecutable lex) -#else - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, LinearExecutable lex) -#endif + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, LinearExecutable lex) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -558,11 +546,7 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// NewExecutable to scan the contents of /// Set of protections found from extraction, null on error -#if NET20 || NET35 - private ProtectionDictionary? HandleExtractableProtections(Dictionary.KeyCollection? classes, string fileName, NewExecutable nex) -#else - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, NewExecutable nex) -#endif + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, NewExecutable nex) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -607,11 +591,7 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// PortableExecutable to scan the contents of /// Set of protections found from extraction, null on error -#if NET20 || NET35 - private ProtectionDictionary? HandleExtractableProtections(Dictionary.KeyCollection? classes, string fileName, PortableExecutable pex) -#else - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, PortableExecutable pex) -#endif + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, PortableExecutable pex) { // If we have an invalid set of classes if (classes == null || !classes.Any()) diff --git a/BinaryObjectScanner/Utilities/Extensions.cs b/BinaryObjectScanner/Utilities/Extensions.cs index b89e9a1d..a40795f6 100644 --- a/BinaryObjectScanner/Utilities/Extensions.cs +++ b/BinaryObjectScanner/Utilities/Extensions.cs @@ -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 /// Queue to add data to /// Queue to get data from #if NET20 || NET35 - public static void AddRange(this Queue original, Queue values) + public static void AddRange(this Queue original, IEnumerable values) #else - public static void AddRange(this ConcurrentQueue original, ConcurrentQueue values) + public static void AddRange(this ConcurrentQueue original, IEnumerable 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 } } \ No newline at end of file diff --git a/Test/Protector.cs b/Test/Protector.cs index 448cca51..89f25096 100644 --- a/Test/Protector.cs +++ b/Test/Protector.cs @@ -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;