com.android.dex.Leb128 Java Examples

The following examples show how to use com.android.dex.Leb128. 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: 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 #2
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 #3
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 #4
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 #5
Source File: EncodedField.java    From buck 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 #6
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 #7
Source File: EncodedField.java    From J2ME-Loader 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: 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: ByteArrayAnnotatedOutput.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
   @Override
public int writeSleb128(int value) {
       if (stretchy) {
           ensureCapacity(cursor + 5); // pessimistic
       }
       int cursorBefore = cursor;
       Leb128.writeSignedLeb128(this, value);
       return (cursor - cursorBefore);
   }
 
Example #10
Source File: ByteArrayAnnotatedOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSleb128(int value) {
    if (stretchy) {
        ensureCapacity(cursor + 5); // pessimistic
    }
    int cursorBefore = cursor;
    Leb128.writeSignedLeb128(this, value);
    return (cursor - cursorBefore);
}
 
Example #11
Source File: ByteArrayAnnotatedOutput.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public int writeSleb128(int value) {
    if (stretchy) {
        ensureCapacity(cursor + 5); // pessimistic
    }
    int cursorBefore = cursor;
    Leb128.writeSignedLeb128(this, value);
    return (cursor - cursorBefore);
}
 
Example #12
Source File: EncodedMethod.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int diff = methodIdx - lastIndex;
    int accessFlags = getAccessFlags();
    int codeOff = OffsettedItem.getAbsoluteOffsetOr0(code);
    boolean hasCode = (codeOff != 0);
    boolean shouldHaveCode = (accessFlags &
            (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) == 0;

    /*
     * Verify that code appears if and only if a method is
     * declared to have it.
     */
    if (hasCode != shouldHaveCode) {
        throw new UnsupportedOperationException(
                "code vs. access_flags mismatch");
    }

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        method.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    method_idx:   " + Hex.u4(methodIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.methodString(accessFlags));
        out.annotate(Leb128.unsignedLeb128Size(codeOff),
                "    code_off:     " + Hex.u4(codeOff));
    }

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

    return methodIdx;
}
 
Example #13
Source File: StringDataItem.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the write size for a given value.
 *
 * @param value {@code non-null;} the string value
 * @return {@code >= 2}; the write size, in bytes
 */
private static int writeSize(CstString value) {
    int utf16Size = value.getUtf16Size();

    // The +1 is for the '\0' termination byte.
    return Leb128.unsignedLeb128Size(utf16Size)
        + value.getUtf8Size() + 1;
}
 
Example #14
Source File: ByteArrayAnnotatedOutput.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
   @Override
public int writeUleb128(int value) {
       if (stretchy) {
           ensureCapacity(cursor + 5); // pessimistic
       }
       int cursorBefore = cursor;
       Leb128.writeUnsignedLeb128(this, value);
       return (cursor - cursorBefore);
   }
 
Example #15
Source File: IndexMap.java    From Box with Apache License 2.0 5 votes vote down vote up
private void transformAnnotation(EncodedValueReader reader) {
    int fieldCount = reader.readAnnotation();
    Leb128.writeUnsignedLeb128(out, adjustType(reader.getAnnotationType()));
    Leb128.writeUnsignedLeb128(out, fieldCount);
    for (int i = 0; i < fieldCount; i++) {
        Leb128.writeUnsignedLeb128(out, adjustString(reader.readAnnotationName()));
        transform(reader);
    }
}
 
Example #16
Source File: ByteArrayAnnotatedOutput.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public int writeUleb128(int value) {
    if (stretchy) {
        ensureCapacity(cursor + 5); // pessimistic
    }
    int cursorBefore = cursor;
    Leb128.writeUnsignedLeb128(this, value);
    return (cursor - cursorBefore);
}
 
Example #17
Source File: EncodedMethod.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int diff = methodIdx - lastIndex;
    int accessFlags = getAccessFlags();
    int codeOff = OffsettedItem.getAbsoluteOffsetOr0(code);
    boolean hasCode = (codeOff != 0);
    boolean shouldHaveCode = (accessFlags &
            (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) == 0;

    /*
     * Verify that code appears if and only if a method is
     * declared to have it.
     */
    if (hasCode != shouldHaveCode) {
        throw new UnsupportedOperationException(
                "code vs. access_flags mismatch");
    }

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        method.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    method_idx:   " + Hex.u4(methodIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.methodString(accessFlags));
        out.annotate(Leb128.unsignedLeb128Size(codeOff),
                "    code_off:     " + Hex.u4(codeOff));
    }

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

    return methodIdx;
}
 
Example #18
Source File: StringDataItem.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the write size for a given value.
 *
 * @param value {@code non-null;} the string value
 * @return {@code >= 2}; the write size, in bytes
 */
private static int writeSize(CstString value) {
    int utf16Size = value.getUtf16Size();

    // The +1 is for the '\0' termination byte.
    return Leb128.unsignedLeb128Size(utf16Size)
        + value.getUtf8Size() + 1;
}
 
Example #19
Source File: IndexMap.java    From buck with Apache License 2.0 5 votes vote down vote up
private void transformAnnotation(EncodedValueReader reader) {
    int fieldCount = reader.readAnnotation();
    Leb128.writeUnsignedLeb128(out, adjustType(reader.getAnnotationType()));
    Leb128.writeUnsignedLeb128(out, fieldCount);
    for (int i = 0; i < fieldCount; i++) {
        Leb128.writeUnsignedLeb128(out, adjustString(reader.readAnnotationName()));
        transform(reader);
    }
}
 
Example #20
Source File: IndexMap.java    From buck with Apache License 2.0 5 votes vote down vote up
private void transformArray(EncodedValueReader reader) {
    int size = reader.readArray();
    Leb128.writeUnsignedLeb128(out, size);
    for (int i = 0; i < size; i++) {
        transform(reader);
    }
}
 
Example #21
Source File: ByteArrayAnnotatedOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeUleb128(int value) {
    if (stretchy) {
        ensureCapacity(cursor + 5); // pessimistic
    }
    int cursorBefore = cursor;
    Leb128.writeUnsignedLeb128(this, value);
    return (cursor - cursorBefore);
}
 
Example #22
Source File: IndexMap.java    From Box with Apache License 2.0 5 votes vote down vote up
private void transformArray(EncodedValueReader reader) {
    int size = reader.readArray();
    Leb128.writeUnsignedLeb128(out, size);
    for (int i = 0; i < size; i++) {
        transform(reader);
    }
}
 
Example #23
Source File: StringDataItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the write size for a given value.
 *
 * @param value {@code non-null;} the string value
 * @return {@code >= 2}; the write size, in bytes
 */
private static int writeSize(CstString value) {
    int utf16Size = value.getUtf16Size();

    // The +1 is for the '\0' termination byte.
    return Leb128.unsignedLeb128Size(utf16Size)
        + value.getUtf8Size() + 1;
}
 
Example #24
Source File: EncodedMethod.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int diff = methodIdx - lastIndex;
    int accessFlags = getAccessFlags();
    int codeOff = OffsettedItem.getAbsoluteOffsetOr0(code);
    boolean hasCode = (codeOff != 0);
    boolean shouldHaveCode = (accessFlags &
            (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) == 0;

    /*
     * Verify that code appears if and only if a method is
     * declared to have it.
     */
    if (hasCode != shouldHaveCode) {
        throw new UnsupportedOperationException(
                "code vs. access_flags mismatch");
    }

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        method.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    method_idx:   " + Hex.u4(methodIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.methodString(accessFlags));
        out.annotate(Leb128.unsignedLeb128Size(codeOff),
                "    code_off:     " + Hex.u4(codeOff));
    }

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

    return methodIdx;
}
 
Example #25
Source File: Leb128UtilsTest.java    From buck with Apache License 2.0 5 votes vote down vote up
public void testDecodeUnsignedLeb() throws IOException {
    assertEquals(0, Leb128.readUnsignedLeb128(new ByteArrayByteInput((byte) 0)));
    assertEquals(1, Leb128.readUnsignedLeb128(new ByteArrayByteInput((byte) 1)));
    assertEquals(127, Leb128.readUnsignedLeb128(new ByteArrayByteInput((byte) 0x7f)));
    assertEquals(16256, Leb128.readUnsignedLeb128(
            new ByteArrayByteInput((byte) 0x80, (byte) 0x7f)));
}
 
Example #26
Source File: Leb128UtilsTest.java    From buck with Apache License 2.0 5 votes vote down vote up
public void testDecodeSignedLeb() throws IOException {
    assertEquals(0, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 0)));
    assertEquals(1, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 1)));
    assertEquals(-1, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 0x7f)));
    assertEquals(0x3c, Leb128.readSignedLeb128(new ByteArrayByteInput((byte) 0x3c)));
    assertEquals(-128, Leb128.readSignedLeb128(
            new ByteArrayByteInput((byte) 0x80, (byte) 0x7f)));
}
 
Example #27
Source File: StaticValuesParser.java    From Box with Apache License 2.0 5 votes vote down vote up
public int processFields(List<FieldNode> fields) throws DecodeException {
	int count = Leb128.readUnsignedLeb128(in);
	for (int i = 0; i < count; i++) {
		Object value = parseValue();
		if (i < fields.size()) {
			fields.get(i).addAttr(FieldInitAttr.constValue(value));
		}
	}
	return count;
}
 
Example #28
Source File: ByteArrayAnnotatedOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeSleb128(int value) {
    if (stretchy) {
        ensureCapacity(cursor + 5); // pessimistic
    }
    int cursorBefore = cursor;
    Leb128.writeSignedLeb128(this, value);
    return (cursor - cursorBefore);
}
 
Example #29
Source File: ByteArrayAnnotatedOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int writeUleb128(int value) {
    if (stretchy) {
        ensureCapacity(cursor + 5); // pessimistic
    }
    int cursorBefore = cursor;
    Leb128.writeUnsignedLeb128(this, value);
    return (cursor - cursorBefore);
}
 
Example #30
Source File: IndexMap.java    From Box with Apache License 2.0 5 votes vote down vote up
private void transformArray(EncodedValueReader reader) {
    int size = reader.readArray();
    Leb128.writeUnsignedLeb128(out, size);
    for (int i = 0; i < size; i++) {
        transform(reader);
    }
}