laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/python/pyosmocom/+/38272?usp=email )
Change subject: osmocom.construct.Asn1DerInteger ......................................................................
osmocom.construct.Asn1DerInteger
This is a 'construct' type which can be used for encoding/decoding integer values according to ASN.1 DER encoding rules.
Change-Id: I0cfe97daf957919de86453d6d44f9c99ab3075ac --- M src/osmocom/construct.py 1 file changed, 15 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/python/pyosmocom refs/changes/72/38272/1
diff --git a/src/osmocom/construct.py b/src/osmocom/construct.py index def5d85..cd1fbe2 100644 --- a/src/osmocom/construct.py +++ b/src/osmocom/construct.py @@ -16,6 +16,7 @@ from construct.core import evaluate from construct.lib import integertypes
+from osmocom import tlv from osmocom.utils import b2h, h2b, swap_nibbles
# (C) 2021-2022 by Harald Welte laforge@osmocom.org @@ -619,6 +620,20 @@ stream_write(stream, data, length, path) return obj
+class Asn1DerInteger(Construct): + """An integer value using ASN.1 DER encoding rules.""" + def _parse(self, stream, context, path): + data = stream_read_entire(stream, path) + val, remainder = tlv.bertlv_parse_len(data) + return val + + def _build(self, obj, stream, context, path): + if not isinstance(obj, integertypes): + raise IntegerError(f"value {obj} is not an integer", path=path) + val = tlv.bertlv_encode_len(obj) + stream_write(stream, val, len(val), path) + return obj + # merged definitions of 24.008 + 23.040 TypeOfNumber = Enum(BitsInteger(3), unknown=0, international=1, national=2, network_specific=3, short_code=4, alphanumeric=5, abbreviated=6, reserved_for_extension=7)