mirror of
https://github.com/Ap0dexMe0/unixtract.git
synced 2026-07-16 05:04:22 +02:00
funai_upg: add support for decryption + keys & add funai_upg_phl extractor for them
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
mod include;
|
||||
pub mod include;
|
||||
pub mod funai_des;
|
||||
use std::any::Any;
|
||||
use crate::AppContext;
|
||||
|
||||
@@ -9,11 +10,14 @@ use binrw::BinReaderExt;
|
||||
|
||||
use crate::utils::common;
|
||||
use include::*;
|
||||
use crate::keys;
|
||||
use funai_des::funai_des_decrypt;
|
||||
|
||||
pub fn is_funai_upg_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, 6)?;
|
||||
if header == b"UPG\x00\x00\x00" {
|
||||
let header = common::read_file(&file, 0, 8)?;
|
||||
let entry_count = u16::from_le_bytes(header[6..8].try_into()?);
|
||||
if header[..6] == *b"UPG\x00\x00\x00" && entry_count > 0 {
|
||||
Ok(Some(Box::new(())))
|
||||
} else {
|
||||
Ok(None)
|
||||
@@ -24,33 +28,51 @@ pub fn extract_funai_upg(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(),
|
||||
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
|
||||
|
||||
let header: Header = file.read_le()?;
|
||||
println!("File info:\nFile size: {}\nEntry count: {}", header.file_size, header.entry_count);
|
||||
let mut key: Option<u32> = None;
|
||||
|
||||
println!("File info:\nFile size: {}\nEntry count: {}", header.file_size, header.entry_count);
|
||||
|
||||
for i in 0..header.entry_count {
|
||||
let entry: Entry = file.read_le()?;
|
||||
println!("\n({}/{}) - Type: {}, Size: {}", i + 1, header.entry_count, entry.entry_type, entry.entry_size);
|
||||
|
||||
let data = common::read_exact(&mut file, entry.entry_size as usize - 0x46)?; //size has the flags + crc32 + hash
|
||||
|
||||
let mut data = common::read_exact(&mut file, entry.entry_size as usize - 0x46)?; //size has the flags + crc32 + hash
|
||||
let _crc32 = common::read_exact(&mut file, 4)?; //crc32 includes the entry header and hash
|
||||
let _hash = common::read_exact(&mut file, 64)?; //hash is only used on encrypted entries
|
||||
|
||||
//find key using descriptor entry
|
||||
if entry.entry_type == 0 && entry.encryption_flag == 1 && key.is_none() {
|
||||
for key_hex in keys::FUNAI_UPG {
|
||||
let key_bytes = hex::decode(key_hex)?;
|
||||
let key_u32 = u32::from_le_bytes(key_bytes.as_slice().try_into()?);
|
||||
let decrypted = funai_des_decrypt(&data, key_u32);
|
||||
|
||||
if is_valid_ver_string(&decrypted) {
|
||||
println!("Matched key: {}\nFirmware info: {}",
|
||||
key_hex, common::string_from_bytes(&decrypted));
|
||||
key = Some(key_u32);
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("\n({}/{}) - Type: {}, Size: {}", i + 1, header.entry_count, entry.entry_type, entry.entry_size);
|
||||
|
||||
if entry.encryption_flag == 1 {
|
||||
//not supported yet
|
||||
println!("- Warning: Cannot decrypt entry, saving encrypted data!");
|
||||
if let Some(key_u32) = key {
|
||||
println!("- Decrypting...");
|
||||
data = funai_des_decrypt(&data, key_u32);
|
||||
} else {
|
||||
println!("- Warning! Failed to find decryption key, saving encrypted data")
|
||||
}
|
||||
}
|
||||
|
||||
if entry.encryption_flag == 0 && entry.entry_type == 0 {
|
||||
let entry_string = common::string_from_bytes(&data);
|
||||
println!("Descriptor entry info:\n{}", entry_string);
|
||||
}
|
||||
|
||||
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}_{}.bin", i + 1, entry.entry_type));
|
||||
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", entry.entry_type));
|
||||
|
||||
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!");
|
||||
println!("-- Saved file!");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user