Skip to content

Commit

Permalink
fix tilib enum print values of signed decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Jan 27, 2025
1 parent 42b3c97 commit 77a1751
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
31 changes: 26 additions & 5 deletions src/til/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,23 @@ impl Enum {
pub struct EnumMember {
pub name: Option<IDBString>,
pub comment: Option<CommentType>,
pub value: u64,
pub value: EnumValue,
}

// TODO have this type as Signed/Unsigned based on flags?
#[derive(Clone, Debug, Copy)]
pub enum EnumValue {
U32(u32),
U64(u64),
}

impl EnumValue {
pub fn as_u64(&self) -> u64 {
match *self {
EnumValue::U32(x) => x.into(),
EnumValue::U64(x) => x,
}
}
}

#[derive(Clone, Debug)]
Expand All @@ -59,7 +75,7 @@ pub(crate) struct EnumRaw {
is_unsigned: bool,
output_format: EnumFormat,
groups: Option<Vec<u16>>,
members: Vec<u64>,
members: Vec<EnumValue>,
storage_size: Option<NonZeroU8>,
}

Expand Down Expand Up @@ -171,12 +187,17 @@ impl EnumRaw {
}
// Allowed at InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x45242f deserialize_enum
// NOTE this is originaly i32, but wrapping_add a u32/i32 have the same result
low_acc = low_acc.wrapping_add(input.read_de()?);
if is_64 {
low_acc = low_acc.wrapping_add(input.read_de()?);
high_acc = high_acc.wrapping_add(input.read_de()?);
// Allowed at InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x452472 deserialize_enum
Ok(EnumValue::U64(
(((high_acc as u64) << 32) | low_acc as u64) & mask,
))
} else {
low_acc = low_acc.wrapping_add(input.read_de()?);
Ok(EnumValue::U32(low_acc & mask as u32))
}
// Allowed at InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x452472 deserialize_enum
Ok((((high_acc as u64) << 32) | low_acc as u64) & mask)
})
.collect::<anyhow::Result<_>>()?;

Expand Down
21 changes: 13 additions & 8 deletions src/tools/tilib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use idb_rs::til::array::Array;
use idb_rs::til::bitfield::Bitfield;
use idb_rs::til::function::{CallingConvention, Function};
use idb_rs::til::pointer::Pointer;
use idb_rs::til::r#enum::Enum;
use idb_rs::til::r#enum::{Enum, EnumValue};
use idb_rs::til::r#struct::{Struct, StructMemberAtt};
use idb_rs::til::section::TILSection;
use idb_rs::til::union::Union;
Expand Down Expand Up @@ -1205,14 +1205,19 @@ fn print_til_type_enum(
fmt.write_all(member_name.as_bytes())?;
}
write!(fmt, " = ")?;
match til_enum.output_format {
Char if member.value <= 0xFF => {
write!(fmt, "'{}'", (member.value) as u8 as char)?
match (til_enum.output_format, member.value) {
(Char, value) if value.as_u64() <= 0xFF => {
write!(fmt, "'{}'", value.as_u64() as u8 as char)?
}
Char => write!(fmt, "'\\xu{:X}'", member.value)?,
Hex => write!(fmt, "{:#X}", member.value)?,
SignedDecimal => write!(fmt, "{}", member.value as i64)?,
UnsignedDecimal => write!(fmt, "{:X}", member.value)?,
(Char, value) => write!(fmt, "'\\xu{:X}'", value.as_u64())?,
(Hex, value) => write!(fmt, "{:#X}", value.as_u64())?,
(SignedDecimal, EnumValue::U32(value)) => {
write!(fmt, "{}", value as i32)?
}
(SignedDecimal, EnumValue::U64(value)) => {
write!(fmt, "{}", value as i32)?
}
(UnsignedDecimal, value) => write!(fmt, "{:X}", value.as_u64())?,
}
// TODO find this in InnerRef
if let Some(8) = til_enum.storage_size.map(NonZeroU8::get) {
Expand Down

0 comments on commit 77a1751

Please sign in to comment.