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: 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 #2
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 #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: 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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #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: 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 #23
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 #24
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 #25
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 #26
Source File: DecodedInstruction.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the B register number, as a code unit. This will throw if the
 * value is out of the range of an unsigned code unit.
 */
public final short getBUnit() {
    int b = getB();

    if ((b & ~0xffff) != 0) {
        throw new DexException("Register B out of range: " + Hex.u8(b));
    }

    return (short) b;
}
 
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: AnnotationParser.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a list of annotation lists.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the list of annotation lists read from the attribute
 * data
 */
private AnnotationsList parseAnnotationsList(
        AnnotationVisibility visibility) throws IOException {
    int count = input.readUnsignedByte();

    if (observer != null) {
        parsed(1, "num_parameters: " + Hex.u1(count));
    }

    AnnotationsList outerList = new AnnotationsList(count);

    for (int i = 0; i < count; i++) {
        if (observer != null) {
            parsed(0, "parameter_annotations[" + i + "]:");
            changeIndent(1);
        }

        Annotations annotations = parseAnnotations(visibility);
        outerList.set(i, annotations);

        if (observer != null) {
            observer.changeIndent(-1);
        }
    }

    outerList.setImmutable();
    return outerList;
}
 
Example #29
Source File: Ropper.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Does the conversion.
 */
private void doit() {
    int[] workSet = Bits.makeBitSet(maxLabel);

    Bits.set(workSet, 0);
    addSetupBlocks();
    setFirstFrame();

    for (;;) {
        int offset = Bits.findFirst(workSet, 0);
        if (offset < 0) {
            break;
        }
        Bits.clear(workSet, offset);
        ByteBlock block = blocks.labelToBlock(offset);
        Frame frame = startFrames[offset];
        try {
            processBlock(block, frame, workSet);
        } catch (SimException ex) {
            ex.addContext("...while working on block " + Hex.u2(offset));
            throw ex;
        }
    }

    addReturnBlock();
    addSynchExceptionHandlerBlock();
    addExceptionSetupBlocks();

    if (hasSubroutines) {
        // Subroutines are very rare, so skip this step if it's n/a
        inlineSubroutines();
    }
}
 
Example #30
Source File: InsnFormat.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to return a literal bits comment string.
 *
 * @param value the value
 * @param width the width of the constant, in bits (used for displaying
 * the uninterpreted bits; one of: {@code 4 8 16 32 64}
 * @return {@code non-null;} the comment
 */
protected static String literalBitsComment(CstLiteralBits value,
        int width) {
    StringBuilder sb = new StringBuilder(20);

    sb.append("#");

    long bits;

    if (value instanceof CstLiteral64) {
        bits = ((CstLiteral64) value).getLongBits();
    } else {
        bits = value.getIntBits();
    }

    switch (width) {
        case 4:  sb.append(Hex.uNibble((int) bits)); break;
        case 8:  sb.append(Hex.u1((int) bits));      break;
        case 16: sb.append(Hex.u2((int) bits));      break;
        case 32: sb.append(Hex.u4((int) bits));      break;
        case 64: sb.append(Hex.u8(bits));            break;
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    return sb.toString();
}