├── README.md ├── build.gradle └── src └── main └── java ├── gnu └── getopt │ ├── Getopt.java │ ├── LongOpt.java │ ├── MessagesBundle.properties │ ├── MessagesBundle_chs.properties │ ├── MessagesBundle_cht.properties │ ├── MessagesBundle_cs.properties │ ├── MessagesBundle_de.properties │ ├── MessagesBundle_es.properties │ ├── MessagesBundle_fr.properties │ ├── MessagesBundle_hu.properties │ ├── MessagesBundle_it.properties │ ├── MessagesBundle_ja.properties │ ├── MessagesBundle_nl.properties │ ├── MessagesBundle_no.properties │ ├── MessagesBundle_pl.properties │ └── MessagesBundle_ro.properties └── javafuzz ├── FindSubs.txt ├── Helper.java └── JavaFuzz.java /README.md: -------------------------------------------------------------------------------- 1 | JavaFuzz Android 2 | ================ 3 | 4 | 5 | Android JavaFuzz Version. In order to compile type: 6 | 7 | gradle build 8 | 9 | Instrument Android APK 10 | ---------------------- 11 | 12 | In order to instrument it you need: 13 | 14 | - smali 15 | - zip/unzip 16 | - aapt 17 | 18 | Once you have obtained the javafuzz.jar you have to dex it: 19 | 20 | dx --dex --output=afuz.dex build/libs/javafuzz-1.0.jar 21 | 22 | Uncompress it to obtain the necessary java xml resources: 23 | 24 | unzip build/libs/javafuzz-1.0.jar 25 | 26 | Then uncompress the apk you need to instruments in the same directory: 27 | 28 | unzip your.apk 29 | 30 | Instrument the code: 31 | 32 | baksmali classes.dex 33 | baksmali afuz.dex 34 | smali out 35 | cp out.dex classes.dex 36 | 37 | Then build the final instrumented apk: 38 | 39 | aapt remove your.apk classes.dex 40 | aapt add your.apk classes.dex 41 | aapt add your.apk gnu/getopt/MessagesBundle* 42 | 43 | 44 | Run The Fuzzer 45 | -------------- 46 | 47 | Transfer the instrumented apk to the "device": 48 | 49 | adb push your.apk /sdcard/ 50 | mkdir -p /data/local/tmp 51 | ANDROID_DATA=/data/local/tmp /system/bin/dalvikvm -Xss256k -Xmx100m \ 52 | -Xbootclasspath:/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar \ 53 | -classpath /sdcard/your.apk javafuzz.JavaFuzz -c class.to.Test -v -m 54 | 55 | 56 | Manual 57 | ------ 58 | 59 | NAME 60 | JavaFuzz - Java Class Fuzzer 61 | 62 | SYNOPSIS 63 | JavaFuzz.jar [-v] -c class [-e type] [-l StringLength]... 64 | 65 | DESCRIPTION 66 | JavaFuzz is a java classes fuzzer based on the the Java Reflection 67 | API. The reflection API represents, or reflects, the classes, interfaces, 68 | and objects in the current Java Virtual Machine. Using the reflection 69 | API JavaFuzz can contruct and invoke any given class (or list of classes). 70 | After getting the types that a class accepts will construct the classes using 71 | large values. 72 | 73 | 74 | OPTIONS 75 | -v Verbose - Fully Print Exceptions. Very usuafull and you better use 76 | that if you want to spot any weird exceptions. 77 | 78 | -m Fuzz methods of a Class, Can take Long time to finish 79 | 80 | -c Classname 81 | Input is Class name e.g java.net.URL , you cannot use -f at the same 82 | time. 83 | 84 | -f Read Class names from a file. Classnames should be on in each line. 85 | 86 | -s You can set the fuzzing String, for example http://www.example.com 87 | if you dont want repeats, use it with -l1 88 | 89 | -e You can set the type you want to overflow with the MAX_VALUE on top 90 | for example if you want to pass twice the size of a double to a class 91 | which is defined to accept only double you do "-e double" 92 | Warning: If you do that with an integer it will overflow and 93 | become -2. 94 | Values can be : int, double, float, long, short. 95 | 96 | -r Number of recursions until constructs the class [Default 20] 97 | If needs more it will set type to null and consider it Infinite. 98 | Usually when trying to construct types that dont get any arguments 99 | it will be fine, if it will try to construct classes that their 100 | types accept arguments and so on... JavaFuzz will keep constructing 101 | types until it gets the asked types. 102 | 103 | -k Set the value for int,float,long,short,double 104 | e.g. -k int=100 or -k double=20000 or -k int=19,float=49 and so on. 105 | 106 | -a Set size of used array when fuzzing [Default 800] 107 | This option can be maximum Integer.MAX_VALUE 108 | 109 | -l Set length of used Strings when fuzzing [Default 1024] 110 | This option can be maximum Integer.MAX_VALUE 111 | 112 | -o Find if a specific class requires a cosntant and brute-force 113 | all possible possitions until the constant is in the correct 114 | positiont. [This option will add further delays] 115 | 116 | -i JavaFuzz will ignore the specified method(s) helpful when you found a bug 117 | in a method but you want to dig deeper. (Seperate methods with commas) 118 | e.g. for java.awt.Image you could use -i getGraphics,getScaledInstance 119 | 120 | -n JavaFuzz will fuzz the specified method(s) only 121 | e.g. for java.awt.Font you could use -n applySize,pDispose 122 | NOTE: You cannot use -i at the same time 123 | 124 | -u Fuzz only high or low values respectively e.g. Integer high is +MAX_VALUE 125 | and low value is -MAX_VALUE (or MIN_VALUE) [-u low or -u high] 126 | 127 | - p Enforce a Constant and bruteforce the position. Thetype can 128 | be int,double,float,short,string e.g. -p double=1 129 | 130 | -g Use it when you want to replace a class, for example it could be used to replace 131 | abstract classes or interfaces -g org.replace.this=org.with.this 132 | the auto replacement mode can be invoked using -g org.replace.this={A} 133 | and for complete automation use -ga 134 | 135 | 136 | 137 | EXAMPLES 138 | 139 | java -jar JavaFuzz.jar -c java.lang.String -v 140 | java -jar JavaFuzz.jar -f classes.txt -v -e int 141 | java -jar JavaFuzz.jar -c java.net.URL -e int -s http://www.example.com 142 | 143 | BUGS 144 | Version <= 0.3 145 | It cannot construct classes with types : 146 | a) Multidimensional array that is not int,double,float,short,long,string 147 | b) Array that is not int,double,float,short,long,string 148 | 149 | FIXES/UPDATES 150 | Version >= 0.7 151 | Enforce a Constant and bruteforce the position. The type can be int,double,float,short,string 152 | flag is -p 153 | 154 | Version >= 0.6 155 | You can filter in and out method(s) and you can supply multiple types with -k 156 | Minor error handling fixes 157 | 158 | Version >= 0.5 159 | The bugs listed in <=0.3 are fixed. If you find the same problem let me know 160 | 161 | AUTHOR 162 | Emmanouel Kellinis 163 | 164 | 165 | License 166 | ------- 167 | 168 | The code is under GPLv2 unless specified otherwise in the single files. -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | version = '1.0' 4 | 5 | jar { 6 | from {configurations.compile.collect {zipTree(it)}} 7 | manifest { 8 | attributes 'Main-Class': 'javafuzz.JavaFuzz' 9 | } 10 | doFirst { 11 | from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 12 | } 13 | exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 14 | sourceSets.main.resources.srcDirs = ["src/main/java"] 15 | } 16 | 17 | repositories { 18 | mavenCentral() 19 | } 20 | 21 | test { 22 | systemProperties 'property': 'value' 23 | } -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/Getopt.java: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* Getopt.java -- Java port of GNU getopt from glibc 2.0.6 3 | /* 4 | /* Copyright (c) 1987-1997 Free Software Foundation, Inc. 5 | /* Java Port Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com) 6 | /* 7 | /* This program is free software; you can redistribute it and/or modify 8 | /* it under the terms of the GNU Library General Public License as published 9 | /* by the Free Software Foundation; either version 2 of the License or 10 | /* (at your option) any later version. 11 | /* 12 | /* This program is distributed in the hope that it will be useful, but 13 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | /* GNU Library General Public License for more details. 16 | /* 17 | /* You should have received a copy of the GNU Library General Public License 18 | /* along with this program; see the file COPYING.LIB. If not, write to 19 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 20 | /* Boston, MA 02111-1307 USA 21 | /**************************************************************************/ 22 | 23 | package gnu.getopt; 24 | 25 | import java.util.Locale; 26 | import java.util.ResourceBundle; 27 | import java.text.MessageFormat; 28 | 29 | /**************************************************************************/ 30 | 31 | /** 32 | * This is a Java port of GNU getopt, a class for parsing command line 33 | * arguments passed to programs. It it based on the C getopt() functions 34 | * in glibc 2.0.6 and should parse options in a 100% compatible manner. 35 | * If it does not, that is a bug. The programmer's interface is also 36 | * very compatible. 37 | *

38 | * To use Getopt, create a Getopt object with a argv array passed to the 39 | * main method, then call the getopt() method in a loop. It will return an 40 | * int that contains the value of the option character parsed from the 41 | * command line. When there are no more options to be parsed, it 42 | * returns -1. 43 | *

44 | * A command line option can be defined to take an argument. If an 45 | * option has an argument, the value of that argument is stored in an 46 | * instance variable called optarg, which can be accessed using the 47 | * getOptarg() method. If an option that requires an argument is 48 | * found, but there is no argument present, then an error message is 49 | * printed. Normally getopt() returns a '?' in this situation, but 50 | * that can be changed as described below. 51 | *

52 | * If an invalid option is encountered, an error message is printed 53 | * to the standard error and getopt() returns a '?'. The value of the 54 | * invalid option encountered is stored in the instance variable optopt 55 | * which can be retrieved using the getOptopt() method. To suppress 56 | * the printing of error messages for this or any other error, set 57 | * the value of the opterr instance variable to false using the 58 | * setOpterr() method. 59 | *

60 | * Between calls to getopt(), the instance variable optind is used to 61 | * keep track of where the object is in the parsing process. After all 62 | * options have been returned, optind is the index in argv of the first 63 | * non-option argument. This variable can be accessed with the getOptind() 64 | * method. 65 | *

66 | * Note that this object expects command line options to be passed in the 67 | * traditional Unix manner. That is, proceeded by a '-' character. 68 | * Multiple options can follow the '-'. For example "-abc" is equivalent 69 | * to "-a -b -c". If an option takes a required argument, the value 70 | * of the argument can immediately follow the option character or be 71 | * present in the next argv element. For example, "-cfoo" and "-c foo" 72 | * both represent an option character of 'c' with an argument of "foo" 73 | * assuming c takes a required argument. If an option takes an argument 74 | * that is not required, then any argument must immediately follow the 75 | * option character in the same argv element. For example, if c takes 76 | * a non-required argument, then "-cfoo" represents option character 'c' 77 | * with an argument of "foo" while "-c foo" represents the option 78 | * character 'c' with no argument, and a first non-option argv element 79 | * of "foo". 80 | *

81 | * The user can stop getopt() from scanning any further into a command line 82 | * by using the special argument "--" by itself. For example: 83 | * "-a -- -d" would return an option character of 'a', then return -1 84 | * The "--" is discarded and "-d" is pointed to by optind as the first 85 | * non-option argv element. 86 | *

87 | * Here is a basic example of using Getopt: 88 | *

89 | *

  90 |   * Getopt g = new Getopt("testprog", argv, "ab:c::d");
  91 |   * //
  92 |   * int c;
  93 |   * String arg;
  94 |   * while ((c = g.getopt()) != -1)
  95 |   *   {
  96 |   *     switch(c)
  97 |   *       {
  98 |   *          case 'a':
  99 |   *          case 'd':
 100 |   *            System.out.print("You picked " + (char)c + "\n");
 101 |   *            break;
 102 |   *            //
 103 |   *          case 'b':
 104 |   *          case 'c':
 105 |   *            arg = g.getOptarg();
 106 |   *            System.out.print("You picked " + (char)c + 
 107 |   *                             " with an argument of " +
 108 |   *                             ((arg != null) ? arg : "null") + "\n");
 109 |   *            break;
 110 |   *            //
 111 |   *          case '?':
 112 |   *            break; // getopt() already printed an error
 113 |   *            //
 114 |   *          default:
 115 |   *            System.out.print("getopt() returned " + c + "\n");
 116 |   *       }
 117 |   *   }
 118 |   * 
119 | *

120 | * In this example, a new Getopt object is created with three params. 121 | * The first param is the program name. This is for printing error 122 | * messages in the form "program: error message". In the C version, this 123 | * value is taken from argv[0], but in Java the program name is not passed 124 | * in that element, thus the need for this parameter. The second param is 125 | * the argument list that was passed to the main() method. The third 126 | * param is the list of valid options. Each character represents a valid 127 | * option. If the character is followed by a single colon, then that 128 | * option has a required argument. If the character is followed by two 129 | * colons, then that option has an argument that is not required. 130 | *

131 | * Note in this example that the value returned from getopt() is cast to 132 | * a char prior to printing. This is required in order to make the value 133 | * display correctly as a character instead of an integer. 134 | *

135 | * If the first character in the option string is a colon, for example 136 | * ":abc::d", then getopt() will return a ':' instead of a '?' when it 137 | * encounters an option with a missing required argument. This allows the 138 | * caller to distinguish between invalid options and valid options that 139 | * are simply incomplete. 140 | *

141 | * In the traditional Unix getopt(), -1 is returned when the first non-option 142 | * charcter is encountered. In GNU getopt(), the default behavior is to 143 | * allow options to appear anywhere on the command line. The getopt() 144 | * method permutes the argument to make it appear to the caller that all 145 | * options were at the beginning of the command line, and all non-options 146 | * were at the end. For example, calling getopt() with command line args 147 | * of "-a foo bar -d" returns options 'a' and 'd', then sets optind to 148 | * point to "foo". The program would read the last two argv elements as 149 | * "foo" and "bar", just as if the user had typed "-a -d foo bar". 150 | *

151 | * The user can force getopt() to stop scanning the command line with 152 | * the special argument "--" by itself. Any elements occuring before the 153 | * "--" are scanned and permuted as normal. Any elements after the "--" 154 | * are returned as is as non-option argv elements. For example, 155 | * "foo -a -- bar -d" would return option 'a' then -1. optind would point 156 | * to "foo", "bar" and "-d" as the non-option argv elements. The "--" 157 | * is discarded by getopt(). 158 | *

159 | * There are two ways this default behavior can be modified. The first is 160 | * to specify traditional Unix getopt() behavior (which is also POSIX 161 | * behavior) in which scanning stops when the first non-option argument 162 | * encountered. (Thus "-a foo bar -d" would return 'a' as an option and 163 | * have "foo", "bar", and "-d" as non-option elements). The second is to 164 | * allow options anywhere, but to return all elements in the order they 165 | * occur on the command line. When a non-option element is ecountered, 166 | * an integer 1 is returned and the value of the non-option element is 167 | * stored in optarg is if it were the argument to that option. For 168 | * example, "-a foo -d", returns first 'a', then 1 (with optarg set to 169 | * "foo") then 'd' then -1. When this "return in order" functionality 170 | * is enabled, the only way to stop getopt() from scanning all command 171 | * line elements is to use the special "--" string by itself as described 172 | * above. An example is "-a foo -b -- bar", which would return 'a', then 173 | * integer 1 with optarg set to "foo", then 'b', then -1. optind would 174 | * then point to "bar" as the first non-option argv element. The "--" 175 | * is discarded. 176 | *

177 | * The POSIX/traditional behavior is enabled by either setting the 178 | * property "gnu.posixly_correct" or by putting a '+' sign as the first 179 | * character of the option string. The difference between the two 180 | * methods is that setting the gnu.posixly_correct property also forces 181 | * certain error messages to be displayed in POSIX format. To enable 182 | * the "return in order" functionality, put a '-' as the first character 183 | * of the option string. Note that after determining the proper 184 | * behavior, Getopt strips this leading '+' or '-', meaning that a ':' 185 | * placed as the second character after one of those two will still cause 186 | * getopt() to return a ':' instead of a '?' if a required option 187 | * argument is missing. 188 | *

189 | * In addition to traditional single character options, GNU Getopt also 190 | * supports long options. These are preceeded by a "--" sequence and 191 | * can be as long as desired. Long options provide a more user-friendly 192 | * way of entering command line options. For example, in addition to a 193 | * "-h" for help, a program could support also "--help". 194 | *

195 | * Like short options, long options can also take a required or non-required 196 | * argument. Required arguments can either be specified by placing an 197 | * equals sign after the option name, then the argument, or by putting the 198 | * argument in the next argv element. For example: "--outputdir=foo" and 199 | * "--outputdir foo" both represent an option of "outputdir" with an 200 | * argument of "foo", assuming that outputdir takes a required argument. 201 | * If a long option takes a non-required argument, then the equals sign 202 | * form must be used to specify the argument. In this case, 203 | * "--outputdir=foo" would represent option outputdir with an argument of 204 | * "foo" while "--outputdir foo" would represent the option outputdir 205 | * with no argument and a first non-option argv element of "foo". 206 | *

207 | * Long options can also be specified using a special POSIX argument 208 | * format (one that I highly discourage). This form of entry is 209 | * enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid 210 | * option string. This causes getopt to treat the name following the 211 | * "-W" as the name of the long option. For example, "-W outputdir=foo" 212 | * would be equivalent to "--outputdir=foo". The name can immediately 213 | * follow the "-W" like so: "-Woutputdir=foo". Option arguments are 214 | * handled identically to normal long options. If a string follows the 215 | * "-W" that does not represent a valid long option, then getopt() returns 216 | * 'W' and the caller must decide what to do. Otherwise getopt() returns 217 | * a long option value as described below. 218 | *

219 | * While long options offer convenience, they can also be tedious to type 220 | * in full. So it is permissible to abbreviate the option name to as 221 | * few characters as required to uniquely identify it. If the name can 222 | * represent multiple long options, then an error message is printed and 223 | * getopt() returns a '?'. 224 | *

225 | * If an invalid option is specified or a required option argument is 226 | * missing, getopt() prints an error and returns a '?' or ':' exactly 227 | * as for short options. Note that when an invalid long option is 228 | * encountered, the optopt variable is set to integer 0 and so cannot 229 | * be used to identify the incorrect option the user entered. 230 | *

231 | * Long options are defined by LongOpt objects. These objects are created 232 | * with a contructor that takes four params: a String representing the 233 | * object name, a integer specifying what arguments the option takes 234 | * (the value is one of LongOpt.NO_ARGUMENT, LongOpt.REQUIRED_ARGUMENT, 235 | * or LongOpt.OPTIONAL_ARGUMENT), a StringBuffer flag object (described 236 | * below), and an integer value (described below). 237 | *

238 | * To enable long option parsing, create an array of LongOpt's representing 239 | * the legal options and pass it to the Getopt() constructor. WARNING: If 240 | * all elements of the array are not populated with LongOpt objects, the 241 | * getopt() method will throw a NullPointerException. 242 | *

243 | * When getopt() is called and a long option is encountered, one of two 244 | * things can be returned. If the flag field in the LongOpt object 245 | * representing the long option is non-null, then the integer value field 246 | * is stored there and an integer 0 is returned to the caller. The val 247 | * field can then be retrieved from the flag field. Note that since the 248 | * flag field is a StringBuffer, the appropriate String to integer converions 249 | * must be performed in order to get the actual int value stored there. 250 | * If the flag field in the LongOpt object is null, then the value field 251 | * of the LongOpt is returned. This can be the character of a short option. 252 | * This allows an app to have both a long and short option sequence 253 | * (say, "-h" and "--help") that do the exact same thing. 254 | *

255 | * With long options, there is an alternative method of determining 256 | * which option was selected. The method getLongind() will return the 257 | * the index in the long option array (NOT argv) of the long option found. 258 | * So if multiple long options are configured to return the same value, 259 | * the application can use getLongind() to distinguish between them. 260 | *

261 | * Here is an expanded Getopt example using long options and various 262 | * techniques described above: 263 | *

264 | *

 265 |   * int c;
 266 |   * String arg;
 267 |   * LongOpt[] longopts = new LongOpt[3];
 268 |   * // 
 269 |   * StringBuffer sb = new StringBuffer();
 270 |   * longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
 271 |   * longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o'); 
 272 |   * longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
 273 |   * // 
 274 |   * Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
 275 |   * g.setOpterr(false); // We'll do our own error handling
 276 |   * //
 277 |   * while ((c = g.getopt()) != -1)
 278 |   *   switch (c)
 279 |   *     {
 280 |   *        case 0:
 281 |   *          arg = g.getOptarg();
 282 |   *          System.out.println("Got long option with value '" +
 283 |   *                             (char)(new Integer(sb.toString())).intValue()
 284 |   *                             + "' with argument " +
 285 |   *                             ((arg != null) ? arg : "null"));
 286 |   *          break;
 287 |   *          //
 288 |   *        case 1:
 289 |   *          System.out.println("I see you have return in order set and that " +
 290 |   *                             "a non-option argv element was just found " +
 291 |   *                             "with the value '" + g.getOptarg() + "'");
 292 |   *          break;
 293 |   *          //
 294 |   *        case 2:
 295 |   *          arg = g.getOptarg();
 296 |   *          System.out.println("I know this, but pretend I didn't");
 297 |   *          System.out.println("We picked option " +
 298 |   *                             longopts[g.getLongind()].getName() +
 299 |   *                           " with value " + 
 300 |   *                           ((arg != null) ? arg : "null"));
 301 |   *          break;
 302 |   *          //
 303 |   *        case 'b':
 304 |   *          System.out.println("You picked plain old option " + (char)c);
 305 |   *          break;
 306 |   *          //
 307 |   *        case 'c':
 308 |   *        case 'd':
 309 |   *          arg = g.getOptarg();
 310 |   *          System.out.println("You picked option '" + (char)c + 
 311 |   *                             "' with argument " +
 312 |   *                             ((arg != null) ? arg : "null"));
 313 |   *          break;
 314 |   *          //
 315 |   *        case 'h':
 316 |   *          System.out.println("I see you asked for help");
 317 |   *          break;
 318 |   *          //
 319 |   *        case 'W':
 320 |   *          System.out.println("Hmmm. You tried a -W with an incorrect long " +
 321 |   *                             "option name");
 322 |   *          break;
 323 |   *          //
 324 |   *        case ':':
 325 |   *          System.out.println("Doh! You need an argument for option " +
 326 |   *                             (char)g.getOptopt());
 327 |   *          break;
 328 |   *          //
 329 |   *        case '?':
 330 |   *          System.out.println("The option '" + (char)g.getOptopt() + 
 331 |   *                           "' is not valid");
 332 |   *          break;
 333 |   *          //
 334 |   *        default:
 335 |   *          System.out.println("getopt() returned " + c);
 336 |   *          break;
 337 |   *     }
 338 |   * //
 339 |   * for (int i = g.getOptind(); i < argv.length ; i++)
 340 |   *   System.out.println("Non option argv element: " + argv[i] + "\n");
 341 |   * 
342 | *

343 | * There is an alternative form of the constructor used for long options 344 | * above. This takes a trailing boolean flag. If set to false, Getopt 345 | * performs identically to the example, but if the boolean flag is true 346 | * then long options are allowed to start with a single '-' instead of 347 | * "--". If the first character of the option is a valid short option 348 | * character, then the option is treated as if it were the short option. 349 | * Otherwise it behaves as if the option is a long option. Note that 350 | * the name given to this option - long_only - is very counter-intuitive. 351 | * It does not cause only long options to be parsed but instead enables 352 | * the behavior described above. 353 | *

354 | * Note that the functionality and variable names used are driven from 355 | * the C lib version as this object is a port of the C code, not a 356 | * new implementation. This should aid in porting existing C/C++ code, 357 | * as well as helping programmers familiar with the glibc version to 358 | * adapt to the Java version even if it seems very non-Java at times. 359 | *

360 | * In this release I made all instance variables protected due to 361 | * overwhelming public demand. Any code which relied on optarg, 362 | * opterr, optind, or optopt being public will need to be modified to 363 | * use the appropriate access methods. 364 | *

365 | * Please send all bug reports, requests, and comments to 366 | * arenn@urbanophile.com. 367 | * 368 | * @version 1.0.7 369 | * 370 | * @author Roland McGrath (roland@gnu.ai.mit.edu) 371 | * @author Ulrich Drepper (drepper@cygnus.com) 372 | * @author Aaron M. Renn (arenn@urbanophile.com) 373 | * 374 | * @see LongOpt 375 | */ 376 | public class Getopt extends Object 377 | { 378 | 379 | /**************************************************************************/ 380 | 381 | /* 382 | * Class Variables 383 | */ 384 | 385 | /** 386 | * Describe how to deal with options that follow non-option ARGV-elements. 387 | * 388 | * If the caller did not specify anything, 389 | * the default is REQUIRE_ORDER if the property 390 | * gnu.posixly_correct is defined, PERMUTE otherwise. 391 | * 392 | * The special argument `--' forces an end of option-scanning regardless 393 | * of the value of `ordering'. In the case of RETURN_IN_ORDER, only 394 | * `--' can cause `getopt' to return -1 with `optind' != ARGC. 395 | * 396 | * REQUIRE_ORDER means don't recognize them as options; 397 | * stop option processing when the first non-option is seen. 398 | * This is what Unix does. 399 | * This mode of operation is selected by either setting the property 400 | * gnu.posixly_correct, or using `+' as the first character 401 | * of the list of option characters. 402 | */ 403 | protected static final int REQUIRE_ORDER = 1; 404 | 405 | /** 406 | * PERMUTE is the default. We permute the contents of ARGV as we scan, 407 | * so that eventually all the non-options are at the end. This allows options 408 | * to be given in any order, even with programs that were not written to 409 | * expect this. 410 | */ 411 | protected static final int PERMUTE = 2; 412 | 413 | /** 414 | * RETURN_IN_ORDER is an option available to programs that were written 415 | * to expect options and other ARGV-elements in any order and that care about 416 | * the ordering of the two. We describe each non-option ARGV-element 417 | * as if it were the argument of an option with character code 1. 418 | * Using `-' as the first character of the list of option characters 419 | * selects this mode of operation. 420 | */ 421 | protected static final int RETURN_IN_ORDER = 3; 422 | 423 | /**************************************************************************/ 424 | 425 | /* 426 | * Instance Variables 427 | */ 428 | 429 | /** 430 | * For communication from `getopt' to the caller. 431 | * When `getopt' finds an option that takes an argument, 432 | * the argument value is returned here. 433 | * Also, when `ordering' is RETURN_IN_ORDER, 434 | * each non-option ARGV-element is returned here. 435 | */ 436 | protected String optarg; 437 | 438 | /** 439 | * Index in ARGV of the next element to be scanned. 440 | * This is used for communication to and from the caller 441 | * and for communication between successive calls to `getopt'. 442 | * 443 | * On entry to `getopt', zero means this is the first call; initialize. 444 | * 445 | * When `getopt' returns -1, this is the index of the first of the 446 | * non-option elements that the caller should itself scan. 447 | * 448 | * Otherwise, `optind' communicates from one call to the next 449 | * how much of ARGV has been scanned so far. 450 | */ 451 | protected int optind = 0; 452 | 453 | /** 454 | * Callers store false here to inhibit the error message 455 | * for unrecognized options. 456 | */ 457 | protected boolean opterr = true; 458 | 459 | /** 460 | * When an unrecognized option is encountered, getopt will return a '?' 461 | * and store the value of the invalid option here. 462 | */ 463 | protected int optopt = '?'; 464 | 465 | /** 466 | * The next char to be scanned in the option-element 467 | * in which the last option character we returned was found. 468 | * This allows us to pick up the scan where we left off. 469 | * 470 | * If this is zero, or a null string, it means resume the scan 471 | * by advancing to the next ARGV-element. 472 | */ 473 | protected String nextchar; 474 | 475 | /** 476 | * This is the string describing the valid short options. 477 | */ 478 | protected String optstring; 479 | 480 | /** 481 | * This is an array of LongOpt objects which describ the valid long 482 | * options. 483 | */ 484 | protected LongOpt[] long_options; 485 | 486 | /** 487 | * This flag determines whether or not we are parsing only long args 488 | */ 489 | protected boolean long_only; 490 | 491 | /** 492 | * Stores the index into the long_options array of the long option found 493 | */ 494 | protected int longind; 495 | 496 | /** 497 | * The flag determines whether or not we operate in strict POSIX compliance 498 | */ 499 | protected boolean posixly_correct; 500 | 501 | /** 502 | * A flag which communicates whether or not checkLongOption() did all 503 | * necessary processing for the current option 504 | */ 505 | protected boolean longopt_handled; 506 | 507 | /** 508 | * The index of the first non-option in argv[] 509 | */ 510 | protected int first_nonopt = 1; 511 | 512 | /** 513 | * The index of the last non-option in argv[] 514 | */ 515 | protected int last_nonopt = 1; 516 | 517 | /** 518 | * Flag to tell getopt to immediately return -1 the next time it is 519 | * called. 520 | */ 521 | private boolean endparse = false; 522 | 523 | /** 524 | * Saved argument list passed to the program 525 | */ 526 | protected String[] argv; 527 | 528 | /** 529 | * Determines whether we permute arguments or not 530 | */ 531 | protected int ordering; 532 | 533 | /** 534 | * Name to print as the program name in error messages. This is necessary 535 | * since Java does not place the program name in argv[0] 536 | */ 537 | protected String progname; 538 | 539 | /** 540 | * The localized strings are kept in a separate file 541 | */ 542 | private ResourceBundle _messages = ResourceBundle.getBundle( 543 | "gnu/getopt/MessagesBundle", Locale.getDefault()); 544 | 545 | /**************************************************************************/ 546 | 547 | /* 548 | * Constructors 549 | */ 550 | 551 | /** 552 | * Construct a basic Getopt instance with the given input data. Note that 553 | * this handles "short" options only. 554 | * 555 | * @param progname The name to display as the program name when printing errors 556 | * @param argv The String array passed as the command line to the program. 557 | * @param optstring A String containing a description of the valid args for this program 558 | */ 559 | public 560 | Getopt(String progname, String[] argv, String optstring) 561 | { 562 | this(progname, argv, optstring, null, false); 563 | } 564 | 565 | /**************************************************************************/ 566 | 567 | /** 568 | * Construct a Getopt instance with given input data that is capable of 569 | * parsing long options as well as short. 570 | * 571 | * @param progname The name to display as the program name when printing errors 572 | * @param argv The String array passed as the command ilne to the program 573 | * @param optstring A String containing a description of the valid short args for this program 574 | * @param long_options An array of LongOpt objects that describes the valid long args for this program 575 | */ 576 | public 577 | Getopt(String progname, String[] argv, String optstring, 578 | LongOpt[] long_options) 579 | { 580 | this(progname, argv, optstring, long_options, false); 581 | } 582 | 583 | /**************************************************************************/ 584 | 585 | /** 586 | * Construct a Getopt instance with given input data that is capable of 587 | * parsing long options and short options. Contrary to what you might 588 | * think, the flag 'long_only' does not determine whether or not we 589 | * scan for only long arguments. Instead, a value of true here allows 590 | * long arguments to start with a '-' instead of '--' unless there is a 591 | * conflict with a short option name. 592 | * 593 | * @param progname The name to display as the program name when printing errors 594 | * @param argv The String array passed as the command ilne to the program 595 | * @param optstring A String containing a description of the valid short args for this program 596 | * @param long_options An array of LongOpt objects that describes the valid long args for this program 597 | * @param long_only true if long options that do not conflict with short options can start with a '-' as well as '--' 598 | */ 599 | public 600 | Getopt(String progname, String[] argv, String optstring, 601 | LongOpt[] long_options, boolean long_only) 602 | { 603 | if (optstring.length() == 0) 604 | optstring = " "; 605 | 606 | // This function is essentially _getopt_initialize from GNU getopt 607 | this.progname = progname; 608 | this.argv = argv; 609 | this.optstring = optstring; 610 | this.long_options = long_options; 611 | this.long_only = long_only; 612 | 613 | // Check for property "gnu.posixly_correct" to determine whether to 614 | // strictly follow the POSIX standard. This replaces the "POSIXLY_CORRECT" 615 | // environment variable in the C version 616 | if (System.getProperty("gnu.posixly_correct", null) == null) 617 | posixly_correct = false; 618 | else 619 | { 620 | posixly_correct = true; 621 | _messages = ResourceBundle.getBundle("gnu/getopt/MessagesBundle", 622 | Locale.US); 623 | } 624 | 625 | // Determine how to handle the ordering of options and non-options 626 | if (optstring.charAt(0) == '-') 627 | { 628 | ordering = RETURN_IN_ORDER; 629 | if (optstring.length() > 1) 630 | this.optstring = optstring.substring(1); 631 | } 632 | else if (optstring.charAt(0) == '+') 633 | { 634 | ordering = REQUIRE_ORDER; 635 | if (optstring.length() > 1) 636 | this.optstring = optstring.substring(1); 637 | } 638 | else if (posixly_correct) 639 | { 640 | ordering = REQUIRE_ORDER; 641 | } 642 | else 643 | { 644 | ordering = PERMUTE; // The normal default case 645 | } 646 | } 647 | 648 | /**************************************************************************/ 649 | 650 | /* 651 | * Instance Methods 652 | */ 653 | 654 | /** 655 | * In GNU getopt, it is possible to change the string containg valid options 656 | * on the fly because it is passed as an argument to getopt() each time. In 657 | * this version we do not pass the string on every call. In order to allow 658 | * dynamic option string changing, this method is provided. 659 | * 660 | * @param optstring The new option string to use 661 | */ 662 | public void 663 | setOptstring(String optstring) 664 | { 665 | if (optstring.length() == 0) 666 | optstring = " "; 667 | 668 | this.optstring = optstring; 669 | } 670 | 671 | /**************************************************************************/ 672 | 673 | /** 674 | * optind it the index in ARGV of the next element to be scanned. 675 | * This is used for communication to and from the caller 676 | * and for communication between successive calls to `getopt'. 677 | * 678 | * When `getopt' returns -1, this is the index of the first of the 679 | * non-option elements that the caller should itself scan. 680 | * 681 | * Otherwise, `optind' communicates from one call to the next 682 | * how much of ARGV has been scanned so far. 683 | */ 684 | public int 685 | getOptind() 686 | { 687 | return(optind); 688 | } 689 | 690 | /**************************************************************************/ 691 | 692 | /** 693 | * This method allows the optind index to be set manually. Normally this 694 | * is not necessary (and incorrect usage of this method can lead to serious 695 | * lossage), but optind is a public symbol in GNU getopt, so this method 696 | * was added to allow it to be modified by the caller if desired. 697 | * 698 | * @param optind The new value of optind 699 | */ 700 | public void 701 | setOptind(int optind) 702 | { 703 | this.optind = optind; 704 | } 705 | 706 | /**************************************************************************/ 707 | 708 | /** 709 | * Since in GNU getopt() the argument vector is passed back in to the 710 | * function every time, the caller can swap out argv on the fly. Since 711 | * passing argv is not required in the Java version, this method allows 712 | * the user to override argv. Note that incorrect use of this method can 713 | * lead to serious lossage. 714 | * 715 | * @param argv New argument list 716 | */ 717 | public void 718 | setArgv(String[] argv) 719 | { 720 | this.argv = argv; 721 | } 722 | 723 | /**************************************************************************/ 724 | 725 | /** 726 | * For communication from `getopt' to the caller. 727 | * When `getopt' finds an option that takes an argument, 728 | * the argument value is returned here. 729 | * Also, when `ordering' is RETURN_IN_ORDER, 730 | * each non-option ARGV-element is returned here. 731 | * No set method is provided because setting this variable has no effect. 732 | */ 733 | public String 734 | getOptarg() 735 | { 736 | return(optarg); 737 | } 738 | 739 | /**************************************************************************/ 740 | 741 | /** 742 | * Normally Getopt will print a message to the standard error when an 743 | * invalid option is encountered. This can be suppressed (or re-enabled) 744 | * by calling this method. There is no get method for this variable 745 | * because if you can't remember the state you set this to, why should I? 746 | */ 747 | public void 748 | setOpterr(boolean opterr) 749 | { 750 | this.opterr = opterr; 751 | } 752 | 753 | /**************************************************************************/ 754 | 755 | /** 756 | * When getopt() encounters an invalid option, it stores the value of that 757 | * option in optopt which can be retrieved with this method. There is 758 | * no corresponding set method because setting this variable has no effect. 759 | */ 760 | public int 761 | getOptopt() 762 | { 763 | return(optopt); 764 | } 765 | 766 | /**************************************************************************/ 767 | 768 | /** 769 | * Returns the index into the array of long options (NOT argv) representing 770 | * the long option that was found. 771 | */ 772 | public int 773 | getLongind() 774 | { 775 | return(longind); 776 | } 777 | 778 | /**************************************************************************/ 779 | 780 | /** 781 | * Exchange the shorter segment with the far end of the longer segment. 782 | * That puts the shorter segment into the right place. 783 | * It leaves the longer segment in the right place overall, 784 | * but it consists of two parts that need to be swapped next. 785 | * This method is used by getopt() for argument permutation. 786 | */ 787 | protected void 788 | exchange(String[] argv) 789 | { 790 | int bottom = first_nonopt; 791 | int middle = last_nonopt; 792 | int top = optind; 793 | String tem; 794 | 795 | while (top > middle && middle > bottom) 796 | { 797 | if (top - middle > middle - bottom) 798 | { 799 | // Bottom segment is the short one. 800 | int len = middle - bottom; 801 | int i; 802 | 803 | // Swap it with the top part of the top segment. 804 | for (i = 0; i < len; i++) 805 | { 806 | tem = argv[bottom + i]; 807 | argv[bottom + i] = argv[top - (middle - bottom) + i]; 808 | argv[top - (middle - bottom) + i] = tem; 809 | } 810 | // Exclude the moved bottom segment from further swapping. 811 | top -= len; 812 | } 813 | else 814 | { 815 | // Top segment is the short one. 816 | int len = top - middle; 817 | int i; 818 | 819 | // Swap it with the bottom part of the bottom segment. 820 | for (i = 0; i < len; i++) 821 | { 822 | tem = argv[bottom + i]; 823 | argv[bottom + i] = argv[middle + i]; 824 | argv[middle + i] = tem; 825 | } 826 | // Exclude the moved top segment from further swapping. 827 | bottom += len; 828 | } 829 | } 830 | 831 | // Update records for the slots the non-options now occupy. 832 | 833 | first_nonopt += (optind - last_nonopt); 834 | last_nonopt = optind; 835 | } 836 | 837 | /**************************************************************************/ 838 | 839 | /** 840 | * Check to see if an option is a valid long option. Called by getopt(). 841 | * Put in a separate method because this needs to be done twice. (The 842 | * C getopt authors just copy-pasted the code!). 843 | * 844 | * @param longind A buffer in which to store the 'val' field of found LongOpt 845 | * 846 | * @return Various things depending on circumstances 847 | */ 848 | protected int 849 | checkLongOption() 850 | { 851 | LongOpt pfound = null; 852 | int nameend; 853 | boolean ambig; 854 | boolean exact; 855 | 856 | longopt_handled = true; 857 | ambig = false; 858 | exact = false; 859 | longind = -1; 860 | 861 | nameend = nextchar.indexOf("="); 862 | if (nameend == -1) 863 | nameend = nextchar.length(); 864 | 865 | // Test all lnog options for either exact match or abbreviated matches 866 | for (int i = 0; i < long_options.length; i++) 867 | { 868 | if (long_options[i].getName().startsWith(nextchar.substring(0, nameend))) 869 | { 870 | if (long_options[i].getName().equals(nextchar.substring(0, nameend))) 871 | { 872 | // Exact match found 873 | pfound = long_options[i]; 874 | longind = i; 875 | exact = true; 876 | break; 877 | } 878 | else if (pfound == null) 879 | { 880 | // First nonexact match found 881 | pfound = long_options[i]; 882 | longind = i; 883 | } 884 | else 885 | { 886 | // Second or later nonexact match found 887 | ambig = true; 888 | } 889 | } 890 | } // for 891 | 892 | // Print out an error if the option specified was ambiguous 893 | if (ambig && !exact) 894 | { 895 | if (opterr) 896 | { 897 | Object[] msgArgs = { progname, argv[optind] }; 898 | System.err.println(MessageFormat.format( 899 | _messages.getString("getopt.ambigious"), 900 | msgArgs)); 901 | } 902 | 903 | nextchar = ""; 904 | optopt = 0; 905 | ++optind; 906 | 907 | return('?'); 908 | } 909 | 910 | if (pfound != null) 911 | { 912 | ++optind; 913 | 914 | if (nameend != nextchar.length()) 915 | { 916 | if (pfound.has_arg != LongOpt.NO_ARGUMENT) 917 | { 918 | if (nextchar.substring(nameend).length() > 1) 919 | optarg = nextchar.substring(nameend+1); 920 | else 921 | optarg = ""; 922 | } 923 | else 924 | { 925 | if (opterr) 926 | { 927 | // -- option 928 | if (argv[optind - 1].startsWith("--")) 929 | { 930 | Object[] msgArgs = { progname, pfound.name }; 931 | System.err.println(MessageFormat.format( 932 | _messages.getString("getopt.arguments1"), 933 | msgArgs)); 934 | } 935 | // +option or -option 936 | else 937 | { 938 | Object[] msgArgs = { progname, new 939 | Character(argv[optind-1].charAt(0)).toString(), 940 | pfound.name }; 941 | System.err.println(MessageFormat.format( 942 | _messages.getString("getopt.arguments2"), 943 | msgArgs)); 944 | } 945 | } 946 | 947 | nextchar = ""; 948 | optopt = pfound.val; 949 | 950 | return('?'); 951 | } 952 | } // if (nameend) 953 | else if (pfound.has_arg == LongOpt.REQUIRED_ARGUMENT) 954 | { 955 | if (optind < argv.length) 956 | { 957 | optarg = argv[optind]; 958 | ++optind; 959 | } 960 | else 961 | { 962 | if (opterr) 963 | { 964 | Object[] msgArgs = { progname, argv[optind-1] }; 965 | System.err.println(MessageFormat.format( 966 | _messages.getString("getopt.requires"), 967 | msgArgs)); 968 | } 969 | 970 | nextchar = ""; 971 | optopt = pfound.val; 972 | if (optstring.charAt(0) == ':') 973 | return(':'); 974 | else 975 | return('?'); 976 | } 977 | } // else if (pfound) 978 | 979 | nextchar = ""; 980 | 981 | if (pfound.flag != null) 982 | { 983 | pfound.flag.setLength(0); 984 | pfound.flag.append(pfound.val); 985 | 986 | return(0); 987 | } 988 | 989 | return(pfound.val); 990 | } // if (pfound != null) 991 | 992 | longopt_handled = false; 993 | 994 | return(0); 995 | } 996 | 997 | /**************************************************************************/ 998 | 999 | /** 1000 | * This method returns a char that is the current option that has been 1001 | * parsed from the command line. If the option takes an argument, then 1002 | * the internal variable 'optarg' is set which is a String representing 1003 | * the the value of the argument. This value can be retrieved by the 1004 | * caller using the getOptarg() method. If an invalid option is found, 1005 | * an error message is printed and a '?' is returned. The name of the 1006 | * invalid option character can be retrieved by calling the getOptopt() 1007 | * method. When there are no more options to be scanned, this method 1008 | * returns -1. The index of first non-option element in argv can be 1009 | * retrieved with the getOptind() method. 1010 | * 1011 | * @return Various things as described above 1012 | */ 1013 | public int 1014 | getopt() 1015 | { 1016 | optarg = null; 1017 | 1018 | if (endparse == true) 1019 | return(-1); 1020 | 1021 | if ((nextchar == null) || (nextchar.equals(""))) 1022 | { 1023 | // If we have just processed some options following some non-options, 1024 | // exchange them so that the options come first. 1025 | if (last_nonopt > optind) 1026 | last_nonopt = optind; 1027 | if (first_nonopt > optind) 1028 | first_nonopt = optind; 1029 | 1030 | if (ordering == PERMUTE) 1031 | { 1032 | // If we have just processed some options following some non-options, 1033 | // exchange them so that the options come first. 1034 | if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) 1035 | exchange(argv); 1036 | else if (last_nonopt != optind) 1037 | first_nonopt = optind; 1038 | 1039 | // Skip any additional non-options 1040 | // and extend the range of non-options previously skipped. 1041 | while ((optind < argv.length) && (argv[optind].equals("") || 1042 | (argv[optind].charAt(0) != '-') || argv[optind].equals("-"))) 1043 | { 1044 | optind++; 1045 | } 1046 | 1047 | last_nonopt = optind; 1048 | } 1049 | 1050 | // The special ARGV-element `--' means premature end of options. 1051 | // Skip it like a null option, 1052 | // then exchange with previous non-options as if it were an option, 1053 | // then skip everything else like a non-option. 1054 | if ((optind != argv.length) && argv[optind].equals("--")) 1055 | { 1056 | optind++; 1057 | 1058 | if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) 1059 | exchange (argv); 1060 | else if (first_nonopt == last_nonopt) 1061 | first_nonopt = optind; 1062 | 1063 | last_nonopt = argv.length; 1064 | 1065 | optind = argv.length; 1066 | } 1067 | 1068 | // If we have done all the ARGV-elements, stop the scan 1069 | // and back over any non-options that we skipped and permuted. 1070 | if (optind == argv.length) 1071 | { 1072 | // Set the next-arg-index to point at the non-options 1073 | // that we previously skipped, so the caller will digest them. 1074 | if (first_nonopt != last_nonopt) 1075 | optind = first_nonopt; 1076 | 1077 | return(-1); 1078 | } 1079 | 1080 | // If we have come to a non-option and did not permute it, 1081 | // either stop the scan or describe it to the caller and pass it by. 1082 | if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || 1083 | argv[optind].equals("-")) 1084 | { 1085 | if (ordering == REQUIRE_ORDER) 1086 | return(-1); 1087 | 1088 | optarg = argv[optind++]; 1089 | return(1); 1090 | } 1091 | 1092 | // We have found another option-ARGV-element. 1093 | // Skip the initial punctuation. 1094 | if (argv[optind].startsWith("--")) 1095 | nextchar = argv[optind].substring(2); 1096 | else 1097 | nextchar = argv[optind].substring(1); 1098 | } 1099 | 1100 | // Decode the current option-ARGV-element. 1101 | 1102 | /* Check whether the ARGV-element is a long option. 1103 | 1104 | If long_only and the ARGV-element has the form "-f", where f is 1105 | a valid short option, don't consider it an abbreviated form of 1106 | a long option that starts with f. Otherwise there would be no 1107 | way to give the -f short option. 1108 | 1109 | On the other hand, if there's a long option "fubar" and 1110 | the ARGV-element is "-fu", do consider that an abbreviation of 1111 | the long option, just like "--fu", and not "-f" with arg "u". 1112 | 1113 | This distinction seems to be the most useful approach. */ 1114 | if ((long_options != null) && (argv[optind].startsWith("--") 1115 | || (long_only && ((argv[optind].length() > 2) || 1116 | (optstring.indexOf(argv[optind].charAt(1)) == -1))))) 1117 | { 1118 | int c = checkLongOption(); 1119 | 1120 | if (longopt_handled) 1121 | return(c); 1122 | 1123 | // Can't find it as a long option. If this is not getopt_long_only, 1124 | // or the option starts with '--' or is not a valid short 1125 | // option, then it's an error. 1126 | // Otherwise interpret it as a short option. 1127 | if (!long_only || argv[optind].startsWith("--") 1128 | || (optstring.indexOf(nextchar.charAt(0)) == -1)) 1129 | { 1130 | if (opterr) 1131 | { 1132 | if (argv[optind].startsWith("--")) 1133 | { 1134 | Object[] msgArgs = { progname, nextchar }; 1135 | System.err.println(MessageFormat.format( 1136 | _messages.getString("getopt.unrecognized"), 1137 | msgArgs)); 1138 | } 1139 | else 1140 | { 1141 | Object[] msgArgs = { progname, new 1142 | Character(argv[optind].charAt(0)).toString(), 1143 | nextchar }; 1144 | System.err.println(MessageFormat.format( 1145 | _messages.getString("getopt.unrecognized2"), 1146 | msgArgs)); 1147 | } 1148 | } 1149 | 1150 | nextchar = ""; 1151 | ++optind; 1152 | optopt = 0; 1153 | 1154 | return('?'); 1155 | } 1156 | } // if (longopts) 1157 | 1158 | // Look at and handle the next short option-character */ 1159 | int c = nextchar.charAt(0); //**** Do we need to check for empty str? 1160 | if (nextchar.length() > 1) 1161 | nextchar = nextchar.substring(1); 1162 | else 1163 | nextchar = ""; 1164 | 1165 | String temp = null; 1166 | if (optstring.indexOf(c) != -1) 1167 | temp = optstring.substring(optstring.indexOf(c)); 1168 | 1169 | if (nextchar.equals("")) 1170 | ++optind; 1171 | 1172 | if ((temp == null) || (c == ':')) 1173 | { 1174 | if (opterr) 1175 | { 1176 | if (posixly_correct) 1177 | { 1178 | // 1003.2 specifies the format of this message 1179 | Object[] msgArgs = { progname, new 1180 | Character((char)c).toString() }; 1181 | System.err.println(MessageFormat.format( 1182 | _messages.getString("getopt.illegal"), msgArgs)); 1183 | } 1184 | else 1185 | { 1186 | Object[] msgArgs = { progname, new 1187 | Character((char)c).toString() }; 1188 | System.err.println(MessageFormat.format( 1189 | _messages.getString("getopt.invalid"), msgArgs)); 1190 | } 1191 | } 1192 | 1193 | optopt = c; 1194 | 1195 | return('?'); 1196 | } 1197 | 1198 | // Convenience. Treat POSIX -W foo same as long option --foo 1199 | if ((temp.charAt(0) == 'W') && (temp.length() > 1) && (temp.charAt(1) == ';')) 1200 | { 1201 | if (!nextchar.equals("")) 1202 | { 1203 | optarg = nextchar; 1204 | } 1205 | // No further cars in this argv element and no more argv elements 1206 | else if (optind == argv.length) 1207 | { 1208 | if (opterr) 1209 | { 1210 | // 1003.2 specifies the format of this message. 1211 | Object[] msgArgs = { progname, new 1212 | Character((char)c).toString() }; 1213 | System.err.println(MessageFormat.format( 1214 | _messages.getString("getopt.requires2"), msgArgs)); 1215 | } 1216 | 1217 | optopt = c; 1218 | if (optstring.charAt(0) == ':') 1219 | return(':'); 1220 | else 1221 | return('?'); 1222 | } 1223 | else 1224 | { 1225 | // We already incremented `optind' once; 1226 | // increment it again when taking next ARGV-elt as argument. 1227 | nextchar = argv[optind]; 1228 | optarg = argv[optind]; 1229 | } 1230 | 1231 | c = checkLongOption(); 1232 | 1233 | if (longopt_handled) 1234 | return(c); 1235 | else 1236 | // Let the application handle it 1237 | { 1238 | nextchar = null; 1239 | ++optind; 1240 | return('W'); 1241 | } 1242 | } 1243 | 1244 | if ((temp.length() > 1) && (temp.charAt(1) == ':')) 1245 | { 1246 | if ((temp.length() > 2) && (temp.charAt(2) == ':')) 1247 | // This is an option that accepts and argument optionally 1248 | { 1249 | if (!nextchar.equals("")) 1250 | { 1251 | optarg = nextchar; 1252 | ++optind; 1253 | } 1254 | else 1255 | { 1256 | optarg = null; 1257 | } 1258 | 1259 | nextchar = null; 1260 | } 1261 | else 1262 | { 1263 | if (!nextchar.equals("")) 1264 | { 1265 | optarg = nextchar; 1266 | ++optind; 1267 | } 1268 | else if (optind == argv.length) 1269 | { 1270 | if (opterr) 1271 | { 1272 | // 1003.2 specifies the format of this message 1273 | Object[] msgArgs = { progname, new 1274 | Character((char)c).toString() }; 1275 | System.err.println(MessageFormat.format( 1276 | _messages.getString("getopt.requires2"), msgArgs)); 1277 | } 1278 | 1279 | optopt = c; 1280 | 1281 | if (optstring.charAt(0) == ':') 1282 | return(':'); 1283 | else 1284 | return('?'); 1285 | } 1286 | else 1287 | { 1288 | optarg = argv[optind]; 1289 | ++optind; 1290 | 1291 | // Ok, here's an obscure Posix case. If we have o:, and 1292 | // we get -o -- foo, then we're supposed to skip the --, 1293 | // end parsing of options, and make foo an operand to -o. 1294 | // Only do this in Posix mode. 1295 | if ((posixly_correct) && optarg.equals("--")) 1296 | { 1297 | // If end of argv, error out 1298 | if (optind == argv.length) 1299 | { 1300 | if (opterr) 1301 | { 1302 | // 1003.2 specifies the format of this message 1303 | Object[] msgArgs = { progname, new 1304 | Character((char)c).toString() }; 1305 | System.err.println(MessageFormat.format( 1306 | _messages.getString("getopt.requires2"), msgArgs)); 1307 | } 1308 | 1309 | optopt = c; 1310 | 1311 | if (optstring.charAt(0) == ':') 1312 | return(':'); 1313 | else 1314 | return('?'); 1315 | } 1316 | 1317 | // Set new optarg and set to end 1318 | // Don't permute as we do on -- up above since we 1319 | // know we aren't in permute mode because of Posix. 1320 | optarg = argv[optind]; 1321 | ++optind; 1322 | first_nonopt = optind; 1323 | last_nonopt = argv.length; 1324 | endparse = true; 1325 | } 1326 | } 1327 | 1328 | nextchar = null; 1329 | } 1330 | } 1331 | 1332 | return(c); 1333 | } 1334 | 1335 | } // Class Getopt 1336 | 1337 | 1338 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/LongOpt.java: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* LongOpt.java -- Long option object for Getopt 3 | /* 4 | /* Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com) 5 | /* 6 | /* This program is free software; you can redistribute it and/or modify 7 | /* it under the terms of the GNU Library General Public License as published 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 16 | /* You should have received a copy of the GNU Library General Public License 17 | /* along with this program; see the file COPYING.LIB. If not, write to 18 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | package gnu.getopt; 23 | 24 | import java.util.Locale; 25 | import java.util.ResourceBundle; 26 | import java.text.MessageFormat; 27 | 28 | /**************************************************************************/ 29 | 30 | /** 31 | * This object represents the definition of a long option in the Java port 32 | * of GNU getopt. An array of LongOpt objects is passed to the Getopt 33 | * object to define the list of valid long options for a given parsing 34 | * session. Refer to the getopt documentation for details on the 35 | * format of long options. 36 | * 37 | * @version 1.0.5 38 | * @author Aaron M. Renn (arenn@urbanophile.com) 39 | * 40 | * @see Getopt 41 | */ 42 | public class LongOpt extends Object 43 | { 44 | 45 | /**************************************************************************/ 46 | 47 | /* 48 | * Class Variables 49 | */ 50 | 51 | /** 52 | * Constant value used for the "has_arg" constructor argument. This 53 | * value indicates that the option takes no argument. 54 | */ 55 | public static final int NO_ARGUMENT = 0; 56 | 57 | /** 58 | * Constant value used for the "has_arg" constructor argument. This 59 | * value indicates that the option takes an argument that is required. 60 | */ 61 | public static final int REQUIRED_ARGUMENT = 1; 62 | 63 | /** 64 | * Constant value used for the "has_arg" constructor argument. This 65 | * value indicates that the option takes an argument that is optional. 66 | */ 67 | public static final int OPTIONAL_ARGUMENT = 2; 68 | 69 | /**************************************************************************/ 70 | 71 | /* 72 | * Instance Variables 73 | */ 74 | 75 | /** 76 | * The name of the long option 77 | */ 78 | protected String name; 79 | 80 | /** 81 | * Indicates whether the option has no argument, a required argument, or 82 | * an optional argument. 83 | */ 84 | protected int has_arg; 85 | 86 | /** 87 | * If this variable is not null, then the value stored in "val" is stored 88 | * here when this long option is encountered. If this is null, the value 89 | * stored in "val" is treated as the name of an equivalent short option. 90 | */ 91 | protected StringBuffer flag; 92 | 93 | /** 94 | * The value to store in "flag" if flag is not null, otherwise the 95 | * equivalent short option character for this long option. 96 | */ 97 | protected int val; 98 | 99 | /** 100 | * Localized strings for error messages 101 | */ 102 | private ResourceBundle _messages = ResourceBundle.getBundle( 103 | "gnu/getopt/MessagesBundle", Locale.getDefault()); 104 | 105 | /**************************************************************************/ 106 | 107 | /* 108 | * Constructors 109 | */ 110 | 111 | /** 112 | * Create a new LongOpt object with the given parameter values. If the 113 | * value passed as has_arg is not valid, then an exception is thrown. 114 | * 115 | * @param name The long option String. 116 | * @param has_arg Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT). 117 | * @param flag If non-null, this is a location to store the value of "val" when this option is encountered, otherwise "val" is treated as the equivalent short option character. 118 | * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null. 119 | * 120 | * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT. 121 | */ 122 | public 123 | LongOpt(String name, int has_arg, 124 | StringBuffer flag, int val) throws IllegalArgumentException 125 | { 126 | // Validate has_arg 127 | if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT) 128 | && (has_arg != OPTIONAL_ARGUMENT)) 129 | { 130 | Object[] msgArgs = { new Integer(has_arg).toString() }; 131 | throw new IllegalArgumentException(MessageFormat.format( 132 | _messages.getString("getopt.invalidValue"), msgArgs)); 133 | } 134 | 135 | // Store off values 136 | this.name = name; 137 | this.has_arg = has_arg; 138 | this.flag = flag; 139 | this.val = val; 140 | } 141 | 142 | /**************************************************************************/ 143 | 144 | /** 145 | * Returns the name of this LongOpt as a String 146 | * 147 | * @return Then name of the long option 148 | */ 149 | public String 150 | getName() 151 | { 152 | return(name); 153 | } 154 | 155 | /**************************************************************************/ 156 | 157 | /** 158 | * Returns the value set for the 'has_arg' field for this long option 159 | * 160 | * @return The value of 'has_arg' 161 | */ 162 | public int 163 | getHasArg() 164 | { 165 | return(has_arg); 166 | } 167 | 168 | /**************************************************************************/ 169 | 170 | /** 171 | * Returns the value of the 'flag' field for this long option 172 | * 173 | * @return The value of 'flag' 174 | */ 175 | public StringBuffer 176 | getFlag() 177 | { 178 | return(flag); 179 | } 180 | 181 | /** 182 | * Returns the value of the 'val' field for this long option 183 | * 184 | * @return The value of 'val' 185 | */ 186 | public int 187 | getVal() 188 | { 189 | return(val); 190 | } 191 | 192 | /**************************************************************************/ 193 | 194 | } // Class LongOpt 195 | 196 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle.properties -- English language error messages 3 | /* 4 | /* Copyright (c) 1998 by William King (wrking@eng.sun.com) and 5 | /* Aaron M. Renn (arenn@urbanophile.com) 6 | /* 7 | /* This program is free software; you can redistribute it and/or modify 8 | /* it under the terms of the GNU Library General Public License as published 9 | /* by the Free Software Foundation; either version 2 of the License or 10 | /* (at your option) any later version. 11 | /* 12 | /* This program is distributed in the hope that it will be useful, but 13 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | /* GNU Library General Public License for more details. 16 | /* 17 | /* You should have received a copy of the GNU Library General Public License 18 | /* along with this program; see the file COPYING.LIB. If not, write to 19 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 20 | /* Boston, MA 02111-1307 USA 21 | /**************************************************************************/ 22 | 23 | getopt.ambigious={0}: option ''{1}'' is ambiguous 24 | getopt.arguments1={0}: option ''--{1}'' doesn't allow an argument 25 | getopt.arguments2={0}: option ''{1}{2}'' doesn't allow an argument 26 | getopt.requires={0}: option ''{1}'' requires an argument 27 | getopt.unrecognized={0}: unrecognized option ''--{1}'' 28 | getopt.unrecognized2={0}: unrecognized option ''{1}{2}'' 29 | getopt.illegal={0}: illegal option -- {1} 30 | getopt.invalid={0}: invalid option -- {1} 31 | getopt.requires2={0}: option requires an argument -- {1} 32 | getopt.invalidValue=Invalid value {0} for parameter 'has_arg' 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_chs.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_chs.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_cht.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle.properties - Triditional Chinese language error messages 3 | /* 4 | /* Copyright (c) 2012 by David Zhang (david290@qq.com) 5 | /* 6 | /* This program is free software; you can redistribute it and/or modify 7 | /* it under the terms of the GNU Library General Public License as published 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 16 | /* You should have received a copy of the GNU Library General Public License 17 | /* along with this program; see the file COPYING.LIB. If not, write to 18 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | getopt.ambigious={0}: 選項 ''{1}'' 有歧義 23 | getopt.arguments1={0}:選項''--{1}'' 不能帶參數 24 | getopt.arguments2={0}:選項''{1}{2}''不能帶參數 25 | getopt.requires={0}: 選項 ''{1}'' 要求帶有參數 26 | getopt.unrecognized={0}: 無法識別的選項 ''--{1}'' 27 | getopt.unrecognized2={0}:無法識別的選項''{1}{2}'' 28 | getopt.illegal={0}: 非法選項 -- {1} 29 | getopt.invalid={0}: 無效選項 -- {1} 30 | getopt.requires2={0}:選項需要有參數 -- {1} 31 | getopt.invalidValue=選項 'has_arg'的值 {0} 非法 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_cs.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_cs.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_de.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_de.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_es.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_es.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_fr.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_fr.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_hu.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_hu.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_it.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_it.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_ja.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle.properties -- Japanese language error messages 3 | /* 4 | /* Copyright (c) 2001 by Yasuoka Masahiko (yasuoka@yasuoka.net) 5 | /* 6 | /* This program is free software; you can redistribute it and/or modify 7 | /* it under the terms of the GNU Library General Public License as published 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 16 | /* You should have received a copy of the GNU Library General Public License 17 | /* along with this program; see the file COPYING.LIB. If not, write to 18 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | getopt.ambigious={0}: ''{1}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u66d6\u6627\u3067\u3059\u3002 23 | getopt.arguments1={0}: ''--{1}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3082\u3061\u307e\u305b\u3093\u3002 24 | getopt.arguments2={0}: ''{1}{2}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3082\u3061\u307e\u305b\u3093\u3002 25 | getopt.requires={0}: ''{1}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5fc5\u8981\u3067\u3059\u3002 26 | getopt.unrecognized={0}: ''--{1}'' \u306f\u7121\u52b9\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002 27 | getopt.unrecognized2={0}: ''{1}{2}'' \u306f\u7121\u52b9\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002 28 | getopt.illegal={0}: -- {1} \u306f\u4e0d\u6b63\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002 29 | getopt.invalid={0}: -- {1} \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002 30 | getopt.requires2={0}: -- {1} \u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5fc5\u8981\u3067\u3059\u3002 31 | getopt.invalidValue={0} \u306f\u3001'has_arg' \u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u4e0d\u6b63\u306a\u5024\u3067\u3059\u3002 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_nl.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle_nl.properties -- Dutch language error messages 3 | /* 4 | /* Copyright (c) 1999 by Ernst de Haan (ernst@jollem.com) 5 | /* 6 | /* This program is free software; you can redistribute it and/or modify 7 | /* it under the terms of the GNU Library General Public License as published 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 16 | /* You should have received a copy of the GNU Library General Public License 17 | /* along with this program; see the file COPYING.LIB. If not, write to 18 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | getopt.ambigious={0}: optie ''{1}'' is ambigue 23 | getopt.arguments1={0}: optie ''--{1}'' staat geen argumenten toe 24 | getopt.arguments2={0}: optie ''{1}{2}'' staat geen argumenten toe 25 | getopt.requires={0}: optie ''{1}'' heeft een argument nodig 26 | getopt.unrecognized={0}: onbekende optie ''--{1}'' 27 | getopt.unrecognized2={0}: onbekende optie ''{1}{2}'' 28 | getopt.illegal={0}: niet-toegestane optie -- {1} 29 | getopt.invalid={0}: onjuiste optie -- {1} 30 | getopt.requires2={0}: optie heeft een argument nodig -- {1} 31 | getopt.invalidValue=Ongeldige waarde {0} voor parameter 'has_arg' 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_no.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thypon/AndroidFuzz/a152590fe2818ee790205529992a9ee9fe1efc25/src/main/java/gnu/getopt/MessagesBundle_no.properties -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_pl.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle_pl.properties -- Polish language error messages 3 | /* 4 | /* Polish Messages Copyright (c) 2006 by Krzysztof Szyma?ski (sirch.s@gmail.com) 5 | /* These messages are encoded in ISO-8859-2 6 | /* 7 | /* This program is free software; you can redistribute it and/or modify 8 | /* it under the terms of the GNU Library General Public License as published 9 | /* by the Free Software Foundation; either version 2 of the License or 10 | /* (at your option) any later version. 11 | /* 12 | /* This program is distributed in the hope that it will be useful, but 13 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | /* GNU Library General Public License for more details. 16 | /* 17 | /* You should have received a copy of the GNU Library General Public License 18 | /* along with this program; see the file COPYING.LIB. If not, write to 19 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 20 | /* Boston, MA 02111-1307 USA 21 | /**************************************************************************/ 22 | 23 | 24 | getopt.ambigious={0}: opcja ''{1}''jest wieloznaczna 25 | getopt.arguments1={0}: opcja ''--{1}'' nie akceptuje argumentu 26 | getopt.arguments2={0}: opcja ''{1}{2}'' nie akceptuje argumentu 27 | getopt.requires={0}: opcja ''{1}'' wymaga argumentu 28 | getopt.unrecognized={0}: nierozpoznana opcja ''--{1}'' 29 | getopt.unrecognized2={0}: nierozpoznana opcja ''{1}{2}'' 30 | getopt.illegal={0}: nie dopuszczalna opcja --{1} 31 | getopt.invalid={0}: b??dna opcja --{1} 32 | getopt.requires2={0}: opcja --{1} oczekuje argumentu 33 | getopt.invalidValue=Nie poprawna warto?? {0} argument 'has_arg' 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/main/java/gnu/getopt/MessagesBundle_ro.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle_fr.properties -- Romanian language error messages 3 | /* 4 | /* Copyright (c) 1999 Free Software Foundation, Inc. 5 | /* Marian-Nicolae Ion , 2004, 6 | /* These messages are encoded in ISO-8859-2 7 | /* 8 | /* This program is free software; you can redistribute it and/or modify 9 | /* it under the terms of the GNU Library General Public License as published 10 | /* by the Free Software Foundation; either version 2 of the License or 11 | /* (at your optiunea) any later version. 12 | /* 13 | /* This program is distributed in the hope that it will be useful, but 14 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 15 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | /* GNU Library General Public License for more details. 17 | /* 18 | /* You should have received a copy of the GNU Library General Public License 19 | /* along with this program; see the file COPYING.LIB. If not, write to 20 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 21 | /* Boston, MA 02111-1307 USA 22 | /**************************************************************************/ 23 | 24 | getopt.ambigious={0}: opţiunea ''{1}'' este ambiguă 25 | getopt.arguments1={0}: opţiunea ''--{1}'' nu acceptă parametru 26 | getopt.arguments2={0}: opţiunea ''{1}{2}'' nu acceptă parametru 27 | getopt.requires={0}: opţiunea ''{1}'' cere un parametru 28 | getopt.unrecognized={0}: opţiune necunoscută ''--{1}'' 29 | getopt.unrecognized2={0}: opţiune necunoscută ''{1}{2}'' 30 | getopt.illegal={0}: opţiune ilegală -- {1} 31 | getopt.invalid={0}: opţiune invalidă -- {1} 32 | getopt.requires2={0}: această opţiune cere un parametru -- {1} 33 | getopt.invalidValue=Valoare invalidă {0} pentru parametrul 'has_arg' 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/javafuzz/Helper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Maintainer: Emmanouel Kellinis (me@cipher.org.uk) 3 | * Java Classes Fuzzer - Reflection Based 4 | * http://www.cipher.org.uk 5 | */ 6 | 7 | package javafuzz; 8 | 9 | import java.lang.reflect.Field; 10 | import java.lang.reflect.Constructor; 11 | import java.lang.reflect.Method; 12 | 13 | class Helper { 14 | 15 | //Return a random constant to the constructor 16 | public static Object returnConsant(Class cls)throws Exception { 17 | Object constant = null; 18 | if (cls == Object.class) return constant; 19 | Field[] fields = cls.getDeclaredFields(); 20 | 21 | for (int n=0; n1){constant = 26 | fields[n+1].get(null);return constant;} 27 | else {constant = fields[n].get(null);return constant;} 28 | } 29 | 30 | } 31 | catch(Exception e) {} } 32 | return constant; 33 | } 34 | 35 | //Bruteforce constant in the constructor 36 | public static void BeefConstructor(Constructor cons,Object[] args, Object constant, int v, int g) throws Exception{ 37 | String output=""; 38 | if (constant==null) { 39 | if (args.length>0) {outArgs(args);out(DoIt(cons,args,v));} 40 | else out(DoIt(cons,v)); 41 | } 42 | 43 | else { 44 | out("+++++++++++++++++++++++++++++++++++"); 45 | out("Try the simple version - no constant"); 46 | outArgs(args);out(DoIt(cons,args,v)); 47 | out("++++++++++++++++++++++++++++++++++++"); 48 | if (g==1){ 49 | for (int a=0;a0) { 64 | if (MethodArgs.length>0){outArgs(args);out(DoIt2(cons,mtd,args,MethodArgs,v));} 65 | else {outArgs(args);out(DoIt2(cons,mtd,args,new Double(1),v));} 66 | } 67 | else { 68 | if (MethodArgs.length>0){outArgs(args);out(DoIt2(cons,mtd,MethodArgs,new Integer(1),v));} 69 | else {outArgs(args);out(DoIt2(cons,mtd,v));} 70 | } 71 | } 72 | 73 | else { 74 | if (args.length>0) { 75 | 76 | out("+++++++++++++++++++++++++++++++++++"); 77 | out("Try the Obvious version - no constant"); 78 | if (MethodArgs.length>0){outArgs(args);out(DoIt2(cons,mtd,args,MethodArgs,v));} 79 | else {outArgs(args);out(DoIt2(cons,mtd,args,new Double(1),v));} 80 | out("++++++++++++++++++++++++++++++++++++"); 81 | 82 | if (g==1) { 83 | for (int a=0;a0){outArgs(tmpr);out(DoIt2(cons,mtd,tmpr,MethodArgs,v));} 88 | else {outArgs(args);out(DoIt2(cons,mtd,tmpr,new Double(1),v));} 89 | } 90 | } 91 | } 92 | else { 93 | if (MethodArgs.length>0){outArgs(args);out(DoIt2(cons,mtd,MethodArgs,new Integer(1),v));} 94 | else {outArgs(args);out(DoIt2(cons,mtd,v));} 95 | } 96 | 97 | 98 | } 99 | } 100 | 101 | 102 | //Little print helpers 103 | public static void out(String str) {System.out.println("\n"+str);} 104 | public static void outArgs(Object[] obj) {System.out.println("\nSubmit: ");for (int a=0;a \t"+a[f].getName()+"\nTypes -> \t("); 428 | 429 | for (int k=0;k \t"+className); 433 | Constructor cons = cls.getConstructor(types); 434 | 435 | //High Values - No Methods 436 | etternalLoop=0; 437 | if (limit.equals("high") | limit.equals("") ){ 438 | args = slapObject(ff,1,Exceed) ; 439 | System.out.print("\n[MAX] Status -> \t"); 440 | help.BeefConstructor(cons,args,Constant,v,oo); 441 | System.out.print("\n"); 442 | } 443 | //Low Values - No Methods 444 | if (limit.equals("low") | limit.equals("") ){ 445 | args = slapObject(ff,0,Exceed) ; 446 | System.out.print("[MIN] Status -> \t"); 447 | help.BeefConstructor(cons,args,Constant,v,oo); 448 | System.out.print("\n"); 449 | } 450 | //Method Attack 451 | if (attackMethods==1){ 452 | //Hi Values 453 | if (limit.equals("high") | limit.equals("") ){ 454 | args = slapObject(ff,1,Exceed) ; 455 | methodSlap(a[f],cls,args,1,v); 456 | } 457 | //Low Values 458 | if (limit.equals("low") | limit.equals("") ){ 459 | args = slapObject(ff,0,Exceed) ; 460 | methodSlap(a[f],cls,args,0,v); 461 | } 462 | } 463 | 464 | System.out.println("--------------------------------------"); 465 | 466 | 467 | } 468 | } catch(Exception e){} 469 | } 470 | 471 | public static void DoIt (Constructor cons, Object[] args, int v){ 472 | try { 473 | cons.newInstance(args); 474 | System.out.print(" No Problem\n"); 475 | } 476 | catch(Exception e){ 477 | if (v==1){ System.out.print("Exception("+e.getCause() +")\n");} 478 | else {System.out.print("Exception\n");} 479 | } 480 | } 481 | public static void DoIt (Constructor cons, int v){ 482 | try { 483 | cons.newInstance(); 484 | System.out.print(" No Problem\n"); 485 | } 486 | catch(Exception e){ 487 | if (v==1){ System.out.print("Exception("+e.getCause() +")\n");} 488 | else {System.out.print("Exception\n");} 489 | } 490 | } 491 | 492 | 493 | 494 | 495 | //Expand Methods and throw Slaped Objects in 496 | public static void methodSlap(Constructor cs,Class cls,Object[] args,int hilo,int v) { 497 | try { 498 | Method[] allMethods = cls.getDeclaredMethods(); 499 | String hilow; 500 | System.out.println("\n~~ Methods Fuzzing ~~"); 501 | 502 | Object tmpCLS = help.returnConsant(cls); 503 | if (pp!=null) {tmpCLS = pp;} 504 | 505 | for (int a=0;a \t"+allMethods[a].getName()+" ["+cls.getName()+"]: -=IGNORED=-\n"); 540 | } 541 | 542 | else { 543 | System.out.print("\nMethod -> \t"+allMethods[a].getName()+" ["+cls.getName()+"]\nTypes -> \t("); 544 | for (int k=0;k0){ 687 | for(int h=0;h0) {tmpr[h-1] = pp;} 693 | for (int display=0;display