| 1 |
package naming |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
|
| 6 |
"github.com/knqyf263/go-cpe/common" |
| 7 |
"github.com/pkg/errors" |
| 8 |
) |
| 9 |
|
| 10 |
// UnbindURI is a top level function used to unbind a URI to a WFN. |
| 11 |
// @param uri String representing the URI to be unbound. |
| 12 |
// @return WellFormedName representing the unbound URI. |
| 13 |
func UnbindURI(uri string) (common.WellFormedName, error) { |
| 14 |
if err := common.ValidateURI(uri); err != nil { |
| 15 |
return nil, errors.Wrap(err, "Failed to validate uri") |
| 16 |
} |
| 17 |
result := common.WellFormedName{} |
| 18 |
for i := 0; i < 8; i++ { |
| 19 |
v := getCompURI(uri, i) |
| 20 |
d, err := decode(v) |
| 21 |
if i != 6 && err != nil { |
| 22 |
return nil, err |
| 23 |
} |
| 24 |
switch i { |
| 25 |
case 1: |
| 26 |
err = result.Set(common.AttributePart, d) |
| 27 |
case 2: |
| 28 |
err = result.Set(common.AttributeVendor, d) |
| 29 |
case 3: |
| 30 |
err = result.Set(common.AttributeProduct, d) |
| 31 |
case 4: |
| 32 |
err = result.Set(common.AttributeVersion, d) |
| 33 |
case 5: |
| 34 |
err = result.Set(common.AttributeUpdate, d) |
| 35 |
case 6: |
| 36 |
// Special handling for edition component. |
| 37 |
// Unpack edition if needed. |
| 38 |
if v == "" || v == "-" || v[0] != '~' { |
| 39 |
// Just a logical value or a non-packed value. |
| 40 |
// So unbind to legacy edition, leaving other |
| 41 |
// extended attributes unspecified. |
| 42 |
err = result.Set(common.AttributeEdition, d) |
| 43 |
} else { |
| 44 |
// We have five values packed together here. |
| 45 |
if result, err = unpack(v, result); err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
} |
| 49 |
case 7: |
| 50 |
err = result.Set(common.AttributeLanguage, d) |
| 51 |
} |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
} |
| 56 |
return result, nil |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
// UnbindFS is a top level function to unbind a formatted string to WFN. |
| 61 |
// @param fs Formatted string to unbind |
| 62 |
// @return WellFormedName |
| 63 |
// @throws ParseException |
| 64 |
func UnbindFS(fs string) (common.WellFormedName, error) { |
| 65 |
// Validate the formatted string |
| 66 |
if err := common.ValidateFS(fs); err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
// Initialize empty WFN |
| 70 |
result := common.NewWellFormedName() |
| 71 |
// The cpe scheme is the 0th component, the cpe version is the 1st. |
| 72 |
// So we start parsing at the 2nd component. |
| 73 |
for a := 2; a != 13; a++ { |
| 74 |
// Get the a'th string field. |
| 75 |
s := getCompFS(fs, a) |
| 76 |
// Unbind the string. |
| 77 |
v, err := unbindValueFS(s) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
|
| 82 |
// Set the value of the corresponding attribute. |
| 83 |
switch a { |
| 84 |
case 2: |
| 85 |
result.Set(common.AttributePart, v) |
| 86 |
break |
| 87 |
case 3: |
| 88 |
result.Set(common.AttributeVendor, v) |
| 89 |
break |
| 90 |
case 4: |
| 91 |
result.Set(common.AttributeProduct, v) |
| 92 |
break |
| 93 |
case 5: |
| 94 |
result.Set(common.AttributeVersion, v) |
| 95 |
break |
| 96 |
case 6: |
| 97 |
result.Set(common.AttributeUpdate, v) |
| 98 |
break |
| 99 |
case 7: |
| 100 |
result.Set(common.AttributeEdition, v) |
| 101 |
break |
| 102 |
case 8: |
| 103 |
result.Set(common.AttributeLanguage, v) |
| 104 |
break |
| 105 |
case 9: |
| 106 |
result.Set(common.AttributeSwEdition, v) |
| 107 |
break |
| 108 |
case 10: |
| 109 |
result.Set(common.AttributeTargetSw, v) |
| 110 |
break |
| 111 |
case 11: |
| 112 |
result.Set(common.AttributeTargetHw, v) |
| 113 |
break |
| 114 |
case 12: |
| 115 |
result.Set(common.AttributeOther, v) |
| 116 |
break |
| 117 |
} |
| 118 |
} |
| 119 |
return result, nil |
| 120 |
} |
| 121 |
|
| 122 |
// getCompURI return the i'th component of the URI. |
| 123 |
// @param uri String representation of URI to retrieve components from. |
| 124 |
// @param i Index of component to return. |
| 125 |
// @return If i = 0, returns the URI scheme. Otherwise, returns the i'th |
| 126 |
// component of uri. |
| 127 |
func getCompURI(uri string, i int) string { |
| 128 |
if i == 0 { |
| 129 |
return uri[:strings.Index(uri, "/")] |
| 130 |
} |
| 131 |
sa := strings.Split(uri, ":") |
| 132 |
// If requested component exceeds the number |
| 133 |
// of components in URI, return blank |
| 134 |
if i >= len(sa) { |
| 135 |
return "" |
| 136 |
} |
| 137 |
if i == 1 { |
| 138 |
return strings.TrimLeft(sa[1], "/") |
| 139 |
} |
| 140 |
return sa[i] |
| 141 |
} |
| 142 |
|
| 143 |
// Returns the i'th field of the formatted string. The colon is the field |
| 144 |
// delimiter unless prefixed by a backslash. |
| 145 |
// @param fs formatted string to retrieve from |
| 146 |
// @param i index of field to retrieve from fs. |
| 147 |
// @return value of index of formatted string |
| 148 |
func getCompFS(fs string, i int) string { |
| 149 |
if i < 0 { |
| 150 |
return "" |
| 151 |
} |
| 152 |
|
| 153 |
for j := 0; j < i; j++ { |
| 154 |
// return the substring from index 0 to the first occurence of an |
| 155 |
// unescaped colon |
| 156 |
colonIdx := common.GetUnescapedColonIndex(fs) |
| 157 |
if colonIdx == 0 { |
| 158 |
fs = "" |
| 159 |
break |
| 160 |
} |
| 161 |
fs = fs[colonIdx+1:] |
| 162 |
} |
| 163 |
endIdx := common.GetUnescapedColonIndex(fs) |
| 164 |
// If no colon is found, we are at the end of the formatted string, |
| 165 |
// so just return what's left. |
| 166 |
if endIdx == 0 { |
| 167 |
return fs |
| 168 |
} |
| 169 |
return fs[:endIdx] |
| 170 |
} |
| 171 |
|
| 172 |
// Takes a string value and returns the appropriate logical value if string |
| 173 |
// is the bound form of a logical value. If string is some general value |
| 174 |
// string, add quoting of non-alphanumerics as needed. |
| 175 |
// @param s value to be unbound |
| 176 |
// @return logical value or quoted string |
| 177 |
// @throws ParseException |
| 178 |
func unbindValueFS(s string) (interface{}, error) { |
| 179 |
if s == "*" { |
| 180 |
any, _ := common.NewLogicalValue("ANY") |
| 181 |
return any, nil |
| 182 |
} |
| 183 |
if s == "-" { |
| 184 |
na, _ := common.NewLogicalValue("NA") |
| 185 |
return na, nil |
| 186 |
} |
| 187 |
result, err := addQuoting(s) |
| 188 |
if err != nil { |
| 189 |
return nil, err |
| 190 |
} |
| 191 |
return result, nil |
| 192 |
} |
| 193 |
|
| 194 |
// Inspect each character in a string, copying quoted characters, with |
| 195 |
// their escaping, into the result. Look for unquoted non alphanumerics |
| 196 |
// and if not "*" or "?", add escaping. |
| 197 |
// @param s |
| 198 |
// @return |
| 199 |
// @throws ParseException |
| 200 |
func addQuoting(s string) (result string, err error) { |
| 201 |
idx := 0 |
| 202 |
embedded := false |
| 203 |
for idx < len(s) { |
| 204 |
c := s[idx : idx+1] |
| 205 |
if common.IsAlphanum(c) || c == "_" { |
| 206 |
// Alphanumeric characters pass untouched. |
| 207 |
result += c |
| 208 |
idx++ |
| 209 |
embedded = true |
| 210 |
continue |
| 211 |
} |
| 212 |
if c == "\\" { |
| 213 |
// Anything quoted in the bound string stays quoted in the |
| 214 |
// unbound string. |
| 215 |
result += s[idx : idx+2] |
| 216 |
idx += 2 |
| 217 |
embedded = true |
| 218 |
continue |
| 219 |
} |
| 220 |
if c == "*" { |
| 221 |
// An unquoted asterisk must appear at the beginning or the end |
| 222 |
// of the string. |
| 223 |
if idx == 0 || idx == len(s)-1 { |
| 224 |
result += c |
| 225 |
idx = idx + 1 |
| 226 |
embedded = true |
| 227 |
continue |
| 228 |
} |
| 229 |
return "", errors.Wrap(common.ErrParse, "Error! cannot have unquoted * embedded in formatted string.") |
| 230 |
} |
| 231 |
if c == "?" { |
| 232 |
// An unquoted question mark must appear at the beginning or |
| 233 |
// end of the string, or in a leading or trailing sequence. |
| 234 |
// if // ? legal at beginning or end |
| 235 |
valid := false |
| 236 |
if idx == 0 || idx == len(s)-1 { |
| 237 |
// ? legal at beginning or end |
| 238 |
valid = true |
| 239 |
} else if !embedded && idx > 0 && s[idx-1:idx] == "?" { |
| 240 |
// embedded is false, so must be preceded by ? |
| 241 |
valid = true |
| 242 |
} else if embedded && len(s) >= idx+2 && s[idx+1:idx+2] == "?" { |
| 243 |
// embedded is true, so must be followed by ? |
| 244 |
valid = true |
| 245 |
} |
| 246 |
|
| 247 |
if !valid { |
| 248 |
return "", errors.Wrap(common.ErrParse, "Error! cannot have unquoted ? embedded in formatted string.") |
| 249 |
} |
| 250 |
result += c |
| 251 |
idx++ |
| 252 |
embedded = false |
| 253 |
continue |
| 254 |
} |
| 255 |
// All other characters must be quoted. |
| 256 |
result += "\\" + c |
| 257 |
idx++ |
| 258 |
embedded = true |
| 259 |
} |
| 260 |
return result, nil |
| 261 |
} |
| 262 |
|
| 263 |
// decode scans a string and returns a copy with all percent-encoded characters |
| 264 |
// decoded. This function is the inverse of pctEncode() defined in the |
| 265 |
// CPENameBinder class. Only legal percent-encoded forms are decoded. |
| 266 |
// Others raise a ParseException. |
| 267 |
// @param s String to be decoded |
| 268 |
// @return decoded string |
| 269 |
// @throws ParseException |
| 270 |
// @see CPENameBinder#pctEncode(java.lang.String) |
| 271 |
func decode(s string) (interface{}, error) { |
| 272 |
if s == "" { |
| 273 |
any, _ := common.NewLogicalValue("ANY") |
| 274 |
return any, nil |
| 275 |
} |
| 276 |
if s == "-" { |
| 277 |
na, _ := common.NewLogicalValue("NA") |
| 278 |
return na, nil |
| 279 |
} |
| 280 |
// Start the scanning loop. |
| 281 |
// Normalize: convert all uppercase letters to lowercase first. |
| 282 |
s = strings.ToLower(s) |
| 283 |
result := "" |
| 284 |
idx := 0 |
| 285 |
embedded := false |
| 286 |
for idx < len(s) { |
| 287 |
// Get the idx'th character of s. |
| 288 |
c := s[idx : idx+1] |
| 289 |
// Deal with dot, hyphen, and tilde: decode with quoting. |
| 290 |
if c == "." || c == "-" || c == "~" { |
| 291 |
result += "\\" + c |
| 292 |
idx++ |
| 293 |
// a non-%01 encountered. |
| 294 |
embedded = true |
| 295 |
continue |
| 296 |
} |
| 297 |
if c != "%" { |
| 298 |
result += c |
| 299 |
idx++ |
| 300 |
// a non-%01 encountered. |
| 301 |
embedded = true |
| 302 |
continue |
| 303 |
} |
| 304 |
// We get here if we have a substring starting w/ '%'. |
| 305 |
form := s[idx : idx+3] |
| 306 |
if form == "%01" { |
| 307 |
valid := false |
| 308 |
if idx == 0 || idx == len(s)-3 { |
| 309 |
valid = true |
| 310 |
} else if !embedded && (idx-3 >= 0) && s[idx-3:idx] == "%01" { |
| 311 |
valid = true |
| 312 |
} else if embedded && len(s) >= idx+6 && s[idx+3:idx+6] == "%01" { |
| 313 |
valid = true |
| 314 |
} |
| 315 |
|
| 316 |
if valid { |
| 317 |
result += "?" |
| 318 |
idx = idx + 3 |
| 319 |
continue |
| 320 |
} else { |
| 321 |
return nil, errors.Wrapf(common.ErrParse, "Error decoding string") |
| 322 |
} |
| 323 |
} else if form == "%02" { |
| 324 |
if idx == 0 || idx == len(s)-3 { |
| 325 |
result += "*" |
| 326 |
} else { |
| 327 |
return nil, errors.Wrapf(common.ErrParse, "Error decoding string") |
| 328 |
} |
| 329 |
} else if form == "%21" { |
| 330 |
result += `\!` |
| 331 |
} else if form == "%22" { |
| 332 |
result += `\"` |
| 333 |
} else if form == "%23" { |
| 334 |
result += `\#` |
| 335 |
} else if form == "%24" { |
| 336 |
result += `\$` |
| 337 |
} else if form == "%25" { |
| 338 |
result += `\%` |
| 339 |
} else if form == "%26" { |
| 340 |
result += `\&` |
| 341 |
} else if form == "%27" { |
| 342 |
result += `\'` |
| 343 |
} else if form == "%28" { |
| 344 |
result += `\(` |
| 345 |
} else if form == "%29" { |
| 346 |
result += `\)` |
| 347 |
} else if form == "%2a" { |
| 348 |
result += `\*` |
| 349 |
} else if form == "%2b" { |
| 350 |
result += `\+` |
| 351 |
} else if form == "%2c" { |
| 352 |
result += `\,` |
| 353 |
} else if form == "%2f" { |
| 354 |
result += `\/` |
| 355 |
} else if form == "%3a" { |
| 356 |
result += `\:` |
| 357 |
} else if form == "%3b" { |
| 358 |
result += `\;` |
| 359 |
} else if form == "%3c" { |
| 360 |
result += `\<` |
| 361 |
} else if form == "%3d" { |
| 362 |
result += `\=` |
| 363 |
} else if form == "%3e" { |
| 364 |
result += `\>` |
| 365 |
} else if form == "%3f" { |
| 366 |
result += `\?` |
| 367 |
} else if form == "%40" { |
| 368 |
result += `\@` |
| 369 |
} else if form == "%5b" { |
| 370 |
result += `\[` |
| 371 |
} else if form == "%5c" { |
| 372 |
result += `\\` |
| 373 |
} else if form == "%5d" { |
| 374 |
result += `\]` |
| 375 |
} else if form == "%5e" { |
| 376 |
result += `\^` |
| 377 |
} else if form == "%60" { |
| 378 |
result += "\\`" |
| 379 |
} else if form == "%7b" { |
| 380 |
result += `\{` |
| 381 |
} else if form == "%7c" { |
| 382 |
result += `\|` |
| 383 |
} else if form == "%7d" { |
| 384 |
result += `\}` |
| 385 |
} else if form == "%7e" { |
| 386 |
result += `\~` |
| 387 |
} else { |
| 388 |
return nil, errors.Wrapf(common.ErrParse, "Unknown form: %s", form) |
| 389 |
} |
| 390 |
idx = idx + 3 |
| 391 |
embedded = true |
| 392 |
} |
| 393 |
return result, nil |
| 394 |
} |
| 395 |
|
| 396 |
// unpack unpacks the elements in s and sets the attributes in the given |
| 397 |
// WellFormedName accordingly. |
| 398 |
// @param s packed String |
| 399 |
// @param wfn WellFormedName |
| 400 |
// @return The augmented WellFormedName. |
| 401 |
func unpack(s string, wfn common.WellFormedName) (common.WellFormedName, error) { |
| 402 |
// Set each component in the WFN. |
| 403 |
editions := strings.SplitN(s[1:], "~", 5) |
| 404 |
if len(editions) != 5 { |
| 405 |
return nil, errors.Wrap(common.ErrParse, "editions must be 5") |
| 406 |
} |
| 407 |
attributes := []string{common.AttributeEdition, common.AttributeSwEdition, common.AttributeTargetSw, |
| 408 |
common.AttributeTargetHw, common.AttributeOther} |
| 409 |
for i, a := range attributes { |
| 410 |
e, err := decode(editions[i]) |
| 411 |
if err != nil { |
| 412 |
return nil, err |
| 413 |
} |
| 414 |
if err = wfn.Set(a, e); err != nil { |
| 415 |
return nil, err |
| 416 |
} |
| 417 |
} |
| 418 |
return wfn, nil |
| 419 |
} |