Write license info from file

This commit is contained in:
jaby 2022-11-16 03:35:28 +01:00
parent 0b5e67582d
commit 3d9e87a400
4 changed files with 54 additions and 23 deletions

View File

@ -2,6 +2,8 @@ use super::{*, SectorWriter, {CDDesc, Error}};
use super::super::types::{helper::{DirectoryRecordMember, PathTableMember}, layout::Layout, *}; use super::super::types::{helper::{DirectoryRecordMember, PathTableMember}, layout::Layout, *};
use builder::SubModeBuilder; use builder::SubModeBuilder;
use cdtypes::types::{cdstring::{AString, DString}, date::*, dir_record::*, helper::{round_bytes_mode2_form1, sector_count_mode2_form1}, path_table::*, pvd as cd_pvd, lsb_msb::*, sector::Mode2Form1}; use cdtypes::types::{cdstring::{AString, DString}, date::*, dir_record::*, helper::{round_bytes_mode2_form1, sector_count_mode2_form1}, path_table::*, pvd as cd_pvd, lsb_msb::*, sector::Mode2Form1};
use tool_helper::{BufferedInputFile, open_input_file_buffered};
use std::io::{Read, Seek, SeekFrom};
const ROOT_DIR_NAME:&'static str = "\x00"; const ROOT_DIR_NAME:&'static str = "\x00";
const SYSTEM_AREA_SECTOR_COUNT:usize = 16; const SYSTEM_AREA_SECTOR_COUNT:usize = 16;
@ -76,7 +78,7 @@ pub fn encode_psx_image(cd_desc: CDDesc, sec_writer: &mut dyn SectorWriter) -> R
for element in cd_desc.get_memory_layout() { for element in cd_desc.get_memory_layout() {
match element { match element {
Layout::SystemArea(system_area) => process_system_area(&system_area.borrow(), sec_writer)?, Layout::SystemArea(system_area) => process_system_area(&mut system_area.borrow_mut(), sec_writer)?,
Layout::PVD(pvd) => process_pvd(&pvd.borrow(), cd_desc.path_table.clone(), cd_desc.root.clone(), vol_sector_count, sec_writer)?, Layout::PVD(pvd) => process_pvd(&pvd.borrow(), cd_desc.path_table.clone(), cd_desc.root.clone(), vol_sector_count, sec_writer)?,
Layout::PathTables(path_table) => process_path_table(&path_table.borrow(), cd_desc.root.clone(), sec_writer)?, Layout::PathTables(path_table) => process_path_table(&path_table.borrow(), cd_desc.root.clone(), sec_writer)?,
Layout::Directory(dir) => process_directory_record(&dir.borrow(), sec_writer)?, Layout::Directory(dir) => process_directory_record(&dir.borrow(), sec_writer)?,
@ -88,7 +90,7 @@ pub fn encode_psx_image(cd_desc: CDDesc, sec_writer: &mut dyn SectorWriter) -> R
} }
fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWriter) -> Result<(), Error> {
fn write_license_file(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { fn write_license_file(sec_writer: &mut dyn SectorWriter, mut license_file: BufferedInputFile) -> Result<(), Error> {
fn write_data_zeros(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { fn write_data_zeros(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> {
for _ in 0..4 { for _ in 0..4 {
sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?;
@ -97,14 +99,37 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit
Ok(()) Ok(())
} }
fn write_license_string(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { fn write_license_string(sec_writer: &mut dyn SectorWriter, license_file: &mut BufferedInputFile) -> Result<(), Error> {
sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; const LICENSE_STRING_START:u64 = 0x2488;
let mut license_string_buffer = [0u8; Mode2Form1::DATA_SIZE];
license_file.seek(SeekFrom::Start(LICENSE_STRING_START))?;
license_file.read(&mut license_string_buffer)?;
let sub_mode = SubModeBuilder::new_mode1().create();
let sector = builder::create_xa_data_for_raw(sub_mode, &license_string_buffer);
sec_writer.write_cd_xa_data(sector)?;
Ok(()) Ok(())
} }
fn write_license_logo(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { fn write_license_logo(sec_writer: &mut dyn SectorWriter, license_file: &mut BufferedInputFile) -> Result<(), Error> {
const LICENSE_LOGO_START:u64 = 0x2488;
license_file.seek(SeekFrom::Start(LICENSE_LOGO_START))?;
for _ in 0..4 { for _ in 0..4 {
sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; const LICENSE_SECTOR_REST:i64 = 0x120;
let mut license_logo_buffer = [0u8; Mode2Form1::DATA_SIZE];
license_file.read(&mut license_logo_buffer)?;
license_file.seek(SeekFrom::Current(LICENSE_SECTOR_REST))?;
let sub_mode = SubModeBuilder::new_mode1().create();
let sector = builder::create_xa_data_for_raw(sub_mode, &license_logo_buffer);
sec_writer.write_cd_xa_data(sector)?;
} }
Ok(()) Ok(())
} }
@ -124,8 +149,8 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit
} }
write_data_zeros(sec_writer)?; write_data_zeros(sec_writer)?;
write_license_string(sec_writer)?; write_license_string(sec_writer, &mut license_file)?;
write_license_logo(sec_writer)?; write_license_logo(sec_writer, &mut license_file)?;
write_audio_zeros(sec_writer) write_audio_zeros(sec_writer)
} }
@ -134,17 +159,18 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit
return Err(Error::from_text(format!("System Area required to start at sector 0 of Track - found LBA: {}", system_area_lba))); return Err(Error::from_text(format!("System Area required to start at sector 0 of Track - found LBA: {}", system_area_lba)));
} }
if system_area.license_data.is_empty() { if let Some(license_file) = &system_area.license_file_path {
// No license specified - filling it with zeros let license_file = open_input_file_buffered(license_file)?;
write_license_file(sec_writer, license_file)
}
else {
// No license specified - filling it with zeros
for _ in 0..SYSTEM_AREA_SECTOR_COUNT { for _ in 0..SYSTEM_AREA_SECTOR_COUNT {
sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?;
} }
Ok(()) Ok(())
}
else {
write_license_file(sec_writer)
} }
} }

View File

@ -44,7 +44,7 @@ fn parse_configuration(config: config_reader::Configuration) -> Result<CDDesc, E
} }
if let Some(license_path) = config.license_path { if let Some(license_path) = config.license_path {
cd_desc.system_area.borrow_mut().license_data = read_file(license_path)?; cd_desc.system_area.borrow_mut().license_file_path = Some(license_path);
} }
parse_dir(&mut cd_desc.root.borrow_mut(), config.root)?; parse_dir(&mut cd_desc.root.borrow_mut(), config.root)?;

View File

@ -4,7 +4,7 @@ pub mod file_map;
use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord, path_table::PathTableL}; use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord, path_table::PathTableL};
use file_map::FileSystemMap; use file_map::FileSystemMap;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, path::PathBuf, rc::Rc};
pub use tool_helper::Error; pub use tool_helper::Error;
pub type SharedPtr<T> = Rc<RefCell<T>>; pub type SharedPtr<T> = Rc<RefCell<T>>;
@ -56,13 +56,13 @@ impl CDDesc {
} }
pub struct SystemArea { pub struct SystemArea {
pub(in super) track_rel_lba: usize, pub(in super) track_rel_lba: usize,
pub(in super) license_data: Vec<u8>, pub(in super) license_file_path: Option<PathBuf>,
} }
impl SystemArea { impl SystemArea {
pub fn new() -> SystemArea { pub fn new() -> SystemArea {
SystemArea{track_rel_lba: 0, license_data: Vec::new()} SystemArea{track_rel_lba: 0, license_file_path: None}
} }
} }

View File

@ -3,8 +3,9 @@ use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf};
pub mod bits; pub mod bits;
pub mod raw; pub mod raw;
pub type Output = Box<dyn Write>; pub type BufferedInputFile = BufReader<std::fs::File>;
pub type Input = Box<dyn Read>; pub type Output = Box<dyn Write>;
pub type Input = Box<dyn Read>;
pub trait ErrorString { pub trait ErrorString {
fn to_string(self) -> String; fn to_string(self) -> String;
@ -113,11 +114,15 @@ pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> { pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
match input_file { match input_file {
Some(input_path) => Ok(Box::new(BufReader::new(std::fs::File::open(input_path)?))), Some(input_path) => Ok(Box::new(open_input_file_buffered(&input_path)?)),
None => Ok(Box::new(BufReader::new(std::io::stdin()))), None => Ok(Box::new(BufReader::new(std::io::stdin()))),
} }
} }
pub fn open_input_file_buffered(input_path: &PathBuf) -> Result<BufferedInputFile, Error> {
Ok(BufReader::new(std::fs::File::open(input_path)?))
}
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String, Error> { pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String, Error> {
Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name))?.to_owned()) Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name))?.to_owned())
} }