summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/main/decompiler/ConsoleDecompiler.java
blob: bfb4e70eec49eb853537fd53f9b529526706babd (plain)
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
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 org.jetbrains.java.decompiler.main.decompiler;

import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.Fernflower;
import org.jetbrains.java.decompiler.main.decompiler.helper.PrintStreamLogger;
import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider;
import org.jetbrains.java.decompiler.main.extern.IDecompilatSaver;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.util.InterpreterUtil;

import java.io.*;
import java.util.*;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

@SuppressWarnings({"UseOfSystemOutOrSystemErr", "CallToPrintStackTrace"})
public class ConsoleDecompiler implements IBytecodeProvider, IDecompilatSaver {

  private File root;

  private Fernflower fernflower;

  private HashMap<String, ZipOutputStream> mapArchiveStreams = new HashMap<String, ZipOutputStream>();

  private HashMap<String, HashSet<String>> mapArchiveEntries = new HashMap<String, HashSet<String>>();

  public ConsoleDecompiler() {
    this(null);
  }

  public ConsoleDecompiler(HashMap<String, Object> propertiesCustom) {
    this(new PrintStreamLogger(IFernflowerLogger.WARNING, System.out), propertiesCustom);
  }

  protected ConsoleDecompiler(IFernflowerLogger logger, HashMap<String, Object> propertiesCustom) {
    fernflower = new Fernflower(this, this, propertiesCustom);
    DecompilerContext.setLogger(logger);
  }

  public static void main(String[] args) {

    try {

      if (args != null && args.length > 1) {

        HashMap<String, Object> mapOptions = new HashMap<String, Object>();

        List<String> lstSources = new ArrayList<String>();
        List<String> lstLibraries = new ArrayList<String>();

        boolean isOption = true;
        for (int i = 0; i < args.length - 1; ++i) { // last parameter - destination
          String arg = args[i];

          if (isOption && arg.startsWith("-") &&
              arg.length() > 5 && arg.charAt(4) == '=') {
            String value = arg.substring(5).toUpperCase(Locale.US);
            if ("TRUE".equals(value)) {
              value = "1";
            }
            else if ("FALSE".equals(value)) {
              value = "0";
            }

            mapOptions.put(arg.substring(1, 4), value);
          }
          else {
            isOption = false;

            if (arg.startsWith("-e=")) {
              lstLibraries.add(arg.substring(3));
            }
            else {
              lstSources.add(arg);
            }
          }
        }

        if (lstSources.isEmpty()) {
          printHelp();
        }
        else {
          ConsoleDecompiler decompiler = new ConsoleDecompiler(
            new PrintStreamLogger(IFernflowerLogger.INFO, System.out),
            mapOptions);

          for (String source : lstSources) {
            decompiler.addSpace(new File(source), true);
          }

          for (String library : lstLibraries) {
            decompiler.addSpace(new File(library), false);
          }

          decompiler.decompileContext(new File(args[args.length - 1]));
        }
      }
      else {
        printHelp();
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private static void printHelp() {
    System.out.println("Usage: java ConsoleDecompiler ( -<option>=<value>)* (<source>)+ <destination>");
    System.out.println("Example: java ConsoleDecompiler -dgs=true c:\\mysource\\ c:\\my.jar d:\\decompiled\\");
  }

  public void addSpace(File file, boolean isOwn) throws IOException {
    fernflower.getStructcontext().addSpace(file, isOwn);
  }

  public void decompileContext(File root) {
    this.root = root;
    try {
      fernflower.decompileContext();
    }
    finally {
      fernflower.clearContext();
    }
  }

  // *******************************************************************
  // Interface IBytecodeProvider
  // *******************************************************************

  public InputStream getBytecodeStream(String externPath, String internPath) {

    try {
      File file = new File(externPath);

      if (internPath == null) {
        return new FileInputStream(file);
      }
      else { // archive file
        ZipFile archive = new ZipFile(file);

        Enumeration<? extends ZipEntry> en = archive.entries();
        while (en.hasMoreElements()) {
          ZipEntry entr = en.nextElement();

          if (entr.getName().equals(internPath)) {
            return archive.getInputStream(entr);
          }
        }
      }
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }

    return null;
  }

  // *******************************************************************
  // Interface IDecompilatSaver
  // *******************************************************************

  private String getAbsolutePath(String path) {
    return new File(root, path).getAbsolutePath();
  }

  private boolean addEntryName(String filename, String entry) {
    HashSet<String> set = mapArchiveEntries.get(filename);
    if (set == null) {
      mapArchiveEntries.put(filename, set = new HashSet<String>());
    }

    return set.add(entry);
  }

  public void copyEntry(String source, String destpath, String archivename, String entryName) {

    try {
      String filename = new File(getAbsolutePath(destpath), archivename).getAbsolutePath();

      if (!addEntryName(filename, entryName)) {
        DecompilerContext.getLogger().writeMessage("Zip entry already exists: " +
                                                   destpath + "," + archivename + "," + entryName, IFernflowerLogger.WARNING);
        return;
      }

      ZipFile srcarchive = new ZipFile(new File(source));

      Enumeration<? extends ZipEntry> en = srcarchive.entries();
      while (en.hasMoreElements()) {
        ZipEntry entr = en.nextElement();

        if (entr.getName().equals(entryName)) {
          InputStream in = srcarchive.getInputStream(entr);

          ZipOutputStream out = mapArchiveStreams.get(filename);
          out.putNextEntry(new ZipEntry(entryName));

          InterpreterUtil.copyInputStream(in, out);
          in.close();
        }
      }

      srcarchive.close();
    }
    catch (IOException ex) {
      DecompilerContext.getLogger()
        .writeMessage("Error copying zip file entry: " + source + "," + destpath + "," + archivename + "," + entryName,
                      IFernflowerLogger.WARNING);
      ex.printStackTrace();
    }
  }

  public void copyFile(String source, String destpath, String destfilename) {
    try {
      InterpreterUtil.copyFile(new File(source), new File(destfilename));
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public void saveFile(String path, String filename, String content) {
    try {
      BufferedWriter out =
        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(getAbsolutePath(path), filename)), "UTF8"));
      out.write(content);
      out.flush();
      out.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public void createArchive(String path, String archivename, Manifest manifest) {

    try {
      File file = new File(getAbsolutePath(path), archivename);
      file.createNewFile();

      ZipOutputStream out;
      if (manifest != null) { // jar
        out = new JarOutputStream(new FileOutputStream(file), manifest);
      }
      else {
        out = new ZipOutputStream(new FileOutputStream(file));
      }
      mapArchiveStreams.put(file.getAbsolutePath(), out);
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public void saveClassEntry(String path, String archivename,
                             String qualifiedName, String entryName, String content) {
    saveEntry(path, archivename, entryName, content);
  }

  public void saveClassFile(String path, String qualifiedName, String entryName, String content) {
    saveFile(path, entryName, content);
  }

  public void saveEntry(String path, String archivename, String entryName,
                        String content) {

    try {
      String filename = new File(getAbsolutePath(path), archivename).getAbsolutePath();

      if (!addEntryName(filename, entryName)) {
        DecompilerContext.getLogger().writeMessage("Zip entry already exists: " +
                                                   path + "," + archivename + "," + entryName, IFernflowerLogger.WARNING);
        return;
      }

      ZipOutputStream out = mapArchiveStreams.get(filename);
      out.putNextEntry(new ZipEntry(entryName));

      if (content != null) {
        BufferedWriter outwriter = new BufferedWriter(new OutputStreamWriter(out, "UTF8"));
        outwriter.write(content);
        outwriter.flush();
      }
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public void saveFolder(String path) {
    File f = new File(getAbsolutePath(path));
    f.mkdirs();
  }


  public void closeArchive(String path, String archivename) {
    try {
      String filename = new File(getAbsolutePath(path), archivename).getAbsolutePath();

      mapArchiveEntries.remove(filename);
      OutputStream out = mapArchiveStreams.remove(filename);
      out.flush();
      out.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}