- obj.lenindef = lenindef
- obj.ber_encoded = True
- return obj, (v[EOC_LEN:] if lenindef else v)
- raise TagMismatch(
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
-
- def __repr__(self):
- return pp_console_row(next(self.pps()))
-
- def pps(self, decode_path=()):
- value = None
- blob = None
- if self.ready:
- bit_len, blob = self._value
- value = "%d bits" % bit_len
- if len(self.specs) > 0:
- blob = tuple(self.named)
- yield _pp(
- obj=self,
- asn1_type_name=self.asn1_type_name,
- obj_name=self.__class__.__name__,
- decode_path=decode_path,
- value=value,
- blob=blob,
- optional=self.optional,
- default=self == self.default,
- impl=None if self.tag == self.tag_default else tag_decode(self.tag),
- expl=None if self._expl is None else tag_decode(self._expl),
- offset=self.offset,
- tlen=self.tlen,
- llen=self.llen,
- vlen=self.vlen,
- expl_offset=self.expl_offset if self.expled else None,
- expl_tlen=self.expl_tlen if self.expled else None,
- expl_llen=self.expl_llen if self.expled else None,
- expl_vlen=self.expl_vlen if self.expled else None,
- expl_lenindef=self.expl_lenindef,
- lenindef=self.lenindef,
- ber_encoded=self.ber_encoded,
- bered=self.bered,
- )
- defined_by, defined = self.defined or (None, None)
- if defined_by is not None:
- yield defined.pps(
- decode_path=decode_path + (DecodePathDefBy(defined_by),)
+ return obj, tail
+ if t != self.tag_constructed:
+ raise TagMismatch(
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ if not ctx.get("bered", False):
+ raise DecodeError(
+ "unallowed BER constructed encoding",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ if tag_only: # pragma: no cover
+ return None
+ lenindef = False
+ try:
+ l, llen, v = len_decode(lv)
+ except LenIndefForm:
+ llen, l, v = 1, 0, lv[1:]
+ lenindef = True
+ except DecodeError as err:
+ raise err.__class__(
+ msg=err.msg,
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ if l > len(v):
+ raise NotEnoughData(
+ "encoded length is longer than data",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ if not lenindef and l == 0:
+ raise NotEnoughData(
+ "zero length",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ chunks = []
+ sub_offset = offset + tlen + llen
+ vlen = 0
+ while True:
+ if lenindef:
+ if v[:EOC_LEN].tobytes() == EOC:
+ break
+ else:
+ if vlen == l:
+ break
+ if vlen > l:
+ raise DecodeError(
+ "chunk out of bounds",
+ klass=self.__class__,
+ decode_path=decode_path + (str(len(chunks) - 1),),
+ offset=chunks[-1].offset,
+ )
+ sub_decode_path = decode_path + (str(len(chunks)),)
+ try:
+ chunk, v_tail = BitString().decode(
+ v,
+ offset=sub_offset,
+ decode_path=sub_decode_path,
+ leavemm=True,
+ ctx=ctx,
+ _ctx_immutable=False,
+ )
+ except TagMismatch:
+ raise DecodeError(
+ "expected BitString encoded chunk",
+ klass=self.__class__,
+ decode_path=sub_decode_path,
+ offset=sub_offset,
+ )
+ chunks.append(chunk)
+ sub_offset += chunk.tlvlen
+ vlen += chunk.tlvlen
+ v = v_tail
+ if len(chunks) == 0:
+ raise DecodeError(
+ "no chunks",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ values = []
+ bit_len = 0
+ for chunk_i, chunk in enumerate(chunks[:-1]):
+ if chunk.bit_len % 8 != 0:
+ raise DecodeError(
+ "BitString chunk is not multiple of 8 bits",
+ klass=self.__class__,
+ decode_path=decode_path + (str(chunk_i),),
+ offset=chunk.offset,
+ )
+ values.append(bytes(chunk))
+ bit_len += chunk.bit_len
+ chunk_last = chunks[-1]
+ values.append(bytes(chunk_last))
+ bit_len += chunk_last.bit_len
+ obj = self.__class__(
+ value=(bit_len, b"".join(values)),
+ impl=self.tag,
+ expl=self._expl,
+ default=self.default,
+ optional=self.optional,
+ _specs=self.specs,
+ _decoded=(offset, llen, vlen + (EOC_LEN if lenindef else 0)),
+ )
+ obj.lenindef = lenindef
+ obj.ber_encoded = True
+ return obj, (v[EOC_LEN:] if lenindef else v)
+
+ def __repr__(self):
+ return pp_console_row(next(self.pps()))
+
+ def pps(self, decode_path=()):
+ value = None
+ blob = None
+ if self.ready:
+ bit_len, blob = self._value
+ value = "%d bits" % bit_len
+ if len(self.specs) > 0:
+ blob = tuple(self.named)
+ yield _pp(
+ obj=self,
+ asn1_type_name=self.asn1_type_name,
+ obj_name=self.__class__.__name__,
+ decode_path=decode_path,
+ value=value,
+ blob=blob,
+ optional=self.optional,
+ default=self == self.default,
+ impl=None if self.tag == self.tag_default else tag_decode(self.tag),
+ expl=None if self._expl is None else tag_decode(self._expl),
+ offset=self.offset,
+ tlen=self.tlen,
+ llen=self.llen,
+ vlen=self.vlen,
+ expl_offset=self.expl_offset if self.expled else None,
+ expl_tlen=self.expl_tlen if self.expled else None,
+ expl_llen=self.expl_llen if self.expled else None,
+ expl_vlen=self.expl_vlen if self.expled else None,
+ expl_lenindef=self.expl_lenindef,
+ lenindef=self.lenindef,
+ ber_encoded=self.ber_encoded,
+ bered=self.bered,
+ )
+ defined_by, defined = self.defined or (None, None)
+ if defined_by is not None:
+ yield defined.pps(
+ decode_path=decode_path + (DecodePathDefBy(defined_by),)