forked from danielplohmann/smda
-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Use frozenset for printable chars to enable O(1) membership checks in string detection loops #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
β‘ Use frozenset for printable chars to enable O(1) membership checks in string detection loops #29
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
| """ | ||
|
|
@@ -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 | ||
| and (maxlen is None or ascii_len < maxlen) | ||
| and rva + 1 < len(smda_report.buffer) | ||
| ): | ||
|
|
@@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| and second_char == 0 | ||
| and (maxlen is None or unicode_len < 2 * maxlen) | ||
| and rva + 3 < len(smda_report.buffer) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
char < 127check on the preceding line is redundant. All characters in_PRINTABLE_CHARShave an ordinal value less than 127 (the maximum isord('~')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.