Package ghidra.program.model.mem

# Interface MemBuffer

All Known Subinterfaces:
  `CodeUnit`, `Data`, `Instruction`, `MemBufferMixin`, `MutableMemBuffer`

All Known Implementing Classes:
  `AbstractBytesPcodeExecutorStatePiece.StateMemBuffer`, `ByteMemBufferImpl`, `DataStub`, `DumbMemBufferImpl`, `EmulateMemoryStateBuffer`, `InstructionDB`, `InstructionStub`, `MemoryBufferImpl`, `PseudoData`, `PseudoInstruction`, `WrappedMemBuffer`

public interface MemBuffer
MemBuffer provides an array like interface into memory at a specific address. Bytes can be retrieved by using a positive offset from the current position. Depending on the implementation, the offset may be restricted to a specific positive range. If the implementation does have a restriction, then a MemoryAccessException will be thrown, except for the `getBytes(byte[], int)` method which will return 0. The purpose of this class is to allow an efficient implementation that buffers memory accesses and does not have to keep translating addresses. This was designed to be passed to a language parser. One advantage of MemBuffer over a byte array is that if necessary the actual Memory and Address can be retrieved in case all of the necessary bytes are not local. This interface does not provide methods to reposition the memory buffer. This is so that it is clear that methods accepting this base class are not to mess which the base Address for this object. Memory-backed access is an optional implementation dependent capability. In addition, the use of the relative offset is implementation dependent, but in general those implementations which are backed by memory may choose to wrap the offset when computing the corresponding memory address. The treatment of the offset argument should be consistent across the various methods for a given implementation.

See Also:
  - `MutableMemBuffer`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Address`
  `getAddress()`
  Get the Address which corresponds to the offset 0.
  `BigInteger`
  `getBigInteger(int offset, int size, boolean signed)`
  returns the value at the given offset, taking into account the endianess.
  `byte`
  `getByte(int offset)`
  Get one byte from memory at the current position plus offset.
  `int`
  `getBytes(byte[] b, int offset)`
  Reads `b.length` bytes from this memory buffer starting at the address of this memory buffer plus the given memoryBufferOffset from that position.
  `default InputStream`
  `getInputStream()`
  Returns a stream that supplies the bytes of this buffer, starting at offset 0.
  `default InputStream`
  `getInputStream(int initialPosition, int length)`
  Returns a stream that supplies the bytes of this buffer, starting at the specified offset.
  `int`
  `getInt(int offset)`
  returns the int at the given offset, taking into account the endianess.
  `long`
  `getLong(int offset)`
  returns the long at the given offset, taking into account the endianess.
  `Memory`
  `getMemory()`
  Get the Memory object actually used by the MemBuffer.
  `short`
  `getShort(int offset)`
  returns the short at the given offset, taking into account the endianess.
  `default int`
  `getUnsignedByte(int offset)`
  Get one unsigned byte from memory at the current position plus offset.
  `default long`
  `getUnsignedInt(int offset)`
  Returns the unsigned int at the given offset, taking into account the endianess.
  `default int`
  `getUnsignedShort(int offset)`
  Returns the unsigned short at the given offset, taking into account the endian-ness.
  `default int`
  `getVarLengthInt(int offset, int len)`
  Returns the signed value of the integer (of the specified length) at the specified offset.
  `default long`
  `getVarLengthUnsignedInt(int offset, int len)`
  Returns the unsigned value of the integer (of the specified length) at the specified offset.
  `boolean`
  `isBigEndian()`
  Returns true if the underlying bytes are in big-endian order, false if they are little endian.
  `default boolean`
  `isInitializedMemory()`
  Returns true if this buffer's starting address has valid data.

- ## Method Details

  - ### isInitializedMemory

    default boolean isInitializedMemory()
    Returns true if this buffer's starting address has valid data.
    Returns:
      boolean true if first byte of memory buffer can be read

  - ### getByte

    byte getByte(int offset) throws MemoryAccessException
    Get one byte from memory at the current position plus offset.
    Parameters:
      `offset` - the displacement from the current position.
    Returns:
      the data at offset from the current position.
    Throws:
      `MemoryAccessException` - if memory cannot be read at the specified offset

  - ### getUnsignedByte

    default int getUnsignedByte(int offset) throws MemoryAccessException
    Get one unsigned byte from memory at the current position plus offset.
    Parameters:
      `offset` - the displacement from the current position.
    Returns:
      the byte data at offset from the current position, as a `int` value.
    Throws:
      `MemoryAccessException` - if memory cannot be read at the specified offset

  - ### getBytes

    int getBytes(byte[] b, int offset)
    Reads `b.length` bytes from this memory buffer starting at the address of this memory buffer plus the given memoryBufferOffset from that position. The actual number of bytes may be fewer if bytes can't be read.
    Parameters:
      `b` - the buffer into which bytes will be placed
      `offset` - the offset in this memory buffer from which to start reading bytes.
    Returns:
      the number of bytes read which may be fewer than b.length if available bytes are exhausted or no bytes are available at the specified offset.

  - ### getAddress

    Address getAddress()
    Get the Address which corresponds to the offset 0.
    Returns:
      the current address of offset 0.

  - ### getMemory

    Memory getMemory()
    Get the Memory object actually used by the MemBuffer.
    Returns:
      the Memory used by this MemBuffer or null if not available.

  - ### isBigEndian

    boolean isBigEndian()
    Returns true if the underlying bytes are in big-endian order, false if they are little endian.
    Returns:
      true if the underlying bytes are in big-endian order, false if they are little endian.

  - ### getShort

    short getShort(int offset) throws MemoryAccessException
    returns the short at the given offset, taking into account the endianess.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
    Returns:
      the short at the given offset, taking into account the endianess.
    Throws:
      `MemoryAccessException` - if a 2-byte short value cannot be read at the specified offset

  - ### getUnsignedShort

    default int getUnsignedShort(int offset) throws MemoryAccessException
    Returns the unsigned short at the given offset, taking into account the endian-ness.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
    Returns:
      the unsigned short at the given offset, as a `int`, taking into account the endianess.
    Throws:
      `MemoryAccessException` - if a 2-byte short value cannot be read at the specified offset

  - ### getInt

    int getInt(int offset) throws MemoryAccessException
    returns the int at the given offset, taking into account the endianess.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
    Returns:
      the int at the given offset, taking into account the endianess.
    Throws:
      `MemoryAccessException` - if a 4-byte integer value cannot be read at the specified offset

  - ### getUnsignedInt

    default long getUnsignedInt(int offset) throws MemoryAccessException
    Returns the unsigned int at the given offset, taking into account the endianess.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
    Returns:
      the unsigned int at the given offset, as a `long`, taking into account the endianess.
    Throws:
      `MemoryAccessException` - if a 4-byte integer value cannot be read at the specified offset

  - ### getLong

    long getLong(int offset) throws MemoryAccessException
    returns the long at the given offset, taking into account the endianess.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
    Returns:
      the long at the given offset, taking into account the endianess.
    Throws:
      `MemoryAccessException` - if a 8-byte long value cannot be read at the specified offset

  - ### getBigInteger

    BigInteger getBigInteger(int offset, int size, boolean signed) throws MemoryAccessException
    returns the value at the given offset, taking into account the endianess.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
      `size` - the number of bytes to include in the value
      `signed` - true if value should be treated as a signed twos-compliment value.
    Returns:
      the value at the given offset, taking into account the endianess.
    Throws:
      `MemoryAccessException` - if the request size value cannot be read at the specified offset

  - ### getVarLengthInt

    default int getVarLengthInt(int offset, int len) throws MemoryAccessException
    Returns the signed value of the integer (of the specified length) at the specified offset.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
      `len` - the number of bytes that the integer occupies (ie. 2 bytes == short int, 4 bytes == 32bit int, etc), valid lens are 1, 2 and 4.
    Returns:
      int integer value
    Throws:
      `MemoryAccessException`

  - ### getVarLengthUnsignedInt

    default long getVarLengthUnsignedInt(int offset, int len) throws MemoryAccessException
    Returns the unsigned value of the integer (of the specified length) at the specified offset.
    Parameters:
      `offset` - the offset from the membuffers origin (the address that it is set at)
      `len` - the number of bytes that the integer occupies (ie. 2 bytes == short int, 4 bytes == 32bit int, etc), valid lens are 1, 2 and 4.
    Returns:
      long integer value
    Throws:
      `MemoryAccessException`

  - ### getInputStream

    default InputStream getInputStream()
    Returns a stream that supplies the bytes of this buffer, starting at offset 0.

    Note: the default implementation will produce invalid results if the underlying MemBuffer instance is is mutated to point to different memory.

    Returns:
      an InputStream that returns the bytes of this mem buffer

  - ### getInputStream

    default InputStream getInputStream(int initialPosition, int length)
    Returns a stream that supplies the bytes of this buffer, starting at the specified offset.

    Note: the default implementation will produce invalid results if the underlying MemBuffer instance is is mutated to point to different memory.

    Parameters:
      `initialPosition` - location in membuffer where the stream should start
      `length` - number of bytes to limit the stream to
    Returns:
      an InputSTream that returns the bytes of this mem buffer