Search in sources :

Example 1 with DexReader

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

the class CodeItem method makeAnnotator.

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

        private SectionAnnotator debugInfoAnnotator = null;

        @Override
        public void annotateSection(@Nonnull AnnotatedBytes out) {
            debugInfoAnnotator = annotator.getAnnotator(ItemType.DEBUG_INFO_ITEM);
            super.annotateSection(out);
        }

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

        @Override
        public int getItemAlignment() {
            return 4;
        }

        @Override
        public void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
            try {
                DexReader reader = dexFile.readerAt(out.getCursor());
                int registers = reader.readUshort();
                out.annotate(2, "registers_size = %d", registers);
                int inSize = reader.readUshort();
                out.annotate(2, "ins_size = %d", inSize);
                int outSize = reader.readUshort();
                out.annotate(2, "outs_size = %d", outSize);
                int triesCount = reader.readUshort();
                out.annotate(2, "tries_size = %d", triesCount);
                int debugInfoOffset = reader.readInt();
                out.annotate(4, "debug_info_off = 0x%x", debugInfoOffset);
                if (debugInfoOffset > 0) {
                    addDebugInfoIdentity(debugInfoOffset, itemIdentity);
                }
                int instructionSize = reader.readSmallUint();
                out.annotate(4, "insns_size = 0x%x", instructionSize);
                out.annotate(0, "instructions:");
                out.indent();
                out.setLimit(out.getCursor(), out.getCursor() + instructionSize * 2);
                int end = reader.getOffset() + instructionSize * 2;
                try {
                    while (reader.getOffset() < end) {
                        Instruction instruction = DexBackedInstruction.readFrom(reader);
                        // if we read past the end of the instruction list
                        if (reader.getOffset() > end) {
                            out.annotateTo(end, "truncated instruction");
                            reader.setOffset(end);
                        } else {
                            switch(instruction.getOpcode().format) {
                                case Format10x:
                                    annotateInstruction10x(out, instruction);
                                    break;
                                case Format35c:
                                    annotateInstruction35c(out, (Instruction35c) instruction);
                                    break;
                                case Format3rc:
                                    annotateInstruction3rc(out, (Instruction3rc) instruction);
                                    break;
                                case ArrayPayload:
                                    annotateArrayPayload(out, (ArrayPayload) instruction);
                                    break;
                                case PackedSwitchPayload:
                                    annotatePackedSwitchPayload(out, (PackedSwitchPayload) instruction);
                                    break;
                                case SparseSwitchPayload:
                                    annotateSparseSwitchPayload(out, (SparseSwitchPayload) instruction);
                                    break;
                                default:
                                    annotateDefaultInstruction(out, instruction);
                                    break;
                            }
                        }
                        assert reader.getOffset() == out.getCursor();
                    }
                } catch (ExceptionWithContext ex) {
                    ex.printStackTrace(System.err);
                    out.annotate(0, "annotation error: %s", ex.getMessage());
                    out.moveTo(end);
                    reader.setOffset(end);
                } finally {
                    out.clearLimit();
                    out.deindent();
                }
                if (triesCount > 0) {
                    if ((reader.getOffset() % 4) != 0) {
                        reader.readUshort();
                        out.annotate(2, "padding");
                    }
                    out.annotate(0, "try_items:");
                    out.indent();
                    try {
                        for (int i = 0; i < triesCount; i++) {
                            out.annotate(0, "try_item[%d]:", i);
                            out.indent();
                            try {
                                int startAddr = reader.readSmallUint();
                                out.annotate(4, "start_addr = 0x%x", startAddr);
                                int instructionCount = reader.readUshort();
                                out.annotate(2, "insn_count = 0x%x", instructionCount);
                                int handlerOffset = reader.readUshort();
                                out.annotate(2, "handler_off = 0x%x", handlerOffset);
                            } finally {
                                out.deindent();
                            }
                        }
                    } finally {
                        out.deindent();
                    }
                    int handlerListCount = reader.readSmallUleb128();
                    out.annotate(0, "encoded_catch_handler_list:");
                    out.annotateTo(reader.getOffset(), "size = %d", handlerListCount);
                    out.indent();
                    try {
                        for (int i = 0; i < handlerListCount; i++) {
                            out.annotate(0, "encoded_catch_handler[%d]", i);
                            out.indent();
                            try {
                                int handlerCount = reader.readSleb128();
                                out.annotateTo(reader.getOffset(), "size = %d", handlerCount);
                                boolean hasCatchAll = handlerCount <= 0;
                                handlerCount = Math.abs(handlerCount);
                                if (handlerCount != 0) {
                                    out.annotate(0, "handlers:");
                                    out.indent();
                                    try {
                                        for (int j = 0; j < handlerCount; j++) {
                                            out.annotate(0, "encoded_type_addr_pair[%d]", i);
                                            out.indent();
                                            try {
                                                int typeIndex = reader.readSmallUleb128();
                                                out.annotateTo(reader.getOffset(), TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));
                                                int handlerAddress = reader.readSmallUleb128();
                                                out.annotateTo(reader.getOffset(), "addr = 0x%x", handlerAddress);
                                            } finally {
                                                out.deindent();
                                            }
                                        }
                                    } finally {
                                        out.deindent();
                                    }
                                }
                                if (hasCatchAll) {
                                    int catchAllAddress = reader.readSmallUleb128();
                                    out.annotateTo(reader.getOffset(), "catch_all_addr = 0x%x", catchAllAddress);
                                }
                            } finally {
                                out.deindent();
                            }
                        }
                    } finally {
                        out.deindent();
                    }
                }
            } catch (ExceptionWithContext ex) {
                out.annotate(0, "annotation error: %s", ex.getMessage());
            }
        }

        private String formatRegister(int registerNum) {
            return String.format("v%d", registerNum);
        }

        private void annotateInstruction10x(@Nonnull AnnotatedBytes out, @Nonnull Instruction instruction) {
            out.annotate(2, instruction.getOpcode().name);
        }

        private void annotateInstruction35c(@Nonnull AnnotatedBytes out, @Nonnull Instruction35c instruction) {
            List<String> args = Lists.newArrayList();
            int registerCount = instruction.getRegisterCount();
            if (registerCount == 1) {
                args.add(formatRegister(instruction.getRegisterC()));
            } else if (registerCount == 2) {
                args.add(formatRegister(instruction.getRegisterC()));
                args.add(formatRegister(instruction.getRegisterD()));
            } else if (registerCount == 3) {
                args.add(formatRegister(instruction.getRegisterC()));
                args.add(formatRegister(instruction.getRegisterD()));
                args.add(formatRegister(instruction.getRegisterE()));
            } else if (registerCount == 4) {
                args.add(formatRegister(instruction.getRegisterC()));
                args.add(formatRegister(instruction.getRegisterD()));
                args.add(formatRegister(instruction.getRegisterE()));
                args.add(formatRegister(instruction.getRegisterF()));
            } else if (registerCount == 5) {
                args.add(formatRegister(instruction.getRegisterC()));
                args.add(formatRegister(instruction.getRegisterD()));
                args.add(formatRegister(instruction.getRegisterE()));
                args.add(formatRegister(instruction.getRegisterF()));
                args.add(formatRegister(instruction.getRegisterG()));
            }
            String reference = ReferenceUtil.getReferenceString(instruction.getReference());
            out.annotate(6, String.format("%s {%s}, %s", instruction.getOpcode().name, Joiner.on(", ").join(args), reference));
        }

        private void annotateInstruction3rc(@Nonnull AnnotatedBytes out, @Nonnull Instruction3rc instruction) {
            int startRegister = instruction.getStartRegister();
            int endRegister = startRegister + instruction.getRegisterCount() - 1;
            String reference = ReferenceUtil.getReferenceString(instruction.getReference());
            out.annotate(6, String.format("%s {%s .. %s}, %s", instruction.getOpcode().name, formatRegister(startRegister), formatRegister(endRegister), reference));
        }

        private void annotateDefaultInstruction(@Nonnull AnnotatedBytes out, @Nonnull Instruction instruction) {
            List<String> args = Lists.newArrayList();
            if (instruction instanceof OneRegisterInstruction) {
                args.add(formatRegister(((OneRegisterInstruction) instruction).getRegisterA()));
                if (instruction instanceof TwoRegisterInstruction) {
                    args.add(formatRegister(((TwoRegisterInstruction) instruction).getRegisterB()));
                    if (instruction instanceof ThreeRegisterInstruction) {
                        args.add(formatRegister(((ThreeRegisterInstruction) instruction).getRegisterC()));
                    }
                }
            } else if (instruction instanceof VerificationErrorInstruction) {
                String verificationError = VerificationError.getVerificationErrorName(((VerificationErrorInstruction) instruction).getVerificationError());
                if (verificationError != null) {
                    args.add(verificationError);
                } else {
                    args.add("invalid verification error type");
                }
            }
            if (instruction instanceof ReferenceInstruction) {
                args.add(ReferenceUtil.getReferenceString(((ReferenceInstruction) instruction).getReference()));
            } else if (instruction instanceof OffsetInstruction) {
                int offset = ((OffsetInstruction) instruction).getCodeOffset();
                String sign = offset >= 0 ? "+" : "-";
                args.add(String.format("%s0x%x", sign, Math.abs(offset)));
            } else if (instruction instanceof NarrowLiteralInstruction) {
                int value = ((NarrowLiteralInstruction) instruction).getNarrowLiteral();
                if (NumberUtils.isLikelyFloat(value)) {
                    args.add(String.format("%d # %f", value, Float.intBitsToFloat(value)));
                } else {
                    args.add(String.format("%d", value));
                }
            } else if (instruction instanceof WideLiteralInstruction) {
                long value = ((WideLiteralInstruction) instruction).getWideLiteral();
                if (NumberUtils.isLikelyDouble(value)) {
                    args.add(String.format("%d # %f", value, Double.longBitsToDouble(value)));
                } else {
                    args.add(String.format("%d", value));
                }
            } else if (instruction instanceof FieldOffsetInstruction) {
                int fieldOffset = ((FieldOffsetInstruction) instruction).getFieldOffset();
                args.add(String.format("field@0x%x", fieldOffset));
            } else if (instruction instanceof VtableIndexInstruction) {
                int vtableIndex = ((VtableIndexInstruction) instruction).getVtableIndex();
                args.add(String.format("vtable@%d", vtableIndex));
            } else if (instruction instanceof InlineIndexInstruction) {
                int inlineIndex = ((InlineIndexInstruction) instruction).getInlineIndex();
                args.add(String.format("inline@%d", inlineIndex));
            }
            out.annotate(instruction.getCodeUnits() * 2, "%s %s", instruction.getOpcode().name, Joiner.on(", ").join(args));
        }

        private void annotateArrayPayload(@Nonnull AnnotatedBytes out, @Nonnull ArrayPayload instruction) {
            List<Number> elements = instruction.getArrayElements();
            int elementWidth = instruction.getElementWidth();
            out.annotate(2, instruction.getOpcode().name);
            out.indent();
            out.annotate(2, "element_width = %d", elementWidth);
            out.annotate(4, "size = %d", elements.size());
            out.annotate(0, "elements:");
            out.indent();
            for (int i = 0; i < elements.size(); i++) {
                if (elementWidth == 8) {
                    long value = elements.get(i).longValue();
                    if (NumberUtils.isLikelyDouble(value)) {
                        out.annotate(elementWidth, "element[%d] = %d # %f", i, value, Double.longBitsToDouble(value));
                    } else {
                        out.annotate(elementWidth, "element[%d] = %d", i, value);
                    }
                } else {
                    int value = elements.get(i).intValue();
                    if (NumberUtils.isLikelyFloat(value)) {
                        out.annotate(elementWidth, "element[%d] = %d # %f", i, value, Float.intBitsToFloat(value));
                    } else {
                        out.annotate(elementWidth, "element[%d] = %d", i, value);
                    }
                }
            }
            if (out.getCursor() % 2 != 0) {
                out.annotate(1, "padding");
            }
            out.deindent();
            out.deindent();
        }

        private void annotatePackedSwitchPayload(@Nonnull AnnotatedBytes out, @Nonnull PackedSwitchPayload instruction) {
            List<? extends SwitchElement> elements = instruction.getSwitchElements();
            out.annotate(2, instruction.getOpcode().name);
            out.indent();
            out.annotate(2, "size = %d", elements.size());
            if (elements.size() == 0) {
                out.annotate(4, "first_key");
            } else {
                out.annotate(4, "first_key = %d", elements.get(0).getKey());
                out.annotate(0, "targets:");
                out.indent();
                for (int i = 0; i < elements.size(); i++) {
                    out.annotate(4, "target[%d] = %d", i, elements.get(i).getOffset());
                }
                out.deindent();
            }
            out.deindent();
        }

        private void annotateSparseSwitchPayload(@Nonnull AnnotatedBytes out, @Nonnull SparseSwitchPayload instruction) {
            List<? extends SwitchElement> elements = instruction.getSwitchElements();
            out.annotate(2, instruction.getOpcode().name);
            out.indent();
            out.annotate(2, "size = %d", elements.size());
            if (elements.size() > 0) {
                out.annotate(0, "keys:");
                out.indent();
                for (int i = 0; i < elements.size(); i++) {
                    out.annotate(4, "key[%d] = %d", i, elements.get(i).getKey());
                }
                out.deindent();
                out.annotate(0, "targets:");
                out.indent();
                for (int i = 0; i < elements.size(); i++) {
                    out.annotate(4, "target[%d] = %d", i, elements.get(i).getOffset());
                }
                out.deindent();
            }
            out.deindent();
        }

        private void addDebugInfoIdentity(int debugInfoOffset, String methodString) {
            if (debugInfoAnnotator != null) {
                debugInfoAnnotator.setItemIdentity(debugInfoOffset, methodString);
            }
        }
    };
}
Also used : DexReader(org.jf.dexlib2.dexbacked.DexReader) DexBackedInstruction(org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction) ExceptionWithContext(org.jf.util.ExceptionWithContext) Nonnull(javax.annotation.Nonnull) AnnotatedBytes(org.jf.dexlib2.util.AnnotatedBytes) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 2 with DexReader

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

the class DexBackedClassDef method getDirectMethods.

@Nonnull
public Iterable<? extends DexBackedMethod> getDirectMethods(final boolean skipDuplicates) {
    if (directMethodCount > 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(getDirectMethodsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int methodsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForDirectMethods();
        return new Iterable<DexBackedMethod>() {

            @Nonnull
            @Override
            public Iterator<DexBackedMethod> iterator() {
                final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator = annotationsDirectory.getMethodAnnotationIterator();
                final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator = annotationsDirectory.getParameterAnnotationIterator();
                return new VariableSizeLookaheadIterator<DexBackedMethod>(dexFile.getDataBuffer(), methodsStartOffset) {

                    private int count;

                    @Nullable
                    private MethodReference previousMethod;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedMethod readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > directMethodCount) {
                                virtualMethodsOffset = reader.getOffset();
                                return endOfData();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedMethod item = new DexBackedMethod(dexFile, reader, DexBackedClassDef.this, previousIndex, methodAnnotationIterator, parameterAnnotationIterator, hiddenApiRestrictions);
                            MethodReference currentMethod = previousMethod;
                            MethodReference nextMethod = ImmutableMethodReference.of(item);
                            previousMethod = nextMethod;
                            previousIndex = item.methodIndex;
                            if (skipDuplicates && currentMethod != null && currentMethod.equals(nextMethod)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        if (directMethodsOffset > 0) {
            virtualMethodsOffset = directMethodsOffset;
        }
        return ImmutableSet.of();
    }
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) Nonnull(javax.annotation.Nonnull)

Example 3 with DexReader

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

the class DexBackedClassDef method getStaticFields.

@Nonnull
public Iterable<? extends DexBackedField> getStaticFields(final boolean skipDuplicates) {
    if (staticFieldCount > 0) {
        DexReader<? extends DexBuffer> reader = dexFile.getDataBuffer().readerAt(staticFieldsOffset);
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int staticInitialValuesOffset = dexFile.getBuffer().readSmallUint(classDefOffset + ClassDefItem.STATIC_VALUES_OFFSET);
        final int fieldsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForStaticFields();
        return new Iterable<DexBackedField>() {

            @Nonnull
            @Override
            public Iterator<DexBackedField> iterator() {
                final AnnotationsDirectory.AnnotationIterator annotationIterator = annotationsDirectory.getFieldAnnotationIterator();
                final EncodedArrayItemIterator staticInitialValueIterator = EncodedArrayItemIterator.newOrEmpty(dexFile, staticInitialValuesOffset);
                return new VariableSizeLookaheadIterator<DexBackedField>(dexFile.getDataBuffer(), fieldsStartOffset) {

                    private int count;

                    @Nullable
                    private FieldReference previousField;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedField readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > staticFieldCount) {
                                instanceFieldsOffset = reader.getOffset();
                                return endOfData();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedField item = new DexBackedField(dexFile, reader, DexBackedClassDef.this, previousIndex, staticInitialValueIterator, annotationIterator, hiddenApiRestrictions);
                            FieldReference currentField = previousField;
                            FieldReference nextField = ImmutableFieldReference.of(item);
                            previousField = nextField;
                            previousIndex = item.fieldIndex;
                            if (skipDuplicates && currentField != null && currentField.equals(nextField)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        instanceFieldsOffset = staticFieldsOffset;
        return ImmutableSet.of();
    }
}
Also used : ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Nonnull(javax.annotation.Nonnull) AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) EncodedArrayItemIterator(org.jf.dexlib2.dexbacked.util.EncodedArrayItemIterator) Nonnull(javax.annotation.Nonnull)

Example 4 with DexReader

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

the class DexBackedClassDef method getVirtualMethods.

@Nonnull
public Iterable<? extends DexBackedMethod> getVirtualMethods(final boolean skipDuplicates) {
    if (virtualMethodCount > 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(getVirtualMethodsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int methodsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForVirtualMethods();
        return new Iterable<DexBackedMethod>() {

            final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator = annotationsDirectory.getMethodAnnotationIterator();

            final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator = annotationsDirectory.getParameterAnnotationIterator();

            @Nonnull
            @Override
            public Iterator<DexBackedMethod> iterator() {
                return new VariableSizeLookaheadIterator<DexBackedMethod>(dexFile.getDataBuffer(), methodsStartOffset) {

                    private int count;

                    @Nullable
                    private MethodReference previousMethod;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedMethod readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > virtualMethodCount) {
                                return endOfData();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedMethod item = new DexBackedMethod(dexFile, reader, DexBackedClassDef.this, previousIndex, methodAnnotationIterator, parameterAnnotationIterator, hiddenApiRestrictions);
                            MethodReference currentMethod = previousMethod;
                            MethodReference nextMethod = ImmutableMethodReference.of(item);
                            previousMethod = nextMethod;
                            previousIndex = item.methodIndex;
                            if (skipDuplicates && currentMethod != null && currentMethod.equals(nextMethod)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        return ImmutableSet.of();
    }
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) Nonnull(javax.annotation.Nonnull)

Example 5 with DexReader

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

the class DexBackedClassDef method getSize.

/**
 * Calculate and return the private size of a class definition.
 *
 * Calculated as: class_def_item size + type_id size + interfaces type_list +
 * annotations_directory_item overhead + class_data_item + static values overhead +
 * methods size + fields size
 *
 * @return size in bytes
 */
public int getSize() {
    // class_def_item has 8 uint fields in dex files
    int size = 8 * 4;
    // type_ids size
    size += TypeIdItem.ITEM_SIZE;
    // add interface list size if any
    int interfacesLength = getInterfaces().size();
    if (interfacesLength > 0) {
        // add size of the type_list
        // uint for size
        size += 4;
        // ushort per type_item
        size += interfacesLength * 2;
    }
    // annotations directory size if it exists
    AnnotationsDirectory directory = getAnnotationsDirectory();
    if (!AnnotationsDirectory.EMPTY.equals(directory)) {
        // 4 uints in annotations_directory_item
        size += 4 * 4;
        Set<? extends DexBackedAnnotation> classAnnotations = directory.getClassAnnotations();
        if (!classAnnotations.isEmpty()) {
            // uint for size
            size += 4;
            // uint per annotation_off
            size += classAnnotations.size() * 4;
        // TODO: should we add annotation_item size? what if it's shared?
        }
    }
    // static values and/or metadata
    int staticInitialValuesOffset = dexFile.getBuffer().readSmallUint(classDefOffset + ClassDefItem.STATIC_VALUES_OFFSET);
    if (staticInitialValuesOffset != 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(staticInitialValuesOffset);
        // encoded_array size field
        size += reader.peekSmallUleb128Size();
    }
    // class_data_item
    int classDataOffset = dexFile.getBuffer().readSmallUint(classDefOffset + ClassDefItem.CLASS_DATA_OFFSET);
    if (classDataOffset > 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(classDataOffset);
        // staticFieldCount
        reader.readSmallUleb128();
        // instanceFieldCount
        reader.readSmallUleb128();
        // directMethodCount
        reader.readSmallUleb128();
        // virtualMethodCount
        reader.readSmallUleb128();
        size += reader.getOffset() - classDataOffset;
    }
    for (DexBackedField dexBackedField : getFields()) {
        size += dexBackedField.getSize();
    }
    for (DexBackedMethod dexBackedMethod : getMethods()) {
        size += dexBackedMethod.getSize();
    }
    return size;
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)

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