1 # Copyright 2016, Tresys Technology, LLC 2 # 3 # This file is part of SETools. 4 # 5 # SETools is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Lesser General Public License as 7 # published by the Free Software Foundation, either version 2.1 of 8 # the License, or (at your option) any later version. 9 # 10 # SETools is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Lesser General Public License for more details. 14 # 15 # You should have received a copy of the GNU Lesser General Public 16 # License along with SETools. If not, see 17 # <http://www.gnu.org/licenses/>. 18 # 19 20 21 def match_regex(obj, criteria, regex): 22 """ 23 Match the object with optional regular expression. 24 25 Parameters: 26 obj The object to match. 27 criteria The criteria to match. 28 regex If regular expression matching should be used. 29 """ 30 31 if regex: 32 return bool(criteria.search(str(obj))) 33 else: 34 return obj == criteria 35 36 37 def match_set(obj, criteria, equal): 38 """ 39 Match the object (a set) with optional set equality. 40 41 Parameters: 42 obj The object to match. (a set) 43 criteria The criteria to match. (a set) 44 equal If set equality should be used. Otherwise 45 any set intersection will match. 46 """ 47 48 if equal: 49 return obj == criteria 50 else: 51 return bool(obj.intersection(criteria)) 52 53 54 def match_in_set(obj, criteria, regex): 55 """ 56 Match if the criteria is in the list, with optional 57 regular expression matching. 58 59 Parameters: 60 obj The object to match. 61 criteria The criteria to match. 62 regex If regular expression matching should be used. 63 """ 64 65 if regex: 66 return [m for m in obj if criteria.search(str(m))] 67 else: 68 return criteria in obj 69 70 71 def match_indirect_regex(obj, criteria, indirect, regex): 72 """ 73 Match the object with optional regular expression and indirection. 74 75 Parameters: 76 obj The object to match. 77 criteria The criteria to match. 78 regex If regular expression matching should be used. 79 indirect If object indirection should be used, e.g. 80 expanding an attribute. 81 """ 82 83 if indirect: 84 if regex: 85 return [o for o in obj.expand() if criteria.search(str(o))] 86 else: 87 return set(criteria.expand()).intersection(obj.expand()) 88 else: 89 return match_regex(obj, criteria, regex) 90 91 92 def match_regex_or_set(obj, criteria, equal, regex): 93 """ 94 Match the object (a set) with either set comparisons 95 (equality or intersection) or by regex matching of the 96 set members. Regular expression matching will override 97 the set equality option. 98 99 Parameters: 100 obj The object to match. (a set) 101 criteria The criteria to match. 102 equal If set equality should be used. Otherwise 103 any set intersection will match. Ignored 104 if regular expression matching is used. 105 regex If regular expression matching should be used. 106 """ 107 108 if regex: 109 return [m for m in obj if criteria.search(str(m))] 110 else: 111 return match_set(obj, set(criteria), equal) 112 113 114 def match_range(obj, criteria, subset, overlap, superset, proper): 115 """ 116 Match ranges of objects. 117 118 obj An object with attributes named "low" and "high", representing the range. 119 criteria An object with attributes named "low" and "high", representing the criteria. 120 subset If true, the criteria will match if it is a subset obj's range. 121 overlap If true, the criteria will match if it overlaps any of the obj's range. 122 superset If true, the criteria will match if it is a superset of the obj's range. 123 proper If true, use proper superset/subset operations. 124 No effect if not using set operations. 125 """ 126 127 if overlap: 128 return ((obj.low <= criteria.low <= obj.high) or ( 129 obj.low <= criteria.high <= obj.high) or ( 130 criteria.low <= obj.low and obj.high <= criteria.high)) 131 elif subset: 132 if proper: 133 return ((obj.low < criteria.low and criteria.high <= obj.high) or ( 134 obj.low <= criteria.low and criteria.high < obj.high)) 135 else: 136 return obj.low <= criteria.low and criteria.high <= obj.high 137 elif superset: 138 if proper: 139 return ((criteria.low < obj.low and obj.high <= criteria.high) or ( 140 criteria.low <= obj.low and obj.high < criteria.high)) 141 else: 142 return (criteria.low <= obj.low and obj.high <= criteria.high) 143 else: 144 return criteria.low == obj.low and obj.high == criteria.high 145 146 147 def match_level(obj, criteria, dom, domby, incomp): 148 """ 149 Match the an MLS level. 150 151 obj The level to match. 152 criteria The criteria to match. (a level) 153 dom If true, the criteria will match if it dominates obj. 154 domby If true, the criteria will match if it is dominated by obj. 155 incomp If true, the criteria will match if it is incomparable to obj. 156 """ 157 158 if dom: 159 return (criteria >= obj) 160 elif domby: 161 return (criteria <= obj) 162 elif incomp: 163 return (criteria ^ obj) 164 else: 165 return (criteria == obj) 166