|
| 1 | +/* |
| 2 | + Copyright © 2021 The CDI Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cdi |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/pkg/errors" |
| 23 | +) |
| 24 | + |
| 25 | +// QualifiedName returns the qualified name for a device. |
| 26 | +// The syntax for a qualified device names is |
| 27 | +// "<vendor>/<class>=<name>". |
| 28 | +// A valid vendor name may contain the following runes: |
| 29 | +// 'A'-'Z', 'a'-'z', '0'-'9', '.', '-', '_'. |
| 30 | +// A valid class name may contain the following runes: |
| 31 | +// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_'. |
| 32 | +// A valid device name may containe the following runes: |
| 33 | +// 'A'-'Z', 'a'-'z', '0'-'9', '-', '_', '.', ':' |
| 34 | +func QualifiedName(vendor, class, name string) string { |
| 35 | + return vendor + "/" + class + "=" + name |
| 36 | +} |
| 37 | + |
| 38 | +// IsQualifiedName tests if a device name is qualified. |
| 39 | +func IsQualifiedName(device string) bool { |
| 40 | + _, _, _, err := ParseQualifiedName(device) |
| 41 | + return err == nil |
| 42 | +} |
| 43 | + |
| 44 | +// ParseQualifiedName splits a qualified name into device vendor, class, |
| 45 | +// and name. If the device fails to parse as a qualified name, or if any |
| 46 | +// of the split components fail to pass syntax validation, vendor and |
| 47 | +// class are returned as empty, together with the verbatim input as the |
| 48 | +// name and an error describing the reason for failure. |
| 49 | +func ParseQualifiedName(device string) (string, string, string, error) { |
| 50 | + vendor, class, name := ParseDevice(device) |
| 51 | + |
| 52 | + if vendor == "" { |
| 53 | + return "", "", device, errors.Errorf("unqualified device %q, missing vendor", device) |
| 54 | + } |
| 55 | + if class == "" { |
| 56 | + return "", "", device, errors.Errorf("unqualified device %q, missing class", device) |
| 57 | + } |
| 58 | + if name == "" { |
| 59 | + return "", "", device, errors.Errorf("unqualified device %q, missing device name", device) |
| 60 | + } |
| 61 | + |
| 62 | + if err := ValidateVendorName(vendor); err != nil { |
| 63 | + return "", "", device, errors.Wrapf(err, "invalid device %q", device) |
| 64 | + } |
| 65 | + if err := ValidateClassName(class); err != nil { |
| 66 | + return "", "", device, errors.Wrapf(err, "invalid device %q", device) |
| 67 | + } |
| 68 | + if err := ValidateDeviceName(name); err != nil { |
| 69 | + return "", "", device, errors.Wrapf(err, "invalid device %q", device) |
| 70 | + } |
| 71 | + |
| 72 | + return vendor, class, name, nil |
| 73 | +} |
| 74 | + |
| 75 | +// ParseDevice tries to split a device name into vendor, class, and name. |
| 76 | +// If this fails, for instance in the case of unqualified device names, |
| 77 | +// ParseDevice returns an empty vendor and class together with name set |
| 78 | +// to the verbatim input. |
| 79 | +func ParseDevice(device string) (string, string, string) { |
| 80 | + if device == "" || device[0] == '/' { |
| 81 | + return "", "", device |
| 82 | + } |
| 83 | + |
| 84 | + parts := strings.SplitN(device, "=", 2) |
| 85 | + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 86 | + return "", "", device |
| 87 | + } |
| 88 | + |
| 89 | + name := parts[1] |
| 90 | + vendor, class := ParseQualifier(parts[0]) |
| 91 | + if vendor == "" { |
| 92 | + return "", "", device |
| 93 | + } |
| 94 | + |
| 95 | + return vendor, class, name |
| 96 | +} |
| 97 | + |
| 98 | +// ParseQualifier splits a device qualifier into vendor and class. |
| 99 | +// The syntax for a device qualifier is |
| 100 | +// "<vendor>/<class>" |
| 101 | +// If parsing fails, an empty vendor and the class set to the |
| 102 | +// verbatim input is returned. |
| 103 | +func ParseQualifier(kind string) (string, string) { |
| 104 | + parts := strings.SplitN(kind, "/", 2) |
| 105 | + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 106 | + return "", kind |
| 107 | + } |
| 108 | + return parts[0], parts[1] |
| 109 | +} |
| 110 | + |
| 111 | +// ValidateVendorName checks the validity of a vendor name. |
| 112 | +// A vendor name may contain the following ASCII characters: |
| 113 | +// - upper- and lowercase letters ('A'-'Z', 'a'-'z') |
| 114 | +// - digits ('0'-'9') |
| 115 | +// - underscore, dash, and dot ('_', '-', and '.') |
| 116 | +func ValidateVendorName(vendor string) error { |
| 117 | + if vendor == "" { |
| 118 | + return errors.Errorf("invalid (empty) vendor name") |
| 119 | + } |
| 120 | + if !isLetter(rune(vendor[0])) { |
| 121 | + return errors.Errorf("invalid vendor %q, should start with letter", vendor) |
| 122 | + } |
| 123 | + for _, c := range string(vendor[1 : len(vendor)-1]) { |
| 124 | + switch { |
| 125 | + case isAlphaNumeric(c): |
| 126 | + case c == '_' || c == '-' || c == '.': |
| 127 | + default: |
| 128 | + return errors.Errorf("invalid character '%c' in vendor name %q", |
| 129 | + c, vendor) |
| 130 | + } |
| 131 | + } |
| 132 | + if !isAlphaNumeric(rune(vendor[len(vendor)-1])) { |
| 133 | + return errors.Errorf("invalid vendor %q, should end with letter", vendor) |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +// ValidateClassName checks the validity of class name. |
| 140 | +// A class name may contain the following ASCII characters: |
| 141 | +// - upper- and lowercase letters ('A'-'Z', 'a'-'z') |
| 142 | +// - digits ('0'-'9') |
| 143 | +// - underscore and dash ('_', '-') |
| 144 | +func ValidateClassName(class string) error { |
| 145 | + if class == "" { |
| 146 | + return errors.Errorf("invalid (empty) device class") |
| 147 | + } |
| 148 | + if !isLetter(rune(class[0])) { |
| 149 | + return errors.Errorf("invalid class %q, should start with letter", class) |
| 150 | + } |
| 151 | + for _, c := range string(class[1 : len(class)-1]) { |
| 152 | + switch { |
| 153 | + case isAlphaNumeric(c): |
| 154 | + case c == '_' || c == '-': |
| 155 | + default: |
| 156 | + return errors.Errorf("invalid character '%c' in device class %q", |
| 157 | + c, class) |
| 158 | + } |
| 159 | + } |
| 160 | + if !isAlphaNumeric(rune(class[len(class)-1])) { |
| 161 | + return errors.Errorf("invalid class %q, should end with letter", class) |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +// ValidateDeviceName checks the validity of a device name. |
| 167 | +// A device name may contain the following ASCII characters: |
| 168 | +// - upper- and lowercase letters ('A'-'Z', 'a'-'z') |
| 169 | +// - digits ('0'-'9') |
| 170 | +// - underscore, dash, dot, colon ('_', '-', '.', ':') |
| 171 | +func ValidateDeviceName(name string) error { |
| 172 | + if name == "" { |
| 173 | + return errors.Errorf("invalid (empty) device name") |
| 174 | + } |
| 175 | + if !isLetter(rune(name[0])) { |
| 176 | + return errors.Errorf("invalid name %q, should start with letter", name) |
| 177 | + } |
| 178 | + for _, c := range string(name[1 : len(name)-1]) { |
| 179 | + switch { |
| 180 | + case isAlphaNumeric(c): |
| 181 | + case c == '_' || c == '-' || c == '.' || c == ':': |
| 182 | + default: |
| 183 | + return errors.Errorf("invalid character '%c' in device name %q", |
| 184 | + c, name) |
| 185 | + } |
| 186 | + } |
| 187 | + if !isAlphaNumeric(rune(name[len(name)-1])) { |
| 188 | + return errors.Errorf("invalid name %q, should start with letter", name) |
| 189 | + } |
| 190 | + return nil |
| 191 | +} |
| 192 | + |
| 193 | +func isLetter(c rune) bool { |
| 194 | + return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') |
| 195 | +} |
| 196 | + |
| 197 | +func isDigit(c rune) bool { |
| 198 | + return '0' <= c && c <= '9' |
| 199 | +} |
| 200 | + |
| 201 | +func isAlphaNumeric(c rune) bool { |
| 202 | + return isLetter(c) || isDigit(c) |
| 203 | +} |
0 commit comments