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
This commit is contained in:
Harald Welte
2024-06-09 21:38:09 +02:00
parent 05349a0c65
commit cdf661b24c

View File

@@ -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):