com.android.dx.util.ByteArray Java Examples

The following examples show how to use com.android.dx.util.ByteArray. 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: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Does the dumping.
 */
public void dump() {
    byte[] bytes = getBytes();
    ByteArray ba = new ByteArray(bytes);

    /*
     * First, parse the file completely, so we can safely refer to
     * attributes, etc.
     */
    classFile = new DirectClassFile(ba, getFilePath(), getStrictParse());
    classFile.setAttributeFactory(StdAttributeFactory.THE_ONE);
    classFile.getMagic(); // Force parsing to happen.

    // Next, reparse it and observe the process.
    DirectClassFile liveCf =
        new DirectClassFile(ba, getFilePath(), getStrictParse());
    liveCf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    liveCf.setObserver(this);
    liveCf.getMagic(); // Force parsing to happen.
}
 
Example #2
Source File: DirectClassFile.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param bytes {@code non-null;} the bytes of the file
 * @param filePath {@code non-null;} the file path for the class,
 * excluding any base directory specification
 * @param strictParse whether to be strict about parsing; if
 * {@code false}, this avoids doing checks that only exist
 * for purposes of verification (such as magic number matching and
 * path-package consistency checking)
 */
public DirectClassFile(ByteArray bytes, String filePath,
                       boolean strictParse) {
    if (bytes == null) {
        throw new NullPointerException("bytes == null");
    }

    if (filePath == null) {
        throw new NullPointerException("filePath == null");
    }

    this.filePath = filePath;
    this.bytes = bytes;
    this.strictParse = strictParse;
    this.accessFlags = -1;
}
 
Example #3
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: ClassDumper.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Does the dumping.
 */
public void dump() {
    byte[] bytes = getBytes();
    ByteArray ba = new ByteArray(bytes);
    DirectClassFile cf =
        new DirectClassFile(ba, getFilePath(), getStrictParse());

    cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    cf.setObserver(this);
    cf.getMagic(); // Force parsing to happen.

    int at = getAt();
    if (at != bytes.length) {
        parsed(ba, at, bytes.length - at, "<extra data at end of file>");
    }
}
 
Example #5
Source File: StdAttributeFactory.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: StdAttributeFactory.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: StdAttributeFactory.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #9
Source File: StringDataItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@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);
}
 
Example #10
Source File: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #11
Source File: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void startParsingMember(ByteArray bytes, int offset, String name,
        String descriptor) {
    if (descriptor.indexOf('(') < 0) {
        // It's a field, not a method
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    suppressDump = false;

    if (first) {
        first = false;
    } else {
        parsed(bytes, offset, 0, "\n");
    }

    parsed(bytes, offset, 0, "method " + name + " " + descriptor);
    suppressDump = true;
}
 
Example #12
Source File: StdAttributeFactory.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #13
Source File: StdAttributeFactory.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source File: DirectClassFile.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param bytes {@code non-null;} the bytes of the file
 * @param filePath {@code non-null;} the file path for the class,
 * excluding any base directory specification
 * @param strictParse whether to be strict about parsing; if
 * {@code false}, this avoids doing checks that only exist
 * for purposes of verification (such as magic number matching and
 * path-package consistency checking)
 */
public DirectClassFile(ByteArray bytes, String filePath,
                       boolean strictParse) {
    if (bytes == null) {
        throw new NullPointerException("bytes == null");
    }

    if (filePath == null) {
        throw new NullPointerException("filePath == null");
    }

    this.filePath = filePath;
    this.bytes = bytes;
    this.strictParse = strictParse;
    this.accessFlags = -1;
}
 
Example #15
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #16
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #17
Source File: StdAttributeFactory.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #19
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #20
Source File: StringDataItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@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);
}
 
Example #21
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a {@code BootstrapMethods} attribute.
 */
private Attribute bootstrapMethods(DirectClassFile cf, int offset, int length,
        ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }

    ByteArray bytes = cf.getBytes();
    int numMethods = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2,
                        "num_boostrap_methods: " + Hex.u2(numMethods));
    }

    offset += 2;
    length -= 2;

    BootstrapMethodsList methods = parseBootstrapMethods(bytes, cf.getConstantPool(),
                                                         cf.getThisClass(), numMethods,
                                                         offset, length, observer);
    return new AttBootstrapMethods(methods);
}
 
Example #22
Source File: DotDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
private void run() {
    ByteArray ba = new ByteArray(bytes);

    /*
     * First, parse the file completely, so we can safely refer to
     * attributes, etc.
     */
    classFile = new DirectClassFile(ba, filePath, strictParse);
    classFile.setAttributeFactory(StdAttributeFactory.THE_ONE);
    classFile.getMagic(); // Force parsing to happen.

    // Next, reparse it and observe the process.
    DirectClassFile liveCf =
        new DirectClassFile(ba, filePath, strictParse);
    liveCf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    liveCf.setObserver(this);
    liveCf.getMagic(); // Force parsing to happen.
}
 
Example #23
Source File: ClassDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Does the dumping.
 */
public void dump() {
    byte[] bytes = getBytes();
    ByteArray ba = new ByteArray(bytes);
    DirectClassFile cf =
        new DirectClassFile(ba, getFilePath(), getStrictParse());

    cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    cf.setObserver(this);
    cf.getMagic(); // Force parsing to happen.

    int readBytes = getReadBytes();
    if (readBytes != bytes.length) {
        parsed(ba, readBytes, bytes.length - readBytes, "<extra data at end of file>");
    }
}
 
Example #24
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an {@code EnclosingMethod} attribute.
 */
private Attribute enclosingMethod(DirectClassFile cf, int offset,
        int length, ParseObserver observer) {
    if (length != 4) {
        throwBadLength(4);
    }

    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();

    int idx = bytes.getUnsignedShort(offset);
    CstType type = (CstType) pool.get(idx);

    idx = bytes.getUnsignedShort(offset + 2);
    CstNat method = (CstNat) pool.get0Ok(idx);

    Attribute result = new AttEnclosingMethod(type, method);

    if (observer != null) {
        observer.parsed(bytes, offset, 2, "class: " + type);
        observer.parsed(bytes, offset + 2, 2, "method: " +
                        DirectClassFile.stringOrNone(method));
    }

    return result;
}
 
Example #25
Source File: CstString.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance from a {@code String}.
 *
 * @param string {@code non-null;} the UTF-8 value as a string
 */
public CstString(String string) {
    if (string == null) {
        throw new NullPointerException("string == null");
    }

    this.string = string.intern();
    this.bytes = new ByteArray(stringToUtf8Bytes(string));
}
 
Example #26
Source File: CstString.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance from some UTF-8 bytes.
 *
 * @param bytes {@code non-null;} array of the UTF-8 bytes
 */
public CstString(ByteArray bytes) {
    if (bytes == null) {
        throw new NullPointerException("bytes == null");
    }

    this.bytes = bytes;
    this.string = utf8BytesToString(bytes).intern();
}
 
Example #27
Source File: CstString.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance from a {@code String}.
 *
 * @param string {@code non-null;} the UTF-8 value as a string
 */
public CstString(String string) {
    if (string == null) {
        throw new NullPointerException("string == null");
    }

    this.string = string.intern();
    this.bytes = new ByteArray(stringToUtf8Bytes(string));
}
 
Example #28
Source File: ConstantPoolParser.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a utf8 constant.
 *
 * @param at offset to the start of the constant (where the tag byte is)
 * @return {@code non-null;} the parsed value
 */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);

    at += 3; // Skip to the data.

    ByteArray ubytes = bytes.slice(at, at + length);

    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
 
Example #29
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    int count = bytes.getUnsignedShort(offset); // line_number_table_length

    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);
}
 
Example #30
Source File: StdAttributeFactory.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    int count = bytes.getUnsignedShort(offset); // line_number_table_length

    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);
}