Crates.io | rustbin |
lib.rs | rustbin |
version | 0.8.0 |
source | src |
created_at | 2024-05-08 21:29:20.211702 |
updated_at | 2024-06-26 17:17:50.151399 |
description | Parsers for binary file formats (mostly Windows related). |
homepage | |
repository | https://github.com/sunilkr/rustbin |
max_upload_size | |
id | 1234584 |
size | 299,256 |
This is a learning project to understand Rust language and create fairly complex file parsers.
Rustbin is created as a library which shall add more file parsers.
Every value which is part of a header is wrapped in HeaderField
struct. HeaderField
struct provides 3 values:
Note: This example works only if built on Windows OS.
extern crate rustbin;
extern crate serde_json;
use std::{env, fs::OpenOptions, io::BufReader, path::Path};
use rustbin::{pe::{PeImage, ser::min::MinPeImage}, types::Header};
fn main() {
//Create PathBuf for self (on Windows only).
let args:Vec<String> = env::args().collect();
let exe_name = args.get(0).unwrap();
let binpath = Path::new(&exe_name);
//Open file handle in read mode.
let Ok(f) = OpenOptions::new()
.read(true)
.open(binpath)
else {
panic!("Failed to open file in read mode.");
};
//Parse the file from offset 0.
let Ok(parsed) = parse_file(f, ParseAs::PE) else {
panic!("Failed to parse as `PE`.");
};
let ParsedAs::PE(pe_image) = parsed;
//Convert parsed image to a minimal set of `serde::Serialize`able values without metadata.
let min_pe = MinPeImage::from(&pe_image);
//Serialize minimal pe image to indented json.
let json_str = serde_json::to_string_pretty(&min_pe).unwrap();
println!("{}", json_str);
}