Search in sources :

Example 6 with DexReader

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

the class DexBackedStringReference method getSize.

/**
 * Calculate and return the private size of a string reference.
 *
 * Calculated as: string_data_off + string_data_item size
 *
 * @return size in bytes
 */
public int getSize() {
    // uint for string_data_off
    int size = StringIdItem.ITEM_SIZE;
    // add the string data length:
    int stringOffset = dexFile.getStringSection().getOffset(stringIndex);
    int stringDataOffset = dexFile.getBuffer().readSmallUint(stringOffset);
    DexReader reader = dexFile.getDataBuffer().readerAt(stringDataOffset);
    size += reader.peekSmallUleb128Size();
    int utf16Length = reader.readSmallUleb128();
    // and string data itself:
    size += reader.peekStringLength(utf16Length);
    return size;
}
Also used : DexReader(org.jf.dexlib2.dexbacked.DexReader)

Example 7 with DexReader

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

the class DebugInfoItem method makeAnnotator.

@Nonnull
public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
    return new SectionAnnotator(annotator, mapItem) {

        @Nonnull
        @Override
        public String getItemName() {
            return "debug_info_item";
        }

        @Override
        public void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
            DexReader reader = dexFile.getBuffer().readerAt(out.getCursor());
            int lineStart = reader.readBigUleb128();
            out.annotateTo(reader.getOffset(), "line_start = %d", lineStart & 0xFFFFFFFFL);
            int parametersSize = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "parameters_size = %d", parametersSize);
            if (parametersSize > 0) {
                out.annotate(0, "parameters:");
                out.indent();
                for (int i = 0; i < parametersSize; i++) {
                    int paramaterIndex = reader.readSmallUleb128() - 1;
                    out.annotateTo(reader.getOffset(), "%s", StringIdItem.getOptionalReferenceAnnotation(dexFile, paramaterIndex, true));
                }
                out.deindent();
            }
            out.annotate(0, "debug opcodes:");
            out.indent();
            int codeAddress = 0;
            int lineNumber = lineStart;
            loop: while (true) {
                int opcode = reader.readUbyte();
                switch(opcode) {
                    case DebugItemType.END_SEQUENCE:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_END_SEQUENCE");
                            break loop;
                        }
                    case DebugItemType.ADVANCE_PC:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_ADVANCE_PC");
                            out.indent();
                            int addressDiff = reader.readSmallUleb128();
                            codeAddress += addressDiff;
                            out.annotateTo(reader.getOffset(), "addr_diff = +0x%x: 0x%x", addressDiff, codeAddress);
                            out.deindent();
                            break;
                        }
                    case DebugItemType.ADVANCE_LINE:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_ADVANCE_LINE");
                            out.indent();
                            int lineDiff = reader.readSleb128();
                            lineNumber += lineDiff;
                            out.annotateTo(reader.getOffset(), "line_diff = +%d: %d", Math.abs(lineDiff), lineNumber);
                            out.deindent();
                            break;
                        }
                    case DebugItemType.START_LOCAL:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_START_LOCAL");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            int nameIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "name_idx = %s", StringIdItem.getOptionalReferenceAnnotation(dexFile, nameIndex, true));
                            int typeIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "type_idx = %s", TypeIdItem.getOptionalReferenceAnnotation(dexFile, typeIndex));
                            out.deindent();
                            break;
                        }
                    case DebugItemType.START_LOCAL_EXTENDED:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_START_LOCAL_EXTENDED");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            int nameIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "name_idx = %s", StringIdItem.getOptionalReferenceAnnotation(dexFile, nameIndex, true));
                            int typeIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "type_idx = %s", TypeIdItem.getOptionalReferenceAnnotation(dexFile, typeIndex));
                            int sigIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "sig_idx = %s", StringIdItem.getOptionalReferenceAnnotation(dexFile, sigIndex, true));
                            out.deindent();
                            break;
                        }
                    case DebugItemType.END_LOCAL:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_END_LOCAL");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            out.deindent();
                            break;
                        }
                    case DebugItemType.RESTART_LOCAL:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_RESTART_LOCAL");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            out.deindent();
                            break;
                        }
                    case DebugItemType.PROLOGUE_END:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_SET_PROLOGUE_END");
                            break;
                        }
                    case DebugItemType.EPILOGUE_BEGIN:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_SET_EPILOGUE_BEGIN");
                            break;
                        }
                    case DebugItemType.SET_SOURCE_FILE:
                        {
                            out.annotateTo(reader.getOffset(), "DBG_SET_FILE");
                            out.indent();
                            int nameIdx = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "name_idx = %s", StringIdItem.getOptionalReferenceAnnotation(dexFile, nameIdx));
                            out.deindent();
                            break;
                        }
                    default:
                        int adjusted = opcode - 0x0A;
                        int addressDiff = adjusted / 15;
                        int lineDiff = (adjusted % 15) - 4;
                        codeAddress += addressDiff;
                        lineNumber += lineDiff;
                        out.annotateTo(reader.getOffset(), "address_diff = +0x%x:0x%x, line_diff = +%d:%d, ", addressDiff, codeAddress, lineDiff, lineNumber);
                        break;
                }
            }
            out.deindent();
        }
    };
}
Also used : DexReader(org.jf.dexlib2.dexbacked.DexReader) Nonnull(javax.annotation.Nonnull) AnnotatedBytes(org.jf.dexlib2.util.AnnotatedBytes) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 8 with DexReader

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

the class StringDataItem method makeAnnotator.

@Nonnull
public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
    return new SectionAnnotator(annotator, mapItem) {

        @Nonnull
        @Override
        public String getItemName() {
            return "string_data_item";
        }

        @Override
        protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
            DexReader reader = dexFile.getBuffer().readerAt(out.getCursor());
            int utf16Length = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "utf16_size = %d", utf16Length);
            String value = reader.readString(utf16Length);
            out.annotateTo(reader.getOffset() + 1, "data = \"%s\"", StringUtils.escapeString(value));
        }
    };
}
Also used : DexReader(org.jf.dexlib2.dexbacked.DexReader) Nonnull(javax.annotation.Nonnull) AnnotatedBytes(org.jf.dexlib2.util.AnnotatedBytes) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 9 with DexReader

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

the class HiddenApiClassDataItem method makeAnnotator.

@Nonnull
public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
    return new SectionAnnotator(annotator, mapItem) {

        @Nonnull
        @Override
        public String getItemName() {
            return "hiddenapi_class_data_item";
        }

        @Override
        protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
            int startOffset = out.getCursor();
            out.annotate(4, "size = 0x%x", dexFile.getDataBuffer().readSmallUint(out.getCursor()));
            int index = 0;
            for (ClassDef classDef : dexFile.getClasses()) {
                out.annotate(0, "[%d] %s", index, classDef);
                out.indent();
                int offset = dexFile.getDataBuffer().readSmallUint(out.getCursor());
                if (offset == 0) {
                    out.annotate(4, "offset = 0x%x", offset);
                } else {
                    out.annotate(4, "offset = 0x%x (absolute offset: 0x%x)", offset, startOffset + offset);
                }
                int nextOffset = out.getCursor();
                if (offset > 0) {
                    out.deindent();
                    out.moveTo(startOffset + offset);
                    DexReader<? extends DexBuffer> reader = dexFile.getBuffer().readerAt(out.getCursor());
                    for (Field field : classDef.getStaticFields()) {
                        out.annotate(0, "%s:", field);
                        out.indent();
                        int restrictions = reader.readSmallUleb128();
                        out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s", restrictions, HiddenApiRestriction.formatHiddenRestrictions(restrictions));
                        out.deindent();
                    }
                    for (Field field : classDef.getInstanceFields()) {
                        out.annotate(0, "%s:", field);
                        out.indent();
                        int restrictions = reader.readSmallUleb128();
                        out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s", restrictions, HiddenApiRestriction.formatHiddenRestrictions(restrictions));
                        out.deindent();
                    }
                    for (Method method : classDef.getDirectMethods()) {
                        out.annotate(0, "%s:", method);
                        out.indent();
                        int restrictions = reader.readSmallUleb128();
                        out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s", restrictions, HiddenApiRestriction.formatHiddenRestrictions(restrictions));
                        out.deindent();
                    }
                    for (Method method : classDef.getVirtualMethods()) {
                        out.annotate(0, "%s:", method);
                        out.indent();
                        int restrictions = reader.readSmallUleb128();
                        out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s", restrictions, HiddenApiRestriction.formatHiddenRestrictions(restrictions));
                        out.deindent();
                    }
                    out.indent();
                }
                out.moveTo(nextOffset);
                out.deindent();
                index++;
            }
        }
    };
}
Also used : Field(org.jf.dexlib2.iface.Field) ClassDef(org.jf.dexlib2.iface.ClassDef) Nonnull(javax.annotation.Nonnull) Method(org.jf.dexlib2.iface.Method) AnnotatedBytes(org.jf.dexlib2.util.AnnotatedBytes) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 10 with DexReader

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

the class AnnotationItem method getReferenceAnnotation.

public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int annotationItemOffset) {
    try {
        DexReader reader = dexFile.getDataBuffer().readerAt(annotationItemOffset);
        reader.readUbyte();
        int typeIndex = reader.readSmallUleb128();
        String annotationType = dexFile.getTypeSection().get(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);
}
Also used : DexReader(org.jf.dexlib2.dexbacked.DexReader)

Aggregations

Nonnull (javax.annotation.Nonnull)15 DexReader (org.jf.dexlib2.dexbacked.DexReader)9 Nullable (javax.annotation.Nullable)7 AnnotatedBytes (org.jf.dexlib2.util.AnnotatedBytes)7 AnnotationsDirectory (org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)5 VariableSizeLookaheadIterator (org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator)5 Instruction (org.jf.dexlib2.iface.instruction.Instruction)3 Opcode (org.jf.dexlib2.Opcode)2 DexBackedInstruction (org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction)2 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)2 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)2 ExceptionWithContext (org.jf.util.ExceptionWithContext)2 File (java.io.File)1 CDexBackedDexFile (org.jf.dexlib2.dexbacked.CDexBackedDexFile)1 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)1 DexBackedFieldReference (org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference)1 DexBackedMethodReference (org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference)1 EncodedArrayItemIterator (org.jf.dexlib2.dexbacked.util.EncodedArrayItemIterator)1