diff options
author | Ilya Etingof <etingof@gmail.com> | 2017-09-10 21:37:33 +0200 |
---|---|---|
committer | Ilya Etingof <etingof@gmail.com> | 2017-09-10 21:37:33 +0200 |
commit | 789a4e2f4f921fdac37f28df557319b89df3050c (patch) | |
tree | 6c22518ece9cde33a25032aa2b156eab039ef341 | |
parent | 68e26963ae5da07d6f8eceabf5df168b92851d0c (diff) | |
download | pyasn1-git-789a4e2f4f921fdac37f28df557319b89df3050c.tar.gz |
wrap open type blob on encoding if needed
-rw-r--r-- | pyasn1/codec/ber/encoder.py | 16 | ||||
-rw-r--r-- | pyasn1/codec/cer/encoder.py | 17 |
2 files changed, 26 insertions, 7 deletions
diff --git a/pyasn1/codec/ber/encoder.py b/pyasn1/codec/ber/encoder.py index c97efd2..0d48a90 100644 --- a/pyasn1/codec/ber/encoder.py +++ b/pyasn1/codec/ber/encoder.py @@ -361,11 +361,21 @@ class SequenceEncoder(AbstractItemEncoder): while idx > 0: idx -= 1 if namedTypes: - if namedTypes[idx].isOptional and not value[idx].isValue: + namedType = namedTypes[idx] + if namedType.isOptional and not value[idx].isValue: continue - if namedTypes[idx].isDefaulted and value[idx] == namedTypes[idx].asn1Object: + if namedType.isDefaulted and value[idx] == namedType.asn1Object: continue - substrate = encodeFun(value[idx], **options) + substrate + + chunk = encodeFun(value[idx], **options) + + # wrap open type blob if needed + if namedTypes and namedType.openType: + asn1Spec = namedType.asn1Object + if asn1Spec.tagSet and not asn1Spec.isSameTypeWith(value[idx]): + chunk = encodeFun(asn1Spec.clone(chunk), **options) + + substrate = chunk + substrate return substrate, True, True diff --git a/pyasn1/codec/cer/encoder.py b/pyasn1/codec/cer/encoder.py index 50cb346..e305a6d 100644 --- a/pyasn1/codec/cer/encoder.py +++ b/pyasn1/codec/cer/encoder.py @@ -151,14 +151,23 @@ class SequenceEncoder(encoder.SequenceEncoder): while idx > 0: idx -= 1 if namedTypes: - if namedTypes[idx].isOptional and not value[idx].isValue: + namedType = namedTypes[idx] + if namedType.isOptional and not value[idx].isValue: continue - if namedTypes[idx].isDefaulted and value[idx] == namedTypes[idx].asn1Object: + if namedType.isDefaulted and value[idx] == namedType.asn1Object: continue - options.update(ifNotEmpty=namedTypes and namedTypes[idx].isOptional) + options.update(ifNotEmpty=namedTypes and namedType.isOptional) - substrate = encodeFun(value[idx], **options) + substrate + chunk = encodeFun(value[idx], **options) + + # wrap open type blob if needed + if namedTypes and namedType.openType: + asn1Spec = namedType.asn1Object + if asn1Spec.tagSet and not asn1Spec.isSameTypeWith(value[idx]): + chunk = encodeFun(asn1Spec.clone(chunk), **options) + + substrate = chunk + substrate return substrate, True, True |