Skip to content

Commit 1788c58

Browse files
committed
Refactor
1 parent b92ed46 commit 1788c58

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

dfint64_patch/strings_context/extract_strings_with_subs.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,8 @@ def group_by_subroutines(
6868
return raw_result
6969

7070

71-
def extract_strings_grouped_by_subs(pe_file: BufferedReader) -> dict[Rva, list[StringCrossReference]]:
72-
pe = lief.PE.parse(pe_file)
73-
assert pe is not None
71+
def extract_strings_grouped_by_subs(pe: lief.PE.Binary) -> dict[SubroutineInfo, list[StringCrossReference]]:
7472
code_section = pe.sections[0]
75-
76-
image_base = pe.optional_header.imagebase
77-
7873
strings_with_xrefs = extract_strings_with_xrefs(pe)
7974

8075
subroutines = list(
@@ -89,10 +84,10 @@ def extract_strings_grouped_by_subs(pe_file: BufferedReader) -> dict[Rva, list[S
8984

9085
raw_result = group_by_subroutines(strings_with_xrefs, subroutines)
9186

92-
result: dict[Rva, list[StringCrossReference]] = {}
87+
result: dict[SubroutineInfo, list[StringCrossReference]] = {}
9388
for subroutine, string_xrefs in sorted(raw_result.items(), key=itemgetter(0)):
9489
sorted_xrefs = sorted(string_xrefs, key=lambda x: x.cross_reference)
95-
result[Rva(image_base + subroutine.start)] = sorted_xrefs
90+
result[subroutine] = sorted_xrefs
9691

9792
return result
9893

@@ -106,8 +101,12 @@ class ExtractConfig(DictConfig):
106101
@with_config(ExtractConfig, ".extract.yaml")
107102
def main(conf: ExtractConfig) -> None:
108103
with Path(conf.file_name).open("rb") as pe_file:
109-
for subroutine, strings in extract_strings_grouped_by_subs(pe_file).items():
110-
print(f"[sub_{subroutine:x}]")
104+
pe = lief.PE.parse(pe_file)
105+
assert pe is not None
106+
107+
image_base = pe.optional_header.imagebase
108+
for subroutine, strings in extract_strings_grouped_by_subs(pe).items():
109+
print(f"[sub_{image_base + subroutine.start:x}]")
111110
for string in strings:
112111
print(string.string)
113112

0 commit comments

Comments
 (0)