Skip to content

Commit

Permalink
Rename BlobReadContext -> BlobReaderContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Nov 27, 2022
1 parent c0283c7 commit 983873b
Show file tree
Hide file tree
Showing 32 changed files with 72 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet.Dynamic/DynamicCilOperandResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public DynamicCilOperandResolver(SerializedModuleDefinition contextModule, CilMe

case TableIndex.StandAloneSig:
var reader = new BinaryStreamReader((byte[])_tokens[(int)token.Rid]!);
return CallingConventionSignature.FromReader(new BlobReadContext(_readerContext), ref reader);
var blobReadContext = new BlobReaderContext(_readerContext);
return CallingConventionSignature.FromReader(ref blobReadContext, ref reader);
}

return token;
Expand Down
4 changes: 2 additions & 2 deletions src/AsmResolver.DotNet.Dynamic/DynamicMethodHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static void ReadLocalVariables(CilMethodBody methodBody, MethodDefinition
throw new ArgumentException("Method body should reference a serialized module.");

var reader = new BinaryStreamReader(localSig);
var context = new BlobReadContext(module.ReaderContext, DynamicTypeSignatureResolver.Instance);
if (CallingConventionSignature.FromReader(context, ref reader)
var context = new BlobReaderContext(module.ReaderContext, DynamicTypeSignatureResolver.Instance);
if (CallingConventionSignature.FromReader(ref context, ref reader)
is not LocalVariablesSignature localsSignature)
{
throw new ArgumentException("Invalid local variables signature.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static DynamicTypeSignatureResolver()
public static bool IsSupported => GetTypeFromHandleUnsafeMethod is not null;

/// <inheritdoc />
public override TypeSignature ResolveRuntimeType(ref BlobReadContext context, nint address)
public override TypeSignature ResolveRuntimeType(ref BlobReaderContext context, nint address)
{
if (!IsSupported)
throw new PlatformNotSupportedException("The current platform does not support the translation of raw type handles to System.Type instances.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public SerializedCustomAttribute(ModuleReaderContext context, MetadataToken toke
$"Invalid signature blob index in custom attribute {MetadataToken}.");
}

return CustomAttributeSignature.FromReader(new BlobReadContext(_context), Constructor, reader);
return CustomAttributeSignature.FromReader(new BlobReaderContext(_context), Constructor, reader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SerializedFieldDefinition(ModuleReaderContext context, MetadataToken toke
$"Invalid signature blob index in field {MetadataToken.ToString()}.");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
return FieldSignature.FromReader(ref context, ref reader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SerializedMemberReference(
$"Invalid signature blob index in member reference {MetadataToken.ToString()}.");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
return CallingConventionSignature.FromReader(ref context, ref reader, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SerializedMethodDefinition(ModuleReaderContext context, MetadataToken tok
$"Invalid signature blob index in method {MetadataToken.ToString()}.");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
return MethodSignature.FromReader(ref context, ref reader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SerializedMethodSpecification(ModuleReaderContext context, MetadataToken
$"Invalid instantiation blob index in method specification {MetadataToken.ToString()}.");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
return GenericInstanceMethodSignature.FromReader(ref context, ref reader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public SerializedPropertyDefinition(ModuleReaderContext context, MetadataToken t
$"Invalid signature blob index in property {MetadataToken.ToString()}.");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
return PropertySignature.FromReader(ref context, ref reader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public SerializedSecurityDeclaration(
$"Invalid permission set blob index in security declaration {MetadataToken.ToString()}.");
}

return PermissionSetSignature.FromReader(new BlobReadContext(_context), ref reader);
return PermissionSetSignature.FromReader(new BlobReaderContext(_context), ref reader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SerializedStandAloneSignature(
$"Invalid signature blob index in stand-alone signature {MetadataToken.ToString()}.");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
return CallingConventionSignature.FromReader(ref context, ref reader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SerializedTypeSpecification(ModuleReaderContext context, MetadataToken to
$"Invalid blob signature for type specification {MetadataToken.ToString()}");
}

var context = new BlobReadContext(_context);
var context = new BlobReaderContext(_context);
context.StepInToken(MetadataToken);
return TypeSignature.FromReader(ref context, ref reader);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AsmResolver.DotNet.Serialized;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.PE.DotNet.Metadata.Tables;
Expand All @@ -10,44 +10,31 @@ namespace AsmResolver.DotNet.Signatures
/// Provides a context in which a metadata blob parser exists in. This includes the original module reader context
/// as well as a mechanism to protect against infinite recursion.
/// </summary>
public struct BlobReadContext
public struct BlobReaderContext
{
private Stack<MetadataToken>? _traversedTokens;

/// <summary>
/// Creates a new instance of the <see cref="BlobReadContext"/> structure.
/// Creates a new instance of the <see cref="BlobReaderContext"/> structure.
/// </summary>
/// <param name="readerContext">The original read context.</param>
public BlobReadContext(ModuleReaderContext readerContext)
public BlobReaderContext(ModuleReaderContext readerContext)
: this(readerContext, PhysicalTypeSignatureResolver.Instance)
{
}

/// <summary>
/// Creates a new instance of the <see cref="BlobReadContext"/> structure.
/// Creates a new instance of the <see cref="BlobReaderContext"/> structure.
/// </summary>
/// <param name="readerContext">The original read context.</param>
/// <param name="resolver">The object responsible for resolving raw type metadata tokens and addresses.</param>
public BlobReadContext(ModuleReaderContext readerContext, ITypeSignatureResolver resolver)
public BlobReaderContext(ModuleReaderContext readerContext, ITypeSignatureResolver resolver)
{
ReaderContext = readerContext;
TypeSignatureResolver = resolver;
_traversedTokens = null;
}

/// <summary>
/// Creates a new instance of the <see cref="BlobReadContext"/> structure.
/// </summary>
/// <param name="readerContext">The original read context.</param>
/// <param name="resolver">The object responsible for resolving raw type metadata tokens and addresses.</param>
/// <param name="traversedTokens">A collection of traversed metadata tokens.</param>
public BlobReadContext(ModuleReaderContext readerContext, ITypeSignatureResolver resolver, IEnumerable<MetadataToken> traversedTokens)
{
ReaderContext = readerContext;
TypeSignatureResolver = resolver;
_traversedTokens = new Stack<MetadataToken>(traversedTokens);
}

/// <summary>
/// Gets the module reader context.
/// </summary>
Expand All @@ -64,15 +51,36 @@ public ITypeSignatureResolver TypeSignatureResolver
get;
}

/// <summary>
/// Records a step in the blob reading process where a metadata token into the tables stream is about to
/// be traversed.
/// </summary>
/// <param name="token">The token to traverse</param>
/// <returns>
/// <c>true</c> if this token was recorded, <c>false</c> if the token was already traversed before.
/// </returns>
public bool StepInToken(MetadataToken token)
{
_traversedTokens ??= new Stack<MetadataToken>();

if (_traversedTokens.Contains(token))
if (_traversedTokens is null)
_traversedTokens = new Stack<MetadataToken>();
else if (_traversedTokens.Contains(token))
return false;

_traversedTokens.Push(token);
return true;
}

/// <summary>
/// Records a step in the blob reading process where the last recorded metadata token into the tables stream
/// was traversed and processed completely.
/// </summary>
/// <exception cref="InvalidOperationException">Occurs when there was no token traversed.</exception>
public void StepOutToken()
{
if (_traversedTokens is null)
throw new InvalidOperationException();

_traversedTokens.Pop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class CallingConventionSignature : ExtendableBlobSignature, IImp
/// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param>
/// <returns>The read signature.</returns>
public static CallingConventionSignature? FromReader(
ref BlobReadContext context,
ref BlobReaderContext context,
ref BinaryStreamReader reader,
bool readToEnd = true)
{
Expand All @@ -35,7 +35,7 @@ public abstract class CallingConventionSignature : ExtendableBlobSignature, IImp
return signature;
}

private static CallingConventionSignature? ReadSignature(ref BlobReadContext context, ref BinaryStreamReader reader)
private static CallingConventionSignature? ReadSignature(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
byte flag = reader.ReadByte();
reader.Offset--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CustomAttributeArgument
/// <param name="argumentType">The type of the argument to read.</param>
/// <param name="reader">The input stream.</param>
/// <returns>The argument.</returns>
public static CustomAttributeArgument FromReader(in BlobReadContext context, TypeSignature argumentType,
public static CustomAttributeArgument FromReader(in BlobReaderContext context, TypeSignature argumentType,
ref BinaryStreamReader reader)
{
var elementReader = CustomAttributeArgumentReader.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public bool IsNullArray
private set;
}

public void ReadValue(in BlobReadContext context, ref BinaryStreamReader reader, TypeSignature valueType)
public void ReadValue(in BlobReaderContext context, ref BinaryStreamReader reader, TypeSignature valueType)
{
var module = context.ReaderContext.ParentModule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public CustomAttributeArgument Argument
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The input stream.</param>
/// <returns>The argument.</returns>
public static CustomAttributeNamedArgument FromReader(in BlobReadContext context, ref BinaryStreamReader reader)
public static CustomAttributeNamedArgument FromReader(in BlobReaderContext context, ref BinaryStreamReader reader)
{
var memberType = (CustomAttributeArgumentMemberType) reader.ReadByte();
var argumentType = TypeSignature.ReadFieldOrPropType(context, ref reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public IList<CustomAttributeNamedArgument> NamedArguments
/// <returns>The signature.</returns>
/// <exception cref="FormatException">Occurs when the input stream does not point to a valid signature.</exception>
public static CustomAttributeSignature FromReader(
in BlobReadContext context,
in BlobReaderContext context,
ICustomAttributeType ctor,
in BinaryStreamReader reader)
{
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Signatures/FieldSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static FieldSignature CreateInstance(TypeSignature fieldType)
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The blob input stream.</param>
/// <returns>The field signature.</returns>
public static FieldSignature FromReader(ref BlobReadContext context, ref BinaryStreamReader reader)
public static FieldSignature FromReader(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
return new(
(CallingConventionAttributes) reader.ReadByte(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AsmResolver.DotNet.Signatures
public class GenericInstanceMethodSignature : CallingConventionSignature, IGenericArgumentsProvider
{
internal static GenericInstanceMethodSignature? FromReader(
ref BlobReadContext context,
ref BlobReaderContext context,
ref BinaryStreamReader reader)
{
if (!reader.CanRead(sizeof(byte)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class LocalVariablesSignature : CallingConventionSignature
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The input stream.</param>
/// <returns>The signature.</returns>
public static LocalVariablesSignature FromReader(ref BlobReadContext context, ref BinaryStreamReader reader)
public static LocalVariablesSignature FromReader(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
var result = new LocalVariablesSignature();
result.Attributes = (CallingConventionAttributes) reader.ReadByte();
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Signatures/MethodSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MethodSignature : MethodSignatureBase
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The blob input stream.</param>
/// <returns>The method signature.</returns>
public static MethodSignature FromReader(ref BlobReadContext context, ref BinaryStreamReader reader)
public static MethodSignature FromReader(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
var result = new MethodSignature(
(CallingConventionAttributes) reader.ReadByte(),
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Signatures/MethodSignatureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public override bool IsImportedInModule(ModuleDefinition module)
/// </summary>
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The input stream.</param>
protected void ReadParametersAndReturnType(ref BlobReadContext context, ref BinaryStreamReader reader)
protected void ReadParametersAndReturnType(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
// Parameter count.
if (!reader.TryReadCompressedUInt32(out uint parameterCount))
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Signatures/PropertySignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PropertySignature : MethodSignatureBase
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The blob input stream.</param>
/// <returns>The property signature.</returns>
public static PropertySignature? FromReader(ref BlobReadContext context, ref BinaryStreamReader reader)
public static PropertySignature? FromReader(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
var attributes = (CallingConventionAttributes) reader.ReadByte();
if ((attributes & CallingConventionAttributes.Property) == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PermissionSetSignature : ExtendableBlobSignature
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The input blob stream.</param>
/// <returns>The permission set.</returns>
public static PermissionSetSignature FromReader(in BlobReadContext context, ref BinaryStreamReader reader)
public static PermissionSetSignature FromReader(in BlobReaderContext context, ref BinaryStreamReader reader)
{
var result = new PermissionSetSignature();
if (reader.ReadByte() != '.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SecurityAttribute
/// <param name="context">The blob reader context.</param>
/// <param name="reader">The input blob stream.</param>
/// <returns>The security attribute.</returns>
public static SecurityAttribute FromReader(in BlobReadContext context, ref BinaryStreamReader reader)
public static SecurityAttribute FromReader(in BlobReaderContext context, ref BinaryStreamReader reader)
{
string? typeName = reader.ReadSerString();
var type = string.IsNullOrEmpty(typeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AsmResolver.DotNet.Signatures
/// </summary>
public class SerializedCustomAttributeSignature : CustomAttributeSignature
{
private readonly BlobReadContext _context;
private readonly BlobReaderContext _context;
private readonly TypeSignature[] _fixedArgTypes;
private readonly BinaryStreamReader _reader;

Expand All @@ -21,7 +21,7 @@ public class SerializedCustomAttributeSignature : CustomAttributeSignature
/// <param name="fixedArgTypes">The types of all fixed arguments.</param>
/// <param name="reader">The input blob reader.</param>
public SerializedCustomAttributeSignature(
in BlobReadContext context,
in BlobReaderContext context,
IEnumerable<TypeSignature> fixedArgTypes,
in BinaryStreamReader reader)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public IList<ArrayDimension> Dimensions
get;
}

internal new static ArrayTypeSignature FromReader(ref BlobReadContext context, ref BinaryStreamReader reader)
internal new static ArrayTypeSignature FromReader(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
var signature = new ArrayTypeSignature(TypeSignature.FromReader(ref context, ref reader));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GenericInstanceTypeSignature : TypeSignature, IGenericArgumentsProv
private ITypeDefOrRef _genericType;
private bool _isValueType;

internal new static GenericInstanceTypeSignature FromReader(ref BlobReadContext context, ref BinaryStreamReader reader)
internal new static GenericInstanceTypeSignature FromReader(ref BlobReaderContext context, ref BinaryStreamReader reader)
{
var genericType = TypeSignature.FromReader(ref context, ref reader);
var signature = new GenericInstanceTypeSignature(genericType.ToTypeDefOrRef(), genericType.ElementType == ElementType.ValueType);
Expand Down
Loading

0 comments on commit 983873b

Please sign in to comment.