1 '''
2 Matchers
3
4 Various matcher classes required by crash signatures
5
6 @author: Christian Holler (:decoder)
7
8 @license:
9
10 This Source Code Form is subject to the terms of the Mozilla Public
11 License, v. 2.0. If a copy of the MPL was not distributed with this
12 file, You can obtain one at http://mozilla.org/MPL/2.0/.
13
14 @contact: choller@mozilla.com
15 '''
16
17
18 from __future__ import print_function
19
20 import re
21 from FTB.Signatures import JSONHelper
22
25 self.isPCRE = False
26 self.compiledValue = None
27
28 if isinstance(obj, str) or isinstance(obj, unicode):
29 self.value = str(obj)
30
31
32 if self.value.startswith("/") and self.value.endswith("/"):
33 self.isPCRE = True
34 self.value = self.value[1:-1]
35 self.compiledValue = re.compile(self.value)
36 else:
37 self.value = JSONHelper.getStringChecked(obj, "value", True)
38
39 matchType = JSONHelper.getStringChecked(obj, "matchType", False)
40 if matchType != None:
41 if matchType.lower() == "contains":
42 pass
43 elif matchType.lower() == "pcre":
44 self.isPCRE = True
45 self.compiledValue = re.compile(self.value)
46 else:
47 raise RuntimeError("Unknown match operator specified: %s" % matchType)
48
50 if self.isPCRE:
51 return self.compiledValue.search(val) != None
52 else:
53 return self.value in val
54
57
59 if (self.isPCRE):
60 return '/%s/' % self.value
61
62 return self.value
63
66
69 self.matchType = None
70
71 if isinstance(obj, str) or isinstance(obj, unicode):
72 if len(obj) > 0:
73 numberMatchComponents = obj.split(None, 1)
74 numIdx = 0
75
76 if len(numberMatchComponents) > 1:
77 numIdx = 1
78 matchType = numberMatchComponents[0]
79
80 if matchType == "==":
81 pass
82 elif matchType == "<":
83 self.matchType = NumberMatchType.LT
84 elif matchType == "<=":
85 self.matchType = NumberMatchType.LE
86 elif matchType == ">":
87 self.matchType = NumberMatchType.GT
88 elif matchType == ">=":
89 self.matchType = NumberMatchType.GE
90 else:
91 raise RuntimeError("Unknown match operator specified: %s" % matchType)
92
93 try:
94 self.value = long(numberMatchComponents[numIdx], 16)
95 except ValueError:
96 raise RuntimeError("Invalid number specified: %s" % numberMatchComponents[numIdx])
97 else:
98
99 self.value = None
100
101 elif isinstance(obj, int):
102 self.value = obj
103 else:
104 raise RuntimeError("Invalid type %s in NumberMatch." % type(obj))
105
107 if value == None:
108 return self.value == None
109
110 if self.matchType == NumberMatchType.GE:
111 return value >= self.value
112 elif self.matchType == NumberMatchType.GT:
113 return value > self.value
114 elif self.matchType == NumberMatchType.LE:
115 return value <= self.value
116 elif self.matchType == NumberMatchType.LT:
117 return value < self.value
118 else:
119 return value == self.value
120