Skip to content

Commit dd9f0d1

Browse files
committed
dir/gen3: factor out helper to obtain directory name
Signed-off-by: Daniel Maslowski <[email protected]>
1 parent bed5394 commit dd9f0d1

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/dir/gen3.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ pub struct CPDHeader {
3535
// _10: u32,
3636
}
3737

38+
impl CPDHeader {
39+
pub fn name(&self) -> String {
40+
let n = self.part_name;
41+
match std::str::from_utf8(&n) {
42+
// some names are shorter than 4 bytes and padded with 0x0
43+
Ok(n) => n.trim_end_matches('\0').to_string(),
44+
Err(_) => format!("{:02x?}", n),
45+
}
46+
}
47+
}
48+
3849
const HEADER_SIZE: usize = core::mem::size_of::<CPDHeader>();
3950

4051
// See <https://github.com/corna/me_cleaner> `check_and_remove_modules_gen3()`
@@ -141,12 +152,7 @@ impl CodePartitionDirectory {
141152
let Ok((header, _)) = CPDHeader::read_from_prefix(&data) else {
142153
return Err("could not parse CPD header".to_string());
143154
};
144-
let n = header.part_name;
145-
let name = match std::str::from_utf8(&n) {
146-
// some names are shorter than 4 bytes and padded with 0x0
147-
Ok(n) => n.trim_end_matches('\0').to_string(),
148-
Err(_) => format!("{:02x?}", n),
149-
};
155+
let name = header.name();
150156
let header_size = if header.version_or_checksum == 0x00140102 {
151157
HEADER_SIZE + 4
152158
} else {

0 commit comments

Comments
 (0)