From 71437eac811058136b99bf59488444ebf03e1475 Mon Sep 17 00:00:00 2001 From: SuspiciousActivity <38253929+SuspiciousActivity@users.noreply.github.com> Date: Thu, 11 Mar 2021 19:05:26 +0100 Subject: [PATCH] Cleanup --- src/me/ByteEdit/boxes/SearchBox.java | 1 - src/me/ByteEdit/decompiler/CFRDecompiler.java | 42 +------ .../decompiler/FernflowerDecompiler.java | 5 +- .../decompiler/ProcyonDecompiler.java | 8 +- src/me/ByteEdit/edit/Assembler.java | 64 ++++++----- src/me/ByteEdit/edit/Disassembler.java | 103 +++++++++--------- src/me/ByteEdit/edit/SearchBox.java | 1 - src/me/ByteEdit/main/ArchiveTreeModel.java | 2 +- src/me/ByteEdit/main/HugeStringsToolTips.java | 3 +- src/me/ByteEdit/main/Main.java | 20 +--- src/me/ByteEdit/utils/ClassUtil.java | 9 +- src/me/ByteEdit/utils/UnicodeUtils.java | 6 +- 12 files changed, 106 insertions(+), 158 deletions(-) diff --git a/src/me/ByteEdit/boxes/SearchBox.java b/src/me/ByteEdit/boxes/SearchBox.java index f7fce4f..be033c4 100644 --- a/src/me/ByteEdit/boxes/SearchBox.java +++ b/src/me/ByteEdit/boxes/SearchBox.java @@ -125,7 +125,6 @@ public void replaceFind() { public void replace() { if (!Main.decompiler.isEditable()) return; - String txt = Main.txtByteEditView.getText(); int startPos = Main.txtByteEditView.getSelectionStart(); Main.txtByteEditView.replaceSelection(txtReplace.getText()); Main.txtByteEditView.select(startPos, startPos + txtReplace.getText().length()); diff --git a/src/me/ByteEdit/decompiler/CFRDecompiler.java b/src/me/ByteEdit/decompiler/CFRDecompiler.java index e6e0280..becdaaa 100644 --- a/src/me/ByteEdit/decompiler/CFRDecompiler.java +++ b/src/me/ByteEdit/decompiler/CFRDecompiler.java @@ -3,24 +3,13 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; -import java.lang.reflect.Field; import java.util.Collection; import java.util.HashMap; -import java.util.Map; import org.benf.cfr.reader.PluginRunner; import org.benf.cfr.reader.api.ClassFileSource; import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair; -import org.benf.cfr.reader.entities.ClassFile; -import org.benf.cfr.reader.entities.Method; -import org.benf.cfr.reader.entities.constantpool.ConstantPool; -import org.benf.cfr.reader.state.ClassFileSourceImpl; -import org.benf.cfr.reader.state.DCCommonState; -import org.benf.cfr.reader.util.bytestream.BaseByteData; -import org.benf.cfr.reader.util.getopt.OptionsImpl; -import org.benf.cfr.reader.util.output.ToStringDumper; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodNode; public class CFRDecompiler implements IDecompiler { @@ -83,10 +72,10 @@ public class CFRDecompiler implements IDecompiler { @Override public String decompile(ClassNode cn) { - return doDecompilation(cn, getBytes(cn), null); + return doDecompilation(cn, getBytes(cn)); } - private static String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) { + private static String doDecompilation(ClassNode cn, byte[] b) { try { HashMap ops = options; ClassFileSource cfs = new ClassFileSource() { @@ -114,24 +103,7 @@ public Collection addJar(String arg0) { throw new RuntimeException(); } }; - PluginRunner runner = new PluginRunner(ops, cfs); - if (mn != null) { - BaseByteData data = new BaseByteData(b); - ClassFile cf = new ClassFile(data, "", initDCState(ops, cfs)); - Field cpf = Method.class.getDeclaredField("cp"); - Field descI = Method.class.getDeclaredField("descriptorIndex"); - descI.setAccessible(true); - cpf.setAccessible(true); - for (Method m : cf.getMethodByName(mn.name)) { - ConstantPool cp = (ConstantPool) cpf.get(m); - if (cp.getUTF8Entry(descI.getInt(m)).getValue().equals(mn.desc)) { - ToStringDumper tsd = new ToStringDumper(); - m.dump(tsd, true); - return tsd.toString(); - } - } - } - String decompilation = runner.getDecompilationFor(cn.name); + String decompilation = new PluginRunner(ops, cfs).getDecompilationFor(cn.name); System.gc(); // cfr has a performance bug return decompilation; } catch (Exception e) { @@ -142,12 +114,4 @@ public Collection addJar(String arg0) { return sw.toString(); } } - - private static DCCommonState initDCState(Map optionsMap, ClassFileSource classFileSource) { - OptionsImpl options = new OptionsImpl(optionsMap); - if (classFileSource == null) - classFileSource = new ClassFileSourceImpl(options); - DCCommonState dcCommonState = new DCCommonState(options, classFileSource); - return dcCommonState; - } } diff --git a/src/me/ByteEdit/decompiler/FernflowerDecompiler.java b/src/me/ByteEdit/decompiler/FernflowerDecompiler.java index b5dad7c..1649784 100644 --- a/src/me/ByteEdit/decompiler/FernflowerDecompiler.java +++ b/src/me/ByteEdit/decompiler/FernflowerDecompiler.java @@ -17,7 +17,6 @@ import org.jetbrains.java.decompiler.struct.StructContext; import org.jetbrains.java.decompiler.struct.lazy.LazyLoader; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodNode; public class FernflowerDecompiler implements IBytecodeProvider, IResultSaver, IDecompiler { @@ -53,10 +52,10 @@ public class FernflowerDecompiler implements IBytecodeProvider, IResultSaver, ID @Override public String decompile(ClassNode cn) { - return doDecompilation(cn, getBytes(cn), null); + return doDecompilation(cn, getBytes(cn)); } - public String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) { + public String doDecompilation(ClassNode cn, byte[] b) { try { // TODO decompile method only this.bytes = b; diff --git a/src/me/ByteEdit/decompiler/ProcyonDecompiler.java b/src/me/ByteEdit/decompiler/ProcyonDecompiler.java index 7feb7f2..06d10e0 100644 --- a/src/me/ByteEdit/decompiler/ProcyonDecompiler.java +++ b/src/me/ByteEdit/decompiler/ProcyonDecompiler.java @@ -5,7 +5,6 @@ import java.lang.reflect.Field; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodNode; import com.strobel.assembler.InputTypeLoader; import com.strobel.assembler.metadata.Buffer; @@ -21,10 +20,10 @@ public class ProcyonDecompiler implements IDecompiler { @Override public String decompile(ClassNode cn) { - return doDecompilation(cn, getBytes(cn), null); + return doDecompilation(cn, getBytes(cn)); } - private static String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) { + private static String doDecompilation(ClassNode cn, byte[] b) { try { // TODO decompile method only DecompilerSettings settings = new DecompilerSettings(); @@ -48,9 +47,8 @@ public boolean tryLoadType(String s, Buffer buffer) { buffer.putByteArray(b, 0, b.length); buffer.position(0); return true; - } else { - return backLoader.tryLoadType(s, buffer); } + return backLoader.tryLoadType(s, buffer); } }); TypeReference type = metadataSystem.lookupType(cn.name); diff --git a/src/me/ByteEdit/edit/Assembler.java b/src/me/ByteEdit/edit/Assembler.java index 278129d..c7aa0aa 100644 --- a/src/me/ByteEdit/edit/Assembler.java +++ b/src/me/ByteEdit/edit/Assembler.java @@ -339,6 +339,8 @@ public static ClassNode assemble(String input) { tryCatchBlocksToParse.clear(); stage = 0; } else if (s.startsWith("// #Signature: ")) { + if (node == null) + throw new IllegalStateException("node is null"); node.signature = UnicodeUtils.unescape(hsr, s.substring(15)); } else if (s.equals("// #TryCatch:")) { stage = 1; @@ -352,6 +354,8 @@ public static ClassNode assemble(String input) { localVarsToParse.add(s); } } else if (s.equals("}")) { + if (node == null) + throw new IllegalStateException("node is null"); if (!annotationsForNext.isEmpty()) { if (node.visibleAnnotations == null) { node.visibleAnnotations = new ArrayList<>(); @@ -404,6 +408,8 @@ public static ClassNode assemble(String input) { } clazz.methods.add(node); } else if (!s.startsWith("\t")) { + if (node == null) + throw new IllegalStateException("node is null"); s = s.substring(0, s.length() - 2); if (s.contains(" throws ")) { String exceptions = s.substring(s.lastIndexOf(" throws ") + 8); @@ -477,6 +483,8 @@ public static ClassNode assemble(String input) { s = s.substring(1); if (!temp.isEmpty()) { if (s.equals("]")) { + if (node == null) + throw new IllegalStateException("node is null"); temp += s; node.instructions.add(getNode(hsr, temp, methodLabelMap)); temp = ""; @@ -487,6 +495,8 @@ public static ClassNode assemble(String input) { if (s.endsWith("[")) { temp += s + "\n"; } else { + if (node == null) + throw new IllegalStateException("node is null"); node.instructions.add(getNode(hsr, s, methodLabelMap)); } } @@ -508,7 +518,8 @@ public static ClassNode assemble(String input) { return null; } finally { try { - read.close(); + if (read != null) + read.close(); } catch (Exception e) { } } @@ -1559,38 +1570,37 @@ public LabelNode apply(SwitchIntContainer t, LabelNode u) { private static Object[] parseFrameList(HugeStringsRev hsr, String str, HashMap labels) { ArrayList list = new ArrayList<>(); String l = str.substring(1, str.length() - 1); - if (l.isEmpty()) { + if (l.isEmpty()) return EMPTY_OBJECT_ARRAY; - } else { - for (String asd : l.split(", ")) { - if (!asd.startsWith("(")) { - int frameType = ClassUtil.getFrameTypeByName(asd); - if (frameType != -1) - list.add(Integer.valueOf(frameType)); - else - list.add(UnicodeUtils.unescape(hsr, asd.substring(1, asd.length() - 1))); - } else { - if (asd.startsWith("(label) ")) { - int labelNr = Integer.parseInt(asd.split(" ")[1]); - boolean found = false; - for (Map.Entry entry : labels.entrySet()) { - if (entry.getValue() == labelNr) { - list.add(entry.getKey()); - found = true; - } - } - if (!found) { - LabelNode ln = new LabelNode(new Label()); - labels.put(ln, labelNr); - list.add(ln); + + for (String asd : l.split(", ")) { + if (!asd.startsWith("(")) { + int frameType = ClassUtil.getFrameTypeByName(asd); + if (frameType != -1) + list.add(Integer.valueOf(frameType)); + else + list.add(UnicodeUtils.unescape(hsr, asd.substring(1, asd.length() - 1))); + } else { + if (asd.startsWith("(label) ")) { + int labelNr = Integer.parseInt(asd.split(" ")[1]); + boolean found = false; + for (Map.Entry entry : labels.entrySet()) { + if (entry.getValue() == labelNr) { + list.add(entry.getKey()); + found = true; } - } else { - list.add(ClassUtil.getCastedValue(asd.split(" ")[1], asd.split("\\) ")[0].substring(1))); } + if (!found) { + LabelNode ln = new LabelNode(new Label()); + labels.put(ln, labelNr); + list.add(ln); + } + } else { + list.add(ClassUtil.getCastedValue(asd.split(" ")[1], asd.split("\\) ")[0].substring(1))); } } - return list.toArray(new Object[list.size()]); } + return list.toArray(new Object[list.size()]); } } diff --git a/src/me/ByteEdit/edit/Disassembler.java b/src/me/ByteEdit/edit/Disassembler.java index b103f32..e2ac21e 100644 --- a/src/me/ByteEdit/edit/Disassembler.java +++ b/src/me/ByteEdit/edit/Disassembler.java @@ -127,7 +127,7 @@ private static String disassemble(ClassNode classNode) { ctx.next("\n// #Fields\n"); ctx.next(doFields(classNode.fields, hs)); ctx.next("\n// #Methods\n"); - ctx.next(doMethods(classNode.methods, classNode.name, hs)); + ctx.next(doMethods(classNode.methods, hs)); ctx.next("}\n"); if (!hs.isEmpty()) @@ -154,48 +154,47 @@ private static String doAnnotations(List annotations, HugeString s += o + " = ["; valBefore = false; continue; - } else { - if (o instanceof String[]) { - String[] arr = (String[]) o; - boolean w8ing = false; - for (String rofl : arr) { - if (!w8ing) { - s += UnicodeUtils.escapeWithSpaces(hs, rofl) + "/"; - w8ing = true; - } else { - s += UnicodeUtils.escapeWithSpaces(hs, rofl) + "]"; - } + } + if (o instanceof String[]) { + String[] arr = (String[]) o; + boolean w8ing = false; + for (String rofl : arr) { + if (!w8ing) { + s += UnicodeUtils.escapeWithSpaces(hs, rofl) + "/"; + w8ing = true; + } else { + s += UnicodeUtils.escapeWithSpaces(hs, rofl) + "]"; } - s += ", "; - } else if (o instanceof List) { - List list = (List) o; - s += "{ "; - for (Object obj : list) { - if (obj instanceof String[]) { - String[] arr = (String[]) obj; - boolean w8ing = false; - for (String rofl : arr) { - if (!w8ing) { - s += UnicodeUtils.escapeWithSpaces(hs, rofl) + "/"; - w8ing = true; - } else { - s += UnicodeUtils.escapeWithSpaces(hs, rofl); - } + } + s += ", "; + } else if (o instanceof List) { + List list = (List) o; + s += "{ "; + for (Object obj : list) { + if (obj instanceof String[]) { + String[] arr = (String[]) obj; + boolean w8ing = false; + for (String rofl : arr) { + if (!w8ing) { + s += UnicodeUtils.escapeWithSpaces(hs, rofl) + "/"; + w8ing = true; + } else { + s += UnicodeUtils.escapeWithSpaces(hs, rofl); } - } else { - s += "\"" + UnicodeUtils.escape(hs, String.valueOf(obj)) + "\""; } - s += ", "; + } else { + s += "\"" + UnicodeUtils.escape(hs, String.valueOf(obj)) + "\""; } - s = s.substring(0, s.length() - 2); - s += " }], "; - } else if (o instanceof String) { - s += "\"" + UnicodeUtils.escapeWithSpaces(hs, (String) o) + "\"], "; - } else { - s += "(" + o.getClass().getName().replace(".", "/") + ") " + o + "], "; + s += ", "; } - valBefore = true; + s = s.substring(0, s.length() - 2); + s += " }], "; + } else if (o instanceof String) { + s += "\"" + UnicodeUtils.escapeWithSpaces(hs, (String) o) + "\"], "; + } else { + s += "(" + o.getClass().getName().replace(".", "/") + ") " + o + "], "; } + valBefore = true; } if (s.endsWith(", ")) s = s.substring(0, s.length() - 2); @@ -207,7 +206,7 @@ private static String doAnnotations(List annotations, HugeString return ctx.finish(); } - private static String doMethods(List methods, String className, HugeStrings hs) + private static String doMethods(List methods, HugeStrings hs) throws InterruptedException, ExecutionException { boolean multithreaded = Main.INSTANCE.mntmMultithreaded.isSelected(); Future[] futures = new Future[methods.size()]; @@ -221,7 +220,7 @@ public String call() throws Exception { ms += "\t// #Max: l:" + mn.maxLocals + " s:" + mn.maxStack + "\n"; if (mn.signature != null) ms += "\t// #Signature: " + UnicodeUtils.escapeWithSpaces(hs, mn.signature) + "\n"; - String[] dis = disassembleMethod(className, mn, hs); + String[] dis = disassembleMethod(mn, hs); ms += dis[2]; ms += dis[1]; if (mn.visibleAnnotations != null && !mn.visibleAnnotations.isEmpty()) @@ -286,7 +285,7 @@ private static String doFields(List fields, HugeStrings hs) { return ctx.finish(); } - private static String[] disassembleMethod(String className, MethodNode mn, HugeStrings hs) { + private static String[] disassembleMethod(MethodNode mn, HugeStrings hs) { HashMap labels = new HashMap(); String localVarTable; String tryCatchTable; @@ -299,7 +298,7 @@ private static String[] disassembleMethod(String className, MethodNode mn, HugeS for (AbstractInsnNode n = insnList.getFirst(); n != null; n = n.getNext()) { if (n instanceof LabelNode) { int id = labels.size() + 1; - if (deobfNumbers) + if (deobfNumbers && labelsReversed != null) labelsReversed.add(((LabelNode) n).getLabel()); labels.put(((LabelNode) n).getLabel(), id); } @@ -336,7 +335,7 @@ private static String[] disassembleMethod(String className, MethodNode mn, HugeS insnList = calc.get(); int i = 0; for (AbstractInsnNode n = insnList.getFirst(); n != null; n = n.getNext()) { - if (n instanceof LabelNode) { + if (n instanceof LabelNode && labelsReversed != null) { ((LabelNode) n).setLabel(labelsReversed.get(i++)); } } @@ -359,9 +358,8 @@ public static String disassembleInstruction(AbstractInsnNode n, HashMap selectFile(((ByteEditTreeNode) e.getPath().getLastPathComponent()).path)); - } - } - } - }); + tree.addTreeSelectionListener(e -> SingleThreadedExecutor + .execute(() -> selectFile(((ByteEditTreeNode) e.getPath().getLastPathComponent()).path))); new DropTarget(tree, new DropTargetListener() { @Override @@ -760,7 +748,7 @@ public void setRenameInfo(Matcher m, int lineEnd) throws BadLocationException { } } - private void saveCurrentClassNode() { + private static void saveCurrentClassNode() { String txt = txtByteEditView.getText(); for (String s : txt.split("\\/\\/ #Annotations:\n")) { if (s.isEmpty()) diff --git a/src/me/ByteEdit/utils/ClassUtil.java b/src/me/ByteEdit/utils/ClassUtil.java index 5069bd5..25e908e 100644 --- a/src/me/ByteEdit/utils/ClassUtil.java +++ b/src/me/ByteEdit/utils/ClassUtil.java @@ -320,11 +320,9 @@ public static String getAccessFlagsFull(int access) { s += "enum "; acc ^= ACC_ENUM; } - if (acc == access) { + if (acc == access) return s; - } else { - return "0x" + Integer.toHexString(access) + " "; - } + return "0x" + Integer.toHexString(access) + " "; } /** @@ -544,8 +542,7 @@ public static String getDecompiledValue(Object o, String desc, boolean escapeSpa DecompilationInterface di = diMap.get(o.getClass()); if (di != null) return di.apply(o, desc, escapeSpaces, hs); - else - return o.toString(); + return o.toString(); } public static String getDecompiledValue(Object o, String desc, HugeStrings hs) { diff --git a/src/me/ByteEdit/utils/UnicodeUtils.java b/src/me/ByteEdit/utils/UnicodeUtils.java index eedf30d..3a9fde8 100644 --- a/src/me/ByteEdit/utils/UnicodeUtils.java +++ b/src/me/ByteEdit/utils/UnicodeUtils.java @@ -84,11 +84,9 @@ public static String unescape(HugeStringsRev hsr, String s, boolean force) { sb.append(c); } if (stage != 0) { - if (unicodeBuffer != 0) { + if (unicodeBuffer != 0) throw new IllegalArgumentException("The unicode at the end is incomplete."); - } else { - throw new IllegalArgumentException("'\\' at the end is invalid."); - } + throw new IllegalArgumentException("'\\' at the end is invalid."); } return sb.toString(); }