Skip to content

Commit

Permalink
Implemented "Single + Double Inf/-Inf token patching" option.
Browse files Browse the repository at this point in the history
Related Issue #128

Also updated layout: GdiCharSet = 0 + background caption to filter projects by path
  • Loading branch information
3F committed Dec 15, 2019
1 parent 1bbae8a commit c6598f6
Show file tree
Hide file tree
Showing 24 changed files with 445 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ public string MetaLib
}
}

public PatchesType Patches
{
get => _ExportTaskImplementation.Patches;
set => _ExportTaskImplementation.Patches = value;
}

public long PatchesRaw
{
set => Patches = (PatchesType)value;
}

public PeCheckType PeCheck
{
get => _ExportTaskImplementation.PeCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ public string MetaLib
}
}

public PatchesType Patches
{
get => _ExportTaskImplementation.Patches;
set => _ExportTaskImplementation.Patches = value;
}

public long PatchesRaw
{
set => Patches = (PatchesType)value;
}

public PeCheckType PeCheck
{
get => _ExportTaskImplementation.PeCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ public string MetaLib
}
}

public PatchesType Patches
{
get => _Values.Patches;
set => _Values.Patches = value;
}

public PeCheckType PeCheck
{
get => _Values.PeCheck;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@

//# Author of original code ([Decompiled] MIT-License): Copyright (c) 2009-2015 Robert Giesecke
//# Use Readme & LICENSE files for details.

//# Modifications: Copyright (c) 2016-2019 Denis Kuzmin < entry.reg@gmail.com > GitHub/3F
//$ Distributed under the MIT License (MIT)

namespace RGiesecke.DllExport.MSBuild
{
public interface IInputRawValues: IInputValues
{
long PatchesRaw { set; }

int PeCheckRaw { set; }
}
}
6 changes: 6 additions & 0 deletions RGiesecke.DllExport/IInputValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ string MetaLib
set;
}

PatchesType Patches
{
get;
set;
}

PeCheckType PeCheck
{
get;
Expand Down
6 changes: 6 additions & 0 deletions RGiesecke.DllExport/InputValuesCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public string MetaLib
set;
}

public PatchesType Patches
{
get;
set;
}

public PeCheckType PeCheck
{
get;
Expand Down
73 changes: 73 additions & 0 deletions RGiesecke.DllExport/Parsing/Actions/ClassParserAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//$ Distributed under the MIT License (MIT)

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace RGiesecke.DllExport.Parsing.Actions
{
Expand All @@ -31,6 +33,15 @@ public override void Execute(ParserStateValues state, string trimmedLine)
state.AddLine = false;
state.State = ParserState.MethodDeclaration;
}
else if(trimmedLine.StartsWith(".field", StringComparison.Ordinal))
{
if(TreatField(state, ref trimmedLine))
{
state.AddLine = false;
state.Result.Add(trimmedLine);
}
return;
}
else
{
if(!trimmedLine.StartsWith("} // end of class", StringComparison.Ordinal))
Expand All @@ -41,5 +52,67 @@ public override void Execute(ParserStateValues state, string trimmedLine)
state.State = state.ClassNames.Count > 0 ? ParserState.Class : ParserState.Normal;
}
}

/// <param name="state"></param>
/// <param name="raw">raw definition of the .field</param>
/// <returns>true if processed</returns>
private bool TreatField(ParserStateValues state, ref string raw)
{
if((Parser.InputValues.Patches & PatchesType.InfToken) == PatchesType.InfToken)
{
// .field public static literal float32 'Infinity' = float32(inf)
// .field public static literal float32 'NegativeInfinity' = float32(-inf)
// .field public static literal float64 'Infinity' = float64(inf)
// .field public static literal float64 'NegativeInfinity' = float64(-inf)

Match m = Regex.Match
(
raw,
@"=\s*
float(?:(?'x64'64)|32)
\(
(?'sign'-?)
inf
\)
",
RegexOptions.IgnorePatternWhitespace
);

if(m.Success)
{
raw = new string(' ', 2) + raw.Substring(0, m.Index) + GetFloatDef(m);
return true;
}
}

return false;
}

private static string GetFloatDef(Match fld)
{
var sb = new StringBuilder(4);
sb.Append("= float");

if(fld.Groups["x64"].Success)
{
sb.Append("64");
sb.Append
(
fld.Groups["sign"].Value.Length > 0 ?
"(0xFFF0000000000000)" : "(0x7FF0000000000000)"
);

return sb.ToString();
}

sb.Append("32");
sb.Append
(
fld.Groups["sign"].Value.Length > 0 ?
"(0xFF800000)" : "(0x7F800000)"
);

return sb.ToString();
}
}
}
70 changes: 70 additions & 0 deletions RGiesecke.DllExport/Parsing/Actions/MethodParserAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//$ Distributed under the MIT License (MIT)

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace RGiesecke.DllExport.Parsing.Actions
{
Expand All @@ -22,6 +24,74 @@ public override void Execute(ParserStateValues state, string trimmedLine)
state.State = ParserState.Class;
return;
}

if(trimmedLine.StartsWith("IL_", StringComparison.Ordinal))
{
if(TreatIL(state, ref trimmedLine))
{
state.AddLine = false;
state.Result.Add(trimmedLine);
}
return;
}
}

private bool TreatIL(ParserStateValues state, ref string raw)
{
if((Parser.InputValues.Patches & PatchesType.InfToken) == PatchesType.InfToken)
{
// ldc.r8 inf
// ldc.r8 -inf
// ldc.r4 inf
// ldc.r4 -inf

Match m = Regex.Match
(
raw,
@"
ldc.r(?:(?'x64'8)|4)
\s*
(?'sign'-?)
inf
",
RegexOptions.IgnorePatternWhitespace
);

if(m.Success)
{
raw = new string(' ', 4) + raw.Substring(0, m.Index) + GetFloatDef(m);
return true;
}
}

return false;
}

private static string GetFloatDef(Match fld)
{
var sb = new StringBuilder(4);
sb.Append("ldc.r");

if(fld.Groups["x64"].Success)
{
sb.Append("8 ");
sb.Append
(
fld.Groups["sign"].Value.Length > 0 ?
"(00 00 00 00 00 00 F0 FF)" : "(00 00 00 00 00 00 F0 7F)"
);

return sb.ToString();
}

sb.Append("4 ");
sb.Append
(
fld.Groups["sign"].Value.Length > 0 ?
"(00 00 80 FF)" : "(00 00 80 7F)"
);

return sb.ToString();
}
}
}
24 changes: 24 additions & 0 deletions RGiesecke.DllExport/PatchesType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

//# Author of original code ([Decompiled] MIT-License): Copyright (c) 2009-2015 Robert Giesecke
//# Use Readme & LICENSE files for details.

//# Modifications: Copyright (c) 2016-2019 Denis Kuzmin < entry.reg@gmail.com > GitHub/3F
//$ Distributed under the MIT License (MIT)

namespace RGiesecke.DllExport
{
public enum PatchesType: long
{
None,

/// <summary>
/// Affects ldc.r8; ldc.r4; .field;
///
/// inf/-inf to 0x7F800000/0xFF800000
/// 0x7FF0000000000000/0xFFF0000000000000
///
/// https://github.com/3F/DllExport/issues/128
/// </summary>
InfToken = 0x01,
}
}
7 changes: 6 additions & 1 deletion RGiesecke.DllExport/PeCheckType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

//# Author of original code ([Decompiled] MIT-License): Copyright (c) 2009-2015 Robert Giesecke
//# Use Readme & LICENSE files for details.

//# Modifications: Copyright (c) 2016-2019 Denis Kuzmin < entry.reg@gmail.com > GitHub/3F
//$ Distributed under the MIT License (MIT)

namespace RGiesecke.DllExport
{
public enum PeCheckType: int
Expand Down
1 change: 1 addition & 0 deletions RGiesecke.DllExport/RGiesecke.DllExport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DisabledAssemblyResolver.cs" />
<Compile Include="PatchesType.cs" />
<Compile Include="PeCheckType.cs" />
<Compile Include="DllExportLogginCodes.cs" />
<Compile Include="DllExportServiceProviderExtensions.cs" />
Expand Down
5 changes: 5 additions & 0 deletions Wizard/CompilerCfg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ public struct CompilerCfg
/// Type of checking PE32/PE32+ module.
/// </summary>
public PeCheckType peCheck;

/// <summary>
/// Optional patches.
/// </summary>
public PatchesType patches;
}
}
14 changes: 14 additions & 0 deletions Wizard/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ public static int ToInteger(this string value)
return Int32.Parse(value);
}

/// <summary>
/// To get long integer value from string.
/// </summary>
/// <param name="value">Any compatible value.</param>
/// <returns></returns>
public static long ToLongInteger(this string value)
{
if(String.IsNullOrWhiteSpace(value)) {
return 0;
}

return Int64.Parse(value);
}

/// <summary>
/// Open url through default application.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Wizard/MSBuildProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public struct MSBuildProperties
/// </summary>
public const string DXP_PE_CHECK = "DllExportPeCheck";

/// <summary>
/// Optional patches.
/// </summary>
public const string DXP_PATCHES = "DllExportPatches";

/// <summary>
/// Platform Target for binaries.
/// </summary>
Expand Down
Loading

0 comments on commit c6598f6

Please sign in to comment.