laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/37145?usp=email )
Change subject: pySim.tlv.COMPR_TLV_IE: Patch comprehension bit if derived class misses it ......................................................................
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 --- M pySim/tlv.py 1 file changed, 28 insertions(+), 1 deletion(-)
Approvals: laforge: Looks good to me, approved Jenkins Builder: Verified osmith: Looks good to me, but someone else must approve
diff --git a/pySim/tlv.py b/pySim/tlv.py index 5835d33..3d7a420 100644 --- a/pySim/tlv.py +++ b/pySim/tlv.py @@ -268,7 +268,21 @@ 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):