-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnaming.go
452 lines (388 loc) · 10.1 KB
/
naming.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package cpe
import (
"fmt"
"strings"
)
// Item reprecents a CPE item.
type Item struct {
part PartAttr
vendor StringAttr
product StringAttr
version StringAttr
update StringAttr
edition StringAttr
language StringAttr
sw_edition StringAttr
target_sw StringAttr
target_hw StringAttr
other StringAttr
}
// NewItem returns empty Item.
func NewItem() *Item {
return &Item{
part: PartNotSet,
vendor: Any,
product: Any,
version: Any,
update: Any,
edition: Any,
language: Any,
sw_edition: Any,
target_sw: Any,
target_hw: Any,
other: Any,
}
}
var goCpeOriginalDelim = "[goCpeOriginalDelim]"
func NewItemFromWfn(wfn string) (*Item, error) {
if strings.HasPrefix(wfn, "wfn:[") {
wfn = strings.TrimPrefix(wfn, "wfn:[")
} else {
return nil, cpeerr{reason: err_invalid_wfn}
}
if strings.HasSuffix(wfn, "]") {
wfn = strings.TrimSuffix(wfn, "]")
} else {
return nil, cpeerr{reason: err_invalid_wfn}
}
item := NewItem()
for _, attr := range strings.Split(wfn, ",") {
sepattr := strings.Split(attr, "=")
if len(sepattr) != 2 {
return nil, cpeerr{reason: err_invalid_wfn}
}
n, v := sepattr[0], sepattr[1]
switch n {
case "part":
item.part = newPartAttrFromWfnEncoded(v)
case "vendor":
item.vendor = newStringAttrFromWfnEncoded(v)
case "product":
item.product = newStringAttrFromWfnEncoded(v)
case "version":
item.version = newStringAttrFromWfnEncoded(v)
case "update":
item.update = newStringAttrFromWfnEncoded(v)
case "edition":
item.edition = newStringAttrFromWfnEncoded(v)
case "language":
item.language = newStringAttrFromWfnEncoded(v)
case "sw_edition":
item.sw_edition = newStringAttrFromWfnEncoded(v)
case "target_sw":
item.target_sw = newStringAttrFromWfnEncoded(v)
case "target_hw":
item.target_hw = newStringAttrFromWfnEncoded(v)
case "other":
item.other = newStringAttrFromWfnEncoded(v)
}
}
return item, nil
}
func NewItemFromUri(uri string) (*Item, error) {
if strings.HasPrefix(uri, "cpe:/") {
uri = strings.TrimPrefix(uri, "cpe:/")
} else {
return nil, cpeerr{reason: err_invalid_wfn}
}
item := NewItem()
for i, attr := range strings.Split(uri, ":") {
switch i {
case 0:
item.part = newPartAttrFromUriEncoded(attr)
case 1:
item.vendor = newStringAttrFromUriEncoded(attr)
case 2:
item.product = newStringAttrFromUriEncoded(attr)
case 3:
item.version = newStringAttrFromUriEncoded(attr)
case 4:
item.update = newStringAttrFromUriEncoded(attr)
case 5:
editions := strings.Split(attr, "~")
if len(editions) == 1 {
item.edition = newStringAttrFromUriEncoded(editions[0])
} else if len(editions) == 6 {
item.edition = newStringAttrFromUriEncoded(editions[1])
item.sw_edition = newStringAttrFromUriEncoded(editions[2])
item.target_sw = newStringAttrFromUriEncoded(editions[3])
item.target_hw = newStringAttrFromUriEncoded(editions[4])
item.other = newStringAttrFromUriEncoded(editions[5])
} else {
return nil, cpeerr{reason: err_invalid_wfn}
}
}
}
return item, nil
}
func NewItemFromFormattedString(str string) (*Item, error) {
if strings.HasPrefix(str, "cpe:2.3:") {
str = replaceToDelim(strings.TrimPrefix(str, "cpe:2.3:"))
} else {
return nil, cpeerr{reason: err_invalid_wfn}
}
attrs := strings.Split(str, ":")
if len(attrs) != 11 {
return nil, cpeerr{reason: err_invalid_wfn}
}
item := NewItem()
for i, attr := range attrs {
switch i {
case 0:
item.part = newPartAttrFromFmtEncoded(replaceFromDelim(attr))
case 1:
item.vendor = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 2:
item.product = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 3:
item.version = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 4:
item.update = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 5:
item.edition = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 6:
item.language = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 7:
item.sw_edition = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 8:
item.target_sw = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 9:
item.target_hw = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
case 10:
item.other = newStringAttrFromFmtEncoded(replaceFromDelim(attr))
}
}
return item, nil
}
// Wfn returns a string of Well-Formed string data model.
func (m *Item) Wfn() string {
wfn := "wfn:["
first := true
for _, it := range []struct {
name string
attr Attribute
}{
{"part", m.part},
{"vendor", m.vendor},
{"product", m.product},
{"version", m.version},
{"update", m.update},
{"edition", m.edition},
{"language", m.language},
{"sw_edition", m.sw_edition},
{"target_sw", m.target_sw},
{"target_hw", m.target_hw},
{"other", m.other},
} {
if !it.attr.IsEmpty() {
if first {
first = false
} else {
wfn += ","
}
wfn += it.name + "=" + it.attr.wfnEncoded()
}
}
wfn += "]"
return wfn
}
// Wfn returns a string of uri binding.
func (m *Item) Uri() string {
uri := "cpe:/"
l := []struct {
name string
attr Attribute
}{
{"part", m.part},
{"vendor", m.vendor},
{"product", m.product},
{"version", m.version},
{"update", m.update},
}
for c, it := range l {
if !it.attr.IsEmpty() {
uri += it.attr.urlEncoded()
}
if c+1 != len(l) {
uri += ":"
}
}
if m.target_hw.urlEncoded() != "" ||
m.target_sw.urlEncoded() != "" ||
m.sw_edition.urlEncoded() != "" ||
m.other.urlEncoded() != "" {
uri += ":~" + m.edition.urlEncoded()
uri += "~" + m.sw_edition.urlEncoded()
uri += "~" + m.target_sw.urlEncoded()
uri += "~" + m.target_hw.urlEncoded()
uri += "~" + m.other.urlEncoded()
} else {
uri += ":" + m.edition.urlEncoded()
}
uri += ":" + m.language.urlEncoded()
return strings.TrimRight(uri, ":*")
}
// Wfn returns a formatted string binding.
func (m *Item) Formatted() string {
fmted := "cpe:2.3"
for _, it := range []Attribute{
m.part, m.vendor, m.product, m.version, m.update, m.edition, m.language, m.sw_edition, m.target_sw, m.target_hw, m.other,
} {
if !it.IsEmpty() {
fmted += ":" + it.fmtString()
} else {
fmted += ":*"
}
}
return fmted
}
// SetPart sets part of item. returns error if p is invalid.
func (i *Item) SetPart(p PartAttr) error {
if !p.IsValid() {
return cpeerr{reason: err_invalid_type, attr: []interface{}{p, "part"}}
}
i.part = p
return nil
}
// Part returns part of item.
func (i *Item) Part() PartAttr {
return i.part
}
// SetVendor sets vendor of item. returns error if s is invalid.
func (i *Item) SetVendor(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.vendor = s
return nil
}
// Vendor returns vendor of item.
func (i *Item) Vendor() StringAttr {
return i.vendor
}
// SetProduct sets vendor of item. returns error if s is invalid.
func (i *Item) SetProduct(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.product = s
return nil
}
// Vendor returns product of item.
func (i *Item) Product() StringAttr {
return i.product
}
// SetVersion sets version of item. returns error if s is invalid.
func (i *Item) SetVersion(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.version = s
return nil
}
// Version returns version of item.
func (i *Item) Version() StringAttr {
return i.version
}
// SetUpdate sets update of item. returns error if s is invalid.
func (i *Item) SetUpdate(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.update = s
return nil
}
// Update returns update of item.
func (i *Item) Update() StringAttr {
return i.update
}
// SetEdition sets edition of item. returns error if s is invalid.
func (i *Item) SetEdition(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.edition = s
return nil
}
// Edition returns edition of item.
func (i *Item) Edition() StringAttr {
return i.edition
}
// SetLanguage sets language of item. returns error if s is invalid.
func (i *Item) SetLanguage(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.language = s
return nil
}
// Language returns language of item.
func (i *Item) Language() StringAttr {
return i.language
}
// SetSwEdition sets sw_edition of item. returns error if s is invalid.
func (i *Item) SetSwEdition(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.sw_edition = s
return nil
}
// SwEdition returns sw_edition of item.
func (i *Item) SwEdition() StringAttr {
return i.sw_edition
}
// SetTargetSw sets target_sw of item. returns error if s is invalid.
func (i *Item) SetTargetSw(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.target_sw = s
return nil
}
// TargetSw returns target_sw of item.
func (i *Item) TargetSw() StringAttr {
return i.target_sw
}
// SetTargetHw sets target_hw of item. returns error if s is invalid.
func (i *Item) SetTargetHw(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.target_hw = s
return nil
}
// TargetHw returns target_hw of item.
func (i *Item) TargetHw() StringAttr {
return i.target_hw
}
// SetOther sets other of item. returns error if s is invalid.
func (i *Item) SetOther(s StringAttr) error {
if !s.IsValid() {
return cpeerr{reason: err_invalid_attribute_str}
}
i.other = s
return nil
}
// Other returns other of item.
func (i *Item) Other() StringAttr {
return i.other
}
func replaceToDelim(str string) string {
return strings.Replace(str, "\\:", goCpeOriginalDelim, -1)
}
func replaceFromDelim(str string) string {
return strings.Replace(str, goCpeOriginalDelim, "\\:", -1)
}
type cpeerr struct {
reason string
attr []interface{}
}
var (
err_invalid_type = "\"%#v\" is not valid as %v attribute."
err_invalid_attribute_str = "invalid attribute string."
err_invalid_wfn = "invalid wfn string."
)
func (e cpeerr) Error() string {
return fmt.Sprintf("cpe:"+e.reason, e.attr...)
}