-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path3903-adds-getOriginalBytes-function-to-flat-API.patch
More file actions
57 lines (53 loc) · 2.24 KB
/
Copy path3903-adds-getOriginalBytes-function-to-flat-API.patch
File metadata and controls
57 lines (53 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: jesko <rattle@nullteilerfrei.de>
Date: Sun, 23 Jan 2022 15:35:43 +0100
Subject: [PATCH] 3903: adds getOriginalBytes function to flat API
---
.../program/flatapi/FlatProgramAPI.java | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java b/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java
index 7a97a642ec..bd133cd4c2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java
@@ -41,6 +41,7 @@ import ghidra.features.base.memsearch.searcher.MemorySearcher;
import ghidra.framework.main.AppInfo;
import ghidra.framework.model.*;
import ghidra.framework.plugintool.PluginTool;
+import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.*;
import ghidra.program.model.data.*;
import ghidra.program.model.lang.CompilerSpec;
@@ -2009,6 +2010,32 @@ public class FlatProgramAPI {
return bytes;
}
+ /**
+ * Reads length number of bytes starting at the specified address as they appear in
+ * the original binary.
+ * Note: this could be inefficient if length is large
+ * @param address the address to start reading
+ * @param length the number of bytes to read
+ * @return an array of bytes
+ * @see ghidra.program.model.mem.Memory
+ */
+ protected byte[] getOriginalBytes(Address address, int length) {
+ MemoryBlock stringMemoryBlock = currentProgram.getMemory().getBlock(address);
+ if (stringMemoryBlock == null)
+ return null;
+ FileBytes fileBytes = currentProgram.getMemory().getAllFileBytes().get(0);
+ MemoryBlockSourceInfo memoryInformation = stringMemoryBlock.getSourceInfos().get(0);
+ long fileOffset = address.getOffset() - memoryInformation.getMinAddress().getOffset()
+ + memoryInformation.getFileBytesOffset();
+ try {
+ byte[] result = new byte[length];
+ fileBytes.getOriginalBytes(fileOffset, result);
+ return result;
+ } catch (IOException X) {
+ return null;
+ }
+ }
+
/**
* Sets the 'byte' value at the specified address.
* @param address the address to set the 'byte'
--
2.45.1