Class WrappedMemBuffer

java.lang.Object
ghidra.program.model.mem.WrappedMemBuffer
All Implemented Interfaces:
MemBuffer

public class WrappedMemBuffer extends Object implements MemBuffer
WrappedMemBuffer implements a MemBuffer that provides a zero based index on top of an underlying membuffer with at a given address. It can buffer N bytes at time using the constructor that takes a cache size. However the default is to provide no buffering. Use of the buffer can reduce the overall number of calls to Memory, greatly reducing the overhead of various error checks. This implementation will not wrap if the end of the memory space is encountered. The getByte(int), getBytes(byte[], int) methods can cause the bytes in the buffer cache if the request is outside of the current cached bytes. WARNING: The underlying MemBuffer should not change its base address. Using a mutable MemBuffer can cause problematic behavior if not controlled carefully. WARNING: Not thread-safe.
  • Constructor Details

    • WrappedMemBuffer

      public WrappedMemBuffer(MemBuffer buf, int baseOffset) throws AddressOutOfBoundsException
      Construct a wrapped MemBuffer with an adjustable base offset
      Parameters:
      buf - memory buffer
      baseOffset - offset relative to the underlying buffer's start address (addr + baseOffset) will be the 0 index into this buffer
      Throws:
      AddressOutOfBoundsException
    • WrappedMemBuffer

      public WrappedMemBuffer(MemBuffer buf, int bufferSize, int baseOffset) throws AddressOutOfBoundsException
      Construct a wrapped MemBuffer with an adjustable base offset
      Parameters:
      buf - memory buffer
      bufferSize - size of cache buffer - specify 0 for no buffering
      baseOffset - offset relative to the underlying buffer's start address (addr + baseOffset) will be the 0 index into this buffer
      Throws:
      AddressOutOfBoundsException
  • Method Details

    • getAddress

      public Address getAddress()
      Description copied from interface: MemBuffer
      Get the Address which corresponds to the offset 0.
      Specified by:
      getAddress in interface MemBuffer
      Returns:
      the current address of offset 0.
    • getByte

      public byte getByte(int offset) throws MemoryAccessException
      Description copied from interface: MemBuffer
      Get one byte from memory at the current position plus offset.
      Specified by:
      getByte in interface MemBuffer
      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
    • getBytes

      public int getBytes(byte[] b, int offset)
      Description copied from interface: MemBuffer
      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.
      Specified by:
      getBytes in interface MemBuffer
      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.
    • getMemory

      public Memory getMemory()
      Description copied from interface: MemBuffer
      Get the Memory object actually used by the MemBuffer.
      Specified by:
      getMemory in interface MemBuffer
      Returns:
      the Memory used by this MemBuffer or null if not available.
    • isBigEndian

      public boolean isBigEndian()
      Description copied from interface: MemBuffer
      Returns true if the underlying bytes are in big-endian order, false if they are little endian.
      Specified by:
      isBigEndian in interface MemBuffer
      Returns:
      true if the underlying bytes are in big-endian order, false if they are little endian.
    • getShort

      public short getShort(int offset) throws MemoryAccessException
      Description copied from interface: MemBuffer
      returns the short at the given offset, taking into account the endianness.
      Specified by:
      getShort in interface MemBuffer
      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 endianness.
      Throws:
      MemoryAccessException - if a 2-byte short value cannot be read at the specified offset
    • getInt

      public int getInt(int offset) throws MemoryAccessException
      Description copied from interface: MemBuffer
      returns the int at the given offset, taking into account the endianness.
      Specified by:
      getInt in interface MemBuffer
      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 endianness.
      Throws:
      MemoryAccessException - if a 4-byte integer value cannot be read at the specified offset
    • getLong

      public long getLong(int offset) throws MemoryAccessException
      Description copied from interface: MemBuffer
      returns the long at the given offset, taking into account the endianness.
      Specified by:
      getLong in interface MemBuffer
      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 endianness.
      Throws:
      MemoryAccessException - if a 8-byte long value cannot be read at the specified offset
    • getBigInteger

      public BigInteger getBigInteger(int offset, int size, boolean signed) throws MemoryAccessException
      Description copied from interface: MemBuffer
      returns the value at the given offset, taking into account the endianness.
      Specified by:
      getBigInteger in interface MemBuffer
      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 endianness.
      Throws:
      MemoryAccessException - if the request size value cannot be read at the specified offset