typecontainer module

binaryninja.typecontainer.TypeContainer(handle)

A TypeContainer is a generic interface to access various Binary Ninja models that contain types.

class TypeContainer(handle: LP_BNTypeContainer)[source]

Bases: object

A TypeContainer is a generic interface to access various Binary Ninja models that contain types. Types are stored with both a unique id and a unique name.

The TypeContainer class should not generally be instantiated directly. Instances can be retrieved from the following properties and methods in the API:

Parameters:

handle (LP_BNTypeContainer) – Handle pointer (Internal use only.)

add_types(types: Mapping[_types.QualifiedNameType, _types.Type], progress_func: Callable[[int, int], bool] | None = None) Mapping[_types.QualifiedName, str] | None[source]

Add or update types to a Type Container. If the Type Container already contains a type with the same name as a type being added, the existing type will be replaced with the definition given to this function, and references will be updated in the source model.

An optional progress callback is included because adding many types can be a slow operation.

Parameters:
  • types (Mapping[_types.QualifiedNameType, _types.Type]) – Dict from name -> definition of new types to add

  • progress_func (Callable[[int, int], bool] | None) – Optional function to call for progress updates

Returns:

Dict from name -> id of type in Type Container for all added types if successful, None otherwise.

Return type:

Mapping[_types.QualifiedName, str] | None

delete_type(type_id: str) bool[source]

Delete a type in the Type Container. Behavior of references to this type is not specified and you may end up with broken references if any still exist.

Parameters:

type_id (str) – Id of type to delete

Returns:

True if successful

Return type:

bool

get_type_by_id(type_id: str) Type | None[source]

Get the definition of the type in the Type Container with the given id. If no type with that id exists, returns None.

Parameters:

type_id (str) – Id of type

Returns:

Type object, if exists, else, None

Return type:

Type | None

get_type_by_name(type_name: _types.QualifiedNameType) _types.Type | None[source]

Get the definition of the type in the Type Container with the given name. If no type with that name exists, returns None.

Parameters:

type_name (_types.QualifiedNameType) – Name of type

Returns:

Type object, if exists, else, None

Return type:

_types.Type | None

get_type_id(type_name: _types.QualifiedNameType) str | None[source]

Get the unique id of the type in the Type Container with the given name. If no type with that name exists, returns None.

Parameters:

type_name (_types.QualifiedNameType) – Name of type

Returns:

Type id, if exists, else, None

Return type:

str | None

get_type_name(type_id: str) QualifiedName | None[source]

Get the unique name of the type in the Type Container with the given id. If no type with that id exists, returns None.

Parameters:

type_id (str) – Id of type

Returns:

Type name, if exists, else, None

Return type:

QualifiedName | None

parse_type_string(source: str, import_dependencies: bool = True) Tuple[Tuple[_types.QualifiedNameType, _types.Type] | None, List[TypeParserError]][source]

Parse a single type and name from a string containing their definition, with knowledge of the types in the Type Container.

Parameters:
  • source (str) – Source code to parse

  • import_dependencies (bool) – If Type Library / Type Archive types should be imported during parsing

Returns:

A tuple of (result, errors) where result is a tuple of (type, name) or None of there was a fatal error.

Return type:

Tuple[Tuple[_types.QualifiedNameType, _types.Type] | None, List[TypeParserError]]

parse_types_from_source(source: str, file_name: str, options: List[str] | None = None, include_dirs: List[str] | None = None, auto_type_source: str = '', import_dependencies: bool = True) Tuple[TypeParserResult | None, List[TypeParserError]][source]

Parse an entire block of source into types, variables, and functions, with knowledge of the types in the Type Container.

Parameters:
  • source (str) – Source code to parse

  • file_name (str) – Name of the file containing the source (optional: exists on disk)

  • options (List[str] | None) – Optional string arguments to pass as options, e.g. command line arguments

  • include_dirs (List[str] | None) – Optional list of directories to include in the header search path

  • auto_type_source (str) – Optional source of types if used for automatically generated types

  • import_dependencies (bool) – If Type Library / Type Archive types should be imported during parsing

Returns:

A tuple of (result, errors) where the result is None if there was a fatal error

Return type:

Tuple[TypeParserResult | None, List[TypeParserError]]

rename_type(type_id: str, new_name: _types.QualifiedNameType) bool[source]

Rename a type in the Type Container. All references to this type will be updated (by id) to use the new name.

Parameters:
  • type_id (str) – Id of type to update

  • new_name (_types.QualifiedNameType) – New name for the type

Returns:

True if successful

Return type:

bool

property container_type: TypeContainerType

Get the type of underlying model the Type Container is accessing. :return: Container type enum

property id: str

Get an id string for the Type Container. This will be unique within a given analysis session, but may not be globally unique. :return: Identifier string

property mutable: bool

Test if the Type Container supports mutable operations (add, rename, delete) :return: True if mutable

property name: str

Get a user-friendly name for the Type Container. :return: Display name

property platform: Platform

Get the Platform object associated with this Type Container. All Type Containers have exactly one associated Platform (as opposed to, e.g. Type Libraries). :return: Associated Platform object

property type_ids: List[str] | None

Get all type ids in a Type Container. :return: List of all type ids

property type_names: List[QualifiedName] | None

Get all type names in a Type Container. :return: List of all type names

property type_names_and_ids: Mapping[str, QualifiedName] | None

Get a mapping of all type ids and type names in a Type Container. :return: Dict of type id -> type name

property types: Mapping[str, Tuple[QualifiedName, Type]] | None

Get a mapping of all types in a Type Container. :return: All types in a dict of type id -> (type name, type definition)