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
|
/*******************************************************************************
* 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.analysis;
import com.google.common.io.ByteStreams;
import cuchaz.enigma.mapping.entry.ClassEntry;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.tree.ClassNode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
public class ParsedJar {
private final Map<String, byte[]> classBytes;
private final Map<String, ClassNode> nodeCache = new HashMap<>();
public ParsedJar(JarFile jar) throws IOException {
Map<String, byte[]> uClassBytes = new LinkedHashMap<>();;
try {
// get the jar entries that correspond to classes
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
// is this a class file?
if (entry.getName().endsWith(".class")) {
try (InputStream input = new BufferedInputStream(jar.getInputStream(entry))) {
String path = entry.getName().substring(0, entry.getName().length() - ".class".length());
uClassBytes.put(path, ByteStreams.toByteArray(input));
}
}
}
} finally {
jar.close();
classBytes = Collections.unmodifiableMap(uClassBytes);
}
}
public ParsedJar(JarInputStream jar) throws IOException {
Map<String, byte[]> uClassBytes = new LinkedHashMap<>();
try {
// get the jar entries that correspond to classes
JarEntry entry;
while ((entry = jar.getNextJarEntry()) != null) {
// is this a class file?
if (entry.getName().endsWith(".class")) {
String path = entry.getName().substring(0, entry.getName().length() - ".class".length());
uClassBytes.put(path, ByteStreams.toByteArray(jar));
jar.closeEntry();
}
}
} finally {
jar.close();
classBytes = Collections.unmodifiableMap(uClassBytes);
}
}
public void visitReader(Function<String, ClassVisitor> visitorFunction, int options) {
for (String s : classBytes.keySet()) {
ClassNode nodeCached = nodeCache.get(s);
if (nodeCached != null) {
nodeCached.accept(visitorFunction.apply(s));
} else {
new ClassReader(classBytes.get(s)).accept(visitorFunction.apply(s), options);
}
}
}
public void visitNode(Consumer<ClassNode> consumer) {
for (String s : classBytes.keySet()) {
consumer.accept(getClassNode(s));
}
}
public int getClassCount() {
return classBytes.size();
}
public List<ClassEntry> getClassEntries() {
List<ClassEntry> entries = new ArrayList<>(classBytes.size());
for (String s : classBytes.keySet()) {
entries.add(new ClassEntry(s));
}
return entries;
}
public ClassNode getClassNode(String name) {
return nodeCache.computeIfAbsent(name, (n) -> {
ClassReader reader = new ClassReader(classBytes.get(name));
ClassNode node = new ClassNode();
reader.accept(node, 0);
return node;
});
}
public Map<String, byte[]> getClassDataMap() {
return classBytes;
}
}
|