Class LEB128

java.lang.Object
ghidra.program.model.data.LEB128

public class LEB128 extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Max number of bytes that is supported by the deserialization code.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    decode(byte[] bytes, int offset, boolean isSigned)
    Decodes a LEB128 number from a byte array and returns it as a long.
    static int
    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
    Reads a signed LEB128 variable length integer from the stream.
    static long
    Reads an unsigned LEB128 variable length integer from the stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_SUPPORTED_LENGTH

      public static final int MAX_SUPPORTED_LENGTH
      Max number of bytes that is supported by the deserialization code.
      See Also:
  • Constructor Details

    • LEB128

      public LEB128()
  • Method Details

    • unsigned

      public static long unsigned(InputStream is) throws IOException
      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

      public static long signed(InputStream is) throws IOException
      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

      public static long read(InputStream is, boolean isSigned) throws IOException
      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 from
      isSigned - 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

      public static int getLength(InputStream is) throws IOException
      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

      public static long decode(byte[] bytes, int offset, boolean isSigned) throws IOException
      Decodes a LEB128 number from a byte array and returns it as a long.

      See read(InputStream, boolean)

      Parameters:
      bytes - the bytes representing the LEB128 number
      offset - offset in byte array of where to start reading bytes
      isSigned - 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