Files
unixtract/src/formats/sony_bdp/mod.rs
T

90 lines
3.0 KiB
Rust
Raw Normal View History

2026-02-17 17:28:59 +01:00
mod include;
2026-02-05 15:18:26 +01:00
use std::any::Any;
2026-02-17 17:28:59 +01:00
use crate::{InputTarget, AppContext};
2026-02-05 15:18:26 +01:00
2025-10-31 19:37:03 +01:00
use std::fs::File;
use std::path::{Path, PathBuf};
use std::fs::{self, OpenOptions};
use std::io::{Cursor, Write};
2026-02-17 17:28:59 +01:00
use binrw::BinReaderExt;
2025-10-31 19:37:03 +01:00
use crate::utils::common;
2025-10-31 19:37:03 +01:00
use crate::formats;
2026-02-17 17:28:59 +01:00
use include::*;
2025-10-31 19:37:03 +01:00
2026-02-05 18:53:35 +01:00
pub fn is_sony_bdp_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"\x01\x73\xEC\xC9" || header == b"\x01\x73\xEC\x1F" || header == b"\xEC\x7D\xB0\xB0" { //MSB1x, MSB0x, BDPPxx
2026-02-05 15:18:26 +01:00
Ok(Some(Box::new(())))
2025-10-31 19:37:03 +01:00
} else {
2026-02-05 15:18:26 +01:00
Ok(None)
2025-10-31 19:37:03 +01:00
}
}
pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
2025-10-31 19:37:03 +01:00
let obf_header = common::read_exact(&mut file, 300)?;
let header = hex_substitute(&obf_header);
let mut hdr_reader = Cursor::new(header);
let hdr: Header = hdr_reader.read_le()?;
println!("File info:\nFirmware: {}\nVersion: {}\nDate: {}\nFile size: {}",
2025-10-31 19:37:03 +01:00
hdr.firmware_name(), hdr.firmware_version(), hdr.date(), hdr.file_size);
let mut last_file_path: Option<PathBuf> = None;
let mut first_entry_offset = 0;
let mut i = 0;
2025-10-31 19:37:03 +01:00
loop {
if (i != 0) && (hdr_reader.position() >= first_entry_offset) {
2025-10-31 19:37:03 +01:00
break
}
let entry: Entry = hdr_reader.read_le()?;
if entry.size == 0 {
continue
}
println!("\n#{} - Offset: {}, Size: {}", i, entry.offset, entry.size);
if i == 0 {
2025-10-31 19:37:03 +01:00
first_entry_offset = entry.offset as u64;
}
let obf_data = common::read_file(&file, entry.offset as u64, entry.size as usize)?;
let data = hex_substitute(&obf_data);
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", i+1));
2025-10-31 19:37:03 +01:00
last_file_path = Some(output_path.clone());
fs::create_dir_all(&app_ctx.output_dir)?;
2025-10-31 19:37:03 +01:00
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
out_file.write_all(&data)?;
println!("- Saved file!");
i += 1;
}
//The last file is often a Mtk BDP file so we can extract that here.
if last_file_path.is_some() {
println!("\nChecking if it's also MTK BDP...");
2026-02-05 15:18:26 +01:00
2025-10-31 19:37:03 +01:00
let last_file = File::open(last_file_path.unwrap())?;
let mtk_extraction_path = app_ctx.output_dir.join(format!("{}", i));
//this is getting stupid...
let ctx: AppContext = AppContext { input: InputTarget::File(last_file), output_dir: mtk_extraction_path, options: app_ctx.options.clone() };
2026-02-05 15:18:26 +01:00
if let Some(result) = formats::mtk_bdp::is_mtk_bdp_file(&ctx)? {
2025-10-31 19:37:03 +01:00
println!("- MTK BDP file detected!\n");
2026-02-05 15:18:26 +01:00
formats::mtk_bdp::extract_mtk_bdp(&ctx, result)?;
2025-10-31 19:37:03 +01:00
} else {
println!("- Not an MTK BDP file.");
}
}
Ok(())
}