Search in sources :

Example 1 with BaksmaliDumper

use of com.googlecode.d2j.smali.BaksmaliDumper in project dex2jar by pxb1988.

the class BaksmaliTest method dotest.

private void dotest(Path f) throws Exception {
    Path smali0 = new File("target/" + f.getFileName() + "-smali0.zip").toPath();
    try (FileSystem fs0 = BaseCmd.createZip(smali0)) {
        Baksmali.from(f).to(fs0.getPath("/"));
    }
    Path smali1 = new File("target/" + f.getFileName() + "-smali1.zip").toPath();
    try (FileSystem fs0 = BaseCmd.openZip(smali0);
        FileSystem fs1 = BaseCmd.createZip(smali1)) {
        BaksmaliDumper baksmaliDumper = new BaksmaliDumper();
        BaksmaliDexFileVisitor v = new BaksmaliDexFileVisitor(fs1.getPath("/"), baksmaliDumper);
        Smali.smali(fs0.getPath("/"), v);
    }
}
Also used : Path(java.nio.file.Path) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) FileSystem(java.nio.file.FileSystem) BaksmaliDexFileVisitor(com.googlecode.d2j.smali.BaksmaliDexFileVisitor) File(java.io.File)

Example 2 with BaksmaliDumper

use of com.googlecode.d2j.smali.BaksmaliDumper in project dex2jar by pxb1988.

the class DexWaveTest method toStd.

public static String toStd(DexClassNode expected) throws IOException {
    StringWriter stringWriter = new StringWriter();
    BufferedWriter bufferedWriter = new BufferedWriter(stringWriter);
    BaksmaliDumpOut out = new BaksmaliDumpOut(bufferedWriter);
    final BaksmaliDumper bs = new BaksmaliDumper(true, false);
    bs.baksmaliClass(expected, out);
    bufferedWriter.close();
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) BaksmaliDumpOut(com.googlecode.d2j.smali.BaksmaliDumpOut) BufferedWriter(java.io.BufferedWriter)

Example 3 with BaksmaliDumper

use of com.googlecode.d2j.smali.BaksmaliDumper in project dex2jar by pxb1988.

the class TestUtils method translateAndCheck.

public static byte[] translateAndCheck(DexFileNode fileNode, DexClassNode clzNode) throws AnalyzerException, IllegalAccessException {
    // 1. convert to .class
    Dex2Asm dex2Asm = new Dex2Asm() {

        @Override
        public void convertCode(DexMethodNode methodNode, MethodVisitor mv, ClzCtx clzCtx) {
            try {
                super.convertCode(methodNode, mv, clzCtx);
            } catch (Exception ex) {
                BaksmaliDumper d = new BaksmaliDumper();
                try {
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.err, "UTF-8"));
                    d.baksmaliMethod(methodNode, out);
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                throw new DexException(ex, "Failed to convert code for %s", methodNode.method);
            }
        }
    };
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw);
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(String classInternalName) {
            return rca;
        }
    };
    if (fileNode != null) {
        dex2Asm.convertClass(clzNode, cvf, fileNode);
    } else {
        dex2Asm.convertClass(clzNode, cvf);
    }
    byte[] data = cw.toByteArray();
    // 2. verify .class
    ClassReader cr = new ClassReader(data);
    TestUtils.verify(cr);
    // 3. convert back to dex
    CfOptions cfOptions = new CfOptions();
    cfOptions.strictNameCheck = false;
    DexOptions dexOptions = new DexOptions();
    if (fileNode != null && fileNode.dexVersion >= DexConstants.DEX_037) {
        dexOptions.minSdkVersion = 26;
    }
    DirectClassFile dcf = new DirectClassFile(data, rca.getClassName() + ".class", true);
    dcf.setAttributeFactory(new StdAttributeFactory());
    com.android.dx.dex.file.DexFile dxFile = new com.android.dx.dex.file.DexFile(dexOptions);
    try {
        CfTranslator.translate(new DxContext(), dcf, data, cfOptions, dexOptions, dxFile);
    } catch (ParseException e) {
        if ("MethodHandle not supported".equals(e.getMessage())) {
            e.printStackTrace();
        } else {
            throw e;
        }
    }
    return data;
}
Also used : DexException(com.googlecode.d2j.DexException) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) LambadaNameSafeClassAdapter(com.googlecode.d2j.dex.LambadaNameSafeClassAdapter) StdAttributeFactory(com.android.dx.cf.direct.StdAttributeFactory) MethodVisitor(org.objectweb.asm.MethodVisitor) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) Dex2Asm(com.googlecode.d2j.dex.Dex2Asm) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexOptions(com.android.dx.dex.DexOptions) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) DexException(com.googlecode.d2j.DexException) ZipException(java.util.zip.ZipException) ParseException(com.android.dx.cf.iface.ParseException) ClassWriter(org.objectweb.asm.ClassWriter) DxContext(com.android.dx.command.dexer.DxContext) DirectClassFile(com.android.dx.cf.direct.DirectClassFile) ClassReader(org.objectweb.asm.ClassReader) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory) CfOptions(com.android.dx.dex.cf.CfOptions) ParseException(com.android.dx.cf.iface.ParseException)

Example 4 with BaksmaliDumper

use of com.googlecode.d2j.smali.BaksmaliDumper in project dex2jar by pxb1988.

the class SmaliTest method test.

@Test
public void test() throws IOException {
    DexFileNode dfn = new DexFileNode();
    try (InputStream is = SmaliTest.class.getResourceAsStream("/a.smali")) {
        Smali.smaliFile("a.smali", is, dfn);
    }
    for (DexClassNode dcn : dfn.clzs) {
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        new BaksmaliDumper(true, true).baksmaliClass(dcn, new BaksmaliDumpOut(w));
        w.flush();
    }
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) DexFileNode(com.googlecode.d2j.node.DexFileNode) BaksmaliDumpOut(com.googlecode.d2j.smali.BaksmaliDumpOut) Test(org.junit.Test)

Example 5 with BaksmaliDumper

use of com.googlecode.d2j.smali.BaksmaliDumper in project dex2jar by pxb1988.

the class SmaliTest method pbaksmali.

private static String pbaksmali(DexClassNode dcn) throws IOException {
    StringWriter bufWriter = new StringWriter();
    BufferedWriter w = new BufferedWriter(bufWriter);
    new BaksmaliDumper(true, true).baksmaliClass(dcn, new BaksmaliDumpOut(w));
    w.flush();
    bufWriter.flush();
    return bufWriter.toString();
}
Also used : BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) BaksmaliDumpOut(com.googlecode.d2j.smali.BaksmaliDumpOut)

Aggregations

BaksmaliDumper (com.googlecode.d2j.smali.BaksmaliDumper)6 BaksmaliDumpOut (com.googlecode.d2j.smali.BaksmaliDumpOut)3 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)1 StdAttributeFactory (com.android.dx.cf.direct.StdAttributeFactory)1 ParseException (com.android.dx.cf.iface.ParseException)1 DxContext (com.android.dx.command.dexer.DxContext)1 DexOptions (com.android.dx.dex.DexOptions)1 CfOptions (com.android.dx.dex.cf.CfOptions)1 DexException (com.googlecode.d2j.DexException)1 ClassVisitorFactory (com.googlecode.d2j.dex.ClassVisitorFactory)1 Dex2Asm (com.googlecode.d2j.dex.Dex2Asm)1 LambadaNameSafeClassAdapter (com.googlecode.d2j.dex.LambadaNameSafeClassAdapter)1 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 DexFileNode (com.googlecode.d2j.node.DexFileNode)1 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)1 BaksmaliDexFileVisitor (com.googlecode.d2j.smali.BaksmaliDexFileVisitor)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1