Class LEB128

java.lang.Object
ghidra.app.util.bin.format.dwarf4.LEB128

public class LEB128 extends Object
Class to hold result of reading a LEB128 value, along with size and position metadata.

Note: If a LEB128 value that would result in a native value longer than 64bits is attempted to be read, an IOException will be thrown, and the stream's position will be left at the last read byte.

If this was a valid (but overly large) LEB128, the caller's stream will be left still pointing to LEB data.

  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the value as an signed int32.
    long
    Returns the value as a 64bit primitive long.
    int
    Returns the value as an unsigned int32.
    static long
    decode(byte[] bytes, boolean isSigned)
    Decodes a LEB128 number from a byte array and returns it as a long.
    static long
    decode(byte[] bytes, int offset, boolean isSigned)
    Decodes a LEB128 number from a byte array and returns it as a long.
    int
    Returns the number of bytes that were used to store the LEB128 value in the stream it was read from.
    long
    Returns the offset of the LEB128 value in the stream it was read from.
    static int
    Reads a LEB128 signed number from the BinaryReader and returns it as a java 32 bit int.
    static long
    readAsLong(BinaryReader reader, boolean isSigned)
    Reads a LEB128 number from the BinaryReader and returns it as a java 64 bit long int.
    static int
    Reads a LEB128 unsigned number from the BinaryReader and returns it as a java 32 bit int.
    static LEB128
    Reads an signed LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.
    static LEB128
    Reads an unsigned LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.
    static LEB128
    readValue(BinaryReader reader, boolean isSigned)
    Reads a LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • asUInt32

      public int asUInt32() throws IOException
      Returns the value as an unsigned int32. If the actual value is outside the positive range of a java int (ie. 0.. Integer.MAX_VALUE), an exception is thrown.
      Returns:
      int in the range of 0 to Integer.MAX_VALUE
      Throws:
      IOException - if value is outside range
    • asInt32

      public int asInt32() throws IOException
      Returns the value as an signed int32. If the actual value is outside the range of a java int (ie. Integer.MIN_VALUE.. Integer.MAX_VALUE), an exception is thrown.
      Returns:
      int in the range of Integer.MIN_VALUE to Integer.MAX_VALUE
      Throws:
      IOException - if value is outside range
    • asLong

      public long asLong()
      Returns the value as a 64bit primitive long. Interpreting the signed-ness of the value will depend on the way the value was read (ie. if readSignedValue(BinaryReader) vs. readUnsignedValue(BinaryReader) was used).
      Returns:
      long value.
    • getOffset

      public long getOffset()
      Returns the offset of the LEB128 value in the stream it was read from.
      Returns:
      stream offset of the LEB128 value
    • getLength

      public int getLength()
      Returns the number of bytes that were used to store the LEB128 value in the stream it was read from.
      Returns:
      number of bytes used to store the read LEB128 value
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • readValue

      public static LEB128 readValue(BinaryReader reader, boolean isSigned) throws IOException
      Reads a LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.

      See readAsLong(BinaryReader, boolean).

      Parameters:
      reader - BinaryReader to read bytes from
      isSigned - true if the value is signed
      Returns:
      a LEB128 instance with the read LEB128 value with metadata
      Throws:
      IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
    • readUnsignedValue

      public static LEB128 readUnsignedValue(BinaryReader reader) throws IOException
      Reads an unsigned LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.

      See readAsLong(BinaryReader, boolean).

      Parameters:
      reader - BinaryReader to read bytes from
      Returns:
      a LEB128 instance with the read LEB128 value with metadata
      Throws:
      IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
    • readSignedValue

      public static LEB128 readSignedValue(BinaryReader reader) throws IOException
      Reads an signed LEB128 value from the BinaryReader and returns a LEB128 instance that contains the value along with size and position metadata.

      See readAsLong(BinaryReader, boolean).

      Parameters:
      reader - BinaryReader to read bytes from
      Returns:
      a LEB128 instance with the read LEB128 value with metadata
      Throws:
      IOException - if an I/O error occurs or value is outside the range of a java 64 bit int
    • readAsInt32

      public static int readAsInt32(BinaryReader reader) throws IOException
      Reads a LEB128 signed number from the BinaryReader and returns it as a java 32 bit int.

      If the value of the number can not fit in the int type, an IOException will be thrown.

      Parameters:
      reader - BinaryReader to read bytes from
      Returns:
      signed int32 value
      Throws:
      IOException - if error reading bytes or value is outside the range of a signed int32
    • readAsUInt32

      public static int readAsUInt32(BinaryReader reader) throws IOException
      Reads a LEB128 unsigned number from the BinaryReader and returns it as a java 32 bit int.

      If the value of the number can not fit in the positive range of the int type, an IOException will be thrown.

      Parameters:
      reader - BinaryReader to read bytes from
      Returns:
      unsigned int32 value 0..Integer.MAX_VALUE
      Throws:
      IOException - if error reading bytes or value is outside the positive range of a java 32 bit int (ie. 0..Integer.MAX_VALUE)
    • readAsLong

      public static long readAsLong(BinaryReader reader, boolean isSigned) throws IOException
      Reads a LEB128 number from the BinaryReader and returns it as a java 64 bit long int.

      Large unsigned integers that use all 64 bits are be 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:
      reader - BinaryReader to read 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 value is outside the range of a java 64 bit int
    • decode

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

      See readAsLong(BinaryReader, boolean).

      Parameters:
      bytes - the bytes representing the LEB128 number
      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 error reading bytes or value is outside the range of a java 64 bit int
    • 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 readAsLong(BinaryReader, 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 error reading bytes or value is outside the range of a java 64 bit int