Change in pysim[master]: utils: Add bertlv_encode_tag()

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat May 29 20:42:26 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/24450 )


Change subject: utils: Add bertlv_encode_tag()
......................................................................

utils: Add bertlv_encode_tag()

We so far had decoders for BER-TLV tags, but no encoder yet.

Change-Id: I4183546bed9d6232ddcefad764f4e67afcf8b2ed
---
M pySim/utils.py
1 file changed, 45 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/50/24450/1

diff --git a/pySim/utils.py b/pySim/utils.py
index 74655d4..e4caadf 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -212,6 +212,51 @@
 			i += 1
 		return ({'class':cls, 'constructed':constructed, 'tag':tag}, binary[i:])
 
+def bertlv_encode_tag(t) -> bytes:
+    """Encode a single Tag value according to ITU-T X.690 8.1.2
+    """
+    def get_top7_bits(inp:int) -> Tuple[int, int]:
+        """Get top 7 bits of integer. Returns those 7 bits as integer and the remaining LSBs."""
+        remain_bits = inp.bit_length()
+        if remain_bits >= 7:
+            bitcnt = 7
+        else:
+            bitcnt = remain_bits
+        outp = inp >> (remain_bits - bitcnt)
+        remainder = inp & ~ (inp << (remain_bits - bitcnt))
+        return outp, remainder
+
+    if isinstance(t, int):
+        # FIXME: multiple byte tags
+        tag = t & 0x1f
+        constructed = True if t & 0x20 else False
+        cls = t >> 6
+    else:
+        tag = tdict['tag']
+        constructed = tdict['constructed']
+        cls = tdict['class']
+    if tag <= 30:
+        t = tag & 0x1f
+        if constructed:
+            t |= 0x20
+        t |= (cls & 3) << 6
+        return bytes([t])
+    else: # multi-byte tag
+        t = 0x1f;
+        if constructed:
+            t |= 0x20
+        t |= (cls & 3) << 6
+        tag_bytes = bytes([t])
+        remain = tag
+        while True:
+            t, remain = get_top7_bits(remain)
+            if remain:
+                t |= 0x80
+            tag_bytes.append(bytes[t])
+            if not remain:
+                break
+        return tag_bytes
+
 def 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.

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/24450
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I4183546bed9d6232ddcefad764f4e67afcf8b2ed
Gerrit-Change-Number: 24450
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210529/0270844c/attachment.htm>


More information about the gerrit-log mailing list