Search in sources :

Example 1 with DexFileVisitor

use of com.googlecode.d2j.visitors.DexFileVisitor in project dex2jar by pxb1988.

the class SmaliCmd method doCommandLine.

@Override
protected void doCommandLine() throws Exception {
    if (showVersionThenExits) {
        System.out.println("smali 1.4.2p (https://sourceforge.net/p/dex2jar)");
        System.out.println("Copyright (c) 2009-2013 Panxiaobo (pxb1988@gmail.com)");
        System.out.println("Apache license (http://www.apache.org/licenses/LICENSE-2.0)");
        return;
    }
    if (!readSmaliFromStdin && remainingArgs.length < 1) {
        System.err.println("ERRPR: no file to process");
        return;
    }
    if (output == null) {
        output = new File("out.dex").toPath();
    }
    Smali smali = new Smali();
    DexFileWriter fw = new DexFileWriter();
    DexFileVisitor fv = new DexFileVisitor(fw) {

        @Override
        public void visitEnd() {
        // intercept the call to super
        }
    };
    if (readSmaliFromStdin) {
        smali.smaliFile("<stdin>", System.in, fv);
        System.err.println("smali <stdin> -> " + output);
    }
    for (String s : remainingArgs) {
        Path file = new File(s).toPath();
        if (!Files.exists(file)) {
            System.err.println("skip " + file + ", it is not a dir or a file");
        } else {
            System.err.println("smali " + s + " -> " + output);
            smali.smali(file, fv);
        }
    }
    fw.visitEnd();
    byte[] data = fw.toByteArray();
    Files.write(output, data);
}
Also used : Path(java.nio.file.Path) DexFileVisitor(com.googlecode.d2j.visitors.DexFileVisitor) DexFileWriter(com.googlecode.d2j.dex.writer.DexFileWriter) File(java.io.File)

Example 2 with DexFileVisitor

use of com.googlecode.d2j.visitors.DexFileVisitor 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)

Aggregations

DexFileWriter (com.googlecode.d2j.dex.writer.DexFileWriter)2 DexFileVisitor (com.googlecode.d2j.visitors.DexFileVisitor)2 File (java.io.File)2 Method (com.googlecode.d2j.Method)1 DexFileReader (com.googlecode.d2j.reader.DexFileReader)1 Op (com.googlecode.d2j.reader.Op)1 DexClassVisitor (com.googlecode.d2j.visitors.DexClassVisitor)1 DexCodeVisitor (com.googlecode.d2j.visitors.DexCodeVisitor)1 DexMethodVisitor (com.googlecode.d2j.visitors.DexMethodVisitor)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1