Search in sources :

Example 1 with DexFileWriter

use of com.googlecode.d2j.dex.writer.DexFileWriter 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 DexFileWriter

use of com.googlecode.d2j.dex.writer.DexFileWriter in project dex2jar by pxb1988.

the class SmaliTest method toDex.

private byte[] toDex(DexClassNode dexClassNode2) {
    DexFileWriter w = new DexFileWriter();
    dexClassNode2.accept(w);
    w.visitEnd();
    return w.toByteArray();
}
Also used : DexFileWriter(com.googlecode.d2j.dex.writer.DexFileWriter)

Example 3 with DexFileWriter

use of com.googlecode.d2j.dex.writer.DexFileWriter 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 4 with DexFileWriter

use of com.googlecode.d2j.dex.writer.DexFileWriter 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 5 with DexFileWriter

use of com.googlecode.d2j.dex.writer.DexFileWriter in project dex2jar by pxb1988.

the class AppWriterTest method testDupClz.

@Test(expected = DexWriteException.class)
public void testDupClz() {
    DexFileWriter w = new DexFileWriter();
    DexClassVisitor cv = w.visit(0, "La/b;", null, null);
    cv.visitEnd();
    cv = w.visit(0, "La/b;", null, null);
    cv.visitEnd();
    w.visitEnd();
}
Also used : DexFileWriter(com.googlecode.d2j.dex.writer.DexFileWriter) Test(org.junit.Test)

Aggregations

DexFileWriter (com.googlecode.d2j.dex.writer.DexFileWriter)6 File (java.io.File)3 Test (org.junit.Test)3 Method (com.googlecode.d2j.Method)2 DexFileReader (com.googlecode.d2j.reader.DexFileReader)2 DexFileVisitor (com.googlecode.d2j.visitors.DexFileVisitor)2 Field (com.googlecode.d2j.Field)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