Search in sources :

Example 1 with DexFileReader

use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.

the class SkipDupMethod method test.

@Test
public void test() throws IOException {
    InputStream is = SkipDupMethod.class.getClassLoader().getResourceAsStream("i200.dex");
    Assert.assertNotNull(is);
    DexFileReader reader = new DexFileReader(is);
    DexFileNode dfn1 = new DexFileNode();
    reader.accept(dfn1, DexFileReader.KEEP_ALL_METHODS);
    DexFileNode dfn2 = new DexFileNode();
    reader.accept(dfn2, 0);
    Assert.assertTrue(dfn1.clzs.get(0).methods.size() > dfn2.clzs.get(0).methods.size());
}
Also used : InputStream(java.io.InputStream) DexFileNode(com.googlecode.d2j.node.DexFileNode) DexFileReader(com.googlecode.d2j.reader.DexFileReader) Test(org.junit.Test)

Example 2 with DexFileReader

use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.

the class DexWeaverCmd method doCommandLine.

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length == 0) {
        throw new HelpException("no odex");
    }
    final Map<String, Method> map = new HashMap<>();
    for (String ln : Files.readAllLines(config, StandardCharsets.UTF_8)) {
        if (ln.startsWith("#") || ln.length() == 0) {
            continue;
        }
        String[] x = ln.split("=");
        map.put(x[0], parseMethod(x[1]));
    }
    DexFileWriter out = new DexFileWriter();
    DexFileVisitor fv = new DexFileVisitor(out) {

        @Override
        public DexClassVisitor visit(int access_flags, String className, String superClass, String[] interfaceNames) {
            DexClassVisitor dcv = super.visit(access_flags, className, superClass, interfaceNames);
            if (dcv != null) {
                return new DexClassVisitor(dcv) {

                    @Override
                    public DexMethodVisitor visitMethod(int accessFlags, Method method) {
                        DexMethodVisitor dmv = super.visitMethod(accessFlags, method);
                        if (dmv != null) {
                            return new DexMethodVisitor(dmv) {

                                @Override
                                public DexCodeVisitor visitCode() {
                                    DexCodeVisitor code = super.visitCode();
                                    if (code != null) {
                                        return new DexCodeVisitor(code) {

                                            @Override
                                            public void visitMethodStmt(Op op, int[] args, Method method) {
                                                Method replaceTo = map.get(method.toString());
                                                if (replaceTo != null) {
                                                    switch(op) {
                                                        case INVOKE_DIRECT:
                                                        case INVOKE_INTERFACE:
                                                        case INVOKE_STATIC:
                                                        case INVOKE_SUPER:
                                                        case INVOKE_VIRTUAL:
                                                            super.visitMethodStmt(Op.INVOKE_STATIC, args, replaceTo);
                                                            break;
                                                        case INVOKE_DIRECT_RANGE:
                                                        case INVOKE_INTERFACE_RANGE:
                                                        case INVOKE_STATIC_RANGE:
                                                        case INVOKE_SUPER_RANGE:
                                                        case INVOKE_VIRTUAL_RANGE:
                                                            super.visitMethodStmt(Op.INVOKE_STATIC_RANGE, args, replaceTo);
                                                            break;
                                                        default:
                                                    }
                                                } else {
                                                    super.visitMethodStmt(op, args, method);
                                                }
                                            }
                                        };
                                    }
                                    return code;
                                }
                            };
                        }
                        return dmv;
                    }
                };
            }
            return dcv;
        }

        @Override
        public void visitEnd() {
        }
    };
    for (String f : remainingArgs) {
        byte[] data = ZipUtil.readDex(new File(f).toPath());
        DexFileReader r = new DexFileReader(data);
        r.accept(fv);
    }
    if (stub != null) {
        byte[] data = ZipUtil.readDex(stub);
        DexFileReader r = new DexFileReader(data);
        r.accept(new DexFileVisitor(out) {

            @Override
            public void visitEnd() {
            }
        });
    }
    out.visitEnd();
    byte[] data = out.toByteArray();
    Files.write(output, data);
}
Also used : Op(com.googlecode.d2j.reader.Op) HashMap(java.util.HashMap) DexFileWriter(com.googlecode.d2j.dex.writer.DexFileWriter) DexFileReader(com.googlecode.d2j.reader.DexFileReader) Method(com.googlecode.d2j.Method) DexFileVisitor(com.googlecode.d2j.visitors.DexFileVisitor) DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) DexClassVisitor(com.googlecode.d2j.visitors.DexClassVisitor) File(java.io.File) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor)

Example 3 with DexFileReader

use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.

the class AppWriterTest method test4.

@Test
public void test4() throws IOException {
    DexFileWriter w = new DexFileWriter();
    DexFileReader dexFileReader = new DexFileReader(new File("../dex-translator/src/test/resources/dexes/i_jetty.dex"));
    dexFileReader.accept(w);
    w.toByteArray();
}
Also used : DexFileWriter(com.googlecode.d2j.dex.writer.DexFileWriter) DexFileReader(com.googlecode.d2j.reader.DexFileReader) File(java.io.File) Test(org.junit.Test)

Example 4 with DexFileReader

use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.

the class SmaliTest method readDex.

Map<String, DexClassNode> readDex(File path) throws IOException {
    DexFileReader dexFileReader = new DexFileReader(ZipUtil.readDex(path));
    DexFileNode dexFileNode = new DexFileNode();
    dexFileReader.accept(dexFileNode);
    Map<String, DexClassNode> map = new HashMap<>();
    for (DexClassNode c : dexFileNode.clzs) {
        map.put(c.className, c);
    }
    return map;
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) HashMap(java.util.HashMap) DexFileNode(com.googlecode.d2j.node.DexFileNode) DexFileReader(com.googlecode.d2j.reader.DexFileReader)

Example 5 with DexFileReader

use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.

the class GenerateCompileStubFromOdex method doDex.

private void doDex(ByteBuffer bs, final Path out) {
    DexFileReader reader = new DexFileReader(bs);
    DexFileNode fileNode = new DexFileNode();
    reader.accept(fileNode, DexFileReader.SKIP_CODE);
    Dex2Asm dex2Asm = new Dex2Asm();
    dex2Asm.convertDex(fileNode, new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(final String classInternalName) {
            final Path target = out.resolve(classInternalName + ".class");
            if (Files.exists(target)) {
                System.err.println("class " + classInternalName + " is already exists, skipping.");
                return null;
            }
            return new ClassVisitor(Opcodes.ASM4, new ClassWriter(ClassWriter.COMPUTE_MAXS)) {

                @Override
                public void visitEnd() {
                    super.visitEnd();
                    ClassWriter cw = (ClassWriter) cv;
                    byte[] data = cw.toByteArray();
                    try {
                        BaseCmd.createParentDirectories(target);
                        Files.write(target, data);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
                    if (noPrivate && 0 != (access & Opcodes.ACC_PRIVATE)) {
                        return null;
                    }
                    return super.visitField(access, name, desc, signature, value);
                }

                @Override
                public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                    if (noPrivate && 0 != (access & Opcodes.ACC_PRIVATE)) {
                        return null;
                    }
                    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                    if (0 != ((Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT) & access)) {
                        return mv;
                    }
                    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("stub");
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");
                    mv.visitInsn(Opcodes.ATHROW);
                    return mv;
                }
            };
        }
    });
}
Also used : Path(java.nio.file.Path) DexFileReader(com.googlecode.d2j.reader.DexFileReader) IOException(java.io.IOException) Dex2Asm(com.googlecode.d2j.dex.Dex2Asm) DexFileNode(com.googlecode.d2j.node.DexFileNode) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory)

Aggregations

DexFileReader (com.googlecode.d2j.reader.DexFileReader)5 DexFileNode (com.googlecode.d2j.node.DexFileNode)3 DexFileWriter (com.googlecode.d2j.dex.writer.DexFileWriter)2 File (java.io.File)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Method (com.googlecode.d2j.Method)1 ClassVisitorFactory (com.googlecode.d2j.dex.ClassVisitorFactory)1 Dex2Asm (com.googlecode.d2j.dex.Dex2Asm)1 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 Op (com.googlecode.d2j.reader.Op)1 DexClassVisitor (com.googlecode.d2j.visitors.DexClassVisitor)1 DexCodeVisitor (com.googlecode.d2j.visitors.DexCodeVisitor)1 DexFileVisitor (com.googlecode.d2j.visitors.DexFileVisitor)1 DexMethodVisitor (com.googlecode.d2j.visitors.DexMethodVisitor)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1