mirror of
https://github.com/Ap0dexMe0/unixtract.git
synced 2026-07-16 05:04:22 +02:00
nvt_timg: add sparse support + some extra changes
This commit is contained in:
+33
-18
@@ -1,23 +1,36 @@
|
|||||||
use std::str;
|
use std::str;
|
||||||
use std::path::{Path};
|
use std::path::{Path};
|
||||||
use std::io::{Write};
|
use std::io::{Seek, Write};
|
||||||
use std::fs::{self, File, OpenOptions};
|
use std::fs::{self, File, OpenOptions};
|
||||||
use binrw::{BinRead, BinReaderExt};
|
use binrw::{BinRead, BinReaderExt};
|
||||||
|
|
||||||
use crate::utils::common;
|
use crate::utils::common;
|
||||||
use crate::utils::compression::{decompress_gzip};
|
use crate::utils::compression::{decompress_gzip};
|
||||||
|
use crate::utils::sparse::{unsparse_to_file};
|
||||||
|
|
||||||
|
#[derive(Debug, BinRead)]
|
||||||
|
struct TIMG {
|
||||||
|
#[br(count = 4)] _magic_bytes: Vec<u8>, //TIMG
|
||||||
|
_unused1: u32,
|
||||||
|
data_size: u32,
|
||||||
|
_unused2: u32,
|
||||||
|
#[br(count = 16)] _md5_checksum: Vec<u8>,
|
||||||
|
#[br(count = 256)] _signature: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, BinRead)]
|
#[derive(Debug, BinRead)]
|
||||||
struct PIMG {
|
struct PIMG {
|
||||||
#[br(count = 4)] magic_bytes: Vec<u8>,
|
#[br(count = 4)] magic_bytes: Vec<u8>, //PIMG
|
||||||
_unknown1: u32,
|
_unused1: u32,
|
||||||
size: u32,
|
size: u32,
|
||||||
_unknown2: u32,
|
_unused2: u32,
|
||||||
#[br(count = 16)] _checksum: Vec<u8>,
|
#[br(count = 16)] _md5_checksum: Vec<u8>,
|
||||||
#[br(count = 16)] name_bytes: Vec<u8>,
|
#[br(count = 16)] name_bytes: Vec<u8>,
|
||||||
#[br(count = 64)] dest_dev_bytes: Vec<u8>,
|
#[br(count = 64)] dest_dev_bytes: Vec<u8>,
|
||||||
#[br(count = 16)] comp_type_bytes: Vec<u8>,
|
#[br(count = 16)] comp_type_bytes: Vec<u8>,
|
||||||
#[br(count = 1032)] _comment: Vec<u8>,
|
_unknown1: u32,
|
||||||
|
#[br(count = 1024)] _comment: Vec<u8>,
|
||||||
|
_unknown2: u32,
|
||||||
}
|
}
|
||||||
impl PIMG {
|
impl PIMG {
|
||||||
fn name(&self) -> String {
|
fn name(&self) -> String {
|
||||||
@@ -41,15 +54,14 @@ pub fn is_nvt_timg_file(file: &File) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_nvt_timg(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn extract_nvt_timg(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let _timg = common::read_exact(&mut file, 288)?; //TIMG magic + header
|
let file_size = file.metadata()?.len();
|
||||||
|
let timg: TIMG = file.read_le()?;
|
||||||
|
println!("File info:\nData size: {}", timg.data_size);
|
||||||
|
|
||||||
let mut pimg_i = 1;
|
let mut pimg_i = 0;
|
||||||
loop {
|
while file.stream_position()? < file_size as u64 {
|
||||||
let pimg = match file.read_le::<PIMG>() {
|
pimg_i += 1;
|
||||||
Ok(val) => val,
|
let pimg: PIMG = file.read_le()?;
|
||||||
Err(_) => break, // EOF
|
|
||||||
};
|
|
||||||
//there is an old format of TIMG, this is just temporary fix to prevent false extraction until i implement it.
|
|
||||||
if pimg.magic_bytes != b"PIMG" {
|
if pimg.magic_bytes != b"PIMG" {
|
||||||
println!("Invalid PIMG magic!");
|
println!("Invalid PIMG magic!");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -60,19 +72,23 @@ pub fn extract_nvt_timg(mut file: &File, output_folder: &str) -> Result<(), Box<
|
|||||||
println!("\n#{} - {}, Size: {}, Dest: {}, Compression: {}", pimg_i, pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
|
println!("\n#{} - {}, Size: {}, Dest: {}, Compression: {}", pimg_i, pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
|
||||||
|
|
||||||
let out_data;
|
let out_data;
|
||||||
|
let output_path = Path::new(&output_folder).join(pimg.name() + ".bin");
|
||||||
|
|
||||||
if pimg.comp_type() == "gzip" && data.starts_with(b"\x1F\x8B") { //additionally check for gzip header, because sometimes its deceptive
|
if pimg.comp_type() == "gzip" && data.starts_with(b"\x1F\x8B") { //additionally check for gzip header, because sometimes its deceptive
|
||||||
println!("- Decompressing gzip...");
|
println!("- Decompressing gzip...");
|
||||||
out_data = decompress_gzip(&data)?;
|
out_data = decompress_gzip(&data)?;
|
||||||
} else if pimg.comp_type() == "none" {
|
} else if pimg.comp_type() == "none" || pimg.comp_type() == "" {
|
||||||
out_data = data;
|
out_data = data;
|
||||||
|
} else if pimg.comp_type() == "sparse" {
|
||||||
|
println!("- Unsparsing...");
|
||||||
|
unsparse_to_file(&data, output_path)?;
|
||||||
|
println!("-- Saved file!");
|
||||||
|
continue
|
||||||
} else {
|
} else {
|
||||||
println!("- Warning: unsupported compression type!");
|
println!("- Warning: unsupported compression type!");
|
||||||
out_data = data;
|
out_data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
let output_path = Path::new(&output_folder).join(pimg.name() + ".bin");
|
|
||||||
|
|
||||||
fs::create_dir_all(&output_folder)?;
|
fs::create_dir_all(&output_folder)?;
|
||||||
let mut out_file = OpenOptions::new()
|
let mut out_file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
@@ -82,7 +98,6 @@ pub fn extract_nvt_timg(mut file: &File, output_folder: &str) -> Result<(), Box<
|
|||||||
out_file.write_all(&out_data)?;
|
out_file.write_all(&out_data)?;
|
||||||
|
|
||||||
println!("-- Saved file!");
|
println!("-- Saved file!");
|
||||||
pimg_i += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("\nExtraction finished!");
|
println!("\nExtraction finished!");
|
||||||
|
|||||||
Reference in New Issue
Block a user