mirror of
https://github.com/Ap0dexMe0/unixtract.git
synced 2026-07-16 13:14:22 +02:00
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:
@@ -0,0 +1,30 @@
|
||||
use crate::utils::common;
|
||||
use binrw::BinRead;
|
||||
|
||||
#[derive(BinRead)]
|
||||
pub struct Header {
|
||||
_magic_bytes: [u8; 4],
|
||||
pub version_major: u32,
|
||||
pub version_minor: u32,
|
||||
_unused: u32,
|
||||
firmware_name_bytes: [u8; 16],
|
||||
pub data_size: u32,
|
||||
_md5_checksum: [u8; 16], //data checksum
|
||||
pub part_count: u32,
|
||||
_data_start_offset: u32,
|
||||
_signature: [u8; 128],
|
||||
_header_checksum: u32, //CRC32, calculated with the field set to 0
|
||||
}
|
||||
impl Header {
|
||||
pub fn firmware_name(&self) -> String {
|
||||
common::string_from_bytes(&self.firmware_name_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(BinRead)]
|
||||
pub struct PartEntry {
|
||||
pub id: u32,
|
||||
pub size: u32,
|
||||
pub offset: u32,
|
||||
_md5_checksum: [u8; 16],
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
mod include;
|
||||
use std::any::Any;
|
||||
use crate::AppContext;
|
||||
|
||||
use std::path::Path;
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::Write;
|
||||
use binrw::BinReaderExt;
|
||||
|
||||
use crate::utils::common;
|
||||
use include::*;
|
||||
|
||||
pub fn is_novatek_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"NFWB" {
|
||||
Ok(Some(Box::new(())))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_novatek(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 header: Header = file.read_le()?;
|
||||
println!("File info:\nFirmware name: {}\nVersion: {}.{}\nData size: {}\nPart count: {}",
|
||||
header.firmware_name(), header.version_major, header.version_minor, header.data_size, header.part_count);
|
||||
|
||||
let mut entries: Vec<PartEntry> = Vec::new();
|
||||
for _i in 0..header.part_count {
|
||||
let part: PartEntry = file.read_le()?;
|
||||
entries.push(part);
|
||||
}
|
||||
|
||||
let mut e_i = 0;
|
||||
for entry in &entries {
|
||||
e_i += 1;
|
||||
println!("\n({}/{}) - ID: {}, Offset: {}, Size: {}", e_i, entries.len(), entry.id, entry.offset, entry.size);
|
||||
|
||||
let data = common::read_file(&file, entry.offset as u64, entry.size as usize)?;
|
||||
|
||||
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}_{}.bin", e_i, entry.id));
|
||||
|
||||
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(&data)?;
|
||||
|
||||
println!("- Saved file!");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user