bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java

196 lines
6.4 KiB
Java
Raw Normal View History

2019-04-17 06:45:15 +00:00
package the.bytecode.club.bytecodeviewer.util;
import org.objectweb.asm.tree.ClassNode;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
2019-04-17 06:45:15 +00:00
import java.lang.reflect.Field;
import java.util.*;
import static the.bytecode.club.bytecodeviewer.Constants.gson;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* 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 3 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, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* A collection of Misc Utils.
*
2018-01-31 15:41:24 +00:00
* @author Konloch
*/
public class MiscUtils {
2018-01-31 15:41:24 +00:00
private static final String AB = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final String AN = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
2021-04-12 20:19:12 +00:00
private static final Random rnd = new Random();
private static HashSet<String> createdRandomizedNames = new HashSet<>();
2018-01-31 15:41:24 +00:00
/**
* Returns a random string without numbers
*
* @param len the length of the String
* @return the randomized string
*/
public static String randomString(int len) {
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
sb.append(AB.charAt(rnd.nextInt(AB.length())));
return sb.toString();
}
/**
* Ensures it will only return a uniquely generated names, contains a dupe checker to be sure
*
* @return the unique randomized name of 25 characters.
*/
public static String getRandomizedName() {
boolean generated = false;
String name = "";
while (!generated) {
String randomizedName = MiscUtils.randomString(25);
if (!createdRandomizedNames.contains(randomizedName)) {
createdRandomizedNames.add(randomizedName);
name = randomizedName;
generated = true;
}
}
return name;
}
2018-01-31 15:41:24 +00:00
public static void printProcess(Process process) throws Exception {
//Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
2018-01-31 15:41:24 +00:00
is = process.getErrorStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
2018-01-31 15:41:24 +00:00
}
/**
* Returns a random string with numbers
*
* @param len the length of the String
* @return the randomized string
*/
public static String randomStringNum(int len) {
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
sb.append(AN.charAt(rnd.nextInt(AN.length())));
return sb.toString();
}
/**
* Checks the file system to ensure it's a unique name
*
* @param start directory it'll be in
* @param ext the file extension it'll use
* @return the unique name
*/
public static String getUniqueName(String start, String ext) {
String s = null;
boolean b = true;
2021-04-12 22:31:22 +00:00
File f;
String m;
2018-01-31 15:41:24 +00:00
while (b) {
m = MiscUtils.randomString(32);
f = new File(start + m + ext);
if (!f.exists()) {
s = start + m;
b = false;
}
}
return s;
}
/**
* Checks the file system to ensure it's a unique number
*
* @param start directory it'll be in
* @param ext the file extension it'll use
* @return the unique number
*/
public static int getClassNumber(String start, String ext) {
boolean b = true;
int i = 0;
while (b) {
File tempF = new File(start + i + ext);
if (!tempF.exists())
b = false;
else
i++;
}
return i;
}
public static String extension(String name) {
return name.substring(name.lastIndexOf('.') + 1);
}
public static String append(File file, String extension) {
String path = file.getAbsolutePath();
if (!path.endsWith(extension))
path = path + extension;
return path;
}
public static int fileContainersHash(ArrayList<FileContainer> fileContainers) {
StringBuilder block = new StringBuilder();
for (FileContainer container : fileContainers) {
block.append(container.name);
for (ClassNode node : container.classes) {
block.append(node.name);
}
}
return block.hashCode();
}
/**
* Converts an array list to a string
*
* @param a array
* @return string with newline per array object
*/
public static String listToString(List<String> a) {
return gson.toJson(a);
}
2019-04-17 06:45:15 +00:00
/**
* @author JoshTheWolfe
*/
2021-04-12 20:19:12 +00:00
@SuppressWarnings({"unchecked"})
2019-04-17 06:45:15 +00:00
public static void updateEnv(String name, String val) throws ReflectiveOperationException {
Map<String, String> env = System.getenv();
Field field = env.getClass().getDeclaredField("m");
field.setAccessible(true);
((Map<String, String>) field.get(env)).put(name, val);
}
}