JADX Decompiler Update

This commit is contained in:
Konloch 2024-10-02 18:49:44 -06:00
parent b1004792b6
commit a094e52add

AI 샘플 코드 생성 중입니다

Loading...

View File

@ -22,17 +22,19 @@ import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler; import jadx.api.JadxDecompiler;
import me.konloch.kontainer.io.DiskReader; import me.konloch.kontainer.io.DiskReader;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.Settings;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI; import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler; import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.util.ExceptionUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import the.bytecode.club.bytecodeviewer.util.TempFile;
import java.io.*; import java.io.*;
import static the.bytecode.club.bytecodeviewer.Constants.*; import static the.bytecode.club.bytecodeviewer.Constants.*;
import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.ERROR; import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.*;
import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.JADX;
/** /**
* JADX Java Wrapper * JADX Java Wrapper
@ -49,96 +51,80 @@ public class JADXDecompiler extends AbstractDecompiler
@Override @Override
public String decompileClassNode(ClassNode cn, byte[] bytes) public String decompileClassNode(ClassNode cn, byte[] bytes)
{ {
String fileStart = TEMP_DIRECTORY + FS; TempFile tempFile = null;
String exception;
String exception = "";
final File tempClass = new File(MiscUtils.getUniqueNameBroken(fileStart, ".class") + ".class");
try (FileOutputStream fos = new FileOutputStream(tempClass))
{
fos.write(bytes);
}
catch (IOException e)
{
BytecodeViewer.handleException(e);
}
File freeDirectory = new File(findUnusedFile(fileStart));
freeDirectory.mkdirs();
try try
{ {
JadxArgs args = new JadxArgs(); //create the temporary files
args.setInputFile(tempClass); tempFile = TempFile.createTemporaryFile(true, ".class");
args.setOutDir(freeDirectory); File tempDirectory = tempFile.getParent();
args.setOutDirSrc(freeDirectory); File tempClassFile = tempFile.getFile();
args.setOutDirRes(freeDirectory);
JadxDecompiler jadx = new JadxDecompiler(args); //write the class-file with bytes
jadx.load(); try (FileOutputStream fos = new FileOutputStream(tempClassFile))
jadx.saveSources();
}
catch (StackOverflowError | Exception e)
{
StringWriter exceptionWriter = new StringWriter();
e.printStackTrace(new PrintWriter(exceptionWriter));
e.printStackTrace();
exception = exceptionWriter.toString();
}
tempClass.delete();
if (freeDirectory.exists())
return findFile(MiscUtils.listFiles(freeDirectory));
if (exception.isEmpty())
exception = "Decompiled source file not found!";
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
}
public String findUnusedFile(String start)
{
long index = 0;
while (true)
{
File f = new File(start + index);
if (!f.exists())
return f.toString();
}
}
public String findFile(File[] fileArray)
{
for (File f : fileArray)
{
if (f.isDirectory())
return findFile(MiscUtils.listFiles(f));
else
{ {
String s; fos.write(bytes);
}
try //setup JADX Args
{ JadxArgs args = new JadxArgs();
s = DiskReader.loadAsString(f.getAbsolutePath()); args.setInputFile(tempClassFile);
} args.setOutDir(tempDirectory);
catch (Exception e) args.setOutDirSrc(tempDirectory);
{ args.setOutDirRes(tempDirectory);
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
e.printStackTrace();
String exception = ExceptionUI.SEND_STACKTRACE_TO_NL + sw;
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception; //init jadx decompiler
} JadxDecompiler jadx = new JadxDecompiler(args);
return s; //load jadx
jadx.load();
//decompile
jadx.saveSources();
//handle simulated errors
if(Constants.DEV_FLAG_DECOMPILERS_SIMULATED_ERRORS)
throw new RuntimeException(DEV_MODE_SIMULATED_ERROR.toString());
return searchForJavaFile(MiscUtils.listFiles(tempDirectory));
}
catch (Throwable e)
{
exception = ExceptionUtils.exceptionToString(e);
}
finally
{
//cleanup temp files
if(tempFile != null)
tempFile.cleanup();
}
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL
+ TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
}
public String searchForJavaFile(File[] files) throws Exception
{
for (File file : files)
{
if (file.isDirectory())
return searchForJavaFile(MiscUtils.listFiles(file));
else if(file.getName().toLowerCase().endsWith(".java"))
{
String contents = DiskReader.loadAsString(file.getAbsolutePath());
//cleanup
if(Settings.DECOMPILERS_AUTOMATICALLY_CLEANUP)
file.delete();
return contents;
} }
} }
return "JADX error!" + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR; return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL
+ TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL
+ "JADX failed to produce any Java files from the provided source.";
} }
@Override @Override