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
This commit is contained in:
Harald Welte 2023-10-23 01:54:36 +02:00
parent 237ddb5bb3
commit 10669f2ddf
1 changed files with 15 additions and 8 deletions

View File

@ -262,15 +262,22 @@ def bertlv_encode_tag(t) -> bytes:
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: