Skip to content

Commit

Permalink
Start using ProtectionDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Nov 1, 2024
1 parent 9a3fde0 commit 683cfb6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 426 deletions.
65 changes: 18 additions & 47 deletions BinaryObjectScanner/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using BinaryObjectScanner.Interfaces;
using BinaryObjectScanner.Utilities;
using SabreTools.Serialization.Wrappers;
using static BinaryObjectScanner.Utilities.Dictionary;

namespace BinaryObjectScanner
{
Expand Down Expand Up @@ -49,18 +48,10 @@ public static IEnumerable<IPathCheck?> PathCheckClasses
/// <param name="path">Path of the file or directory to check</param>
/// <param name="scanner">Scanner object to use for options and scanning</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Dictionary<string, Queue<string>> HandlePathChecks(string path, IEnumerable<string>? files)
#else
public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathChecks(string path, IEnumerable<string>? files)
#endif
public static ProtectionDictionary HandlePathChecks(string path, IEnumerable<string>? files)
{
// Create the output dictionary
#if NET20 || NET35
var protections = new Dictionary<string, Queue<string>>();
#else
var protections = new ConcurrentDictionary<string, ConcurrentQueue<string>>();
#endif
var protections = new ProtectionDictionary();

// Preprocess the list of files
files = files?.Select(f => f.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar))?.ToList();
Expand All @@ -74,7 +65,7 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
{
var subProtections = checkClass?.PerformCheck(path, files);
if (subProtections != null)
AppendToDictionary(protections, path, subProtections);
protections.Append(path, subProtections);
#if NET20 || NET35
}
#else
Expand Down Expand Up @@ -114,11 +105,7 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
/// <param name="stream">Stream to scan the contents of</param>
/// <param name="scanner">Scanner object to use on extractable contents</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Dictionary<string, Queue<string>>? HandleExtractable(IExtractable impl, string fileName, Stream? stream, Scanner scanner)
#else
public static ConcurrentDictionary<string, ConcurrentQueue<string>>? HandleExtractable(IExtractable impl, string fileName, Stream? stream, Scanner scanner)
#endif
public static ProtectionDictionary? HandleExtractable(IExtractable impl, string fileName, Stream? stream, Scanner scanner)
{
// If the extractable file itself fails
try
Expand All @@ -142,8 +129,8 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
}

// Prepare the returned protections
StripFromKeys(subProtections, tempPath);
PrependToKeys(subProtections, fileName);
subProtections.StripFromKeys(tempPath);

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 132 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
subProtections.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
Expand All @@ -162,11 +149,7 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
/// <param name="mz">MSDOS to scan the contents of</param>
/// <param name="scanner">Scanner object to use on extractable contents</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Dictionary<string, Queue<string>>? HandleExtractable(IExtractableMSDOSExecutable impl, string fileName, MSDOS mz, Scanner scanner)
#else
public static ConcurrentDictionary<string, ConcurrentQueue<string>>? HandleExtractable(IExtractableMSDOSExecutable impl, string fileName, MSDOS mz, Scanner scanner)
#endif
public static ProtectionDictionary? HandleExtractable(IExtractableMSDOSExecutable impl, string fileName, MSDOS mz, Scanner scanner)
{
// If the extractable file itself fails
try
Expand All @@ -190,8 +173,8 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
}

// Prepare the returned protections
StripFromKeys(subProtections, tempPath);
PrependToKeys(subProtections, fileName);
subProtections.StripFromKeys(tempPath);

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 176 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
subProtections.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
Expand All @@ -210,11 +193,7 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
/// <param name="lex">LinearExecutable to scan the contents of</param>
/// <param name="scanner">Scanner object to use on extractable contents</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Dictionary<string, Queue<string>>? HandleExtractable(IExtractableLinearExecutable impl, string fileName, LinearExecutable lex, Scanner scanner)
#else
public static ConcurrentDictionary<string, ConcurrentQueue<string>>? HandleExtractable(IExtractableLinearExecutable impl, string fileName, LinearExecutable lex, Scanner scanner)
#endif
public static ProtectionDictionary? HandleExtractable(IExtractableLinearExecutable impl, string fileName, LinearExecutable lex, Scanner scanner)
{
// If the extractable file itself fails
try
Expand All @@ -238,8 +217,8 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
}

// Prepare the returned protections
StripFromKeys(subProtections, tempPath);
PrependToKeys(subProtections, fileName);
subProtections.StripFromKeys(tempPath);

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 220 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
subProtections.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
Expand All @@ -258,11 +237,7 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
/// <param name="nex">NewExecutable to scan the contents of</param>
/// <param name="scanner">Scanner object to use on extractable contents</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Dictionary<string, Queue<string>>? HandleExtractable(IExtractableNewExecutable impl, string fileName, NewExecutable nex, Scanner scanner)
#else
public static ConcurrentDictionary<string, ConcurrentQueue<string>>? HandleExtractable(IExtractableNewExecutable impl, string fileName, NewExecutable nex, Scanner scanner)
#endif
public static ProtectionDictionary? HandleExtractable(IExtractableNewExecutable impl, string fileName, NewExecutable nex, Scanner scanner)
{
// If the extractable file itself fails
try
Expand All @@ -286,8 +261,8 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
}

// Prepare the returned protections
StripFromKeys(subProtections, tempPath);
PrependToKeys(subProtections, fileName);
subProtections.StripFromKeys(tempPath);

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 264 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
subProtections.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
Expand All @@ -306,11 +281,7 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
/// <param name="pex">PortableExecutable to scan the contents of</param>
/// <param name="scanner">Scanner object to use on extractable contents</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET20 || NET35
public static Dictionary<string, Queue<string>>? HandleExtractable(IExtractablePortableExecutable impl, string fileName, PortableExecutable pex, Scanner scanner)
#else
public static ConcurrentDictionary<string, ConcurrentQueue<string>>? HandleExtractable(IExtractablePortableExecutable impl, string fileName, PortableExecutable pex, Scanner scanner)
#endif
public static ProtectionDictionary? HandleExtractable(IExtractablePortableExecutable impl, string fileName, PortableExecutable pex, Scanner scanner)
{
// If the extractable file itself fails
try
Expand All @@ -334,8 +305,8 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
}

// Prepare the returned protections
StripFromKeys(subProtections, tempPath);
PrependToKeys(subProtections, fileName);
subProtections.StripFromKeys(tempPath);

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-x64, net8.0, Release)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, win-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, linux-arm64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build (Test, osx-x64, net8.0, Debug)

Dereference of a possibly null reference.

Check warning on line 308 in BinaryObjectScanner/Handler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
subProtections.PrependToKeys(fileName);
return subProtections;
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion BinaryObjectScanner/Interfaces/IPathCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IPathCheck
/// <param name="files">Enumerable of strings representing files in a directory</param>
/// <remarks>This can do some limited content checking as well, but it's suggested to use a content check instead, if possible</remarks>
#if NET20 || NET35
Queue<string> CheckDirectoryPath(string path, IEnumerable<string>? files);
List<string> CheckDirectoryPath(string path, IEnumerable<string>? files);
#else
ConcurrentQueue<string> CheckDirectoryPath(string path, IEnumerable<string>? files);
#endif
Expand Down
12 changes: 6 additions & 6 deletions BinaryObjectScanner/ProtectionDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void Append(string key, string[] values)

// Add the key if needed and then append the lists
#if NET20 || NET35
if (!original.ContainsKey(key))
original[key] = new Queue<string>();
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
Expand All @@ -72,8 +72,8 @@ public void Append(string key, ConcurrentQueue<string> values)

// Add the key if needed and then append the lists
#if NET20 || NET35
if (!original.ContainsKey(key))
original[key] = new Queue<string>();
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
Expand All @@ -94,8 +94,8 @@ public void Append(ProtectionDictionary? addition)
foreach (string key in addition.Keys)
{
#if NET20 || NET35
if (!original.ContainsKey(key))
original[key] = new Queue<string>();
if (!ContainsKey(key))
this[key] = new Queue<string>();
#else
TryAdd(key, new ConcurrentQueue<string>());
#endif
Expand Down
Loading

0 comments on commit 683cfb6

Please sign in to comment.