From 3946a9d01c9373381a83e1e5867cbc30eef6ec99 Mon Sep 17 00:00:00 2001 From: Nico Mexis Date: Wed, 13 Dec 2023 11:50:00 +0100 Subject: [PATCH] Temporary fix for remapping types Fixes #27 --- .../googlecode/d2j/reader/DexFileReader.java | 5 +-- .../googlecode/dex2jar/tools/Dex2jarCmd.java | 5 ++- .../dex2jar/tools/Dex2jarMultiThreadCmd.java | 5 ++- .../java/com/googlecode/d2j/dex/Dex2jar.java | 12 +++++- .../d2j/dex/LambadaNameSafeClassAdapter.java | 39 ++++++++++--------- .../googlecode/dex2jar/test/TestUtils.java | 2 +- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/dex-reader/src/main/java/com/googlecode/d2j/reader/DexFileReader.java b/dex-reader/src/main/java/com/googlecode/d2j/reader/DexFileReader.java index 94047fe45..d647a9e51 100755 --- a/dex-reader/src/main/java/com/googlecode/d2j/reader/DexFileReader.java +++ b/dex-reader/src/main/java/com/googlecode/d2j/reader/DexFileReader.java @@ -94,11 +94,10 @@ public class DexFileReader implements BaseDexFileReader { */ public static final int KEEP_CLINIT = 1 << 7; - /** - * keep clinit method when {@link #SKIP_DEBUG} - */ public static final int SKIP_EXCEPTION = 1 << 8; + public static final int DONT_SANITIZE_NAMES = 1 << 9; + // private static final int REVERSE_ENDIAN_CONSTANT = 0x78563412; static final int DBG_END_SEQUENCE = 0x00; diff --git a/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarCmd.java b/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarCmd.java index d4f5ee07b..cb86697f5 100644 --- a/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarCmd.java +++ b/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarCmd.java @@ -58,6 +58,9 @@ public static void main(String... args) { @Opt(opt = "nc", longOpt = "no-code", hasArg = false, description = "") private boolean noCode = false; + @Opt(opt = "dsn", longOpt = "dont-sanitize-names", hasArg = false, description = "do not replace '_' by '-'") + private boolean dontSanitizeNames = false; + @Override protected void doCommandLine() throws Exception { if (remainingArgs.length == 0) { @@ -101,7 +104,7 @@ protected void doCommandLine() throws Exception { BaksmaliBaseDexExceptionHandler handler = notHandleException ? null : new BaksmaliBaseDexExceptionHandler(); Dex2jar.from(reader).withExceptionHandler(handler).reUseReg(reuseReg).topoLogicalSort() .skipDebug(!debugInfo).optimizeSynchronized(this.optmizeSynchronized).printIR(printIR) - .noCode(noCode).skipExceptions(skipExceptions).to(file); + .noCode(noCode).skipExceptions(skipExceptions).dontSanitizeNames(dontSanitizeNames).to(file); if (!notHandleException) { if (handler.hasException()) { diff --git a/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarMultiThreadCmd.java b/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarMultiThreadCmd.java index 4b9962484..6e3c23d6d 100644 --- a/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarMultiThreadCmd.java +++ b/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarMultiThreadCmd.java @@ -40,6 +40,9 @@ public static void main(String... args) { @Opt(opt = "fl", longOpt = "file-list", description = "a file contains a list of dex to process") private Path fileList; + @Opt(opt = "dsn", longOpt = "dont-sanitize-names", hasArg = false, description = "do not replace '_' by '-'") + private boolean dontSanitizeNames = false; + @Override protected void doCommandLine() throws Exception { List f = new ArrayList<>(Arrays.asList(remainingArgs)); @@ -95,7 +98,7 @@ private void run0(String fileName, final ExecutorService executorService) throws @Override public ClassVisitor create(final String name) { final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw); + final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw, dontSanitizeNames); return new ClassVisitor(Constants.ASM_VERSION, rca) { @Override public void visitEnd() { diff --git a/dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2jar.java b/dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2jar.java index c05d28a7a..14aca49e8 100644 --- a/dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2jar.java +++ b/dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2jar.java @@ -75,7 +75,8 @@ public void doTranslate(final Path dist, final ByteArrayOutputStream baos) { @Override public ClassVisitor create(final String name) { final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw); + final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw, + (readerConfig & DexFileReader.DONT_SANITIZE_NAMES) != 0); return new ClassVisitor(Constants.ASM_VERSION, rca) { @Override public void visitEnd() { @@ -314,6 +315,15 @@ public Dex2jar skipExceptions(boolean b) { return this; } + public Dex2jar dontSanitizeNames(boolean b) { + if (b) { + this.readerConfig |= DexFileReader.DONT_SANITIZE_NAMES; + } else { + this.readerConfig &= ~DexFileReader.DONT_SANITIZE_NAMES; + } + return this; + } + public Dex2jar setRandom(Random random) { Dex2jar.random = random; return this; diff --git a/dex-translator/src/main/java/com/googlecode/d2j/dex/LambadaNameSafeClassAdapter.java b/dex-translator/src/main/java/com/googlecode/d2j/dex/LambadaNameSafeClassAdapter.java index f5b2e1490..43b54ffa4 100644 --- a/dex-translator/src/main/java/com/googlecode/d2j/dex/LambadaNameSafeClassAdapter.java +++ b/dex-translator/src/main/java/com/googlecode/d2j/dex/LambadaNameSafeClassAdapter.java @@ -1,35 +1,34 @@ package com.googlecode.d2j.dex; +import com.googlecode.dex2jar.tools.Constants; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.commons.ClassRemapper; import org.objectweb.asm.commons.Remapper; public class LambadaNameSafeClassAdapter extends ClassRemapper { + private final boolean dontSanitizeNames; + public String getClassName() { - return remapper.mapType(className); + return dontSanitizeNames ? className : remapper.mapType(className); } - public LambadaNameSafeClassAdapter(ClassVisitor cv) { - super(cv, new Remapper() { - private String fixName(String name) { - if (name != null) { - return name.replace('-', '_'); - } - return null; - } + private static String fixName(String name) { + if (name == null) return null; + return name.replace('-', '_'); + } - private String[] fixNames(String[] names) { - if (names != null) { - String[] ret = new String[names.length]; - for (int i = 0; i < names.length; ++i) { - ret[i] = fixName(names[i]); - } - return ret; - } - return null; - } + private static String[] fixNames(String[] names) { + if (names == null) return null; + String[] ret = new String[names.length]; + for (int i = 0; i < names.length; ++i) + ret[i] = fixName(names[i]); + return ret; + } + public LambadaNameSafeClassAdapter(ClassVisitor cv, boolean dontSanitizeNames) { + super(Constants.ASM_VERSION, cv, dontSanitizeNames ? new Remapper() { + } : new Remapper() { @Override public String mapType(String type) { return super.mapType(fixName(type)); @@ -85,6 +84,8 @@ public String map(String internalName) { return super.map(fixName(internalName)); } }); + + this.dontSanitizeNames = dontSanitizeNames; } } diff --git a/dex-translator/src/test/java/com/googlecode/dex2jar/test/TestUtils.java b/dex-translator/src/test/java/com/googlecode/dex2jar/test/TestUtils.java index 6512bffdd..e8591e6d8 100644 --- a/dex-translator/src/test/java/com/googlecode/dex2jar/test/TestUtils.java +++ b/dex-translator/src/test/java/com/googlecode/dex2jar/test/TestUtils.java @@ -290,7 +290,7 @@ public void convertCode(DexMethodNode methodNode, MethodVisitor mv, ClzCtx clzCt } }; final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw); + final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw, false); ClassVisitorFactory cvf = classInternalName -> rca; if (fileNode != null) { dex2Asm.convertClass(clzNode, cvf, fileNode);