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
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.bytecode;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javassist.CtClass;
import javassist.bytecode.ConstPool;
import javassist.bytecode.EnclosingMethodAttribute;
import javassist.bytecode.InnerClassesAttribute;
import com.beust.jcommander.internal.Lists;
import cuchaz.enigma.analysis.JarIndex;
import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.EntryFactory;
public class InnerClassWriter {
private JarIndex m_index;
public InnerClassWriter(JarIndex index) {
m_index = index;
}
public void write(CtClass c) {
// build the class chain (inner to outer)
ClassEntry obfClassEntry = EntryFactory.getClassEntry(c);
List<ClassEntry> obfClassChain = Lists.newArrayList();
ClassEntry checkClassEntry = obfClassEntry;
while (checkClassEntry != null) {
obfClassChain.add(checkClassEntry);
checkClassEntry = m_index.getOuterClass(checkClassEntry);
}
// change order: outer to inner
Collections.reverse(obfClassChain);
// does this class have any inner classes?
Collection<ClassEntry> obfInnerClassEntries = m_index.getInnerClasses(obfClassEntry);
boolean isInnerClass = obfClassChain.size() > 1;
if (isInnerClass) {
// it's an inner class, rename it to the fully qualified name
StringBuilder buf = new StringBuilder();
for (ClassEntry obfChainClassEntry : obfClassChain) {
if (buf.length() == 0) {
buf.append(obfChainClassEntry.getName());
} else {
buf.append("$");
buf.append(obfChainClassEntry.getSimpleName());
}
}
c.setName(buf.toString());
BehaviorEntry caller = m_index.getAnonymousClassCaller(obfClassEntry);
if (caller != null) {
// write the enclosing method attribute
if (caller.getName().equals("<clinit>")) {
c.getClassFile().addAttribute(new EnclosingMethodAttribute(c.getClassFile().getConstPool(), caller.getClassName()));
} else {
c.getClassFile().addAttribute(new EnclosingMethodAttribute(c.getClassFile().getConstPool(), caller.getClassName(), caller.getName(), caller.getSignature().toString()));
}
}
}
if (isInnerClass || !obfInnerClassEntries.isEmpty()) {
// create an inner class attribute
InnerClassesAttribute attr = new InnerClassesAttribute(c.getClassFile().getConstPool());
c.getClassFile().addAttribute(attr);
// write the ancestry, but not the outermost class
for (int i=1; i<obfClassChain.size(); i++) {
writeInnerClass(attr, obfClassChain, obfClassChain.get(i));
}
// write the inner classes
for (ClassEntry obfInnerClassEntry : obfInnerClassEntries) {
// extend the class chain
List<ClassEntry> extendedObfClassChain = Lists.newArrayList(obfClassChain);
extendedObfClassChain.add(obfInnerClassEntry);
String fullyQualifiedInnerClassName = writeInnerClass(attr, extendedObfClassChain, obfInnerClassEntry);
// make sure we only reference the fully qualified inner class name
c.replaceClassName(obfInnerClassEntry.getName(), fullyQualifiedInnerClassName);
}
}
}
private String writeInnerClass(InnerClassesAttribute attr, List<ClassEntry> obfClassChain, ClassEntry obfClassEntry) {
// get the new inner class name
String obfInnerClassName = getFullyQualifiedName(obfClassChain, obfClassEntry);
String obfParentClassName = getFullyQualifiedParentName(obfClassChain, obfClassEntry);
// here's what the JVM spec says about the InnerClasses attribute
// append(inner, parent, 0 if anonymous else simple name, flags);
// update the attribute with this inner class
ConstPool constPool = attr.getConstPool();
int innerClassIndex = constPool.addClassInfo(obfInnerClassName);
int parentClassIndex = constPool.addClassInfo(obfParentClassName);
int innerClassSimpleNameIndex = 0;
int accessFlags = 0;
if (!m_index.isAnonymousClass(obfClassEntry)) {
innerClassSimpleNameIndex = constPool.addUtf8Info(obfClassEntry.getSimpleName());
}
attr.append(innerClassIndex, parentClassIndex, innerClassSimpleNameIndex, accessFlags);
/* DEBUG
System.out.println(String.format("\tOBF: %s -> ATTR: %s,%s,%s (replace %s with %s)",
obfClassEntry,
attr.innerClass(attr.tableLength() - 1),
attr.outerClass(attr.tableLength() - 1),
attr.innerName(attr.tableLength() - 1),
Constants.NonePackage + "/" + obfInnerClassName,
obfClassEntry.getName()
));
*/
return obfInnerClassName;
}
private String getFullyQualifiedParentName(List<ClassEntry> classChain, ClassEntry classEntry) {
assert(classChain.size() > 1);
assert(classChain.contains(classEntry));
StringBuilder buf = new StringBuilder();
for (int i=0; classChain.get(i) != classEntry; i++) {
ClassEntry chainEntry = classChain.get(i);
if (buf.length() == 0) {
buf.append(chainEntry.getName());
} else {
buf.append("$");
buf.append(chainEntry.getSimpleName());
}
}
return buf.toString();
}
private String getFullyQualifiedName(List<ClassEntry> classChain, ClassEntry classEntry) {
boolean isInner = classChain.size() > 1;
if (isInner) {
return getFullyQualifiedParentName(classChain, classEntry) + "$" + classEntry.getSimpleName();
} else {
return classEntry.getName();
}
}
}
|