summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Matveev <stargrave@stargrave.org>2020-02-10 15:09:33 +0300
committerSergey Matveev <stargrave@stargrave.org>2020-02-10 19:12:26 +0300
commita5f63bdd812919be17945607d974825895448804 (patch)
treeab7356606a2583214bff0850e3869cd0a5ccb39d
parentf33cfe692f34b97b3ff885a7128304bf2775c00e (diff)
downloadpyderasn-a5f63bdd812919be17945607d974825895448804.tar.xz
Remove unneeded _decode_chunk method
-rwxr-xr-xpyderasn.py204
1 files changed, 99 insertions, 105 deletions
diff --git a/pyderasn.py b/pyderasn.py
index 0358867..ea1b0b9 100755
--- a/pyderasn.py
+++ b/pyderasn.py
@@ -2630,64 +2630,6 @@ class BitString(Obj):
octets,
))
- def _decode_chunk(self, lv, offset, decode_path):
- try:
- l, llen, v = len_decode(lv)
- 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 l == 0:
- raise NotEnoughData(
- "zero length",
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
- pad_size = byte2int(v)
- if l == 1 and pad_size != 0:
- raise DecodeError(
- "invalid empty value",
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
- if pad_size > 7:
- raise DecodeError(
- "too big pad",
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
- if byte2int(v[l - 1:l]) & ((1 << pad_size) - 1) != 0:
- raise DecodeError(
- "invalid pad",
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
- v, tail = v[:l], v[l:]
- obj = self.__class__(
- value=((len(v) - 1) * 8 - pad_size, v[1:].tobytes()),
- impl=self.tag,
- expl=self._expl,
- default=self.default,
- optional=self.optional,
- _specs=self.specs,
- _decoded=(offset, llen, l),
- )
- return obj, tail
-
def _decode(self, tlv, offset, decode_path, ctx, tag_only):
try:
t, tlen, lv = tag_strip(tlv)
@@ -2701,7 +2643,62 @@ class BitString(Obj):
if t == self.tag:
if tag_only: # pragma: no cover
return None
- return self._decode_chunk(lv, offset, decode_path)
+ try:
+ l, llen, v = len_decode(lv)
+ 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 l == 0:
+ raise NotEnoughData(
+ "zero length",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ pad_size = byte2int(v)
+ if l == 1 and pad_size != 0:
+ raise DecodeError(
+ "invalid empty value",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ if pad_size > 7:
+ raise DecodeError(
+ "too big pad",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ if byte2int(v[l - 1:l]) & ((1 << pad_size) - 1) != 0:
+ raise DecodeError(
+ "invalid pad",
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ v, tail = v[:l], v[l:]
+ obj = self.__class__(
+ value=((len(v) - 1) * 8 - pad_size, v[1:].tobytes()),
+ impl=self.tag,
+ expl=self._expl,
+ default=self.default,
+ optional=self.optional,
+ _specs=self.specs,
+ _decoded=(offset, llen, l),
+ )
+ return obj, tail
if t != self.tag_constructed:
raise TagMismatch(
klass=self.__class__,
@@ -3056,51 +3053,6 @@ class OctetString(Obj):
self._value,
))
- def _decode_chunk(self, lv, offset, decode_path, ctx):
- try:
- l, llen, v = len_decode(lv)
- 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,
- )
- v, tail = v[:l], v[l:]
- try:
- obj = self.__class__(
- value=v.tobytes(),
- bounds=(self._bound_min, self._bound_max),
- impl=self.tag,
- expl=self._expl,
- default=self.default,
- optional=self.optional,
- _decoded=(offset, llen, l),
- ctx=ctx,
- )
- except DecodeError as err:
- raise DecodeError(
- msg=err.msg,
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
- except BoundsError as err:
- raise DecodeError(
- msg=str(err),
- klass=self.__class__,
- decode_path=decode_path,
- offset=offset,
- )
- return obj, tail
-
def _decode(self, tlv, offset, decode_path, ctx, tag_only):
try:
t, tlen, lv = tag_strip(tlv)
@@ -3114,7 +3066,49 @@ class OctetString(Obj):
if t == self.tag:
if tag_only:
return None
- return self._decode_chunk(lv, offset, decode_path, ctx)
+ try:
+ l, llen, v = len_decode(lv)
+ 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,
+ )
+ v, tail = v[:l], v[l:]
+ try:
+ obj = self.__class__(
+ value=v.tobytes(),
+ bounds=(self._bound_min, self._bound_max),
+ impl=self.tag,
+ expl=self._expl,
+ default=self.default,
+ optional=self.optional,
+ _decoded=(offset, llen, l),
+ ctx=ctx,
+ )
+ except DecodeError as err:
+ raise DecodeError(
+ msg=err.msg,
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ except BoundsError as err:
+ raise DecodeError(
+ msg=str(err),
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
+ return obj, tail
if t != self.tag_constructed:
raise TagMismatch(
klass=self.__class__,