com.android.dex.SizeOf Java Examples

The following examples show how to use com.android.dex.SizeOf. 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 File: DexMerger.java    From buck with Apache License 2.0 5 votes vote down vote up
private void plus(Dex d, boolean exact) {
    TableOfContents contents = d.getTableOfContents();
    idsDefs += contents.stringIds.size * SizeOf.STRING_ID_ITEM
            + contents.typeIds.size * SizeOf.TYPE_ID_ITEM
            + contents.protoIds.size * SizeOf.PROTO_ID_ITEM
            + contents.fieldIds.size * SizeOf.MEMBER_ID_ITEM
            + contents.methodIds.size * SizeOf.MEMBER_ID_ITEM
            + contents.classDefs.size * SizeOf.CLASS_DEF_ITEM;
    mapList = SizeOf.UINT + (contents.sections.length * SizeOf.MAP_ITEM);
    typeList += fourByteAlign(contents.typeLists.byteCount); // We count each dex's
    // typelists section as realigned on 4 bytes, because each typelist of each dex's
    // typelists section is aligned on 4 bytes. If we didn't, there is a case where each
    // size of both dex's typelists section is a multiple of 2 but not a multiple of 4,
    // and the sum of both sizes is a multiple of 4 but would not be sufficient to write
    // each typelist aligned on 4 bytes.
    stringData += contents.stringDatas.byteCount;
    annotationsDirectory += contents.annotationsDirectories.byteCount;
    annotationsSet += contents.annotationSets.byteCount;
    annotationsSetRefList += contents.annotationSetRefLists.byteCount;

    if (exact) {
        code += contents.codes.byteCount;
        classData += contents.classDatas.byteCount;
        encodedArray += contents.encodedArrays.byteCount;
        annotation += contents.annotations.byteCount;
        debugInfo += contents.debugInfos.byteCount;
    } else {
        // at most 1/4 of the bytes in a code section are uleb/sleb
        code += (int) Math.ceil(contents.codes.byteCount * 1.25);
        // at most 1/3 of the bytes in a class data section are uleb/sleb
        classData += (int) Math.ceil(contents.classDatas.byteCount * 1.34);
        // all of the bytes in an encoding arrays section may be uleb/sleb
        encodedArray += contents.encodedArrays.byteCount * 2;
        // all of the bytes in an annotations section may be uleb/sleb
        annotation += (int) Math.ceil(contents.annotations.byteCount * 2);
        // all of the bytes in a debug info section may be uleb/sleb
        debugInfo += computeDuplicatedDebugInfoSize(d) * 2;
    }
}
 
Example #2
Source File: ProtoIdItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.PROTO_ID_ITEM;
}
 
Example #3
Source File: DexMerger.java    From Box with Apache License 2.0 4 votes vote down vote up
private void plus(TableOfContents contents, boolean exact) {
    idsDefs += contents.stringIds.size * SizeOf.STRING_ID_ITEM
            + contents.typeIds.size * SizeOf.TYPE_ID_ITEM
            + contents.protoIds.size * SizeOf.PROTO_ID_ITEM
            + contents.fieldIds.size * SizeOf.MEMBER_ID_ITEM
            + contents.methodIds.size * SizeOf.MEMBER_ID_ITEM
            + contents.classDefs.size * SizeOf.CLASS_DEF_ITEM;
    mapList = SizeOf.UINT + (contents.sections.length * SizeOf.MAP_ITEM);
    typeList += fourByteAlign(contents.typeLists.byteCount); // We count each dex's
    // typelists section as realigned on 4 bytes, because each typelist of each dex's
    // typelists section is aligned on 4 bytes. If we didn't, there is a case where each
    // size of both dex's typelists section is a multiple of 2 but not a multiple of 4,
    // and the sum of both sizes is a multiple of 4 but would not be sufficient to write
    // each typelist aligned on 4 bytes.
    stringData += contents.stringDatas.byteCount;
    annotationsDirectory += contents.annotationsDirectories.byteCount;
    annotationsSet += contents.annotationSets.byteCount;
    annotationsSetRefList += contents.annotationSetRefLists.byteCount;

    if (exact) {
        code += contents.codes.byteCount;
        classData += contents.classDatas.byteCount;
        encodedArray += contents.encodedArrays.byteCount;
        annotation += contents.annotations.byteCount;
        debugInfo += contents.debugInfos.byteCount;
    } else {
        // at most 1/4 of the bytes in a code section are uleb/sleb
        code += (int) Math.ceil(contents.codes.byteCount * 1.25);
        // at most 2/3 of the bytes in a class data section are uleb/sleb that may change
        // (assuming the worst case that section contains only methods and no fields)
        classData += (int) Math.ceil(contents.classDatas.byteCount * 1.67);
        // all of the bytes in an encoding arrays section may be uleb/sleb
        encodedArray += contents.encodedArrays.byteCount * 2;
        // all of the bytes in an annotations section may be uleb/sleb
        annotation += (int) Math.ceil(contents.annotations.byteCount * 2);
        // all of the bytes in a debug info section may be uleb/sleb. The additive constant
        // is a fudge factor observed to be required when merging small
        // DEX files (b/68483205).
        debugInfo += contents.debugInfos.byteCount * 2 + 8;
    }
}
 
Example #4
Source File: MemberIdItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.MEMBER_ID_ITEM;
}
 
Example #5
Source File: StringIdItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.STRING_ID_ITEM;
}
 
Example #6
Source File: HeaderItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.HEADER_ITEM;
}
 
Example #7
Source File: HeaderItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    int mapOff = file.getMap().getFileOffset();
    Section firstDataSection = file.getFirstDataSection();
    Section lastDataSection = file.getLastDataSection();
    int dataOff = firstDataSection.getFileOffset();
    int dataSize = lastDataSection.getFileOffset() +
        lastDataSection.writeSize() - dataOff;

    String magic = file.getDexOptions().getMagic();

    if (out.annotates()) {
        out.annotate(8, "magic: " + new CstString(magic).toQuoted());
        out.annotate(4, "checksum");
        out.annotate(20, "signature");
        out.annotate(4, "file_size:       " +
                     Hex.u4(file.getFileSize()));
        out.annotate(4, "header_size:     " + Hex.u4(SizeOf.HEADER_ITEM));
        out.annotate(4, "endian_tag:      " + Hex.u4(DexFormat.ENDIAN_TAG));
        out.annotate(4, "link_size:       0");
        out.annotate(4, "link_off:        0");
        out.annotate(4, "map_off:         " + Hex.u4(mapOff));
    }

    // Write the magic number.
    for (int i = 0; i < 8; i++) {
        out.writeByte(magic.charAt(i));
    }

    // Leave space for the checksum and signature.
    out.writeZeroes(24);

    out.writeInt(file.getFileSize());
    out.writeInt(SizeOf.HEADER_ITEM);
    out.writeInt(DexFormat.ENDIAN_TAG);

    /*
     * Write zeroes for the link size and data, as the output
     * isn't a staticly linked file.
     */
    out.writeZeroes(8);

    out.writeInt(mapOff);

    // Write out each section's respective header part.
    file.getStringIds().writeHeaderPart(out);
    file.getTypeIds().writeHeaderPart(out);
    file.getProtoIds().writeHeaderPart(out);
    file.getFieldIds().writeHeaderPart(out);
    file.getMethodIds().writeHeaderPart(out);
    file.getClassDefs().writeHeaderPart(out);

    if (out.annotates()) {
        out.annotate(4, "data_size:       " + Hex.u4(dataSize));
        out.annotate(4, "data_off:        " + Hex.u4(dataOff));
    }

    out.writeInt(dataSize);
    out.writeInt(dataOff);
}
 
Example #8
Source File: ClassDefItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.CLASS_DEF_ITEM;
}
 
Example #9
Source File: TypeIdItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.TYPE_ID_ITEM;
}
 
Example #10
Source File: ProtoIdItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.PROTO_ID_ITEM;
}
 
Example #11
Source File: MemberIdItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.MEMBER_ID_ITEM;
}
 
Example #12
Source File: StringIdItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.STRING_ID_ITEM;
}
 
Example #13
Source File: HeaderItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.HEADER_ITEM;
}
 
Example #14
Source File: HeaderItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    int mapOff = file.getMap().getFileOffset();
    Section firstDataSection = file.getFirstDataSection();
    Section lastDataSection = file.getLastDataSection();
    int dataOff = firstDataSection.getFileOffset();
    int dataSize = lastDataSection.getFileOffset() +
        lastDataSection.writeSize() - dataOff;

    String magic = file.getDexOptions().getMagic();

    if (out.annotates()) {
        out.annotate(8, "magic: " + new CstString(magic).toQuoted());
        out.annotate(4, "checksum");
        out.annotate(20, "signature");
        out.annotate(4, "file_size:       " +
                     Hex.u4(file.getFileSize()));
        out.annotate(4, "header_size:     " + Hex.u4(SizeOf.HEADER_ITEM));
        out.annotate(4, "endian_tag:      " + Hex.u4(DexFormat.ENDIAN_TAG));
        out.annotate(4, "link_size:       0");
        out.annotate(4, "link_off:        0");
        out.annotate(4, "map_off:         " + Hex.u4(mapOff));
    }

    // Write the magic number.
    for (int i = 0; i < 8; i++) {
        out.writeByte(magic.charAt(i));
    }

    // Leave space for the checksum and signature.
    out.writeZeroes(24);

    out.writeInt(file.getFileSize());
    out.writeInt(SizeOf.HEADER_ITEM);
    out.writeInt(DexFormat.ENDIAN_TAG);

    /*
     * Write zeroes for the link size and data, as the output
     * isn't a staticly linked file.
     */
    out.writeZeroes(8);

    out.writeInt(mapOff);

    // Write out each section's respective header part.
    file.getStringIds().writeHeaderPart(out);
    file.getTypeIds().writeHeaderPart(out);
    file.getProtoIds().writeHeaderPart(out);
    file.getFieldIds().writeHeaderPart(out);
    file.getMethodIds().writeHeaderPart(out);
    file.getClassDefs().writeHeaderPart(out);

    if (out.annotates()) {
        out.annotate(4, "data_size:       " + Hex.u4(dataSize));
        out.annotate(4, "data_off:        " + Hex.u4(dataOff));
    }

    out.writeInt(dataSize);
    out.writeInt(dataOff);
}
 
Example #15
Source File: ClassDefItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.CLASS_DEF_ITEM;
}
 
Example #16
Source File: TypeIdItem.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.TYPE_ID_ITEM;
}
 
Example #17
Source File: DexMerger.java    From buck with Apache License 2.0 4 votes vote down vote up
private void transformCode(Dex in, Code code, IndexMap indexMap) {
    contentsOut.codes.size++;
    codeOut.assertFourByteAligned();

    codeOut.writeUnsignedShort(code.getRegistersSize());
    codeOut.writeUnsignedShort(code.getInsSize());
    codeOut.writeUnsignedShort(code.getOutsSize());

    Code.Try[] tries = code.getTries();
    Code.CatchHandler[] catchHandlers = code.getCatchHandlers();
    codeOut.writeUnsignedShort(tries.length);

    int debugInfoOffset = code.getDebugInfoOffset();
    if (debugInfoOffset != 0) {
        codeOut.writeInt(debugInfoOut.getPosition());
        transformDebugInfoItem(in.open(debugInfoOffset), indexMap);
    } else {
        codeOut.writeInt(0);
    }

    short[] instructions = code.getInstructions();
    short[] newInstructions = instructionTransformer.transform(indexMap, instructions);
    codeOut.writeInt(newInstructions.length);
    codeOut.write(newInstructions);

    if (tries.length > 0) {
        if (newInstructions.length % 2 == 1) {
            codeOut.writeShort((short) 0); // padding
        }

        /*
         * We can't write the tries until we've written the catch handlers.
         * Unfortunately they're in the opposite order in the dex file so we
         * need to transform them out-of-order.
         */
        Dex.Section triesSection = dexOut.open(codeOut.getPosition());
        codeOut.skip(tries.length * SizeOf.TRY_ITEM);
        int[] offsets = transformCatchHandlers(indexMap, catchHandlers);
        transformTries(triesSection, tries, offsets);
    }
}
 
Example #18
Source File: ProtoIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.PROTO_ID_ITEM;
}
 
Example #19
Source File: MemberIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.MEMBER_ID_ITEM;
}
 
Example #20
Source File: StringIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.STRING_ID_ITEM;
}
 
Example #21
Source File: HeaderItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.HEADER_ITEM;
}
 
Example #22
Source File: HeaderItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    int mapOff = file.getMap().getFileOffset();
    Section firstDataSection = file.getFirstDataSection();
    Section lastDataSection = file.getLastDataSection();
    int dataOff = firstDataSection.getFileOffset();
    int dataSize = lastDataSection.getFileOffset() +
        lastDataSection.writeSize() - dataOff;

    String magic = file.getDexOptions().getMagic();

    if (out.annotates()) {
        out.annotate(8, "magic: " + new CstString(magic).toQuoted());
        out.annotate(4, "checksum");
        out.annotate(20, "signature");
        out.annotate(4, "file_size:       " +
                     Hex.u4(file.getFileSize()));
        out.annotate(4, "header_size:     " + Hex.u4(SizeOf.HEADER_ITEM));
        out.annotate(4, "endian_tag:      " + Hex.u4(DexFormat.ENDIAN_TAG));
        out.annotate(4, "link_size:       0");
        out.annotate(4, "link_off:        0");
        out.annotate(4, "map_off:         " + Hex.u4(mapOff));
    }

    // Write the magic number.
    for (int i = 0; i < 8; i++) {
        out.writeByte(magic.charAt(i));
    }

    // Leave space for the checksum and signature.
    out.writeZeroes(24);

    out.writeInt(file.getFileSize());
    out.writeInt(SizeOf.HEADER_ITEM);
    out.writeInt(DexFormat.ENDIAN_TAG);

    /*
     * Write zeroes for the link size and data, as the output
     * isn't a staticly linked file.
     */
    out.writeZeroes(8);

    out.writeInt(mapOff);

    // Write out each section's respective header part.
    file.getStringIds().writeHeaderPart(out);
    file.getTypeIds().writeHeaderPart(out);
    file.getProtoIds().writeHeaderPart(out);
    file.getFieldIds().writeHeaderPart(out);
    file.getMethodIds().writeHeaderPart(out);
    file.getClassDefs().writeHeaderPart(out);

    if (out.annotates()) {
        out.annotate(4, "data_size:       " + Hex.u4(dataSize));
        out.annotate(4, "data_off:        " + Hex.u4(dataOff));
    }

    out.writeInt(dataSize);
    out.writeInt(dataOff);
}
 
Example #23
Source File: ClassDefItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.CLASS_DEF_ITEM;
}
 
Example #24
Source File: TypeIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.TYPE_ID_ITEM;
}
 
Example #25
Source File: DexMerger.java    From Box with Apache License 2.0 4 votes vote down vote up
private void transformCode(Dex in, Code code, IndexMap indexMap) {
    contentsOut.codes.size++;
    codeOut.assertFourByteAligned();

    codeOut.writeUnsignedShort(code.getRegistersSize());
    codeOut.writeUnsignedShort(code.getInsSize());
    codeOut.writeUnsignedShort(code.getOutsSize());

    Code.Try[] tries = code.getTries();
    Code.CatchHandler[] catchHandlers = code.getCatchHandlers();
    codeOut.writeUnsignedShort(tries.length);

    int debugInfoOffset = code.getDebugInfoOffset();
    if (debugInfoOffset != 0) {
        codeOut.writeInt(debugInfoOut.getPosition());
        transformDebugInfoItem(in.open(debugInfoOffset), indexMap);
    } else {
        codeOut.writeInt(0);
    }

    short[] instructions = code.getInstructions();
    short[] newInstructions = instructionTransformer.transform(indexMap, instructions);
    codeOut.writeInt(newInstructions.length);
    codeOut.write(newInstructions);

    if (tries.length > 0) {
        if (newInstructions.length % 2 == 1) {
            codeOut.writeShort((short) 0); // padding
        }

        /*
         * We can't write the tries until we've written the catch handlers.
         * Unfortunately they're in the opposite order in the dex file so we
         * need to transform them out-of-order.
         */
        Dex.Section triesSection = dexOut.open(codeOut.getPosition());
        codeOut.skip(tries.length * SizeOf.TRY_ITEM);
        int[] offsets = transformCatchHandlers(indexMap, catchHandlers);
        transformTries(triesSection, tries, offsets);
    }
}
 
Example #26
Source File: DexMerger.java    From Box with Apache License 2.0 4 votes vote down vote up
private void plus(TableOfContents contents, boolean exact) {
    idsDefs += contents.stringIds.size * SizeOf.STRING_ID_ITEM
            + contents.typeIds.size * SizeOf.TYPE_ID_ITEM
            + contents.protoIds.size * SizeOf.PROTO_ID_ITEM
            + contents.fieldIds.size * SizeOf.MEMBER_ID_ITEM
            + contents.methodIds.size * SizeOf.MEMBER_ID_ITEM
            + contents.classDefs.size * SizeOf.CLASS_DEF_ITEM;
    mapList = SizeOf.UINT + (contents.sections.length * SizeOf.MAP_ITEM);
    typeList += fourByteAlign(contents.typeLists.byteCount); // We count each dex's
    // typelists section as realigned on 4 bytes, because each typelist of each dex's
    // typelists section is aligned on 4 bytes. If we didn't, there is a case where each
    // size of both dex's typelists section is a multiple of 2 but not a multiple of 4,
    // and the sum of both sizes is a multiple of 4 but would not be sufficient to write
    // each typelist aligned on 4 bytes.
    stringData += contents.stringDatas.byteCount;
    annotationsDirectory += contents.annotationsDirectories.byteCount;
    annotationsSet += contents.annotationSets.byteCount;
    annotationsSetRefList += contents.annotationSetRefLists.byteCount;

    if (exact) {
        code += contents.codes.byteCount;
        classData += contents.classDatas.byteCount;
        encodedArray += contents.encodedArrays.byteCount;
        annotation += contents.annotations.byteCount;
        debugInfo += contents.debugInfos.byteCount;
    } else {
        // at most 1/4 of the bytes in a code section are uleb/sleb
        code += (int) Math.ceil(contents.codes.byteCount * 1.25);
        // at most 2/3 of the bytes in a class data section are uleb/sleb that may change
        // (assuming the worst case that section contains only methods and no fields)
        classData += (int) Math.ceil(contents.classDatas.byteCount * 1.67);
        // all of the bytes in an encoding arrays section may be uleb/sleb
        encodedArray += contents.encodedArrays.byteCount * 2;
        // all of the bytes in an annotations section may be uleb/sleb
        annotation += (int) Math.ceil(contents.annotations.byteCount * 2);
        // all of the bytes in a debug info section may be uleb/sleb. The additive constant
        // is a fudge factor observed to be required when merging small
        // DEX files (b/68483205).
        debugInfo += contents.debugInfos.byteCount * 2 + 8;
    }
}
 
Example #27
Source File: ProtoIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.PROTO_ID_ITEM;
}
 
Example #28
Source File: MemberIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.MEMBER_ID_ITEM;
}
 
Example #29
Source File: StringIdItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.STRING_ID_ITEM;
}
 
Example #30
Source File: HeaderItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSize() {
    return SizeOf.HEADER_ITEM;
}