From cdf661b24cfebb63b57405c2b55b3c42a8f277c8 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 9 Jun 2024 21:38:09 +0200 Subject: [PATCH] pySim.tlv.COMPR_TLV_IE: Patch comprehension bit if derived class misses it Our current implementation assumes that all COMPR_TLV_IE are created with a raw tag value that has the comprehension bit set. Check for this during the class __new__ method and print a warning if we have to fix it up Change-Id: I299cd65f32dffda9040d18c17a374e8dc9ebe7da --- pySim/tlv.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pySim/tlv.py b/pySim/tlv.py index 5835d339..3d7a4205 100644 --- a/pySim/tlv.py +++ b/pySim/tlv.py @@ -268,7 +268,21 @@ class BER_TLV_IE(TLV_IE): return bertlv_encode_len(len(val)) -class COMPR_TLV_IE(TLV_IE): +class ComprTlvMeta(TlvMeta): + def __new__(mcs, name, bases, namespace, **kwargs): + x = super().__new__(mcs, name, bases, namespace) + if x.tag: + # we currently assume that the tag values always have the comprehension bit set; + # let's fix it up if a derived class has forgotten about that + if x.tag > 0xff and x.tag & 0x8000 == 0: + print("Fixing up COMPR_TLV_IE class %s: tag=0x%x has no comprehension bit" % (name, x.tag)) + x.tag = x.tag | 0x8000 + elif x.tag & 0x80 == 0: + print("Fixing up COMPR_TLV_IE class %s: tag=0x%x has no comprehension bit" % (name, x.tag)) + x.tag = x.tag | 0x80 + return x + +class COMPR_TLV_IE(TLV_IE, metaclass=ComprTlvMeta): """TLV_IE formated as COMPREHENSION-TLV as described in ETSI TS 101 220.""" def __init__(self, **kwargs):