Debian Codegraph @

matching/cpe_name_matcher.go

raw
1 package matching
2
3 import (
4 "strings"
5
6 "github.com/knqyf263/go-cpe/common"
7 )
8
9 // Relation is enumeration for relational values.
10 type Relation int
11
12 const (
13 // DISJOINT : disjoint
14 DISJOINT Relation = iota
15 // SUBSET : subset
16 SUBSET
17 // SUPERSET : superset
18 SUPERSET
19 // EQUAL : equal
20 EQUAL
21 // UNDEFINED : undefined
22 UNDEFINED
23 )
24
25 // IsDisjoint tests two Well Formed Names for disjointness.
26 // @param source Source WFN
27 // @param target Target WFN
28 // @return true if the names are disjoint, false otherwise
29 func IsDisjoint(source, target common.WellFormedName) bool {
30 // if any pairwise comparison is disjoint, the names are disjoint.
31 results := CompareWFNs(source, target)
32 for _, result := range results {
33 if result == DISJOINT {
34 return true
35 }
36 }
37 return false
38 }
39
40 // IsEqual tests two Well Formed Names for equality.
41 // @param source Source WFN
42 // @param target Target WFN
43 // @return true if the names are equal, false otherwise
44 func IsEqual(source, target common.WellFormedName) bool {
45 // if every pairwise comparison is equal, the names are equal.
46 results := CompareWFNs(source, target)
47 for _, result := range results {
48 if result != EQUAL {
49 return false
50 }
51 }
52 return true
53 }
54
55 // IsSubset tests if the source Well Formed Name is a subset of the target Well Formed
56 // Name.
57 // @param source Source WFN
58 // @param target Target WFN
59 // @return true if the source is a subset of the target, false otherwise
60 func IsSubset(source, target common.WellFormedName) bool {
61 // if any comparison is anything other than subset or equal, then target is
62 // not a subset of source.
63 results := CompareWFNs(source, target)
64 for _, result := range results {
65 if result != SUBSET && result != EQUAL {
66 return false
67 }
68 }
69 return true
70 }
71
72 // IsSuperset tests if the source Well Formed name is a superset of the target Well Formed Name.
73 // @param source Source WFN
74 // @param target Target WFN
75 // @return true if the source is a superset of the target, false otherwise
76 func IsSuperset(source, target common.WellFormedName) bool {
77 // if any comparison is anything other than superset or equal, then target is not
78 // a superset of source.
79 results := CompareWFNs(source, target)
80 for _, result := range results {
81 if result != SUPERSET && result != EQUAL {
82 return false
83 }
84 }
85 return true
86 }
87
88 // CompareWFNs compares each attribute value pair in two Well Formed Names.
89 // @param source Source WFN
90 // @param target Target WFN
91 // @return A Hashtable mapping attribute string to attribute value Relation
92 func CompareWFNs(source, target common.WellFormedName) map[string]Relation {
93 result := map[string]Relation{}
94 result[common.AttributePart] = compare(source.Get(common.AttributePart), target.Get(common.AttributePart))
95 result[common.AttributeVendor] = compare(source.Get(common.AttributeVendor), target.Get(common.AttributeVendor))
96 result[common.AttributeProduct] = compare(source.Get(common.AttributeProduct), target.Get(common.AttributeProduct))
97 result[common.AttributeVersion] = compare(source.Get(common.AttributeVersion), target.Get(common.AttributeVersion))
98 result[common.AttributeUpdate] = compare(source.Get(common.AttributeUpdate), target.Get(common.AttributeUpdate))
99 result[common.AttributeEdition] = compare(source.Get(common.AttributeEdition), target.Get(common.AttributeEdition))
100 result[common.AttributeLanguage] = compare(source.Get(common.AttributeLanguage), target.Get(common.AttributeLanguage))
101 result[common.AttributeSwEdition] = compare(source.Get(common.AttributeSwEdition), target.Get(common.AttributeSwEdition))
102 result[common.AttributeTargetSw] = compare(source.Get(common.AttributeTargetSw), target.Get(common.AttributeTargetSw))
103 result[common.AttributeTargetHw] = compare(source.Get(common.AttributeTargetHw), target.Get(common.AttributeTargetHw))
104 result[common.AttributeOther] = compare(source.Get(common.AttributeOther), target.Get(common.AttributeOther))
105 return result
106 }
107
108 // Compares an attribute value pair.
109 // @param source Source attribute value.
110 // @param target Target attribute value.
111 // @return The relation between the two attribute values.
112 func compare(source, target interface{}) Relation {
113 var s, t string
114 var ok bool
115
116 // matching is case insensitive, convert strings to lowercase.
117 if s, ok = source.(string); ok {
118 s = strings.ToLower(s)
119 }
120 if t, ok = target.(string); ok {
121 t = strings.ToLower(t)
122 }
123 // Unquoted wildcard characters yield an undefined result.
124 if common.ContainsWildcards(t) {
125 return UNDEFINED
126 }
127
128 // If source and target values are equal, then result is equal.
129 if source == target {
130 return EQUAL
131 }
132
133 // Check to see if source or target are Logical Values.
134 var lvSource, lvTarget common.LogicalValue
135 if lv, ok := source.(common.LogicalValue); ok {
136 lvSource = lv
137 }
138 if lv, ok := target.(common.LogicalValue); ok {
139 lvTarget = lv
140 }
141 // If source value is ANY, result is a superset.
142 if lvSource.IsANY() {
143 return SUPERSET
144 }
145 // If target value is ANY, result is a subset.
146 if lvTarget.IsANY() {
147 return SUBSET
148 }
149 // If source or target is NA, result is disjoint.
150 if lvSource.IsNA() {
151 return DISJOINT
152 }
153 // if (lvTarget != null) {
154 if lvTarget.IsNA() {
155 return DISJOINT
156 }
157 // only Strings will get to this point, not LogicalValues
158 return compareStrings(s, t)
159 }
160
161 // compareStrings compares a source string to a target string, and addresses the condition
162 // in which the source string includes unquoted special characters. It
163 // performs a simple regular expression match, with the assumption that
164 // (as required) unquoted special characters appear only at the beginning
165 // and/or the end of the source string. It also properly differentiates
166 // between unquoted and quoted special characters.
167 //
168 // @return Relation between source and target Strings.
169 func compareStrings(source, target string) Relation {
170 var start, begins, ends, index, leftover, escapes int
171 end := len(source)
172
173 if source[0] == '*' {
174 start = 1
175 begins = -1
176 } else {
177 for start < len(source) && source[start] == '?' {
178 start++
179 begins++
180 }
181 }
182
183 if source[end-1] == '*' && IsEvenWildcards(source, end-1) {
184 end--
185 ends = -1
186 } else {
187 for end > 0 && source[end-1] == '?' && IsEvenWildcards(source, end-1) {
188 end--
189 ends++
190 }
191 }
192
193 // only ? (e.g. "???")
194 if strings.Trim(source, "?") == "" {
195 if len(source) >= common.LengthWithEscapeCharacters(target) {
196 return SUPERSET
197 }
198 return DISJOINT
199 }
200 source = source[start:end]
201 index = -1
202 leftover = len(target)
203 for leftover > 0 {
204 index = common.IndexOf(target, source, index+1)
205 if index == -1 {
206 break
207 }
208 escapes = common.CountEscapeCharacters(target[:index])
209 if index > 0 && begins != -1 && begins < (index-escapes) {
210 break
211 }
212 escapes = common.CountEscapeCharacters(target[index+1:])
213 leftover = len(target) - index - escapes - len(source)
214 if leftover > 0 && ends != -1 && leftover > ends {
215 continue
216 }
217 return SUPERSET
218 }
219 return DISJOINT
220 }
221
222 // IsEvenWildcards searches a string for the backslash character
223 // @param str string to search in
224 // @param idx end index
225 // @return true if the number of backslash characters is even, false if odd
226 func IsEvenWildcards(str string, idx int) bool {
227 result := 0
228 for idx > 0 && str[idx-1] == '\\' {
229 idx--
230 result++
231 }
232 return result%2 == 0
233 }