LEB128 ("Little-Endian Base 128") is a variable-length encoding for arbitrary signed or unsigned integer quantities. The format was borrowed from the DWARF3 specification. In a
.dex file, LEB128 is only ever used to encode 32-bit quantities.
Each LEB128 encoded value consists of one to five bytes, which together represent a single 32-bit value. Each byte has its most significant bit set except for the final byte in the sequence, which has its most significant bit clear. The remaining seven bits of each byte are payload, with the least significant seven bits of the quantity in the first byte, the next seven in the second byte and so on. In the case of a signed LEB128 (
sleb128), the most significant payload bit of the final byte in the sequence is sign-extended to produce the final value. In the unsigned case (
uleb128), any bits not explicitly represented are interpreted as
|Bitwise diagram of a two-byte LEB128 value|
|First byte||Second byte|
| ||bit6||bit5||bit4||bit3||bit2||bit1||bit0|| ||bit13||bit12||bit11||bit10||bit9||bit8||bit7|
uleb128p1 is used to represent a signed value, where the representation is of the value plus one encoded as a
uleb128. This makes the encoding of
-1 (alternatively thought of as the unsigned value
0xffffffff) — but no other negative number — a single byte, and is useful in exactly those cases where the represented number must either be non-negative or
0xffffffff), and where no other negative values are allowed (or where large unsigned values are unlikely to be needed).
Here are some examples of the formats:
|Encoded Sequence||As ||As ||As |