Search in sources :

Example 11 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project smali by JesusFreke.

the class DumpCommand method dump.

/**
     * Writes an annotated hex dump of the given dex file to output.
     *
     * @param dexFile The dex file to dump
     * @param output An OutputStream to write the annotated hex dump to. The caller is responsible for closing this
     *               when needed.
     *
     * @throws IOException
     */
public static void dump(@Nonnull DexBackedDexFile dexFile, @Nonnull OutputStream output) throws IOException {
    Writer writer = new BufferedWriter(new OutputStreamWriter(output));
    int consoleWidth = ConsoleUtil.getConsoleWidth();
    if (consoleWidth <= 0) {
        consoleWidth = 120;
    }
    RawDexFile rawDexFile = new RawDexFile(dexFile.getOpcodes(), dexFile);
    DexAnnotator annotator = new DexAnnotator(rawDexFile, consoleWidth);
    annotator.writeAnnotations(writer);
}
Also used : DexAnnotator(org.jf.dexlib2.dexbacked.raw.util.DexAnnotator) RawDexFile(org.jf.dexlib2.dexbacked.raw.RawDexFile)

Example 12 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile 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 13 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project smali by JesusFreke.

the class ListDexCommand method run.

@Override
public void run() {
    if (help || inputList == null || inputList.isEmpty()) {
        usage();
        return;
    }
    if (inputList.size() > 1) {
        System.err.println("Too many files specified");
        usage();
        return;
    }
    String input = inputList.get(0);
    File file = new File(input);
    if (!file.exists()) {
        System.err.println(String.format("Could not find the file: %s", input));
        System.exit(-1);
    }
    List<String> entries;
    try {
        MultiDexContainer<? extends DexBackedDexFile> container = DexFileFactory.loadDexContainer(file, Opcodes.getDefault());
        entries = container.getDexEntryNames();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    for (String entry : entries) {
        System.out.println(entry);
    }
}
Also used : IOException(java.io.IOException) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) File(java.io.File)

Example 14 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project android by JetBrains.

the class DexParser method constructMethodRefTreeForDex.

@NotNull
static PackageTreeNode constructMethodRefTreeForDex(@NotNull DexBackedDexFile dexFile) {
    PackageTreeNode root = new PackageTreeNode("", "root", PackageTreeNode.NodeType.PACKAGE, null);
    Set<String> classesWithDefinition = dexFile.getClasses().stream().map(DexBackedClassDef::getType).collect(Collectors.toSet());
    Multimap<String, MethodReference> methodsByClassName = getMethodsByClassName(dexFile);
    for (String className : methodsByClassName.keySet()) {
        Collection<MethodReference> methods = methodsByClassName.get(className);
        for (MethodReference ref : methods) {
            root.insert("", DebuggerUtilsEx.signatureToName(className), ref, classesWithDefinition.contains(className));
        }
    }
    root.sortByCount();
    return root;
}
Also used : MethodReference(org.jf.dexlib2.iface.reference.MethodReference) DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project android by JetBrains.

the class DexParser method getMethodsByClassName.

@NotNull
private static Multimap<String, MethodReference> getMethodsByClassName(@NotNull DexBackedDexFile dexFile) {
    Multimap<String, MethodReference> methodsByClass = ArrayListMultimap.create();
    for (int i = 0, m = dexFile.getMethodCount(); i < m; i++) {
        MethodReference methodRef = new DexBackedMethodReference(dexFile, i);
        methodsByClass.put(methodRef.getDefiningClass(), methodRef);
    }
    return methodsByClass;
}
Also used : DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)20 Test (org.junit.Test)9 IOException (java.io.IOException)7 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)5 File (java.io.File)4 DexEntryFinder (org.jf.dexlib2.DexFileFactory.DexEntryFinder)4 NotNull (org.jetbrains.annotations.NotNull)3 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)3 ClassDef (org.jf.dexlib2.iface.ClassDef)3 DexFile (org.jf.dexlib2.iface.DexFile)3 DexBuilder (org.jf.dexlib2.writer.builder.DexBuilder)3 FileInputStream (java.io.FileInputStream)2 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)2 DexBackedOdexFile (org.jf.dexlib2.dexbacked.DexBackedOdexFile)2 DexBackedMethodReference (org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference)2 Annotation (org.jf.dexlib2.iface.Annotation)2 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)2 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)2