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
+29 -25
View File
@@ -3,8 +3,31 @@ use std::path::Path;
use std::fs::{self, OpenOptions};
use std::io::{Write, Seek};
use binrw::{BinRead, BinReaderExt};
use crate::common;
#[derive(Debug, BinRead)]
#[br(little)]
struct Header {
#[br(count = 4)] _magic_bytes: Vec<u8>,
#[br(count = 4)] _flags: Vec<u8>,
_header_size: u32,
#[br(count = 40)] _unknown1: Vec<u8>,
part_count: u32,
_first_part_offset: u32,
#[br(count = 116)] _unknown2: Vec<u8>,
}
#[derive(Debug, BinRead)]
#[br(little)]
struct PartEntry {
#[br(count = 16)] _unknown: Vec<u8>,
index: u32,
size: u32,
offset: u32,
}
pub fn is_novatek_file(file: &File) -> bool {
let header = common::read_file(&file, 0, 4).expect("Failed to read from file.");
if header == b"NFWB" {
@@ -15,37 +38,18 @@ pub fn is_novatek_file(file: &File) -> bool {
}
pub fn extract_novatek(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
let _magic = common::read_exact(&mut file, 4)?; //NFWB magic
let _flags = common::read_exact(&mut file, 4)?;
let _header_size = common::read_exact(&mut file, 4)?;
let _ = common::read_exact(&mut file, 40)?; //unknown
let header: Header = file.read_le()?;
let part_count_bytes = common::read_exact(&mut file, 4)?;
let part_count = u32::from_le_bytes(part_count_bytes.try_into().unwrap());
println!("\nPart count: {}", header.part_count);
let _first_part_offset = common::read_exact(&mut file, 4)?;
println!("\nPart count: {}", part_count);
let _ = common::read_exact(&mut file, 116)?;
for i in 0..part_count {
let _ = common::read_exact(&mut file, 16)?; //unknown
let index_bytes = common::read_exact(&mut file, 4)?;
let index = u32::from_le_bytes(index_bytes.try_into().unwrap());
let size_bytes = common::read_exact(&mut file, 4)?;
let size = u32::from_le_bytes(size_bytes.try_into().unwrap());
let offset_bytes = common::read_exact(&mut file, 4)?;
let offset = u32::from_le_bytes(offset_bytes.try_into().unwrap());
for i in 0..header.part_count {
let part: PartEntry = file.read_le()?;
let current_pos = file.stream_position()?;
let data = common::read_file(&file, offset as u64, size as usize)?;
let data = common::read_file(&file, part.offset as u64, part.size as usize)?;
println!("\nPart {}: index: {}, size: {}, offset: {}", i + 1, index, size, offset);
println!("\nPart {}: index: {}, size: {}, offset: {}", i + 1, part.index, part.size, part.offset);
let output_path = Path::new(&output_folder).join(format!("part_{}.bin", i + 1));