Add EndianIO.parse_pointer function

This is to parse in-memory pointer data bytes into an actual pointer value.
This commit is contained in:
Sybren A. Stüvel 2025-01-24 15:34:46 +01:00
parent f2e28edc05
commit eb69ca5632

View File

@ -129,6 +129,20 @@ class EndianIO:
return cls.read_ulong(fileobj) return cls.read_ulong(fileobj)
raise ValueError("unsupported pointer size %d" % pointer_size) raise ValueError("unsupported pointer size %d" % pointer_size)
@classmethod
def parse_pointer(cls, pointer_data: bytes):
"""Parse bytes as a pointer value."""
pointer_size = len(pointer_data)
try:
typestruct = {
4: cls.UINT,
8: cls.ULONG,
}[pointer_size]
except KeyError:
raise ValueError("unsupported pointer size %d" % pointer_size)
return typestruct.unpack(pointer_data)[0]
@classmethod @classmethod
def write_pointer(cls, fileobj: typing.IO[bytes], pointer_size: int, value: int): def write_pointer(cls, fileobj: typing.IO[bytes], pointer_size: int, value: int):
"""Write a pointer to a file.""" """Write a pointer to a file."""