Debian Codegraph @

common/well_formed_name.go

raw
1 package common
2
3 import (
4 "fmt"
5 "strings"
6 "unicode"
7
8 "github.com/pkg/errors"
9 )
10
11 const (
12 AttributePart = "part"
13 AttributeVendor = "vendor"
14 AttributeProduct = "product"
15 AttributeVersion = "version"
16 AttributeUpdate = "update"
17 AttributeEdition = "edition"
18 AttributeLanguage = "language"
19 AttributeSwEdition = "sw_edition"
20 AttributeTargetSw = "target_sw"
21 AttributeTargetHw = "target_hw"
22 AttributeOther = "other"
23 )
24
25 var (
26 attributes = []string{AttributePart, AttributeVendor, AttributeProduct, AttributeVersion, AttributeUpdate,
27 AttributeEdition, AttributeLanguage, AttributeSwEdition, AttributeTargetSw, AttributeTargetHw, AttributeOther}
28
29 // ErrIllegalAttribute is returned when illegal argument
30 ErrIllegalAttribute = errors.New("Illegal attribute")
31 // ErrParse is returned when parse error
32 ErrParse = errors.New("Parse error")
33 )
34
35 // WellFormedName represents a Well Formed Name, as defined
36 // in the CPE Specification version 2.3.
37 //
38 // @see <a href="http://cpe.mitre.org">cpe.mitre.org</a> for details.
39 type WellFormedName map[string]interface{}
40
41 // NewWellFormedName constructs a new WellFormedName object, with all components set to the default value "ANY".
42 func NewWellFormedName() WellFormedName {
43 wfn := WellFormedName{}
44 for _, a := range attributes {
45 if a != AttributePart {
46 wfn[a], _ = NewLogicalValue("ANY")
47 }
48 }
49 return wfn
50 }
51
52 // Initialize sets each component to the given parameter value.
53 // If a parameter is null, the component is set to the default value "ANY".
54 // @param part string representing the part component
55 // @param vendor string representing the vendor component
56 // @param product string representing the product component
57 // @param version string representing the version component
58 // @param update string representing the update component
59 // @param edition string representing the edition component
60 // @param language string representing the language component
61 // @param sw_edition string representing the sw_edition component
62 // @param target_sw string representing the target_sw component
63 // @param target_hw string representing the target_hw component
64 // @param other string representing the other component
65 func (wfn WellFormedName) Initialize(part, vendor, product, version, update, edition, language, swEdition, targetSw, targetHw, other interface{}) {
66 wfn[AttributePart] = part
67 wfn[AttributeVendor] = vendor
68 wfn[AttributeProduct] = product
69 wfn[AttributeVersion] = version
70 wfn[AttributeUpdate] = update
71 wfn[AttributeEdition] = edition
72 wfn[AttributeLanguage] = language
73 wfn[AttributeSwEdition] = swEdition
74 wfn[AttributeTargetSw] = targetSw
75 wfn[AttributeTargetHw] = targetHw
76 wfn[AttributeOther] = other
77 }
78
79 // Get gets attribute
80 // @param attribute String representing the component value to get
81 // @return the String value of the given component, or default value "ANY"
82 // if the component does not exist
83 func (wfn WellFormedName) Get(attribute string) interface{} {
84 if v, ok := wfn[attribute]; ok {
85 return v
86 }
87 any, _ := NewLogicalValue("ANY")
88 return any
89 }
90
91 // Set sets the given attribute to value, if the attribute is in the list of permissible components
92 // @param attribute String representing the component to set
93 // @param value Object representing the value of the given component
94 func (wfn WellFormedName) Set(attribute string, value interface{}) (err error) {
95 if valid := IsValidAttribute(attribute); !valid {
96 return ErrIllegalAttribute
97 }
98
99 if value == nil {
100 wfn[attribute], _ = NewLogicalValue("ANY")
101 return nil
102 }
103
104 if _, ok := value.(LogicalValue); ok {
105 if attribute == AttributePart {
106 return errors.Wrap(ErrIllegalAttribute, "part component cannot be a logical value")
107 }
108 wfn[attribute] = value
109 return nil
110 }
111
112 svalue, ok := value.(string)
113 if !ok {
114 return errors.Wrap(ErrIllegalAttribute, "value must be a logical value or string")
115 }
116
117 if err = ValidateStringValue(svalue); err != nil {
118 return errors.Wrap(err, "Failed to validate a value")
119 }
120
121 // part must be a, o, or h
122 if attribute == AttributePart {
123 if svalue != "a" && svalue != "o" && svalue != "h" {
124 return errors.Wrapf(ErrParse, "part component must be one of the following: 'a', 'o', 'h': %s", svalue)
125 }
126 }
127
128 // should be good to go
129 wfn[attribute] = value
130
131 return nil
132 }
133
134 // GetString gets attribute as string
135 // @param attribute String representing the component value to get
136 // @return the String value of the given component, or default value "ANY"
137 // if the component does not exist
138 func (wfn WellFormedName) GetString(attribute string) string {
139 return fmt.Sprintf("%s", wfn.Get(attribute))
140 }
141
142 // String returns string representation of the WellFormedName
143 func (wfn WellFormedName) String() string {
144 s := "wfn:["
145 for _, attr := range attributes {
146 s += fmt.Sprintf("%s=", attr)
147 o := wfn.Get(attr)
148 if lv, ok := o.(LogicalValue); ok {
149 s += fmt.Sprintf("%s, ", lv)
150 } else {
151 s += fmt.Sprintf("\"%s\", ", o)
152 }
153 }
154 s = strings.TrimSpace(s)
155 s = strings.TrimRight(s, ",")
156 s += "]"
157 return s
158 }
159
160 // IsValidAttribute validates an attribute name
161 func IsValidAttribute(attribute string) (valid bool) {
162 for _, a := range attributes {
163 if a == attribute {
164 valid = true
165 }
166 }
167 return valid
168 }
169
170 // ValidateStringValue validates an string value
171 func ValidateStringValue(svalue string) (err error) {
172 // svalue has more than one unquoted star
173 if strings.HasPrefix(svalue, "**") || strings.HasSuffix(svalue, "**") {
174 return errors.Wrapf(ErrParse, "component cannot contain more than one * in sequence: %s", svalue)
175 }
176
177 prev := ' ' // dummy value
178 for i, r := range svalue {
179 // check for printable characters - no control characters
180 if !unicode.IsPrint(r) {
181 return errors.Wrapf(ErrParse, "encountered non printable character in: %s", svalue)
182 }
183 // svalue has whitespace
184 if unicode.IsSpace(r) {
185 return errors.Wrapf(ErrParse, "component cannot contain whitespace:: %s", svalue)
186 }
187 if unicode.IsPunct(r) && prev != '\\' && r != '\\' {
188 // svalue has an unquoted *
189 if r == '*' && (i != 0 && i != len(svalue)-1) {
190 return errors.Wrapf(ErrParse, "component cannot contain embedded *: %s", svalue)
191 }
192
193 if r != '*' && r != '?' && r != '_' {
194 // svalue has unquoted punctuation embedded
195 return errors.Wrapf(ErrParse, "component cannot contain unquoted punctuation: %s", svalue)
196 }
197 }
198 prev = r
199 }
200
201 if strings.Contains(svalue, "?") {
202 if svalue == "?" {
203 // single ? is valid
204 return nil
205 }
206
207 s := strings.Trim(svalue, "?")
208 if ContainsQuestions(s) {
209 return errors.Wrapf(ErrParse, "component cannot contain embedded ?: %s", svalue)
210 }
211 }
212
213 // single asterisk is not allowed
214 if svalue == "*" {
215 return errors.Wrapf(ErrParse, "component cannot be a single *: %s", svalue)
216 }
217
218 // quoted hyphen not allowed by itself
219 if svalue == `\-` {
220 return errors.Wrapf(ErrParse, "component cannot be quoted hyphen: %s", svalue)
221 }
222
223 return nil
224 }