utils: Fix bertlv_encode_tag() for multi-byte tags
We used to support only single-byte tags in bertlv_encode_tag,
let's fix that. The easy option is to simply call bertlv_parse_tag,
as that already supported multi-byte tags.
Change-Id: If0bd9137883c4c8b01c4dfcbb53cabeee5c1ce2b
diff --git a/pySim/utils.py b/pySim/utils.py
index 6cacdaa..7459a3f 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -262,15 +262,22 @@
remainder = inp & ~ (inp << (remain_bits - bitcnt))
return outp, remainder
+ def count_int_bytes(inp: int) -> int:
+ """count the number of bytes require to represent the given integer."""
+ i = 1
+ inp = inp >> 8
+ while inp:
+ i += 1
+ inp = inp >> 8
+ return i
+
if isinstance(t, int):
- # FIXME: multiple byte tags
- tag = t & 0x1f
- constructed = True if t & 0x20 else False
- cls = t >> 6
- else:
- tag = t['tag']
- constructed = t['constructed']
- cls = t['class']
+ # first convert to a dict representation
+ tag_size = count_int_bytes(t)
+ t, remainder = bertlv_parse_tag(t.to_bytes(tag_size, 'big'))
+ tag = t['tag']
+ constructed = t['constructed']
+ cls = t['class']
if tag <= 30:
t = tag & 0x1f
if constructed: