Skip to content

Commit 06cfcc9

Browse files
authored
Merge 3804f35 into 36c020c
2 parents 36c020c + 3804f35 commit 06cfcc9

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

Confuser.Renamer/NameService.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Text;
56
using Confuser.Core;
67
using Confuser.Core.Services;
78
using Confuser.Renamer.Analyzers;
9+
using Confuser.Renamer.Properties;
810
using dnlib.DotNet;
911

1012
namespace Confuser.Renamer {
@@ -213,7 +215,9 @@ string MakeGenericName(string name, int? count) {
213215
return string.Format("{0}`{1}", name, count.Value);
214216
}
215217

216-
public string ObfuscateName(string name, RenameMode mode) {
218+
public string ObfuscateName(string name, RenameMode mode) => ObfuscateName(null, name, mode);
219+
220+
public string ObfuscateName(string format, string name, RenameMode mode) {
217221
string newName = null;
218222
name = ParseGenericName(name, out var count);
219223

@@ -240,6 +244,17 @@ public string ObfuscateName(string name, RenameMode mode) {
240244
byte[] hash = Utils.Xor(Utils.SHA1(Encoding.UTF8.GetBytes(name)), nameSeed);
241245
for (int i = 0; i < 100; i++) {
242246
newName = ObfuscateNameInternal(hash, mode);
247+
248+
try {
249+
if (!(format is null))
250+
newName = string.Format(CultureInfo.InvariantCulture, format, newName);
251+
}
252+
catch (FormatException ex) {
253+
throw new ArgumentException(
254+
string.Format(CultureInfo.InvariantCulture, Resources.NameService_ObfuscateName_InvalidFormat, format),
255+
nameof(format), ex);
256+
}
257+
243258
if (!identifiers.Contains(MakeGenericName(newName, count)))
244259
break;
245260
hash = Utils.SHA1(hash);

Confuser.Renamer/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Confuser.Renamer/Properties/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,8 @@
135135
<data name="ReflectionAnalyzer_Analyze_TracingArgumentsFailed" xml:space="preserve">
136136
<value>Tracing arguments for {0} call in {1} failed. What ever member is referenced here by reflection won't be properly handled by the name obfuscation.</value>
137137
</data>
138+
<data name="NameService_ObfuscateName_InvalidFormat" xml:space="preserve">
139+
<value>Invalid format string provided for the namespace format. Got: {0}</value>
140+
<comment>Exception message</comment>
141+
</data>
138142
</root>

Confuser.Renamer/RenamePhase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa
8080
typeDef.Namespace = "";
8181
}
8282
else {
83-
typeDef.Namespace = service.ObfuscateName(typeDef.Namespace, mode);
83+
var nsFormat = parameters.GetParameter(context, def, "nsFormat", "{0}");
84+
typeDef.Namespace = service.ObfuscateName(nsFormat, typeDef.Namespace, mode);
8485
typeDef.Name = service.ObfuscateName(typeDef.Name, mode);
8586
}
8687
foreach (var param in typeDef.GenericParameters)

0 commit comments

Comments
 (0)