API Cleanup

This commit is contained in:
Konloch 2021-07-06 17:23:34 -07:00
parent de8cb5b4e7
commit ec91bd08e8
6 changed files with 62 additions and 33 deletions

View File

@ -1,12 +1,9 @@
package the.bytecode.club.bytecodeviewer; package the.bytecode.club.bytecodeviewer;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import javax.swing.*; import javax.swing.*;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -27,7 +24,6 @@ import the.bytecode.club.bytecodeviewer.resources.importing.ImportResource;
import static the.bytecode.club.bytecodeviewer.Constants.*; import static the.bytecode.club.bytecodeviewer.Constants.*;
import static the.bytecode.club.bytecodeviewer.Settings.addRecentPlugin; import static the.bytecode.club.bytecodeviewer.Settings.addRecentPlugin;
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentTypes.JAVA_AND_BYTECODE;
import static the.bytecode.club.bytecodeviewer.util.MiscUtils.guessLanguage; import static the.bytecode.club.bytecodeviewer.util.MiscUtils.guessLanguage;
/*************************************************************************** /***************************************************************************
@ -114,6 +110,9 @@ import static the.bytecode.club.bytecodeviewer.util.MiscUtils.guessLanguage;
public class BytecodeViewer public class BytecodeViewer
{ {
public static boolean EXPERIMENTAL_TAB_CODE = false;
public static boolean DEV_MODE = false; //if true error streams as preserved
public static String[] launchArgs; public static String[] launchArgs;
public static MainViewerGUI viewer = null; public static MainViewerGUI viewer = null;
public static ClassNodeLoader loader = new ClassNodeLoader(); //might be insecure due to assholes targeting BCV, public static ClassNodeLoader loader = new ClassNodeLoader(); //might be insecure due to assholes targeting BCV,
@ -121,10 +120,8 @@ public class BytecodeViewer
public static Refactorer refactorer = new Refactorer(); public static Refactorer refactorer = new Refactorer();
public static List<FileContainer> files = new ArrayList<>(); //all of BCV's loaded files/classes/etc public static List<FileContainer> files = new ArrayList<>(); //all of BCV's loaded files/classes/etc
public static List<Process> createdProcesses = new ArrayList<>(); public static List<Process> createdProcesses = new ArrayList<>();
public static final DecompilerViewComponent krakatau = new DecompilerViewComponent("Krakatau", JAVA_AND_BYTECODE);
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); public static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static final boolean EXPERIMENTAL_TAB_CODE = false;
public static final boolean DEV_MODE = false; //if true error streams as preserved
private static final Thread versionChecker = new Thread(new VersionChecker(), "Version Checker"); private static final Thread versionChecker = new Thread(new VersionChecker(), "Version Checker");
private static final Thread pingBack = new Thread(new PingBack(), "Pingback"); private static final Thread pingBack = new Thread(new PingBack(), "Pingback");
private static final Thread installFatJar = new Thread(new InstallFatJar(), "Install Fat-Jar"); private static final Thread installFatJar = new Thread(new InstallFatJar(), "Install Fat-Jar");
@ -298,7 +295,10 @@ public class BytecodeViewer
return null; return null;
} }
/**
* Returns the File Container by the specific name
*/
public static FileContainer getFileContainer(String name) public static FileContainer getFileContainer(String name)
{ {
for (FileContainer container : files) for (FileContainer container : files)
@ -307,11 +307,17 @@ public class BytecodeViewer
return null; return null;
} }
/**
* Returns all of the loaded File Containers
*/
public static List<FileContainer> getFiles() { public static List<FileContainer> getFiles() {
return files; return files;
} }
/**
* Returns a ClassNode by name specific namefrom a specific File Container
*/
public static ClassNode getClassNode(FileContainer container, String name) public static ClassNode getClassNode(FileContainer container, String name)
{ {
for (ClassNode c : container.classes) for (ClassNode c : container.classes)
@ -337,23 +343,11 @@ public class BytecodeViewer
} }
/** /**
* Grab the byte array from the loaded Class object * Grab the byte array from the loaded Class object by getting the resource from the classloader
*
* @param clazz
* @return
* @throws IOException
*/ */
public static byte[] getClassFile(Class<?> clazz) throws IOException public static byte[] getClassFileBytes(Class<?> clazz) throws IOException
{ {
try (InputStream is = clazz.getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class"); return ClassFileUtils.getClassFileBytes(clazz);
ByteArrayOutputStream baos = new ByteArrayOutputStream())
{
int r;
byte[] buffer = new byte[8192];
while ((r = Objects.requireNonNull(is).read(buffer)) >= 0)
baos.write(buffer, 0, r);
return baos.toByteArray();
}
} }
/** /**

View File

@ -40,8 +40,8 @@ public enum Decompiler
BYTECODE_DISASSEMBLER("Bytecode Disassembler", new BytecodeDisassembler(), new JRadioButtonMenuItem("Bytecode")), BYTECODE_DISASSEMBLER("Bytecode Disassembler", new BytecodeDisassembler(), new JRadioButtonMenuItem("Bytecode")),
HEXCODE_VIEWER("Hexcode Viewer", null, new JRadioButtonMenuItem("Hexcode")), HEXCODE_VIEWER("Hexcode Viewer", null, new JRadioButtonMenuItem("Hexcode")),
SMALI_DISASSEMBLER("Smali Disassembler", new SmaliDisassembler(), new DecompilerViewComponent("Smali", BYTECODE)), SMALI_DISASSEMBLER("Smali Disassembler", new SmaliDisassembler(), new DecompilerViewComponent("Smali", BYTECODE)),
KRAKATAU_DECOMPILER("Krakatau Decompiler", new KrakatauDecompiler(), BytecodeViewer.krakatau), KRAKATAU_DECOMPILER("Krakatau Decompiler", new KrakatauDecompiler(), DecompilerViewComponent.KRAKATAU),
KRAKATAU_DISASSEMBLER("Krakatau Disassembler", new KrakatauDisassembler(), BytecodeViewer.krakatau), KRAKATAU_DISASSEMBLER("Krakatau Disassembler", new KrakatauDisassembler(), DecompilerViewComponent.KRAKATAU),
JD_DECOMPILER("JD-GUI Decompiler", new JDGUIDecompiler(), new DecompilerViewComponent("JD-GUI", JAVA)), JD_DECOMPILER("JD-GUI Decompiler", new JDGUIDecompiler(), new DecompilerViewComponent("JD-GUI", JAVA)),
JADX_DECOMPILER("JADX Decompiler", new JADXDecompiler(), new DecompilerViewComponent("JADX", JAVA)), JADX_DECOMPILER("JADX Decompiler", new JADXDecompiler(), new DecompilerViewComponent("JADX", JAVA)),
ASM_TEXTIFY_DISASSEMBLER("ASM Disassembler", new ASMTextifierDecompiler(), new DecompilerViewComponent("ASM Textify", BYTECODE)), ASM_TEXTIFY_DISASSEMBLER("ASM Disassembler", new ASMTextifierDecompiler(), new DecompilerViewComponent("ASM Textify", BYTECODE)),

View File

@ -11,7 +11,7 @@ import the.bytecode.club.bytecodeviewer.decompilers.InternalDecompiler;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.DirectoryLoader; import the.bytecode.club.bytecodeviewer.decompilers.jdgui.DirectoryLoader;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.CommonPreferences; import the.bytecode.club.bytecodeviewer.decompilers.jdgui.CommonPreferences;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.PlainTextPrinter; import the.bytecode.club.bytecodeviewer.decompilers.jdgui.PlainTextPrinter;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.ClassFileUtil; import the.bytecode.club.bytecodeviewer.decompilers.jdgui.JDGUIClassFileUtil;
import me.konloch.kontainer.io.DiskReader; import me.konloch.kontainer.io.DiskReader;
import org.jd.core.v1.ClassFileToJavaSourceDecompiler; import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
@ -78,8 +78,8 @@ public class JDGUIDecompiler extends InternalDecompiler
String pathToClass = tempClass.getAbsolutePath().replace('/', File.separatorChar).replace('\\', File.separatorChar); String pathToClass = tempClass.getAbsolutePath().replace('/', File.separatorChar).replace('\\', File.separatorChar);
String directoryPath = ClassFileUtil.ExtractDirectoryPath(pathToClass); String directoryPath = JDGUIClassFileUtil.ExtractDirectoryPath(pathToClass);
String internalPath = ClassFileUtil.ExtractInternalPath(directoryPath, pathToClass); String internalPath = JDGUIClassFileUtil.ExtractInternalPath(directoryPath, pathToClass);
CommonPreferences preferences = new CommonPreferences() { CommonPreferences preferences = new CommonPreferences() {
@Override @Override

View File

@ -13,7 +13,8 @@ import org.jd.core.v1.service.deserializer.classfile.ClassFileFormatException;
import org.jd.core.v1.service.deserializer.classfile.ClassFileReader; import org.jd.core.v1.service.deserializer.classfile.ClassFileReader;
public class ClassFileUtil { public class JDGUIClassFileUtil
{
public static final char INTERNAL_PACKAGE_SEPARATOR = '/'; public static final char INTERNAL_PACKAGE_SEPARATOR = '/';
public static final String CLASS_FILE_SUFFIX = ".class"; public static final String CLASS_FILE_SUFFIX = ".class";

View File

@ -7,6 +7,8 @@ import the.bytecode.club.bytecodeviewer.util.RefreshWorkPane;
import javax.swing.*; import javax.swing.*;
import static the.bytecode.club.bytecodeviewer.gui.components.DecompilerViewComponent.DecompilerComponentTypes.JAVA_AND_BYTECODE;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com * * Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
@ -31,6 +33,8 @@ import javax.swing.*;
*/ */
public class DecompilerViewComponent public class DecompilerViewComponent
{ {
public static final DecompilerViewComponent KRAKATAU = new DecompilerViewComponent("Krakatau", JAVA_AND_BYTECODE);
private final String name; private final String name;
private final JMenu menu; private final JMenu menu;
private final DecompilerComponentTypes types; private final DecompilerComponentTypes types;
@ -38,7 +42,8 @@ public class DecompilerViewComponent
private final JRadioButtonMenuItem bytecode = new TranslatedJRadioButtonMenuItem("Bytecode", Translation.BYTECODE); private final JRadioButtonMenuItem bytecode = new TranslatedJRadioButtonMenuItem("Bytecode", Translation.BYTECODE);
private final JCheckBoxMenuItem editable = new TranslatedJCheckBoxMenuItem("Editable", Translation.EDITABLE); private final JCheckBoxMenuItem editable = new TranslatedJCheckBoxMenuItem("Editable", Translation.EDITABLE);
public DecompilerViewComponent(String name, DecompilerComponentTypes types) { public DecompilerViewComponent(String name, DecompilerComponentTypes types)
{
this.name = name; this.name = name;
this.menu = new JMenu(name); this.menu = new JMenu(name);
this.types = types; this.types = types;
@ -92,4 +97,4 @@ public class DecompilerViewComponent
BYTECODE, BYTECODE,
JAVA_AND_BYTECODE JAVA_AND_BYTECODE
} }
} }

View File

@ -0,0 +1,29 @@
package the.bytecode.club.bytecodeviewer.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
/**
* @author Konloch
* @since 7/6/2021
*/
public class ClassFileUtils
{
/**
* Grab the byte array from the loaded Class object by getting the resource from the classloader
*/
public static byte[] getClassFileBytes(Class<?> clazz) throws IOException
{
try (InputStream is = clazz.getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class");
ByteArrayOutputStream baos = new ByteArrayOutputStream())
{
int r;
byte[] buffer = new byte[8192];
while ((r = Objects.requireNonNull(is).read(buffer)) >= 0)
baos.write(buffer, 0, r);
return baos.toByteArray();
}
}
}