osmocom.tlv

TLV parser/encoder for BER-TLV, DGI and COMPREHENSION-TLV

TLV parser/encoder library supporting various formats.

class osmocom.tlv.BER_TLV_IE(**kwargs)

TLV_IE formatted as ASN.1 BER described in ITU-T X.690 8.1.2.

class osmocom.tlv.COMPACT_TLV_IE(**kwargs)

TLV_IE formatted as COMPACT-TLV described in ISO 7816

to_tlv(context: dict = {})

Convert the internal representation to binary TLV bytes.

class osmocom.tlv.COMPR_TLV_IE(**kwargs)

TLV_IE formated as COMPREHENSION-TLV as described in ETSI TS 101 220.

is_tag_compatible(rawtag: int) bool

Override is_tag_compatible as we need to mask out the comprehension bit when doing compares.

class osmocom.tlv.ComprTlvMeta(name, bases, namespace, **kwargs)
class osmocom.tlv.DGI_TLV_IE(**kwargs)

TLV_IE formated as GlobalPlatform Systems Scripting Language Specification v1.1.0 Annex B.

class osmocom.tlv.IE(**kwargs)

Base class for various Information Elements. We understand the notion of a hierarchy of IEs on top of the Transcodable class.

child_by_name(name: str) IE | None

Return a child IE instance of given snake-case/json type name. This only works in case there is no more than one child IE of the given type.

child_by_type(cls) IE | None

Return a child IE instance of given type (class). This only works in case there is no more than one child IE of the given type.

from_bytes(do: bytes, context: dict = {})

Parse the value part from binary bytes to internal representation.

from_dict(decoded: dict)

Set the IE internal decoded representation to data from the argument. If this is a nested IE, the child IE instance list is re-created.

This method is symmetrical to to_dict() above, i.e. the outer dict must contain just a single key-value pair, where the key is the snake-reformatted type name of ‘self’

from_val_dict(decoded)

Set the IE internal decoded representation to data from the argument. If this is a nested IE, the child IE instance list is re-created.

This method is symmetrical to to_val_dict() aboe, i.e. there is no outer dict containig the snake-reformatted type name of ‘self’.

is_constructed()

Is this IE constructed by further nested IEs?

to_bytes(context: dict = {}) bytes

Convert the internal representation of the value part to binary bytes.

to_dict()

Return a JSON-serializable dict representing the [nested] IE data. The returned data contains an outer dict with the snake-reformatted type of ‘self’ and is hence sufficient to re-create an object from it.

abstract to_ie(context: dict = {}) bytes

Convert the internal representation to entire IE including IE header.

to_val_dict()

Return a JSON-serializable dict representing just the [nested] value portion of the IE data. This does not include any indication of the type of ‘self’, so the resulting dict alone will be insufficient ot recreate an object from it without additional type information.

class osmocom.tlv.TLV_IE(**kwargs)

Abstract base class for various TLV type Information Elements.

is_tag_compatible(rawtag) bool

Is the given rawtag compatible with this class?

to_ie(context: dict = {})

Convert the internal representation to entire IE including IE header.

to_tlv(context: dict = {})

Convert the internal representation to binary TLV bytes.

class osmocom.tlv.TLV_IE_Collection(desc=None, **kwargs)

A TLV_IE_Collection consists of multiple TLV_IE classes identified by their tags. A given encoded DO may contain any of them in any order, and may contain multiple instances of each DO.

from_bytes(binary: bytes, context: dict = {}) List[TLV_IE]

Create a list of TLV_IEs from the collection based on binary input data. :param binary: binary bytes of encoded data

Returns:

list of instances of TLV_IE sub-classes containing parsed data

from_dict(decoded: List[dict]) List[TLV_IE]

Create a list of TLV_IE instances from the collection based on an array of dicts, where they key indicates the name of the TLV_IE subclass to use.

class osmocom.tlv.TlvCollectionMeta(name, bases, namespace, **kwargs)

Metaclass which we use to set some class variables at the time of defining a subclass. This allows us to create subclasses for each Collection type, where the class represents fixed parameters like the nested IE classes and instances of it represent the actual TLV data.

class osmocom.tlv.TlvMeta(name, bases, namespace, **kwargs)

Metaclass which we use to set some class variables at the time of defining a subclass. This allows us to create subclasses for each TLV/IE type, where the class represents fixed parameters like the tag/type and instances of it represent the actual TLV data.

class osmocom.tlv.Transcodable
from_bytes(do: bytes, context: dict = {})

Convert from binary bytes to internal representation. Store the decoded result in the internal state and return it.

to_bytes(context: dict = {}) bytes

Convert from internal representation to binary bytes. Store the binary result in the internal state and return it.

osmocom.tlv.bertlv_encode_len(length: int) bytes

Encode a single Length value according to ITU-T X.690 8.1.3; only the definite form is supported here. :param length: length value to be encoded

Returns:

binary output data of BER-TLV length field

osmocom.tlv.bertlv_encode_tag(t) bytes

Encode a single Tag value according to ITU-T X.690 8.1.2

osmocom.tlv.bertlv_parse_len(binary: bytes) Tuple[int, bytes]

Parse a single Length value according to ITU-T X.690 8.1.3; only the definite form is supported here. :param binary: binary input data of BER-TLV length field

Returns:

Tuple of (length, remainder)

osmocom.tlv.bertlv_parse_one(binary: bytes) Tuple[dict, int, bytes, bytes]

Parse a single TLV IE at the start of the given binary data. :param binary: binary input data of BER-TLV length field

Returns:

dict, len:int, remainder:bytes)

Return type:

Tuple of (tag

osmocom.tlv.bertlv_parse_one_rawtag(binary: bytes) Tuple[int, int, bytes, bytes]

Parse a single TLV IE at the start of the given binary data; return tag as raw integer. :param binary: binary input data of BER-TLV length field

Returns:

int, len:int, remainder:bytes)

Return type:

Tuple of (tag

osmocom.tlv.bertlv_parse_tag(binary: bytes) Tuple[dict, bytes]

Parse a single Tag value according to ITU-T X.690 8.1.2 :param binary: binary input data of BER-TLV length field

Returns:

int, constructed:bool, tag:int}, remainder:bytes)

Return type:

Tuple of ({class

osmocom.tlv.bertlv_parse_tag_raw(binary: bytes) Tuple[int, bytes]

Get a single raw Tag from start of input according to ITU-T X.690 8.1.2 :param binary: binary input data of BER-TLV length field

Returns: Tuple of (tag:int, remainder:bytes)

osmocom.tlv.bertlv_return_one_rawtlv(binary: bytes) Tuple[int, int, bytes, bytes]

Return one single [encoded] TLV IE at the start of the given binary data. :param binary: binary input data of BER-TLV length field

Returns:

int, len:int, tlv:bytes, remainder:bytes)

Return type:

Tuple of (tag

osmocom.tlv.comprehensiontlv_encode_tag(tag) bytes

Encode a single Tag according to ETSI TS 101 220 Section 7.1.1

osmocom.tlv.comprehensiontlv_parse_one(binary: bytes) Tuple[dict, int, bytes, bytes]

Parse a single TLV IE at the start of the given binary data. :param binary: binary input data of BER-TLV length field

Returns:

dict, len:int, remainder:bytes)

Return type:

Tuple of (tag

osmocom.tlv.comprehensiontlv_parse_tag(binary: bytes) Tuple[dict, bytes]

Parse a single Tag according to ETSI TS 101 220 Section 7.1.1

osmocom.tlv.comprehensiontlv_parse_tag_raw(binary: bytes) Tuple[int, bytes]

Parse a single Tag according to ETSI TS 101 220 Section 7.1.1

osmocom.tlv.dgi_encode_len(length: int) bytes

Encode a single Length value according to GlobalPlatform Systems Scripting Language Specification v1.1.0 Annex B. :param length: length value to be encoded

Returns:

binary output data of encoded length field

osmocom.tlv.dgi_parse_len(binary: bytes) Tuple[int, bytes]

Parse a single Length value according to GlobalPlatform Systems Scripting Language Specification v1.1.0 Annex B. :param binary: binary input data of BER-TLV length field

Returns:

Tuple of (length, remainder)

osmocom.tlv.flatten_dict_lists(inp)

hierarchically flatten each list-of-dicts into a single dict. This is useful to make the output of hierarchical TLV decoder structures flatter and more easy to read.