Java Code Examples for org.jf.dexlib2.dexbacked.DexBackedDexFile

The following examples show how to use org.jf.dexlib2.dexbacked.DexBackedDexFile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: HeyGirl   Source File: ProtoIdItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int protoIndex) {
    int offset = dexFile.getProtoIdItemOffset(protoIndex);

    StringBuilder sb = new StringBuilder();
    sb.append("(");

    int parametersOffset = dexFile.readSmallUint(offset + PARAMETERS_OFFSET);
    sb.append(TypeListItem.asString(dexFile, parametersOffset));
    sb.append(")");

    int returnTypeIndex = dexFile.readSmallUint(offset + RETURN_TYPE_OFFSET);
    String returnType = dexFile.getType(returnTypeIndex);
    sb.append(returnType);

    return sb.toString();
}
 
Example 2
Source Project: ZjDroid   Source File: TypeListItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int typeListOffset) {
    if (typeListOffset == 0) {
        return "";
    }

    StringBuilder sb = new StringBuilder();

    int size = dexFile.readSmallUint(typeListOffset);
    for (int i=0; i<size; i++) {
        int typeIndex = dexFile.readUshort(typeListOffset + 4 + i*2);
        String type = dexFile.getType(typeIndex);
        sb.append(type);
    }
    return sb.toString();
}
 
Example 3
Source Project: atlas   Source File: SmaliDiffUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Set<DexBackedClassDef> scanClasses(File smaliDir, List<File> newFiles) throws PatchException {

        Set<DexBackedClassDef> classes = Sets.newHashSet();
        try {
            for (File newFile : newFiles) {
                DexBackedDexFile newDexFile = DexFileFactory.loadDexFile(newFile, Opcodes.getDefault());
                Set<? extends DexBackedClassDef> dexClasses = newDexFile.getClasses();
                classes.addAll(dexClasses);
            }

            final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
            final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");

            for (DexBackedClassDef classDef : classes) {
                String className = classDef.getType();
                ApkPatch.currentClassType = null;
                AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classes, 19), true, true);
                File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
            }
        } catch (Exception e) {
            throw new PatchException(e);
        }
        return classes;
    }
 
Example 4
Source Project: zjdroid   Source File: ProtoIdItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int protoIndex) {
    int offset = dexFile.getProtoIdItemOffset(protoIndex);

    StringBuilder sb = new StringBuilder();
    sb.append("(");

    int parametersOffset = dexFile.readSmallUint(offset + PARAMETERS_OFFSET);
    sb.append(TypeListItem.asString(dexFile, parametersOffset));
    sb.append(")");

    int returnTypeIndex = dexFile.readSmallUint(offset + RETURN_TYPE_OFFSET);
    String returnType = dexFile.getType(returnTypeIndex);
    sb.append(returnType);

    return sb.toString();
}
 
Example 5
Source Project: zjdroid   Source File: AnnotationsDirectory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static List<Set<? extends DexBackedAnnotation>> getParameterAnnotations(
        @Nonnull final DexBackedDexFile dexFile, final int annotationSetListOffset) {
    if (annotationSetListOffset > 0) {
        final int size = dexFile.readSmallUint(annotationSetListOffset);

        return new FixedSizeList<Set<? extends DexBackedAnnotation>>() {
            @Nonnull
            @Override
            public Set<? extends DexBackedAnnotation> readItem(int index) {
                int annotationSetOffset = dexFile.readSmallUint(annotationSetListOffset + 4 + index * 4);
                return getAnnotations(dexFile, annotationSetOffset);
            }

            @Override public int size() { return size; }
        };
    }
    return ImmutableList.of();
}
 
Example 6
Source Project: zjdroid   Source File: TypeListItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int typeListOffset) {
    if (typeListOffset == 0) {
        return "";
    }

    StringBuilder sb = new StringBuilder();

    int size = dexFile.readSmallUint(typeListOffset);
    for (int i=0; i<size; i++) {
        int typeIndex = dexFile.readUshort(typeListOffset + 4 + i*2);
        String type = dexFile.getType(typeIndex);
        sb.append(type);
    }
    return sb.toString();
}
 
Example 7
Source Project: HeyGirl   Source File: TypeListItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int typeListOffset) {
    if (typeListOffset == 0) {
        return "";
    }

    StringBuilder sb = new StringBuilder();

    int size = dexFile.readSmallUint(typeListOffset);
    for (int i=0; i<size; i++) {
        int typeIndex = dexFile.readUshort(typeListOffset + 4 + i*2);
        String type = dexFile.getType(typeIndex);
        sb.append(type);
    }
    return sb.toString();
}
 
Example 8
Source Project: zjdroid   Source File: AnnotationsDirectory.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static AnnotationsDirectory newOrEmpty(@Nonnull DexBackedDexFile dexFile,
                                              int directoryAnnotationsOffset) {
    if (directoryAnnotationsOffset == 0) {
        return EMPTY;
    }
    return new AnnotationsDirectoryImpl(dexFile, directoryAnnotationsOffset);
}
 
Example 9
Source Project: HeyGirl   Source File: AnnotationItem.java    License: Apache License 2.0 5 votes vote down vote up
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int annotationItemOffset) {
    try {
        DexReader reader = dexFile.readerAt(annotationItemOffset);
        reader.readUbyte();
        int typeIndex = reader.readSmallUleb128();
        String annotationType = dexFile.getType(typeIndex);
        return String.format("annotation_item[0x%x]: %s", annotationItemOffset, annotationType);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("annotation_item[0x%x]", annotationItemOffset);
}
 
Example 10
Source Project: HeyGirl   Source File: DexBackedArrayPayload.java    License: Apache License 2.0 5 votes vote down vote up
public DexBackedArrayPayload(@Nonnull DexBackedDexFile dexFile,
                             int instructionStart) {
    super(dexFile, OPCODE, instructionStart);

    elementWidth = dexFile.readUshort(instructionStart + ELEMENT_WIDTH_OFFSET);
    elementCount = dexFile.readSmallUint(instructionStart + ELEMENT_COUNT_OFFSET);
}
 
Example 11
Source Project: ZjDroid   Source File: AnnotationSetRefList.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int annotationSetRefListOffset) {
    if (annotationSetRefListOffset == 0) {
        return "annotation_set_ref_list[NO_OFFSET]";
    }
    return String.format("annotation_set_ref_list[0x%x]", annotationSetRefListOffset);
}
 
Example 12
Source Project: zjdroid   Source File: TypeListItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeListOffset) {
    if (typeListOffset == 0) {
        return "type_list_item[NO_OFFSET]";
    }

    try {
        String typeList = asString(dexFile, typeListOffset);
        return String.format("type_list_item[0x%x]: %s", typeListOffset, typeList);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("type_list_item[0x%x]", typeListOffset);
}
 
Example 13
Source Project: HeyGirl   Source File: DexBackedInstruction.java    License: Apache License 2.0 5 votes vote down vote up
public DexBackedInstruction(@Nonnull DexBackedDexFile dexFile,
                            @Nonnull Opcode opcode,
                            int instructionStart) {
    this.dexFile = dexFile;
    this.opcode = opcode;
    this.instructionStart = instructionStart;
}
 
Example 14
Source Project: ZjDroid   Source File: MethodIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int methodIndex) {
    int methodOffset = dexFile.getMethodIdItemOffset(methodIndex);
    int classIndex = dexFile.readUshort(methodOffset + CLASS_OFFSET);
    String classType = dexFile.getType(classIndex);

    int protoIndex = dexFile.readUshort(methodOffset + PROTO_OFFSET);
    String protoString = ProtoIdItem.asString(dexFile, protoIndex);

    int nameIndex = dexFile.readSmallUint(methodOffset + NAME_OFFSET);
    String methodName = dexFile.getString(nameIndex);

    return String.format("%s->%s%s", classType, methodName, protoString);
}
 
Example 15
Source Project: HeyGirl   Source File: DebugInfo.java    License: Apache License 2.0 5 votes vote down vote up
public static DebugInfo newOrEmpty(@Nonnull DexBackedDexFile dexFile, int debugInfoOffset,
                                   @Nonnull DexBackedMethodImplementation methodImpl) {
    if (debugInfoOffset == 0) {
        return EmptyDebugInfo.INSTANCE;
    }
    return new DebugInfoImpl(dexFile, debugInfoOffset, methodImpl);
}
 
Example 16
Source Project: zjdroid   Source File: MethodIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int methodIndex) {
    int methodOffset = dexFile.getMethodIdItemOffset(methodIndex);
    int classIndex = dexFile.readUshort(methodOffset + CLASS_OFFSET);
    String classType = dexFile.getType(classIndex);

    int protoIndex = dexFile.readUshort(methodOffset + PROTO_OFFSET);
    String protoString = ProtoIdItem.asString(dexFile, protoIndex);

    int nameIndex = dexFile.readSmallUint(methodOffset + NAME_OFFSET);
    String methodName = dexFile.getString(nameIndex);

    return String.format("%s->%s%s", classType, methodName, protoString);
}
 
Example 17
Source Project: HeyGirl   Source File: StringIdItem.java    License: Apache License 2.0 5 votes vote down vote up
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int stringIndex, boolean quote) {
    try {
        String string = dexFile.getString(stringIndex);
        if (quote) {
            string = String.format("\"%s\"", StringUtils.escapeString(string));
        }
        return String.format("string_id_item[%d]: %s", stringIndex, string);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("string_id_item[%d]", stringIndex);
}
 
Example 18
Source Project: ZjDroid   Source File: TypeListItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeListOffset) {
    if (typeListOffset == 0) {
        return "type_list_item[NO_OFFSET]";
    }

    try {
        String typeList = asString(dexFile, typeListOffset);
        return String.format("type_list_item[0x%x]: %s", typeListOffset, typeList);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("type_list_item[0x%x]", typeListOffset);
}
 
Example 19
Source Project: HeyGirl   Source File: TypeIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getOptionalReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeIndex) {
    if (typeIndex == -1) {
        return "type_id_item[NO_INDEX]";
    }
    return getReferenceAnnotation(dexFile, typeIndex);
}
 
Example 20
Source Project: ZjDroid   Source File: StringIdItem.java    License: Apache License 2.0 5 votes vote down vote up
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int stringIndex, boolean quote) {
    try {
        String string = dexFile.getString(stringIndex);
        if (quote) {
            string = String.format("\"%s\"", StringUtils.escapeString(string));
        }
        return String.format("string_id_item[%d]: %s", stringIndex, string);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("string_id_item[%d]", stringIndex);
}
 
Example 21
Source Project: ZjDroid   Source File: MethodIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int methodIndex) {
    int methodOffset = dexFile.getMethodIdItemOffset(methodIndex);
    int classIndex = dexFile.readUshort(methodOffset + CLASS_OFFSET);
    String classType = dexFile.getType(classIndex);

    int protoIndex = dexFile.readUshort(methodOffset + PROTO_OFFSET);
    String protoString = ProtoIdItem.asString(dexFile, protoIndex);

    int nameIndex = dexFile.readSmallUint(methodOffset + NAME_OFFSET);
    String methodName = dexFile.getString(nameIndex);

    return String.format("%s->%s%s", classType, methodName, protoString);
}
 
Example 22
Source Project: ZjDroid   Source File: MethodIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int methodIndex) {
    try {
        String methodString = asString(dexFile, methodIndex);
        return String.format("method_id_item[%d]: %s", methodIndex, methodString);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("method_id_item[%d]", methodIndex);
}
 
Example 23
Source Project: ZjDroid   Source File: FieldIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int fieldIndex) {
    int fieldOffset = dexFile.getFieldIdItemOffset(fieldIndex);
    int classIndex = dexFile.readUshort(fieldOffset + CLASS_OFFSET);
    String classType = dexFile.getType(classIndex);

    int typeIndex = dexFile.readUshort(fieldOffset + TYPE_OFFSET);
    String fieldType = dexFile.getType(typeIndex);

    int nameIndex = dexFile.readSmallUint(fieldOffset + NAME_OFFSET);
    String fieldName = dexFile.getString(nameIndex);

    return String.format("%s->%s:%s", classType, fieldName, fieldType);
}
 
Example 24
Source Project: zjdroid   Source File: TypeIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getOptionalReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeIndex) {
    if (typeIndex == -1) {
        return "type_id_item[NO_INDEX]";
    }
    return getReferenceAnnotation(dexFile, typeIndex);
}
 
Example 25
Source Project: JAADAS   Source File: DexClassProvider.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return names of classes in dex/apk file.
 *
 * @param file
 *            file to dex/apk file. Can be the path of a zip file.
 *
 * @return set of class names
 */
public static Set<String> classesOfDex(File file) throws IOException {
	Set<String> classes = new HashSet<String>();
	// TODO (SA): Go for API 1 because DexlibWrapper does so, but needs more attention
	DexBackedDexFile d = DexFileFactory.loadDexFile(file, 1, false);
	for (ClassDef c : d.getClasses()) {
		String name = Util.dottedClassName(c.getType());
		classes.add(name);
	}
	return classes;
}
 
Example 26
Source Project: ZjDroid   Source File: TypeIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeIndex) {
    try {
        String typeString = dexFile.getType(typeIndex);
        return String.format("type_id_item[%d]: %s", typeIndex, typeString);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return String.format("type_id_item[%d]", typeIndex);
}
 
Example 27
Source Project: ZjDroid   Source File: FieldIdItem.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String asString(@Nonnull DexBackedDexFile dexFile, int fieldIndex) {
    int fieldOffset = dexFile.getFieldIdItemOffset(fieldIndex);
    int classIndex = dexFile.readUshort(fieldOffset + CLASS_OFFSET);
    String classType = dexFile.getType(classIndex);

    int typeIndex = dexFile.readUshort(fieldOffset + TYPE_OFFSET);
    String fieldType = dexFile.getType(typeIndex);

    int nameIndex = dexFile.readSmallUint(fieldOffset + NAME_OFFSET);
    String fieldName = dexFile.getString(nameIndex);

    return String.format("%s->%s:%s", classType, fieldName, fieldType);
}
 
Example 28
Source Project: ZjDroid   Source File: DexBackedReference.java    License: Apache License 2.0 5 votes vote down vote up
public static Reference makeReference(@Nonnull DexBackedDexFile dexFile, int referenceType, int referenceIndex) {
    switch (referenceType) {
        case ReferenceType.STRING:
            return new DexBackedStringReference(dexFile, referenceIndex);
        case ReferenceType.TYPE:
            return new DexBackedTypeReference(dexFile, referenceIndex);
        case ReferenceType.METHOD:
            return new DexBackedMethodReference(dexFile, referenceIndex);
        case ReferenceType.FIELD:
            return new DexBackedFieldReference(dexFile, referenceIndex);
        default:
            throw new ExceptionWithContext("Invalid reference type: %d", referenceType);
    }
}
 
Example 29
Source Project: Box   Source File: SmaliUtils.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static String getSmaliCode(DexNode dex, int clsDefOffset) {
	try {
		Path path = dex.getDexFile().getPath();
		DexBackedDexFile dexFile = DexFileFactory.loadDexFile(path.toFile(), null);
		DexBackedClassDef dexBackedClassDef = new DexBackedClassDef(dexFile, clsDefOffset);
		return getSmaliCode(dexBackedClassDef);
	} catch (Exception e) {
		LOG.error("Error generating smali", e);
		return "Error generating smali code: " + e.getMessage()
				+ '\n' + Utils.getStackTrace(e);
	}
}
 
Example 30
Source Project: ratel   Source File: SmaliDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private void decode() throws AndrolibException {
    try {
        final BaksmaliOptions options = new BaksmaliOptions();

        // options
        options.deodex = false;
        options.implicitReferences = false;
        options.parameterRegisters = true;
        options.localsDirective = true;
        options.sequentialLabels = true;
        options.debugInfo = mBakDeb;
        options.codeOffsets = false;
        options.accessorComments = false;
        options.registerInfo = 0;
        options.inlineResolver = null;

        // set jobs automatically
        int jobs = Runtime.getRuntime().availableProcessors();
        if (jobs > 6) {
            jobs = 6;
        }

        // create the dex
        DexBackedDexFile dexFile = DexFileFactory.loadDexEntry(mApkFile, mDexFile, true, Opcodes.forApi(mApi));

        if (dexFile.isOdexFile()) {
            throw new AndrolibException("Warning: You are disassembling an odex file without deodexing it.");
        }

        if (dexFile instanceof DexBackedOdexFile) {
            options.inlineResolver =
                    InlineMethodResolver.createInlineMethodResolver(((DexBackedOdexFile)dexFile).getOdexVersion());
        }

        Baksmali.disassembleDexFile(dexFile, mOutDir, jobs, options);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    }
}