-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdCli.java
More file actions
272 lines (225 loc) · 8.71 KB
/
JdCli.java
File metadata and controls
272 lines (225 loc) · 8.71 KB
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
/*
Copyright (C) 2022 hidenorly
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 com.github.hidenorly.jdcli;
import com.github.hidenorly.jdcli.OptParse;
import com.github.hidenorly.jdcli.OptParse.OptParseItem;
import org.jd.core.v1.api.*;
import org.jd.core.v1.api.Decompiler;
import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
import org.jd.core.v1.api.loader.Loader;
import org.jd.core.v1.api.loader.LoaderException;
import org.jd.core.v1.api.printer.Printer;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.util.Vector;
import java.util.List;
import java.util.ArrayList;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
public class JdCli {
static protected String mOutputPath;
static List<String> mOutputPaths = new ArrayList<String>();
static protected boolean mIsOutputFile;
protected static void ensureDirectory(String path){
try{
String basePath = new File(path).getParent();
Files.createDirectories( Paths.get( basePath ) );
} catch(Exception ex) {
}
}
protected static String getNumberedFilename(String filename, int num){
int nPos1 = filename.lastIndexOf( ".java" );
int nPos2 = filename.lastIndexOf( "_", nPos1 );
if( nPos2 !=-1 ){
filename = filename.substring( 0, nPos2 + 1 ) + String.valueOf( num ) + ".java";
} else {
if( num > 1 ){
filename = filename.substring( 0, nPos1 ) + "_" + String.valueOf( num ) + ".java";
}
}
return filename;
}
protected static String getFullClassPathFromSourceCode(String source){
String filename = "null";
// extract class name
int nPos1 = source.indexOf( "package " );
int nPos2 = source.indexOf( ";", nPos1 );
if( nPos1!=-1 && nPos2!=-1 ){
filename = source.substring( nPos1 + 8, nPos2 );
filename = filename.replace( ".", "/" );
int nPos3 = filename.indexOf( ":" );
if( nPos3 != -1 ){
filename = filename.substring( nPos3 + 1 );
}
nPos1 = source.indexOf( "class ", nPos2 + 1);
if( nPos1 !=-1 ){
nPos2 = source.indexOf( " ", nPos1 + 7 );
if( nPos2 !=-1 ){
filename = filename + "/" + source.substring( nPos1 + 6, nPos2 );
}
}
}
return filename;
}
protected static String getNonDuplicatedFilename(String filename){
// check duplicated or not
int num = 1;
while( mOutputPaths.contains( filename ) ){
num++;
filename = getNumberedFilename( filename, num );
}
mOutputPaths.add( filename );
return filename;
}
protected static String getOutputPath(String source){
String filename = getFullClassPathFromSourceCode( source );
filename = filename+".java";
filename = getNonDuplicatedFilename( filename );
filename = mOutputPath + "/" + filename;
return filename;
}
static protected void doDisassemble(String path){
Loader loader = new Loader() {
protected InputStream getStream(String internalName){
InputStream is = this.getClass().getResourceAsStream( "/" + internalName + ".class" );
Path targetPath = null;
try{
int nPos = internalName.indexOf(":");
if( nPos !=-1 ){
// this is .jar case
String zipPath = internalName.substring(0, nPos);
String classPath = internalName.substring(nPos+1);
Path zipFile = Paths.get( zipPath );
ClassLoader loader = null;
FileSystem fs = FileSystems.newFileSystem( zipFile, loader );
targetPath = fs.getPath( classPath );
} else {
targetPath = Paths.get( path );
}
} catch (Exception ex) {
}
if( is == null && targetPath != null ){
try{
is = Files.newInputStream( targetPath );
} catch( Exception ex ){
}
}
return is;
}
@Override
public byte[] load(String internalName) throws LoaderException {
InputStream is = getStream( internalName );
if (is == null) {
System.out.println( "Loader::load:"+internalName+":failed" );
return null;
} else {
try (InputStream in=is; ByteArrayOutputStream out=new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read > 0) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
return out.toByteArray();
} catch (IOException e) {
throw new LoaderException(e);
}
}
}
@Override
public boolean canLoad(String internalName) {
return true;
}
};
Printer printer = new Printer() {
protected static final String TAB = " ";
protected static final String NEWLINE = "\n";
protected int indentationCount = 0;
protected StringBuilder sb = new StringBuilder();
@Override public String toString() { return sb.toString(); }
@Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {}
@Override public void end() { }
@Override public void printText(String text) { sb.append(text); }
@Override public void printNumericConstant(String constant) { sb.append(constant); }
@Override public void printStringConstant(String constant, String ownerInternalName) { sb.append(constant); }
@Override public void printKeyword(String keyword) { sb.append(keyword); }
@Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) { sb.append(name); }
@Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { sb.append(name); }
@Override public void indent() { this.indentationCount++; }
@Override public void unindent() { this.indentationCount--; }
@Override public void startLine(int lineNumber) { for (int i=0; i<indentationCount; i++) sb.append(TAB); }
@Override public void endLine() { sb.append(NEWLINE); }
@Override public void extraLine(int count) { while (count-- > 0) sb.append(NEWLINE); }
@Override public void startMarker(int type) {}
@Override public void endMarker(int type) {}
};
ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler();
try{
decompiler.decompile(loader, printer, path);
} catch (Exception ex) {
}
String source = printer.toString();
if( mIsOutputFile ){
String outputPath = getOutputPath( source );
ensureDirectory( outputPath );
try {
PrintWriter ps = new PrintWriter( new FileOutputStream( outputPath ) );
ps.print( source );
ps.flush();
ps.close();
} catch( Exception ex ){
}
} else {
System.out.println( source );
}
}
protected static List<Path> getClassPaths(String path){
List<Path> result = new ArrayList<Path>();
Path thePath = Paths.get( path );
if( Files.isDirectory( thePath ) || path.endsWith(".apk") || path.endsWith(".zip") || path.endsWith(".jar") ){
result = FileLister.getFileList( path, ".*\\.class" );
} else {
result.add( thePath );
}
return result;
}
public static void main(String[] args) {
Vector<OptParseItem> options = new Vector<OptParseItem>();
options.add( new OptParseItem("-o", "--output", true, "", "Specify output path if you want to output as file") );
OptParse opt = new OptParse( args, options, "JdCli [options] target1.class [target2.class ...]");
mOutputPath = opt.values.get("-o");
mIsOutputFile = !opt.values.get("-o").isEmpty();
for( String anArg : opt.args ){
List<Path> paths = getClassPaths( anArg );
for( Path thePath : paths ){
try {
if( thePath.toString().startsWith( anArg ) ){
doDisassemble( thePath.toString() );
} else {
doDisassemble( anArg+":"+thePath.toString() );
}
} catch( Exception ex ) {
}
}
}
}
}