Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* @(#)Helper.java
*
* JReversePro - Java Decompiler / Disassembler.
* Copyright (C) 2000 2001 Karthik Kumar.
* EMail: [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it , under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program.If not, write to
* The Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
package jreversepro.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Helper contains a list of assorted methods that 'helps'
* in manipulating the data present in the class file.
*
* @author Karthik Kumar
*/
public class Helper implements KeyWords {
/**
* Working Version Could be compromised
*/
static final String DEFAULT_VERSION = "1.2.2";
private static final Logger log = LoggerFactory.getLogger(Helper.class);
/**
* Debug flag
* Default value = false.
*/
static boolean debugFlag;
/*
static initializer method.
*/
static {
debugFlag = false;
}
/**
* Private constructor to prevent any instance from being created.
*/
private Helper() {
}
/**
* Returns the arguments in array form
* given the JVM signature.
*
* For example , IILjava/lang/String
* could be returned as
* ( int , int , java/lang/String ).
*
* @param aSignature Signature of the method.
* @return The method arguments as a List
*/
public static List getArguments(String aSignature) {
List args = new ArrayList<>();
int endIndex = aSignature.indexOf(")");
if (endIndex != 1) {
aSignature = aSignature.substring(1, endIndex);
String origStr = aSignature;
int length = origStr.length();
//Start Processing Rhs
int curIndex = 0;
while (curIndex < length) {
aSignature = origStr.substring(curIndex);
int tokenLength = getSignTokenLength(aSignature);
String tokenString = aSignature.substring(0, tokenLength);
int semiColon = tokenString.indexOf(";");
if (semiColon != -1) {
tokenString = tokenString.substring(0, semiColon);
}
args.add(tokenString);
curIndex += tokenLength;
}
}
return args;
}
/**
* Determines the Java representation , given the JVM representation
* of data types.
*
*
*
dataType
*
formatDataType(dataType)
*
B
byte
*
C
char
*
D
double
*
F
float
*
I
int
*
J
long
*
S
short
*
V
void
*
Z
boolean
*
[Z
boolean [] , array representation
*
*
Ljava/lang/String
* java/lang/String
*
*
* @param aDataType JVM representation of the data type.
* @param associated If set, then an array representation is returned.�
* @return Java Language representation of aDataType.
*/
public static String getJavaDataType(String aDataType, boolean associated) {
char firstChar = aDataType.charAt(0);
if (aDataType.length() == 1) {
switch (firstChar) {
case 'B':
return "byte";
case 'C':
return "char";
case 'D':
return "double";
case 'F':
return "float";
case 'I':
return "int";
case 'J':
return "long";
case 'S':
return "short";
case 'V':
return "void";
case 'Z':
return "boolean";
default:
return "invalid";
}
} else if (firstChar == '[') {
String type = getJavaDataType(aDataType.substring(1), associated);
if (!associated) {
return type + "[]";
} else {
return type;
}
} else if (firstChar == 'L') {
int len = aDataType.length();
if (!aDataType.contains(";")) {
return aDataType.substring(1);
} else {
return aDataType.substring(1, len - 1);
}
}
return aDataType;
}
/**
* Returns the Package name alone from a fully qualified name.
*
* For Example , if FullName = java/lang/StringBuffer,
* then a call to getPackageName(arg) returns the
* value java.lang.
*
*
* @param aFullName A Fully Qualified Name.
* @return the package name , alone with the dots separating the
* classes.
*/
public static String getPackageName(String aFullName) {
aFullName = Helper.getJavaDataType(aFullName, false);
aFullName = aFullName.replace('/', '.');
int dotIndex = aFullName.lastIndexOf(".");
if (dotIndex != -1) {
return aFullName.substring(0, dotIndex);
} else {
return "";
}
}
/**
* Given the Signature of the method , this provides us the
* return type.
*
*
* @param aSignature Signature of the method.
* @return the return type associated with the method signature,
* The type returned corresponds to JVM representation.
*/
public static String getReturnType(String aSignature) {
int index = aSignature.indexOf(")");
return aSignature.substring(index + 1);
}
/**
* Determines the length of the JVM datatype representation
* given the JVM signature.
*
*
*
dataType
*
getSignTokenLength(dataType)(dataType)
*
all basic data types
1
*
[XYZ
len(XYZ) + 1
*
*
Ljava/lang/String
* len(Ljava/lang/String)
*
*
* @param aDataType Signature of a method as present in
* the class file in JVM representation, containing a list
* of datatypes.
* @return the length of the first valid datatype.
*/
public static int getSignTokenLength(String aDataType) {
char ch = aDataType.charAt(0);
switch (ch) {
case 'B':
case 'C':
case 'D':
case 'F':
case 'I':
case 'J':
case 'S':
case 'V':
case 'Z':
return 1;
case '[':
return (getSignTokenLength(aDataType.substring(1)) + 1);
case 'L':
int semiColon = aDataType.indexOf(";");
if (semiColon == -1) {
return aDataType.length();
} else {
return (semiColon + 1);
}
default:
return 0;
}
}
/**
* Both boolean and char are represented as integers .
* This takes care of the conversions
*
* @param value Old Value.
* @param datatype Datatype of the value.
* @return Returns the new value after making appropriate
* changes.
*/
public static String getValue(String value, String datatype) {
if (datatype == null) {
return value;
}
int lastQIndex = value.lastIndexOf('?');
int lastColonIndex = value.lastIndexOf(":");
if (lastQIndex == -1 || lastColonIndex == -1 || lastQIndex > lastColonIndex) {
return getAtomicValue(value, datatype);
}
String condition = value.substring(0, lastQIndex);
String val1 = value.substring(lastQIndex + 1, lastColonIndex);
String val2 = value.substring(lastColonIndex + 1);
return condition + "? " + getAtomicValue(val1, datatype) + ": " + getAtomicValue(val2, datatype);
}
/**
* Both boolean and char are represented as integers .
* This takes care of the conversions
*
* @param value Old Value.
* @param datatype Datatype of the value.
* @return Returns the new value after making appropriate
* changes.
*/
private static String getAtomicValue(String value, String datatype) {
value = value.trim();
if (datatype.equals(JVM_BOOLEAN)) { //boolean
if (value.compareTo("1") == 0) {
return TRUE;
} else if (value.compareTo("0") == 0) {
return FALSE;
} else {
return value;
}
} else if (datatype.equals(JVM_CHAR)) { //Character
try {
StringBuilder sb = new StringBuilder();
int intvalue = Integer.parseInt(value);
sb.append("'").append((char) intvalue).append("'");
return sb.toString();
} catch (NumberFormatException _ex) {
return value;
}
}
return value;
}
/**
* Checks if the given datatype is a basic data type or not.
*
* @param type the datatype to be checked.
* @return true , if it is.
* false , otherwise.
*/
public static boolean isBasicType(String type) {
return (type.equals(KeyWords.INT) || type.equals(KeyWords.BOOLEAN) || type.equals(KeyWords.BYTE) || type.equals(
KeyWords.CHAR) || type.equals(KeyWords.SHORT) || type.equals(KeyWords.FLOAT) || type.equals(KeyWords
.LONG) || type.equals(
KeyWords.DOUBLE));
}
/**
* @return the value of debug flag.
*/
public static boolean isDebug() {
return debugFlag;
}
/**
* @param logMsg Message to be logged.
*/
public static void log(String logMsg) {
if (debugFlag) {
log.info(logMsg);
}
}
/**
* @param ex Exception to be logged.
*/
public static void log(Exception ex) {
if (debugFlag) {
ex.printStackTrace(System.out);
}
}
/**
* Log without end-of-line at the end.
*
* @param logMsg Message to be logged.
*/
public static void logNoEol(String logMsg) {
if (debugFlag) {
System.out.print(logMsg);
}
}
/**
* Extracts the value of a particular number of bits.
*
* For example lowNBits(169 , 5 )
* returns (10101001,5) -> 10101 i.e 21 .
*
* @param aValue Value containing the integer in string form.
* @param aNumBits Number of bits that is to be extracted.
* @return the masked value of the N Bit Number.
*/
public static String lowNbits(String aValue, int aNumBits) {
int val = Integer.parseInt(aValue);
if (val == 0) {
return aValue;
} else {
int res = 0;
for (int i = 0; i < aNumBits; i++) {
int mask = (int) Math.pow(2, i);
if ((val & mask) != 0) {
res += mask;
}
}
return (String.valueOf(res));
}
}
/**
* Inserts a '\' before all the escape characters ,
* line '\n' , '\t' to provide better readability.
*
*
* @param aLiteral String containing the escape characters.
* @return the new String containing the new escape sequence
* of characters.
*/
public static String replaceEscapeChars(String aLiteral) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < aLiteral.length(); i++) {
result.append(representChar(aLiteral.charAt(i)));
}
return result.toString();
}
/**
* Returns the String representation of the character .
*
* @param aChar - Character.
* @return the new String representing the character.
*/
private static String representChar(char aChar) {
switch (aChar) {
case '\n':
return "\\n";
case '\t':
return "\\t";
case '\\':
return "\\\\";
case '"':
return "\\" + "\"";
default:
return String.valueOf(aChar);
}
}
/**
* Converts a signed 'byte' to an unsigned integer.
*
*
* @param aByteVal a Byte Value.
* @return unsigned integer equivalent of aByteVal.
*/
public static int signedToUnsigned(int aByteVal) {
return (aByteVal < 0) ? aByteVal + 256 : aByteVal;
}
/**
* Toggles the debug flag.
*
* @return Returns the new debug flag after toggling.
*/
public static boolean toggleDebug() {
debugFlag = !debugFlag;
return debugFlag;
}
/**
* Checks for the version compatibility between the system JRE
* and the JRE for which the application is written for.
*
* @return true , if System JRE is >= DEFAULT_VERISON ( 1.2.2 ).
* false , otherwise.
*/
public static boolean versionCheck() {
String version = System.getProperty("java.version");
for (int i = 0; i <= 5; i += 2) {
int versionVal = version.charAt(i);
int workingVal = DEFAULT_VERSION.charAt(i);
if (versionVal > workingVal) {
return true;
} else if (versionVal < workingVal) {
log.error("This Software is designed to run under " + DEFAULT_VERSION);
log.error("Please upgrade your JRE" + " from http://java.sun.com/products/j2se" + " for your " +
"operating system");
return false;
}
}
return true;
}
}