RESTRUCTURING 1

- change formats to have their own folders with mod + include files + anything they want
- change format list to be defined in formats.rs instead of in each format
- move utils to their respective formats
- some minor code changes in some formats
This commit is contained in:
theubusu
2026-02-17 17:28:59 +01:00
parent 9fe39280cd
commit aa2249f024
69 changed files with 1678 additions and 1629 deletions
+38
View File
@@ -0,0 +1,38 @@
use crate::utils::common;
use binrw::BinRead;
#[derive(Debug, BinRead)]
pub struct TIMG {
_magic_bytes: [u8; 4], //TIMG
_unused1: u32,
pub data_size: u32,
_unused2: u32,
_md5_checksum: [u8; 16],
_signature: [u8; 256],
}
#[derive(Debug, BinRead)]
pub struct PIMG {
pub magic_bytes: [u8; 4], //PIMG
_unused1: u32,
pub size: u32,
_unused2: u32,
_md5_checksum: [u8; 16],
name_bytes: [u8; 16],
dest_dev_bytes: [u8; 64],
comp_type_bytes: [u8; 16],
_unknown1: u32,
_comment: [u8; 1024],
_unknown2: u32,
}
impl PIMG {
pub fn name(&self) -> String {
common::string_from_bytes(&self.name_bytes)
}
pub fn dest_dev(&self) -> String {
common::string_from_bytes(&self.dest_dev_bytes)
}
pub fn comp_type(&self) -> String {
common::string_from_bytes(&self.comp_type_bytes)
}
}
+74
View File
@@ -0,0 +1,74 @@
mod include;
use std::any::Any;
use crate::AppContext;
use std::path::Path;
use std::io::{Seek, Write};
use std::fs::{self, OpenOptions};
use binrw::BinReaderExt;
use crate::utils::common;
use crate::utils::compression::{decompress_gzip};
use crate::utils::sparse::{unsparse_to_file};
use include::*;
pub fn is_nvt_timg_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
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" {
Ok(Some(Box::new(())))
} else {
Ok(None)
}
}
pub fn extract_nvt_timg(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
let file_size = file.metadata()?.len();
let timg: TIMG = file.read_le()?;
println!("File info:\nData size: {}", timg.data_size);
let mut pimg_i = 0;
while file.stream_position()? < file_size as u64 {
pimg_i += 1;
let pimg: PIMG = file.read_le()?;
if &pimg.magic_bytes != b"PIMG" {
println!("Invalid PIMG magic!");
return Ok(());
}
let data = common::read_exact(&mut file, pimg.size as usize)?;
println!("\n#{} - {}, Size: {}, Dest: {}, Compression: {}", pimg_i, pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
let out_data;
let output_path = Path::new(&app_ctx.output_dir).join(pimg.name() + ".bin");
if pimg.comp_type() == "gzip" && data.starts_with(b"\x1F\x8B") { //additionally check for gzip header, because sometimes its deceptive
println!("- Decompressing gzip...");
out_data = decompress_gzip(&data)?;
} else if pimg.comp_type() == "none" || pimg.comp_type() == "" {
out_data = data;
} else if pimg.comp_type() == "sparse" {
println!("- Unsparsing...");
unsparse_to_file(&data, output_path)?;
println!("-- Saved file!");
continue
} else {
println!("- Warning: unsupported compression type!");
out_data = data;
}
fs::create_dir_all(&app_ctx.output_dir)?;
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
out_file.write_all(&out_data)?;
println!("-- Saved file!");
}
println!("\nExtraction finished!");
Ok(())
}