Imported Upstream version 1.2.1
[packages/binwalk.git] / binwalk / filter.py
index 5192cd9..a65e535 100644 (file)
@@ -1,12 +1,14 @@
+import re
 import common
-from smartsig import SmartSignature
+from smartsignature import SmartSignature
 
 class MagicFilter:
        '''
        Class to filter libmagic results based on include/exclude rules and false positive detection.
        An instance of this class is available via the Binwalk.filter object.
+       Note that all filter strings should be in lower case.
 
-       Example code which creates include, exclude, and grep filters before running a Binwalk scan:
+       Example code which creates include, exclude, and grep filters before running a binwalk scan:
 
                import binwalk
 
@@ -58,7 +60,7 @@ class MagicFilter:
                Adds a new filter which explicitly includes results that contain
                the specified matching text.
 
-               @match     - Case insensitive text, or list of texts, to match.
+               @match     - Regex, or list of regexs, to match.
                @exclusive - If True, then results that do not explicitly contain
                             a FILTER_INCLUDE match will be excluded. If False,
                             signatures that contain the FILTER_INCLUDE match will
@@ -67,22 +69,21 @@ class MagicFilter:
                
                Returns None.
                '''
-               include_filter = {
-                               'type'          : self.FILTER_INCLUDE,
-                               'filter'        : ''
-               }
-
-               if type(match) != type([]):
+               if not isinstance(match, type([])):
                        matches = [match]
                else:
                        matches = match
 
                for m in matches:
+                       include_filter = {}
+
                        if m:
                                if exclusive and not self.exclusive_filter:
                                        self.exclusive_filter = True
 
-                               include_filter['filter'] = m.lower()
+                               include_filter['type'] = self.FILTER_INCLUDE
+                               include_filter['filter'] = m
+                               include_filter['regex'] = re.compile(m)
                                self.filters.append(include_filter)
 
        def exclude(self, match):
@@ -90,23 +91,22 @@ class MagicFilter:
                Adds a new filter which explicitly excludes results that contain
                the specified matching text.
 
-               @match - Case insensitive text, or list of texts, to match.
+               @match - Regex, or list of regexs, to match.
                
                Returns None.
                '''
-               exclude_filter = {
-                               'type'          : self.FILTER_EXCLUDE,
-                               'filter'        : ''
-               }
-
-               if type(match) != type([]):
+               if not isinstance(match, type([])):
                        matches = [match]
                else:
                        matches = match
 
                for m in matches:
+                       exclude_filter = {}
+
                        if m:
-                               exclude_filter['filter'] = m.lower()
+                               exclude_filter['type'] = self.FILTER_EXCLUDE
+                               exclude_filter['filter'] = m
+                               exclude_filter['regex'] = re.compile(m)
                                self.filters.append(exclude_filter)
 
        def filter(self, data):
@@ -124,7 +124,7 @@ class MagicFilter:
                # Loop through the filters to see if any of them are a match. 
                # If so, return the registered type for the matching filter (FILTER_INCLUDE | FILTER_EXCLUDE). 
                for f in self.filters:
-                       if f['filter'] in data:
+                       if f['regex'].search(data):
                                return f['type']
 
                # If there was not explicit match and exclusive filtering is enabled, return FILTER_EXCLUDE.
@@ -166,7 +166,7 @@ class MagicFilter:
                Add or check case-insensitive grep filters against the supplied data string.
 
                @data    - Data string to check grep filters against. Not required if filters is specified.
-               @filters - Filter, or list of filters, to add to the grep filters list. Not required if data is specified.
+               @filters - Regex, or list of regexs, to add to the grep filters list. Not required if data is specified.
 
                Returns None if data is not specified.
                If data is specified, returns True if the data contains a grep filter, or if no grep filters exist.
@@ -174,14 +174,14 @@ class MagicFilter:
                '''
                # Add any specified filters to self.grep_filters
                if filters:
-                       if type(filters) != type([]):
+                       if not isinstance(filters, type([])):
                                gfilters = [filters]
                        else:
                                gfilters = filters
 
                        for gfilter in gfilters:
                                # Filters are case insensitive
-                               self.grep_filters.append(gfilter.lower())
+                               self.grep_filters.append(re.compile(gfilter))
 
                # Check the data against all grep filters until one is found
                if data is not None:
@@ -194,7 +194,7 @@ class MagicFilter:
 
                        # If a filter exists in data, return True
                        for gfilter in self.grep_filters:
-                               if gfilter in data:
+                               if gfilter.search(data):
                                        return True
 
                        # Else, return False