From 0ccae4e4b7a85e954ae5a71bea19471a5ca8e3bb Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 5 Nov 2024 00:25:22 -0500 Subject: [PATCH] Move static check collections to separate class --- BinaryObjectScanner/Data/StaticChecks.cs | 119 +++++++++++++++++++++ BinaryObjectScanner/FileType/Executable.cs | 100 ++--------------- BinaryObjectScanner/Handler.cs | 28 +---- BinaryObjectScanner/Scanner.cs | 9 +- 4 files changed, 132 insertions(+), 124 deletions(-) create mode 100644 BinaryObjectScanner/Data/StaticChecks.cs diff --git a/BinaryObjectScanner/Data/StaticChecks.cs b/BinaryObjectScanner/Data/StaticChecks.cs new file mode 100644 index 00000000..1b7f0af9 --- /dev/null +++ b/BinaryObjectScanner/Data/StaticChecks.cs @@ -0,0 +1,119 @@ +using System.Collections.Generic; +using BinaryObjectScanner.Interfaces; +using SabreTools.Serialization.Wrappers; + +namespace BinaryObjectScanner.Data +{ + internal static class StaticChecks + { + #region Public Collections + + /// + /// Cache for all IContentCheck types + /// + public static List ContentCheckClasses + { + get + { + contentCheckClasses ??= Factory.InitCheckClasses(); + return contentCheckClasses ?? []; + } + } + + /// + /// Cache for all IExecutableCheck types + /// + public static List> LinearExecutableCheckClasses + { + get + { + linearExecutableCheckClasses ??= Factory.InitCheckClasses>(); + return linearExecutableCheckClasses ?? []; + } + } + + /// + /// Cache for all IExecutableCheck types + /// + public static List> MSDOSExecutableCheckClasses + { + get + { + msdosExecutableCheckClasses ??= Factory.InitCheckClasses>(); + return msdosExecutableCheckClasses ?? []; + } + } + + /// + /// Cache for all IExecutableCheck types + /// + public static List> NewExecutableCheckClasses + { + get + { + newExecutableCheckClasses ??= Factory.InitCheckClasses>(); + return newExecutableCheckClasses ?? []; + } + } + + /// + /// Cache for all IPathCheck types + /// + public static List PathCheckClasses + { + get + { + pathCheckClasses ??= Factory.InitCheckClasses(); + return pathCheckClasses ?? []; + } + } + + /// + /// Cache for all IExecutableCheck types + /// + public static List> PortableExecutableCheckClasses + { + get + { + portableExecutableCheckClasses ??= Factory.InitCheckClasses>(); + return portableExecutableCheckClasses ?? []; + } + } + + #endregion + + #region Internal Instances + + /// + /// Cache for all IContentCheck types + /// + private static List? contentCheckClasses; + + /// + /// Cache for all IExecutableCheck types + /// + private static List>? linearExecutableCheckClasses; + + /// + /// Cache for all IExecutableCheck types + /// + private static List>? msdosExecutableCheckClasses; + + /// + /// Cache for all IExecutableCheck types + /// + private static List>? newExecutableCheckClasses; + + /// + /// Cache for all IPathCheck types + /// + private static List? pathCheckClasses; + + /// + /// Cache for all IExecutableCheck types + /// + private static List>? portableExecutableCheckClasses; + + #endregion + } +} \ No newline at end of file diff --git a/BinaryObjectScanner/FileType/Executable.cs b/BinaryObjectScanner/FileType/Executable.cs index b9ceba46..faca897c 100644 --- a/BinaryObjectScanner/FileType/Executable.cs +++ b/BinaryObjectScanner/FileType/Executable.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using BinaryObjectScanner.Data; using BinaryObjectScanner.Interfaces; using SabreTools.IO.Extensions; using SabreTools.Serialization.Wrappers; @@ -28,95 +29,6 @@ namespace BinaryObjectScanner.FileType /// public bool IncludePackers { get; set; } - /// - /// Cache for all IContentCheck types - /// - public static List ContentCheckClasses - { - get - { - contentCheckClasses ??= Factory.InitCheckClasses(); - return contentCheckClasses ?? []; - } - } - - /// - /// Cache for all IExecutableCheck types - /// - public static List> LinearExecutableCheckClasses - { - get - { - linearExecutableCheckClasses ??= Factory.InitCheckClasses>(); - return linearExecutableCheckClasses ?? []; - } - } - - /// - /// Cache for all IExecutableCheck types - /// - public static List> MSDOSExecutableCheckClasses - { - get - { - msdosExecutableCheckClasses ??= Factory.InitCheckClasses>(); - return msdosExecutableCheckClasses ?? []; - } - } - - /// - /// Cache for all IExecutableCheck types - /// - public static List> NewExecutableCheckClasses - { - get - { - newExecutableCheckClasses ??= Factory.InitCheckClasses>(); - return newExecutableCheckClasses ?? []; - } - } - - /// - /// Cache for all IExecutableCheck types - /// - public static List> PortableExecutableCheckClasses - { - get - { - portableExecutableCheckClasses ??= Factory.InitCheckClasses>(); - return portableExecutableCheckClasses ?? []; - } - } - - #endregion - - #region Internal Instances - - /// - /// Cache for all IContentCheck types - /// - private static List? contentCheckClasses; - - /// - /// Cache for all IExecutableCheck types - /// - private static List>? linearExecutableCheckClasses; - - /// - /// Cache for all IExecutableCheck types - /// - private static List>? msdosExecutableCheckClasses; - - /// - /// Cache for all IExecutableCheck types - /// - private static List>? newExecutableCheckClasses; - - /// - /// Cache for all IExecutableCheck types - /// - private static List>? portableExecutableCheckClasses; - #endregion /// @@ -149,22 +61,22 @@ namespace BinaryObjectScanner.FileType if (wrapper is MSDOS mz) { - var subProtections = RunExecutableChecks(file, mz, MSDOSExecutableCheckClasses, includeDebug); + var subProtections = RunExecutableChecks(file, mz, StaticChecks.MSDOSExecutableCheckClasses, includeDebug); protections.AddRange(subProtections.Values); } else if (wrapper is LinearExecutable lex) { - var subProtections = RunExecutableChecks(file, lex, LinearExecutableCheckClasses, includeDebug); + var subProtections = RunExecutableChecks(file, lex, StaticChecks.LinearExecutableCheckClasses, includeDebug); protections.AddRange(subProtections.Values); } else if (wrapper is NewExecutable nex) { - var subProtections = RunExecutableChecks(file, nex, NewExecutableCheckClasses, includeDebug); + var subProtections = RunExecutableChecks(file, nex, StaticChecks.NewExecutableCheckClasses, includeDebug); protections.AddRange(subProtections.Values); } else if (wrapper is PortableExecutable pex) { - var subProtections = RunExecutableChecks(file, pex, PortableExecutableCheckClasses, includeDebug); + var subProtections = RunExecutableChecks(file, pex, StaticChecks.PortableExecutableCheckClasses, includeDebug); protections.AddRange(subProtections.Values); } @@ -206,7 +118,7 @@ namespace BinaryObjectScanner.FileType } // Iterate through all checks - ContentCheckClasses.IterateWithAction(checkClass => + StaticChecks.ContentCheckClasses.IterateWithAction(checkClass => { // Get the protection for the class, if possible var protection = checkClass.CheckContents(file!, fileContent, includeDebug); diff --git a/BinaryObjectScanner/Handler.cs b/BinaryObjectScanner/Handler.cs index 6b81803a..c00549fb 100644 --- a/BinaryObjectScanner/Handler.cs +++ b/BinaryObjectScanner/Handler.cs @@ -1,37 +1,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using BinaryObjectScanner.Data; using BinaryObjectScanner.Interfaces; namespace BinaryObjectScanner { internal static class Handler { - #region Public Collections - - /// - /// Cache for all IPathCheck types - /// - public static List PathCheckClasses - { - get - { - pathCheckClasses ??= Factory.InitCheckClasses(); - return pathCheckClasses ?? []; - } - } - - #endregion - - #region Internal Instances - - /// - /// Cache for all IPathCheck types - /// - private static List? pathCheckClasses; - - #endregion - #region Multiple Implementation Wrappers /// @@ -49,7 +25,7 @@ namespace BinaryObjectScanner files = files?.Select(f => f.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar))?.ToList(); // Iterate through all checks - PathCheckClasses.IterateWithAction(checkClass => + StaticChecks.PathCheckClasses.IterateWithAction(checkClass => { var subProtections = checkClass?.PerformCheck(path, files); if (subProtections != null) diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index c9b537f9..c5288265 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using BinaryObjectScanner.Data; using BinaryObjectScanner.FileType; using BinaryObjectScanner.Interfaces; using SabreTools.IO.Extensions; @@ -403,7 +404,7 @@ namespace BinaryObjectScanner if (wrapper is MSDOS mz) { // Standard checks - var subProtections = executable.RunExecutableChecks(fileName, mz, Executable.MSDOSExecutableCheckClasses, _options.IncludeDebug); + var subProtections = executable.RunExecutableChecks(fileName, mz, StaticChecks.MSDOSExecutableCheckClasses, _options.IncludeDebug); protections.Append(fileName, subProtections.Values); // Extractable checks @@ -413,7 +414,7 @@ namespace BinaryObjectScanner else if (wrapper is LinearExecutable lex) { // Standard checks - var subProtections = executable.RunExecutableChecks(fileName, lex, Executable.LinearExecutableCheckClasses, _options.IncludeDebug); + var subProtections = executable.RunExecutableChecks(fileName, lex, StaticChecks.LinearExecutableCheckClasses, _options.IncludeDebug); protections.Append(fileName, subProtections.Values); // Extractable checks @@ -423,7 +424,7 @@ namespace BinaryObjectScanner else if (wrapper is NewExecutable nex) { // Standard checks - var subProtections = executable.RunExecutableChecks(fileName, nex, Executable.NewExecutableCheckClasses, _options.IncludeDebug); + var subProtections = executable.RunExecutableChecks(fileName, nex, StaticChecks.NewExecutableCheckClasses, _options.IncludeDebug); protections.Append(fileName, subProtections.Values); // Extractable checks @@ -433,7 +434,7 @@ namespace BinaryObjectScanner else if (wrapper is PortableExecutable pex) { // Standard checks - var subProtections = executable.RunExecutableChecks(fileName, pex, Executable.PortableExecutableCheckClasses, _options.IncludeDebug); + var subProtections = executable.RunExecutableChecks(fileName, pex, StaticChecks.PortableExecutableCheckClasses, _options.IncludeDebug); protections.Append(fileName, subProtections.Values); // Extractable checks