Added FileHeaderUtils
This commit is contained in:
parent
de433f6d84
commit
eb09cebf04
|
@ -37,6 +37,7 @@ import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJCheckB
|
||||||
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJTextField;
|
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJTextField;
|
||||||
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedVisibleComponent;
|
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedVisibleComponent;
|
||||||
import the.bytecode.club.bytecodeviewer.util.FileDrop;
|
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.LazyNameUtil;
|
||||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
||||||
|
|
||||||
|
@ -346,7 +347,7 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File
|
||||||
}
|
}
|
||||||
|
|
||||||
//view classes
|
//view classes
|
||||||
if (content != null && MiscUtils.getFileHeaderMagicNumber(content).equalsIgnoreCase("cafebabe")
|
if (content != null && FileHeaderUtils.doesFileHeaderMatch(content, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)
|
||||||
|| name.endsWith(".class"))
|
|| name.endsWith(".class"))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.objectweb.asm.tree.ClassNode;
|
||||||
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
||||||
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.FileHeaderUtils;
|
||||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
||||||
|
|
||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
|
@ -82,7 +83,7 @@ public class CompiledJavaPluginLaunchStrategy implements PluginLaunchStrategy {
|
||||||
String name = entry.getName();
|
String name = entry.getName();
|
||||||
if (name.endsWith(".class")) {
|
if (name.endsWith(".class")) {
|
||||||
byte[] bytes = MiscUtils.getBytes(jis);
|
byte[] bytes = MiscUtils.getBytes(jis);
|
||||||
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) {
|
if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) {
|
||||||
try {
|
try {
|
||||||
ClassReader cr = new ClassReader(bytes);
|
ClassReader cr = new ClassReader(bytes);
|
||||||
ClassNode cn = new ClassNode();
|
ClassNode cn = new ClassNode();
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.objectweb.asm.tree.ClassNode;
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
|
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
|
||||||
|
import the.bytecode.club.bytecodeviewer.util.FileHeaderUtils;
|
||||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
||||||
|
|
||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
|
@ -109,7 +110,7 @@ public class ResourceContainerImporter
|
||||||
public ResourceContainerImporter addClassResource(String name, InputStream stream) throws IOException
|
public ResourceContainerImporter addClassResource(String name, InputStream stream) throws IOException
|
||||||
{
|
{
|
||||||
byte[] bytes = MiscUtils.getBytes(stream);
|
byte[] bytes = MiscUtils.getBytes(stream);
|
||||||
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe"))
|
if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,6 +7,7 @@ import org.objectweb.asm.tree.ClassNode;
|
||||||
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
||||||
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
|
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
|
||||||
import the.bytecode.club.bytecodeviewer.resources.importing.Importer;
|
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.JarUtils;
|
||||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
||||||
|
|
||||||
|
@ -42,7 +43,7 @@ public class ClassResourceImporter implements Importer
|
||||||
byte[] bytes = MiscUtils.getBytes(fis);
|
byte[] bytes = MiscUtils.getBytes(fis);
|
||||||
ResourceContainer container = new ResourceContainer(file);
|
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);
|
final ClassNode cn = JarUtils.getNode(bytes);
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
||||||
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
|
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
|
||||||
import the.bytecode.club.bytecodeviewer.resources.importing.ImportResource;
|
import the.bytecode.club.bytecodeviewer.resources.importing.ImportResource;
|
||||||
import the.bytecode.club.bytecodeviewer.resources.importing.Importer;
|
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.JarUtils;
|
||||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
||||||
|
|
||||||
|
@ -80,7 +81,7 @@ public class DirectoryResourceImporter implements Importer
|
||||||
if (fileName.endsWith(".class"))
|
if (fileName.endsWith(".class"))
|
||||||
{
|
{
|
||||||
byte[] bytes = Files.readAllBytes(Paths.get(child.getAbsolutePath()));
|
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);
|
final ClassNode cn = JarUtils.getNode(bytes);
|
||||||
allDirectoryClasses.put(FilenameUtils.removeExtension(trimmedPath), cn);
|
allDirectoryClasses.put(FilenameUtils.removeExtension(trimmedPath), cn);
|
||||||
|
|
|
@ -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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -83,7 +83,7 @@ public class JarUtils
|
||||||
if (!entry.isDirectory())
|
if (!entry.isDirectory())
|
||||||
files.put(name, bytes);
|
files.put(name, bytes);
|
||||||
} else {
|
} else {
|
||||||
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) {
|
if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) {
|
||||||
try {
|
try {
|
||||||
final ClassNode cn = getNode(bytes);
|
final ClassNode cn = getNode(bytes);
|
||||||
container.resourceClasses.put(FilenameUtils.removeExtension(name), cn);
|
container.resourceClasses.put(FilenameUtils.removeExtension(name), cn);
|
||||||
|
@ -138,7 +138,7 @@ public class JarUtils
|
||||||
if (!name.endsWith(".class")) {
|
if (!name.endsWith(".class")) {
|
||||||
files.put(name, bytes);
|
files.put(name, bytes);
|
||||||
} else {
|
} else {
|
||||||
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe"))
|
if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER))
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
final ClassNode cn = getNode(bytes);
|
final ClassNode cn = getNode(bytes);
|
||||||
|
@ -171,7 +171,7 @@ public class JarUtils
|
||||||
final String name = entry.getName();
|
final String name = entry.getName();
|
||||||
if (name.endsWith(".class")) {
|
if (name.endsWith(".class")) {
|
||||||
byte[] bytes = MiscUtils.getBytes(jis);
|
byte[] bytes = MiscUtils.getBytes(jis);
|
||||||
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) {
|
if (FileHeaderUtils.doesFileHeaderMatch(bytes, FileHeaderUtils.JAVA_CLASS_FILE_HEADER)) {
|
||||||
try {
|
try {
|
||||||
final ClassNode cn = getNode(bytes);
|
final ClassNode cn = getNode(bytes);
|
||||||
classes.add(cn);
|
classes.add(cn);
|
||||||
|
|
|
@ -166,15 +166,6 @@ public class MiscUtils
|
||||||
}
|
}
|
||||||
return i;
|
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)
|
public static File autoAppendFileExtension(String extension, File file)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user