def __init__(self, parent, offset): """ :param parent: This object parent :param offset: offset from the start of the parent's data """ self.parent = parent self.elf = self.parent.elf self.offset = ElfOffset(offset)
def _get_section_by_index(self, index) -> ElfSectionHeader: if int(index) >= len(self): raise KeyError( f'Index {int(index)} is out of section table (size={int(self.elf.header.e_shnum)})' ) offset = int(self.elf.header.e_shoff.data) + int( self.elf.header.e_shentsize.data) * int(index) return ElfSectionHeader(self.elf, ElfOffset(offset))
def __init__(cls, *args, **kwargs): def make_generic_getter(t, offset): def _generic_getter(self): return t(self, offset) return _generic_getter def make_generic_setter(t, offset): def _generic_setter(self, data): t(self, offset).data = data return _generic_setter curr_offset = ElfOffset(0) for index, prop in enumerate(cls.PROPERTIES): getter = make_generic_getter(prop.type, curr_offset) setter = make_generic_setter(prop.type, curr_offset) setattr(cls, prop.name, property(getter, setter)) curr_offset += prop.type.size() super().__init__(*args, **kwargs)
def offset(self) -> ElfOffset: return ElfOffset(int(self.header.sh_offset))
def size(self) -> ElfOffset: return ElfOffset(int(self.header.sh_size))
def offset(self) -> ElfOffset: return ElfOffset(0)
def raw_write(self, data: bytearray, offset: ElfOffset): self._driver.write(data, offset.calc(self))
def raw_read(self, offset: ElfOffset, size: ElfOffset) -> bytearray: return self._driver.read(offset.calc(self), size.calc(self))
def size(cls) -> ElfOffset: """ :return: Dynamic size of the type """ return ElfOffset(struct.calcsize(cls.STRUCT))
def raw_read(self, offset: ElfOffset = ElfOffset(0), size: ElfOffset = None) -> bytearray: if size is None: size = ElfOffset(len(self)) return self.parent.raw_read(self.offset + offset, size=size)
def size(cls): return ElfOffset(dynamic=1)
def read_string(self, start_offset: int) -> ElfString: return ElfString(self, ElfOffset(start_offset))
def size(cls): return sum((prop.type.size() for prop in cls.PROPERTIES), ElfOffset())
def string(self) -> ElfString: if not self.strtab_exists: raise ValueError( f'String table with accessor {self.strtab_accessor} does not exist' ) return self.strtab.read_string(ElfOffset(self))
def data(self) -> ElfString: return ElfString(self, ElfOffset(0))