Search in sources :

Example 1 with ClassDefinition

use of org.jf.baksmali.Adaptors.ClassDefinition in project dex2jar by pxb1988.

the class SmaliTest method baksmali.

private static String baksmali(DexBackedClassDef def) throws IOException {
    baksmaliOptions opts = new baksmaliOptions();
    opts.outputDebugInfo = false;
    opts.syntheticAccessorResolver = new SyntheticAccessorResolver(Collections.EMPTY_LIST);
    ClassDefinition classDefinition = new ClassDefinition(opts, def);
    StringWriter bufWriter = new StringWriter();
    IndentingWriter writer = new IndentingWriter(bufWriter);
    classDefinition.writeTo((IndentingWriter) writer);
    writer.flush();
    return bufWriter.toString();
}
Also used : org.jf.baksmali.baksmaliOptions(org.jf.baksmali.baksmaliOptions) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Example 2 with ClassDefinition

use of org.jf.baksmali.Adaptors.ClassDefinition in project smali by JesusFreke.

the class Baksmali method disassembleClass.

private static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, BaksmaliOptions options) {
    /**
         * The path for the disassembly file is based on the package name
         * The class descriptor will look something like:
         * Ljava/lang/Object;
         * Where the there is leading 'L' and a trailing ';', and the parts of the
         * package name are separated by '/'
         */
    String classDescriptor = classDef.getType();
    //validate that the descriptor is formatted like we expect
    if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
        System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
        return false;
    }
    File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
    //create and initialize the top level string template
    ClassDefinition classDefinition = new ClassDefinition(options, classDef);
    //write the disassembly
    Writer writer = null;
    try {
        File smaliParent = smaliFile.getParentFile();
        if (!smaliParent.exists()) {
            if (!smaliParent.mkdirs()) {
                // check again, it's likely it was created in a different thread
                if (!smaliParent.exists()) {
                    System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
                    return false;
                }
            }
        }
        if (!smaliFile.exists()) {
            if (!smaliFile.createNewFile()) {
                System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
                return false;
            }
        }
        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
        writer = new IndentingWriter(bufWriter);
        classDefinition.writeTo((IndentingWriter) writer);
    } catch (Exception ex) {
        System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
        ex.printStackTrace();
        // noinspection ResultOfMethodCallIgnored
        smaliFile.delete();
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable ex) {
                System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
                ex.printStackTrace();
            }
        }
    }
    return true;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile) IndentingWriter(org.jf.util.IndentingWriter)

Example 3 with ClassDefinition

use of org.jf.baksmali.Adaptors.ClassDefinition in project smali by JesusFreke.

the class BaksmaliTestUtils method getNormalizedSmali.

@Nonnull
public static String getNormalizedSmali(@Nonnull ClassDef classDef, @Nonnull BaksmaliOptions options, boolean stripComments) throws IOException {
    StringWriter stringWriter = new StringWriter();
    IndentingWriter writer = new IndentingWriter(stringWriter);
    ClassDefinition classDefinition = new ClassDefinition(options, classDef);
    classDefinition.writeTo(writer);
    writer.close();
    return normalizeSmali(stringWriter.toString(), stripComments);
}
Also used : StringWriter(java.io.StringWriter) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) Nonnull(javax.annotation.Nonnull)

Example 4 with ClassDefinition

use of org.jf.baksmali.Adaptors.ClassDefinition in project smali by JesusFreke.

the class AnalysisTest method runTest.

public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException {
    String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
    DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault());
    BaksmaliOptions options = new BaksmaliOptions();
    if (registerInfo) {
        options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE;
        if (isArt) {
            options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56);
        } else {
            options.classPath = new ClassPath();
        }
    }
    options.implicitReferences = false;
    for (ClassDef classDef : dexFile.getClasses()) {
        StringWriter stringWriter = new StringWriter();
        IndentingWriter writer = new IndentingWriter(stringWriter);
        ClassDefinition classDefinition = new ClassDefinition(options, classDef);
        classDefinition.writeTo(writer);
        writer.close();
        String className = classDef.getType();
        String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, className.substring(1, className.length() - 1));
        String smaliContents = readResource(smaliPath);
        Assert.assertEquals(TextUtils.normalizeWhitespace(smaliContents), TextUtils.normalizeWhitespace((stringWriter.toString())));
    }
}
Also used : ClassPath(org.jf.dexlib2.analysis.ClassPath) ClassDef(org.jf.dexlib2.iface.ClassDef) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile)

Example 5 with ClassDefinition

use of org.jf.baksmali.Adaptors.ClassDefinition in project otertool by wuntee.

the class SmaliWorkshop method getSmaliSource.

public static Map<String, File> getSmaliSource(File sourceSmaliOrDexFile, File destinationDirectory) throws IOException {
    Map<String, File> ret = new HashMap<String, File>();
    DexFile dexFile = new DexFile(sourceSmaliOrDexFile);
    IndentingWriter idWriter;
    for (ClassDefItem c : dexFile.ClassDefsSection.getItems()) {
        File classFile = SmaliWorkshop.createSmaliClassFile(destinationDirectory, c);
        String className = SmaliWorkshop.classDefItemToFilename(c);
        logger.debug("Got class: " + className + " [" + classFile + "]");
        BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(classFile)));
        idWriter = new IndentingWriter(fileWriter);
        ClassDefinition cd = new ClassDefinition(c);
        cd.writeTo(idWriter);
        ret.put(className, classFile);
        idWriter.close();
    }
    return (sortMapByKey(ret));
}
Also used : ClassDefItem(org.jf.dexlib.ClassDefItem) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) IndentingWriter(org.jf.util.IndentingWriter) OutputStreamWriter(java.io.OutputStreamWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) File(java.io.File) DexFile(org.jf.dexlib.DexFile) DexFile(org.jf.dexlib.DexFile) BufferedWriter(java.io.BufferedWriter)

Aggregations

ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)5 IndentingWriter (org.jf.util.IndentingWriter)5 StringWriter (java.io.StringWriter)2 DexFile (org.jf.dexlib2.iface.DexFile)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Nonnull (javax.annotation.Nonnull)1 org.jf.baksmali.baksmaliOptions (org.jf.baksmali.baksmaliOptions)1 ClassDefItem (org.jf.dexlib.ClassDefItem)1 DexFile (org.jf.dexlib.DexFile)1 ClassPath (org.jf.dexlib2.analysis.ClassPath)1 ClassDef (org.jf.dexlib2.iface.ClassDef)1 SyntheticAccessorResolver (org.jf.dexlib2.util.SyntheticAccessorResolver)1