| 1 26 package com.yworks.yguard.obf; 27 28 import java.io.*; 29 import java.util.*; 30 31 38 public class KeywordNameMaker implements NameMaker 39 { 40 private static final String DUMMY_ARG_LIST = "dummy"; 42 43 44 private int skipped = 0; private Vector namesToDate = new Vector(); 47 private Hashtable argCount = new Hashtable(); 48 private String [] noObfNames = null; private String [] keywordsToUse; 50 private String [] keywordsToExclude; 51 private String [] firstLetter; 52 private String [] nextLetter; 53 private String [] noKeywords = {}; 54 private String [] someKeywords = { 55 "a", "if", "do", "for", "int", "new", "try", "byte", "case", "char", 56 "else", "goto", "long", "null", "void" 57 }; 58 private String [] allKeywords = { 59 "if", "do", "for", "int", "new", "try", "byte", "case", "char", 60 "else", "goto", "long", "null", "this", "void", "true", "false", 61 "break", "catch", "class", "const", "float", "final", "short", 62 "super", "throw", "while", "double", "import", "native", "public", 63 "return", "static", "switch", "throws", "boolean", "default", 64 "extends", "finally", "package", "private", "abstract", "continue", 65 "volatile", "interface", "protected", "transient", "implements", 66 "instanceof", "synchronized" 67 }; 68 private String [] firstLetterLower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 69 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 70 "y", "z"}; 71 private String [] nextLetterLower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 72 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 73 "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 74 private String [] firstLetterAll = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 75 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 76 "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 77 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 78 "W", "X", "Y", "Z"}; 79 private String [] nextLetterAll = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 80 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 81 "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 82 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 83 "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 84 85 86 88 107 108 110 public KeywordNameMaker() 111 { 112 this(null); 113 } 114 115 116 public KeywordNameMaker(String [] noObfNames) 117 { 118 this(noObfNames, true); 119 } 120 121 122 public KeywordNameMaker(String [] noObfNames, boolean useKeywords) 123 { 124 this(noObfNames, true, false); 125 } 126 127 128 public KeywordNameMaker(String [] noObfNames, boolean useKeywords, boolean lowerCaseOnly) 129 { 130 this.noObfNames = noObfNames == null ? new String [0] : noObfNames; 131 if (useKeywords) 132 { 133 keywordsToUse = someKeywords; 134 keywordsToExclude = someKeywords; 135 } 136 else 137 { 138 keywordsToUse = noKeywords; 139 keywordsToExclude = allKeywords; 140 } 141 if (lowerCaseOnly) 142 { 143 firstLetter = firstLetterLower; 144 nextLetter = nextLetterLower; 145 } 146 else 147 { 148 firstLetter = firstLetterAll; 149 nextLetter = nextLetterAll; 150 } 151 } 152 153 154 public String nextName(String descriptor) 155 { 156 String argList = DUMMY_ARG_LIST; 158 if (descriptor != null) 159 { 160 argList = getArgList(descriptor); 161 } 162 Integer intCount = (Integer )argCount.get(argList); 163 int theCount = 0; 164 if (intCount == null) 165 { 166 argCount.put(argList, new Integer (theCount)); 167 } 168 else 169 { 170 theCount = intCount.intValue() + 1; 171 argCount.remove(argList); 172 argCount.put(argList, new Integer (theCount)); 173 } 174 return getName(theCount); 175 } 176 177 private String getArgList(String descriptor) 179 { 180 int pos = descriptor.indexOf(')'); 181 return descriptor.substring(1, pos); 182 } 183 184 private String getName(int index) 186 { 187 String name = null; 189 if (index < namesToDate.size()) 190 { 191 name = (String )namesToDate.elementAt(index); 192 } 193 else 194 { 195 for (;;) 197 { 198 name = getNewName(index + skipped); 199 if (!Tools.isInArray(name, noObfNames) && 200 (index + skipped < keywordsToUse.length || 201 !Tools.isInArray(name, keywordsToExclude))) 202 { 203 break; 204 } 205 skipped++; 206 } 207 namesToDate.addElement(name); 208 } 209 return name; 210 } 211 212 private String getNewName(int index) 214 { 215 String name = null; 216 217 if (index < keywordsToUse.length) 219 { 220 name = keywordsToUse[index]; 221 } 222 else 223 { 224 index -= keywordsToUse.length; 226 if (index < firstLetter.length) 227 { 228 name = firstLetter[index]; 229 } 230 else 231 { 232 index -= firstLetter.length; 234 int nextLetters = 1; 235 int subspaceSize = nextLetter.length; 236 while (index >= firstLetter.length * subspaceSize) 237 { 238 index -= firstLetter.length * subspaceSize; 239 nextLetters++; 240 subspaceSize *= nextLetter.length; 241 } 242 243 StringBuffer sb = new StringBuffer (firstLetter[index / subspaceSize]); 245 while (subspaceSize != 1) 246 { 247 index %= subspaceSize; 248 subspaceSize /= nextLetter.length; 249 sb.append(nextLetter[index / subspaceSize]); 250 } 251 252 name = sb.toString(); 254 } 255 } 256 return name; 257 } 258 } 259 260 | Popular Tags |