From c72966cd7e4cea30bd9fa812bd78f19ff79dfb42 Mon Sep 17 00:00:00 2001 From: theubusu <80545678+theubusu@users.noreply.github.com> Date: Tue, 17 Feb 2026 18:57:14 +0100 Subject: [PATCH] always seek file to start before starting extractor --- src/formats/amlogic/mod.rs | 4 +--- src/formats/epk/mod.rs | 5 ++++- src/formats/epk2/mod.rs | 1 - src/formats/epk2b/mod.rs | 2 +- src/formats/epk3/mod.rs | 4 ++-- src/formats/rvp/mod.rs | 17 +++++++++++------ src/formats/sddl_sec/mod.rs | 1 - src/main.rs | 8 +++++++- 8 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/formats/amlogic/mod.rs b/src/formats/amlogic/mod.rs index b68190b..a704c70 100644 --- a/src/formats/amlogic/mod.rs +++ b/src/formats/amlogic/mod.rs @@ -4,7 +4,7 @@ use crate::AppContext; use std::path::Path; use std::fs::{self, OpenOptions}; -use std::io::{Write, Seek, SeekFrom}; +use std::io::Write; use binrw::BinReaderExt; use crate::utils::common; @@ -25,9 +25,7 @@ pub fn is_amlogic_file(app_ctx: &AppContext) -> Result>, Box 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()?; - println!("File info -\nImage size: {}\nItem align size: {}\nItem count: {}\nFormat version: {}", header.image_size, header.item_align_size, header.item_count, header.version); diff --git a/src/formats/epk/mod.rs b/src/formats/epk/mod.rs index d23fbec..5643887 100644 --- a/src/formats/epk/mod.rs +++ b/src/formats/epk/mod.rs @@ -1,4 +1,5 @@ use std::any::Any; +use std::io::Seek; use crate::AppContext; use crate::utils::common; @@ -49,7 +50,7 @@ fn match_with_pattern(data: &[u8], pattern: &str) -> bool { } pub fn extract_epk(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { - let file = app_ctx.file().ok_or("Extractor expected file")?; + let mut file = app_ctx.file().ok_or("Extractor expected file")?; let ctx = ctx.downcast::().expect("Missing context"); let versions = common::read_file(&file, 1712, 36)?; @@ -58,6 +59,8 @@ pub fn extract_epk(app_ctx: &AppContext, ctx: Box) -> Result<(), Box Result>, Box) -> Result<(), Box> { let mut file = app_ctx.file().ok_or("Extractor expected file")?; - file.seek(SeekFrom::Start(0))?; 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/mod.rs b/src/formats/epk2b/mod.rs index 53996cd..6d5735d 100644 --- a/src/formats/epk2b/mod.rs +++ b/src/formats/epk2b/mod.rs @@ -13,8 +13,8 @@ use include::*; pub fn is_epk2b_file(app_ctx: &AppContext) -> Result>, Box> { 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)?; + let epk2_magic = common::read_file(&file, 12, 4)?; if epak_magic == b"epak" && epk2_magic == b"EPK2" { Ok(Some(Box::new(()))) } else { diff --git a/src/formats/epk3/mod.rs b/src/formats/epk3/mod.rs index 112260f..804b252 100644 --- a/src/formats/epk3/mod.rs +++ b/src/formats/epk3/mod.rs @@ -4,7 +4,7 @@ use crate::AppContext; use std::fs::{self, OpenOptions}; use std::path::Path; -use std::io::{Write, Seek, SeekFrom, Cursor}; +use std::io::{Write, Cursor}; use binrw::BinReaderExt; use crate::utils::common; @@ -18,7 +18,7 @@ pub fn is_epk3_file(_app_ctx: &AppContext) -> Result>, 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; let _header_signature; diff --git a/src/formats/rvp/mod.rs b/src/formats/rvp/mod.rs index acebb10..1f896fc 100644 --- a/src/formats/rvp/mod.rs +++ b/src/formats/rvp/mod.rs @@ -9,13 +9,16 @@ use std::io::{Write, Read, Cursor, Seek}; use crate::utils::common; use include::*; +pub struct RvpContext { + header_offset: u64, +} + pub fn is_rvp_file(app_ctx: &AppContext) -> Result>, Box> { - let mut file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; + let 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" { - file.seek(std::io::SeekFrom::Start(36))?; //skip rest of header // NOT GOOD PRACTICE SHOULD BE REMOVED - return Ok(Some(Box::new(()))) + return Ok(Some(Box::new(RvpContext {header_offset: 36}))) } //RVP @@ -26,12 +29,14 @@ pub fn is_rvp_file(app_ctx: &AppContext) -> Result>, Box) -> Result<(), Box> { +pub fn extract_rvp(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"); + + file.seek(std::io::SeekFrom::Start(ctx.header_offset))?; 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/sddl_sec/mod.rs b/src/formats/sddl_sec/mod.rs index 96db2cc..5211446 100644 --- a/src/formats/sddl_sec/mod.rs +++ b/src/formats/sddl_sec/mod.rs @@ -66,7 +66,6 @@ fn parse_tdi_to_modules(tdi_data: Vec) -> Result, Box) -> Result<(), Box> { let mut file = app_ctx.file().ok_or("Extractor expected file")?; - file.seek(SeekFrom::Start(0))?; let mut secfile_hdr_reader = Cursor::new(decipher(&common::read_exact(&mut file, 32)?)); let secfile_header: SecHeader = secfile_hdr_reader.read_be()?; diff --git a/src/main.rs b/src/main.rs index bba8397..7d23419 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod utils; use clap::Parser; use std::path::{PathBuf}; -use std::io::{self}; +use std::io::{self, Seek, SeekFrom}; use std::fs::{self, File}; use crate::formats::{Format, get_registry}; @@ -89,6 +89,12 @@ fn main() -> Result<(), Box> { for format in formats { if let Some(ctx) = (format.detector_func)(&app_ctx)? { println!("\n{} detected!", format.name); + + //reset seek of the file if present + if let Some(mut file) = app_ctx.file() { + file.seek(SeekFrom::Start(0))?; + } + (format.extractor_func)(&app_ctx, ctx)?; //extractor returned with no error