daniel has uploaded this change for review. ( https://gerrit.osmocom.org/c/python/pyosmocom/+/42556?usp=email )
Change subject: tlv: Allow control over comprehension bit in COMPR_TLV_IE ......................................................................
tlv: Allow control over comprehension bit in COMPR_TLV_IE
This allows construction of a comprehension TLV with the bit set or cleared. The default is comprehension=True to not break existing code.
```
foo = Foo(decoded=b"2342", comprehension=True) bar = Foo(decoded=b"2342", comprehension=False)
```
Related: OS#6989 Change-Id: I9ca689b9b51152f3907ea470c7b42a0b12208459 --- M src/osmocom/tlv.py 1 file changed, 6 insertions(+), 5 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/python/pyosmocom refs/changes/56/42556/1
diff --git a/src/osmocom/tlv.py b/src/osmocom/tlv.py index ada7380..07adfb9 100644 --- a/src/osmocom/tlv.py +++ b/src/osmocom/tlv.py @@ -58,11 +58,12 @@ compr = bool(binary[0] & 0x80) return ({'comprehension': compr, 'tag': tag}, binary[1:])
-def comprehensiontlv_encode_tag(tag) -> bytes: +def comprehensiontlv_encode_tag(tag, comprehension: bool = True) -> bytes: """Encode a single Tag according to ETSI TS 101 220 Section 7.1.1""" # permit caller to specify tag also as integer value + # This happens if the class is created in code. In that case the if isinstance(tag, int): - compr = bool(tag < 0xff and tag & 0x80) + compr = comprehension tag = {'tag': tag, 'comprehension': compr} compr = tag.get('comprehension', False) if tag['tag'] in [0x00, 0x80, 0xff] or tag['tag'] > 0xff: @@ -74,7 +75,7 @@ return b'\x7f' + byte2.to_bytes(1, 'big') + byte3.to_bytes(1, 'big') else: # 1-byte format - ret = tag['tag'] + ret = tag['tag'] & 0x7f if compr: ret |= 0x80 return ret.to_bytes(1, 'big') @@ -648,7 +649,7 @@
def __init__(self, **kwargs): super().__init__(**kwargs) - self.comprehension = False + self.comprehension = kwargs.get("comprehension", True)
@classmethod def _decode_tag(cls, do: bytes) -> Tuple[dict, bytes]: @@ -672,7 +673,7 @@ return ctag & 0x7f == rawtag & 0x7f
def _encode_tag(self) -> bytes: - return comprehensiontlv_encode_tag(self._compute_tag()) + return comprehensiontlv_encode_tag(self._compute_tag(), self.comprehension)
def _encode_len(self, val: bytes) -> bytes: return bertlv_encode_len(len(val))