Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/dir/gen3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ pub const CPD_MAGIC_BYTES: &[u8] = CPD_MAGIC.as_bytes();

const FOUR_K: usize = 0x1000;

fn stringify_vec(v: Vec<u8>) -> String {
v.iter().map(|b| format!("{b:02x}")).collect::<String>()
}

fn u8_slice_name_to_str(n: &[u8]) -> String {
match std::str::from_utf8(n) {
// some names are padded with 0x0
Ok(n) => n.trim_end_matches('\0').to_string(),
// sometimes, there is no real name
Err(_) => stringify_vec(n.to_vec()),
}
}

// see <https://troopers.de/downloads/troopers17/TR17_ME11_Static.pdf>
#[derive(IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(C, packed)]
Expand All @@ -37,12 +50,7 @@ pub struct CPDHeader {

impl CPDHeader {
pub fn name(&self) -> String {
let n = self.part_name;
match std::str::from_utf8(&n) {
// some names are shorter than 4 bytes and padded with 0x0
Ok(n) => n.trim_end_matches('\0').to_string(),
Err(_) => format!("{n:02x?}"),
}
u8_slice_name_to_str(&self.part_name)
}
}

Expand Down Expand Up @@ -71,10 +79,7 @@ pub struct CPDEntry {

impl CPDEntry {
pub fn name(&self) -> String {
match std::str::from_utf8(&self.name) {
Ok(n) => n.trim_end_matches('\0').to_string(),
Err(_) => format!("{:02x?}", &self.name),
}
u8_slice_name_to_str(&self.name)
}
}

Expand Down Expand Up @@ -111,10 +116,6 @@ pub struct CodePartitionDirectory {
pub name: String,
}

fn stringify_vec(v: Vec<u8>) -> String {
v.iter().map(|b| format!("{b:02x}")).collect::<String>()
}

impl Display for CodePartitionDirectory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let checksum = self.header.version_or_checksum;
Expand Down