Example #1
0
 def encode_enum(self, obj):
     # empty enum
     if len(obj._cont) == 0:
         obj._msg = BER_TLV(self.get_name())
         return
     # get the maximum integer value to know the length on which to encode
     if isinstance(self._ENC_ENUM_LEN, (int, long)) \
     and self._ENC_ENUM_LEN % 8 == 0:
         # encoder forcing ENUMERATED length
         bit_len = abs(self._ENC_ENUM_LEN)
     else:
         # minimum byte length for ENUMERATED
         bit_len = 1 + len_bits(max(obj._cont.values()))
         miss_len = bit_len % 8
         if miss_len != 0:
             bit_len += 8 - miss_len
     # and get the identifier integer value from the string value
     V = Int('V', Pt=obj._cont[obj._val], Type='int%s'%bit_len,
             Repr=self._REPR_ENUM)
     if self._ENUM_BUILD_DICT:
         V.Dict = dict([(i[1], i[0]) for i in obj._cont.items()])
     #
     obj._msg = BER_TLV(obj.get_name())
     obj._msg.set(V)
     self.handle_tag_enc(obj)
Example #2
0
 def encode_enum(self, obj):
     # empty enum
     if len(obj._cont) == 0:
         obj._msg = BER_TLV(self.get_name())
         return
     # get the maximum integer value to know the length on which to encode
     if isinstance(self._ENC_ENUM_LEN, (int, long)) \
     and self._ENC_ENUM_LEN % 8 == 0:
         # encoder forcing ENUMERATED length
         bit_len = abs(self._ENC_ENUM_LEN)
     else:
         # minimum byte length for ENUMERATED
         bit_len = 1 + len_bits(max(obj._cont.values()))
         miss_len = bit_len % 8
         if miss_len != 0:
             bit_len += 8 - miss_len
     # and get the identifier integer value from the string value
     V = Int('V',
             Pt=obj._cont[obj._val],
             Type='int%s' % bit_len,
             Repr=self._REPR_ENUM)
     if self._ENUM_BUILD_DICT:
         V.Dict = dict([(i[1], i[0]) for i in obj._cont.items()])
     #
     obj._msg = BER_TLV(obj.get_name())
     obj._msg.set(V)
     self.handle_tag_enc(obj)
Example #3
0
 def decode_enum_val(self, obj):
     tlv = self.handle_tag_dec(obj)
     # map to an Int, given the length of the value part
     bit_len = tlv[1]()*8
     if bit_len:
         V = Int('V', Type='int%s'%bit_len, Repr=self._REPR_ENUM)
         V.map(str(tlv[2]))
         if self._ENUM_BUILD_TYPE:
             V.Dict = dict([(i[1], i[0]) for i in obj._cont.items()])
         tlv.set(V)
         #
         val_int = V()
         enum_int = obj._cont.values()
         if val_int in enum_int:
             obj._val = obj._cont.keys()[enum_int.index(val_int)]
         elif obj._ext is not None:
             # if extended index value is unknown,
             # the decoded value cannot be retrieved
             # WNG: we put a dummy string here as value
             obj._val = '_ext_%i' % val_int
         elif self._SAFE:
             raise(ASN1_BER_DECODER('%s: invalid ENUMERATED identifier %s' \
                   % (obj.get_fullname(), val_int)))
Example #4
0
 def decode_enum_val(self, obj):
     tlv = self.handle_tag_dec(obj)
     # map to an Int, given the length of the value part
     bit_len = tlv[1]() * 8
     if bit_len:
         V = Int('V', Type='int%s' % bit_len, Repr=self._REPR_ENUM)
         V.map(str(tlv[2]))
         if self._ENUM_BUILD_TYPE:
             V.Dict = dict([(i[1], i[0]) for i in obj._cont.items()])
         tlv.set(V)
         #
         val_int = V()
         enum_int = obj._cont.values()
         if val_int in enum_int:
             obj._val = obj._cont.keys()[enum_int.index(val_int)]
         elif obj._ext is not None:
             # if extended index value is unknown,
             # the decoded value cannot be retrieved
             # WNG: we put a dummy string here as value
             obj._val = '_ext_%i' % val_int
         elif self._SAFE:
             raise(ASN1_BER_DECODER('%s: invalid ENUMERATED identifier %s' \
                   % (obj.get_fullname(), val_int)))