mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 10:49:34 +02:00
GP-1186 added method for finding addresses where a specific byte from a fileBytes object was loaded into memory
This commit is contained in:
parent
47247acf07
commit
9d80870415
2 changed files with 45 additions and 2 deletions
|
@ -17,8 +17,7 @@ package ghidra.program.model.mem;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import ghidra.framework.store.LockException;
|
||||
import ghidra.program.database.mem.*;
|
||||
|
@ -847,4 +846,29 @@ public interface Memory extends AddressSetView {
|
|||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of addresses where the byte at the given offset
|
||||
* from the given FileBytes was loaded into memory.
|
||||
* @param offset the file offset in the given FileBytes of the byte that is to be
|
||||
* located in memory
|
||||
* @param fileBytes the FileBytesobject whose byte is to be located in memory
|
||||
* @return a list of addresses that are associated with the given
|
||||
* FileBytes and offset
|
||||
*/
|
||||
public default List<Address> locateAddressesForFileBytesOffset(FileBytes fileBytes, long offset) {
|
||||
List<Address> list = new ArrayList<>();
|
||||
for (MemoryBlock memBlock : getBlocks()) {
|
||||
for (MemoryBlockSourceInfo info : memBlock.getSourceInfos()) {
|
||||
Optional<FileBytes> blockFileBytes = info.getFileBytes();
|
||||
if (blockFileBytes.isPresent() && blockFileBytes.get().equals(fileBytes)) {
|
||||
Address addr = info.locateAddressForFileOffset(offset);
|
||||
if (addr != null) {
|
||||
list.add(addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -294,6 +294,25 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
|||
catch (IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAddressForFileBytesAndOffset() throws Exception {
|
||||
FileBytes fileBytes = createFileBytes();
|
||||
mem.createInitializedBlock("test1", addr(100), fileBytes, 10, 50, false);
|
||||
mem.createInitializedBlock("test2", addr(200), fileBytes, 40, 50, false);
|
||||
|
||||
List<Address> addresses = mem.locateAddressesForFileBytesOffset(fileBytes, 0);
|
||||
assertTrue(addresses.isEmpty());
|
||||
|
||||
addresses = mem.locateAddressesForFileBytesOffset(fileBytes, 10);
|
||||
assertEquals(1, addresses.size());
|
||||
assertEquals(addr(100), addresses.get(0));
|
||||
|
||||
addresses = mem.locateAddressesForFileBytesOffset(fileBytes, 40);
|
||||
assertEquals(2, addresses.size());
|
||||
assertTrue(addresses.contains(addr(130)));
|
||||
assertTrue(addresses.contains(addr(200)));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue