Skip to content

Commit

Permalink
Temporary fix for remapping types
Browse files Browse the repository at this point in the history
Fixes #27
  • Loading branch information
ThexXTURBOXx committed Dec 13, 2023
1 parent 406c438 commit 3946a9d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> f = new ArrayList<>(Arrays.asList(remainingArgs));
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 11 additions & 1 deletion dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
Expand Down Expand Up @@ -85,6 +84,8 @@ public String map(String internalName) {
return super.map(fixName(internalName));
}
});

this.dontSanitizeNames = dontSanitizeNames;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3946a9d

Please sign in to comment.