From 6abd5728597de5bb85a8ab3aebddcfee33bc88e0 Mon Sep 17 00:00:00 2001 From: yck1509 Date: Sun, 27 Mar 2016 16:16:49 +0800 Subject: [PATCH] Preserve generic parameter count in obfuscated name Fix #463 --- Confuser.Renamer/NameService.cs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Confuser.Renamer/NameService.cs b/Confuser.Renamer/NameService.cs index 08660b48b..e35402373 100644 --- a/Confuser.Renamer/NameService.cs +++ b/Confuser.Renamer/NameService.cs @@ -182,8 +182,30 @@ string ObfuscateNameInternal(byte[] hash, RenameMode mode) { } } + string ParseGenericName(string name, out int? count) { + if (name.LastIndexOf('`') != -1) { + int index = name.LastIndexOf('`'); + int c; + if (int.TryParse(name.Substring(index + 1), out c)) { + count = c; + return name.Substring(0, index); + } + } + count = null; + return name; + } + + string MakeGenericName(string name, int? count) { + if (count == null) + return name; + else + return string.Format("{0}`{1}", name, count.Value); + } + public string ObfuscateName(string name, RenameMode mode) { string newName = null; + int? count; + name = ParseGenericName(name, out count); if (string.IsNullOrEmpty(name)) return string.Empty; @@ -196,7 +218,7 @@ public string ObfuscateName(string name, RenameMode mode) { if (reversibleRenamer == null) throw new ArgumentException("Password not provided for reversible renaming."); newName = reversibleRenamer.Encrypt(name); - return newName; + return MakeGenericName(newName, count); } if (nameMap1.ContainsKey(name)) @@ -205,7 +227,7 @@ public string ObfuscateName(string name, RenameMode mode) { byte[] hash = Utils.Xor(Utils.SHA1(Encoding.UTF8.GetBytes(name)), nameSeed); for (int i = 0; i < 100; i++) { newName = ObfuscateNameInternal(hash, mode); - if (!identifiers.Contains(newName)) + if (!identifiers.Contains(MakeGenericName(newName, count))) break; hash = Utils.SHA1(hash); } @@ -215,7 +237,7 @@ public string ObfuscateName(string name, RenameMode mode) { nameMap1[name] = newName; } - return newName; + return MakeGenericName(newName, count); } public string RandomName() {