Skip to content

Commit

Permalink
Made a variety of changes for compatibility reasons which should allo…
Browse files Browse the repository at this point in the history
…w the core PE assembly to be used in .NET 4.5+ and .NET Standard 1.6+ and the PE.Resources in .NET 4.5+ and .NET Standard 2.
  • Loading branch information
lkinsella committed Sep 29, 2018
1 parent 6b583a6 commit 7a2a35e
Show file tree
Hide file tree
Showing 33 changed files with 239 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Src/Workshell.PE/Content/CLR/CLRHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal CLRHeader(Location location, IMAGE_COR20_HEADER header)

internal static async Task<CLRHeader> GetAsync(PortableExecutableImage image, Location clrLocation)
{
var size = Marshal.SizeOf<IMAGE_COR20_HEADER>();
var size = Utils.SizeOf<IMAGE_COR20_HEADER>();
var location = new Location(clrLocation.FileOffset, clrLocation.RelativeVirtualAddress, clrLocation.VirtualAddress, size.ToUInt32(), size.ToUInt32(), clrLocation.Section);
var stream = image.GetStream();

Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/DOSHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<byte[]> GetBytesAsync()

#region Static Properties

public static int Size { get; } = Marshal.SizeOf<IMAGE_DOS_HEADER>();
public static int Size { get; } = Utils.SizeOf<IMAGE_DOS_HEADER>();

#endregion

Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/FileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public CharacteristicsType GetCharacteristics()

#region Static Properties

public static int Size { get; } = Marshal.SizeOf<IMAGE_FILE_HEADER>();
public static int Size { get; } = Utils.SizeOf<IMAGE_FILE_HEADER>();

#endregion

Expand Down
4 changes: 2 additions & 2 deletions Src/Workshell.PE/OptionalHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public DllCharacteristicsType GetDllCharacteristics()

#region Static Properties

public static int Size32 { get; } = Marshal.SizeOf<IMAGE_OPTIONAL_HEADER32>();
public static int Size64 { get; } = Marshal.SizeOf<IMAGE_OPTIONAL_HEADER64>();
public static int Size32 { get; } = Utils.SizeOf<IMAGE_OPTIONAL_HEADER32>();
public static int Size64 { get; } = Utils.SizeOf<IMAGE_OPTIONAL_HEADER64>();

#endregion

Expand Down
6 changes: 3 additions & 3 deletions Src/Workshell.PE/SectionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public enum SectionCharacteristicsType : uint

public sealed class SectionTableEntry : IEquatable<SectionTableEntry>, ISupportsLocation, ISupportsBytes
{
private static readonly uint _headerSize = Marshal.SizeOf<IMAGE_SECTION_HEADER>().ToUInt32();
private static readonly uint _headerSize = Utils.SizeOf<IMAGE_SECTION_HEADER>().ToUInt32();

private readonly PortableExecutableImage _image;
private readonly IMAGE_SECTION_HEADER _header;
Expand Down Expand Up @@ -280,7 +280,7 @@ internal SectionTable(PortableExecutableImage image, IMAGE_SECTION_HEADER[] sect
_image = image;
_table = new SectionTableEntry[sectionHeaders.Length];

var size = (Marshal.SizeOf<IMAGE_SECTION_HEADER>() * sectionHeaders.Length).ToUInt32();
var size = (Utils.SizeOf<IMAGE_SECTION_HEADER>() * sectionHeaders.Length).ToUInt32();

Location = new Location(image.GetCalculator(), tableOffset, tableOffset.ToUInt32(), imageBase + tableOffset, size, size);

Expand All @@ -291,7 +291,7 @@ internal SectionTable(PortableExecutableImage image, IMAGE_SECTION_HEADER[] sect
var entry = new SectionTableEntry(_image,this,sectionHeaders[i],offset,imageBase);

_table[i] = entry;
offset += Marshal.SizeOf<IMAGE_SECTION_HEADER>().ToUInt32();
offset += Utils.SizeOf<IMAGE_SECTION_HEADER>().ToUInt32();
}
}

Expand Down
9 changes: 8 additions & 1 deletion Src/Workshell.PE/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ internal static class Utils

#region Methods

public static int SizeOf<T>() where T : struct
{
var result = Marshal.SizeOf(typeof(T));

return result;
}

public static T Read<T>(byte[] bytes) where T : struct
{
var ptr = Marshal.AllocHGlobal(bytes.Length);
Expand Down Expand Up @@ -169,7 +176,7 @@ public static void Write<T>(T structure, byte[] buffer, int startIndex, int coun

public static void Write<T>(T structure, Stream stream) where T : struct
{
var size = Marshal.SizeOf<T>();
var size = Utils.SizeOf<T>();

Write<T>(structure,stream,size);
}
Expand Down
12 changes: 10 additions & 2 deletions Src/Workshell.PE/Workshell.PE.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard1.6;net45</TargetFrameworks>

<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
Expand All @@ -11,24 +11,32 @@
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Workshell.PE.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

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

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

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

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
<PackageReference Include="System.Reflection" Version="4.3.0" />
<PackageReference Include="System.Reflection.Extensions" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="System.Net.Http" Version="4.3.3" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions dotNET PE.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workshell.PE.Testbed", "src
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workshell.PE.Resources", "src\Workshell.PE.Resources\Workshell.PE.Resources.csproj", "{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{85C1A49C-0316-4C2D-B2E3-17B1F8200EE7}"
ProjectSection(SolutionItems) = preProject
src\CommonAssemblyInfo.cs = src\CommonAssemblyInfo.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
22 changes: 22 additions & 0 deletions src/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
#region License
// Copyright(c) Workshell Ltd
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System.Reflection;

[assembly: AssemblyConfiguration("")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task<AcceleratorsTable> GetAsync(ResourceLanguage language)

using (var mem = new MemoryStream(data))
{
var size = Marshal.SizeOf<ACCELTABLEENTRY>();
var size = Utils.SizeOf<ACCELTABLEENTRY>();
var count = mem.Length / size;
var accelerators = new AcceleratorEntry[count];

Expand Down
4 changes: 2 additions & 2 deletions src/Workshell.PE.Resources/Graphics/BitmapResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace Workshell.PE.Resources.Graphics
{
public sealed class BitmapResource : Resource
{
private static readonly int BitmapInfoHeaderSize = Marshal.SizeOf<BITMAPINFOHEADER>();
private static readonly int BitmapFileHeaderSize = Marshal.SizeOf<BITMAPFILEHEADER>();
private static readonly int BitmapInfoHeaderSize = Utils.SizeOf<BITMAPINFOHEADER>();
private static readonly int BitmapFileHeaderSize = Utils.SizeOf<BITMAPFILEHEADER>();

public BitmapResource(PortableExecutableImage image, ResourceType type, ResourceDirectoryEntry entry, ResourceId id) : base(image, type, entry, id)
{
Expand Down
39 changes: 39 additions & 0 deletions src/Workshell.PE.Resources/Graphics/CursorData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region License
// Copyright(c) Workshell Ltd
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Workshell.PE.Resources.Graphics
{
internal struct CursorData
{
public ushort HotspotX;
public ushort HotspotY;
public ushort Width;
public ushort Height;
public byte ColorCount;
public byte[] DIB;
public bool IsPNG;
}
}
14 changes: 7 additions & 7 deletions src/Workshell.PE.Resources/Graphics/CursorGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public async Task SaveAsync(Stream stream, ResourceLanguage language, CursorGrou
else
{
var group = await _resource.GetGroupAsync(language).ConfigureAwait(false);
var cursorsInfo = new List<(ushort Id, ushort HotspotX, ushort HotspotY, byte[] DIB)>(group.Count);
var cursorsInfo = new List<Tuple<ushort, ushort, ushort, byte[]>>(group.Count);

for (var i = 0; i < group.Count; i++)
{
Expand All @@ -148,7 +148,7 @@ public async Task SaveAsync(Stream stream, ResourceLanguage language, CursorGrou
}
}

cursorsInfo.Add((entry.CursorId, hotspotX, hotspotY, dib));
cursorsInfo.Add(new Tuple<ushort, ushort, ushort, byte[]>(entry.CursorId, hotspotX, hotspotY, dib));
}

var offsets = new uint[group.Count];
Expand All @@ -159,7 +159,7 @@ public async Task SaveAsync(Stream stream, ResourceLanguage language, CursorGrou
var info = cursorsInfo[i];

offsets[i] = offset;
offset += info.DIB.Length.ToUInt32();
offset += info.Item4.Length.ToUInt32();
}

await stream.WriteUInt16Async(0).ConfigureAwait(false);
Expand All @@ -175,17 +175,17 @@ public async Task SaveAsync(Stream stream, ResourceLanguage language, CursorGrou
await stream.WriteByteAsync(Convert.ToByte(entry.Height)).ConfigureAwait(false);
await stream.WriteByteAsync(Convert.ToByte(entry.BitCount)).ConfigureAwait(false);
await stream.WriteByteAsync(0).ConfigureAwait(false);
await stream.WriteUInt16Async(info.HotspotX).ConfigureAwait(false);
await stream.WriteUInt16Async(info.HotspotY).ConfigureAwait(false);
await stream.WriteInt32Async(info.DIB.Length).ConfigureAwait(false);
await stream.WriteUInt16Async(info.Item2).ConfigureAwait(false);
await stream.WriteUInt16Async(info.Item3).ConfigureAwait(false);
await stream.WriteInt32Async(info.Item4.Length).ConfigureAwait(false);
await stream.WriteUInt32Async(offsets[i]).ConfigureAwait(false);
}

for (var i = 0; i < group.Count; i++)
{
var info = cursorsInfo[i];

await stream.WriteBytesAsync(info.DIB).ConfigureAwait(false);
await stream.WriteBytesAsync(info.Item4).ConfigureAwait(false);
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/Workshell.PE.Resources/Graphics/CursorResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;

using Workshell.PE.Extensions;
using Workshell.PE.Resources.Native;

Expand Down Expand Up @@ -230,7 +231,7 @@ public async Task<Bitmap> GetBitmapAsync(Color backgroundColor, ResourceLanguage
return result;
}

private async Task<(ushort HotspotX, ushort HotspotY, ushort Width, ushort Height, byte ColorCount, byte[] DIB, bool IsPNG)> GetCursorDataAsync(ResourceLanguage language)
private async Task<CursorData> GetCursorDataAsync(ResourceLanguage language)
{
var buffer = await GetBytesAsync(language).ConfigureAwait(false);
byte[] dib;
Expand Down Expand Up @@ -274,7 +275,18 @@ public async Task<Bitmap> GetBitmapAsync(Color backgroundColor, ResourceLanguage
}
}

return (hotspotX, hotspotY, width, height, colorCount, dib, isPNG);
var result = new CursorData()
{
HotspotX = hotspotX,
HotspotY = hotspotY,
Width = width,
Height = height,
ColorCount = colorCount,
DIB = dib,
IsPNG = isPNG
};

return result;
}

#endregion
Expand Down
37 changes: 37 additions & 0 deletions src/Workshell.PE.Resources/Graphics/IconData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region License
// Copyright(c) Workshell Ltd
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Workshell.PE.Resources.Graphics
{
internal struct IconData
{
public ushort Width;
public ushort Height;
public byte ColorCount;
public byte[] DIB;
public bool IsPNG;
}
}
13 changes: 11 additions & 2 deletions src/Workshell.PE.Resources/Graphics/IconResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public async Task<Bitmap> GetBitmapAsync(Color backgroundColor, ResourceLanguage
return result;
}

private async Task<(ushort Width, ushort Height, byte ColorCount, byte[] DIB, bool IsPNG)> GetIconDataAsync(ResourceLanguage language)
private async Task<IconData> GetIconDataAsync(ResourceLanguage language)
{
var buffer = await GetBytesAsync(language).ConfigureAwait(false);

Expand Down Expand Up @@ -275,7 +275,16 @@ public async Task<Bitmap> GetBitmapAsync(Color backgroundColor, ResourceLanguage
}
}

return (width, height, colorCount, dib, isPNG);
var result = new IconData()
{
Width = width,
Height = height,
ColorCount = colorCount,
DIB = dib,
IsPNG = isPNG
};

return result;
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Workshell.PE.Resources/Native/BITMAPINFOHEADER.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct BITMAPINFOHEADER

public void Initialize()
{
biSize = (uint)Marshal.SizeOf(this);
biSize = (uint)Utils.SizeOf<BITMAPINFOHEADER>();
}
}
}
Loading

0 comments on commit 7a2a35e

Please sign in to comment.