From 41a16c86ddfa12bc30a2af386ec35a29ddd92d4f Mon Sep 17 00:00:00 2001 From: "j.barotin" Date: Wed, 2 Nov 2016 17:10:03 +0100 Subject: [PATCH] add a _DISPLAY_LABEL option in order to choose displaying ASN1 code in order to string label for enum and choice type --- libmich/asn1/PER.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/libmich/asn1/PER.py b/libmich/asn1/PER.py index 346b568..a1f1fa6 100644 --- a/libmich/asn1/PER.py +++ b/libmich/asn1/PER.py @@ -277,6 +277,11 @@ class PER(ASN1.ASN1Codec): # CODEC customizations: # to build dictionnary for encoded ENUMERATED, CHOICE, ... _ENUM_BUILD_DICT = True + + # add specific option in order to choose displaying ASN1 code or string + # label for enum and choice type + _DISPLAY_LABEL = True + # to pad BIT STRING with CONTAINING object to octet-align it _U_BITSTR_CONTAIN_PAD = True # @@ -1413,7 +1418,12 @@ def decode_enum(self, obj, buf): if ind >= root_num: raise(ASN1_PER_DECODER('%s: invalid enumerated index (%s)'\ % (obj.get_fullname(), ind))) - obj._val = obj._cont.keys()[ind] + + if self._DISPLAY_LABEL : + obj._val = obj._cont.keys()[ind] + else: + obj._val = obj._cont[obj._cont.keys()[ind]] + return buf #--------------------------------------------------------------------------# @@ -1637,6 +1647,8 @@ def decode_choice(self, obj, buf): return self._decode_choice_ext(obj, buf) else: root_names = obj._cont.keys() + + idx = 0 # # for CHOICE in the root # 3) get choice's name @@ -1664,6 +1676,7 @@ def decode_choice(self, obj, buf): % (obj.get_fullname(), ind._val))) cho_name = obj._cont.keys()[ind._val] cho = obj._cont[cho_name] + idx = ind._val # # 3bis) get potential padding #if self.is_aligned(): @@ -1674,7 +1687,11 @@ def decode_choice(self, obj, buf): obj._msg.append(cho._msg) self._off += cho._msg.bit_len() # - obj._val = (cho_name, cho._val) + if self._DISPLAY_LABEL: + obj._val = (cho_name, cho._val) + else: + obj._val = (idx, cho._val) + # clean up content object cho._val = None return buf