Skip to content

Commit a38ece3

Browse files
authored
Merge c8f7371 into 0f72683
2 parents 0f72683 + c8f7371 commit a38ece3

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

Confuser.Renamer/NameService.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ internal class NameService : INameService {
6262

6363
readonly HashSet<string> identifiers = new HashSet<string>();
6464

65-
readonly byte[] nameId = new byte[8];
65+
long _nameId;
66+
readonly StringBuilder _nameBuilder = new StringBuilder();
6667
readonly Dictionary<string, string> _originalToObfuscatedNameMap = new Dictionary<string, string>();
6768
readonly Dictionary<string, string> _obfuscatedToOriginalNameMap = new Dictionary<string, string>();
6869
readonly Dictionary<string, string> _prefixesMap = new Dictionary<string, string>();
@@ -166,14 +167,6 @@ public void Analyze(IDnlibDef def) {
166167
analyze.Analyze(this, context, ProtectionParameters.Empty, def, true);
167168
}
168169

169-
void IncrementNameId() {
170-
for (int i = nameId.Length - 1; i >= 0; i--) {
171-
nameId[i]++;
172-
if (nameId[i] != 0)
173-
break;
174-
}
175-
}
176-
177170
string ObfuscateNameInternal(byte[] hash, RenameMode mode) {
178171
switch (mode) {
179172
case RenameMode.Empty:
@@ -187,11 +180,9 @@ string ObfuscateNameInternal(byte[] hash, RenameMode mode) {
187180
case RenameMode.Reflection:
188181
return Utils.EncodeString(hash, reflectionCharset);
189182
case RenameMode.Decodable:
190-
IncrementNameId();
191183
return "_" + Utils.EncodeString(hash, alphaNumCharset);
192184
case RenameMode.Sequential:
193-
IncrementNameId();
194-
return "_" + Utils.EncodeString(nameId, alphaNumCharset);
185+
return "_" + GetNextSequentialName();
195186
default:
196187
throw new NotSupportedException("Rename mode '" + mode + "' is not supported.");
197188
}
@@ -444,15 +435,24 @@ DisplayNormalizedName CompressTypeName(string typeName, RenameMode renameMode)
444435
{
445436
if (!_prefixesMap.TryGetValue(typeName, out string prefix))
446437
{
447-
IncrementNameId();
448-
prefix = Utils.EncodeString(nameId, alphaNumCharset);
449-
_prefixesMap.Add(typeName, prefix);
438+
_prefixesMap.Add(typeName, GetNextSequentialName());
450439
}
451440

452441
return new DisplayNormalizedName(typeName, prefix);
453442
}
454443

455444
return new DisplayNormalizedName(typeName, typeName);
456445
}
446+
447+
string GetNextSequentialName() {
448+
var number = _nameId++;
449+
var bigLength = (long)alphaNumCharset.Length;
450+
_nameBuilder.Clear();
451+
do {
452+
number = Math.DivRem(number, bigLength, out var remainder);
453+
_nameBuilder.Append(alphaNumCharset[remainder]);
454+
} while (number != 0);
455+
return _nameBuilder.ToString();
456+
}
457457
}
458458
}

Tests/MessageDeobfuscation.Test/MessageDeobfuscationTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ await Run(
4747
eventId = "_cbPBZqkDuaNXOkmJtacrG2uYfZs";
4848
}
4949
else {
50-
classId = "_g";
51-
nestedClassId = "_e";
52-
methodId = "_C";
53-
fieldId = "_d";
54-
propertyId = "_E";
55-
eventId = "_b";
50+
classId = "_F";
51+
nestedClassId = "_D";
52+
methodId = "_c";
53+
fieldId = "_C";
54+
propertyId = "_e";
55+
eventId = "_A";
5656
}
5757

5858
void CheckName(string expectedFullName, string expectedShortName, string obfuscatedName) {
@@ -92,8 +92,8 @@ public static IEnumerable<object[]> RenameModeAndExpectedObfuscatedOutput() =>
9292
new object[] {
9393
nameof(RenameMode.Sequential), new[] {
9494
"Exception",
95-
" at _g._e._c(String )",
96-
" at _B._f()"
95+
" at _F._D._B(String )",
96+
" at _b._E()"
9797
}
9898
}
9999
};
@@ -122,8 +122,8 @@ await RunDeobfuscationWithPassword(true, null, "_1", Array.Empty<string>(),
122122
public async Task CheckPasswordDependsOnSeed() {
123123
var expectedObfuscatedOutput = new[] {
124124
"Exception",
125-
" at oZuuchQgRo99FxO43G5kj2LB6aE3b$hsLiIOVL3cn0lg.CN9UQGJKgUbt4OKGBs8_vig.FtV6w7kWA1GcUNTnc2UDptg(String )",
126-
" at EcGxTPKtKIEeZuP3ekjPVhrVKQsiovm5zMkq5xfZbt1V.xvkt0Ir5VfNl8phozRzOvg8()"
125+
" at oZuuchQgRo99FxO43G5kj2LB6aE3b$hsLiIOVL3cn0lg.98C7L64wnMJK6DFKHzyWSw8.at9I2jHJrbSIlewmDrNXdMI(String )",
126+
" at EcGxTPKtKIEeZuP3ekjPVhrVKQsiovm5zMkq5xfZbt1V.AiskF07vqbD8ZFG03Jyiiu8()"
127127
};
128128
await RunDeobfuscationWithPassword(true, Seed, "_0", expectedObfuscatedOutput,
129129
outputPath => {
@@ -143,8 +143,8 @@ await RunDeobfuscationWithPassword(true, Seed, "_1", expectedObfuscatedOutput,
143143
public async Task MessageDeobfuscationWithPassword() {
144144
var expectedObfuscatedOutput = new[] {
145145
"Exception",
146-
" at oQmpV$y2k2b9P3d6GP1cxGPuRtKaNIZvZcKpZXSfKFG8.CE8t0VDPQk9$jgv1XuRwt1k.FhsPrCLqIAaPKe7abGklvY4(String )",
147-
" at EbUjRcrC76NnA7RJlhQffrfp$vMGHdDfqtVFtWrAOPyD.xgIw9voebB21PlxPFA_hs60()"
146+
" at oQmpV$y2k2b9P3d6GP1cxGPuRtKaNIZvZcKpZXSfKFG8.99_z9Rxdp_fWfuD3fr45FSA.at9DaPNMANuLaMV_3scPWDU(String )",
147+
" at EbUjRcrC76NnA7RJlhQffrfp$vMGHdDfqtVFtWrAOPyD.AkpOh$3Zo3M8ga5lTY9etcM()"
148148
};
149149
await RunDeobfuscationWithPassword(false, null, "", expectedObfuscatedOutput, outputPath => {
150150
var deobfuscator = new MessageDeobfuscator(Password);

0 commit comments

Comments
 (0)