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.api.Plugin;
import the.bytecode.club.bytecodeviewer.plugin.PluginLaunchStrategy; import the.bytecode.club.bytecodeviewer.plugin.PluginLaunchStrategy;
import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -71,20 +72,23 @@ public class CompiledJavaPluginLaunchStrategy implements PluginLaunchStrategy {
return loaded; 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)); ZipInputStream jis = new ZipInputStream(new FileInputStream(jarFile));
ZipEntry entry; ZipEntry entry;
Set<LoadedNodeData> set = new HashSet<>(); Set<LoadedNodeData> set = new HashSet<>();
while ((entry = jis.getNextEntry()) != null) { while ((entry = jis.getNextEntry()) != null)
try { {
try
{
String name = entry.getName(); String name = entry.getName();
if (name.endsWith(".class")) { if (name.endsWith(".class"))
{
byte[] bytes = JarUtils.getBytes(jis); byte[] bytes = JarUtils.getBytes(jis);
String magic = String.format("%02X", bytes[0]) + String.format("%02X", bytes[1]) + String.format( if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
"%02X", bytes[2]) + String.format("%02X", bytes[3]); {
if (magic.equalsIgnoreCase("cafebabe")) {
try { try {
ClassReader cr = new ClassReader(bytes); ClassReader cr = new ClassReader(bytes);
ClassNode cn = new ClassNode(); 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.resources.importing.Importer;
import the.bytecode.club.bytecodeviewer.util.FileContainer; import the.bytecode.club.bytecodeviewer.util.FileContainer;
import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -23,12 +24,7 @@ public class ClassResourceImporter implements Importer
try try
{ {
byte[] bytes = JarUtils.getBytes(new FileInputStream(file)); byte[] bytes = JarUtils.getBytes(new FileInputStream(file));
String cafebabe = String.format("%02X", bytes[0]) if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
+ String.format("%02X", bytes[1])
+ String.format("%02X", bytes[2])
+ String.format("%02X", bytes[3]);
if (cafebabe.equalsIgnoreCase("cafebabe"))
{ {
final ClassNode cn = JarUtils.getNode(bytes); 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.resources.importing.Importer;
import the.bytecode.club.bytecodeviewer.util.FileContainer; import the.bytecode.club.bytecodeviewer.util.FileContainer;
import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import java.io.File; import java.io.File;
import java.nio.file.Files; import java.nio.file.Files;
@ -74,14 +75,7 @@ public class DirectoryResourceImporter implements Importer
else if (fileName.endsWith(".class")) else if (fileName.endsWith(".class"))
{ {
byte[] bytes = Files.readAllBytes(Paths.get(child.getAbsolutePath())); byte[] bytes = Files.readAllBytes(Paths.get(child.getAbsolutePath()));
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
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"))
{ {
final ClassNode cn = JarUtils.getNode(bytes); final ClassNode cn = JarUtils.getNode(bytes);
allDirectoryClasses.put(trimmedPath, cn); allDirectoryClasses.put(trimmedPath, cn);

View file

@ -73,10 +73,8 @@ public class JarUtils {
if (!entry.isDirectory()) if (!entry.isDirectory())
files.put(name, bytes); files.put(name, bytes);
} else { } else {
String cafebabe = if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("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")) {
try { try {
final ClassNode cn = getNode(bytes); final ClassNode cn = getNode(bytes);
container.classes.add(cn); container.classes.add(cn);
@ -131,9 +129,8 @@ public class JarUtils {
if (!name.endsWith(".class")) { if (!name.endsWith(".class")) {
files.put(name, bytes); files.put(name, bytes);
} else { } else {
String cafebabe = if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("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")) {
try { try {
final ClassNode cn = getNode(bytes); final ClassNode cn = getNode(bytes);
container.classes.add(cn); container.classes.add(cn);
@ -164,10 +161,8 @@ public class JarUtils {
final String name = entry.getName(); final String name = entry.getName();
if (name.endsWith(".class")) { if (name.endsWith(".class")) {
byte[] bytes = getBytes(jis); byte[] bytes = getBytes(jis);
String cafebabe = if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("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")) {
try { try {
final ClassNode cn = getNode(bytes); final ClassNode cn = getNode(bytes);
classes.add(cn); classes.add(cn);

View file

@ -156,6 +156,14 @@ public class MiscUtils
return i; 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) { public static String extension(String name) {
return name.substring(name.lastIndexOf('.') + 1); return name.substring(name.lastIndexOf('.') + 1);
} }