2025-09-27 00:31:18 +02:00
|
|
|
mod formats;
|
2025-10-08 15:14:53 +02:00
|
|
|
mod keys;
|
2025-10-30 22:04:59 +01:00
|
|
|
mod utils;
|
2025-09-27 00:31:18 +02:00
|
|
|
|
2025-09-11 20:26:51 +02:00
|
|
|
use clap::Parser;
|
|
|
|
|
use std::path::{PathBuf};
|
2025-10-07 21:43:49 +02:00
|
|
|
use std::io::{self};
|
|
|
|
|
use std::fs::{self, File};
|
2026-02-05 15:18:26 +01:00
|
|
|
use crate::formats::{Format, get_registry};
|
2025-09-11 20:26:51 +02:00
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
|
struct Args {
|
|
|
|
|
input_target: String,
|
2025-12-10 20:27:32 +01:00
|
|
|
output_folder: Option<String>,
|
2025-09-11 20:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 15:18:26 +01:00
|
|
|
pub struct ProgramContext<'a> {
|
|
|
|
|
pub file: &'a std::fs::File,
|
|
|
|
|
pub output_dir: &'a str,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-11 20:26:51 +02:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("unixtract Firmware extractor");
|
2025-09-11 20:26:51 +02:00
|
|
|
let args = Args::parse();
|
|
|
|
|
|
|
|
|
|
let target_path = args.input_target;
|
|
|
|
|
println!("Input target: {}", target_path);
|
2025-12-10 20:27:32 +01:00
|
|
|
let path = PathBuf::from(target_path);
|
|
|
|
|
|
|
|
|
|
let output_path = if args.output_folder.is_some() {
|
|
|
|
|
args.output_folder.unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
format!("_{}", path.file_name().and_then(|s| s.to_str()).unwrap())
|
|
|
|
|
};
|
2025-12-05 20:23:32 +01:00
|
|
|
println!("Output folder: {}\n", output_path);
|
2025-09-11 20:26:51 +02:00
|
|
|
|
2025-10-07 21:43:49 +02:00
|
|
|
let output_folder_path = PathBuf::from(&output_path);
|
|
|
|
|
if output_folder_path.exists() {
|
|
|
|
|
if output_folder_path.is_dir() {
|
|
|
|
|
let is_empty = fs::read_dir(&output_folder_path)?.next().is_none();
|
|
|
|
|
if !is_empty {
|
2025-12-10 20:27:32 +01:00
|
|
|
println!("Warning: Output folder already exists and is NOT empty! Files may be overwritten!");
|
2025-10-07 21:43:49 +02:00
|
|
|
println!("Press Enter if you want to continue...");
|
|
|
|
|
io::stdin().read_line(&mut String::new())?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-05 15:18:26 +01:00
|
|
|
|
|
|
|
|
let file = File::open(path)?;
|
|
|
|
|
let program_context: ProgramContext = ProgramContext { file: &file, output_dir: &output_path };
|
|
|
|
|
let formats: Vec<Format> = get_registry();
|
|
|
|
|
|
|
|
|
|
for format in formats {
|
|
|
|
|
if let Some(ctx) = (format.detect_func)(&program_context)? {
|
|
|
|
|
println!("{} detected!", format.name);
|
|
|
|
|
(format.run_func)(&program_context, Some(ctx))?;
|
|
|
|
|
return Ok(());
|
2025-09-11 20:26:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 15:18:26 +01:00
|
|
|
println!("\nInput format not recognized!");
|
2025-09-11 20:26:51 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|