use core::ops::ControlFlow;
use super::{lossy, trim};
const NAME_MAX: usize = 256;
#[derive(Debug)]
pub(crate) struct Lookup {
vendor: u16,
device: u16,
in_vendor: bool,
pub(crate) vendor_name: Option<String>,
pub(crate) device_name: Option<String>,
}
fn hex4(s: &[u8]) -> Option<u16> {
if s.len() != 4 || !s.iter().all(u8::is_ascii_hexdigit) {
return None;
}
u16::from_str_radix(core::str::from_utf8(s).ok()?, 16).ok()
}
fn entry(line: &[u8]) -> Option<(u16, String)> {
let id = hex4(line.get(..4)?)?;
let rest = line.get(4..)?;
if !rest.starts_with(b" ") {
return None;
}
let name = trim(rest);
(!name.is_empty()).then(|| (id, lossy(name, NAME_MAX)))
}
impl Lookup {
pub(crate) const fn new(vendor: u16, device: u16) -> Lookup {
Lookup {
vendor,
device,
in_vendor: false,
vendor_name: None,
device_name: None,
}
}
pub(crate) fn line(&mut self, line: &[u8]) -> ControlFlow<()> {
let line = line.strip_suffix(b"\r").unwrap_or(line);
if line.is_empty() || line[0] == b'#' {
return ControlFlow::Continue(());
}
if line.starts_with(b"\t\t") {
return ControlFlow::Continue(());
}
if let Some(rest) = line.strip_prefix(b"\t") {
if self.in_vendor
&& let Some((id, name)) = entry(rest)
&& id == self.device
{
self.device_name = Some(name);
return ControlFlow::Break(());
}
return ControlFlow::Continue(());
}
if self.in_vendor || line.starts_with(b"C ") {
return ControlFlow::Break(());
}
if let Some((id, name)) = entry(line)
&& id == self.vendor
{
self.vendor_name = Some(name);
self.in_vendor = true;
}
ControlFlow::Continue(())
}
}
#[cfg(any(test, fuzzing))]
pub(crate) fn find(data: &[u8], vendor: u16, device: u16) -> (Option<String>, Option<String>) {
let mut lookup = Lookup::new(vendor, device);
for line in data
.split(|&b| b == b'\n')
.take(super::mountinfo::MAX_LINES)
{
if lookup.line(line).is_break() {
break;
}
}
(lookup.vendor_name, lookup.device_name)
}
#[cfg(test)]
mod tests {
use super::*;
const DB: &[u8] = b"# pci.ids sample\n\
#\tcomment with a tab\n\
1002 Advanced Micro Devices, Inc. [AMD/ATI]\n\
\t744c Navi 31 [Radeon RX 7900 XT/7900 XTX/7900 GRE/7900M]\n\
\t\t1002 0e3b RX 7900 GRE\n\
10de NVIDIA Corporation\n\
\t2786 AD104 [GeForce RTX 4070]\n\
\t2788 AD104\r\n\
8086 Intel Corporation\n\
C 03 Display controller\n\
\t00 VGA compatible controller\n";
#[test]
fn test_pci_ids_finds_vendor_and_device() {
assert_eq!(
find(DB, 0x10de, 0x2786),
(
Some("NVIDIA Corporation".into()),
Some("AD104 [GeForce RTX 4070]".into())
)
);
assert_eq!(find(DB, 0x10de, 0x2788).1.as_deref(), Some("AD104"));
assert_eq!(find(DB, 0x1002, 0x0e3b).1, None);
assert_eq!(
find(DB, 0x1002, 0x744c).1.as_deref(),
Some("Navi 31 [Radeon RX 7900 XT/7900 XTX/7900 GRE/7900M]")
);
}
#[test]
fn test_pci_ids_unknown_and_malformed() {
assert_eq!(
find(DB, 0x8086, 0x1234),
(Some("Intel Corporation".into()), None)
);
assert_eq!(find(DB, 0xabcd, 0x0001), (None, None));
assert_eq!(find(DB, 0x0003, 0x0000), (None, None));
assert_eq!(find(b"", 1, 2), (None, None));
assert_eq!(find(b"10de\n\tzzzz x\n10dX y\n", 0x10de, 0), (None, None));
assert_eq!(find(b"10deNVIDIA\n", 0x10de, 0), (None, None));
assert_eq!(find(b"\xff\xfe\t\t\t\n\t", 0, 0), (None, None));
}
}