Cafebabe Cleanup

This commit is contained in:
Konloch 2021-06-27 23:45:43 -07:00
parent 10d4bcf1f5
commit 2aa4272187
5 changed files with 29 additions and 32 deletions

View file

@ -13,6 +13,7 @@ import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.api.Plugin;
import the.bytecode.club.bytecodeviewer.plugin.PluginLaunchStrategy;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -71,20 +72,23 @@ public class CompiledJavaPluginLaunchStrategy implements PluginLaunchStrategy {
return loaded;
}
private static Set<LoadedNodeData> loadData(File jarFile) throws Throwable {
private static Set<LoadedNodeData> loadData(File jarFile) throws Throwable
{
ZipInputStream jis = new ZipInputStream(new FileInputStream(jarFile));
ZipEntry entry;
Set<LoadedNodeData> set = new HashSet<>();
while ((entry = jis.getNextEntry()) != null) {
try {
while ((entry = jis.getNextEntry()) != null)
{
try
{
String name = entry.getName();
if (name.endsWith(".class")) {
if (name.endsWith(".class"))
{
byte[] bytes = JarUtils.getBytes(jis);
String magic = String.format("%02X", bytes[0]) + String.format("%02X", bytes[1]) + String.format(
"%02X", bytes[2]) + String.format("%02X", bytes[3]);
if (magic.equalsIgnoreCase("cafebabe")) {
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
{
try {
ClassReader cr = new ClassReader(bytes);
ClassNode cn = new ClassNode();

View file

@ -6,6 +6,7 @@ import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.resources.importing.Importer;
import the.bytecode.club.bytecodeviewer.util.FileContainer;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import java.io.File;
import java.io.FileInputStream;
@ -23,12 +24,7 @@ public class ClassResourceImporter implements Importer
try
{
byte[] bytes = JarUtils.getBytes(new FileInputStream(file));
String cafebabe = String.format("%02X", bytes[0])
+ String.format("%02X", bytes[1])
+ String.format("%02X", bytes[2])
+ String.format("%02X", bytes[3]);
if (cafebabe.equalsIgnoreCase("cafebabe"))
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
{
final ClassNode cn = JarUtils.getNode(bytes);

View file

@ -6,6 +6,7 @@ import the.bytecode.club.bytecodeviewer.resources.importing.Import;
import the.bytecode.club.bytecodeviewer.resources.importing.Importer;
import the.bytecode.club.bytecodeviewer.util.FileContainer;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import java.io.File;
import java.nio.file.Files;
@ -74,14 +75,7 @@ public class DirectoryResourceImporter implements Importer
else if (fileName.endsWith(".class"))
{
byte[] bytes = Files.readAllBytes(Paths.get(child.getAbsolutePath()));
String cafebabe = String.format("%02X", bytes[0])
+ String.format("%02X", bytes[1])
+ String.format("%02X", bytes[2])
+ String.format("%02X", bytes[3]);
//check the header for cafebabe
if (cafebabe.equalsIgnoreCase("cafebabe"))
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
{
final ClassNode cn = JarUtils.getNode(bytes);
allDirectoryClasses.put(trimmedPath, cn);

View file

@ -73,10 +73,8 @@ public class JarUtils {
if (!entry.isDirectory())
files.put(name, bytes);
} else {
String cafebabe =
String.format("%02X", bytes[0]) + String.format("%02X", bytes[1]) + String.format("%02X",
bytes[2]) + String.format("%02X", bytes[3]);
if (cafebabe.equalsIgnoreCase("cafebabe")) {
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
{
try {
final ClassNode cn = getNode(bytes);
container.classes.add(cn);
@ -131,9 +129,8 @@ public class JarUtils {
if (!name.endsWith(".class")) {
files.put(name, bytes);
} else {
String cafebabe =
String.format("%02X", bytes[0]) + String.format("%02X", bytes[1]) + String.format("%02X", bytes[2]) + String.format("%02X", bytes[3]);
if (cafebabe.equalsIgnoreCase("cafebabe")) {
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
{
try {
final ClassNode cn = getNode(bytes);
container.classes.add(cn);
@ -164,10 +161,8 @@ public class JarUtils {
final String name = entry.getName();
if (name.endsWith(".class")) {
byte[] bytes = getBytes(jis);
String cafebabe =
String.format("%02X", bytes[0]) + String.format("%02X", bytes[1]) + String.format("%02X",
bytes[2]) + String.format("%02X", bytes[3]);
if (cafebabe.equalsIgnoreCase("cafebabe")) {
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
{
try {
final ClassNode cn = getNode(bytes);
classes.add(cn);

View file

@ -156,6 +156,14 @@ public class MiscUtils
return i;
}
public static String getFileHeader(byte[] fileContents)
{
return String.format("%02X", fileContents[0])
+ String.format("%02X", fileContents[1])
+ String.format("%02X", fileContents[2])
+ String.format("%02X", fileContents[3]);
}
public static String extension(String name) {
return name.substring(name.lastIndexOf('.') + 1);
}