Skip to content

Commit 857b308

Browse files
committed
Better COFF and Big Endian Strings
1 parent 7834185 commit 857b308

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

objdiff-core/src/arch/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,26 @@ impl DataType {
175175
}
176176
DataType::String => {
177177
if let Some(nul_idx) = bytes.iter().position(|&c| c == b'\0') {
178-
let str_bytes = &bytes[..nul_idx];
178+
let ascii_str_bytes = &bytes[..nul_idx];
179179
// Special case to display (ASCII) as the label for ASCII-only strings.
180-
let (cow, _, had_errors) = encoding_rs::UTF_8.decode(str_bytes);
180+
let (cow, _, had_errors) = encoding_rs::UTF_8.decode(ascii_str_bytes);
181181
if !had_errors && cow.is_ascii() {
182182
let string = format!("{cow}");
183183
let copy_string = escape_special_ascii_characters(string.clone());
184184
strs.push((string, Some("ASCII".into()), Some(copy_string)));
185185
}
186+
186187
for (encoding, encoding_name) in SUPPORTED_ENCODINGS {
187-
let (cow, _, had_errors) = encoding.decode(str_bytes);
188+
let (cow, _, had_errors) = encoding.decode(&bytes);
188189
// Avoid showing ASCII-only strings more than once if the encoding is ASCII-compatible.
189190
if !had_errors && (!encoding.is_ascii_compatible() || !cow.is_ascii()) {
190-
let string = format!("{cow}");
191+
let mut string = format!("{cow}");
192+
193+
// Inline loop to strip all trailing "\0"
194+
while let Some(stripped) = string.strip_suffix('\0') {
195+
string = stripped.to_string();
196+
}
197+
191198
let copy_string = escape_special_ascii_characters(string.clone());
192199
strs.push((string, Some(encoding_name.into()), Some(copy_string)));
193200
}

objdiff-core/src/arch/ppc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl Arch for ArchPpc {
355355
}
356356

357357
fn guess_data_type(&self, resolved: ResolvedInstructionRef, bytes: &[u8]) -> Option<DataType> {
358-
if resolved.relocation.is_some_and(|r| r.symbol.name.starts_with("@stringBase")) {
358+
if resolved.relocation.is_some_and(|r| r.symbol.name.starts_with("@stringBase") || r.symbol.name.starts_with("$SG")) {
359359
// Pooled string.
360360
return Some(DataType::String);
361361
}

0 commit comments

Comments
 (0)