1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! The addressing width of an image or segment: 16-, 32-, or 64-bit.
use crate::Database;
impl Database {
/// The database's addressing width, or `None` if it reports an unrecognized one.
///
/// The width of a [`read_pointer`](Database::read_pointer) and one field of the
/// [`info`](Database::info) snapshot.
#[inline]
#[must_use]
#[doc(alias("inf_get_app_bitness"))]
pub fn bitness(&self) -> Option<Bitness> {
Bitness::try_from_bits(self.bitness_bits().max(0) as u8)
}
}
/// Addressing width: 16-, 32-, or 64-bit.
///
/// A closed set: IDA reports a width in bits, and a value that is not one of these three
/// (including the `0` the facade returns for an absent segment) becomes `None` at the
/// conversion boundary rather than a silent default.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[doc(alias("inf_get_app_bitness"))]
pub enum Bitness {
/// 16-bit addressing.
Bits16,
/// 32-bit addressing.
Bits32,
/// 64-bit addressing.
Bits64,
}
impl Bitness {
/// Interprets a raw bit width: `16`, `32`, or `64`.
///
/// `None` for any other value, which is how the facade's `0` (no such segment) and any
/// unexpected width surface.
#[inline]
#[must_use]
pub const fn try_from_bits(bits: u8) -> Option<Self> {
match bits {
16 => Some(Self::Bits16),
32 => Some(Self::Bits32),
64 => Some(Self::Bits64),
_ => None,
}
}
/// The width in bits: `16`, `32`, or `64`.
#[inline]
#[must_use]
pub const fn bits(self) -> u8 {
match self {
Self::Bits16 => 16,
Self::Bits32 => 32,
Self::Bits64 => 64,
}
}
}
#[cfg(test)]
mod tests {
use assert2::assert;
use super::*;
#[test]
fn known_widths_round_trip() {
for b in [Bitness::Bits16, Bitness::Bits32, Bitness::Bits64] {
assert!(Bitness::try_from_bits(b.bits()) == Some(b));
}
}
#[test]
fn unknown_widths_are_rejected() {
// The facade's "no such segment" sentinel and any odd width map to None.
assert!(Bitness::try_from_bits(0).is_none());
assert!(Bitness::try_from_bits(8).is_none());
assert!(Bitness::try_from_bits(128).is_none());
}
}