Skip to content

Commit

Permalink
Trim namespaces if shortNames parameter is activated, extend Method…
Browse files Browse the repository at this point in the history
…Overloading test (add postProcessAction action)
  • Loading branch information
KvanTTT committed Jan 19, 2021
1 parent 0dd687b commit 4a5ab07
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Confuser.Renamer/MessageDeobfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public string Deobfuscate(string obfuscatedMessage) {
string DecodeSymbolMap(Match match) {
var symbol = match.Value;
if (_symbolMap.TryGetValue(symbol, out string result))
return NameService.ExtractShortName(result);
return NameService.ExtractShortName(symbol);
return NameService.ExtractShortName(result, false);
return NameService.ExtractShortName(symbol, false);
}

string DecodeSymbolPassword(Match match) {
var sym = match.Value;
try {
return NameService.ExtractShortName(_renamer.Decrypt(sym));
return NameService.ExtractShortName(_renamer.Decrypt(sym), false);
}
catch {
return sym;
Expand Down
9 changes: 7 additions & 2 deletions Confuser.Renamer/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ string GetSimplifiedFullName(IDnlibDef dnlibDef) {
}

if (GetParam(dnlibDef, "shortNames")?.Equals("true", StringComparison.OrdinalIgnoreCase) == true) {
result = ExtractShortName(result);
result = ExtractShortName(result, true);
}

return result;
}

public static string ExtractShortName(string fullName) {
public static string ExtractShortName(string fullName, bool trimNamespace) {
const string doubleParen = "::";
int doubleParenIndex = fullName.IndexOf(doubleParen);
if (doubleParenIndex != -1) {
Expand All @@ -381,6 +381,11 @@ public static string ExtractShortName(string fullName) {
return fullName.Substring(slashIndex + 1);
}

if (trimNamespace) {
int dotIndex = fullName.IndexOf('.');
return dotIndex != -1 ? fullName.Substring(dotIndex + 1) : fullName;
}

return fullName;
}
}
Expand Down
26 changes: 25 additions & 1 deletion Tests/MethodOverloading.Test/MethodOverloadingTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Confuser.Core;
using Confuser.Core.Project;
Expand Down Expand Up @@ -31,7 +33,29 @@ await Run(
"class5"
},
new SettingItem<Protection>("rename") { ["mode"] = "decodable", ["shortNames"] = shortNames.ToString().ToLowerInvariant() },
shortNames ? "_shortnames" : "_fullnames"
shortNames ? "_shortnames" : "_fullnames",
seed: "seed",
postProcessAction: outputPath => {
var symbolsPath = Path.Combine(outputPath, "symbols.map");
var symbols = File.ReadAllLines(symbolsPath).Select(line => {
var parts = line.Split('\t');
return new KeyValuePair<string, string>(parts[0], parts[1]);
}).ToDictionary(keyValue => keyValue.Key, keyValue => keyValue.Value);
if (shortNames) {
Assert.Equal("Class", symbols["_OatkF4GhWlgOakbgdlaLpqEglhm"]);
Assert.Equal("NestedClass", symbols["_GYHfKMUMLLO9oVLM117IvfCdmUC"]);
Assert.Equal("OverloadedMethod", symbols["_phF8iy7Y79cwt3EaAFmJzW2bGch"]);
}
else {
Assert.Equal("MethodOverloading.Class", symbols["_iyWU2GdYVZxajP8BQlt8KKTy6qQ"]);
Assert.Equal("MethodOverloading.Program/NestedClass", symbols["_CZIbNVHU7wPJyGhgOcTnIUsFtC0"]);
Assert.Equal("MethodOverloading.Program::OverloadedMethod(System.Object[])", symbols["_LzCBuBOSn49xbtKNsjuJxQZPIEW"]);
Assert.Equal("MethodOverloading.Program::OverloadedMethod(System.String)", symbols["_ywSbkiShk8k3qj7bBrEWEUfs9Km"]);
}
return Task.Delay(0);
}
);

public static IEnumerable<object[]> MethodOverloadingData => new[] { new object[] {false}, new object[] {true}};
Expand Down
6 changes: 3 additions & 3 deletions Tests/MethodOverloading/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Class5 : BaseClass5<string>, Interface5 {
}

public class Program {
public class Test {
public class NestedClass {
public override string ToString() => "test";
}

Expand All @@ -55,15 +55,15 @@ public class Test {

public static object OverloadedMethod(bool cond, float param1, double param2) => cond ? param1 : param2;

public static Test OverloadedMethod(Test test) => test;
public static NestedClass OverloadedMethod(NestedClass nestedClass) => nestedClass;

static int Main(string[] args) {
Console.WriteLine("START");
Console.WriteLine(OverloadedMethod(1));
Console.WriteLine(OverloadedMethod("Hello world"));
Console.WriteLine(OverloadedMethod(new object[] { "object" }));
Console.WriteLine(OverloadedMethod(false, 1.0f, 2.0));
Console.WriteLine(OverloadedMethod(new Test()));
Console.WriteLine(OverloadedMethod(new NestedClass()));
Console.WriteLine(new Class().Method("class"));
Console.WriteLine(new Class2().Method2("class2"));
Console.WriteLine(new Class3().Method3("class3"));
Expand Down

0 comments on commit 4a5ab07

Please sign in to comment.