Search in sources :

Example 11 with ByteArray

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

the class StdAttributeFactory method sourceFile.

/**
     * Parses a {@code SourceFile} attribute.
     */
private Attribute sourceFile(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 AttSourceFile(cst);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "source: " + cst);
    }
    return result;
}
Also used : Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstString(com.taobao.android.dx.rop.cst.CstString) AttSourceFile(com.taobao.android.dx.cf.attrib.AttSourceFile) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 12 with ByteArray

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

the class StdAttributeFactory method parseLocalVariables.

/**
     * Parse the table part of either a {@code LocalVariableTable}
     * or a {@code LocalVariableTypeTable}.
     *
     * @param bytes {@code non-null;} bytes to parse, which should <i>only</i>
     * contain the table data (no header)
     * @param pool {@code non-null;} constant pool to use
     * @param count {@code >= 0;} the number of entries
     * @param typeTable {@code true} iff this is for a type table
     * @return {@code non-null;} the constructed list
     */
private LocalVariableList parseLocalVariables(ByteArray bytes, ConstantPool pool, ParseObserver observer, int count, boolean typeTable) {
    if (bytes.size() != (count * 10)) {
        // "+ 2" is for the count.
        throwBadLength((count * 10) + 2);
    }
    ByteArray.MyDataInputStream in = bytes.makeDataInputStream();
    LocalVariableList list = new LocalVariableList(count);
    try {
        for (int i = 0; i < count; i++) {
            int startPc = in.readUnsignedShort();
            int length = in.readUnsignedShort();
            int nameIdx = in.readUnsignedShort();
            int typeIdx = in.readUnsignedShort();
            int index = in.readUnsignedShort();
            CstString name = (CstString) pool.get(nameIdx);
            CstString type = (CstString) pool.get(typeIdx);
            CstString descriptor = null;
            CstString signature = null;
            if (typeTable) {
                signature = type;
            } else {
                descriptor = type;
            }
            list.set(i, startPc, length, name, descriptor, signature, index);
            if (observer != null) {
                observer.parsed(bytes, i * 10, 10, Hex.u2(startPc) + ".." + Hex.u2(startPc + length) + " " + Hex.u2(index) + " " + name.toHuman() + " " + type.toHuman());
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException("shouldn't happen", ex);
    }
    list.setImmutable();
    return list;
}
Also used : LocalVariableList(com.taobao.android.dx.cf.code.LocalVariableList) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) IOException(java.io.IOException)

Example 13 with ByteArray

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

the class StdAttributeFactory method localVariableTable.

/**
     * Parses a {@code LocalVariableTable} attribute.
     */
private Attribute localVariableTable(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_table_length: " + Hex.u2(count));
    }
    LocalVariableList list = parseLocalVariables(bytes.slice(offset + 2, offset + length), cf.getConstantPool(), observer, count, false);
    return new AttLocalVariableTable(list);
}
Also used : LocalVariableList(com.taobao.android.dx.cf.code.LocalVariableList) ByteArray(com.taobao.android.dx.util.ByteArray) AttLocalVariableTable(com.taobao.android.dx.cf.attrib.AttLocalVariableTable)

Example 14 with ByteArray

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

the class StringDataItem method writeTo0.

/** {@inheritDoc} */
@Override
public void writeTo0(DexFile file, AnnotatedOutput out) {
    ByteArray bytes = value.getBytes();
    int utf16Size = value.getUtf16Size();
    if (out.annotates()) {
        out.annotate(Leb128.unsignedLeb128Size(utf16Size), "utf16_size: " + Hex.u4(utf16Size));
        out.annotate(bytes.size() + 1, value.toQuoted());
    }
    out.writeUleb128(utf16Size);
    out.write(bytes);
    out.writeByte(0);
}
Also used : ByteArray(com.taobao.android.dx.util.ByteArray)

Example 15 with ByteArray

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

the class MemberListParser method parse.

/**
     * Does the actual parsing.
     */
private void parse() {
    int attributeContext = getAttributeContext();
    int count = getCount();
    // Skip the count.
    int at = offset + 2;
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    if (observer != null) {
        observer.parsed(bytes, offset, 2, humanName() + "s_count: " + Hex.u2(count));
    }
    for (int i = 0; i < count; i++) {
        try {
            int accessFlags = bytes.getUnsignedShort(at);
            int nameIdx = bytes.getUnsignedShort(at + 2);
            int descIdx = bytes.getUnsignedShort(at + 4);
            CstString name = (CstString) pool.get(nameIdx);
            CstString desc = (CstString) pool.get(descIdx);
            if (observer != null) {
                observer.startParsingMember(bytes, at, name.getString(), desc.getString());
                observer.parsed(bytes, at, 0, "\n" + humanName() + "s[" + i + "]:\n");
                observer.changeIndent(1);
                observer.parsed(bytes, at, 2, "access_flags: " + humanAccessFlags(accessFlags));
                observer.parsed(bytes, at + 2, 2, "name: " + name.toHuman());
                observer.parsed(bytes, at + 4, 2, "descriptor: " + desc.toHuman());
            }
            at += 6;
            AttributeListParser parser = new AttributeListParser(cf, attributeContext, at, attributeFactory);
            parser.setObserver(observer);
            at = parser.getEndOffset();
            StdAttributeList attributes = parser.getList();
            attributes.setImmutable();
            CstNat nat = new CstNat(name, desc);
            Member member = set(i, accessFlags, nat, attributes);
            if (observer != null) {
                observer.changeIndent(-1);
                observer.parsed(bytes, at, 0, "end " + humanName() + "s[" + i + "]\n");
                observer.endParsingMember(bytes, at, name.getString(), desc.getString(), member);
            }
        } catch (ParseException ex) {
            ex.addContext("...while parsing " + humanName() + "s[" + i + "]");
            throw ex;
        } catch (RuntimeException ex) {
            ParseException pe = new ParseException(ex);
            pe.addContext("...while parsing " + humanName() + "s[" + i + "]");
            throw pe;
        }
    }
    endOffset = at;
}
Also used : StdAttributeList(com.taobao.android.dx.cf.iface.StdAttributeList) CstNat(com.taobao.android.dx.rop.cst.CstNat) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) ParseException(com.taobao.android.dx.cf.iface.ParseException) Member(com.taobao.android.dx.cf.iface.Member)

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