Skip to content

Commit

Permalink
Make a copy of the image for PEReader (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
cshung authored Jan 3, 2020
1 parent 662817d commit af90e20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Reflection.Metadata">
<Version>1.6.0</Version>
</PackageReference>
<PackageReference Include="System.Reflection.Metadata" Version="1.6.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

Expand Down Expand Up @@ -226,27 +227,23 @@ private unsafe void Initialize()

if (MetadataReader == null)
{
Image = File.ReadAllBytes(Filename);
byte[] image = File.ReadAllBytes(Filename);
Image = image;

fixed (byte* p = Image)
PEReader = new PEReader(Unsafe.As<byte[], ImmutableArray<byte>>(ref image));

if (!PEReader.HasMetadata)
{
IntPtr ptr = (IntPtr)p;
PEReader = new PEReader(p, Image.Length);
throw new Exception($"ECMA metadata not found in file '{Filename}'");
}

if (!PEReader.HasMetadata)
{
throw new Exception($"ECMA metadata not found in file '{Filename}'");
}
MetadataReader = PEReader.GetMetadataReader();

MetadataReader = PEReader.GetMetadataReader();
}
}
else
{
ImmutableArray<byte> content = PEReader.GetEntireImage().GetContent();
// TODO: Avoid copying
Image = new byte[content.Length];
content.CopyTo(Image);
Image = Unsafe.As<ImmutableArray<byte>, byte[]>(ref content);
}

ParseHeader();
Expand Down

0 comments on commit af90e20

Please sign in to comment.