Debian Codegraph @

naming/cpe_name_binder.go

raw
1 package naming
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/knqyf263/go-cpe/common"
8 )
9
10 // BindToURI a {@link WellFormedName} object to a URI.
11 // @param w WellFormedName to be bound to URI
12 // @return URI binding of WFN
13 func BindToURI(w common.WellFormedName) (uri string) {
14 // Initialize the output with the CPE v2.2 URI prefix.
15 uri = "cpe:/"
16
17 // Define the attributes that correspond to the seven components in a v2.2. CPE.
18 attributes := []string{"part", "vendor", "product", "version", "update", "edition", "language"}
19
20 for _, a := range attributes {
21 v := ""
22 if a == "edition" {
23 // Call the pack() helper function to compute the proper binding for the edition element.
24 ed := bindValueForURI(w.Get(common.AttributeEdition))
25 swEd := bindValueForURI(w.Get(common.AttributeSwEdition))
26 targetSw := bindValueForURI(w.Get(common.AttributeTargetSw))
27 targetHw := bindValueForURI(w.Get(common.AttributeTargetHw))
28 other := bindValueForURI(w.Get(common.AttributeOther))
29 v = pack(ed, swEd, targetSw, targetHw, other)
30 } else {
31 // Get the value for a in w, then bind to a string
32 // for inclusion in the URI.
33 v = bindValueForURI(w.Get(a))
34 }
35 // Append v to the URI then add a colon.
36 uri += v + ":"
37 }
38 return strings.TrimRight(uri, ":")
39 }
40
41 // BindToFS is top-level function used to bind WFN w to formatted string.
42 // @param w WellFormedName to bind
43 // @return Formatted String
44 func BindToFS(w common.WellFormedName) (fs string) {
45 // Initialize the output with the CPE v2.3 string prefix.
46 fs = "cpe:2.3:"
47 attributes := []string{"part", "vendor", "product", "version",
48 "update", "edition", "language", "sw_edition", "target_sw",
49 "target_hw", "other"}
50 for _, a := range attributes {
51 v := bindValueForFS(w.Get(a))
52 fs += v
53 if a != common.AttributeOther {
54 fs += ":"
55 }
56 }
57 return fs
58 }
59
60 // bindValueForURI converts a string to the proper string for including in a CPE v2.2-conformant URI.
61 // The logical value ANY binds to the blank in the 2.2-conformant URI.
62 // @param s string to be converted
63 // @return converted string
64 func bindValueForURI(s interface{}) string {
65 if lv, ok := s.(common.LogicalValue); ok {
66 // The value NA binds to a blank.
67 if lv.IsANY() {
68 return ""
69 }
70 // The value NA binds to a single hyphen.
71 if lv.IsNA() {
72 return "-"
73 }
74 }
75
76 if str, ok := s.(string); ok {
77 return transformForURI(str)
78 }
79 return ""
80 }
81
82 // bindValueForFS converts the value v to its proper string representation for insertion to formatted string.
83 // @param v value to convert
84 // @return Formatted value
85 func bindValueForFS(v interface{}) string {
86 if lv, ok := v.(common.LogicalValue); ok {
87 // The value NA binds to a asterisk.
88 if lv.IsANY() {
89 return "*"
90 }
91 // The value NA binds to a single hyphen.
92 if lv.IsNA() {
93 return "-"
94 }
95 }
96 if str, ok := v.(string); ok {
97 return processQuotedChars(str)
98 }
99 return ""
100 }
101
102 // Inspect each character in string s. Certain nonalpha characters pass
103 // thru without escaping into the result, but most retain escaping.
104 // @param s
105 // @return
106 func processQuotedChars(s string) (result string) {
107 idx := 0
108 for idx < len(s) {
109 c := s[idx : idx+1]
110 if c == "\\" {
111 // escaped characters are examined.
112 nextchr := s[idx+1 : idx+2]
113 // the period, hyphen and underscore pass unharmed.
114 if nextchr == "." || nextchr == "-" || nextchr == "_" {
115 result += nextchr
116 idx += 2
117 continue
118 } else {
119 // all others retain escaping.
120 result += "\\" + nextchr
121 idx += 2
122 continue
123 }
124 }
125 // unquoted characters pass thru unharmed.
126 result += c
127 idx = idx + 1
128 }
129 return result
130 }
131
132 // transformForURI scans an input string and performs the following transformations:
133 // - Pass alphanumeric characters thru untouched
134 // - Percent-encode quoted non-alphanumerics as needed
135 // - Unquoted special characters are mapped to their special forms
136 // @param s string to be transformed
137 // @return transformed string
138 func transformForURI(s string) (result string) {
139 idx := 0
140 for idx < len(s) {
141 // Get the idx'th character of s.
142 thischar := s[idx : idx+1]
143 if common.IsAlphanum(thischar) {
144 result += thischar
145 idx++
146 continue
147 }
148 // Check for escape character.
149 if thischar == "\\" {
150 idx++
151 nxtchar := s[idx : idx+1]
152 result += pctEncode(nxtchar)
153 idx++
154 continue
155 }
156 // Bind the unquoted '?' special character to "%01".
157 if thischar == "?" {
158 result += "%01"
159 }
160 // Bind the unquoted '*' special character to "%02".
161 if thischar == "*" {
162 result += "%02"
163 }
164 idx++
165
166 }
167 return result
168 }
169
170 // pctEncode returns the appropriate percent-encoding of character c.
171 // Certain characters are returned without encoding.
172 // @param c the single character string to be encoded
173 // @return the percent encoded string
174 func pctEncode(c string) string {
175 if c == "!" {
176 return "%21"
177 }
178 if c == "\"" {
179 return "%22"
180 }
181 if c == "#" {
182 return "%23"
183 }
184 if c == "$" {
185 return "%24"
186 }
187 if c == "%" {
188 return "%25"
189 }
190 if c == "&" {
191 return "%26"
192 }
193 if c == "'" {
194 return "%27"
195 }
196 if c == "(" {
197 return "%28"
198 }
199 if c == ")" {
200 return "%29"
201 }
202 if c == "*" {
203 return "%2a"
204 }
205 if c == "+" {
206 return "%2b"
207 }
208 if c == "," {
209 return "%2c"
210 }
211 // bound without encoding.
212 if c == "-" {
213 return c
214 }
215 // bound without encoding.
216 if c == "." {
217 return c
218 }
219 if c == "/" {
220 return "%2f"
221 }
222 if c == ":" {
223 return "%3a"
224 }
225 if c == ";" {
226 return "%3b"
227 }
228 if c == "<" {
229 return "%3c"
230 }
231 if c == "=" {
232 return "%3d"
233 }
234 if c == ">" {
235 return "%3e"
236 }
237 if c == "?" {
238 return "%3f"
239 }
240 if c == "@" {
241 return "%40"
242 }
243 if c == "[" {
244 return "%5b"
245 }
246 if c == "\\" {
247 return "%5c"
248 }
249 if c == "]" {
250 return "%5d"
251 }
252 if c == "^" {
253 return "%5e"
254 }
255 if c == "`" {
256 return "%60"
257 }
258 if c == "{" {
259 return "%7b"
260 }
261 if c == "|" {
262 return "%7c"
263 }
264 if c == "}" {
265 return "%7d"
266 }
267 if c == "~" {
268 return "%7e"
269 }
270 // Shouldn't reach here, return original character
271 return c
272 }
273
274 /**
275 * Packs the values of the five arguments into the single
276 * edition component. If all the values are blank, the
277 * function returns a blank.
278 * @param ed edition string
279 * @param swEd software edition string
280 * @param tSw target software string
281 * @param tHw target hardware string
282 * @param oth other edition information string
283 * @return the packed string, or blank
284 */
285 func pack(ed, swEd, tSw, tHw, oth string) string {
286 if swEd == "" && tSw == "" && tHw == "" && oth == "" {
287 // All the extended attributes are blank, so don't do
288 // any packing, just return ed.
289 return ed
290 }
291 // Otherwise, pack the five values into a single string
292 // prefixed and internally delimited with the tilde.
293 return fmt.Sprintf("~%s~%s~%s~%s~%s", ed, swEd, tSw, tHw, oth)
294 }