-
Notifications
You must be signed in to change notification settings - Fork 374
/
Main.java
239 lines (211 loc) · 8.42 KB
/
Main.java
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package info.persistent.dex;
import com.android.dexdeps.DexData;
import com.android.dexdeps.DexDataException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class Main {
private boolean countFields;
private boolean includeClasses;
private String packageFilter;
private int maxDepth = Integer.MAX_VALUE;
private DexMethodCounts.Filter filter = DexMethodCounts.Filter.ALL;
private DexMethodCounts.OutputStyle outputStyle = DexMethodCounts.OutputStyle.TREE;
public static void main(String[] args) {
Main main = new Main();
main.run(args);
}
void run(String[] args) {
try {
String[] inputFileNames = parseArgs(args);
int overallCount = 0;
for (String fileName : collectFileNames(inputFileNames)) {
System.out.println("Processing " + fileName);
DexCount counts;
if (countFields) {
counts = new DexFieldCounts(outputStyle);
} else {
counts = new DexMethodCounts(outputStyle);
}
List<RandomAccessFile> dexFiles = openInputFiles(fileName);
for (RandomAccessFile dexFile : dexFiles) {
DexData dexData = new DexData(dexFile);
dexData.load();
counts.generate(dexData, includeClasses, packageFilter, maxDepth, filter);
dexFile.close();
}
counts.output();
overallCount = counts.getOverallCount();
}
System.out.println(String.format("Overall %s count: %d", countFields ? "field" : "method", overallCount));
} catch (UsageException ue) {
usage();
System.exit(2);
} catch (IOException ioe) {
if (ioe.getMessage() != null) {
System.err.println("Failed: " + ioe);
}
System.exit(1);
} catch (DexDataException dde) {
/* a message was already reported, just bail quietly */
System.exit(1);
}
}
/**
* Opens an input file, which could be a .dex or a .jar/.apk with a
* classes.dex inside. If the latter, we extract the contents to a
* temporary file.
*/
List<RandomAccessFile> openInputFiles(String fileName) throws IOException {
List<RandomAccessFile> dexFiles = new ArrayList<RandomAccessFile>();
openInputFileAsZip(fileName, dexFiles);
if (dexFiles.size() == 0) {
File inputFile = new File(fileName);
RandomAccessFile dexFile = new RandomAccessFile(inputFile, "r");
dexFiles.add(dexFile);
}
return dexFiles;
}
/**
* Tries to open an input file as a Zip archive (jar/apk) with a
* "classes.dex" inside.
*/
void openInputFileAsZip(String fileName, List<RandomAccessFile> dexFiles) throws IOException {
ZipFile zipFile;
// Try it as a zip file.
try {
zipFile = new ZipFile(fileName);
} catch (FileNotFoundException fnfe) {
// not found, no point in retrying as non-zip.
System.err.println("Unable to open '" + fileName + "': " +
fnfe.getMessage());
throw fnfe;
} catch (ZipException ze) {
// not a zip
return;
}
// Open and add all files matching "classes.*\.dex" in the zip file.
for (ZipEntry entry : Collections.list(zipFile.entries())) {
if (entry.getName().matches("classes.*\\.dex")) {
dexFiles.add(openDexFile(zipFile, entry));
}
}
zipFile.close();
}
RandomAccessFile openDexFile(ZipFile zipFile, ZipEntry entry) throws IOException {
// We know it's a zip; see if there's anything useful inside. A
// failure here results in some type of IOException (of which
// ZipException is a subclass).
InputStream zis = zipFile.getInputStream(entry);
// Create a temp file to hold the DEX data, open it, and delete it
// to ensure it doesn't hang around if we fail.
File tempFile = File.createTempFile("dexdeps", ".dex");
RandomAccessFile dexFile = new RandomAccessFile(tempFile, "rw");
tempFile.delete();
// Copy all data from input stream to output file.
byte copyBuf[] = new byte[32768];
int actual;
while (true) {
actual = zis.read(copyBuf);
if (actual == -1)
break;
dexFile.write(copyBuf, 0, actual);
}
dexFile.seek(0);
return dexFile;
}
private String[] parseArgs(String[] args) {
int idx;
for (idx = 0; idx < args.length; idx++) {
String arg = args[idx];
if (arg.equals("--") || !arg.startsWith("--")) {
break;
} else if (arg.equals("--count-fields")) {
countFields = true;
} else if (arg.equals("--include-classes")) {
includeClasses = true;
} else if (arg.startsWith("--package-filter=")) {
packageFilter = arg.substring(arg.indexOf('=') + 1);
} else if (arg.startsWith("--max-depth=")) {
maxDepth =
Integer.parseInt(arg.substring(arg.indexOf('=') + 1));
} else if (arg.startsWith("--filter=")) {
filter = Enum.valueOf(
DexMethodCounts.Filter.class,
arg.substring(arg.indexOf('=') + 1).toUpperCase());
} else if (arg.startsWith("--output-style")) {
outputStyle = Enum.valueOf(
DexMethodCounts.OutputStyle.class,
arg.substring(arg.indexOf('=') + 1).toUpperCase());
} else {
System.err.println("Unknown option '" + arg + "'");
throw new UsageException();
}
}
// We expect at least one more argument (file name).
int fileCount = args.length - idx;
if (fileCount == 0) {
throw new UsageException();
}
String[] inputFileNames = new String[fileCount];
System.arraycopy(args, idx, inputFileNames, 0, fileCount);
return inputFileNames;
}
private void usage() {
System.err.print(
"DEX per-package/class method counts v1.5\n" +
"Usage: dex-method-counts [options] <file.{dex,apk,jar,directory}> ...\n" +
"Options:\n" +
" --count-fields\n" +
" --include-classes\n" +
" --package-filter=com.foo.bar\n" +
" --max-depth=N\n" +
" --filter=ALL|DEFINED_ONLY|REFERENCED_ONLY\n" +
" --output-style=FLAT|TREE\n"
);
}
/**
* Checks if input files array contain directories and
* adds it's contents to the file list if so.
* Otherwise just adds a file to the list.
*
* @return a List of file names to process
*/
private List<String> collectFileNames(String[] inputFileNames) {
List<String> fileNames = new ArrayList<String>();
for (String inputFileName : inputFileNames) {
File file = new File(inputFileName);
if (file.isDirectory()) {
String dirPath = file.getAbsolutePath();
for (String fileInDir: file.list()){
fileNames.add(dirPath + File.separator + fileInDir);
}
} else {
fileNames.add(inputFileName);
}
}
return fileNames;
}
private static class UsageException extends RuntimeException {}
}