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>
impl<'a> TypeLib<'a>
Sourcepub const HEADER_SIZE: usize = 0x54
pub const HEADER_SIZE: usize = 0x54
Size of the fixed MSFT header in bytes.
Sourcepub const DISPATCH_HREF_OFFSET: u32 = 0x0100_0000
pub const DISPATCH_HREF_OFFSET: u32 = 0x0100_0000
Offset added to hreftypes for dispatch-side references in dual interfaces.
Sourcepub fn parse(data: &'a [u8]) -> Result<Self, Error>
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.
Sourcepub fn format_version(&self) -> u32
pub fn format_version(&self) -> u32
Format version word at offset 0x04 (typically 0x00010002).
Sourcepub fn lib_guid_offset(&self) -> i32
pub fn lib_guid_offset(&self) -> i32
Offset of the library GUID in the GUID table segment.
Sourcepub fn varflags(&self) -> u32
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.
Sourcepub fn syskind(&self) -> u8
pub fn syskind(&self) -> u8
Target platform (SYSKIND, bits 0-3 of varflags).
0 = Win16, 1 = Win32, 2 = Mac, 3 = Win64.
Sourcepub fn has_help_dll(&self) -> bool
pub fn has_help_dll(&self) -> bool
Whether a help-string DLL offset is present (HELPDLLFLAG, bit 8 of
varflags).
Sourcepub fn version(&self) -> u32
pub fn version(&self) -> u32
Library version (set with SetVersion).
Low 16 bits = major, high 16 bits = minor.
Sourcepub fn version_major(&self) -> u16
pub fn version_major(&self) -> u16
Major version number (low 16 bits of version).
Sourcepub fn version_minor(&self) -> u16
pub fn version_minor(&self) -> u16
Minor version number (high 16 bits of version).
Sourcepub fn typeinfo_count(&self) -> usize
pub fn typeinfo_count(&self) -> usize
Number of TypeInfo entries in this library.
Sourcepub fn helpstring_offset(&self) -> i32
pub fn helpstring_offset(&self) -> i32
Offset of the help string in the string table, or -1.
Sourcepub fn helpstringcontext(&self) -> i32
pub fn helpstringcontext(&self) -> i32
Library-level help string context at offset 0x28.
Sourcepub fn helpcontext(&self) -> i32
pub fn helpcontext(&self) -> i32
Library-level help context at offset 0x2C.
Sourcepub fn nametablecount(&self) -> i32
pub fn nametablecount(&self) -> i32
Number of entries in the name table at offset 0x30.
Sourcepub fn nametablechars(&self) -> i32
pub fn nametablechars(&self) -> i32
Number of characters in the name table at offset 0x34.
Sourcepub fn name_offset(&self) -> i32
pub fn name_offset(&self) -> i32
Offset of the library name in the name table.
Sourcepub fn helpfile_offset(&self) -> i32
pub fn helpfile_offset(&self) -> i32
Offset of the help-file name in the string table, or -1.
Sourcepub fn custom_data_offset(&self) -> i32
pub fn custom_data_offset(&self) -> i32
Offset into the CDGuids directory for library-level custom data, or -1.
Sourcepub fn dispatchpos(&self) -> i32
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.
Sourcepub fn segment(&self, index: usize) -> Option<(usize, usize)>
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).
Sourcepub fn segment_data(&self, index: usize) -> Option<&'a [u8]>
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.
Sourcepub fn guid_hash_data(&self) -> Option<&'a [u8]>
pub fn guid_hash_data(&self) -> Option<&'a [u8]>
Returns the raw GUID hash table data (segment 4).
Sourcepub fn name_hash_data(&self) -> Option<&'a [u8]>
pub fn name_hash_data(&self) -> Option<&'a [u8]>
Returns the raw name hash table data (segment 6).
Sourcepub fn name(&self, offset: i32) -> Option<&'a str>
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.
Sourcepub fn guid(&self, offset: i32) -> Option<Guid<'a>>
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.
Sourcepub fn string(&self, offset: i32) -> Option<&'a str>
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.
Sourcepub fn lib_helpstring(&self) -> Option<&'a str>
pub fn lib_helpstring(&self) -> Option<&'a str>
Returns the library help string from the string table.
Sourcepub fn typeinfo_offset(&self, index: usize) -> Option<i32>
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.
Sourcepub fn member_names(&self, index: usize) -> Vec<&'a str>
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.
Sourcepub fn typeinfo(&self, index: usize) -> Result<TypeInfoEntry<'a>, Error>
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.
Sourcepub fn typeinfos(&'a self) -> TypeInfoIter<'a> ⓘ
pub fn typeinfos(&'a self) -> TypeInfoIter<'a> ⓘ
Returns an iterator over all TypeInfo entries.
Sourcepub fn funcs(&self, ti: &TypeInfoEntry<'_>) -> FuncIter<'a> ⓘ
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.
Sourcepub fn vars(&self, ti: &TypeInfoEntry<'_>) -> VarIter<'a> ⓘ
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.
Sourcepub fn params(&self, func: &FuncRecord<'a>) -> ParamIter<'a> ⓘ
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.
Sourcepub fn func_memid(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<i32>
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.
Sourcepub fn var_memid(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<i32>
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.
Sourcepub fn func_name(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<&'a str>
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.
Sourcepub fn var_name(&self, ti: &TypeInfoEntry<'_>, index: usize) -> Option<&'a str>
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.
Sourcepub fn const_value(&self, offs_value: i32) -> Option<ConstValue<'a>>
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.
Sourcepub fn type_desc(&self, offset: i32) -> Option<(i32, i32)>
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):extrais the pointed-to type descriptor. - For
VT_USERDEFINED(29):extrais the hreftype. - For
VT_SAFEARRAY(27):extrais the element type descriptor.
Returns None if offset is negative, the segment is missing,
or the entry is out of bounds.
Sourcepub fn resolve_hreftype(&self, hreftype: i32) -> Option<usize>
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.
Sourcepub fn resolve_hreftype_full(
&self,
hreftype: i32,
) -> Option<ResolvedHreftype<'a>>
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.
Sourcepub fn guids(&self) -> GuidEntryIter<'a> ⓘ
pub fn guids(&self) -> GuidEntryIter<'a> ⓘ
Returns an iterator over all GuidEntry values in the GUID table.
Sourcepub fn impl_types(&self, ti: &TypeInfoEntry<'_>) -> ImplTypeIter<'a> ⓘ
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.
Sourcepub fn imports(&self) -> ImpInfoIter<'a> ⓘ
pub fn imports(&self) -> ImpInfoIter<'a> ⓘ
Returns an iterator over ImpInfo entries in the import table.
Sourcepub fn imp_files(&self) -> ImpFileIter<'a> ⓘ
pub fn imp_files(&self) -> ImpFileIter<'a> ⓘ
Returns an iterator over ImpFile entries in the import-files
segment (segment 2).
Sourcepub fn imp_file(&self, offset: i32) -> Option<ImpFile<'a>>
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.
Sourcepub fn array_desc(&self, offset: i32) -> Option<ArrayDesc<'a>>
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.
Sourcepub fn cust_data(&self, offset: i32) -> CustDataIter<'a> ⓘ
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.
Sourcepub fn lib_cust_data(&self) -> CustDataIter<'a> ⓘ
pub fn lib_cust_data(&self) -> CustDataIter<'a> ⓘ
Returns the library-level custom data chain.
Sourcepub fn resolve_cust_data_entry(
&self,
entry: &CustDataEntry<'_>,
) -> Option<(Guid<'a>, ConstValue<'a>)>
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.