forked from quay/claircore
/
unbind.go
216 lines (205 loc) · 4.57 KB
/
unbind.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
package cpe
import (
"fmt"
"strings"
)
const (
cpe22Prefix = `cpe:/`
cpe23Prefix = `cpe:2.3:`
)
// Unbind attempts to unbind a string regardless of it be a formatted string or
// URI.
func Unbind(s string) (WFN, error) {
switch {
case strings.HasPrefix(s, cpe22Prefix):
return UnbindURI(s)
case strings.HasPrefix(s, cpe23Prefix):
return UnbindFS(s)
default:
}
return WFN{}, fmt.Errorf("cpe: string does not appear to be a bound WFN")
}
// MustUnbind calls Unbind on the provided string, but panics if any errors are
// encountered.
//
// This is primarily useful for static data where any error is a programmer
// error.
func MustUnbind(s string) WFN {
w, err := Unbind(s)
if err != nil {
panic(err)
}
return w
}
// UnbindURI attempts to unbind a string as CPE 2.2 URI into a WFN.
//
// This function supports unpacking attributes from the "edition" component as
// specified in CPE 2.3.
func UnbindURI(s string) (WFN, error) {
r := WFN{}
if !strings.HasPrefix(s, cpe22Prefix) {
return r, fmt.Errorf("cpe: malformed CPE URI")
}
// URI form allows parts to be elided, so set all the standard components to
// a default of "ANY".
attrs := [...]Attribute{Part, Vendor, Product, Version, Update, Edition, Language}
for _, a := range attrs {
r.Attr[a].Kind = ValueAny
}
var b strings.Builder
// URI form percent-encodes instead of backslash-escaping, so splitting is
// easier than FS form.
comp := strings.Split(s, ":")
// The second component has a slash prefix.
comp[1] = strings.TrimPrefix(comp[1], "/")
for i, c := range comp[1:] {
if i >= len(attrs) {
return r, fmt.Errorf("cpe: unexpected %dth component", i)
}
if i == 5 && strings.HasPrefix(c, "~") {
attrs := [...]Attribute{Edition, SwEdition, TargetSW, TargetHW, Other}
for i, c := range strings.SplitN(c, `~`, 6)[1:] {
r.Attr[attrs[i]].unbindURI(&b, c)
}
continue
}
r.Attr[attrs[i]].unbindURI(&b, c)
}
return r, r.Valid()
}
func (v *Value) unbindURI(b *strings.Builder, s string) {
if b == nil {
b = &strings.Builder{}
b.Grow(len(s))
}
b.Reset()
switch s {
case ``:
v.Kind = ValueAny
case `-`:
v.Kind = ValueNA
default:
v.Kind = ValueSet
valueURI.WriteString(b, strings.ToLower(s))
v.V = b.String()
}
}
// ValueURI is a replace that undoes the URI percent encoding.
var valueURI = strings.NewReplacer(
`.`, `\.`,
`-`, `\-`,
`~`, `\~`,
// The specified algorithm sticks validation logic for * and ? in the
// unquoting. We skip that and just make sure to validate later.
`%01`, `?`,
`%02`, `*`,
`%21`, `\!`,
`%22`, `\"`,
`%23`, `\#`,
`%24`, `\$`,
`%25`, `\%`,
`%26`, `\&`,
`%27`, `\'`,
`%28`, `\(`,
`%29`, `\)`,
`%2a`, `\*`,
`%2b`, `\+`,
`%2c`, `\,`,
`%2f`, `\/`,
`%3a`, `\:`,
`%3b`, `\;`,
`%3c`, `\<`,
`%3d`, `\=`,
`%3e`, `\>`,
`%3f`, `\?`,
`%40`, `\@`,
`%5b`, `\[`,
`%5c`, `\\`,
`%5d`, `\]`,
`%5e`, `\^`,
`%60`, "\\`",
`%7b`, `\{`,
`%7c`, `\|`,
`%7d`, `\}`,
`%7e`, `\~`,
)
// UnbindFS attempts to unbind a string as CPE 2.3 formatted string into a WFN.
func UnbindFS(s string) (WFN, error) {
r := WFN{}
if !strings.HasPrefix(s, cpe23Prefix) {
return r, fmt.Errorf("cpe: malformed CPE formatted string")
}
fs := splitFS(s)
var b strings.Builder
for i, c := range fs[2:] { // Skip the first two segments, "cpe" and "2.3".
r.Attr[i].unbindFS(&b, c)
}
return r, r.Valid()
}
// UnbindFS undoes the FS binding and assigns it to v.
func (v *Value) unbindFS(b *strings.Builder, s string) {
if b == nil {
b = &strings.Builder{}
b.Grow(len(s))
}
switch s {
case ``:
v.Kind = ValueUnset
case `-`:
v.Kind = ValueNA
case `*`:
v.Kind = ValueAny
default:
v.Kind = ValueSet
v.V = unbindFSValue(b, s)
}
}
// SplitFS splits a string in to unquoted-colon separated segments.
func splitFS(s string) []string {
var fs []string
prev, esc := 0, false
for i, r := range s {
switch r {
case '\\':
esc = true
continue
case ':':
if esc {
break
}
fs = append(fs, s[prev:i])
prev = i + 1
default:
}
esc = false
}
fs = append(fs, s[prev:])
return fs
}
// UnbindFSValue does what it says on the tin.
//
// Caller provides scratch space for the return construction via the passed
// strings.Builder.
func unbindFSValue(b *strings.Builder, s string) string {
b.Reset()
esc := false
for _, r := range s {
// We need to re-escape any reserved characters that aren't
// special.
switch {
case r == '\\':
esc = true
b.WriteRune('\\')
continue
case r == '*' || r == '?':
fallthrough
case esc || !reserved(r):
b.WriteRune(r)
default:
b.WriteRune('\\')
b.WriteRune(r)
}
esc = false
}
return b.String()
}