Skip to content

Commit

Permalink
Avoid exceptions on zero-length numeric values
Browse files Browse the repository at this point in the history
  • Loading branch information
LTRData committed Mar 22, 2024
1 parent 6eaa82a commit 6811a1f
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions Library/DiscUtils.Registry/RegistryValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ internal RegistryValue(RegistryHive hive, ValueCell cell)
/// <summary>
/// Gets the type of the value.
/// </summary>
public RegistryValueType DataType
{
get { return _cell.DataType; }
}
public RegistryValueType DataType => _cell.DataType;

/// <summary>
/// Gets the name of the value, or empty string if unnamed.
/// </summary>
public string Name
{
get { return _cell.Name ?? string.Empty; }
}
public string Name => _cell.Name ?? string.Empty;

/// <summary>
/// Gets the value data mapped to a .net object.
Expand Down Expand Up @@ -100,7 +94,7 @@ public object Value
{
get
{
var buffer = ArrayPool<byte>.Shared.Rent(_cell.DataLength & int.MaxValue);
var buffer = ArrayPool<byte>.Shared.Rent(RawLength);
try
{
return ConvertToObject(GetRawData(buffer), DataType);
Expand All @@ -112,14 +106,20 @@ public object Value
}
}

/// <summary>
/// Gets size in bytes of raw value data
/// </summary>
/// <returns>Size in bytes of raw value data</returns>
public int RawLength => _cell.DataLength & int.MaxValue;

/// <summary>
/// Gets raw binary value without interpretation as any particular data type.
/// </summary>
public byte[] RawValue
{
get
{
var buffer = ArrayPool<byte>.Shared.Rent(_cell.DataLength & int.MaxValue);
var buffer = ArrayPool<byte>.Shared.Rent(RawLength);
try
{
return GetRawData(buffer).ToArray();
Expand Down Expand Up @@ -219,20 +219,45 @@ private static object ConvertToObject(ReadOnlySpan<byte> data, RegistryValueType
case RegistryValueType.String:
case RegistryValueType.ExpandString:
case RegistryValueType.Link:
if (data.Length == 0)
{
return null;
}

return EndianUtilities.LittleEndianUnicodeBytesToString(data).Trim('\0');

case RegistryValueType.Dword:
if (data.Length == 0)
{
return null;
}

return EndianUtilities.ToInt32LittleEndian(data);

case RegistryValueType.DwordBigEndian:
if (data.Length == 0)
{
return null;
}

return EndianUtilities.ToInt32BigEndian(data);

case RegistryValueType.MultiString:
if (data.Length == 0)
{
return null;
}

var multiString = EndianUtilities.LittleEndianUnicodeBytesToString(data).TrimEnd('\0');
return multiString.Split('\0');

case RegistryValueType.Qword:
return string.Empty + EndianUtilities.ToUInt64LittleEndian(data);
if (data.Length == 0)
{
return null;
}

return EndianUtilities.ToUInt64LittleEndian(data);

default:
return data.ToArray();
Expand Down Expand Up @@ -297,7 +322,7 @@ private static ReadOnlyMemory<byte> ConvertToData(object value, RegistryValueTyp

private string DataAsString()
{
var buffer = ArrayPool<byte>.Shared.Rent(_cell.DataLength & int.MaxValue);
var buffer = ArrayPool<byte>.Shared.Rent(RawLength);
try
{
var data = GetRawData(buffer);
Expand Down

0 comments on commit 6811a1f

Please sign in to comment.