Skip to content

Commit

Permalink
Add BinaryStreamReader convenience constructors. Deprecate ByteArrayD…
Browse files Browse the repository at this point in the history
…ataSource.CreateReader.
  • Loading branch information
Washi1337 committed Oct 18, 2022
1 parent e9d9e5d commit 54c65ee
Show file tree
Hide file tree
Showing 33 changed files with 77 additions and 60 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -4,7 +4,7 @@
- master

image: Visual Studio 2022
version: 4.11.2-master-build.{build}
version: 5.0.0-master-build.{build}
configuration: Release

skip_commits:
Expand Down
Expand Up @@ -95,7 +95,7 @@ public DynamicCilOperandResolver(SerializedModuleDefinition contextModule, CilMe
break;

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

Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet.Dynamic/DynamicMethodDefinition.cs
Expand Up @@ -103,7 +103,7 @@ private static CilMethodBody CreateDynamicMethodBody(MethodDefinition method, ob
DynamicMethodHelper.ReadLocalVariables(result, method, localSig);

// Read raw instructions.
var reader = ByteArrayDataSource.CreateReader(code);
var reader = new BinaryStreamReader(code);
var disassembler = new CilDisassembler(reader, new DynamicCilOperandResolver(module, result, tokenList));
result.Instructions.AddRange(disassembler.ReadInstructions());

Expand Down
9 changes: 4 additions & 5 deletions src/AsmResolver.DotNet.Dynamic/DynamicMethodHelper.cs
Expand Up @@ -30,13 +30,12 @@ static DynamicMethodHelper()

public static void ReadLocalVariables(CilMethodBody methodBody, MethodDefinition method, byte[] localSig)
{
if (!(method.Module is SerializedModuleDefinition module))
if (method.Module is not SerializedModuleDefinition module)
throw new ArgumentException("Method body should reference a serialized module.");

var reader = ByteArrayDataSource.CreateReader(localSig);
if (ReadLocalVariableSignature(
new BlobReadContext(module.ReaderContext),
ref reader) is not LocalVariablesSignature localsSignature)
var reader = new BinaryStreamReader(localSig);
if (ReadLocalVariableSignature(new BlobReadContext(module.ReaderContext), ref reader)
is not { } localsSignature)
{
throw new ArgumentException("Invalid local variables signature.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Code/Cil/CilMethodBody.cs
Expand Up @@ -227,7 +227,7 @@ public bool VerifyLabelsOnBuild
var section = fatBody.ExtraSections[i];
if (section.IsEHTable)
{
var reader = ByteArrayDataSource.CreateReader(section.Data);
var reader = new BinaryStreamReader(section.Data);
uint size = section.IsFat
? CilExceptionHandler.FatExceptionHandlerSize
: CilExceptionHandler.TinyExceptionHandlerSize;
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Signatures/DataBlobSignature.cs
Expand Up @@ -43,7 +43,7 @@ public byte[] Data
/// <returns>The deserialized literal.</returns>
public object InterpretData(ElementType elementType)
{
var reader = ByteArrayDataSource.CreateReader(Data);
var reader = new BinaryStreamReader(Data);

return elementType switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.PE.File/PEFile.cs
Expand Up @@ -135,7 +135,7 @@ public static PEFile FromFile(IInputFile file)
/// <param name="raw">The raw bytes representing the contents of the PE file to read.</param>
/// <returns>The PE file that was read.</returns>
/// <exception cref="BadImageFormatException">Occurs when the file does not follow the PE file format.</exception>
public static PEFile FromBytes(byte[] raw) => FromReader(ByteArrayDataSource.CreateReader(raw));
public static PEFile FromBytes(byte[] raw) => FromReader(new BinaryStreamReader(raw));

/// <summary>
/// Reads a mapped PE file starting at the provided module base address (HINSTANCE).
Expand Down
Expand Up @@ -15,7 +15,7 @@ public class SerializedBlobStream : BlobStream
/// </summary>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedBlobStream(byte[] rawData)
: this(DefaultName, ByteArrayDataSource.CreateReader(rawData))
: this(DefaultName, new BinaryStreamReader(rawData))
{
}

Expand All @@ -25,7 +25,7 @@ public SerializedBlobStream(byte[] rawData)
/// <param name="name">The name of the stream.</param>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedBlobStream(string name, byte[] rawData)
: this(name, ByteArrayDataSource.CreateReader(rawData))
: this(name, new BinaryStreamReader(rawData))
{
}

Expand Down
Expand Up @@ -18,7 +18,7 @@ public class SerializedGuidStream : GuidStream
/// </summary>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedGuidStream(byte[] rawData)
: this(DefaultName, ByteArrayDataSource.CreateReader(rawData))
: this(DefaultName, new BinaryStreamReader(rawData))
{
}

Expand All @@ -28,7 +28,7 @@ public SerializedGuidStream(byte[] rawData)
/// <param name="name">The name of the stream.</param>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedGuidStream(string name, byte[] rawData)
: this(name, ByteArrayDataSource.CreateReader(rawData))
: this(name, new BinaryStreamReader(rawData))
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.PE/DotNet/Metadata/Metadata.cs
Expand Up @@ -81,7 +81,7 @@ public IList<IMetadataStream> Streams
/// </summary>
/// <param name="data">The raw data.</param>
/// <returns>The read metadata.</returns>
public static Metadata FromBytes(byte[] data) => FromReader(ByteArrayDataSource.CreateReader(data));
public static Metadata FromBytes(byte[] data) => FromReader(new BinaryStreamReader(data));

/// <summary>
/// Reads a .NET metadata directory from a file.
Expand Down
4 changes: 2 additions & 2 deletions src/AsmResolver.PE/DotNet/Metadata/Pdb/SerializedPdbStream.cs
Expand Up @@ -14,7 +14,7 @@ public class SerializedPdbStream : PdbStream
/// </summary>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedPdbStream(byte[] rawData)
: this(DefaultName, ByteArrayDataSource.CreateReader(rawData))
: this(DefaultName, new BinaryStreamReader(rawData))
{
}

Expand All @@ -24,7 +24,7 @@ public SerializedPdbStream(byte[] rawData)
/// <param name="name">The name of the stream.</param>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedPdbStream(string name, byte[] rawData)
: this(name, ByteArrayDataSource.CreateReader(rawData))
: this(name, new BinaryStreamReader(rawData))
{
}

Expand Down
Expand Up @@ -17,7 +17,7 @@ public class SerializedStringsStream : StringsStream
/// </summary>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedStringsStream(byte[] rawData)
: this(DefaultName, ByteArrayDataSource.CreateReader(rawData))
: this(DefaultName, new BinaryStreamReader(rawData))
{
}

Expand All @@ -27,7 +27,7 @@ public SerializedStringsStream(byte[] rawData)
/// <param name="name">The name of the stream.</param>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedStringsStream(string name, byte[] rawData)
: this(name, ByteArrayDataSource.CreateReader(rawData))
: this(name, new BinaryStreamReader(rawData))
{
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ public class SerializedTableStream : TablesStream, ILazyMetadataStream
/// <param name="name">The name of the stream.</param>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedTableStream(MetadataReaderContext context, string name, byte[] rawData)
: this(context, name, ByteArrayDataSource.CreateReader(rawData))
: this(context, name, new BinaryStreamReader(rawData))
{
}

Expand Down
Expand Up @@ -17,7 +17,7 @@ public class SerializedUserStringsStream : UserStringsStream
/// </summary>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedUserStringsStream(byte[] rawData)
: this(DefaultName, ByteArrayDataSource.CreateReader(rawData))
: this(DefaultName, new BinaryStreamReader(rawData))
{
}

Expand All @@ -27,7 +27,7 @@ public SerializedUserStringsStream(byte[] rawData)
/// <param name="name">The name of the stream.</param>
/// <param name="rawData">The raw contents of the stream.</param>
public SerializedUserStringsStream(string name, byte[] rawData)
: this(name, ByteArrayDataSource.CreateReader(rawData))
: this(name, new BinaryStreamReader(rawData))
{
}

Expand Down
Expand Up @@ -22,7 +22,7 @@ public class StrongNamePrivateKey : StrongNamePublicKey
/// <exception cref="NotSupportedException">Occurs when an invalid or unsupported algorithm is specified.</exception>
public new static StrongNamePrivateKey FromFile(string path)
{
var reader = ByteArrayDataSource.CreateReader(System.IO.File.ReadAllBytes(path));
var reader = new BinaryStreamReader(System.IO.File.ReadAllBytes(path));
return FromReader(ref reader);
}

Expand Down
Expand Up @@ -24,7 +24,7 @@ public class StrongNamePublicKey : StrongNameKeyStructure
/// <exception cref="NotSupportedException">Occurs when an invalid or unsupported algorithm is specified.</exception>
public static StrongNamePublicKey FromFile(string path)
{
var reader = ByteArrayDataSource.CreateReader(System.IO.File.ReadAllBytes(path));
var reader = new BinaryStreamReader(System.IO.File.ReadAllBytes(path));
return FromReader(ref reader);
}

Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.Symbols.Pdb/Msf/MsfFile.cs
Expand Up @@ -88,7 +88,7 @@ public MsfFile(uint blockSize)
/// </summary>
/// <param name="data">The data to interpret.</param>
/// <returns>The read MSF file.</returns>
public static MsfFile FromBytes(byte[] data) => FromReader(ByteArrayDataSource.CreateReader(data));
public static MsfFile FromBytes(byte[] data) => FromReader(new BinaryStreamReader(data));

/// <summary>
/// Reads an MSF file from the provided input stream reader.
Expand Down
18 changes: 18 additions & 0 deletions src/AsmResolver/IO/BinaryStreamReader.cs
Expand Up @@ -13,6 +13,24 @@ public struct BinaryStreamReader
[ThreadStatic]
private static int[]? _buffer;

/// <summary>
/// Creates a new binary stream reader on the provided data source.
/// </summary>
/// <param name="data">The data to read from.</param>
public BinaryStreamReader(byte[] data)
: this(new ByteArrayDataSource(data))
{
}

/// <summary>
/// Creates a new binary stream reader on the provided data source.
/// </summary>
/// <param name="dataSource">The object to get the data from.</param>
public BinaryStreamReader(IDataSource dataSource)
: this(dataSource, 0, 0, (uint) dataSource.Length)
{
}

/// <summary>
/// Creates a new binary stream reader on the provided data source.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/AsmResolver/IO/ByteArrayDataSource.cs
Expand Up @@ -46,8 +46,8 @@ public ulong BaseAddress
/// </summary>
/// <param name="data">The byte array to read.</param>
/// <returns>The stream reader.</returns>
public static BinaryStreamReader CreateReader(byte[] data) =>
new(new ByteArrayDataSource(data), 0, 0, (uint) data.Length);
[Obsolete("Use the constructor of AsmResolver.IO.BinaryStreamReader instead.")]
public static BinaryStreamReader CreateReader(byte[] data) => new(data);

/// <inheritdoc />
public bool IsValidAddress(ulong address) => address - BaseAddress < (ulong) _data.Length;
Expand Down
2 changes: 1 addition & 1 deletion test/AsmResolver.DotNet.Tests/AssemblyDefinitionTest.cs
Expand Up @@ -13,7 +13,7 @@ private static AssemblyDefinition Rebuild(AssemblyDefinition assembly)
{
using var stream = new MemoryStream();
assembly.ManifestModule.Write(stream);
return AssemblyDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
return AssemblyDefinition.FromReader(new BinaryStreamReader(stream.ToArray()));
}

[Fact]
Expand Down
Expand Up @@ -103,7 +103,7 @@ public void MarkFilesAsCompressed()
using var stream = new MemoryStream();
ulong address = manifest.WriteManifest(new BinaryStreamWriter(stream), false);

var reader = ByteArrayDataSource.CreateReader(stream.ToArray());
var reader = new BinaryStreamReader(stream.ToArray());
reader.Offset = address;
var newManifest = BundleManifest.FromReader(reader);
AssertBundlesAreEqual(manifest, newManifest);
Expand Down
2 changes: 1 addition & 1 deletion test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs
Expand Up @@ -34,7 +34,7 @@ public void PersistentConstructor()
using var stream = new MemoryStream();
module.Write(stream);

module = ModuleDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
module = ModuleDefinition.FromReader(new BinaryStreamReader(stream.ToArray()));

var type = module.TopLevelTypes.First(t => t.Name == nameof(CustomAttributesTestClass));
Assert.All(type.CustomAttributes, a =>
Expand Down
4 changes: 2 additions & 2 deletions test/AsmResolver.DotNet.Tests/ModuleDefinitionTest.cs
Expand Up @@ -24,7 +24,7 @@ private static ModuleDefinition Rebuild(ModuleDefinition module)
{
using var stream = new MemoryStream();
module.Write(stream);
return ModuleDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
return ModuleDefinition.FromReader(new BinaryStreamReader(stream.ToArray()));
}

[SkippableFact]
Expand Down Expand Up @@ -285,7 +285,7 @@ public void PersistentResources()
// Write and rebuild.
using var stream = new MemoryStream();
module.Write(stream);
var newModule = ModuleDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
var newModule = ModuleDefinition.FromReader(new BinaryStreamReader(stream.ToArray()));

// Assert contents.
var newDirectory = (IResourceDirectory)newModule.NativeResourceDirectory.Entries
Expand Down
6 changes: 3 additions & 3 deletions test/AsmResolver.DotNet.Tests/Resources/ResourceSetTest.cs
Expand Up @@ -87,7 +87,7 @@ public void PersistentIntrinsicElement(string key, ResourceTypeCode type, object
using var stream = new MemoryStream();
set.Write(new BinaryStreamWriter(stream));

var actualSet = ResourceSet.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
var actualSet = ResourceSet.FromReader(new BinaryStreamReader(stream.ToArray()));
var actualEntry = actualSet.First(e => e.Name == key);
Assert.Equal(entry.Type, actualEntry.Type);
Assert.Equal(entry.Data, actualEntry.Data);
Expand All @@ -110,7 +110,7 @@ public void PersistentUserDefinedTypeElement()
using var stream = new MemoryStream();
set.Write(new BinaryStreamWriter(stream));

var actualSet = ResourceSet.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
var actualSet = ResourceSet.FromReader(new BinaryStreamReader(stream.ToArray()));
var actualEntry = actualSet.First(e => e.Name == "Point");
Assert.Equal(entry.Type.FullName, actualEntry.Type.FullName);
Assert.Equal(entry.Data, actualEntry.Data);
Expand All @@ -134,7 +134,7 @@ public void PersistentSetMultipleEntries()

var resourceReader = new ResourceReader(stream);

var actualSet = ResourceSet.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
var actualSet = ResourceSet.FromReader(new BinaryStreamReader(stream.ToArray()));
Assert.Equal(set.Count, actualSet.Count);
for (int i = 0; i < set.Count; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion test/AsmResolver.DotNet.Tests/SecurityDeclarationTest.cs
Expand Up @@ -16,7 +16,7 @@ private static MethodDefinition LookupMethod(string methodName, bool rebuild)
{
var stream = new MemoryStream();
module.Write(stream);
module = ModuleDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
module = ModuleDefinition.FromReader(new BinaryStreamReader(stream.ToArray()));
}

var type = module.TopLevelTypes.First(t => t.Name == nameof(SecurityAttributes));
Expand Down
2 changes: 1 addition & 1 deletion test/AsmResolver.PE.Tests/DotNet/Metadata/MetadataTest.cs
Expand Up @@ -103,7 +103,7 @@ public void PreserveMetadataNoChange()
using var tempStream = new MemoryStream();
metadata.Write(new BinaryStreamWriter(tempStream));

var reader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var reader = new BinaryStreamReader(tempStream.ToArray());
var context = MetadataReaderContext.FromReaderContext(new PEReaderContext(peFile));
var newMetadata = new SerializedMetadata(context, ref reader);

Expand Down
Expand Up @@ -21,7 +21,7 @@ internal static class RowTestUtils

using var tempStream = new MemoryStream();
expected.Write(new BinaryStreamWriter(tempStream), table.Layout);
var reader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var reader = new BinaryStreamReader(tempStream.ToArray());
var newRow = readRow(ref reader, table.Layout);

Assert.Equal(expected, newRow);
Expand All @@ -36,7 +36,7 @@ internal static class RowTestUtils

using var tempStream = new MemoryStream();
expected.Write(new BinaryStreamWriter(tempStream), table.Layout);
var reader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var reader = new BinaryStreamReader(tempStream.ToArray());
var newRow = readRow(new MetadataReaderContext(VirtualAddressFactory.Instance), ref reader, table.Layout);

Assert.Equal(expected, newRow);
Expand Down
Expand Up @@ -18,7 +18,7 @@ public void PersistentStrongNamePublicKey()
using var tempStream = new MemoryStream();
publicKey.Write(new BinaryStreamWriter(tempStream));

var reader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var reader = new BinaryStreamReader(tempStream.ToArray());
var newPublicKey = StrongNamePublicKey.FromReader(ref reader);

Assert.Equal(publicKey.Modulus, newPublicKey.Modulus);
Expand Down
Expand Up @@ -45,7 +45,7 @@ public void PersistentFixedVersionInfo()
versionInfo.Write(new BinaryStreamWriter(tempStream));

// Reload.
var infoReader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var infoReader = new BinaryStreamReader(tempStream.ToArray());
var newVersionInfo = VersionInfoResource.FromReader(ref infoReader);
var newFixedVersionInfo = newVersionInfo.FixedVersionInfo;

Expand Down Expand Up @@ -117,7 +117,7 @@ public void PersistentVarFileInfo()
versionInfo.Write(new BinaryStreamWriter(tempStream));

// Reload.
var infoReader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var infoReader = new BinaryStreamReader(tempStream.ToArray());
var newVersionInfo = VersionInfoResource.FromReader(ref infoReader);

// Verify.
Expand Down Expand Up @@ -152,7 +152,7 @@ public void PersistentStringFileInfo()
versionInfo.Write(new BinaryStreamWriter(tempStream));

// Reload.
var infoReader = ByteArrayDataSource.CreateReader(tempStream.ToArray());
var infoReader = new BinaryStreamReader(tempStream.ToArray());
var newVersionInfo = VersionInfoResource.FromReader(ref infoReader);

// Verify.
Expand Down
Expand Up @@ -20,7 +20,7 @@ private DbiStream GetDbiStream(bool rebuild)
{
using var stream = new MemoryStream();
dbiStream.Write(new BinaryStreamWriter(stream));
dbiStream = DbiStream.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
dbiStream = DbiStream.FromReader(new BinaryStreamReader(stream.ToArray()));
}

return dbiStream;
Expand Down

0 comments on commit 54c65ee

Please sign in to comment.