Source code for memobj.property.simple
import struct
from typing import Any, Union
from . import MemoryProperty
[docs]
class SimpleData(MemoryProperty):
format_string: Union[str, None] = None
def __init__(
self, offset: int | None = None, format_string: Union[str, None] = None
):
super().__init__(offset)
if format_string is not None:
self.format_string = format_string
def _get_format(self) -> str:
if self.format_string is None:
raise ValueError(f"None format_string for {self.__class__.__name__}")
return self.format_string
[docs]
def from_memory(self) -> Any:
return self.read_formatted_from_offset(self._get_format())
[docs]
def to_memory(self, value: Any):
self.write_formatted_to_offset(self._get_format(), value)
[docs]
def memory_size(self) -> int:
return struct.calcsize(self._get_format())
[docs]
class Char(SimpleData):
format_string = "c"
[docs]
class Bool(SimpleData):
format_string = "?"
[docs]
class Float(SimpleData):
format_string = "f"
[docs]
class Double(SimpleData):
format_string = "d"
[docs]
class Signed1(SimpleData):
format_string = "b"
[docs]
class Unsigned1(SimpleData):
format_string = "B"
[docs]
class Signed2(SimpleData):
format_string = "h"
[docs]
class Unsigned2(SimpleData):
format_string = "H"
[docs]
class Signed4(SimpleData):
format_string = "i"
[docs]
class Unsigned4(SimpleData):
format_string = "I"
[docs]
class Signed8(SimpleData):
format_string = "q"
[docs]
class Unsigned8(SimpleData):
format_string = "Q"