From eb09cebf045544f06370e15f4fc8550479eb3725 Mon Sep 17 00:00:00 2001 From: Konloch Date: Wed, 21 Aug 2024 09:49:13 -0600 Subject: [PATCH] Added FileHeaderUtils --- .../gui/resourcelist/ResourceListPane.java | 3 +- .../CompiledJavaPluginLaunchStrategy.java | 3 +- .../resources/ResourceContainerImporter.java | 3 +- .../importing/impl/ClassResourceImporter.java | 3 +- .../impl/DirectoryResourceImporter.java | 3 +- .../bytecodeviewer/util/FileHeaderUtils.java | 31 +++++++++++++++++++ .../club/bytecodeviewer/util/JarUtils.java | 6 ++-- .../club/bytecodeviewer/util/MiscUtils.java | 9 ------ 8 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 src/main/java/the/bytecode/club/bytecodeviewer/util/FileHeaderUtils.java diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java index c24deecf7..c0ca2b720 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java @@ -37,6 +37,7 @@ import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJTextField; import the.bytecode.club.bytecodeviewer.translation.components.TranslatedVisibleComponent; import the.bytecode.club.bytecodeviewer.util.FileDrop; +import the.bytecode.club.bytecodeviewer.util.FileHeaderUtils; import the.bytecode.club.bytecodeviewer.util.LazyNameUtil; import the.bytecode.club.bytecodeviewer.util.MiscUtils; @@ -346,7 +347,7 @@ public void openPath(TreePath path) } //view classes - if (content != null && MiscUtils.getFileHeaderMagicNumber(content).equalsIgnoreCase("cafebabe") + if (content != null && FileHeaderUtils.doesFileHeaderMatch(content, FileHeaderUtils.JAVA_CLASS_FILE_HEADER) || name.endsWith(".class")) { try 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 4d20c6b82..fb6a08a69 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 @@ -10,6 +10,7 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.api.Plugin; import the.bytecode.club.bytecodeviewer.plugin.PluginLaunchStrategy; +import the.bytecode.club.bytecodeviewer.util.FileHeaderUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; /*************************************************************************** @@ -82,7 +83,7 @@ private static Set loadData(File jarFile) throws Throwable String name = entry.getName(); if (name.endsWith(".class")) { byte[] bytes = MiscUtils.getBytes(jis); - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) { + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { try { ClassReader cr = new ClassReader(bytes); ClassNode cn = new ClassNode(); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceContainerImporter.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceContainerImporter.java index f9f0fe604..b873250ac 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceContainerImporter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceContainerImporter.java @@ -11,6 +11,7 @@ import org.apache.commons.io.FilenameUtils; import org.objectweb.asm.tree.ClassNode; import the.bytecode.club.bytecodeviewer.api.ASMUtil; +import the.bytecode.club.bytecodeviewer.util.FileHeaderUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; /*************************************************************************** @@ -109,7 +110,7 @@ else if (!classesOnly) public ResourceContainerImporter addClassResource(String name, InputStream stream) throws IOException { byte[] bytes = MiscUtils.getBytes(stream); - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { try { 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 15f2306a4..e3e53fe25 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 @@ -7,6 +7,7 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.resources.ResourceContainer; import the.bytecode.club.bytecodeviewer.resources.importing.Importer; +import the.bytecode.club.bytecodeviewer.util.FileHeaderUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; @@ -42,7 +43,7 @@ public void open(File file) throws Exception byte[] bytes = MiscUtils.getBytes(fis); ResourceContainer container = new ResourceContainer(file); - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { 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 3c2f1a90f..3a83ed4ac 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 @@ -13,6 +13,7 @@ import the.bytecode.club.bytecodeviewer.resources.ResourceContainer; import the.bytecode.club.bytecodeviewer.resources.importing.ImportResource; import the.bytecode.club.bytecodeviewer.resources.importing.Importer; +import the.bytecode.club.bytecodeviewer.util.FileHeaderUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; @@ -80,7 +81,7 @@ public void open(File file) throws Exception if (fileName.endsWith(".class")) { byte[] bytes = Files.readAllBytes(Paths.get(child.getAbsolutePath())); - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { final ClassNode cn = JarUtils.getNode(bytes); allDirectoryClasses.put(FilenameUtils.removeExtension(trimmedPath), cn); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/FileHeaderUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/FileHeaderUtils.java new file mode 100644 index 000000000..cc305ec91 --- /dev/null +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/FileHeaderUtils.java @@ -0,0 +1,31 @@ +package the.bytecode.club.bytecodeviewer.util; + +import org.apache.commons.lang3.StringUtils; + +/** + * @author Konloch + * @since 8/21/2024 + */ +public class FileHeaderUtils +{ + public static final int JAVA_CLASS_FILE_HEADER = 0xCAFEBABE; + + public static boolean doesFileHeaderMatch(byte[] bytes, int fileHeader) + { + int bytesHeader = ((bytes[0] & 0xFF) << 24) | + ((bytes[1] & 0xFF) << 16) | + ((bytes[2] & 0xFF) << 8) | + ((bytes[3] & 0xFF)); + + return bytesHeader == fileHeader; + } + + public static String getFileHeaderAsString(byte[] bytes) + { + if(bytes == null || bytes.length < 4) + return StringUtils.EMPTY; + + return String.format("%02X%02X%02X%02X", + bytes[0], bytes[1], bytes[2],bytes[3]); + } +} 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 ee2895ef5..2ba211338 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java @@ -83,7 +83,7 @@ public static void importArchiveA(File jarFile) throws IOException if (!entry.isDirectory()) files.put(name, bytes); } else { - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) { + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { try { final ClassNode cn = getNode(bytes); container.resourceClasses.put(FilenameUtils.removeExtension(name), cn); @@ -138,7 +138,7 @@ public static void importArchiveB(File jarFile) throws IOException if (!name.endsWith(".class")) { files.put(name, bytes); } else { - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { try { final ClassNode cn = getNode(bytes); @@ -171,7 +171,7 @@ public static List loadClasses(File jarFile) throws IOException final String name = entry.getName(); if (name.endsWith(".class")) { byte[] bytes = MiscUtils.getBytes(jis); - if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) { + if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) { 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 6be19c50f..c5c4b46a5 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java @@ -166,15 +166,6 @@ public static int getClassNumber(String start, String ext) { } return i; } - - public static String getFileHeaderMagicNumber(byte[] fileContents) - { - if(fileContents == null || fileContents.length < 4) - return StringUtils.EMPTY; - - return String.format("%02X%02X%02X%02X", fileContents[0], - fileContents[1], fileContents[2],fileContents[3]); - } public static File autoAppendFileExtension(String extension, File file) {