From 438d106c96b539ccb79f3623f96b30574bfee25b Mon Sep 17 00:00:00 2001 From: theubusu <80545678+theubusu@users.noreply.github.com> Date: Fri, 6 Feb 2026 16:27:13 +0100 Subject: [PATCH] newreg: simplify getting file/dir and context --- src/formats.rs | 2 +- src/formats/amlogic.rs | 8 ++++---- src/formats/android_ota_payload.rs | 8 ++++---- src/formats/bdl.rs | 8 ++++---- src/formats/epk.rs | 14 +++++++------- src/formats/epk1.rs | 8 ++++---- src/formats/epk2.rs | 8 ++++---- src/formats/epk2b.rs | 8 ++++---- src/formats/epk3.rs | 6 +++--- src/formats/funai_upg.rs | 8 ++++---- src/formats/invincible_image.rs | 8 ++++---- src/formats/msd10.rs | 8 ++++---- src/formats/msd11.rs | 8 ++++---- src/formats/mstar.rs | 8 ++++---- src/formats/mtk_bdp.rs | 10 +++++----- src/formats/mtk_pkg.rs | 10 +++++----- src/formats/mtk_pkg_new.rs | 10 +++++----- src/formats/mtk_pkg_old.rs | 10 +++++----- src/formats/novatek.rs | 8 ++++---- src/formats/nvt_timg.rs | 8 ++++---- src/formats/pana_dvd.rs | 10 +++++----- src/formats/pfl_upg.rs | 8 ++++---- src/formats/pup.rs | 8 ++++---- src/formats/roku.rs | 8 ++++---- src/formats/ruf.rs | 8 ++++---- src/formats/rvp.rs | 8 ++++---- src/formats/samsung_old.rs | 8 ++++---- src/formats/sddl_sec.rs | 8 ++++---- src/formats/slp.rs | 8 ++++---- src/formats/sony_bdp.rs | 8 ++++---- src/main.rs | 21 ++++++++++++++++++--- 31 files changed, 142 insertions(+), 127 deletions(-) diff --git a/src/formats.rs b/src/formats.rs index 38b9035..47f8a89 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -4,7 +4,7 @@ use crate::AppContext; pub struct Format { pub name: &'static str, pub detector_func: fn(&AppContext) -> Result>, Box>, - pub extractor_func: fn(&AppContext, Option>) -> Result<(), Box>, + pub extractor_func: fn(&AppContext, Box) -> Result<(), Box>, } pub mod mstar; diff --git a/src/formats/amlogic.rs b/src/formats/amlogic.rs index 2e8521c..74e55f1 100644 --- a/src/formats/amlogic.rs +++ b/src/formats/amlogic.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "amlogic", detector_func: is_amlogic_file, extractor_func: extract_amlogic } } @@ -50,7 +50,7 @@ impl ItemEntry { } pub fn is_amlogic_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 8, 4)?; if header == b"\x56\x19\xB5\x27" { @@ -60,8 +60,8 @@ pub fn is_amlogic_file(app_ctx: &AppContext) -> Result>, Box } } -pub fn extract_amlogic(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_amlogic(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; file.seek(SeekFrom::Start(0))?; let header: ImageHeader = file.read_le()?; diff --git a/src/formats/android_ota_payload.rs b/src/formats/android_ota_payload.rs index be173fd..0fa13fd 100644 --- a/src/formats/android_ota_payload.rs +++ b/src/formats/android_ota_payload.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "android_ota_payload", detector_func: is_android_ota_payload_file, extractor_func: extract_android_ota_payload } } @@ -23,7 +23,7 @@ struct Header { } pub fn is_android_ota_payload_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"CrAU" { @@ -33,8 +33,8 @@ pub fn is_android_ota_payload_file(app_ctx: &AppContext) -> Result>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_android_ota_payload(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_be()?; println!("File info:\nFormat version: {}\nManifest size: {}", header.file_format_version, header.manifest_size); diff --git a/src/formats/bdl.rs b/src/formats/bdl.rs index 29bf0f0..bd7952f 100644 --- a/src/formats/bdl.rs +++ b/src/formats/bdl.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "bdl", detector_func: is_bdl_file, extractor_func: extract_bdl } } @@ -86,7 +86,7 @@ impl PkgEntry { } pub fn is_bdl_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"ibdl" { @@ -96,8 +96,8 @@ pub fn is_bdl_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_bdl(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: BdlHeader = file.read_le()?; diff --git a/src/formats/epk.rs b/src/formats/epk.rs index 994cd8b..9307d0b 100644 --- a/src/formats/epk.rs +++ b/src/formats/epk.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "epk", detector_func: is_epk_file, extractor_func: extract_epk } } @@ -12,7 +12,7 @@ pub struct EpkContext { } pub fn is_epk_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let versions = common::read_file(&file, 1712, 36)?; if let Some(epk_version) = check_epk_version(&versions) { @@ -51,9 +51,9 @@ fn match_with_pattern(data: &[u8], pattern: &str) -> bool { true } -pub fn extract_epk(app_ctx: &AppContext, ctx: Option>) -> Result<(), Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; - let ctx = ctx.and_then(|c| c.downcast::().ok()).ok_or("Context is invalid or missing!")?; +pub fn extract_epk(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { + let file = app_ctx.file().ok_or("Extractor expected file")?; + let ctx = ctx.downcast::().expect("Missing context"); let versions = common::read_file(&file, 1712, 36)?; @@ -63,10 +63,10 @@ pub fn extract_epk(app_ctx: &AppContext, ctx: Option>) -> Result<() if ctx.epk_version == 2 { println!("EPK2 detected!\n"); - formats::epk2::extract_epk2(app_ctx, None)?; + formats::epk2::extract_epk2(app_ctx, Box::new(()))?; } else if ctx.epk_version == 3 { println!("EPK3 detected!\n"); - formats::epk3::extract_epk3(app_ctx, None)?; + formats::epk3::extract_epk3(app_ctx, Box::new(()))?; } Ok(()) diff --git a/src/formats/epk1.rs b/src/formats/epk1.rs index afc477a..12a177b 100644 --- a/src/formats/epk1.rs +++ b/src/formats/epk1.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "epk1", detector_func: is_epk1_file, extractor_func: extract_epk1 } } @@ -41,7 +41,7 @@ struct Pak { } pub fn is_epk1_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let epk2_magic = common::read_file(&file, 12, 4)?; //for epk2b let epak_magic = common::read_file(&file, 0, 4)?; @@ -52,8 +52,8 @@ pub fn is_epk1_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_epk1(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; //check type of epk1 let epk1_type; let init_pak_count_bytes = common::read_file(&file, 8, 4)?; diff --git a/src/formats/epk2.rs b/src/formats/epk2.rs index a3ed2c2..79dbb1b 100644 --- a/src/formats/epk2.rs +++ b/src/formats/epk2.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "epk2", detector_func: is_epk2_file, extractor_func: extract_epk2 } } @@ -72,7 +72,7 @@ struct Pak { } pub fn is_epk2_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 128, 4)?; if header == b"epak" { @@ -82,8 +82,8 @@ pub fn is_epk2_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_epk2(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let _header_signature = common::read_exact(&mut file, SIGNATURE_SIZE as usize)?; let stored_header = common::read_exact(&mut file, 1584)?; //max header size diff --git a/src/formats/epk2b.rs b/src/formats/epk2b.rs index 110517c..ef39257 100644 --- a/src/formats/epk2b.rs +++ b/src/formats/epk2b.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "epk2b", detector_func: is_epk2b_file, extractor_func: extract_epk2b } } @@ -57,7 +57,7 @@ struct Pak { } pub fn is_epk2b_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let epk2_magic = common::read_file(&file, 12, 4)?; let epak_magic = common::read_file(&file, 0, 4)?; @@ -68,8 +68,8 @@ pub fn is_epk2b_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_epk2b(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: EpkHeader = file.read_le()?; println!("EPK info -\nData size: {}\nPak count: {}\nOTA ID: {}\nVersion: {:02x?}.{:02x?}.{:02x?}", diff --git a/src/formats/epk3.rs b/src/formats/epk3.rs index b48fdd1..ed935fb 100644 --- a/src/formats/epk3.rs +++ b/src/formats/epk3.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "epk3", detector_func: is_epk3_file, extractor_func: extract_epk3 } } @@ -80,8 +80,8 @@ pub fn is_epk3_file(_app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_epk3(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; file.seek(SeekFrom::Start(0))?; let stored_header = common::read_exact(&mut file, 1712)?; let header: Vec; diff --git a/src/formats/funai_upg.rs b/src/formats/funai_upg.rs index e398843..31c86c3 100644 --- a/src/formats/funai_upg.rs +++ b/src/formats/funai_upg.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "funai_upg", detector_func: is_funai_upg_file, extractor_func: extract_funai_upg } } @@ -26,7 +26,7 @@ struct Entry { } pub fn is_funai_upg_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 6)?; if header == b"UPG\x00\x00\x00" { Ok(Some(Box::new(()))) @@ -35,8 +35,8 @@ pub fn is_funai_upg_file(app_ctx: &AppContext) -> Result>, B } } -pub fn extract_funai_upg(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_funai_upg(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_le()?; println!("File info:\nFile size: {}\nEntry count: {}", header.file_size, header.entry_count); diff --git a/src/formats/invincible_image.rs b/src/formats/invincible_image.rs index 5d21dcd..33a0247 100644 --- a/src/formats/invincible_image.rs +++ b/src/formats/invincible_image.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "invincible_image", detector_func: is_invincible_image_file, extractor_func: extract_invincible_image } } @@ -63,7 +63,7 @@ impl Entry { } pub fn is_invincible_image_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 16)?; if header == b"INVINCIBLE_IMAGE" { @@ -73,8 +73,8 @@ pub fn is_invincible_image_file(app_ctx: &AppContext) -> Result>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_invincible_image(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_le()?; println!("File info:\nFile Version: {}.{}\nVersion(1): {}\nVersion(2): {}\nVersion(3): {}\nVersion(4): {}\nData size: {}\nData start offset: {}\nKeep data size: {}\nSkip data size: {}\n\nPayload Count: {}", diff --git a/src/formats/msd10.rs b/src/formats/msd10.rs index 7bc2e96..49acb85 100644 --- a/src/formats/msd10.rs +++ b/src/formats/msd10.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "msd10", detector_func: is_msd10_file, extractor_func: extract_msd10 } } @@ -48,7 +48,7 @@ struct Section { } pub fn is_msd10_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 6)?; if header == b"MSDU10" { @@ -58,8 +58,8 @@ pub fn is_msd10_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_msd10(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: FileHeader = file.read_le()?; println!("\nNumber of sections: {}", header.section_count); diff --git a/src/formats/msd11.rs b/src/formats/msd11.rs index 1159d86..1c5bd1d 100644 --- a/src/formats/msd11.rs +++ b/src/formats/msd11.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "msd11", detector_func: is_msd11_file, extractor_func: extract_msd11 } } @@ -49,7 +49,7 @@ struct Section { } pub fn is_msd11_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 6)?; if header == b"MSDU11" { @@ -59,8 +59,8 @@ pub fn is_msd11_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_msd11(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: FileHeader = file.read_le()?; println!("\nNumber of sections: {}", header.section_count); diff --git a/src/formats/mstar.rs b/src/formats/mstar.rs index 09b6fd6..81b68cb 100644 --- a/src/formats/mstar.rs +++ b/src/formats/mstar.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "mstar", detector_func: is_mstar_file, extractor_func: extract_mstar } } @@ -14,7 +14,7 @@ use crate::utils::lzop::{unlzop_to_file}; use crate::utils::sparse::{unsparse_to_file}; pub fn is_mstar_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 32768)?; let header_string = String::from_utf8_lossy(&header); @@ -33,8 +33,8 @@ fn parse_number(s: &str) -> Option { } } -pub fn extract_mstar(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_mstar(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let file = app_ctx.file().ok_or("Extractor expected file")?; let mut script = common::read_file(&file, 0, 32768)?; diff --git a/src/formats/mtk_bdp.rs b/src/formats/mtk_bdp.rs index 6b66f1c..ba01d1b 100644 --- a/src/formats/mtk_bdp.rs +++ b/src/formats/mtk_bdp.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "mtk_bdp", detector_func: is_mtk_bdp_file, extractor_func: extract_mtk_bdp } } @@ -78,7 +78,7 @@ fn find_bytes(data: &[u8], pattern: &[u8]) -> Option { } pub fn is_mtk_bdp_file(app_ctx: &AppContext) -> Result>, Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let mut file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let file_size = file.metadata()?.len(); let mut data = Vec::new(); @@ -95,9 +95,9 @@ pub fn is_mtk_bdp_file(app_ctx: &AppContext) -> Result>, Box } } -pub fn extract_mtk_bdp(app_ctx: &AppContext, ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; - let ctx = ctx.and_then(|c: Box| c.downcast::().ok()).ok_or("Context is invalid or missing!")?; +pub fn extract_mtk_bdp(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; + let ctx = ctx.downcast::().expect("Missing context"); let offset = ctx.pitit_offset; println!("\nReading PITIT at: {}", offset); diff --git a/src/formats/mtk_pkg.rs b/src/formats/mtk_pkg.rs index 087934d..9b8bac5 100644 --- a/src/formats/mtk_pkg.rs +++ b/src/formats/mtk_pkg.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "mtk_pkg", detector_func: is_mtk_pkg_file, extractor_func: extract_mtk_pkg } } @@ -82,7 +82,7 @@ static HEADER_KEY: [u8; 16] = [ static HEADER_IV: [u8; 16] = [0x00; 16]; pub fn is_mtk_pkg_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let mut encrypted_header = common::read_file(&file, 0, HEADER_SIZE)?; let mut header = decrypt_aes128_cbc_nopad(&encrypted_header, &HEADER_KEY, &HEADER_IV)?; @@ -101,9 +101,9 @@ pub fn is_mtk_pkg_file(app_ctx: &AppContext) -> Result>, Box } } -pub fn extract_mtk_pkg(app_ctx: &AppContext, ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; - let ctx = ctx.and_then(|c: Box| c.downcast::().ok()).ok_or("Context is invalid or missing!")?; +pub fn extract_mtk_pkg(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; + let ctx = ctx.downcast::().expect("Missing context"); let file_size = file.metadata()?.len(); let header = ctx.decrypted_header; diff --git a/src/formats/mtk_pkg_new.rs b/src/formats/mtk_pkg_new.rs index 2fdb74b..fd0e664 100644 --- a/src/formats/mtk_pkg_new.rs +++ b/src/formats/mtk_pkg_new.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "mtk_pkg_new", detector_func: is_mtk_pkg_new_file, extractor_func: extract_mtk_pkg_new } } @@ -71,7 +71,7 @@ impl PartEntry { static HEADER_SIZE: usize = 0x170; pub fn is_mtk_pkg_new_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let encrypted_header = common::read_file(&file, 0, HEADER_SIZE)?; for (key_hex, iv_hex, name) in keys::MTK_PKG_CUST { @@ -92,9 +92,9 @@ pub fn is_mtk_pkg_new_file(app_ctx: &AppContext) -> Result>, Ok(None) } -pub fn extract_mtk_pkg_new(app_ctx: &AppContext, ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; - let ctx = ctx.and_then(|c: Box| c.downcast::().ok()).ok_or("Context is invalid or missing!")?; +pub fn extract_mtk_pkg_new(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; + let ctx = ctx.downcast::().expect("Missing context"); let file_size = file.metadata()?.len(); diff --git a/src/formats/mtk_pkg_old.rs b/src/formats/mtk_pkg_old.rs index f70c32b..3ffe9e1 100644 --- a/src/formats/mtk_pkg_old.rs +++ b/src/formats/mtk_pkg_old.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "mtk_pkg_old", detector_func: is_mtk_pkg_old_file, extractor_func: extract_mtk_pkg_old } } @@ -64,8 +64,8 @@ impl PartEntry { static HEADER_SIZE: usize = 0x98; pub fn is_mtk_pkg_old_file(app_ctx: &AppContext) -> Result>, Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; - + let mut file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; + let encrypted_header = common::read_file(&file, 0, HEADER_SIZE)?; let header = decrypt(&encrypted_header, KEY, Some(HEADER_XOR_MASK)); if &header[4..12] == MTK_HEADER_MAGIC { @@ -83,8 +83,8 @@ pub fn is_mtk_pkg_old_file(app_ctx: &AppContext) -> Result>, } } -pub fn extract_mtk_pkg_old(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_mtk_pkg_old(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let file_size = file.metadata()?.len(); let encrypted_header = common::read_exact(&mut file, HEADER_SIZE)?; diff --git a/src/formats/novatek.rs b/src/formats/novatek.rs index b8a22d3..45ead49 100644 --- a/src/formats/novatek.rs +++ b/src/formats/novatek.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "novatek", detector_func: is_novatek_file, extractor_func: extract_novatek } } @@ -41,7 +41,7 @@ struct PartEntry { } pub fn is_novatek_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"NFWB" { @@ -51,8 +51,8 @@ pub fn is_novatek_file(app_ctx: &AppContext) -> Result>, Box } } -pub fn extract_novatek(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_novatek(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_le()?; println!("File info:\nFirmware name: {}\nVersion: {}.{}\nData size: {}\nPart count: {}", diff --git a/src/formats/nvt_timg.rs b/src/formats/nvt_timg.rs index afcb85c..040a89e 100644 --- a/src/formats/nvt_timg.rs +++ b/src/formats/nvt_timg.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "nvt_timg", detector_func: is_nvt_timg_file, extractor_func: extract_nvt_timg } } @@ -50,7 +50,7 @@ impl PIMG { } pub fn is_nvt_timg_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"TIMG" { @@ -60,8 +60,8 @@ pub fn is_nvt_timg_file(app_ctx: &AppContext) -> Result>, Bo } } -pub fn extract_nvt_timg(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_nvt_timg(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let file_size = file.metadata()?.len(); let timg: TIMG = file.read_le()?; diff --git a/src/formats/pana_dvd.rs b/src/formats/pana_dvd.rs index 76769d2..5eda4e8 100644 --- a/src/formats/pana_dvd.rs +++ b/src/formats/pana_dvd.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "pana_dvd", detector_func: is_pana_dvd_file, extractor_func: extract_pana_dvd } } @@ -150,7 +150,7 @@ pub fn find_aes_key_pair<'a>(key_array: &'a [(&'a str, &'a str, &'a str)], data: } pub fn is_pana_dvd_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 64)?; if let Some(matching_key) = find_key(&keys::PANA_DVD_KEYONLY, &header, b"PROG", 0)? { Ok(Some(Box::new(PanaDvdContext { @@ -181,9 +181,9 @@ pub fn is_pana_dvd_file(app_ctx: &AppContext) -> Result>, Bo } } -pub fn extract_pana_dvd(app_ctx: &AppContext, ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; - let context = ctx.and_then(|c: Box| c.downcast::().ok()).ok_or("Context is invalid or missing!")?; +pub fn extract_pana_dvd(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; + let context = ctx.downcast::().expect("Missing context"); let mut data = Vec::new(); // we need to load the entire file into memory so we can swap it with AES decrypted data if its AES encrypted file.read_to_end(&mut data)?; diff --git a/src/formats/pfl_upg.rs b/src/formats/pfl_upg.rs index d2dd61a..886566f 100644 --- a/src/formats/pfl_upg.rs +++ b/src/formats/pfl_upg.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "pfl_upg", detector_func: is_pfl_upg_file, extractor_func: extract_pfl_upg } } @@ -49,7 +49,7 @@ impl FileHeader { } pub fn is_pfl_upg_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 8)?; if header == b"2SWU3TXV" { @@ -91,8 +91,8 @@ fn decrypt_aes256_ecb(key: &[u8], ciphertext: &[u8]) -> Result, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_pfl_upg(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_le()?; let signature = common::read_exact(&mut file, 128)?; diff --git a/src/formats/pup.rs b/src/formats/pup.rs index b390ce4..2ad20a4 100644 --- a/src/formats/pup.rs +++ b/src/formats/pup.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "pup", detector_func: is_pup_file, extractor_func: extract_pup } } @@ -57,7 +57,7 @@ struct BlockEntry { } pub fn is_pup_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"\x4F\x15\x3D\x1D" || header == b"\x54\x14\xF5\xEE" { //ps4, ps5 @@ -67,8 +67,8 @@ pub fn is_pup_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_pup(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_le()?; println!("File info:\nFile size: {}\nEntry count: {}", diff --git a/src/formats/roku.rs b/src/formats/roku.rs index 44bc99a..6d0b78f 100644 --- a/src/formats/roku.rs +++ b/src/formats/roku.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "roku", detector_func: is_roku_file, extractor_func: extract_roku } } @@ -62,7 +62,7 @@ impl ImageHeader { } pub fn is_roku_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 32)?; let try_decrypt_header = decrypt_aes128_cbc_nopad(&header, &FILE_KEY, &FILE_IV)?; @@ -74,8 +74,8 @@ pub fn is_roku_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_roku(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let mut encrypted_data = Vec::new(); file.read_to_end(&mut encrypted_data)?; diff --git a/src/formats/ruf.rs b/src/formats/ruf.rs index ef1a67c..ea3ab7a 100644 --- a/src/formats/ruf.rs +++ b/src/formats/ruf.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "ruf", detector_func: is_ruf_file, extractor_func: extract_ruf } } @@ -76,7 +76,7 @@ impl RufEntry { } pub fn is_ruf_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 6)?; if header == b"RUF\x00\x00\x00" { @@ -86,8 +86,8 @@ pub fn is_ruf_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_ruf(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: RufHeader = file.read_be()?; if header.is_dual_ruf() { diff --git a/src/formats/rvp.rs b/src/formats/rvp.rs index 83cd619..9c2dfdc 100644 --- a/src/formats/rvp.rs +++ b/src/formats/rvp.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "rvp", detector_func: is_rvp_file, extractor_func: extract_rvp } } @@ -19,7 +19,7 @@ fn decrypt_xor(data: &[u8]) -> Vec { } pub fn is_rvp_file(app_ctx: &AppContext) -> Result>, Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let mut file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; //MVP let header = common::read_file(&file, 0, 4)?; if header == b"UPDT" { @@ -39,8 +39,8 @@ pub fn is_rvp_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_rvp(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let mut obf_data = Vec::new(); //we sadly cannot deXOR on the fly because of its 32 byte pattern file.read_to_end(&mut obf_data)?; diff --git a/src/formats/samsung_old.rs b/src/formats/samsung_old.rs index 5dad9e4..2680c83 100644 --- a/src/formats/samsung_old.rs +++ b/src/formats/samsung_old.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "samsung_old", detector_func: is_samsung_old_dir, extractor_func: extract_samsung_old } } @@ -18,7 +18,7 @@ use crate::utils::aes::{decrypt_aes128_cbc_pcks7}; use md5; pub fn is_samsung_old_dir(app_ctx: &AppContext) -> Result>, Box> { - let dir = match &app_ctx.input {InputTarget::Directory(p) => p, InputTarget::File(_) => return Ok(None)}; + let dir = match app_ctx.dir() {Some(d) => d, None => return Ok(None)}; if Path::new(&dir).join("image").is_dir() & Path::new(&dir).join("image/info.txt").exists(){ Ok(Some(Box::new(()))) @@ -35,8 +35,8 @@ fn decrypt_xor(data: &[u8], key: &str) -> Vec { .collect() } -pub fn extract_samsung_old(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let path = match &app_ctx.input {InputTarget::Directory(p) => p, InputTarget::File(_) => return Err("Extractor expected directory, not file".into())}; +pub fn extract_samsung_old(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let path = app_ctx.dir().ok_or("Extractor expected directory")?; let fw_info = fs::read_to_string(Path::new(&path).join("image/info.txt"))?; println!("Firmware info: {}", fw_info); diff --git a/src/formats/sddl_sec.rs b/src/formats/sddl_sec.rs index 73b59f1..0734d5f 100644 --- a/src/formats/sddl_sec.rs +++ b/src/formats/sddl_sec.rs @@ -1,6 +1,6 @@ //sddl_dec 5.0 use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "sddl_sec", detector_func: is_sddl_sec_file, extractor_func: extract_sddl_sec } } @@ -104,7 +104,7 @@ static DEC_IV: [u8; 16] = [ ]; pub fn is_sddl_sec_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 32)?; let deciph_header = decipher(&header); @@ -157,8 +157,8 @@ fn decipher(s: &[u8]) -> Vec { out } -pub fn extract_sddl_sec(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_sddl_sec(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let mut hdr_reader = Cursor::new(decipher(&common::read_exact(&mut file, 32)?)); let hdr: SddlSecHeader = hdr_reader.read_be()?; diff --git a/src/formats/slp.rs b/src/formats/slp.rs index c0ebba4..0ec6a54 100644 --- a/src/formats/slp.rs +++ b/src/formats/slp.rs @@ -1,5 +1,5 @@ use std::any::Any; -use crate::{InputTarget, AppContext, formats::Format}; +use crate::{AppContext, formats::Format}; pub fn format() -> Format { Format { name: "slp", detector_func: is_slp_file, extractor_func: extract_slp } } @@ -53,7 +53,7 @@ struct EntryNew { } pub fn is_slp_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"SLP\x00" { @@ -63,8 +63,8 @@ pub fn is_slp_file(app_ctx: &AppContext) -> Result>, Box>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_slp(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let header: Header = file.read_le()?; println!("File info:\nModel: {}\nVersion: {}\nFirmware: {}\nNew type: {}\n", diff --git a/src/formats/sony_bdp.rs b/src/formats/sony_bdp.rs index 21a27c9..9543374 100644 --- a/src/formats/sony_bdp.rs +++ b/src/formats/sony_bdp.rs @@ -64,7 +64,7 @@ struct Entry { } pub fn is_sony_bdp_file(app_ctx: &AppContext) -> Result>, Box> { - let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)}; + let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 4)?; if header == b"\x01\x73\xEC\xC9" || header == b"\x01\x73\xEC\x1F" || header == b"\xEC\x7D\xB0\xB0" { //MSB1x, MSB0x, BDPPxx @@ -74,8 +74,8 @@ pub fn is_sony_bdp_file(app_ctx: &AppContext) -> Result>, Bo } } -pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Option>) -> Result<(), Box> { - let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())}; +pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let obf_header = common::read_exact(&mut file, 300)?; let header = hex_substitute(&obf_header); @@ -128,7 +128,7 @@ pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Option>) -> Res if let Some(result) = formats::mtk_bdp::is_mtk_bdp_file(&ctx)? { println!("- MTK BDP file detected!\n"); - formats::mtk_bdp::extract_mtk_bdp(&ctx, Some(result))?; + formats::mtk_bdp::extract_mtk_bdp(&ctx, result)?; } else { println!("- Not an MTK BDP file."); } diff --git a/src/main.rs b/src/main.rs index 1ad9d39..e602d3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,21 @@ pub struct AppContext { pub input: InputTarget, pub output_dir: PathBuf, } +impl AppContext { + pub fn file(&self) -> Option<&File> { + match &self.input { + InputTarget::File(f) => Some(f), + _ => None, + } + } + + pub fn dir(&self) -> Option<&PathBuf> { + match &self.input { + InputTarget::Directory(p) => Some(p), + _ => None, + } + } +} fn main() -> Result<(), Box> { println!("unixtract Firmware extractor"); @@ -69,12 +84,12 @@ fn main() -> Result<(), Box> { } let formats: Vec = get_registry(); - println!("Loaded {} formats!\n", formats.len()); + println!("Loaded {} formats!", formats.len()); for format in formats { if let Some(ctx) = (format.detector_func)(&app_ctx)? { - println!("{} detected!", format.name); - (format.extractor_func)(&app_ctx, Some(ctx))?; + println!("\n{} detected!", format.name); + (format.extractor_func)(&app_ctx, ctx)?; return Ok(()); } }