summaryrefslogtreecommitdiff
path: root/pyasn1/codec/native/encoder.py
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2019-11-23 10:06:57 +0100
committerGitHub <noreply@github.com>2019-11-23 10:06:57 +0100
commit96b0a7742a243dbcdacdcdcc64e141437a40835a (patch)
tree3a085a20e202c339d60cf4cf68b867e2b532e13f /pyasn1/codec/native/encoder.py
parentf10434e31f9293c4e739ac6c5d1519f407bd1540 (diff)
parentcda318a63f8d6a3c43408ac8d3dfa405d3ca7c7c (diff)
downloadpyasn1-git-96b0a7742a243dbcdacdcdcc64e141437a40835a.tar.gz
Make BER/CER/DER decodersstreaming and suspendible (#176)
The goal of this change is to make the decoder yielding on input data starvation and resuming from where it stopped whenever the caller decides to try again (hopefully making sure that some more input becomes available). This change makes it possible for the decoder to operate on streams of data (meaning that the entire DER blob might not be immediately available on input). On top of that, the decoder yields partially reconstructed ASN.1 object on input starvation making it possible for the caller to inspect what has been decoded so far and possibly consume partial ASN.1 data. All these new feature are natively available through `StreamingDecoder` class. Previously published API is implemented as a thin wrapper on top of that ensuring backward compatibility.
Diffstat (limited to 'pyasn1/codec/native/encoder.py')
-rw-r--r--pyasn1/codec/native/encoder.py48
1 files changed, 33 insertions, 15 deletions
diff --git a/pyasn1/codec/native/encoder.py b/pyasn1/codec/native/encoder.py
index a83d00a..6b8ca6d 100644
--- a/pyasn1/codec/native/encoder.py
+++ b/pyasn1/codec/native/encoder.py
@@ -103,7 +103,7 @@ class AnyEncoder(AbstractItemEncoder):
return value.asOctets()
-tagMap = {
+TAG_MAP = {
univ.Boolean.tagSet: BooleanEncoder(),
univ.Integer.tagSet: IntegerEncoder(),
univ.BitString.tagSet: BitStringEncoder(),
@@ -136,7 +136,7 @@ tagMap = {
# Put in ambiguous & non-ambiguous types for faster codec lookup
-typeMap = {
+TYPE_MAP = {
univ.Boolean.typeId: BooleanEncoder(),
univ.Integer.typeId: IntegerEncoder(),
univ.BitString.typeId: BitStringEncoder(),
@@ -171,48 +171,66 @@ typeMap = {
}
-class Encoder(object):
+class SingleItemEncoder(object):
+
+ TAG_MAP = TAG_MAP
+ TYPE_MAP = TYPE_MAP
- # noinspection PyDefaultArgument
- def __init__(self, tagMap, typeMap={}):
- self.__tagMap = tagMap
- self.__typeMap = typeMap
+ def __init__(self, **options):
+ self._tagMap = options.get('tagMap', self.TAG_MAP)
+ self._typeMap = options.get('typeMap', self.TYPE_MAP)
def __call__(self, value, **options):
if not isinstance(value, base.Asn1Item):
- raise error.PyAsn1Error('value is not valid (should be an instance of an ASN.1 Item)')
+ raise error.PyAsn1Error(
+ 'value is not valid (should be an instance of an ASN.1 Item)')
if LOG:
debug.scope.push(type(value).__name__)
- LOG('encoder called for type %s <%s>' % (type(value).__name__, value.prettyPrint()))
+ LOG('encoder called for type %s '
+ '<%s>' % (type(value).__name__, value.prettyPrint()))
tagSet = value.tagSet
try:
- concreteEncoder = self.__typeMap[value.typeId]
+ concreteEncoder = self._typeMap[value.typeId]
except KeyError:
# use base type for codec lookup to recover untagged types
- baseTagSet = tag.TagSet(value.tagSet.baseTag, value.tagSet.baseTag)
+ baseTagSet = tag.TagSet(
+ value.tagSet.baseTag, value.tagSet.baseTag)
try:
- concreteEncoder = self.__tagMap[baseTagSet]
+ concreteEncoder = self._tagMap[baseTagSet]
except KeyError:
raise error.PyAsn1Error('No encoder for %s' % (value,))
if LOG:
- LOG('using value codec %s chosen by %s' % (concreteEncoder.__class__.__name__, tagSet))
+ LOG('using value codec %s chosen by '
+ '%s' % (concreteEncoder.__class__.__name__, tagSet))
pyObject = concreteEncoder.encode(value, self, **options)
if LOG:
- LOG('encoder %s produced: %s' % (type(concreteEncoder).__name__, repr(pyObject)))
+ LOG('encoder %s produced: '
+ '%s' % (type(concreteEncoder).__name__, repr(pyObject)))
debug.scope.pop()
return pyObject
+class Encoder(object):
+ SINGLE_ITEM_ENCODER = SingleItemEncoder
+
+ def __init__(self, **options):
+ self._singleItemEncoder = self.SINGLE_ITEM_ENCODER(**options)
+
+ def __call__(self, pyObject, asn1Spec=None, **options):
+ return self._singleItemEncoder(
+ pyObject, asn1Spec=asn1Spec, **options)
+
+
#: Turns ASN.1 object into a Python built-in type object(s).
#:
#: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
@@ -248,4 +266,4 @@ class Encoder(object):
#: >>> encode(seq)
#: [1, 2, 3]
#:
-encode = Encoder(tagMap, typeMap)
+encode = SingleItemEncoder()