Package ghidra.program.model.data

# Class LEB128

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

public class LEB128 extends Object
Logic for reading/writing 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_SUPPORTED_LENGTH`
  Max number of bytes that is supported by the deserialization code.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `LEB128()`

- ## 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 byte[]`
  `encode(long value, boolean isSigned)`
  Encodes a value into a sequence of LEB128 bytes.
  `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.
  `static int`
  `write(long value, OutputStream os, boolean isSigned)`
  Writes a value to the stream as a sequence of LEB128 bytes.
  `static int`
  `writeSigned(long value, OutputStream os)`
  Writes a value to the stream as a sequence of LEB128 bytes.
  `static int`
  `writeUnsigned(long value, OutputStream os)`
  Writes a value to the stream as a sequence of LEB128 bytes.

  ### 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:
      - Constant Field Values

- ## 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

  - ### encode

    public static byte[] encode(long value, boolean isSigned)
    Encodes a value into a sequence of LEB128 bytes.
    Parameters:
      `value` - to encode
      `isSigned` - boolean flag, if true value is encoded as a signed value, if false value is encoded as an unsigned value
    Returns:
      byte array containing the LEB128 bytes of the value (max 10)

  - ### write

    public static int write(long value, OutputStream os, boolean isSigned) throws IOException
    Writes a value to the stream as a sequence of LEB128 bytes.
    Parameters:
      `value` - to write
      `os` - `OutputStream` to write to
      `isSigned` - boolean flag, if true value is encoded as a signed value, if false value is encoded as an unsigned value
    Returns:
      count of bytes written to stream
    Throws:
      `IOException` - if error writing to stream

  - ### writeUnsigned

    public static int writeUnsigned(long value, OutputStream os) throws IOException
    Writes a value to the stream as a sequence of LEB128 bytes.
    Parameters:
      `value` - to write
      `os` - `OutputStream` to write to
    Returns:
      count of bytes written to stream
    Throws:
      `IOException` - if error writing to stream

  - ### writeSigned

    public static int writeSigned(long value, OutputStream os) throws IOException
    Writes a value to the stream as a sequence of LEB128 bytes.
    Parameters:
      `value` - to write
      `os` - `OutputStream` to write to
    Returns:
      count of bytes written to stream
    Throws:
      `IOException` - if error writing to stream