Search in sources :

Example 16 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class StdAttributeFactory method localVariableTypeTable.

/**
     * Parses a {@code LocalVariableTypeTable} attribute.
     */
private Attribute localVariableTypeTable(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    int count = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "local_variable_type_table_length: " + Hex.u2(count));
    }
    LocalVariableList list = parseLocalVariables(bytes.slice(offset + 2, offset + length), cf.getConstantPool(), observer, count, true);
    return new AttLocalVariableTypeTable(list);
}
Also used : LocalVariableList(com.taobao.android.dx.cf.code.LocalVariableList) AttLocalVariableTypeTable(com.taobao.android.dx.cf.attrib.AttLocalVariableTypeTable) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 17 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class StdAttributeFactory method code.

/**
     * Parses a {@code Code} attribute.
     */
private Attribute code(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 12) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    // u2 max_stack
    int maxStack = bytes.getUnsignedShort(offset);
    // u2 max_locals
    int maxLocals = bytes.getUnsignedShort(offset + 2);
    // u4 code_length
    int codeLength = bytes.getInt(offset + 4);
    int origOffset = offset;
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "max_stack: " + Hex.u2(maxStack));
        observer.parsed(bytes, offset + 2, 2, "max_locals: " + Hex.u2(maxLocals));
        observer.parsed(bytes, offset + 4, 4, "code_length: " + Hex.u4(codeLength));
    }
    offset += 8;
    length -= 8;
    if (length < (codeLength + 4)) {
        return throwTruncated();
    }
    int codeOffset = offset;
    offset += codeLength;
    length -= codeLength;
    BytecodeArray code = new BytecodeArray(bytes.slice(codeOffset, codeOffset + codeLength), pool);
    if (observer != null) {
        code.forEach(new CodeObserver(code.getBytes(), observer));
    }
    // u2 exception_table_length
    int exceptionTableLength = bytes.getUnsignedShort(offset);
    ByteCatchList catches = (exceptionTableLength == 0) ? ByteCatchList.EMPTY : new ByteCatchList(exceptionTableLength);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "exception_table_length: " + Hex.u2(exceptionTableLength));
    }
    offset += 2;
    length -= 2;
    if (length < (exceptionTableLength * 8 + 2)) {
        return throwTruncated();
    }
    for (int i = 0; i < exceptionTableLength; i++) {
        if (observer != null) {
            observer.changeIndent(1);
        }
        int startPc = bytes.getUnsignedShort(offset);
        int endPc = bytes.getUnsignedShort(offset + 2);
        int handlerPc = bytes.getUnsignedShort(offset + 4);
        int catchTypeIdx = bytes.getUnsignedShort(offset + 6);
        CstType catchType = (CstType) pool.get0Ok(catchTypeIdx);
        catches.set(i, startPc, endPc, handlerPc, catchType);
        if (observer != null) {
            observer.parsed(bytes, offset, 8, Hex.u2(startPc) + ".." + Hex.u2(endPc) + " -> " + Hex.u2(handlerPc) + " " + ((catchType == null) ? "<any>" : catchType.toHuman()));
        }
        offset += 8;
        length -= 8;
        if (observer != null) {
            observer.changeIndent(-1);
        }
    }
    catches.setImmutable();
    AttributeListParser parser = new AttributeListParser(cf, CTX_CODE, offset, this);
    parser.setObserver(observer);
    StdAttributeList attributes = parser.getList();
    attributes.setImmutable();
    int attributeByteCount = parser.getEndOffset() - offset;
    if (attributeByteCount != length) {
        return throwBadLength(attributeByteCount + (offset - origOffset));
    }
    return new AttCode(maxStack, maxLocals, code, catches, attributes);
}
Also used : BytecodeArray(com.taobao.android.dx.cf.code.BytecodeArray) StdAttributeList(com.taobao.android.dx.cf.iface.StdAttributeList) ByteCatchList(com.taobao.android.dx.cf.code.ByteCatchList) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstType(com.taobao.android.dx.rop.cst.CstType) AttCode(com.taobao.android.dx.cf.attrib.AttCode) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 18 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class StdAttributeFactory method lineNumberTable.

/**
     * Parses a {@code LineNumberTable} attribute.
     */
private Attribute lineNumberTable(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    // line_number_table_length
    int count = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "line_number_table_length: " + Hex.u2(count));
    }
    offset += 2;
    length -= 2;
    if (length != (count * 4)) {
        throwBadLength((count * 4) + 2);
    }
    LineNumberList list = new LineNumberList(count);
    for (int i = 0; i < count; i++) {
        int startPc = bytes.getUnsignedShort(offset);
        int lineNumber = bytes.getUnsignedShort(offset + 2);
        list.set(i, startPc, lineNumber);
        if (observer != null) {
            observer.parsed(bytes, offset, 4, Hex.u2(startPc) + " " + lineNumber);
        }
        offset += 4;
    }
    list.setImmutable();
    return new AttLineNumberTable(list);
}
Also used : LineNumberList(com.taobao.android.dx.cf.code.LineNumberList) ByteArray(com.taobao.android.dx.util.ByteArray) AttLineNumberTable(com.taobao.android.dx.cf.attrib.AttLineNumberTable)

Example 19 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class StdAttributeFactory method signature.

/**
     * Parses a {@code Signature} attribute.
     */
private Attribute signature(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 2) {
        throwBadLength(2);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    CstString cst = (CstString) pool.get(idx);
    Attribute result = new AttSignature(cst);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "signature: " + cst);
    }
    return result;
}
Also used : Attribute(com.taobao.android.dx.cf.iface.Attribute) AttSignature(com.taobao.android.dx.cf.attrib.AttSignature) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 20 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class AttributeFactory method parse0.

/**
     * Parses attribute content. The base class implements this by constructing
     * an instance of {@link RawAttribute}. Subclasses are expected to
     * override this to do something better in most cases.
     *
     * @param cf {@code non-null;} class file to parse from
     * @param context context to parse in; one of the {@code CTX_*}
     * constants
     * @param name {@code non-null;} the attribute name
     * @param offset offset into {@code bytes} to start parsing at; this
     * is the offset to the start of attribute data, not to the header
     * @param length the length of the attribute data
     * @param observer {@code null-ok;} parse observer to report to, if any
     * @return {@code non-null;} an appropriately-constructed {@link Attribute}
     */
protected Attribute parse0(DirectClassFile cf, int context, String name, int offset, int length, ParseObserver observer) {
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    Attribute result = new RawAttribute(name, bytes, offset, length, pool);
    if (observer != null) {
        observer.parsed(bytes, offset, length, "attribute data");
    }
    return result;
}
Also used : RawAttribute(com.taobao.android.dx.cf.attrib.RawAttribute) Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) RawAttribute(com.taobao.android.dx.cf.attrib.RawAttribute) ByteArray(com.taobao.android.dx.util.ByteArray)

Aggregations

ByteArray (com.taobao.android.dx.util.ByteArray)23 ConstantPool (com.taobao.android.dx.rop.cst.ConstantPool)9 Attribute (com.taobao.android.dx.cf.iface.Attribute)7 CstString (com.taobao.android.dx.rop.cst.CstString)6 DirectClassFile (com.taobao.android.dx.cf.direct.DirectClassFile)4 ParseException (com.taobao.android.dx.cf.iface.ParseException)4 CstType (com.taobao.android.dx.rop.cst.CstType)4 BytecodeArray (com.taobao.android.dx.cf.code.BytecodeArray)3 LocalVariableList (com.taobao.android.dx.cf.code.LocalVariableList)3 ByteCatchList (com.taobao.android.dx.cf.code.ByteCatchList)2 StdAttributeList (com.taobao.android.dx.cf.iface.StdAttributeList)2 CstNat (com.taobao.android.dx.rop.cst.CstNat)2 IntList (com.taobao.android.dx.util.IntList)2 AttCode (com.taobao.android.dx.cf.attrib.AttCode)1 AttConstantValue (com.taobao.android.dx.cf.attrib.AttConstantValue)1 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)1 AttExceptions (com.taobao.android.dx.cf.attrib.AttExceptions)1 AttInnerClasses (com.taobao.android.dx.cf.attrib.AttInnerClasses)1 AttLineNumberTable (com.taobao.android.dx.cf.attrib.AttLineNumberTable)1 AttLocalVariableTable (com.taobao.android.dx.cf.attrib.AttLocalVariableTable)1