Skip to content

Commit

Permalink
Optimizations and fixes
Browse files Browse the repository at this point in the history
* HashStreamCore had issues on net46, backported new implementation instead
* Fixed some Span empty checks that had mistakenly been compares to null
* More use of new ArgumentNullException.ThrowIfNull etc, where possible
* Test cases modified to test with correct path separator for running platform
* Some test case helper methods moved to the common Helpers class
  • Loading branch information
LTRData committed May 24, 2024
1 parent 7213d90 commit 752815b
Show file tree
Hide file tree
Showing 31 changed files with 237 additions and 285 deletions.
25 changes: 10 additions & 15 deletions Library/DiscUtils.BootConfig/DiscUtilsRegistryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ namespace DiscUtils.BootConfig;

internal class DiscUtilsRegistryStorage : BaseStorage
{
private const string ElementsPathTemplate = @"Objects\{0}\Elements";
private const string ElementPathTemplate = @"Objects\{0}\Elements\{1:X8}";
private const string ObjectTypePathTemplate = @"Objects\{0}\Description";
//private const string ObjectsPath = @"Objects";

private readonly RegistryKey _rootKey;

public DiscUtilsRegistryStorage(RegistryKey key)
Expand Down Expand Up @@ -82,7 +77,7 @@ public override IEnumerable<Guid> EnumerateObjects()

public override IEnumerable<int> EnumerateElements(Guid obj)
{
var path = string.Format(CultureInfo.InvariantCulture, ElementsPathTemplate, obj.ToString("B"));
var path = $@"Objects\{obj:B}\Elements";
var parentKey = _rootKey.OpenSubKey(path);
foreach (var key in parentKey.GetSubKeyNames())
{
Expand All @@ -92,7 +87,7 @@ public override IEnumerable<int> EnumerateElements(Guid obj)

public override int GetObjectType(Guid obj)
{
var path = string.Format(CultureInfo.InvariantCulture, ObjectTypePathTemplate, obj.ToString("B"));
var path = $@"Objects\{obj:B}\Description";

var descKey = _rootKey.OpenSubKey(path);

Expand All @@ -102,21 +97,21 @@ public override int GetObjectType(Guid obj)

public override bool HasValue(Guid obj, int element)
{
var path = string.Format(CultureInfo.InvariantCulture, ElementPathTemplate, obj.ToString("B"), element);
var path = $@"Objects\{obj:B}\Elements\{element:X8}";
return _rootKey.OpenSubKey(path) != null;
}

public override bool ObjectExists(Guid obj)
{
var path = string.Format(CultureInfo.InvariantCulture, ObjectTypePathTemplate, obj.ToString("B"));
var path = $@"Objects\{obj:B}\Description";

return _rootKey.OpenSubKey(path) != null;
}

public override Guid CreateObject(Guid obj, int type)
{
var realObj = obj == Guid.Empty ? Guid.NewGuid() : obj;
var path = string.Format(CultureInfo.InvariantCulture, ObjectTypePathTemplate, realObj.ToString("B"));
var path = $@"Objects\{realObj:B}\Description";

var key = _rootKey.CreateSubKey(path);
key.SetValue("Type", type, RegistryValueType.Dword);
Expand All @@ -126,35 +121,35 @@ public override Guid CreateObject(Guid obj, int type)

public override void CreateElement(Guid obj, int element)
{
var path = string.Format(CultureInfo.InvariantCulture, ElementPathTemplate, obj.ToString("B"), element);
var path = $@"Objects\{obj:B}\Elements\{element:X8}";

_rootKey.CreateSubKey(path);
}

public override void DeleteObject(Guid obj)
{
var path = string.Format(CultureInfo.InvariantCulture, ObjectTypePathTemplate, obj.ToString("B"));
var path = $@"Objects\{obj:B}\Description";

_rootKey.DeleteSubKeyTree(path);
}

public override void DeleteElement(Guid obj, int element)
{
var path = string.Format(CultureInfo.InvariantCulture, ElementPathTemplate, obj.ToString("B"), element);
var path = $@"Objects\{obj:B}\Elements\{element:X8}";

_rootKey.DeleteSubKeyTree(path);
}

private object GetValue(Guid obj, int element)
{
var path = string.Format(CultureInfo.InvariantCulture, ElementPathTemplate, obj.ToString("B"), element);
var path = $@"Objects\{obj:B}\Elements\{element:X8}";
var key = _rootKey.OpenSubKey(path);
return key.GetValue("Element");
}

private void SetValue(Guid obj, int element, object value)
{
var path = string.Format(CultureInfo.InvariantCulture, ElementPathTemplate, obj.ToString("B"), element);
var path = $@"Objects\{obj:B}\Elements\{element:X8}";
var key = _rootKey.OpenSubKey(path);
key.SetValue("Element", value);
}
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.Core/Compression/BZip2DecoderStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,14 @@ public override long Position
/// <returns>The number of bytes read.</returns>
public override int Read(byte[] buffer, int offset, int count)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(buffer);
#else
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
#endif

return Read(buffer.AsSpan(offset, count));
}
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.Core/Compression/ZlibStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,14 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati

private static void CheckParams(byte[] buffer, int offset, int count)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(buffer);
#else
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
#endif

if (offset < 0 || offset > buffer.Length)
{
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.Core/DiscFileSystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public class DiscFileSystemInfo
{
internal DiscFileSystemInfo(DiscFileSystem fileSystem, string path)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(path);
#else
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
#endif

var wrongPathSep = System.IO.Path.DirectorySeparatorChar == '\\' ? '/' : '\\';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ public BiosPartitionedDiskBuilder(long capacity, Geometry biosGeometry)
)]
public BiosPartitionedDiskBuilder(long capacity, byte[] bootSectors, Geometry biosGeometry)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(bootSectors);
#else
if (bootSectors == null)
{
throw new ArgumentNullException(nameof(bootSectors));
}
#endif

_capacity = capacity;
_biosGeometry = biosGeometry;
Expand All @@ -94,10 +98,14 @@ public BiosPartitionedDiskBuilder(long capacity, byte[] bootSectors, Geometry bi
/// <param name="sourceDisk">The disk to clone.</param>
public BiosPartitionedDiskBuilder(VirtualDisk sourceDisk)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(sourceDisk);
#else
if (sourceDisk == null)
{
throw new ArgumentNullException(nameof(sourceDisk));
}
#endif

_capacity = sourceDisk.Capacity;
_biosGeometry = sourceDisk.BiosGeometry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LTRData.Extensions.Split;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -117,7 +118,7 @@ public static bool TryCreateFromBinaryForm(byte[] binaryForm, int offset, out Ge

public static GenericAce CreateFromBinaryForm(ReadOnlySpan<byte> binaryForm)
{
if (binaryForm == null)
if (binaryForm.IsEmpty)
{
throw new ArgumentNullException(nameof(binaryForm));
}
Expand All @@ -142,7 +143,7 @@ public static bool TryCreateFromBinaryForm(ReadOnlySpan<byte> binaryForm, out Ge
{
genericAce = null;

if (binaryForm == null || 1 > binaryForm.Length)
if (binaryForm.IsEmpty || 1 > binaryForm.Length)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ public int BinaryLength

public void GetBinaryForm(byte[] binaryForm, int offset)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(binaryForm);
#else
if (null == binaryForm)
{
throw new ArgumentNullException(nameof(binaryForm));
}
#endif

var binaryLength = BinaryLength;
if (offset < 0 || offset > binaryForm.Length - binaryLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,7 @@ internal override string GetSddlForm()
inhObjType = _inheritedObjectType.ToString("D");
}

return string.Format(CultureInfo.InvariantCulture,
"({0};{1};{2};{3};{4};{5})",
GetSddlAceType(AceType),
GetSddlAceFlags(AceFlags),
GetSddlAccessRights(AccessMask),
objType,
inhObjType,
SecurityIdentifier.GetSddlForm());
return $"({GetSddlAceType(AceType)};{GetSddlAceFlags(AceFlags)};{GetSddlAccessRights(AccessMask)};{objType};{inhObjType};{SecurityIdentifier.GetSddlForm()})";
}

private static AceType ConvertType(AceQualifier qualifier, bool isCallback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static bool TryParse(byte[] binaryForm, int offset, out RawAcl rawAcl) =>

public RawAcl(ReadOnlySpan<byte> binaryForm)
{
if (binaryForm == null)
if (binaryForm.IsEmpty)
{
throw new ArgumentNullException(nameof(binaryForm));
}
Expand Down Expand Up @@ -147,10 +147,14 @@ public override void GetBinaryForm(Span<byte> binaryForm)

public void InsertAce(int index, GenericAce ace)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(ace);
#else
if (ace == null)
{
throw new ArgumentNullException(nameof(ace));
}
#endif

_list.Insert(index, ace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ public class RawSecurityDescriptor : GenericSecurityDescriptor

public RawSecurityDescriptor(string sddlForm)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(sddlForm);
#else
if (sddlForm == null)
{
throw new ArgumentNullException(nameof(sddlForm));
}
#endif

SetSddlForm(sddlForm.Replace(" ", ""));

Expand Down
8 changes: 8 additions & 0 deletions Library/DiscUtils.Core/WindowsSecurity/NTAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public sealed class NTAccount : IdentityReference

public NTAccount(string name)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrWhiteSpace(name);
#else
if (name == null)
{
throw new ArgumentNullException(nameof(name));
Expand All @@ -17,12 +20,16 @@ public NTAccount(string name)
{
throw new ArgumentException("empty", nameof(name));
}
#endif

Value = name;
}

public NTAccount(string domainName, string accountName)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrWhiteSpace(accountName);
#else
if (accountName == null)
{
throw new ArgumentNullException(nameof(accountName));
Expand All @@ -32,6 +39,7 @@ public NTAccount(string domainName, string accountName)
{
throw new ArgumentException("empty", nameof(accountName));
}
#endif

if (domainName == null)
{
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.Fat/FatFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,9 @@ public override IEnumerable<string> GetFileSystemEntries(string path, string sea
/// <param name="destinationDirectoryName">The target directory name.</param>
public override void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrWhiteSpace(nameof(destinationDirectoryName));
#else
if (string.IsNullOrEmpty(destinationDirectoryName))
{
if (destinationDirectoryName == null)
Expand All @@ -1185,6 +1188,7 @@ public override void MoveDirectory(string sourceDirectoryName, string destinatio

throw new ArgumentException("Invalid destination name (empty string)");
}
#endif

var destId = GetDirectoryEntry(destinationDirectoryName, out var destParent);
if (destParent == null)
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.Fat/FileName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ public FileName(ReadOnlySpan<byte> data)

public FileName(string name, Encoding encoding)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(name);
#else
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
#endif

if (name.Length > 255)
{
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.HfsPlus/AttributeKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ public override int CompareTo(BTreeKey other)

public int CompareTo(AttributeKey other)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(other);
#else
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
#endif

if (FileId != other.FileId)
{
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.HfsPlus/CatalogKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ public CatalogKey(CatalogNodeId nodeId, string name)

public int CompareTo(CatalogKey other)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(other);
#else
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
#endif

if (NodeId != other.NodeId)
{
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.HfsPlus/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public DirEntry Self

public DirEntry GetEntryByName(string name)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrWhiteSpace(name);
#else
if (name == null)
{
throw new ArgumentNullException(nameof(name));
Expand All @@ -88,6 +91,7 @@ public DirEntry GetEntryByName(string name)
{
throw new ArgumentException("Attempt to lookup empty file name", nameof(name));
}
#endif

var dirEntryData = Context.Catalog.Find(new CatalogKey(NodeId, name));
if (dirEntryData == null)
Expand Down
4 changes: 4 additions & 0 deletions Library/DiscUtils.HfsPlus/ExtentKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public ExtentKey(CatalogNodeId cnid, uint startBlock, bool resource_fork = false

public int CompareTo(ExtentKey other)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(other);
#else
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
#endif

// Sort by file id, fork type, then starting block
if (NodeId != other.NodeId)
Expand Down
Loading

0 comments on commit 752815b

Please sign in to comment.