Skip to content

Commit

Permalink
Eliminate extra dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindshmicrosoft committed Dec 13, 2019
1 parent b5fcdb7 commit 4576f22
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,4 @@
<PackagePath></PackagePath>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.6.0" />
</ItemGroup>
</Project>
38 changes: 0 additions & 38 deletions src/Microsoft.Diagnostics.Runtime/src/Extensions/DebugOnly.cs

This file was deleted.

58 changes: 0 additions & 58 deletions src/Microsoft.Diagnostics.Runtime/src/Extensions/SpanExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public FileVersionInfo GetFileVersionInfo()

PEBuffer buff = AllocBuff();
byte* bytes = versionNode.FetchData(0, versionNode.DataLength, buff);
FileVersionInfo ret = new FileVersionInfo(new Span<byte>(bytes, versionNode.DataLength));
FileVersionInfo ret = new FileVersionInfo(bytes, versionNode.DataLength);

FreeBuff(buff);
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public FileVersionInfo GetFileVersionInfo()
{
PEBuffer buff = _file.AllocBuff();
byte* bytes = FetchData(0, DataLength, buff);

FileVersionInfo ret = new FileVersionInfo(new Span<byte>(bytes, DataLength));
FileVersionInfo ret = new FileVersionInfo(bytes, DataLength);
_file.FreeBuff(buff);
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Text;

namespace Microsoft.Diagnostics.Runtime.Utilities
{
Expand All @@ -27,16 +28,33 @@ public sealed unsafe class FileVersionInfo
/// </summary>
public string? Comments { get; }

internal FileVersionInfo(Span<byte> data)
internal FileVersionInfo(byte[] data, int dataLen)
{
data = data.Slice(DataOffset);
fixed (byte* ptr = data)
{
string dataAsString = new string((char*)ptr, 0, data.Length / 2);
FileVersion = "";
if (dataLen <= DataOffset)
return;

FileVersion = GetDataString(dataAsString, "FileVersion");
Comments = GetDataString(dataAsString, "Comments");
}
var dataAsString = Encoding.Unicode.GetString(data, DataOffset, dataLen - DataOffset);

FileVersion = GetDataString(dataAsString, "FileVersion");
Comments = GetDataString(dataAsString, "Comments");
}

[Obsolete]
internal FileVersionInfo(byte* data, int dataLen)
{
FileVersion = "";
if (dataLen <= 0x5c)
return;

// See http://msdn.microsoft.com/en-us/library/ms647001(v=VS.85).aspx
byte* stringInfoPtr = data + 0x5c; // Gets to first StringInfo

// TODO search for FileVersion string ...
string dataAsString = new string((char*)stringInfoPtr, 0, (dataLen - 0x5c) / 2);

FileVersion = GetDataString(dataAsString, "FileVersion");
Comments = GetDataString(dataAsString, "Comments");
}

private static string? GetDataString(string dataAsString, string fileVersionKey)
Expand Down
37 changes: 7 additions & 30 deletions src/Microsoft.Diagnostics.Runtime/src/Utilities/PEImage/PEImage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Diagnostics.Runtime.Interop;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -220,27 +220,11 @@ public int Read(byte[] dest, int virtualAddress, int bytesRequested)
return read;
}

/// <summary>
/// Reads data out of PE image into a native buffer.
/// </summary>
/// <param name="virtualAddress">The address to read from.</param>
/// <param name="dest">The location to write the data.</param>
/// <returns>The number of bytes actually read from the image and written to dest.</returns>
public int Read(int virtualAddress, Span<byte> dest)
{
int offset = RvaToOffset(virtualAddress);
if (offset == -1)
return 0;

SeekTo(offset);
return Stream.Read(dest);
}

/// <summary>
/// Gets the File Version Information that is stored as a resource in the PE file. (This is what the
/// version tab a file's property page is populated with).
/// </summary>
public FileVersionInfo? GetFileVersionInfo()
public unsafe FileVersionInfo? GetFileVersionInfo()
{
ResourceEntry? versionNode = Resources.Children.FirstOrDefault(r => r.Name == "Version");
if (versionNode == null || versionNode.Children.Count != 1)
Expand All @@ -254,18 +238,11 @@ public int Read(int virtualAddress, Span<byte> dest)
if (size <= FileVersionInfo.DataOffset)
return null;

byte[] buffer = ArrayPool<byte>.Shared.Rent(size);
try
{
int count = versionNode.GetData(buffer);
Span<byte> span = new Span<byte>(buffer, 0, count);
FileVersionInfo result = new FileVersionInfo(span);
return result;
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
byte[] bytes = versionNode.GetData();

FileVersionInfo ret = new FileVersionInfo(bytes, versionNode.Size);

return ret;
}

private ResourceEntry CreateResourceRoot()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace Microsoft.Diagnostics.Runtime.Utilities
Expand Down Expand Up @@ -98,36 +98,24 @@ public byte[] GetData()
return result;
}

/// <summary>
/// Get the data associated with this entry.
/// </summary>
/// <param name="span">The location to write the data</param>
/// <returns>The number of bytes actually read from the image and written to dest</returns>
public int GetData(Span<byte> span)
{
GetDataVaAndSize(out int va, out int size);
if (size == 0 || va == 0)
return 0;

return Image.Read(va, span);
}

/// <summary>
/// A convenience function to get structured data out of this entry.
/// </summary>
/// <typeparam name="T">A struct type to convert.</typeparam>
/// <param name="offset">The offset into the data.</param>
/// <returns>The struct that was read out of the data section.</returns>
public unsafe T GetData<T>(int offset = 0) where T : unmanaged
public T GetData<T>(int offset = 0) where T : struct
{
int size = Unsafe.SizeOf<T>();
GetDataVaAndSize(out int va, out int sectionSize);
if (va == 0 || sectionSize < size + offset)
return default;

T output;
int read = Image.Read(va + offset, new Span<byte>(&output, size));
return read == size ? output : default;
byte[] data = GetData();
int size = Marshal.SizeOf(typeof(T));
if (size + offset > data.Length)
throw new IndexOutOfRangeException();

GCHandle hnd = GCHandle.Alloc(data, GCHandleType.Pinned);
T result = (T)Marshal.PtrToStructure(hnd.AddrOfPinnedObject(), typeof(T));
hnd.Free();

return result;
}

private ResourceEntry[] GetChildren()
Expand Down

0 comments on commit 4576f22

Please sign in to comment.