add philips_bdp_extractor

This commit is contained in:
theubusu
2026-05-04 13:58:34 +02:00
parent 81b9cd169f
commit 196662d218
4 changed files with 245 additions and 2 deletions
+138
View File
@@ -0,0 +1,138 @@
use crate::utils::common;
use binrw::BinRead;
use aes::Aes256;
use aes::cipher::{BlockEncrypt, KeyInit, generic_array::GenericArray};
pub static KEY1: [u8; 32] = [
0x01, 0x06, 0x18, 0x00, 0x0a, 0x22, 0x02, 0x41, 0x4d, 0x41, 0x08, 0x22, 0x12, 0x09, 0x04, 0x20,
0x22, 0x12, 0x02, 0x11, 0x01, 0x05, 0x41, 0x00, 0x05, 0x22, 0x22, 0x0a, 0x24, 0x08, 0x40, 0x24
];
pub static IV1: [u8; 16] = [
0x58, 0x87, 0x40, 0x13, 0x20, 0x00, 0x01, 0x30, 0x03, 0x58, 0x81, 0x42, 0x04, 0x22, 0x9c, 0x01
];
//AES 256 CFB with IV reset every 0x80 bytes
pub fn bebin_decrypt_aes256cfb(data: &[u8], key: &[u8; 32], iv: &[u8; 16]) -> Vec<u8> {
let cipher = Aes256::new(GenericArray::from_slice(key));
let mut out = Vec::with_capacity(data.len());
for chunk in data.chunks(0x80) {
let mut state = *iv;
for block in chunk.chunks(16) {
let mut ks = state.into();
cipher.encrypt_block(&mut ks);
let pt: Vec<u8> = block.iter()
.zip(ks.iter())
.map(|(&c, &k)| c ^ k)
.collect();
let n = block.len();
state[..n].copy_from_slice(block);
out.extend_from_slice(&pt);
}
}
out
}
#[derive(PartialEq, Debug)]
pub enum HeaderType {
Old,
New
}
pub trait UpgHeader {
fn _magic_num(&self) -> [u8; 7];
fn name(&self) -> String;
fn target_num(&self) -> u8;
fn version(&self) -> String;
fn target_size(&self) -> u32;
fn _target_verify(&self) -> u32;
fn entries(&self) -> &[UpgEntry];
}
#[derive(BinRead)]
pub struct UpgHeaderOld {
_magic_num: [u8; 7], //PHILIPS
target_name_bytes: [u8; 8],
pub target_num: u8,
target_version_bytes: [u8; 8],
pub target_size: u32,
_target_verify: u32, //checksum
#[br(count=target_num)] pub entries: Vec<UpgEntry>,
}
impl UpgHeader for UpgHeaderOld {
fn _magic_num(&self) -> [u8; 7] {
self._magic_num
}
fn name(&self) -> String {
common::string_from_bytes(&self.target_name_bytes)
}
fn target_num(&self) -> u8 {
self.target_num
}
fn version(&self) -> String {
common::string_from_bytes(&self.target_version_bytes)
}
fn target_size(&self) -> u32 {
self.target_size
}
fn _target_verify(&self) -> u32 {
self._target_verify
}
fn entries(&self) -> &[UpgEntry] {
&self.entries
}
}
#[derive(BinRead)]
pub struct UpgHeaderNew {
_magic_num: [u8; 7], //PHILIPS
target_name_bytes: [u8; 12],
pub target_num: u8,
target_version_bytes: [u8; 8],
pub target_size: u32,
_target_verify: u32, //checksum
#[br(count=target_num)] pub entries: Vec<UpgEntry>,
}
impl UpgHeader for UpgHeaderNew {
fn _magic_num(&self) -> [u8; 7] {
self._magic_num
}
fn name(&self) -> String {
common::string_from_bytes(&self.target_name_bytes)
}
fn target_num(&self) -> u8 {
self.target_num
}
fn version(&self) -> String {
common::string_from_bytes(&self.target_version_bytes)
}
fn target_size(&self) -> u32 {
self.target_size
}
fn _target_verify(&self) -> u32 {
self._target_verify
}
fn entries(&self) -> &[UpgEntry] {
&self.entries
}
}
#[derive(BinRead)]
pub struct UpgEntry {
pub id: u8,
pub iic: u8,
version_bytes: [u8; 4],
pub offset: u32,
pub size: u32,
}
impl UpgEntry {
pub fn version(&self) -> String {
common::string_from_bytes(&self.version_bytes)
}
}
+94
View File
@@ -0,0 +1,94 @@
mod include;
use std::any::Any;
use crate::{AppContext, InputTarget, formats};
use std::path::Path;
use std::fs::{self, File, OpenOptions};
use std::io::{Write, Seek};
use binrw::BinReaderExt;
use crate::utils::common;
use include::*;
struct PhilipsBdpCtx {
header_type: HeaderType,
}
pub fn is_philips_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, 16)?;
if header.starts_with(b"PHILIPS") {
if header[15].is_ascii_alphanumeric() {
Ok(Some(Box::new(PhilipsBdpCtx {header_type: HeaderType::New})))
} else {
Ok(Some(Box::new(PhilipsBdpCtx {header_type: HeaderType::Old})))
}
} else {
Ok(None)
}
}
pub fn extract_philips_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")?;
let ctx = ctx.downcast::<PhilipsBdpCtx>().expect("Missing context");
let header: Box<dyn UpgHeader> = match ctx.header_type {
HeaderType::Old => Box::new(file.read_le::<UpgHeaderOld>()?),
HeaderType::New => Box::new(file.read_le::<UpgHeaderNew>()?),
};
let header_size = file.stream_position()?;
println!("File info -\nName: {}\nVersion: {}\nTarget size: {}\nEntry count: {}\nHeader type: {:?}\nHeader size: {}",
header.name(), header.version(), header.target_size(), header.target_num(), ctx.header_type, header_size);
for (i, entry) in header.entries().iter().enumerate() {
if entry.id == 0xFF && entry.size == 0xFFFFFFFF {
break
}
println!("\n#{} - ID: {:x}, IIC: {:x}, Version: {}, Offset: {}, Size: {}",
i+1, entry.id, entry.iic, entry.version(), entry.offset, entry.size);
let data = common::read_file(&file, entry.offset as u64 + header_size, entry.size as usize)?;
let out_data;
if entry.id == 0 && app_ctx.has_option("philips_bdp:decrypt") {
println!("- Decrypting...");
out_data = bebin_decrypt_aes256cfb(&data, &KEY1, &IV1);
} else {
out_data = data;
}
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", 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(&out_data)?;
println!("- Saved file!");
//ID 0 should be the main MTK bdp file, since this is just an extra container for that format (like Sony BDP), so we can try to extract it here.
if entry.id == 0 {
println!("Checking if it's also MTK BDP...");
let new_file = File::open(&output_path)?;
//DUMB
let mtk_ctx: AppContext = AppContext { input: InputTarget::File(new_file), output_dir: app_ctx.output_dir.join("0"), options: app_ctx.options.clone() };
if let Some(result) = formats::mtk_bdp::is_mtk_bdp_file(&mtk_ctx)? {
println!("- MTK BDP file detected!\n");
formats::mtk_bdp::extract_mtk_bdp(&mtk_ctx, result)?;
} else {
if app_ctx.has_option("philips_bdp:decrypt") {
println!("- Not an MTK BDP file");
} else {
println!("- Not an MTK BDP file (try with decrypt?)");
}
}
}
}
Ok(())
}