diff --git a/src/formats/funai_bdp/include.rs b/src/formats/funai_bdp/include.rs index 07183ad..846015e 100644 --- a/src/formats/funai_bdp/include.rs +++ b/src/formats/funai_bdp/include.rs @@ -1,8 +1,6 @@ use binrw::BinRead; use crate::utils::common; -pub static DEC_KEY: u32 = 0x13641A98; - #[derive(BinRead)] pub struct IndexTableEntry { name_bytes: [u8; 32], diff --git a/src/formats/funai_bdp/mod.rs b/src/formats/funai_bdp/mod.rs index 92b0e8c..d45f993 100644 --- a/src/formats/funai_bdp/mod.rs +++ b/src/formats/funai_bdp/mod.rs @@ -10,27 +10,38 @@ use binrw::BinReaderExt; use crate::utils::common; use crate::formats::funai_upg::funai_des::funai_des_decrypt; use include::*; +use crate::keys; + +pub struct FunaiBdpContext { + key: u32, +} pub fn is_funai_bdp_file(app_ctx: &AppContext) -> Result>, Box> { let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)}; let header = common::read_file(&file, 0, 16)?; - let dec_header = funai_des_decrypt(&header, DEC_KEY); - if dec_header == b"index_table\x00\x00\x00\x00\x00"{ - Ok(Some(Box::new(()))) - } else { - Ok(None) + for key_hex in keys::FUNAI_BDP { + 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(&header, key_u32); + + if decrypted == b"index_table\x00\x00\x00\x00\x00"{ + return Ok(Some(Box::new(FunaiBdpContext {key: key_u32}))) + } } + + Ok(None) } -pub fn extract_funai_bdp(app_ctx: &AppContext, _ctx: Box) -> Result<(), Box> { +pub fn extract_funai_bdp(app_ctx: &AppContext, ctx: Box) -> Result<(), Box> { let mut file = app_ctx.file().ok_or("Extractor expected file")?; + let ctx = ctx.downcast::().expect("Missing context"); let mut data = Vec::new(); //to decrypt entire file file.read_to_end(&mut data)?; println!("Decrypting file..."); - data = funai_des_decrypt(&data, DEC_KEY); + data = funai_des_decrypt(&data, ctx.key); let mut file_reader = Cursor::new(data); file_reader.seek(SeekFrom::Start(0x20))?; @@ -46,15 +57,16 @@ pub fn extract_funai_bdp(app_ctx: &AppContext, _ctx: Box) -> Result<(), for (i, entry) in entries.iter().enumerate() { println!("\n({}/{}) - {}, Offset: {}, Size: {}", i +1, index_entry_count, entry.name(), entry.offset, entry.size); - //at start of location there is an additional 0x20 with the entry's name - file_reader.seek(SeekFrom::Start(entry.offset as u64 + 0x20))?; - let data = common::read_exact(&mut file_reader, entry.size as usize - 0x20)?; + file_reader.seek(SeekFrom::Start(entry.offset as u64))?; + let data = common::read_exact(&mut file_reader, entry.size as usize)?; let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", entry.name())); 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)?; + + //at start of location there is an additional 0x20 with the entry's name + out_file.write_all(&data[0x20..])?; println!("-- Saved file!"); } diff --git a/src/keys.rs b/src/keys.rs index e3485b7..77a049d 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -108,6 +108,13 @@ pub static FUNAI_UPG: &[&str] = &[ ("68353031"), //PHL-0SDX ]; +//Funai BDP keys +pub static FUNAI_BDP: &[&str] = &[ + ("981A6413"), + ("81AE6734"), + ("09E87783"), +]; + //EPK keys //github.com/openlgtv/epk2extract pub static EPK: &[(&str, &str)] = &[