sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use windows::Win32::System::SystemInformation::{self, SYSTEM_INFO};

fn get_vendor_id_not_great(info: &SYSTEM_INFO) -> String {
    // https://docs.microsoft.com/fr-fr/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
    unsafe {
        match info.Anonymous.Anonymous.wProcessorArchitecture {
            SystemInformation::PROCESSOR_ARCHITECTURE_INTEL => "Intel x86",
            SystemInformation::PROCESSOR_ARCHITECTURE_MIPS => "MIPS",
            SystemInformation::PROCESSOR_ARCHITECTURE_ALPHA => "RISC Alpha",
            SystemInformation::PROCESSOR_ARCHITECTURE_PPC => "PPC",
            SystemInformation::PROCESSOR_ARCHITECTURE_SHX => "SHX",
            SystemInformation::PROCESSOR_ARCHITECTURE_ARM => "ARM",
            SystemInformation::PROCESSOR_ARCHITECTURE_IA64 => "Intel Itanium-based x64",
            SystemInformation::PROCESSOR_ARCHITECTURE_ALPHA64 => "RISC Alpha x64",
            SystemInformation::PROCESSOR_ARCHITECTURE_MSIL => "MSIL",
            SystemInformation::PROCESSOR_ARCHITECTURE_AMD64 => "(Intel or AMD) x64",
            SystemInformation::PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 => "Intel Itanium-based x86",
            SystemInformation::PROCESSOR_ARCHITECTURE_NEUTRAL => "unknown",
            SystemInformation::PROCESSOR_ARCHITECTURE_ARM64 => "ARM x64",
            SystemInformation::PROCESSOR_ARCHITECTURE_ARM32_ON_WIN64 => "ARM",
            SystemInformation::PROCESSOR_ARCHITECTURE_IA32_ON_ARM64 => "Intel Itanium-based x86",
            _ => "unknown",
        }
        .to_owned()
    }
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub(crate) fn get_vendor_id_and_brand(info: &SYSTEM_INFO) -> (String, String) {
    #[cfg(target_arch = "x86")]
    use std::arch::x86::__cpuid;
    #[cfg(target_arch = "x86_64")]
    use std::arch::x86_64::__cpuid;

    unsafe fn add_u32(v: &mut Vec<u8>, i: u32) {
        let i = &i as *const u32 as *const u8;
        unsafe {
            v.push(*i);
            v.push(*i.offset(1));
            v.push(*i.offset(2));
            v.push(*i.offset(3));
        }
    }

    unsafe {
        // First, attempt to read the complete name.
        let res = __cpuid(0x80000000);
        let n_ex_ids = res.eax;
        let brand = if n_ex_ids >= 0x80000004 {
            let mut extdata = Vec::with_capacity(5);

            for i in 0x80000000..=n_ex_ids {
                extdata.push(__cpuid(i));
            }

            // 4 * u32 * nb_entries
            let mut out = Vec::with_capacity(4 * std::mem::size_of::<u32>() * 3);
            for data in extdata.iter().take(5).skip(2) {
                add_u32(&mut out, data.eax);
                add_u32(&mut out, data.ebx);
                add_u32(&mut out, data.ecx);
                add_u32(&mut out, data.edx);
            }
            let mut pos = 0;
            for e in out.iter() {
                if *e == 0 {
                    break;
                }
                pos += 1;
            }
            match std::str::from_utf8(&out[..pos]) {
                Ok(s) => s.to_owned(),
                _ => String::new(),
            }
        } else {
            String::new()
        };

        // The full name was unavailable; fall back to the short version.
        let res = __cpuid(0);
        let mut x = Vec::with_capacity(3 * std::mem::size_of::<u32>());
        add_u32(&mut x, res.ebx);
        add_u32(&mut x, res.edx);
        add_u32(&mut x, res.ecx);
        let mut pos = 0;
        for e in x.iter() {
            if *e == 0 {
                break;
            }
            pos += 1;
        }
        let vendor_id = match std::str::from_utf8(&x[..pos]) {
            Ok(s) => s.to_owned(),
            Err(_) => get_vendor_id_not_great(info),
        };
        (vendor_id, brand)
    }
}

#[cfg(all(not(target_arch = "x86_64"), not(target_arch = "x86")))]
pub(crate) fn get_vendor_id_and_brand(info: &SYSTEM_INFO) -> (String, String) {
    use crate::sys::system::get_reg_string_value;
    use windows::Win32::System::Registry::HKEY_LOCAL_MACHINE;

    const HKEY_PROCESSOR: &str = r"HARDWARE\DESCRIPTION\System\CentralProcessor\0";

    let vendor_id = get_reg_string_value(HKEY_LOCAL_MACHINE, HKEY_PROCESSOR, "VendorIdentifier")
        .unwrap_or_else(|| get_vendor_id_not_great(info));
    let brand = get_reg_string_value(HKEY_LOCAL_MACHINE, HKEY_PROCESSOR, "ProcessorNameString")
        .unwrap_or_default();
    (vendor_id, brand)
}