2025-09-25 17:49:02 +02:00
|
|
|
use std::str;
|
|
|
|
|
use std::path::{Path};
|
2025-11-26 17:29:25 +01:00
|
|
|
use std::io::{Write};
|
2025-09-25 17:49:02 +02:00
|
|
|
use std::fs::{self, File, OpenOptions};
|
2025-10-10 19:52:36 +02:00
|
|
|
use binrw::{BinRead, BinReaderExt};
|
2025-09-25 17:49:02 +02:00
|
|
|
|
2025-11-26 17:29:25 +01:00
|
|
|
use crate::utils::common;
|
|
|
|
|
use crate::utils::compression::{decompress_gzip};
|
2025-09-25 17:49:02 +02:00
|
|
|
|
2025-10-10 19:52:36 +02:00
|
|
|
#[derive(Debug, BinRead)]
|
|
|
|
|
struct PIMG {
|
|
|
|
|
#[br(count = 4)] _magic_bytes: Vec<u8>,
|
|
|
|
|
_unknown1: u32,
|
|
|
|
|
size: u32,
|
|
|
|
|
_unknown2: u32,
|
|
|
|
|
#[br(count = 16)] _checksum: Vec<u8>,
|
|
|
|
|
#[br(count = 16)] name_bytes: Vec<u8>,
|
|
|
|
|
#[br(count = 64)] dest_dev_bytes: Vec<u8>,
|
|
|
|
|
#[br(count = 16)] comp_type_bytes: Vec<u8>,
|
|
|
|
|
#[br(count = 1032)] _comment: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
impl PIMG {
|
|
|
|
|
fn name(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.name_bytes)
|
|
|
|
|
}
|
|
|
|
|
fn dest_dev(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.dest_dev_bytes)
|
|
|
|
|
}
|
|
|
|
|
fn comp_type(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.comp_type_bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 23:43:02 +01:00
|
|
|
pub fn is_nvt_timg_file(file: &File) -> bool {
|
2025-09-25 17:49:02 +02:00
|
|
|
let header = common::read_file(&file, 0, 4).expect("Failed to read from file.");
|
2025-10-07 16:00:32 +02:00
|
|
|
if header == b"TIMG" {
|
2025-09-25 17:49:02 +02:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 23:43:02 +01:00
|
|
|
pub fn extract_nvt_timg(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
|
2025-10-07 16:00:32 +02:00
|
|
|
let _timg = common::read_exact(&mut file, 288)?; //TIMG magic + header
|
2025-09-25 17:49:02 +02:00
|
|
|
|
2025-12-05 20:23:32 +01:00
|
|
|
let mut pimg_i = 1;
|
2025-09-25 17:49:02 +02:00
|
|
|
loop {
|
2025-10-10 19:52:36 +02:00
|
|
|
let pimg = match file.read_le::<PIMG>() {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(_) => break, // EOF
|
|
|
|
|
};
|
|
|
|
|
let data = common::read_exact(&mut file, pimg.size as usize)?;
|
2025-09-25 17:49:02 +02:00
|
|
|
|
2025-12-10 20:27:32 +01:00
|
|
|
println!("\n#{} - {}, Size: {}, Dest: {}, Compression: {}", pimg_i, pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
|
2025-09-25 17:49:02 +02:00
|
|
|
|
|
|
|
|
let out_data;
|
|
|
|
|
|
2025-11-23 23:43:02 +01:00
|
|
|
if pimg.comp_type() == "gzip" && data.starts_with(b"\x1F\x8B") { //additionally check for gzip header, because sometimes its deceptive
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("- Decompressing gzip...");
|
2025-09-25 17:49:02 +02:00
|
|
|
out_data = decompress_gzip(&data)?;
|
2025-10-10 19:52:36 +02:00
|
|
|
} else if pimg.comp_type() == "none" {
|
2025-09-25 17:49:02 +02:00
|
|
|
out_data = data;
|
|
|
|
|
} else {
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("- Warning: unsupported compression type!");
|
2025-09-25 17:49:02 +02:00
|
|
|
out_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-10 19:52:36 +02:00
|
|
|
let output_path = Path::new(&output_folder).join(pimg.name() + ".bin");
|
2025-09-25 17:49:02 +02:00
|
|
|
|
|
|
|
|
fs::create_dir_all(&output_folder)?;
|
|
|
|
|
let mut out_file = OpenOptions::new()
|
2025-10-06 22:44:30 +02:00
|
|
|
.write(true)
|
2025-09-25 17:49:02 +02:00
|
|
|
.create(true)
|
|
|
|
|
.open(output_path)?;
|
|
|
|
|
|
|
|
|
|
out_file.write_all(&out_data)?;
|
|
|
|
|
|
2025-10-06 22:44:30 +02:00
|
|
|
println!("-- Saved file!");
|
2025-12-05 20:23:32 +01:00
|
|
|
pimg_i += 1;
|
2025-09-25 17:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("\nExtraction finished!");
|
2025-09-25 17:49:02 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
2025-10-10 19:52:36 +02:00
|
|
|
}
|