Skip to content

Commit b48a82c

Browse files
committed
Style refactor
* Refactored based on comments given * Ran `ruff` for formatting * Applied style and formatting on `dissect.executable.elf` as well (hope you don't mind)
1 parent 137d17b commit b48a82c

File tree

19 files changed

+569
-322
lines changed

19 files changed

+569
-322
lines changed

dissect/executable/elf/elf.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def __init__(self, fh: BinaryIO):
4242
self.header = self.c_elf.Ehdr(fh)
4343
self.segments = SegmentTable.from_elf(self)
4444
self.section_table = SectionTable.from_elf(self)
45-
self.symbol_tables: list[SymbolTable] = self.section_table.by_type([SHT.SYMTAB, SHT.DYNSYM])
45+
self.symbol_tables: list[SymbolTable] = self.section_table.by_type(
46+
[SHT.SYMTAB, SHT.DYNSYM]
47+
)
4648

4749
def __repr__(self) -> str:
4850
return str(self.header)
@@ -98,7 +100,9 @@ def find(self, condition: Callable, **kwargs) -> list[T]:
98100

99101

100102
class Section:
101-
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
103+
def __init__(
104+
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
105+
):
102106
self.fh = fh
103107
self.idx = idx
104108

@@ -224,7 +228,9 @@ def dump_data(self) -> list[tuple[int, bytes]]:
224228

225229

226230
class Segment:
227-
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
231+
def __init__(
232+
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
233+
):
228234
self.fh = fh
229235
self.idx = idx
230236
self.c_elf = c_elf
@@ -246,7 +252,9 @@ def __repr__(self) -> str:
246252
return repr(self.header)
247253

248254
@classmethod
249-
def from_segment_table(cls, table: SegmentTable, idx: Optional[int] = None) -> Segment:
255+
def from_segment_table(
256+
cls, table: SegmentTable, idx: Optional[int] = None
257+
) -> Segment:
250258
fh = table.fh
251259
return cls(fh, idx, table.c_elf)
252260

@@ -277,7 +285,14 @@ def patch(self, new_data: bytes) -> None:
277285

278286

279287
class SegmentTable(Table[Segment]):
280-
def __init__(self, fh: BinaryIO, offset: int, entries: int, size: int, c_elf: cstruct = c_elf_64):
288+
def __init__(
289+
self,
290+
fh: BinaryIO,
291+
offset: int,
292+
entries: int,
293+
size: int,
294+
c_elf: cstruct = c_elf_64,
295+
):
281296
super().__init__(entries)
282297
self.fh = fh
283298
self.offset = offset
@@ -297,7 +312,9 @@ def from_elf(cls, elf: ELF) -> SegmentTable:
297312
offset = header.e_phoff
298313
entries = header.e_phnum
299314
size = header.e_phentsize
300-
return cls(fh=elf.fh, offset=offset, entries=entries, size=size, c_elf=elf.c_elf)
315+
return cls(
316+
fh=elf.fh, offset=offset, entries=entries, size=size, c_elf=elf.c_elf
317+
)
301318

302319
def related_segments(self, section: Section) -> list[Segment]:
303320
return self.find(lambda x: x.is_related(section))
@@ -318,7 +335,9 @@ def dump_table(self) -> tuple[int, bytearray]:
318335

319336

320337
class StringTable(Section):
321-
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
338+
def __init__(
339+
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
340+
):
322341
super().__init__(fh, idx, c_elf)
323342

324343
self._get_string = lru_cache(256)(self._get_string)
@@ -333,7 +352,9 @@ def _get_string(self, index: int) -> str:
333352

334353

335354
class Symbol:
336-
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
355+
def __init__(
356+
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
357+
):
337358
self.symbol = c_elf.Sym(fh)
338359
self.idx = idx
339360
self.c_elf = c_elf
@@ -388,7 +409,9 @@ def value_based_on_shndx(self, table: SectionTable) -> int:
388409

389410

390411
class SymbolTable(Section, Table[Symbol]):
391-
def __init__(self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64):
412+
def __init__(
413+
self, fh: BinaryIO, idx: Optional[int] = None, c_elf: cstruct = c_elf_64
414+
):
392415
# Initializes Section info
393416
Section.__init__(self, fh, idx, c_elf)
394417
count = self.size // self.entry_size

dissect/executable/pe/helpers/c_pe.py renamed to dissect/executable/pe/c_pe.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dissect.cstruct import cstruct
22

3-
pe_def = """
3+
c_pe_def = """
44
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
55
#define IMAGE_SIZEOF_SHORT_NAME 8
66
@@ -93,12 +93,12 @@
9393
};
9494
9595
typedef struct IMAGE_FILE_HEADER {
96-
MachineType Machine;
97-
WORD NumberOfSections;
98-
DWORD TimeDateStamp;
99-
DWORD PointerToSymbolTable;
100-
DWORD NumberOfSymbols;
101-
WORD SizeOfOptionalHeader;
96+
MachineType Machine;
97+
WORD NumberOfSections;
98+
DWORD TimeDateStamp;
99+
DWORD PointerToSymbolTable;
100+
DWORD NumberOfSymbols;
101+
WORD SizeOfOptionalHeader;
102102
ImageCharacteristics Characteristics;
103103
};
104104
@@ -212,9 +212,9 @@
212212
};
213213
214214
typedef struct IMAGE_NT_HEADERS64 {
215-
DWORD Signature;
216-
IMAGE_FILE_HEADER FileHeader;
217-
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
215+
DWORD Signature;
216+
IMAGE_FILE_HEADER FileHeader;
217+
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
218218
};
219219
220220
flag SectionFlags : DWORD {
@@ -258,16 +258,16 @@
258258
};
259259
260260
typedef struct IMAGE_SECTION_HEADER {
261-
char Name[IMAGE_SIZEOF_SHORT_NAME];
262-
ULONG VirtualSize;
263-
ULONG VirtualAddress;
264-
ULONG SizeOfRawData;
265-
ULONG PointerToRawData;
266-
ULONG PointerToRelocations;
267-
ULONG PointerToLinenumbers;
268-
USHORT NumberOfRelocations;
269-
USHORT NumberOfLinenumbers;
270-
SectionFlags Characteristics;
261+
char Name[IMAGE_SIZEOF_SHORT_NAME];
262+
ULONG VirtualSize;
263+
ULONG VirtualAddress;
264+
ULONG SizeOfRawData;
265+
ULONG PointerToRawData;
266+
ULONG PointerToRelocations;
267+
ULONG PointerToLinenumbers;
268+
USHORT NumberOfRelocations;
269+
USHORT NumberOfLinenumbers;
270+
SectionFlags Characteristics;
271271
};
272272
273273
// --- END OF PE HEADERS
@@ -277,8 +277,8 @@
277277
typedef struct IMAGE_EXPORT_DIRECTORY {
278278
ULONG Characteristics;
279279
ULONG TimeDateStamp;
280-
USHORT MajorVersion;
281-
USHORT MinorVersion;
280+
USHORT MajorVersion;
281+
USHORT MinorVersion;
282282
ULONG Name;
283283
ULONG Base;
284284
ULONG NumberOfFunctions;
@@ -441,12 +441,9 @@
441441
// --- END OF TLS DIRECTORY
442442
"""
443443

444+
c_pe = cstruct().load(c_pe_def)
444445

445-
pestruct = cstruct()
446-
pestruct.load(pe_def)
447-
448-
449-
cv_info_def = """
446+
c_cv_info_def = """
450447
struct GUID {
451448
DWORD Data1;
452449
WORD Data2;
@@ -462,5 +459,4 @@
462459
};
463460
"""
464461

465-
cv_info_struct = cstruct()
466-
cv_info_struct.load(cv_info_def)
462+
c_cv_info = cstruct().load(c_cv_info_def)

0 commit comments

Comments
 (0)