Skip to content

Commit

Permalink
Merge pull request #8 from Workshell/develop
Browse files Browse the repository at this point in the history
Merge of Develop into Master
  • Loading branch information
lkinsella authored May 20, 2023
2 parents c320fb1 + 95f8859 commit 8477a41
Show file tree
Hide file tree
Showing 68 changed files with 2,231 additions and 1,966 deletions.
100 changes: 50 additions & 50 deletions src/Workshell.PE.Resources/Workshell.PE.Resources.csproj
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>Workshell.snk</AssemblyOriginatorKeyFile>
<Configurations>Debug;Release;CI</Configurations>
</PropertyGroup>

<PropertyGroup>
<PackageId>Workshell.PE.Resources</PackageId>
<PackageDescription>A set of classes that extends the Workshell.PE class library to help deal with the standard resource types within an executable such as bitmaps, icons, cursors etc.</PackageDescription>
<PackageProjectUrl>https://github.com/Workshell/pe</PackageProjectUrl>
<PackageIconUrl>http://img.workshell.co.uk/logo_128.png</PackageIconUrl>
<PackageTags>workshell pe executable native resources</PackageTags>
<PackageLicenseFile>license.txt</PackageLicenseFile>
<Copyright>Workshell Ltd</Copyright>
<Authors>Workshell Ltd</Authors>
<RepositoryUrl>https://github.com/Workshell/pe</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.0.0.1</Version>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\license.txt" Pack="true" PackagePath="$(PackageLicenseFile)" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\bin\debug</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\..\bin\release</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>Workshell.snk</AssemblyOriginatorKeyFile>
<Configurations>Debug;Release;CI</Configurations>
</PropertyGroup>

<PropertyGroup>
<PackageId>Workshell.PE.Resources</PackageId>
<PackageDescription>A set of classes that extends the Workshell.PE class library to help deal with the standard resource types within an executable such as bitmaps, icons, cursors etc.</PackageDescription>
<PackageProjectUrl>https://github.com/Workshell/pe</PackageProjectUrl>
<PackageIconUrl>http://img.workshell.co.uk/logo_128.png</PackageIconUrl>
<PackageTags>workshell pe executable native resources</PackageTags>
<PackageLicenseFile>license.txt</PackageLicenseFile>
<Copyright>Workshell Ltd</Copyright>
<Authors>Workshell Ltd</Authors>
<RepositoryUrl>https://github.com/Workshell/pe</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.0.0.1</Version>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\license.txt" Pack="true" PackagePath="$(PackageLicenseFile)" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\bin\debug</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\..\bin\release</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='CI|AnyCPU'">
<OutputPath>..\..\bin\ci</OutputPath>
<DefineConstants>TRACE;SIGNED</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\CommonAssemblyInfo.cs" Link="Properties\CommonAssemblyInfo.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Drawing.Common" Version="4.6.0" />
</ItemGroup>

</PropertyGroup>

<ItemGroup>
<Compile Include="..\CommonAssemblyInfo.cs" Link="Properties\CommonAssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Workshell.PE\Workshell.PE.csproj" />
</ItemGroup>

</Project>
</ItemGroup>

</Project>
10 changes: 5 additions & 5 deletions src/Workshell.PE/Content/CLR/CLRHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal static async Task<CLRHeader> GetAsync(PortableExecutableImage image, Lo
var location = new Location(image, clrLocation.FileOffset, clrLocation.RelativeVirtualAddress, clrLocation.VirtualAddress, size.ToUInt32(), size.ToUInt32(), clrLocation.Section);
var stream = image.GetStream();

stream.Seek(clrLocation.FileOffset.ToInt64(), SeekOrigin.Begin);
stream.Seek(clrLocation.FileOffset, SeekOrigin.Begin);

IMAGE_COR20_HEADER header;

Expand Down Expand Up @@ -207,10 +207,10 @@ public CLRDataDirectory GetManagedNativeHeader()
[FieldAnnotation("Minor Runtime Version", Order = 3)]
public ushort MinorRuntimeVersion { get; }

[FieldAnnotation("MetaData Virtual Address", Order = 4)]
[FieldAnnotation("Meta-data Virtual Address", Order = 4)]
public uint MetaDataAddress { get; }

[FieldAnnotation("MetaData Size", Order = 5)]
[FieldAnnotation("Meta-data Size", Order = 5)]
public uint MetaDataSize { get; }

[FieldAnnotation("Flags", Order = 6)]
Expand All @@ -225,10 +225,10 @@ public CLRDataDirectory GetManagedNativeHeader()
[FieldAnnotation("Resources Size", Order = 9)]
public uint ResourcesSize { get; }

[FieldAnnotation("Strongname Signature Virtual Address", Order = 10)]
[FieldAnnotation("Strong-name Signature Virtual Address", Order = 10)]
public uint StrongNameSignatureAddress { get; }

[FieldAnnotation("Strongname Signature Size", Order = 11)]
[FieldAnnotation("Strong-name Signature Size", Order = 11)]
public uint StrongNameSignatureSize { get; }

[FieldAnnotation("Code Manager Table Virtual Address", Order = 12)]
Expand Down
4 changes: 3 additions & 1 deletion src/Workshell.PE/Content/CLR/CLRMetaDataHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public static async Task<CLRMetaDataHeader> LoadAsync(PortableExecutableImage im
var offset = mdLocation.FileOffset;
var section = mdLocation.Section;

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var signature = await stream.ReadUInt32Async().ConfigureAwait(false);

if (signature != CLR_METADATA_SIGNATURE)
{
throw new PortableExecutableImageException(image, "Incorrect signature found in CLR meta-data header.");
}

var majorVersion = await stream.ReadUInt16Async().ConfigureAwait(false);
var minorVersion = await stream.ReadUInt16Async().ConfigureAwait(false);
Expand Down
8 changes: 5 additions & 3 deletions src/Workshell.PE/Content/CLR/CLRMetaDataStreamTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static async Task<CLRMetaDataStreamTable> LoadAsync(PortableExecutableIma
var rva = calc.OffsetToRVA(offset);
var va = imageBase + rva;
var entries = await LoadTableAsync(image, header, offset, imageBase).ConfigureAwait(false);
ulong size = 0;
var size = 0L;

foreach (var strm in entries)
{
Expand All @@ -75,11 +75,11 @@ public static async Task<CLRMetaDataStreamTable> LoadAsync(PortableExecutableIma
}
}

private static async Task<CLRMetaDataStreamTableEntry[]> LoadTableAsync(PortableExecutableImage image, CLRMetaDataHeader header, ulong baseOffset, ulong imageBase)
private static async Task<CLRMetaDataStreamTableEntry[]> LoadTableAsync(PortableExecutableImage image, CLRMetaDataHeader header, long baseOffset, ulong imageBase)
{
var stream = image.GetStream();

stream.Seek(baseOffset.ToInt64(),SeekOrigin.Begin);
stream.Seek(baseOffset,SeekOrigin.Begin);

var entries = new List<CLRMetaDataStreamTableEntry>();
var offset = baseOffset;
Expand All @@ -104,7 +104,9 @@ private static async Task<CLRMetaDataStreamTableEntry[]> LoadTableAsync(Portable
size += 1;

if (b <= 0)
{
break;
}

streamName.Append((char)b);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Workshell.PE/Content/CLR/CLRMetaDataStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Workshell.PE.Extensions;

namespace Workshell.PE.Content
Expand Down Expand Up @@ -61,10 +62,10 @@ internal static Task<CLRMetaDataStreams> LoadAsync(PortableExecutableImage image
streams[i] = stream;
}

uint rva = 0;
ulong va = 0;
ulong offset = 0;
ulong size = 0;
var rva = 0U;
var va = 0UL;
var offset = 0L;
var size = 0L;

if (streams.Length > 0)
{
Expand All @@ -76,7 +77,9 @@ internal static Task<CLRMetaDataStreams> LoadAsync(PortableExecutableImage image
}

foreach (var stream in streams)
{
size += stream.Location.FileSize;
}

var location = new Location(image, offset, rva, va, size, size);
var result = new CLRMetaDataStreams(image, location, streams);
Expand Down
4 changes: 3 additions & 1 deletion src/Workshell.PE/Content/Certificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ public async Task<byte[]> GetCertificateDataAsync()
var offset = Location.FileOffset + Utils.SizeOf<WIN_CERTIFICATE>().ToUInt32();
var stream = Image.GetStream();

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var buffer = new byte[Length];
var numRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

if (numRead == 0)
{
throw new PortableExecutableImageException(Image, "Could not read certificate data from stream.");
}

return buffer;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Workshell.PE/Content/Debug/DebugDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public static async Task<DebugDirectory> GetAsync(PortableExecutableImage image)

var entrySize = Utils.SizeOf<IMAGE_DEBUG_DIRECTORY>();
var entryCount = dataDirectory.Size / entrySize;
var entries = new Tuple<ulong, IMAGE_DEBUG_DIRECTORY>[entryCount];
var entries = new Tuple<long, IMAGE_DEBUG_DIRECTORY>[entryCount];

for (var i = 0; i < entryCount; i++)
{
var entry = await stream.ReadStructAsync<IMAGE_DEBUG_DIRECTORY>(entrySize).ConfigureAwait(false);

entries[i] = new Tuple<ulong, IMAGE_DEBUG_DIRECTORY>(fileOffset, entry);
entries[i] = new Tuple<long, IMAGE_DEBUG_DIRECTORY>(fileOffset, entry);
}

var directoryEntries = LoadEntries(image, entrySize, entries);
Expand All @@ -98,7 +98,7 @@ public static async Task<DebugDirectory> GetAsync(PortableExecutableImage image)
}
}

private static DebugDirectoryEntry[] LoadEntries(PortableExecutableImage image, int entrySize, Tuple<ulong, IMAGE_DEBUG_DIRECTORY>[] entries)
private static DebugDirectoryEntry[] LoadEntries(PortableExecutableImage image, int entrySize, Tuple<long, IMAGE_DEBUG_DIRECTORY>[] entries)
{
var calc = image.GetCalculator();
var imageBase = image.NTHeaders.OptionalHeader.ImageBase;
Expand Down
3 changes: 2 additions & 1 deletion src/Workshell.PE/Content/Debug/DebugDirectoryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public DebugData GetData()
return null;
}

var calc = _image.GetCalculator();
var rva = AddressOfRawData;
var imageBase = _image.NTHeaders.OptionalHeader.ImageBase;
var location = new Location(_image, PointerToRawData, rva, imageBase + rva, SizeOfData, SizeOfData);
Expand Down Expand Up @@ -175,6 +174,8 @@ public DebugData GetData()
[FieldAnnotation("Pointer to Raw Data", Order = 8)]
public uint PointerToRawData { get; }

public bool IsEmpty => (PointerToRawData == 0 && SizeOfData == 0);

#endregion
}
}
9 changes: 9 additions & 0 deletions src/Workshell.PE/Content/Exceptions/ExceptionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ protected internal ExceptionTable(PortableExecutableImage image, DataDirectory d

#region Static Methods

public static ExceptionTable Get(PortableExecutableImage image)
{
return GetAsync(image).GetAwaiter().GetResult();
}

public static async Task<ExceptionTable> GetAsync(PortableExecutableImage image)
{
if (!image.NTHeaders.DataDirectories.Exists(DataDirectoryType.ExceptionTable))
{
return null;
}

var dataDirectory = image.NTHeaders.DataDirectories[DataDirectoryType.ExceptionTable];

if (DataDirectory.IsNullOrEmpty(dataDirectory))
{
return null;
}

var calc = image.GetCalculator();
var section = calc.RVAToSection(dataDirectory.VirtualAddress);
Expand Down
2 changes: 1 addition & 1 deletion src/Workshell.PE/Content/Exceptions/ExceptionTable64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static async Task<ExceptionTable> GetAsync(PortableExecutableImage imag
var offset = calc.RVAToOffset(dataDirectory.VirtualAddress);
var rva = dataDirectory.VirtualAddress;

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var entrySize = Utils.SizeOf<IMAGE_RUNTIME_FUNCTION_64>();
var entries = new List<ExceptionTableEntry>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task<ExceptionUnwindInfo> GetUnwindInfoAsync()
{
var offset = calc.RVAToOffset(UnwindInfoAddress);

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var versionFlags = await stream.ReadByteAsync().ConfigureAwait(false);
var sizeOfProlog = await stream.ReadByteAsync().ConfigureAwait(false);
Expand Down
Loading

0 comments on commit 8477a41

Please sign in to comment.