change novatek, tpv_timg and pfl_upg extractor to use structs

This commit is contained in:
theubusu
2025-10-10 19:52:36 +02:00
parent 20eddc3a1c
commit 9bb61f893e
6 changed files with 170 additions and 103 deletions
+37 -35
View File
@@ -3,10 +3,37 @@ use std::path::{Path};
use std::io::{Read, Write};
use std::fs::{self, File, OpenOptions};
use binrw::{BinRead, BinReaderExt};
use flate2::read::GzDecoder;
use crate::common;
#[derive(Debug, BinRead)]
#[br(little)]
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)
}
}
pub fn is_tpv_timg_file(file: &File) -> bool {
let header = common::read_file(&file, 0, 4).expect("Failed to read from file.");
if header == b"TIMG" {
@@ -28,51 +55,27 @@ pub fn extract_tpv_timg(mut file: &File, output_folder: &str) -> Result<(), Box<
let _timg = common::read_exact(&mut file, 288)?; //TIMG magic + header
loop {
//PIMG
let mut pimg = [0u8; 4];
if file.read_exact(&mut pimg).is_err() {
break; //EOF
} else {
assert!(&pimg == b"PIMG", "Invalid PIMG section!");
}
let pimg = match file.read_le::<PIMG>() {
Ok(val) => val,
Err(_) => break, // EOF
};
let data = common::read_exact(&mut file, pimg.size as usize)?;
let _ = common::read_exact(&mut file, 4)?; //4 bytes 00
let size_bytes = common::read_exact(&mut file, 4)?;
let size = u32::from_le_bytes(size_bytes.try_into().unwrap());
let _ = common::read_exact(&mut file, 4)?; //4 bytes 00
let _checksum = common::read_exact(&mut file, 16)?; //16 bytes checksum? or maybe signature
let name_bytes = common::read_exact(&mut file, 16)?;
let name = common::string_from_bytes(&name_bytes);
let dev_bytes = common::read_exact(&mut file, 64)?;
let dev = common::string_from_bytes(&dev_bytes);
let comp_bytes = common::read_exact(&mut file, 16)?;
let comp_type = common::string_from_bytes(&comp_bytes);
let _ = common::read_exact(&mut file, 1032)?; //1032 bytes maybe comment? skip this
let data = common::read_exact(&mut file, size as usize)?;
println!("\nPIMG: Name: {}, Size: {}, Dest: {}, Compression: {}", name, size, dev, comp_type);
println!("\nPIMG: Name: {}, Size: {}, Dest: {}, Compression: {}", pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
let out_data;
if comp_type == "gzip" {
if pimg.comp_type() == "gzip" {
println!("- Decompressing gzip...");
out_data = decompress_gzip(&data)?;
} else if comp_type == "none" {
} else if pimg.comp_type() == "none" {
out_data = data;
} else {
println!("- Warning: unsupported compression type!");
out_data = data;
}
let output_path = Path::new(&output_folder).join(name + ".bin");
let output_path = Path::new(&output_folder).join(pimg.name() + ".bin");
fs::create_dir_all(&output_folder)?;
let mut out_file = OpenOptions::new()
@@ -88,5 +91,4 @@ pub fn extract_tpv_timg(mut file: &File, output_folder: &str) -> Result<(), Box<
println!("\nExtraction finished!");
Ok(())
}
}