Package ghidra.program.model.data
Class LEB128
java.lang.Object
ghidra.program.model.data.LEB128
Logic for reading LEB128 values.
LEB128 is a variable length integer encoding that uses 7 bits per byte, with the high bit being reserved as a continuation flag, with the least significant bytes coming first (Little Endian Base 128).
This implementation only supports reading values that decode to at most 64 bits (to fit into a java long).
When reading a value, you must already know if it was written as a signed or unsigned value to be able to decode it correctly.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Max number of bytes that is supported by the deserialization code. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic long
decode
(byte[] bytes, int offset, boolean isSigned) Decodes a LEB128 number from a byte array and returns it as a long.static int
getLength
(InputStream is) Returns the length of the variable length LEB128 value.static long
read
(InputStream is, boolean isSigned) Reads a LEB128 number from the stream and returns it as a java 64 bit long int.static long
signed
(InputStream is) Reads a signed LEB128 variable length integer from the stream.static long
unsigned
(InputStream is) Reads an unsigned LEB128 variable length integer from the stream.
-
Field Details
-
MAX_SUPPORTED_LENGTH
public static final int MAX_SUPPORTED_LENGTHMax number of bytes that is supported by the deserialization code.- See Also:
-
-
Constructor Details
-
LEB128
public LEB128()
-
-
Method Details
-
unsigned
Reads an unsigned LEB128 variable length integer from the stream.- Parameters:
is
-InputStream
to get bytes from- Returns:
- leb128 value, as a long
- Throws:
IOException
- if an I/O error occurs or decoded value is outside the range of a java 64 bit int (or it used more than 10 bytes to be encoded), or there is an error or EOF getting a byte from the InputStream before reaching the end of the encoded value
-
signed
Reads a signed LEB128 variable length integer from the stream.- Parameters:
is
-InputStream
to get bytes from- Returns:
- leb128 value, as a long
- Throws:
IOException
- if an I/O error occurs or decoded value is outside the range of a java 64 bit int (or it used more than 10 bytes to be encoded), or there is an error or EOF getting a byte from the InputStream before reaching the end of the encoded value
-
read
Reads a LEB128 number from the stream and returns it as a java 64 bit long int.Large unsigned integers that use all 64 bits are returned in a java native 'long' type, which is signed. It is up to the caller to treat the value as unsigned.
Large integers that use more than 64 bits will cause an IOException to be thrown.
- Parameters:
is
-InputStream
to get bytes fromisSigned
- true if the value is signed- Returns:
- long integer value. Caller must treat it as unsigned if isSigned parameter was set to false
- Throws:
IOException
- if an I/O error occurs or decoded value is outside the range of a java 64 bit int (or it used more than 10 bytes to be encoded), or there is an error or EOF getting a byte from the InputStream before reaching the end of the encoded value
-
getLength
Returns the length of the variable length LEB128 value.- Parameters:
is
- InputStream to get bytes from- Returns:
- length of the LEB128 value, or -1 if the end of the value is not found
- Throws:
IOException
- if error getting next byte from stream
-
decode
Decodes a LEB128 number from a byte array and returns it as a long.- Parameters:
bytes
- the bytes representing the LEB128 numberoffset
- offset in byte array of where to start reading bytesisSigned
- true if the value is signed- Returns:
- long integer value. Caller must treat it as unsigned if isSigned parameter was set to false
- Throws:
IOException
- if array offset is invalid, decoded value is outside the range of a java 64 bit int (or it used more than 10 bytes to be encoded), or the end of the array was reached before reaching the end of the encoded value
-