Files
volatility3/volatility/framework/layers/scanners/multiregexp.py
T
Mike Auty 29d41470a4 Mass reformat of typing imports
Relented on the strict import of direct objects/classes for the typing
module only.  Typing module components can be directly imported because
it makes the code really painful to read and write otherwise.

This is still in-line with the python style guide adopted from Google at
http://google.github.io/styleguide/pyguide.html section 2.2.
2018-12-16 13:04:22 +00:00

30 lines
914 B
Python

import re
from typing import Generator, List, Tuple, Union
class MultiRegexp(object):
"""Algorithm for multi-string matching"""
def __init__(self) -> None:
self._pattern_strings = [] # type: List[bytes]
self._regex = re.compile(b'')
def add_pattern(self, pattern: bytes) -> None:
self._pattern_strings.append(pattern)
def preprocess(self) -> None:
self._regex = re.compile(b'|'.join(map(re.escape, self._pattern_strings)))
def search(self, haystack: bytes) \
-> Generator[Tuple[int, Union[str, bytes]], None, None]:
if not isinstance(haystack, bytes):
raise TypeError("Search haystack must be a byte string")
for match in re.finditer(self._regex, haystack):
yield (match.start(0), match.group())
if __name__ == '__main__':
import multistring_testrig
multistring_testrig.tester(MultiRegexp())