KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > KeywordNameMaker


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at theit@gmx.de.
22  *
23  *
24  * $Id: KeywordNameMaker.java,v 1.2 2002/05/11 18:55:54 glurk Exp $
25  */

26 package net.sf.javaguard;
27
28
29 import java.io.*;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33
34 /** Name generator that uses (almost) the full Java identifier namespace,
35  * and chooses to put some of the keyword names (legal in JVM, illegal in
36  * Java language) out front in sequence.
37  *
38  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
39  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
40  */

41 public class KeywordNameMaker extends NameMaker {
42   // Constants -------------------------------------------------------------
43
private static final String JavaDoc DUMMY_ARG_LIST = "dummy";
44   
45   
46   // Fields ----------------------------------------------------------------
47
private int skipped = 0; // Names skipped in the sequence
48
private Vector JavaDoc namesToDate = new Vector JavaDoc();
49   private Hashtable JavaDoc argCount = new Hashtable JavaDoc();
50   private String JavaDoc[] noObfNames = null; // List of names not to be obfuscated
51
private String JavaDoc[] keywordsToUse;
52   private String JavaDoc[] keywordsToExclude;
53   private String JavaDoc[] firstLetter;
54   private String JavaDoc[] nextLetter;
55   private String JavaDoc[] noKeywords = {};
56   private String JavaDoc[] someKeywords = {
57     "a", "if", "do", "for", "int", "new", "try", "byte", "case", "char",
58     "else", "goto", "long", "null", "void"
59   };
60   private String JavaDoc[] allKeywords = {
61     "if", "do", "for", "int", "new", "try", "byte", "case", "char",
62     "else", "goto", "long", "null", "this", "void", "true", "false",
63     "break", "catch", "class", "const", "float", "final", "short",
64     "super", "throw", "while", "double", "import", "native", "public",
65     "return", "static", "switch", "throws", "boolean", "default",
66     "extends", "finally", "package", "private", "abstract", "continue",
67     "volatile", "interface", "protected", "transient", "implements",
68     "instanceof", "synchronized"
69   };
70   private String JavaDoc[] firstLetterLower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
71   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
72   "y", "z"};
73   private String JavaDoc[] nextLetterLower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
74   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
75   "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
76   private String JavaDoc[] firstLetterAll = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
77   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
78   "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
79   "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
80   "W", "X", "Y", "Z"};
81   private String JavaDoc[] nextLetterAll = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
82   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
83   "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
84   "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
85   "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
86   
87   
88   
89   
90   /** Ctor. */
91   public KeywordNameMaker() {
92     this(null);
93   }
94   
95   
96   /** Ctor - block names not to be obfuscated from the mapping target space. */
97   public KeywordNameMaker(String JavaDoc[] noObfNames) {
98     this(noObfNames, true);
99   }
100   
101   
102   /** Ctor - block names not to be obfuscated from the mapping target space. */
103   public KeywordNameMaker(String JavaDoc[] noObfNames, boolean useKeywords) {
104     this(noObfNames, true, false);
105   }
106   
107   
108   /** Ctor - block names not to be obfuscated from the mapping target space. */
109   public KeywordNameMaker(String JavaDoc[] noObfNames, boolean useKeywords, boolean lowerCaseOnly) {
110     this.noObfNames = noObfNames == null ? new String JavaDoc[0] : noObfNames;
111     if (useKeywords) {
112       keywordsToUse = someKeywords;
113       keywordsToExclude = someKeywords;
114     } else {
115       keywordsToUse = noKeywords;
116       keywordsToExclude = allKeywords;
117     }
118     if (lowerCaseOnly) {
119       firstLetter = firstLetterLower;
120       nextLetter = nextLetterLower;
121     } else {
122       firstLetter = firstLetterAll;
123       nextLetter = nextLetterAll;
124     }
125   }
126   
127   
128   /** Return the next unique name for this namespace. */
129   protected String JavaDoc getNextName(String JavaDoc descriptor) {
130     // Check for arg-list in hashtable
131
String JavaDoc argList = DUMMY_ARG_LIST;
132     if (null != descriptor) {
133       argList = getArgList(descriptor);
134     }
135     Integer JavaDoc intCount = (Integer JavaDoc)argCount.get(argList);
136     int theCount = 0;
137     if (null == intCount) {
138       argCount.put(argList, new Integer JavaDoc(theCount));
139     } else {
140       theCount = intCount.intValue() + 1;
141       argCount.remove(argList);
142       argCount.put(argList, new Integer JavaDoc(theCount));
143     }
144     return getName(theCount);
145   }
146   
147   
148   // Extract the arg-list from a descriptor
149
private String JavaDoc getArgList(String JavaDoc descriptor) {
150     int pos = descriptor.indexOf(')');
151     return descriptor.substring(1, pos);
152   }
153   
154   
155   // Generate i'th allowed, unique name
156
private String JavaDoc getName(int index) {
157     // If we have previously computed this name, just return it
158
String JavaDoc name = null;
159     if (index < namesToDate.size()) {
160       name = (String JavaDoc)namesToDate.elementAt(index);
161     } else {
162       // Generate a new valid name for the sequence
163
for (;;) {
164         name = getNewName(index + skipped);
165         if (!Tools.isInArray(name, noObfNames) &&
166         (index + skipped < keywordsToUse.length ||
167         !Tools.isInArray(name, keywordsToExclude))) {
168           break;
169         }
170         skipped++;
171       }
172       namesToDate.addElement(name);
173     }
174     return name;
175   }
176   
177   
178   // Generate j'th name in sequence (can repeat keywords)
179
private String JavaDoc getNewName(int index) {
180     String JavaDoc name = null;
181     
182     // Check if we are in the 'keyword' part of the namespace
183
if (index < keywordsToUse.length) {
184       name = keywordsToUse[index];
185     } else {
186       // Check if we are in the single letter part of the namespace
187
index -= keywordsToUse.length;
188       if (index < firstLetter.length) {
189         name = firstLetter[index];
190       } else {
191         // We are in the >=2 letter part of namespace
192
index -= firstLetter.length;
193         int nextLetters = 1;
194         int subspaceSize = nextLetter.length;
195         while (index >= firstLetter.length * subspaceSize) {
196           index -= firstLetter.length * subspaceSize;
197           nextLetters++;
198           subspaceSize *= nextLetter.length;
199         }
200         
201         // Pull out the name
202
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(firstLetter[index / subspaceSize]);
203         while (subspaceSize != 1) {
204           index %= subspaceSize;
205           subspaceSize /= nextLetter.length;
206           sb.append(nextLetter[index / subspaceSize]);
207         }
208         
209         // Check for collision with keywords
210
name = sb.toString();
211       }
212     }
213     return name;
214   }
215 }
216
Popular Tags