Skip to content

Commit

Permalink
Improve handling of large arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Mar 29, 2022
1 parent ac8ffcc commit 0e26919
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
36 changes: 24 additions & 12 deletions dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.googlecode.dex2jar.ir.ts.VoidInvokeTransformer;
import com.googlecode.dex2jar.ir.ts.ZeroTransformer;
import com.googlecode.dex2jar.ir.ts.array.FillArrayTransformer;
import java.io.IOException;
import com.googlecode.dex2jar.tools.Constants;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -549,32 +549,44 @@ public void convertClass(int dexVersion, DexClassNode classNode, ClassVisitorFac
convertMethod(classNode, methodNode, cv, clzCtx);
}
if (clzCtx.hexDecodeMethodNamePrefix != null) {
addHexDecodeMethod(cv, clzCtx.hexDecodeMethodNamePrefix);
addHexDecodeMethod(cv, classNode.className.replaceFirst("^L", "").replaceAll(";$", ""),
clzCtx.hexDecodeMethodNamePrefix);
}
}
cv.visitEnd();
}

private void addHexDecodeMethod(ClassVisitor outCV, String hexDecodeMethodNameBase) {
private static final Set<String> HEX_DECODE_METHODS =
new HashSet<>(Arrays.asList("decode_J", "decode_I", "decode_S", "decode_B"));

private void addHexDecodeMethod(ClassVisitor outCV, String className, String hexDecodeMethodNameBase) {
// the .data is a class-file compiled from res.Hex
try (InputStream is = Dex2Asm.class.getResourceAsStream("/d2j_hex_decode_stub.data")) {
try (InputStream is = Dex2Asm.class.getResourceAsStream("/res/Hex.class")) {
ClassReader cr = new ClassReader(is);
cr.accept(new ClassVisitor(Opcodes.ASM5) {
cr.accept(new ClassVisitor(Constants.ASM_VERSION) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature,
String[] exceptions) {
if (name.startsWith("decode")) {
return outCV.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
hexDecodeMethodNameBase + "$" + name,
desc, signature, exceptions
);
if (HEX_DECODE_METHODS.contains(name)) {
return new MethodVisitor(Constants.ASM_VERSION,
outCV.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
hexDecodeMethodNameBase + "$" + name,
desc, signature, exceptions
)) {
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor,
boolean isInterface) {
super.visitMethodInsn(opcode, owner.equals("res/Hex") ? className : owner, name,
descriptor, isInterface);
}
};
} else {
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
}, ClassReader.EXPAND_FRAMES);
} catch (IOException e) {
throw new RuntimeException("fail to add hex.decode", e);
} catch (Throwable t) {
throw new RuntimeException("Failed to add Hex.decode_*", t);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package res;

import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class Hex {

public static long[] decode_J(String src) {
byte[] d = decode_B(src);
ByteBuffer b = ByteBuffer.wrap(d);
Expand All @@ -12,6 +17,7 @@ public static long[] decode_J(String src) {
s.get(data);
return data;
}

public static int[] decode_I(String src) {
byte[] d = decode_B(src);
ByteBuffer b = ByteBuffer.wrap(d);
Expand All @@ -38,7 +44,7 @@ public static byte[] decode_B(String src) {
for (int i = 0; i < ret.length; i++) {
char h = d[2 * i];
char l = d[2 * i + 1];
int hh = 0;
int hh;
if (h >= '0' && h <= '9') {
hh = h - '0';
} else if (h >= 'a' && h <= 'f') {
Expand All @@ -48,7 +54,7 @@ public static byte[] decode_B(String src) {
} else {
throw new RuntimeException();
}
int ll = 0;
int ll;
if (l >= '0' && l <= '9') {
ll = h - '0';
} else if (l >= 'a' && l <= 'f') {
Expand All @@ -62,4 +68,5 @@ public static byte[] decode_B(String src) {
}
return ret;
}

}
Binary file not shown.

0 comments on commit 0e26919

Please sign in to comment.