1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.mapping;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPOutputStream;
import com.google.common.collect.Lists;
import cuchaz.enigma.analysis.Access;
import cuchaz.enigma.analysis.JarIndex;
import cuchaz.enigma.analysis.MethodImplementationsTreeNode;
import cuchaz.enigma.throwables.IllegalNameException;
import cuchaz.enigma.throwables.MappingConflict;
public class MappingsRenamer {
private JarIndex m_index;
private Mappings m_mappings;
public MappingsRenamer(JarIndex index, Mappings mappings) {
m_index = index;
m_mappings = mappings;
}
public void setMappings(Mappings mappings)
{
this.m_mappings = mappings;
}
public void setClassName(ClassEntry obf, String deobfName) {
deobfName = NameValidator.validateClassName(deobfName, !obf.isInnerClass());
List<ClassMapping> mappingChain = getOrCreateClassMappingChain(obf);
if (mappingChain.size() == 1) {
if (deobfName != null) {
// make sure we don't rename to an existing obf or deobf class
if (m_mappings.containsDeobfClass(deobfName) || m_index.containsObfClass(new ClassEntry(deobfName))) {
throw new IllegalNameException(deobfName, "There is already a class with that name");
}
}
ClassMapping classMapping = mappingChain.get(0);
m_mappings.setClassDeobfName(classMapping, deobfName);
} else {
ClassMapping outerClassMapping = mappingChain.get(mappingChain.size() - 2);
if (deobfName != null) {
// make sure we don't rename to an existing obf or deobf inner class
if (outerClassMapping.hasInnerClassByDeobf(deobfName) || outerClassMapping.hasInnerClassByObfSimple(deobfName)) {
throw new IllegalNameException(deobfName, "There is already a class with that name");
}
}
outerClassMapping.setInnerClassName(obf, deobfName);
}
}
public void removeClassMapping(ClassEntry obf) {
setClassName(obf, null);
}
public void markClassAsDeobfuscated(ClassEntry obf) {
String deobfName = obf.isInnerClass() ? obf.getInnermostClassName() : obf.getName();
List<ClassMapping> mappingChain = getOrCreateClassMappingChain(obf);
if (mappingChain.size() == 1) {
ClassMapping classMapping = mappingChain.get(0);
m_mappings.setClassDeobfName(classMapping, deobfName);
} else {
ClassMapping outerClassMapping = mappingChain.get(mappingChain.size() - 2);
outerClassMapping.setInnerClassName(obf, deobfName);
}
}
private void validateFieldTreeName(FieldEntry obf, String deobfName, byte directionFlag) {
FieldEntry targetEntry = new FieldEntry(obf.getClassEntry(), deobfName, obf.getType());
boolean contains = false;
ClassMapping classMapping = m_mappings.getClassByObf(obf.getClassEntry());
if (classMapping != null && classMapping.containsDeobfField(deobfName, obf.getType())
&& m_index.getAccess(classMapping.getFieldByDeobf(deobfName, obf.getType()).getObfEntry(obf.getClassEntry())).isSubclassAccessible()) {
contains = true;
}
if (!contains && m_index.containsObfField(targetEntry)
&& m_index.getAccess(targetEntry).isSubclassAccessible()) {
contains = true;
}
if (contains) {
String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(obf.getClassName());
if (deobfClassName == null) {
deobfClassName = obf.getClassName();
}
throw new IllegalNameException(deobfName, "There is already a field with that name in class " + deobfClassName);
}
if (directionFlag == 1) {
ClassEntry ancestor = m_index.getTranslationIndex().getSuperclass(obf.getClassEntry());
if (m_index.containsObfClass(ancestor)) {
validateFieldTreeName(obf.cloneToNewClass(ancestor), deobfName, directionFlag);
}
} else if (directionFlag == 2) {
for (ClassEntry child : m_index.getTranslationIndex().getSubclass(obf.getClassEntry())) {
validateFieldTreeName(obf.cloneToNewClass(child), deobfName, directionFlag);
}
}
}
public void setFieldName(FieldEntry obf, String deobfName) {
deobfName = NameValidator.validateFieldName(deobfName);
if (m_index.getAccess(obf).isSubclassAccessible()) {
validateFieldTreeName(obf, deobfName, (byte) 1);
validateFieldTreeName(obf, deobfName, (byte) 2);
} else {
validateFieldTreeName(obf, deobfName, (byte) 0);
}
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.setFieldName(obf.getName(), obf.getType(), deobfName);
}
public void removeFieldMapping(FieldEntry obf) {
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.removeFieldMapping(classMapping.getFieldByObf(obf.getName(), obf.getType()));
}
public void markFieldAsDeobfuscated(FieldEntry obf) {
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.setFieldName(obf.getName(), obf.getType(), obf.getName());
}
private void validateMethodTreeName(MethodEntry entry, String deobfName) {
MethodEntry targetEntry = new MethodEntry(entry.getClassEntry(), deobfName, entry.getSignature());
// TODO: Verify if I don't break things
ClassMapping classMapping = m_mappings.getClassByObf(entry.getClassEntry());
if ((classMapping != null && classMapping.containsDeobfMethod(deobfName, entry.getSignature()) && classMapping.getMethodByObf(entry.getName(), entry.getSignature()) != classMapping.getMethodByDeobf(deobfName, entry.getSignature()))
|| m_index.containsObfBehavior(targetEntry)) {
String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(entry.getClassName());
if (deobfClassName == null) {
deobfClassName = entry.getClassName();
}
throw new IllegalNameException(deobfName, "There is already a method with that name and signature in class " + deobfClassName);
}
if (m_index.getAccess(entry) != Access.Private) {
for (ClassEntry child : m_index.getTranslationIndex().getSubclass(entry.getClassEntry())) {
validateMethodTreeName(entry.cloneToNewClass(child), deobfName);
}
}
}
public void setMethodTreeName(MethodEntry obf, String deobfName) {
Set<MethodEntry> implementations = m_index.getRelatedMethodImplementations(obf);
deobfName = NameValidator.validateMethodName(deobfName);
for (MethodEntry entry : implementations) {
validateMethodTreeName(entry, deobfName);
}
for (MethodEntry entry : implementations) {
setMethodName(entry, deobfName);
}
}
public void setMethodName(MethodEntry obf, String deobfName) {
deobfName = NameValidator.validateMethodName(deobfName);
MethodEntry targetEntry = new MethodEntry(obf.getClassEntry(), deobfName, obf.getSignature());
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
// TODO: Verify if I don't break things
if ((m_mappings.containsDeobfMethod(obf.getClassEntry(), deobfName, obf.getSignature()) && classMapping.getMethodByObf(obf.getName(), obf.getSignature()) != classMapping.getMethodByDeobf(deobfName, obf.getSignature()))
|| m_index.containsObfBehavior(targetEntry)) {
String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(obf.getClassName());
if (deobfClassName == null) {
deobfClassName = obf.getClassName();
}
throw new IllegalNameException(deobfName, "There is already a method with that name and signature in class " + deobfClassName);
}
classMapping.setMethodName(obf.getName(), obf.getSignature(), deobfName);
}
public void removeMethodTreeMapping(MethodEntry obf) {
m_index.getRelatedMethodImplementations(obf).forEach(this::removeMethodMapping);
}
public void removeMethodMapping(MethodEntry obf) {
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.setMethodName(obf.getName(), obf.getSignature(), null);
}
public void markMethodTreeAsDeobfuscated(MethodEntry obf) {
m_index.getRelatedMethodImplementations(obf).forEach(this::markMethodAsDeobfuscated);
}
public void markMethodAsDeobfuscated(MethodEntry obf) {
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.setMethodName(obf.getName(), obf.getSignature(), obf.getName());
}
public void setArgumentTreeName(ArgumentEntry obf, String deobfName) {
if (!(obf.getBehaviorEntry() instanceof MethodEntry)) {
setArgumentName(obf, deobfName);
return;
}
MethodEntry obfMethod = (MethodEntry) obf.getBehaviorEntry();
Set<MethodEntry> implementations = m_index.getRelatedMethodImplementations(obfMethod);
for (MethodEntry entry : implementations) {
ClassMapping classMapping = m_mappings.getClassByObf(entry.getClassEntry());
if (classMapping != null) {
MethodMapping mapping = classMapping.getMethodByObf(entry.getName(), entry.getSignature());
// NOTE: don't need to check arguments for name collisions with names determined by Procyon
// TODO: Verify if I don't break things
if (mapping != null) {
for (ArgumentMapping argumentMapping : Lists.newArrayList(mapping.arguments())) {
if (argumentMapping.getIndex() != obf.getIndex()) {
if (mapping.getDeobfArgumentName(argumentMapping.getIndex()).equals(deobfName)
|| argumentMapping.getName().equals(deobfName)) {
throw new IllegalNameException(deobfName, "There is already an argument with that name");
}
}
}
}
}
}
for (MethodEntry entry : implementations) {
setArgumentName(new ArgumentEntry(obf, entry), deobfName);
}
}
public void setArgumentName(ArgumentEntry obf, String deobfName) {
deobfName = NameValidator.validateArgumentName(deobfName);
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
MethodMapping mapping = classMapping.getMethodByObf(obf.getMethodName(), obf.getMethodSignature());
// NOTE: don't need to check arguments for name collisions with names determined by Procyon
// TODO: Verify if I don't break things
if (mapping != null) {
for (ArgumentMapping argumentMapping : Lists.newArrayList(mapping.arguments())) {
if (argumentMapping.getIndex() != obf.getIndex()) {
if (mapping.getDeobfArgumentName(argumentMapping.getIndex()).equals(deobfName)
|| argumentMapping.getName().equals(deobfName)) {
throw new IllegalNameException(deobfName, "There is already an argument with that name");
}
}
}
}
classMapping.setArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), deobfName);
}
public void removeArgumentMapping(ArgumentEntry obf) {
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.removeArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex());
}
public void markArgumentAsDeobfuscated(ArgumentEntry obf) {
ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry());
classMapping.setArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), obf.getName());
}
public boolean moveFieldToObfClass(ClassMapping classMapping, FieldMapping fieldMapping, ClassEntry obfClass) {
classMapping.removeFieldMapping(fieldMapping);
ClassMapping targetClassMapping = getOrCreateClassMapping(obfClass);
if (!targetClassMapping.containsObfField(fieldMapping.getObfName(), fieldMapping.getObfType())) {
if (!targetClassMapping.containsDeobfField(fieldMapping.getDeobfName(), fieldMapping.getObfType())) {
targetClassMapping.addFieldMapping(fieldMapping);
return true;
} else {
System.err.println("WARNING: deobf field was already there: " + obfClass + "." + fieldMapping.getDeobfName());
}
}
return false;
}
public boolean moveMethodToObfClass(ClassMapping classMapping, MethodMapping methodMapping, ClassEntry obfClass) {
classMapping.removeMethodMapping(methodMapping);
ClassMapping targetClassMapping = getOrCreateClassMapping(obfClass);
if (!targetClassMapping.containsObfMethod(methodMapping.getObfName(), methodMapping.getObfSignature())) {
if (!targetClassMapping.containsDeobfMethod(methodMapping.getDeobfName(), methodMapping.getObfSignature())) {
targetClassMapping.addMethodMapping(methodMapping);
return true;
} else {
System.err.println("WARNING: deobf method was already there: " + obfClass + "." + methodMapping.getDeobfName() + methodMapping.getObfSignature());
}
}
return false;
}
public void write(OutputStream out) throws IOException {
// TEMP: just use the object output for now. We can find a more efficient storage format later
GZIPOutputStream gzipout = new GZIPOutputStream(out);
ObjectOutputStream oout = new ObjectOutputStream(gzipout);
oout.writeObject(this);
gzipout.finish();
}
private ClassMapping getOrCreateClassMapping(ClassEntry obfClassEntry) {
List<ClassMapping> mappingChain = getOrCreateClassMappingChain(obfClassEntry);
return mappingChain.get(mappingChain.size() - 1);
}
private List<ClassMapping> getOrCreateClassMappingChain(ClassEntry obfClassEntry) {
List<ClassEntry> classChain = obfClassEntry.getClassChain();
List<ClassMapping> mappingChain = m_mappings.getClassMappingChain(obfClassEntry);
for (int i = 0; i < classChain.size(); i++) {
ClassEntry classEntry = classChain.get(i);
ClassMapping classMapping = mappingChain.get(i);
if (classMapping == null) {
// create it
classMapping = new ClassMapping(classEntry.getName());
mappingChain.set(i, classMapping);
// add it to the right parent
try {
if (i == 0) {
m_mappings.addClassMapping(classMapping);
} else {
mappingChain.get(i - 1).addInnerClassMapping(classMapping);
}
} catch (MappingConflict mappingConflict) {
mappingConflict.printStackTrace();
}
}
}
return mappingChain;
}
}
|