Skip to content

Commit afd05cb

Browse files
author
Ladislav Andel
committed
A new load method returning a tuple (an instance of current class and byte length)
In case there is a byte pool with many records they need to be extracted separately. Each record might have variable length depending on data in it.
1 parent c09b87c commit afd05cb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

asn1crypto/core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,32 @@ def load(cls, encoded_data, strict=False, **kwargs):
230230
value, _ = _parse_build(encoded_data, spec=spec, spec_params=kwargs, strict=strict)
231231
return value
232232

233+
@classmethod
234+
def load_p(cls, encoded_data, strict=False, **kwargs):
235+
"""
236+
Loads a BER/DER-encoded byte string using the current class as the spec
237+
238+
:param encoded_data:
239+
A byte string of BER or DER-encoded data
240+
241+
:param strict:
242+
A boolean indicating if trailing data should be forbidden - if so, a
243+
ValueError will be raised when trailing data exists
244+
245+
:return:
246+
An instance of the current class and byte length in tuple
247+
"""
248+
249+
if not isinstance(encoded_data, byte_cls):
250+
raise TypeError('encoded_data must be a byte string, not %s' % type_name(encoded_data))
251+
252+
spec = None
253+
if cls.tag is not None:
254+
spec = cls
255+
256+
value, length = _parse_build(encoded_data, spec=spec, spec_params=kwargs, strict=strict)
257+
return value, length
258+
233259
def __init__(self, explicit=None, implicit=None, no_explicit=False, tag_type=None, class_=None, tag=None,
234260
optional=None, default=None, contents=None, method=None):
235261
"""

0 commit comments

Comments
 (0)