Search in sources :

Example 31 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.

the class DexPool method writeTo.

public static void writeTo(@Nonnull DexDataStore dataStore, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {
    DexPool dexPool = new DexPool(input.getOpcodes());
    for (ClassDef classDef : input.getClasses()) {
        dexPool.internClass(classDef);
    }
    dexPool.writeTo(dataStore);
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef)

Example 32 with ClassDef

use of org.jf.dexlib2.iface.ClassDef 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 33 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.

the class BaksmaliTestUtils method assertSmaliCompiledEquals.

public static void assertSmaliCompiledEquals(String source, String expected, BaksmaliOptions options, boolean stripComments) throws IOException, RecognitionException {
    ClassDef classDef = SmaliTestUtils.compileSmali(source, options.apiLevel);
    // Remove unnecessary whitespace and optionally strip all comments from smali file
    String normalizedActual = getNormalizedSmali(classDef, options, stripComments);
    String normalizedExpected = normalizeSmali(expected, stripComments);
    // Assert that normalized strings are now equal
    Assert.assertEquals(normalizedExpected, normalizedActual);
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef)

Example 34 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.

the class DisassemblyTest method runTest.

protected void runTest(@Nonnull String testName, @Nonnull BaksmaliOptions options) {
    try {
        DexBackedDexFile inputDex = getInputDexFile(testName, options);
        Assert.assertEquals(1, inputDex.getClassCount());
        ClassDef inputClass = Iterables.getFirst(inputDex.getClasses(), null);
        Assert.assertNotNull(inputClass);
        String input = BaksmaliTestUtils.getNormalizedSmali(inputClass, options, true);
        String output = BaksmaliTestUtils.readResourceFully(getOutputFilename(testName));
        output = BaksmaliTestUtils.normalizeSmali(output, true);
        // Run smali, baksmali, and then compare strings are equal (minus comments/whitespace)
        Assert.assertEquals(output, input);
    } catch (IOException ex) {
        Assert.fail();
    }
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) ClassDef(org.jf.dexlib2.iface.ClassDef) IOException(java.io.IOException)

Example 35 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.

the class AnalyzedMethodUtil method canAccess.

public static boolean canAccess(@Nonnull TypeProto type, @Nonnull Method virtualMethod, boolean checkPackagePrivate, boolean checkProtected, boolean checkClass) {
    if (checkPackagePrivate && MethodUtil.isPackagePrivate(virtualMethod)) {
        String otherPackage = TypeUtils.getPackage(virtualMethod.getDefiningClass());
        String thisPackage = TypeUtils.getPackage(type.getType());
        if (!otherPackage.equals(thisPackage)) {
            return false;
        }
    }
    if (checkProtected && (virtualMethod.getAccessFlags() & AccessFlags.PROTECTED.getValue()) != 0) {
        if (!TypeProtoUtils.extendsFrom(type, virtualMethod.getDefiningClass())) {
            return false;
        }
    }
    if (checkClass) {
        ClassPath classPath = type.getClassPath();
        ClassDef methodClassDef = classPath.getClassDef(virtualMethod.getDefiningClass());
        if (!TypeUtils.canAccessClass(type.getType(), methodClassDef)) {
            return false;
        }
    }
    return true;
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)47 DexFile (org.jf.dexlib2.iface.DexFile)23 Test (org.junit.Test)21 Method (org.jf.dexlib2.iface.Method)18 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)14 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)14 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)12 IndentingWriter (org.jf.util.IndentingWriter)11 File (java.io.File)10 IOException (java.io.IOException)10 HashSet (java.util.HashSet)8 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)8 Instruction (org.jf.dexlib2.iface.instruction.Instruction)8 Nonnull (javax.annotation.Nonnull)7 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6