Last active
March 24, 2022 02:32
-
-
Save itn3000/f2b177c895eb6a4c740aedec2642e377 to your computer and use it in GitHub Desktop.
read exe version by rust with pelite( https://github.com/CasualX/pelite )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // this works with linux | |
| use std::fs; | |
| use pelite::pe64::Pe; | |
| use pelite::pe32::Pe as Pe32; | |
| use pelite::Error as PeErrors; | |
| use memmap2::Mmap; | |
| fn output_ver_info(p: &std::path::Path) -> Result<(), anyhow::Error> { | |
| { | |
| // let f = std::fs::File::open(p)?; | |
| let fmap = pelite::FileMap::open(p)?; | |
| match pelite::pe64::PeFile::from_bytes(&fmap) { | |
| Ok(pe) => { | |
| let res = pe.resources()?; | |
| output_resource_ver_info(p, &res)?; | |
| }, | |
| Err(e) => { | |
| if let PeErrors::PeMagic = e { | |
| let pe = pelite::pe32::PeFile::from_bytes(&fmap)?; | |
| let res = pe.resources()?; | |
| output_resource_ver_info(p, &res)?; | |
| } else { | |
| if let PeErrors::BadMagic = e { | |
| } else { | |
| return Err(anyhow::Error::from(e)); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| Ok(()) | |
| } | |
| fn output_resource_ver_info(p: &std::path::Path, res: &pelite::resources::Resources) -> Result<(), anyhow::Error> { | |
| let vinfo = res.version_info()?; | |
| let finfo = vinfo.file_info(); | |
| for (lang, strings) in finfo.strings { | |
| for (k, v) in strings { | |
| println!("{:?}, {}: '{}' = '{}'", p, lang, k, v); | |
| } | |
| } | |
| Ok(()) | |
| } | |
| fn main() -> Result<(), anyhow::Error> { | |
| let args: Vec<String> = std::env::args().collect(); | |
| let root = if args.len() >= 2 { | |
| args[1].to_owned() | |
| } else { | |
| return Err(anyhow::anyhow!("you must specify target directory")); | |
| }; | |
| let dir = fs::read_dir(&root)?; | |
| for item in dir { | |
| match item { | |
| Ok(v) => { | |
| let ftype = v.file_type()?; | |
| let p = v.path(); | |
| if ftype.is_file() { | |
| if let Err(e) = output_ver_info(p.as_path()) { | |
| eprintln!("failed to parse ver info({:?}): {:?}", p.as_path(), e); | |
| } | |
| } | |
| }, | |
| Err(e) => { | |
| eprintln!("failed to enum dir: {:?}", e) | |
| } | |
| } | |
| } | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment