From 785f5cfde11a80975f444eaece0deb2d96ad038e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 6 Sep 2025 12:55:09 -0400 Subject: [PATCH] Don't expose ProtectionDictionary anymore --- .../Data/ProtectionDictionary.cs | 32 +++++++++++++++++-- BinaryObjectScanner/Scanner.cs | 24 +++++++++++--- ProtectionScan/Program.cs | 9 +++--- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/BinaryObjectScanner/Data/ProtectionDictionary.cs b/BinaryObjectScanner/Data/ProtectionDictionary.cs index 153272c1..34b93388 100644 --- a/BinaryObjectScanner/Data/ProtectionDictionary.cs +++ b/BinaryObjectScanner/Data/ProtectionDictionary.cs @@ -11,11 +11,13 @@ namespace BinaryObjectScanner.Data /// Represents a mapping from file to a set of protections /// #if NET20 || NET35 - public class ProtectionDictionary : Dictionary> + internal class ProtectionDictionary : Dictionary> #else - public class ProtectionDictionary : ConcurrentDictionary> + internal class ProtectionDictionary : ConcurrentDictionary> #endif { + #region Accessors + /// /// Append one result to a results dictionary /// @@ -198,6 +200,32 @@ namespace BinaryObjectScanner.Data } } + #endregion + + #region Conversion + + /// + /// Reformat a protection dictionary for standard output + /// + /// Reformatted dictionary on success, empty on error + public Dictionary> ToDictionary() + { + // Null or empty protections return empty + if (Count == 0) + return []; + + // Reformat each set into a List + var newDict = new Dictionary>(); + foreach (string key in Keys) + { + newDict[key] = [.. this[key]]; + } + + return newDict; + } + + #endregion + #region Helpers /// diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index 21fc3b21..34838770 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -65,14 +65,30 @@ namespace BinaryObjectScanner /// /// Path to scan /// Dictionary of list of strings representing the found protections - public ProtectionDictionary GetProtections(string path) - => GetProtections([path]); + public Dictionary> GetProtections(string path) + => GetProtectionsImpl(path).ToDictionary(); /// /// Scan the list of paths and get all found protections /// /// Dictionary of list of strings representing the found protections - public ProtectionDictionary GetProtections(List? paths) + public Dictionary> GetProtections(List? paths) + => GetProtectionsImpl(paths).ToDictionary(); + + /// + /// Scan a single path and get all found protections + /// + /// Path to scan + /// Dictionary of list of strings representing the found protections + private ProtectionDictionary GetProtectionsImpl(string path) + => GetProtectionsImpl([path]); + + /// + /// Scan a single path and get all found protections + /// + /// Path to scan + /// Dictionary of list of strings representing the found protections + private ProtectionDictionary GetProtectionsImpl(List? paths) { // If we have no paths, we can't scan if (paths == null || paths.Count == 0) @@ -301,7 +317,7 @@ namespace BinaryObjectScanner // Collect and format all found protections ProtectionDictionary? subProtections = null; if (extracted) - subProtections = GetProtections(tempPath); + subProtections = GetProtectionsImpl(tempPath); // If temp directory cleanup fails try diff --git a/ProtectionScan/Program.cs b/ProtectionScan/Program.cs index 6baa6302..93150c08 100644 --- a/ProtectionScan/Program.cs +++ b/ProtectionScan/Program.cs @@ -1,7 +1,7 @@ using System; +using System.Collections.Generic; using System.IO; using BinaryObjectScanner; -using BinaryObjectScanner.Data; namespace ProtectionScan { @@ -86,7 +86,7 @@ namespace ProtectionScan /// /// File or directory path /// Dictionary of protections found, if any - private static void WriteProtectionResultFile(string path, ProtectionDictionary? protections) + private static void WriteProtectionResultFile(string path, Dictionary> protections) { if (protections == null) { @@ -113,11 +113,12 @@ namespace ProtectionScan foreach (string key in keys) { // Skip over files with no protection - if (protections[key] == null || protections[key].Count == 0) + var value = protections[key]; + if (value.Count == 0) continue; // Sort the detected protections for consistent output - string[] fileProtections = [.. protections[key]]; + string[] fileProtections = [.. value]; Array.Sort(fileProtections); // Format and output the line