From 7a1ae63f9eb0befd4b8130a9c9862aa2e35472a6 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Fri, 11 Oct 2024 21:06:42 +0200 Subject: [PATCH] some more things that were moved to fork of SSVM and some refactoring --- .../objectweb/asm/tree/AbstractInsnNode.java | 2 +- .../deobfuscator/api}/asm/NamedOpcodes.java | 8 +- .../api/asm/matcher/impl/StackMatch.java | 1 - .../deobfuscator/api/helper/MethodHelper.java | 2 +- .../main/java/dev/xdark/ssvm/util/IOUtil.java | 92 ------------------- .../deobfuscator/helper/ReflectionHelper.java | 25 ----- 6 files changed, 7 insertions(+), 123 deletions(-) rename deobfuscator-api/src/main/java/{org/objectweb => uwu/narumi/deobfuscator/api}/asm/NamedOpcodes.java (85%) delete mode 100644 deobfuscator-impl/src/main/java/dev/xdark/ssvm/util/IOUtil.java delete mode 100644 deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/helper/ReflectionHelper.java diff --git a/deobfuscator-api/src/main/java/org/objectweb/asm/tree/AbstractInsnNode.java b/deobfuscator-api/src/main/java/org/objectweb/asm/tree/AbstractInsnNode.java index 886156f1..0efddd64 100644 --- a/deobfuscator-api/src/main/java/org/objectweb/asm/tree/AbstractInsnNode.java +++ b/deobfuscator-api/src/main/java/org/objectweb/asm/tree/AbstractInsnNode.java @@ -37,7 +37,7 @@ import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.NamedOpcodes; +import uwu.narumi.deobfuscator.api.asm.NamedOpcodes; import org.objectweb.asm.Type; /** diff --git a/deobfuscator-api/src/main/java/org/objectweb/asm/NamedOpcodes.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/NamedOpcodes.java similarity index 85% rename from deobfuscator-api/src/main/java/org/objectweb/asm/NamedOpcodes.java rename to deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/NamedOpcodes.java index f1966f70..6f39c358 100644 --- a/deobfuscator-api/src/main/java/org/objectweb/asm/NamedOpcodes.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/NamedOpcodes.java @@ -1,11 +1,13 @@ -package org.objectweb.asm; +package uwu.narumi.deobfuscator.api.asm; + +import org.objectweb.asm.Opcodes; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; -/* -Debug purposes +/** + * Maps opcodes to their names. For debug purposes. */ public final class NamedOpcodes { diff --git a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/matcher/impl/StackMatch.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/matcher/impl/StackMatch.java index 721370c7..432635d9 100644 --- a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/matcher/impl/StackMatch.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/matcher/impl/StackMatch.java @@ -1,6 +1,5 @@ package uwu.narumi.deobfuscator.api.asm.matcher.impl; -import org.objectweb.asm.NamedOpcodes; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.analysis.Frame; import org.objectweb.asm.tree.analysis.OriginalSourceValue; diff --git a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/helper/MethodHelper.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/helper/MethodHelper.java index 58dd24ff..f97981dd 100644 --- a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/helper/MethodHelper.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/helper/MethodHelper.java @@ -2,7 +2,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; -import org.objectweb.asm.NamedOpcodes; +import uwu.narumi.deobfuscator.api.asm.NamedOpcodes; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; diff --git a/deobfuscator-impl/src/main/java/dev/xdark/ssvm/util/IOUtil.java b/deobfuscator-impl/src/main/java/dev/xdark/ssvm/util/IOUtil.java deleted file mode 100644 index 0bcbe561..00000000 --- a/deobfuscator-impl/src/main/java/dev/xdark/ssvm/util/IOUtil.java +++ /dev/null @@ -1,92 +0,0 @@ -package dev.xdark.ssvm.util; - -import java.io.ByteArrayOutputStream; -import java.io.FileDescriptor; -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Field; -import uwu.narumi.deobfuscator.helper.ReflectionHelper; - -/** - * Hacks and utils for io java classes - * - * @author Justus Garbe - */ -public final class IOUtil { - - /** - * Returns either the file descriptor of a {@link FileDescriptor} object or a handle depending on - * the platform. - * - * @param fd the file descriptor - * @return the file descriptor or handle - */ - public static long getHandleOrFd(FileDescriptor fd) { - try { - // return fdHandle == null ? fdField.getInt(fd) : fdHandle.getLong(fd); - return fdHandleOffset == -1 - ? ReflectionHelper.getUnsafe().getInt(fd, fdFieldOffset) - : ReflectionHelper.getUnsafe().getLong(fd, fdHandleOffset); - } catch (Exception e) { - throw new IllegalStateException("Unable to get handle from FileDescriptor", e); - } - } - - private static final Field fdField; - private static final Field fdHandle; - - private static final long fdFieldOffset; - private static final long fdHandleOffset; - - static { - Field fd = null; - Field handle = null; - long fdOffset = -1; - long handleOffset = -1; - try { - fd = FileDescriptor.class.getDeclaredField("fd"); - // fd.setAccessible(true); - fdOffset = ReflectionHelper.getUnsafe().objectFieldOffset(fd); - } catch (NoSuchFieldException e) { - // ignore - } - try { - handle = FileDescriptor.class.getDeclaredField("handle"); - // handle.setAccessible(true); - handleOffset = ReflectionHelper.getUnsafe().objectFieldOffset(handle); - } catch (NoSuchFieldException e) { - // ignore - } - - fdField = fd; - fdHandle = handle; - fdFieldOffset = fdOffset; - fdHandleOffset = handleOffset; - } - - /** - * @param inputStream Input stream, may be {@code null}. - * @return Bytes of stream, or {@code null} if the stream was {@code null} - * @throws IOException When the stream cannot be read from. - */ - public static byte[] readAll(InputStream inputStream) throws IOException { - if (inputStream == null) return null; - int bufferSize = 2048; - try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { - byte[] data = new byte[bufferSize]; - int bytesRead; - int readCount = 0; - while ((bytesRead = inputStream.read(data, 0, bufferSize)) != -1) { - outputStream.write(data, 0, bytesRead); - readCount++; - } - outputStream.flush(); - if (readCount == 1) { - return data; - } - return outputStream.toByteArray(); - } finally { - inputStream.close(); - } - } -} diff --git a/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/helper/ReflectionHelper.java b/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/helper/ReflectionHelper.java deleted file mode 100644 index 1c3abb90..00000000 --- a/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/helper/ReflectionHelper.java +++ /dev/null @@ -1,25 +0,0 @@ -package uwu.narumi.deobfuscator.helper; - -import java.lang.reflect.Field; -import sun.misc.Unsafe; - -public final class ReflectionHelper { - - private static final Unsafe unsafe; - - static { - try { - Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); - theUnsafe.setAccessible(true); - unsafe = (Unsafe) theUnsafe.get(null); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - private ReflectionHelper() {} - - public static Unsafe getUnsafe() { - return unsafe; - } -}