Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions smda/utility/StringExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
# ported back from our PR to capa v4.0.0
# https://github.com/mandiant/capa/blob/v4.0.0/capa/features/extractors/smda/insn.py

# Precomputed set of printable characters for O(1) membership checks
# (string.printable is a string, so `in` does a linear scan; a set is O(1))
_PRINTABLE_CHARS = frozenset(string.printable)


def read_bytes(smda_report, va, num_bytes=None):
"""
Expand Down Expand Up @@ -64,7 +68,7 @@ def detect_ascii_len(smda_report, offset, maxlen=None):
char = smda_report.buffer[rva]
while (
char < 127
and chr(char) in string.printable
and chr(char) in _PRINTABLE_CHARS

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The char < 127 check on the preceding line is redundant. All characters in _PRINTABLE_CHARS have an ordinal value less than 127 (the maximum is ord('~') which is 126). Removing this redundant check would simplify the code and provide a minor performance improvement in this tight loop, which is consistent with the goals of this PR.

and (maxlen is None or ascii_len < maxlen)
and rva + 1 < len(smda_report.buffer)
):
Expand All @@ -87,7 +91,7 @@ def detect_unicode_len(smda_report, offset, maxlen=None):
second_char = smda_report.buffer[rva + 1]
while (
char < 127
and chr(char) in string.printable
and chr(char) in _PRINTABLE_CHARS

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to detect_ascii_len, the char < 127 check on the preceding line is redundant here as well. All characters in _PRINTABLE_CHARS have an ordinal value less than 127. Removing this check would be a good follow-up optimization.

and second_char == 0
and (maxlen is None or unicode_len < 2 * maxlen)
and rva + 3 < len(smda_report.buffer)
Expand Down
Loading