Skip to main content

msft_typelib/
name.rs

1//! [`NameEntry`] -- a decoded entry from the MSFT name table.
2//!
3//! The name table (segment 7) stores all identifier names used in the type
4//! library.  Each entry has a 12-byte header:
5//!
6//! ```text
7//! [hreftype: i32] [next_hash: i32] [namelen: u8] [flags: u8] [hashcode: u16]
8//! [name bytes, padded to 4-byte alignment]
9//! ```
10//!
11//! The `hreftype` links the name to the TypeInfo it belongs to (or `-1`
12//! for names without a TypeInfo, such as parameter names).  The
13//! `next_hash` and `hashcode` fields support the name hash table
14//! (segment 6) for fast lookups.
15
16/// A decoded name table entry yielded by [`NameIter`](crate::NameIter).
17pub struct NameEntry<'a> {
18    offset: usize,
19    name: &'a str,
20    hreftype: i32,
21    next_hash: i32,
22    flags: u8,
23    hashcode: u16,
24}
25
26impl<'a> NameEntry<'a> {
27    /// Creates a `NameEntry` from its decoded components.
28    pub(crate) fn new(
29        offset: usize,
30        name: &'a str,
31        hreftype: i32,
32        next_hash: i32,
33        flags: u8,
34        hashcode: u16,
35    ) -> Self {
36        Self {
37            offset,
38            name,
39            hreftype,
40            next_hash,
41            flags,
42            hashcode,
43        }
44    }
45
46    /// Byte offset of this entry relative to the name table segment start.
47    #[inline]
48    pub fn offset(&self) -> usize {
49        self.offset
50    }
51
52    /// The name string (UTF-8, borrowed from file data).
53    #[inline]
54    pub fn name(&self) -> &'a str {
55        self.name
56    }
57
58    /// `hreftype` of the TypeInfo this name belongs to, or `-1` for global names.
59    #[inline]
60    pub fn hreftype(&self) -> i32 {
61        self.hreftype
62    }
63
64    /// Offset of the next entry in the name hash chain, or `-1` if last.
65    #[inline]
66    pub fn next_hash(&self) -> i32 {
67        self.next_hash
68    }
69
70    /// Flags from the name table header (high byte of the namelen word).
71    ///
72    /// Known flags: `0x10` = used as variable name, `0x20` = name in enum.
73    #[inline]
74    pub fn name_flags(&self) -> u8 {
75        self.flags
76    }
77
78    /// Hash code for this name entry.
79    #[inline]
80    pub fn hashcode(&self) -> u16 {
81        self.hashcode
82    }
83}