Validate a PCI BDF address (format `DDDD:BB:DD.F`).
(bdf: &str)
| 163 | |
| 164 | /// Validate a PCI BDF address (format `DDDD:BB:DD.F`). |
| 165 | pub fn validate_bdf(bdf: &str) -> Result<(), VfioError> { |
| 166 | let bytes = bdf.as_bytes(); |
| 167 | if bytes.len() != 12 { |
| 168 | return Err(VfioError::InvalidBdf { |
| 169 | bdf: bdf.to_string(), |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | // Expected layout: [hex x 4]:[hex x 2]:[hex x 2].[hex x 1] |
| 174 | // 0123 4 56 7 89 A B |
| 175 | let ok = is_hex(bytes[0]) |
| 176 | && is_hex(bytes[1]) |
| 177 | && is_hex(bytes[2]) |
| 178 | && is_hex(bytes[3]) |
| 179 | && bytes[4] == b':' |
| 180 | && is_hex(bytes[5]) |
| 181 | && is_hex(bytes[6]) |
| 182 | && bytes[7] == b':' |
| 183 | && is_hex(bytes[8]) |
| 184 | && is_hex(bytes[9]) |
| 185 | && bytes[10] == b'.' |
| 186 | && is_hex(bytes[11]); |
| 187 | |
| 188 | if ok { |
| 189 | Ok(()) |
| 190 | } else { |
| 191 | Err(VfioError::InvalidBdf { |
| 192 | bdf: bdf.to_string(), |
| 193 | }) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn is_hex(b: u8) -> bool { |
| 198 | b.is_ascii_hexdigit() |
no test coverage detected