cverif 0.0.7

Verifies that compiled machine code faithfully matches its C source
Documentation
//! Development aid: print the structural features and the build report for ELF files.
use cverif::image::Image;

fn main() {
    for p in std::env::args().skip(1) {
        // `image.bin@8000@symbols.nm` loads a raw binary at a hex base with an `nm -S` symbol table.
        let parts: Vec<&str> = p.split('@').collect();
        let loaded = if parts.len() == 3 {
            std::fs::read(parts[0]).map_err(|e| e.to_string()).and_then(|b| {
                let syms = std::fs::read_to_string(parts[2]).map_err(|e| e.to_string())?;
                Image::from_raw(&b, u32::from_str_radix(parts[1], 16).map_err(|e| e.to_string())?, &syms)
            })
        } else {
            std::fs::read(&p)
                .map_err(|e| e.to_string())
                .and_then(|b| Image::from_elf(&b))
        };
        let img = match loaded {
            Ok(i) => i,
            Err(e) => {
                eprintln!("{p}: {e}");
                continue;
            }
        };
        if std::env::var_os("COMPACT").is_some() {
            let r = cverif::detect::detect(&img);
            let c: Vec<String> = r
                .candidates
                .iter()
                .take(3)
                .map(|c| format!("{}:{:.2}", c.0, c.1))
                .collect();
            let fl: Vec<String> = r
                .findings
                .iter()
                .filter(|f| f.verdict != cverif::detect::Verdict::Unknown)
                .map(|f| format!("{}={}", f.flag, f.verdict.label()))
                .collect();
            println!(
                "{p} | code-family={:?} | arch={:?} | {} | level={:?} | {} | contra={:?}",
                r.family_from_code,
                r.arch,
                c.join(" "),
                r.level(),
                fl.join(" "),
                r.contradictions
            );
        } else if std::env::var_os("DUMP_FEATURES").is_some() {
            println!("{p}: {:?}", cverif::detect::features(&img));
        } else {
            println!(
                "{p}:\n{}",
                cverif::detect::detect(&img).render(std::env::var_os("EXPLAIN").is_some())
            );
        }
    }
}