MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / pe_image_base

Function pe_image_base

scripts/memwaste-report.py:114–133  ·  view source on GitHub ↗

The preferred ImageBase of a PE file, or None for anything else. A PE symboliser wants VA = ImageBase + RVA, and the dump gives RVA + runtime base.

(binary: str)

Source from the content-addressed store, hash-verified

112
113
114def pe_image_base(binary: str) -> int | None:
115 """The preferred ImageBase of a PE file, or None for anything else. A PE
116 symboliser wants VA = ImageBase + RVA, and the dump gives RVA + runtime base."""
117 try:
118 with open(binary, "rb") as fh:
119 head = fh.read(4096)
120 except OSError:
121 return None
122 if head[:2] != b"MZ" or len(head) < 0x40:
123 return None
124 pe = int.from_bytes(head[0x3C:0x40], "little")
125 if head[pe:pe + 4] != b"PE\0\0":
126 return None
127 opt = pe + 24
128 magic = int.from_bytes(head[opt:opt + 2], "little")
129 if magic == 0x20B: # PE32+
130 return int.from_bytes(head[opt + 24:opt + 32], "little")
131 if magic == 0x10B: # PE32
132 return int.from_bytes(head[opt + 28:opt + 32], "little")
133 return None
134
135
136def symbolise(binary: str | None, base: str, addrs: list[str]) -> dict[str, str]:

Callers 1

symboliseFunction · 0.85

Calls 2

openFunction · 0.50
readMethod · 0.45

Tested by

no test coverage detected