Skip to content

Commit

Permalink
Added FileHeaderUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Konloch committed Aug 21, 2024
1 parent de433f6 commit eb09ceb
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/***************************************************************************
Expand Down Expand Up @@ -82,7 +83,7 @@ private static Set<LoadedNodeData> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/***************************************************************************
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -171,7 +171,7 @@ public static List<ClassNode> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit eb09ceb

Please sign in to comment.