KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > obfuscator > Main


1 /* Main Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: Main.java.in,v 1.4.2.3 2002/05/28 17:34:14 hoenicke Exp $
18  */

19
20 package jode.obfuscator;
21 import jode.bytecode.ClassInfo;
22 import jode.bytecode.SearchPath;
23 import jode.GlobalOptions;
24
25 import gnu.getopt.LongOpt;
26 import gnu.getopt.Getopt;
27
28 import java.lang.reflect.Modifier JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Random JavaDoc;
35
36 public class Main {
37     public static boolean swapOrder = false;
38
39     public static final int OPTION_STRONGOVERLOAD = 0x0001;
40     public static final int OPTION_PRESERVESERIAL = 0x0002;
41     public static int options = OPTION_PRESERVESERIAL;
42
43     private static final LongOpt[] longOptions = new LongOpt[] {
44     new LongOpt("cp", LongOpt.REQUIRED_ARGUMENT, null, 'c'),
45     new LongOpt("classpath", LongOpt.REQUIRED_ARGUMENT, null, 'c'),
46     new LongOpt("destpath", LongOpt.REQUIRED_ARGUMENT, null, 'd'),
47     new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
48     new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V'),
49     new LongOpt("verbose", LongOpt.OPTIONAL_ARGUMENT, null, 'v'),
50     new LongOpt("debug", LongOpt.OPTIONAL_ARGUMENT, null, 'D'),
51     };
52
53     public static final String JavaDoc[] stripNames = {
54     "unreach", "inner", "lvt", "lnt", "source"
55     };
56     public static final int STRIP_UNREACH = 0x0001;
57     public static final int STRIP_INNERINFO = 0x0002;
58     public static final int STRIP_LVT = 0x0004;
59     public static final int STRIP_LNT = 0x0008;
60     public static final int STRIP_SOURCE = 0x0010;
61     public static int stripping = 0;
62     /**
63      * A random pool used to destroy order of method identifiers and
64      * classes in packages. <br>
65      *
66      * A pseudo random is enough, no need to generate the seed
67      * securely. This makes obfuscating errors reproducable.
68      */

69     public static Random JavaDoc rand = new Random JavaDoc(123456);
70
71     private static ClassBundle bundle;
72
73     public static void usage() {
74     PrintWriter JavaDoc err = GlobalOptions.err;
75         err.println("usage: jode.Obfuscator flags* script");
76     err.println(" -h, --help "+
77             "show this information.");
78     err.println(" -V, --version "+
79             "output version information and exit.");
80     err.println(" -v, --verbose "+
81             "be verbose (multiple times means more verbose).");
82     err.println(" -c, --classpath <path> "+
83             "search for classes in specified classpath.");
84     err.println(" "+
85             "The directories should be separated by ','.");
86     err.println(" -d, --dest <dir> "+
87             "write decompiled files to disk into directory destdir.");
88     err.println(" -D, --debug=... "+
89             "use --debug=help for more information.");
90     }
91
92
93     public static ClassBundle getClassBundle() {
94     return bundle;
95     }
96
97     public static void main(String JavaDoc[] params) {
98     if (params.length == 0) {
99         usage();
100         return;
101     }
102     String JavaDoc cp = null, dest = null;
103         
104     GlobalOptions.err.println(GlobalOptions.copyright);
105     bundle = new ClassBundle();
106     boolean errorInParams = false;
107     Getopt g = new Getopt("jode.obfuscator.Main", params, "hVvc:d:D:",
108                   longOptions, true);
109     for (int opt = g.getopt(); opt != -1; opt = g.getopt()) {
110         switch(opt) {
111         case 0:
112         break;
113         case 'h':
114         usage();
115         errorInParams = true;
116         break;
117         case 'V':
118         GlobalOptions.err.println(GlobalOptions.version);
119         break;
120         case 'c':
121         cp = g.getOptarg();
122         break;
123         case 'd':
124         dest = g.getOptarg();
125         break;
126         case 'v': {
127         String JavaDoc arg = g.getOptarg();
128         if (arg == null)
129             GlobalOptions.verboseLevel++;
130         else {
131             try {
132             GlobalOptions.verboseLevel = Integer.parseInt(arg);
133             } catch (NumberFormatException JavaDoc ex) {
134             GlobalOptions.err.println
135                 ("jode.obfuscator.Main: Argument `"
136                  +arg+"' to --verbose must be numeric:");
137             errorInParams = true;
138             }
139         }
140         break;
141         }
142         case 'D': {
143         String JavaDoc arg = g.getOptarg();
144         if (arg == null)
145             arg = "help";
146         errorInParams |= !GlobalOptions.setDebugging(arg);
147         break;
148         }
149         default:
150         errorInParams = true;
151         break;
152         }
153     }
154     if (errorInParams)
155         return;
156
157         if (g.getOptind() != params.length - 1) {
158             GlobalOptions.err.println("You must specify exactly one script.");
159             return;
160         }
161
162
163     try {
164         String JavaDoc filename = params[g.getOptind()];
165         ScriptParser parser = new ScriptParser
166         (filename.equals("-")
167          ? new InputStreamReader JavaDoc(System.in)
168          : new FileReader JavaDoc(filename));
169         parser.parseOptions(bundle);
170     } catch (IOException JavaDoc ex) {
171         GlobalOptions.err.println
172         ("IOException while reading script file.");
173         ex.printStackTrace(GlobalOptions.err);
174         return;
175     } catch (ParseException ex) {
176         GlobalOptions.err.println("Syntax error in script file: ");
177         GlobalOptions.err.println(ex.getMessage());
178         if (GlobalOptions.verboseLevel > 5)
179         ex.printStackTrace(GlobalOptions.err);
180         return;
181     }
182
183     // Command Line overwrites script options:
184
if (cp != null)
185         bundle.setOption("classpath", Collections.singleton(cp));
186     if (dest != null)
187         bundle.setOption("dest", Collections.singleton(dest));
188
189     bundle.run();
190     }
191 }
192
193
Popular Tags