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.orglaforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/24448 ) Change subject: utils: COMPREHENSION-TLV support ...................................................................... utils: COMPREHENSION-TLV support Change-Id: I8d969382b73fa152ee09c456fa4aee428fb36285 --- M pySim/utils.py M tests/test_utils.py 2 files changed, 66 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/48/24448/1 diff --git a/pySim/utils.py b/pySim/utils.py index 3d96580..d96d05a 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -90,6 +90,50 @@ return (n + 1)//2 ######################################################################### +# poor man's COMPREHENSION-TLV decoder. +######################################################################### + +def comprehensiontlv_parse_tag(binary:bytes) -> Tuple[dict, bytes]: + """Parse a single Tag according to ETSI TS 101 220 Section 7.1.1""" + if binary[0] in [0x00, 0x80, 0xff]: + raise ValueError("Found illegal value 0x%02x in %s" % (binary[0], binary)) + if binary[0] == 0x7f: + # three-byte tag + tag = (binary[1] & 0x7f) << 8 + tag |= binary[2] + compr = True if binary[1] & 0x80 else False + return ({'comprehension': compr, 'tag': tag}, binary[3:]) + else: + # single byte tag + tag = binary[0] & 0x7f + compr = True if binary[0] & 0x80 else False + return ({'comprehension': compr, 'tag': tag}, binary[1:]) + +def comprehensiontlv_encode_tag(tag) -> bytes: + """Encode a single Tag according to ETSI TS 101 220 Section 7.1.1""" + # permit caller to specify tag also as integer value + if isinstance(tag, int): + compr = True if tag < 0xff and tag & 0x80 else False + tag = {'tag': tag, 'comprehension': compr} + compr = tag.get('comprehension', False) + if tag['tag'] in [0x00, 0x80, 0xff] or tag['tag'] > 0xff: + # 3-byte format + byte3 = tag['tag'] & 0xff; + byte2 = (tag['tag'] >> 8) & 0x7f + if compr: + byte2 |= 0x80 + return b'\x7f' + byte2.to_bytes(1, 'big') + byte3.to_bytes(1, 'big') + else: + # 1-byte format + ret = tag['tag'] + if compr: + ret |= 0x80 + return ret.to_bytes(1, 'big') + +# length value coding is equal to BER-TLV + + +######################################################################### # poor man's BER-TLV decoder. To be a more sophisticated OO library later ######################################################################### diff --git a/tests/test_utils.py b/tests/test_utils.py index 17a9300..ed22eff 100755 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -179,6 +179,28 @@ res = utils.bertlv_parse_one(b'\x81\x01\x01'); self.assertEqual(res, ({'tag':1, 'constructed':False, 'class':2}, 1, b'\x01', b'')) +class TestComprTlv(unittest.TestCase): + def test_ComprTlvTagDec(self): + res = utils.comprehensiontlv_parse_tag(b'\x12\x23') + self.assertEqual(res, ({'tag': 0x12, 'comprehension': False}, b'\x23')) + res = utils.comprehensiontlv_parse_tag(b'\x92') + self.assertEqual(res, ({'tag': 0x12, 'comprehension': True}, b'')) + res = utils.comprehensiontlv_parse_tag(b'\x7f\x12\x34') + self.assertEqual(res, ({'tag': 0x1234, 'comprehension': False}, b'')) + res = utils.comprehensiontlv_parse_tag(b'\x7f\x82\x34\x56') + self.assertEqual(res, ({'tag': 0x234, 'comprehension': True}, b'\x56')) + + def test_ComprTlvTagEnc(self): + res = utils.comprehensiontlv_encode_tag(0x12) + self.assertEqual(res, b'\x12') + res = utils.comprehensiontlv_encode_tag({'tag': 0x12}) + self.assertEqual(res, b'\x12') + res = utils.comprehensiontlv_encode_tag({'tag': 0x12, 'comprehension':True}) + self.assertEqual(res, b'\x92') + res = utils.comprehensiontlv_encode_tag(0x1234) + self.assertEqual(res, b'\x7f\x12\x34') + res = utils.comprehensiontlv_encode_tag({'tag': 0x1234, 'comprehension':True}) + self.assertEqual(res, b'\x7f\x92\x34') if __name__ == "__main__": unittest.main() -- To view, visit https://gerrit.osmocom.org/c/pysim/+/24448 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: pysim Gerrit-Branch: master Gerrit-Change-Id: I8d969382b73fa152ee09c456fa4aee428fb36285 Gerrit-Change-Number: 24448 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/c5e5b511/attachment.htm>