Skip to main content

TypeLib

Struct TypeLib 

Source
pub struct TypeLib<'a> { /* private fields */ }
Expand description

Zero-copy view over an MSFT-format type library file.

Created via TypeLib::parse, which validates the header and segment directory. All subsequent accessors borrow from the original byte slice without copying.

§Example

let data = std::fs::read("library.tlb").unwrap();
let lib = msft_typelib::TypeLib::parse(&data)?;
println!("{} typeinfos", lib.typeinfo_count());

Implementations§

Source§

impl<'a> TypeLib<'a>

Source

pub const HEADER_SIZE: usize = 0x54

Size of the fixed MSFT header in bytes.

Source

pub const MAGIC: u32 = 0x5446534D

Expected magic value at offset 0 ("MSFT" in little-endian).

Source

pub const DISPATCH_HREF_OFFSET: u32 = 0x0100_0000

Offset added to hreftypes for dispatch-side references in dual interfaces.

Source

pub fn parse(data: &'a [u8]) -> Result<Self, Error>

Parses an MSFT type library from raw file bytes.

Validates the magic signature, reads the segment directory, and returns a zero-copy view over the data.

§Errors

Returns Error::TooShort if the buffer is smaller than the header or segment directory requires.

Returns Error::BadMagic if the first four bytes are not "MSFT".

Returns Error::InvalidData if required fields cannot be read or computed offsets overflow.

Source

pub fn data(&self) -> &'a [u8]

Returns the complete raw file data backing this type library.

Source

pub fn format_version(&self) -> u32

Format version word at offset 0x04 (typically 0x00010002).

Source

pub fn lib_guid_offset(&self) -> i32

Offset of the library GUID in the GUID table segment.

Source

pub fn lcid(&self) -> u32

Locale identifier (LCID) of this type library.

Source

pub fn lcid2(&self) -> u32

Secondary locale identifier at offset 0x10.

Source

pub fn varflags(&self) -> u32

Variable flags at offset 0x14.

Bits 0-3 encode SYSKIND (target platform). Bit 8 (HELPDLLFLAG) indicates a help-string DLL is present.

Source

pub fn syskind(&self) -> u8

Target platform (SYSKIND, bits 0-3 of varflags).

0 = Win16, 1 = Win32, 2 = Mac, 3 = Win64.

Source

pub fn has_help_dll(&self) -> bool

Whether a help-string DLL offset is present (HELPDLLFLAG, bit 8 of varflags).

Source

pub fn version(&self) -> u32

Library version (set with SetVersion).

Low 16 bits = major, high 16 bits = minor.

Source

pub fn version_major(&self) -> u16

Major version number (low 16 bits of version).

Source

pub fn version_minor(&self) -> u16

Minor version number (high 16 bits of version).

Source

pub fn flags(&self) -> u32

Library flags at offset 0x1C.

Source

pub fn typeinfo_count(&self) -> usize

Number of TypeInfo entries in this library.

Source

pub fn helpstring_offset(&self) -> i32

Offset of the help string in the string table, or -1.

Source

pub fn helpstringcontext(&self) -> i32

Library-level help string context at offset 0x28.

Source

pub fn helpcontext(&self) -> i32

Library-level help context at offset 0x2C.

Source

pub fn nametablecount(&self) -> i32

Number of entries in the name table at offset 0x30.

Source

pub fn nametablechars(&self) -> i32

Number of characters in the name table at offset 0x34.

Source

pub fn name_offset(&self) -> i32

Offset of the library name in the name table.

Source

pub fn helpfile_offset(&self) -> i32

Offset of the help-file name in the string table, or -1.

Source

pub fn custom_data_offset(&self) -> i32

Offset into the CDGuids directory for library-level custom data, or -1.

Source

pub fn dispatchpos(&self) -> i32

Dispatch hreftype at offset 0x4C.

For dual interfaces, this is the hreftype of the IDispatch base. Used with DISPATCH_HREF_OFFSET.

Source

pub fn nimpinfos(&self) -> i32

Number of import-info entries.

Source

pub fn segment(&self, index: usize) -> Option<(usize, usize)>

Returns (file_offset, length) for the segment at index.

Returns None if the segment is empty (offset < 0 or length <= 0).

Source

pub fn segment_data(&self, index: usize) -> Option<&'a [u8]>

Returns the raw bytes for the segment at index.

Returns None if the segment is empty or extends past the end of file.

Source

pub fn guid_hash_data(&self) -> Option<&'a [u8]>

Returns the raw GUID hash table data (segment 4).

Source

pub fn name_hash_data(&self) -> Option<&'a [u8]>

Returns the raw name hash table data (segment 6).

Source

pub fn name(&self, offset: i32) -> Option<&'a str>

Looks up a name by byte offset into the name table segment.

Returns None if offset is negative, the segment is missing, the entry is out of bounds, or the bytes are not valid UTF-8.

Source

pub fn guid(&self, offset: i32) -> Option<Guid<'a>>

Looks up a GUID by byte offset into the GUID table segment.

Returns None if offset is negative, the segment is missing, or the entry is out of bounds.

Source

pub fn string(&self, offset: i32) -> Option<&'a str>

Looks up a string by byte offset into the string table segment.

Returns None if offset is negative, the segment is missing, out of bounds, or not valid UTF-8.

Source

pub fn lib_name(&self) -> Option<&'a str>

Returns the library name from the name table.

Source

pub fn lib_guid(&self) -> Option<Guid<'a>>

Returns the library GUID from the GUID table.

Source

pub fn lib_helpstring(&self) -> Option<&'a str>

Returns the library help string from the string table.

Source

pub fn typeinfo_offset(&self, index: usize) -> Option<i32>

Returns the byte offset of TypeInfo[index] within the TypeInfo segment.

Returns None if index >= typeinfo_count.

Source

pub fn member_names(&self, index: usize) -> Vec<&'a str>

Collects member names for TypeInfo[index] by scanning the name table.

Returns the ordered list of member names (excluding the TypeInfo’s own name). Names are matched by hreftype == typeinfo_offset.

Source

pub fn typeinfo(&self, index: usize) -> Result<TypeInfoEntry<'a>, Error>

Returns the TypeInfo entry at the given index.

§Errors

Returns Error::TypeInfoOutOfRange if index >= typeinfo_count.

Returns Error::TooShort or Error::InvalidData if the entry cannot be read from the file data.

Source

pub fn typeinfos(&'a self) -> TypeInfoIter<'a>

Returns an iterator over all TypeInfo entries.

Source

pub fn funcs(&self, ti: &TypeInfoEntry<'_>) -> FuncIter<'a>

Returns an iterator over FuncRecord values for the given TypeInfo.

Returns an empty iterator if the TypeInfo has no functions or its memoffset is invalid.

Source

pub fn vars(&self, ti: &TypeInfoEntry<'_>) -> VarIter<'a>

Returns an iterator over VarRecord values for the given TypeInfo.

Internally skips past all function records first, since variable records follow functions in the data block.

Returns an empty iterator if the TypeInfo has no variables or its memoffset is invalid.

Source

pub fn params(&self, func: &FuncRecord<'a>) -> ParamIter<'a>

Returns an iterator over ParameterInfo entries for a function.

Parameters occupy the last nrargs * 12 bytes of the record. Returns an empty iterator if the function has no parameters.

Source

pub fn func_memid(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<i32>

Returns the MEMBERID (DISPID) for function[index].

Read from the auxiliary array that follows all func/var records. Returns None if the TypeInfo has no data block or index is out of bounds.

Source

pub fn var_memid(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<i32>

Returns the MEMBERID for variable[index].

Read from the auxiliary array that follows all func/var records. Returns None if the TypeInfo has no data block or index is out of bounds.

Source

pub fn func_name(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<&'a str>

Returns the name of function[index] from the auxiliary name array.

Returns None if the TypeInfo has no data block, index is out of bounds, or the name-table offset is invalid.

Source

pub fn var_name(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<&'a str>

Returns the name of variable[index] from the auxiliary name array.

Returns None if the TypeInfo has no data block, index is out of bounds, or the name-table offset is invalid.

Source

pub fn const_value(&self, offs_value: i32) -> Option<ConstValue<'a>>

Decodes a constant value from an OffsValue field.

If offs_value < 0 the value is packed inline:

  • VT code = (offs_value >> 26) & 0x1F
  • unsigned 26-bit value = offs_value & 0x03FF_FFFF

If offs_value >= 0 the value is stored in the custom-data segment at the given byte offset (2-byte VT tag + value bytes).

Returns None if the offset is non-negative but the custom-data segment is missing or the entry is out of bounds.

Source

pub fn type_desc(&self, offset: i32) -> Option<(i32, i32)>

Reads a type-descriptor table entry at the given byte offset.

Returns (vt, extra) where:

  • For VT_PTR (26): extra is the pointed-to type descriptor.
  • For VT_USERDEFINED (29): extra is the hreftype.
  • For VT_SAFEARRAY (27): extra is the element type descriptor.

Returns None if offset is negative, the segment is missing, or the entry is out of bounds.

Source

pub fn resolve_hreftype(&self, hreftype: i32) -> Option<usize>

Resolves an hreftype to a TypeInfo index (internal references only).

For internal references the hreftype is the byte offset of the TypeInfo within the TypeInfo segment; dividing by TypeInfoEntry::SIZE gives the index.

Returns None if hreftype is negative or the computed index is out of range. For full resolution including external and dispatch references, use resolve_hreftype_full.

Source

pub fn resolve_hreftype_full( &self, hreftype: i32, ) -> Option<ResolvedHreftype<'a>>

Resolves an hreftype to either an internal TypeInfo index or an external import reference.

Handles DISPATCH_HREF_OFFSET by stripping the flag before resolution. Returns None if hreftype is negative.

Source

pub fn guids(&self) -> GuidEntryIter<'a>

Returns an iterator over all GuidEntry values in the GUID table.

Source

pub fn names(&self) -> NameIter<'a>

Returns an iterator over all entries in the name table.

Source

pub fn impl_types(&self, ti: &TypeInfoEntry<'_>) -> ImplTypeIter<'a>

Returns an iterator over implemented interfaces for a coclass.

Traverses the linked list in the reference table starting from the TypeInfo’s res2 field.

Source

pub fn imports(&self) -> ImpInfoIter<'a>

Returns an iterator over ImpInfo entries in the import table.

Source

pub fn imp_files(&self) -> ImpFileIter<'a>

Returns an iterator over ImpFile entries in the import-files segment (segment 2).

Source

pub fn imp_file(&self, offset: i32) -> Option<ImpFile<'a>>

Looks up an import file by byte offset into the import-files segment.

Use this to resolve ImpInfo::imp_file_offset to an ImpFile.

Returns None if offset is negative, the segment is missing, or the entry is out of bounds.

Source

pub fn array_desc(&self, offset: i32) -> Option<ArrayDesc<'a>>

Returns an array descriptor at the given byte offset in segment 10.

Returns None if offset is negative, the segment is missing, or the entry is out of bounds.

Source

pub fn cust_data(&self, offset: i32) -> CustDataIter<'a>

Returns an iterator over the custom data chain starting at offset in the CDGuids directory (segment 12).

The chain is a linked list of CustDataEntry values, ending when next is -1.

Source

pub fn lib_cust_data(&self) -> CustDataIter<'a>

Returns the library-level custom data chain.

Source

pub fn resolve_cust_data_entry( &self, entry: &CustDataEntry<'_>, ) -> Option<(Guid<'a>, ConstValue<'a>)>

Resolves one custom data entry into its GUID and value.

Returns None if the GUID or data offset cannot be resolved.

Auto Trait Implementations§

§

impl<'a> Freeze for TypeLib<'a>

§

impl<'a> RefUnwindSafe for TypeLib<'a>

§

impl<'a> Send for TypeLib<'a>

§

impl<'a> Sync for TypeLib<'a>

§

impl<'a> Unpin for TypeLib<'a>

§

impl<'a> UnsafeUnpin for TypeLib<'a>

§

impl<'a> UnwindSafe for TypeLib<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.