com.android.dx.util.Hex Java Examples

The following examples show how to use com.android.dx.util.Hex. 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: CodeObserver.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitSwitch(int opcode, int offset, int length,
        SwitchList cases, int padding) {
    int sz = cases.size();
    StringBuilder sb = new StringBuilder(sz * 20 + 100);

    sb.append(header(offset));
    if (padding != 0) {
        sb.append(" // padding: " + Hex.u4(padding));
    }
    sb.append('\n');

    for (int i = 0; i < sz; i++) {
        sb.append("  ");
        sb.append(Hex.s4(cases.getValue(i)));
        sb.append(": ");
        sb.append(Hex.u2(cases.getTarget(i)));
        sb.append('\n');
    }

    sb.append("  default: ");
    sb.append(Hex.u2(cases.getDefaultTarget()));

    observer.parsed(bytes, offset, length, sb.toString());
}
 
Example #2
Source File: LocalsArraySet.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public String toHuman() {
    StringBuilder sb = new StringBuilder();

    sb.append("(locals array set; primary)\n");

    sb.append(getPrimary().toHuman());
    sb.append('\n');

    int sz = secondaries.size();
    for (int label = 0; label < sz; label++) {
        LocalsArray la = secondaries.get(label);

        if (la != null) {
            sb.append("(locals array set: primary for caller "
                    + Hex.u2(label) + ")\n");

            sb.append(la.getPrimary().toHuman());
            sb.append('\n');
        }
    }

    return sb.toString();
}
 
Example #3
Source File: ProtoIdsSection.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the portion of the file header that refers to this instance.
 *
 * @param out {@code non-null;} where to write
 */
public void writeHeaderPart(AnnotatedOutput out) {
    throwIfNotPrepared();

    int sz = protoIds.size();
    int offset = (sz == 0) ? 0 : getFileOffset();

    if (sz > 65536) {
        throw new UnsupportedOperationException("too many proto ids");
    }

    if (out.annotates()) {
        out.annotate(4, "proto_ids_size:  " + Hex.u4(sz));
        out.annotate(4, "proto_ids_off:   " + Hex.u4(offset));
    }

    out.writeInt(sz);
    out.writeInt(offset);
}
 
Example #4
Source File: EncodedMethod.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public String toString() {
    StringBuffer sb = new StringBuffer(100);

    sb.append(getClass().getName());
    sb.append('{');
    sb.append(Hex.u2(getAccessFlags()));
    sb.append(' ');
    sb.append(method);

    if (code != null) {
        sb.append(' ');
        sb.append(code);
    }

    sb.append('}');

    return sb.toString();
}
 
Example #5
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 #6
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 #7
Source File: TypeIdsSection.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the portion of the file header that refers to this instance.
 *
 * @param out {@code non-null;} where to write
 */
public void writeHeaderPart(AnnotatedOutput out) {
    throwIfNotPrepared();

    int sz = typeIds.size();
    int offset = (sz == 0) ? 0 : getFileOffset();

    if (sz > DexFormat.MAX_TYPE_IDX + 1) {
        throw new DexIndexOverflowException(
                String.format("Too many type identifiers to fit in one dex file: %1$d; max is %2$d.%n"
                                + "You may try using multi-dex. If multi-dex is enabled then the list of "
                                + "classes for the main dex list is too large.",
                        items().size(), DexFormat.MAX_MEMBER_IDX + 1));
    }

    if (out.annotates()) {
        out.annotate(4, "type_ids_size:   " + Hex.u4(sz));
        out.annotate(4, "type_ids_off:    " + Hex.u4(offset));
    }

    out.writeInt(sz);
    out.writeInt(offset);
}
 
Example #8
Source File: StringDataItem.java    From J2ME-Loader 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 #9
Source File: ArrayData.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected String listingString0(boolean noteIndices) {
    int baseAddress = user.getAddress();
    StringBuilder sb = new StringBuilder(100);
    int sz = values.size();

    sb.append("fill-array-data-payload // for fill-array-data @ ");
    sb.append(Hex.u2(baseAddress));

    for (int i = 0; i < sz; i++) {
        sb.append("\n  ");
        sb.append(i);
        sb.append(": ");
        sb.append(values.get(i).toHuman());
    }

    return sb.toString();
}
 
Example #10
Source File: ArrayData.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected String listingString0(boolean noteIndices) {
    int baseAddress = user.getAddress();
    StringBuffer sb = new StringBuffer(100);
    int sz = values.size();

    sb.append("fill-array-data-payload // for fill-array-data @ ");
    sb.append(Hex.u2(baseAddress));

    for (int i = 0; i < sz; i++) {
        sb.append("\n  ");
        sb.append(i);
        sb.append(": ");
        sb.append(values.get(i).toHuman());
    }

    return sb.toString();
}
 
Example #11
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 #12
Source File: ValueEncoder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Writes out the encoded form of the given array, that is, as
 * an {@code encoded_array} and not including a
 * {@code value_type} prefix. If the output stream keeps
 * (debugging) annotations and {@code topLevel} is
 * {@code true}, then this method will write (debugging)
 * annotations.
 *
 * @param array {@code non-null;} array instance to write
 * @param topLevel {@code true} iff the given annotation is the
 * top-level annotation or {@code false} if it is a sub-annotation
 * of some other annotation
 */
public void writeArray(CstArray array, boolean topLevel) {
    boolean annotates = topLevel && out.annotates();
    CstArray.List list = ((CstArray) array).getList();
    int size = list.size();

    if (annotates) {
        out.annotate("  size: " + Hex.u4(size));
    }

    out.writeUleb128(size);

    for (int i = 0; i < size; i++) {
        Constant cst = list.get(i);
        if (annotates) {
            out.annotate("  [" + Integer.toHexString(i) + "] " +
                    constantToHuman(cst));
        }
        writeConstant(cst);
    }

    if (annotates) {
        out.endAnnotation();
    }
}
 
Example #13
Source File: TypeIdsSection.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the portion of the file header that refers to this instance.
 *
 * @param out {@code non-null;} where to write
 */
public void writeHeaderPart(AnnotatedOutput out) {
    throwIfNotPrepared();

    int sz = typeIds.size();
    int offset = (sz == 0) ? 0 : getFileOffset();

    if (sz > DexFormat.MAX_TYPE_IDX + 1) {
        throw new DexIndexOverflowException("Too many type references: " + sz +
                "; max is " + (DexFormat.MAX_TYPE_IDX + 1) + ".\n" +
                Main.getTooManyIdsErrorMessage());
    }

    if (out.annotates()) {
        out.annotate(4, "type_ids_size:   " + Hex.u4(sz));
        out.annotate(4, "type_ids_off:    " + Hex.u4(offset));
    }

    out.writeInt(sz);
    out.writeInt(offset);
}
 
Example #14
Source File: Ropper.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #addOrReplaceBlock} which recursively removes
 * the given block and all blocks that are (direct and indirect)
 * successors of it whose labels indicate that they are not in the
 * normally-translated range.
 *
 * @param idx {@code non-null;} block to remove (etc.)
 */
private void removeBlockAndSpecialSuccessors(int idx) {
    int minLabel = getMinimumUnreservedLabel();
    BasicBlock block = result.get(idx);
    IntList successors = block.getSuccessors();
    int sz = successors.size();

    result.remove(idx);
    resultSubroutines.remove(idx);

    for (int i = 0; i < sz; i++) {
        int label = successors.get(i);
        if (label >= minLabel) {
            idx = labelToResultIndex(label);
            if (idx < 0) {
                throw new RuntimeException("Invalid label "
                        + Hex.u2(label));
            }
            removeBlockAndSpecialSuccessors(idx);
        }
    }
}
 
Example #15
Source File: AnnotationSetItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    int size = items.length;

    if (annotates) {
        out.annotate(0, offsetString() + " annotation set");
        out.annotate(4, "  size: " + Hex.u4(size));
    }

    out.writeInt(size);

    for (int i = 0; i < size; i++) {
        AnnotationItem item = items[i];
        int offset = item.getAbsoluteOffset();

        if (annotates) {
            out.annotate(4, "  entries[" + Integer.toHexString(i) + "]: " +
                    Hex.u4(offset));
            items[i].annotateTo(out, "    ");
        }

        out.writeInt(offset);
    }
}
 
Example #16
Source File: TypeListItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    TypeIdsSection typeIds = file.getTypeIds();
    int sz = list.size();

    if (out.annotates()) {
        out.annotate(0, offsetString() + " type_list");
        out.annotate(HEADER_SIZE, "  size: " + Hex.u4(sz));
        for (int i = 0; i < sz; i++) {
            Type one = list.getType(i);
            int idx = typeIds.indexOf(one);
            out.annotate(ELEMENT_SIZE,
                         "  " + Hex.u2(idx) + " // " + one.toHuman());
        }
    }

    out.writeInt(sz);

    for (int i = 0; i < sz; i++) {
        out.writeShort(typeIds.indexOf(list.getType(i)));
    }
}
 
Example #17
Source File: CodeObserver.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #visitConstant} where the constant is an
 * {@code int}.
 *
 * @param opcode the opcode
 * @param offset offset to the instruction
 * @param length instruction length
 * @param value constant value
 */
private void visitLiteralInt(int opcode, int offset, int length,
        int value) {
    String commentOrSpace = (length == 1) ? " // " : " ";
    String valueStr;

    opcode = bytes.getUnsignedByte(offset); // Compare with orig op below.
    if ((length == 1) || (opcode == ByteOps.BIPUSH)) {
        valueStr = "#" + Hex.s1(value);
    } else if (opcode == ByteOps.SIPUSH) {
        valueStr = "#" + Hex.s2(value);
    } else {
        valueStr = "#" + Hex.s4(value);
    }

    observer.parsed(bytes, offset, length,
                    header(offset) + commentOrSpace + valueStr);
}
 
Example #18
Source File: AnnotationSetItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    int size = items.length;

    if (annotates) {
        out.annotate(0, offsetString() + " annotation set");
        out.annotate(4, "  size: " + Hex.u4(size));
    }

    out.writeInt(size);

    for (int i = 0; i < size; i++) {
        AnnotationItem item = items[i];
        int offset = item.getAbsoluteOffset();

        if (annotates) {
            out.annotate(4, "  entries[" + Integer.toHexString(i) + "]: " +
                    Hex.u4(offset));
            items[i].annotateTo(out, "    ");
        }

        out.writeInt(offset);
    }
}
 
Example #19
Source File: UniformListItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void writeTo0(DexFile file, AnnotatedOutput out) {
    int size = items.size();

    if (out.annotates()) {
        out.annotate(0, offsetString() + " " + typeName());
        out.annotate(4, "  size: " + Hex.u4(size));
    }

    out.writeInt(size);

    for (OffsettedItem i : items) {
        i.writeTo(file, out);
    }
}
 
Example #20
Source File: SourcePosition.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder(50);

    if (sourceFile != null) {
        sb.append(sourceFile.toHuman());
        sb.append(":");
    }

    if (line >= 0) {
        sb.append(line);
    }

    sb.append('@');

    if (address < 0) {
        sb.append("????");
    } else {
        sb.append(Hex.u2(address));
    }

    return sb.toString();
}
 
Example #21
Source File: EncodedField.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int fieldIdx = file.getFieldIds().indexOf(field);
    int diff = fieldIdx - lastIndex;
    int accessFlags = getAccessFlags();

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        field.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    field_idx:    " + Hex.u4(fieldIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.fieldString(accessFlags));
    }

    out.writeUleb128(diff);
    out.writeUleb128(accessFlags);

    return fieldIdx;
}
 
Example #22
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 #23
Source File: LocalsArraySet.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
public String toHuman() {
    StringBuilder sb = new StringBuilder();

    sb.append("(locals array set; primary)\n");

    sb.append(getPrimary().toHuman());
    sb.append('\n');

    int sz = secondaries.size();
    for (int label = 0; label < sz; label++) {
        LocalsArray la = secondaries.get(label);

        if (la != null) {
            sb.append("(locals array set: primary for caller "
                    + Hex.u2(label) + ")\n");

            sb.append(la.getPrimary().toHuman());
            sb.append('\n');
        }
    }

    return sb.toString();
}
 
Example #24
Source File: CodeObserver.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitLocal(int opcode, int offset, int length,
        int idx, Type type, int value) {
    String idxStr = (length <= 3) ? Hex.u1(idx) : Hex.u2(idx);
    boolean argComment = (length == 1);
    String valueStr = "";

    if (opcode == ByteOps.IINC) {
        valueStr = ", #" +
            ((length <= 3) ? Hex.s1(value) : Hex.s2(value));
    }

    String catStr = "";
    if (type.isCategory2()) {
        catStr = (argComment ? "," : " //") + " category-2";
    }

    observer.parsed(bytes, offset, length,
                    header(offset) + (argComment ? " // " : " ") +
                    idxStr + valueStr + catStr);
}
 
Example #25
Source File: CodeObserver.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #visitConstant} where the constant is an
 * {@code int}.
 *
 * @param opcode the opcode
 * @param offset offset to the instruction
 * @param length instruction length
 * @param value constant value
 */
private void visitLiteralInt(int opcode, int offset, int length,
        int value) {
    String commentOrSpace = (length == 1) ? " // " : " ";
    String valueStr;

    opcode = bytes.getUnsignedByte(offset); // Compare with orig op below.
    if ((length == 1) || (opcode == ByteOps.BIPUSH)) {
        valueStr = "#" + Hex.s1(value);
    } else if (opcode == ByteOps.SIPUSH) {
        valueStr = "#" + Hex.s2(value);
    } else {
        valueStr = "#" + Hex.s4(value);
    }

    observer.parsed(bytes, offset, length,
                    header(offset) + commentOrSpace + valueStr);
}
 
Example #26
Source File: TypeIdItem.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    CstType type = getDefiningClass();
    CstString descriptor = type.getDescriptor();
    int idx = file.getStringIds().indexOf(descriptor);

    if (out.annotates()) {
        out.annotate(0, indexString() + ' ' + descriptor.toHuman());
        out.annotate(4, "  descriptor_idx: " + Hex.u4(idx));
    }

    out.writeInt(idx);
}
 
Example #27
Source File: CatchHandlerList.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Get the human form of this instance, prefixed on each line
 * with the string.
 *
 * @param prefix {@code non-null;} the prefix for every line
 * @param header {@code non-null;} the header for the first line (after the
 * first prefix)
 * @return {@code non-null;} the human form
 */
public String toHuman(String prefix, String header) {
    StringBuilder sb = new StringBuilder(100);
    int size = size();

    sb.append(prefix);
    sb.append(header);
    sb.append("catch ");

    for (int i = 0; i < size; i++) {
        Entry entry = get(i);

        if (i != 0) {
            sb.append(",\n");
            sb.append(prefix);
            sb.append("  ");
        }

        if ((i == (size - 1)) && catchesAll()) {
            sb.append("<any>");
        } else {
            sb.append(entry.getExceptionType().toHuman());
        }

        sb.append(" -> ");
        sb.append(Hex.u2or4(entry.getHandler()));
    }

    return sb.toString();
}
 
Example #28
Source File: DecodedInstruction.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the literal value, masked to be an int in size. This will
 * throw if the value is out of the range of a signed int.
 */
public final int getLiteralInt() {
    if (literal != (int) literal) {
        throw new DexException("Literal out of range: " + Hex.u8(literal));
    }

    return (int) literal;
}
 
Example #29
Source File: CatchHandlerList.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Get the human form of this instance, prefixed on each line
 * with the string.
 *
 * @param prefix {@code non-null;} the prefix for every line
 * @param header {@code non-null;} the header for the first line (after the
 * first prefix)
 * @return {@code non-null;} the human form
 */
public String toHuman(String prefix, String header) {
    StringBuilder sb = new StringBuilder(100);
    int size = size();

    sb.append(prefix);
    sb.append(header);
    sb.append("catch ");

    for (int i = 0; i < size; i++) {
        Entry entry = get(i);

        if (i != 0) {
            sb.append(",\n");
            sb.append(prefix);
            sb.append("  ");
        }

        if ((i == (size - 1)) && catchesAll()) {
            sb.append("<any>");
        } else {
            sb.append(entry.getExceptionType().toHuman());
        }

        sb.append(" -> ");
        sb.append(Hex.u2or4(entry.getHandler()));
    }

    return sb.toString();
}
 
Example #30
Source File: ByteBlockList.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the block with the given label.
 *
 * @param label the label to look for
 * @return {@code non-null;} the block with the given label
 */
public ByteBlock labelToBlock(int label) {
    int idx = indexOfLabel(label);

    if (idx < 0) {
        throw new IllegalArgumentException("no such label: "
                + Hex.u2(label));
    }

    return get(idx);
}