Skip to content

Commit 6abd572

Browse files
committed
Preserve generic parameter count in obfuscated name
Fix yck1509#463
1 parent c890466 commit 6abd572

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

Confuser.Renamer/NameService.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,30 @@ string ObfuscateNameInternal(byte[] hash, RenameMode mode) {
182182
}
183183
}
184184

185+
string ParseGenericName(string name, out int? count) {
186+
if (name.LastIndexOf('`') != -1) {
187+
int index = name.LastIndexOf('`');
188+
int c;
189+
if (int.TryParse(name.Substring(index + 1), out c)) {
190+
count = c;
191+
return name.Substring(0, index);
192+
}
193+
}
194+
count = null;
195+
return name;
196+
}
197+
198+
string MakeGenericName(string name, int? count) {
199+
if (count == null)
200+
return name;
201+
else
202+
return string.Format("{0}`{1}", name, count.Value);
203+
}
204+
185205
public string ObfuscateName(string name, RenameMode mode) {
186206
string newName = null;
207+
int? count;
208+
name = ParseGenericName(name, out count);
187209

188210
if (string.IsNullOrEmpty(name))
189211
return string.Empty;
@@ -196,7 +218,7 @@ public string ObfuscateName(string name, RenameMode mode) {
196218
if (reversibleRenamer == null)
197219
throw new ArgumentException("Password not provided for reversible renaming.");
198220
newName = reversibleRenamer.Encrypt(name);
199-
return newName;
221+
return MakeGenericName(newName, count);
200222
}
201223

202224
if (nameMap1.ContainsKey(name))
@@ -205,7 +227,7 @@ public string ObfuscateName(string name, RenameMode mode) {
205227
byte[] hash = Utils.Xor(Utils.SHA1(Encoding.UTF8.GetBytes(name)), nameSeed);
206228
for (int i = 0; i < 100; i++) {
207229
newName = ObfuscateNameInternal(hash, mode);
208-
if (!identifiers.Contains(newName))
230+
if (!identifiers.Contains(MakeGenericName(newName, count)))
209231
break;
210232
hash = Utils.SHA1(hash);
211233
}
@@ -215,7 +237,7 @@ public string ObfuscateName(string name, RenameMode mode) {
215237
nameMap1[name] = newName;
216238
}
217239

218-
return newName;
240+
return MakeGenericName(newName, count);
219241
}
220242

221243
public string RandomName() {

0 commit comments

Comments
 (0)