Skip to content

Commit

Permalink
added Cecil variant for ddNS features /#14, #2
Browse files Browse the repository at this point in the history
  • Loading branch information
3F committed Oct 15, 2016
1 parent ca2d68b commit 38c5388
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 77 deletions.
23 changes: 17 additions & 6 deletions Configurator/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void reset()
removeMsbuildProperties(
"DllExportNamespace",
"DllExportOrdinalsBase",
"DllExportSkipOnAnyCpu"
"DllExportSkipOnAnyCpu",
"DllExportDDNSCecil"
);
}

Expand All @@ -91,8 +92,10 @@ protected void cfgNamespace()
config.MetaLibPublicKeyToken
);

setProperty("DllExportDDNSCecil", config.useCecil.ToString());

// binary modifications of metalib assembly
ddns.setNamespace(config.script.MetaLib, config.unamespace);
ddns.setNamespace(config.script.MetaLib, config.unamespace, config.useCecil);
}

protected void cfgPlatform()
Expand Down Expand Up @@ -125,16 +128,14 @@ protected void cfgPlatform()
}
}

project.setProperty("PlatformTarget", platform);
project.saveViaDTE();
setProperty("PlatformTarget", platform);

Log.send(this, $"The Export configured for platform: {platformS}");
}

protected void cfgCompiler()
{
project.setProperty("DllExportOrdinalsBase", config.compiler.ordinalsBase.ToString());
project.saveViaDTE();
setProperty("DllExportOrdinalsBase", config.compiler.ordinalsBase.ToString());

Log.send(this, $"The Base for ordinals: {config.compiler.ordinalsBase}");
}
Expand All @@ -146,5 +147,15 @@ protected void removeMsbuildProperties(params string[] names)
}
project.saveViaDTE();
}

private void setProperty(string name, string val)
{
if(String.IsNullOrWhiteSpace(name)) {
return;
}

project.setProperty(name, val);
project.saveViaDTE();
}
}
}
28 changes: 28 additions & 0 deletions Configurator/GUI/InstallationForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Configurator/GUI/InstallationForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private void saveConfig()
{
config.unamespace = getValidNS(comboNS.Text);
config.compiler.ordinalsBase = (int)numOrdinal.Value;
config.useCecil = rbCecil.Checked;

if(rbPlatformX86.Checked) {
config.platform = Platform.x86;
Expand Down
5 changes: 5 additions & 0 deletions Configurator/UserConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public sealed class UserConfig
/// </summary>
public int nsBuffer;

/// <summary>
/// To use Cecil instead of direct modifications.
/// </summary>
public bool useCecil;

/// <summary>
/// Predefined list of namespaces.
/// </summary>
Expand Down
15 changes: 1 addition & 14 deletions Metadata/DllExportAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

/*
* Via Cecil or direct modification:
https://github.com/3F/DllExport/issues/2#issuecomment-231593744
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Expand All @@ -33,29 +34,15 @@ 000005D0 20 02 00 00 00 00 00 00 00 44 33 46 30 30 46 46 ........D3F00FF
000005E0 31 37 37 30 44 45 44 39 37 38 45 43 37 37 34 42 1770DED978EC774B
000005F0 41 33 38 39 46 32 44 43 39 2E 42 30 30 30 30 30 A389F2DC9.B00000
00000600 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000
...
000007A0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000
000007B0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000
000007C0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000
000007D0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000
000007E0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00 3C 00000000000000.<
->
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000005B0 00 C4 7B 01 00 00 00 2F 00 12 05 .Д{..../...
000005C0 00 00 02 00 00 00 00 00 00 00 00 00 00 00 26 00 ..............&.
000005D0 20 02 00 00 00 00 00 00 00 49 2E 77 61 6E 74 2E ........I.want.
000005E0 74 6F 2E 66 6C 79 00 00 00 00 00 00 00 00 00 00 to.fly..........
000005F0 00 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 ................
00000600 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
...
000007A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3C ...............<
*/

using System;
Expand Down
5 changes: 3 additions & 2 deletions NSBin/DDNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public static bool IsValidNS(string name)
/// </summary>
/// <param name="lib">Full path to prepared library.</param>
/// <param name="name">New namespace.</param>
public void setNamespace(string lib, string name)
/// <param name="useCecil">Use Cecil instead of direct modification.</param>
public void setNamespace(string lib, string name, bool useCecil)
{
using(var def = new Rmod(lib, encoding)) {
def.setNamespace(name);
def.setNamespace(name, useCecil);
}
}

Expand Down
3 changes: 2 additions & 1 deletion NSBin/IDDNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public interface IDDNS
/// </summary>
/// <param name="lib">Full path to prepared library.</param>
/// <param name="name">New namespace.</param>
void setNamespace(string lib, string name);
/// <param name="useCecil">To use Cecil instead of direct modification.</param>
void setNamespace(string lib, string name, bool useCecil);
}
}
2 changes: 1 addition & 1 deletion NSBin/Marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace net.r_eg.DllExport.NSBin
{
public class Marker: BinaryData
{
public const int FORMAT_V = 1;
public const int FORMAT_V = 2;

public void write(MarkerData data)
{
Expand Down
5 changes: 5 additions & 0 deletions NSBin/MarkerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ public struct MarkerData
/// Valid buffer size for namespace.
/// </summary>
public int nsBuffer;

/// <summary>
/// Cecil logic.
/// </summary>
public bool viaCecil;
}
}
4 changes: 4 additions & 0 deletions NSBin/NSBin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
67 changes: 47 additions & 20 deletions NSBin/Rmod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* THE SOFTWARE.
*/

// Modification of binary assemblies. Format and specification:
// Via Cecil or direct modification:
//
// https://github.com/3F/DllExport/issues/2#issuecomment-231593744
//
Expand All @@ -48,6 +48,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Mono.Cecil;
using net.r_eg.Conari.Log;

namespace net.r_eg.DllExport.NSBin
Expand Down Expand Up @@ -76,9 +77,9 @@ public static bool IsValidNS(string name)
&& !Regex.IsMatch(name, @"(?:\.(\s*\.)+|\.\s*$)"); // left. ... .right.
}

public void setNamespace(string name)
public void setNamespace(string name, bool viaCecil = true)
{
Log.send(this, $"set new namespace: ({name}) - ({dll})");
Log.send(this, $"set new namespace(Cecil: {viaCecil}): ({name}) - ({dll})");

if(String.IsNullOrWhiteSpace(dll) || !File.Exists(dll)) {
throw new FileNotFoundException($"The '{name}' assembly for modifications was not found.");
Expand All @@ -88,7 +89,13 @@ public void setNamespace(string name)
// throw new ArgumentException("The namespace cannot be null or empty.");
//}

make(nsRule(name));
var ns = nsRule(name);

if(viaCecil) {
makeViaCecil(ns);
return;
}
make(ns);
}

/// <param name="dll">The DllExport assembly for modifications.</param>
Expand All @@ -99,10 +106,37 @@ public Rmod(string dll, Encoding enc)
ripper = new Ripper(dll, enc);
}

protected void make(string ns)
protected void makeViaCecil(string ns)
{
//prepareLib(dll);
AssemblyDefinition asmdef = AssemblyDefinition.ReadAssembly(
ripper.BaseStream,
new ReaderParameters(ReadingMode.Immediate)
);

foreach(TypeDefinition t in asmdef.MainModule.Types)
{
if(t.Namespace.StartsWith(IDNS, StringComparison.InvariantCulture)) {
t.Namespace = ns;
Log.send(this, $"cecil: NS property has been updated for {t.Name}.");
}
}

ripper.BaseStream.SetLength(0);
asmdef.Write(ripper.BaseStream);

using(var m = new Marker(_postfixToUpdated(dll)))
{
m.write(new MarkerData() {
viaCecil = true,
nsName = ns
});
}

msgSuccess(ns);
}

protected void make(string ns)
{
var ident = ripper.getBytesFrom(IDNS);

byte[] data = ripper.readFirst64K();
Expand Down Expand Up @@ -149,9 +183,7 @@ protected void binmod(int lpos, int ident, string ns)
m.write(new MarkerData() { nsPosition = lpos, nsBuffer = buffer, nsName = ns });
}

Log.send(this, "\nThe DllExport Library has been modified !\n");
Log.send(this, $"namespace: '{ns}' :: {dll}");
Log.send(this, "Details here: https://github.com/3F/DllExport/issues/2");
msgSuccess(ns);
}
}

Expand Down Expand Up @@ -181,17 +213,12 @@ protected virtual string nsRule(string name)
return DEFAULT_NS;
}

//protected void prepareLib(string dll)
//{
// string origin = _postfixToOrigin(dll);

// if(!File.Exists(origin)) {
// File.Copy(dll, origin);
// }
// else {
// File.Copy(origin, dll, true);
// }
//}
private void msgSuccess(string ns)
{
Log.send(this, "\nThe DllExport Library has been modified !\n");
Log.send(this, $"namespace: '{ns}' :: {dll}");
Log.send(this, "Details here: https://github.com/3F/DllExport/issues/2");
}

private string _postfixToUpdated(string dll)
{
Expand Down
11 changes: 10 additions & 1 deletion NSBin/SetDDNSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public string Namespace
set;
}

/// <summary>
/// To use Cecil instead of direct modification
/// </summary>
public bool UseCecil
{
get;
set;
} = true;

protected override void ProcessRecord()
{
IDDNS ddns = new DDNS(Encoding.UTF8);
Expand All @@ -63,7 +72,7 @@ protected override void ProcessRecord()
ddns.Log.Received += onMsg;

try {
ddns.setNamespace(Dll, Namespace);
ddns.setNamespace(Dll, Namespace, UseCecil);
}
catch(Exception ex) {
LSender.Send(this, $"ERROR-NSBin: {ex.Message}");
Expand Down
1 change: 1 addition & 0 deletions NSBin/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.PowerShell.5.ReferenceAssemblies" version="1.0.0" targetFramework="net40" />
<package id="Mono.Cecil" version="0.9.6.4" targetFramework="net40" />
</packages>
Loading

0 comments on commit 38c5388

Please sign in to comment.