Class MemoryByteProvider
- All Implemented Interfaces:
ByteProvider,Closeable,AutoCloseable
- Direct Known Subclasses:
MemoryMutableByteProvider
ByteProvider implementation based on Memory.
The bytes returned by this provider are indexed relative to the baseAddress
supplied to the constructor, and are limited to memory blocks of the
same address space.
Warnings:
Using this ByteProvider with memory block/address spaces that are not simple "ram" initialized memory blocks is fraught with peril.
Addresses and address spaces can use all 64 bits of a long as an offset, which
causes a problem when trying to express the correct length() of this ByteProvider as
a long. (this is why address ranges deal with inclusive end values instead of exclusive).
- The return value of
length()is constrained to a max of Long.MAX_VALUE isValidIndex(long)treats its argument as an unsigned int64, and works for the entire address space range.
Not all byte provider index locations between 0 and length() will be valid
(because gaps between memory blocks), and may generate exceptions when those locations are read.
- To avoid this situation, the caller will need to use information from the program's Memory manager to align reads to valid locations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Addressprotected booleanprotected longprotected MemoryFields inherited from interface ghidra.app.util.bin.ByteProvider
EMPTY_BYTEPROVIDER -
Constructor Summary
ConstructorsConstructorDescriptionMemoryByteProvider(Memory memory, Address baseAddress) Constructs a newMemoryByteProviderrelative to the specified base address, containing the address range to the highest address in the same address space currently found in the memory map.MemoryByteProvider(Memory memory, Address baseAddress, boolean firstBlockOnly) Constructs a newMemoryByteProviderrelative to the specified base address, containing the address range to the end of the first memory block, or the highest address in the same address space, currently found in the memory map.MemoryByteProvider(Memory memory, Address baseAddress, Address maxAddress) Constructs a newMemoryByteProviderrelative to the specified base address, with the specified length.MemoryByteProvider(Memory memory, AddressSpace space) Constructs a newMemoryByteProviderfor a specificAddressSpace. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases any resources theByteProvidermay have occupiedstatic MemoryByteProvidercreateDefaultAddressSpaceByteProvider(Program program, boolean firstBlockOnly) Create aByteProviderthat starts at the beginning (e.g.static MemoryByteProvidercreateMemoryBlockByteProvider(Memory memory, MemoryBlock block) Create aByteProviderthat is limited to the specifiedMemoryBlock.static MemoryByteProvidercreateProgramHeaderByteProvider(Program program, boolean firstBlockOnly) Create aByteProviderthat starts at the beginning of the specifiedprogram'smemory, containing either just the first memory block, or all memory blocks (of the same address space).Returns the absolute path (similar to, but not a, URI) to theByteProvider.Returns the address range of the bytes of this provider.Returns the address of the last byte of this provider.getFile()Returns the underlyingFilefor thisByteProvider, or null if thisByteProvideris not associated with aFile.getName()Returns the name of theByteProvider.Returns the address of the first byte of this provider.booleanisEmpty()Returns true if this ByteProvider does not contain any bytes.booleanisValidIndex(long index) Returns true if the specified index is valid.longlength()Returns the length of theByteProviderbytereadByte(long index) Reads a byte at the specified indexbyte[]readBytes(long index, long length) Reads a byte array at the specified indexMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ghidra.app.util.bin.ByteProvider
getFSRL, getInputStream
-
Field Details
-
memory
-
baseAddress
-
maxOffset
protected long maxOffset -
isEmtpy
protected boolean isEmtpy
-
-
Constructor Details
-
MemoryByteProvider
Constructs a newMemoryByteProviderfor a specificAddressSpace. Bytes will be provided relative to the minimum address (typically 0) in the space, and ranges to the highest address in the same address space currently found in the memory map.- Parameters:
memory- theMemoryspace- theAddressSpace
-
MemoryByteProvider
Constructs a newMemoryByteProviderrelative to the specified base address, containing the address range to the highest address in the same address space currently found in the memory map.- Parameters:
memory- theMemorybaseAddress- the base address
-
MemoryByteProvider
Constructs a newMemoryByteProviderrelative to the specified base address, containing the address range to the end of the first memory block, or the highest address in the same address space, currently found in the memory map.- Parameters:
memory- theMemorybaseAddress- the base addressfirstBlockOnly- boolean flag, if true, only the first memory block will be accessible, if false, all memory blocks of the address space will be accessible
-
MemoryByteProvider
Constructs a newMemoryByteProviderrelative to the specified base address, with the specified length.- Parameters:
memory- theMemorybaseAddress- the base addressmaxAddress- the highest address accessible by this provider (inclusive), or null if there is no memory
-
-
Method Details
-
createMemoryBlockByteProvider
Create aByteProviderthat is limited to the specifiedMemoryBlock.- Parameters:
memory-Memoryof the programblock-MemoryBlockto read from- Returns:
- new
ByteProviderthat contains the bytes of the specified MemoryBlock
-
createProgramHeaderByteProvider
public static MemoryByteProvider createProgramHeaderByteProvider(Program program, boolean firstBlockOnly) Create aByteProviderthat starts at the beginning of the specifiedprogram'smemory, containing either just the first memory block, or all memory blocks (of the same address space).- Parameters:
program-Programto readfirstBlockOnly- boolean flag, if true, only the first memory block will be accessible via the returned provider, if false, all memory blocks of the address space will be accessible- Returns:
- new
MemoryByteProvider, starting at program's minAddress
-
createDefaultAddressSpaceByteProvider
public static MemoryByteProvider createDefaultAddressSpaceByteProvider(Program program, boolean firstBlockOnly) Create aByteProviderthat starts at the beginning (e.g. 0) of the specifiedprogram'sdefault address space memory, containing either the first memory block, or all memory blocks (of the same address space).- Parameters:
program-Programto readfirstBlockOnly- boolean flag, if true, only the first memory block will be accessible via the returned provider, if false, all memory blocks of the address space will be accessible- Returns:
- new
MemoryByteProvider, starting at program's minAddress
-
getStartAddress
Returns the address of the first byte of this provider.- Returns:
- address of the first byte returned by this provider (at index 0)
-
getEndAddress
Returns the address of the last byte of this provider.- Returns:
- address of the last byte returned by this provider
-
getAddressSet
Returns the address range of the bytes of this provider.- Returns:
- address range of first byte to last byte of this provider
-
isEmpty
public boolean isEmpty()Description copied from interface:ByteProviderReturns true if this ByteProvider does not contain any bytes.- Specified by:
isEmptyin interfaceByteProvider- Returns:
- boolean true if this provider is empty, false if contains bytes
-
getFile
Description copied from interface:ByteProviderReturns the underlyingFilefor thisByteProvider, or null if thisByteProvideris not associated with aFile.- Specified by:
getFilein interfaceByteProvider- Returns:
- the underlying file for this byte provider
-
getName
Description copied from interface:ByteProviderReturns the name of theByteProvider. For example, the underlying file name.- Specified by:
getNamein interfaceByteProvider- Returns:
- the name of the
ByteProvideror null if there is no name
-
getAbsolutePath
Description copied from interface:ByteProviderReturns the absolute path (similar to, but not a, URI) to theByteProvider. For example, the complete path to the file.- Specified by:
getAbsolutePathin interfaceByteProvider- Returns:
- the absolute path to the
ByteProvideror null if not associated with aFile.
-
length
Description copied from interface:ByteProviderReturns the length of theByteProvider- Specified by:
lengthin interfaceByteProvider- Returns:
- the length of the
ByteProvider - Throws:
IOException- if an I/O error occurs
-
isValidIndex
public boolean isValidIndex(long index) Description copied from interface:ByteProviderReturns true if the specified index is valid.- Specified by:
isValidIndexin interfaceByteProvider- Parameters:
index- the index in the byte provider to check- Returns:
- true if the specified index is valid
-
readByte
Description copied from interface:ByteProviderReads a byte at the specified index- Specified by:
readBytein interfaceByteProvider- Parameters:
index- the index of the byte to read- Returns:
- the byte read from the specified index
- Throws:
IOException- if an I/O error occurs
-
readBytes
Description copied from interface:ByteProviderReads a byte array at the specified index- Specified by:
readBytesin interfaceByteProvider- Parameters:
index- the index of the byte to readlength- the number of bytes to read- Returns:
- the byte array read from the specified index
- Throws:
IOException- if an I/O error occurs
-
close
public void close()Description copied from interface:ByteProviderReleases any resources theByteProvidermay have occupied- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceByteProvider- Specified by:
closein interfaceCloseable
-