From 2aa4272187425ce3fe6aa8cfcef537ea1f7362ad Mon Sep 17 00:00:00 2001 From: Konloch Date: Sun, 27 Jun 2021 23:45:43 -0700 Subject: [PATCH] Cafebabe Cleanup --- .../CompiledJavaPluginLaunchStrategy.java | 18 +++++++++++------- .../importing/impl/ClassResourceImporter.java | 8 ++------ .../impl/DirectoryResourceImporter.java | 10 ++-------- .../club/bytecodeviewer/util/JarUtils.java | 17 ++++++----------- .../club/bytecodeviewer/util/MiscUtils.java | 8 ++++++++ 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/CompiledJavaPluginLaunchStrategy.java b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/CompiledJavaPluginLaunchStrategy.java index dadc5ca2..da2d1a9a 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/CompiledJavaPluginLaunchStrategy.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/CompiledJavaPluginLaunchStrategy.java @@ -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 loadData(File jarFile) throws Throwable { + private static Set loadData(File jarFile) throws Throwable + { ZipInputStream jis = new ZipInputStream(new FileInputStream(jarFile)); ZipEntry entry; Set 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(); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/ClassResourceImporter.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/ClassResourceImporter.java index b5ce4436..3d3c2791 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/ClassResourceImporter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/ClassResourceImporter.java @@ -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); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java index d6b5a0bd..0437cb59 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java @@ -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); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java index 01a5bb69..ce9f85ea 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java @@ -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); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java index 14c3ade2..87dc3422 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java @@ -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); }