Don't expose ProtectionDictionary anymore

This commit is contained in:
Matt Nadareski
2025-09-06 12:55:09 -04:00
parent f5b1868eec
commit 785f5cfde1
3 changed files with 55 additions and 10 deletions

View File

@@ -11,11 +11,13 @@ namespace BinaryObjectScanner.Data
/// Represents a mapping from file to a set of protections
/// </summary>
#if NET20 || NET35
public class ProtectionDictionary : Dictionary<string, Queue<string>>
internal class ProtectionDictionary : Dictionary<string, Queue<string>>
#else
public class ProtectionDictionary : ConcurrentDictionary<string, ConcurrentQueue<string>>
internal class ProtectionDictionary : ConcurrentDictionary<string, ConcurrentQueue<string>>
#endif
{
#region Accessors
/// <summary>
/// Append one result to a results dictionary
/// </summary>
@@ -198,6 +200,32 @@ namespace BinaryObjectScanner.Data
}
}
#endregion
#region Conversion
/// <summary>
/// Reformat a protection dictionary for standard output
/// </summary>
/// <returns>Reformatted dictionary on success, empty on error</returns>
public Dictionary<string, List<string>> ToDictionary()
{
// Null or empty protections return empty
if (Count == 0)
return [];
// Reformat each set into a List
var newDict = new Dictionary<string, List<string>>();
foreach (string key in Keys)
{
newDict[key] = [.. this[key]];
}
return newDict;
}
#endregion
#region Helpers
/// <summary>

View File

@@ -65,14 +65,30 @@ namespace BinaryObjectScanner
/// </summary>
/// <param name="path">Path to scan</param>
/// <returns>Dictionary of list of strings representing the found protections</returns>
public ProtectionDictionary GetProtections(string path)
=> GetProtections([path]);
public Dictionary<string, List<string>> GetProtections(string path)
=> GetProtectionsImpl(path).ToDictionary();
/// <summary>
/// Scan the list of paths and get all found protections
/// </summary>
/// <returns>Dictionary of list of strings representing the found protections</returns>
public ProtectionDictionary GetProtections(List<string>? paths)
public Dictionary<string, List<string>> GetProtections(List<string>? paths)
=> GetProtectionsImpl(paths).ToDictionary();
/// <summary>
/// Scan a single path and get all found protections
/// </summary>
/// <param name="path">Path to scan</param>
/// <returns>Dictionary of list of strings representing the found protections</returns>
private ProtectionDictionary GetProtectionsImpl(string path)
=> GetProtectionsImpl([path]);
/// <summary>
/// Scan a single path and get all found protections
/// </summary>
/// <param name="path">Path to scan</param>
/// <returns>Dictionary of list of strings representing the found protections</returns>
private ProtectionDictionary GetProtectionsImpl(List<string>? 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

View File

@@ -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
/// </summary>
/// <param name="path">File or directory path</param>
/// <param name="protections">Dictionary of protections found, if any</param>
private static void WriteProtectionResultFile(string path, ProtectionDictionary? protections)
private static void WriteProtectionResultFile(string path, Dictionary<string, List<string>> 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