(self, value, asn1Spec=None, encodeFun=None, **options)
| 74 | raise error.PyAsn1Error('Not implemented') |
| 75 | |
| 76 | def encode(self, value, asn1Spec=None, encodeFun=None, **options): |
| 77 | |
| 78 | if asn1Spec is None: |
| 79 | tagSet = value.tagSet |
| 80 | else: |
| 81 | tagSet = asn1Spec.tagSet |
| 82 | |
| 83 | # untagged item? |
| 84 | if not tagSet: |
| 85 | substrate, isConstructed, isOctets = self.encodeValue( |
| 86 | value, asn1Spec, encodeFun, **options |
| 87 | ) |
| 88 | return substrate |
| 89 | |
| 90 | defMode = options.get('defMode', True) |
| 91 | |
| 92 | substrate = b'' |
| 93 | |
| 94 | for idx, singleTag in enumerate(tagSet.superTags): |
| 95 | |
| 96 | defModeOverride = defMode |
| 97 | |
| 98 | # base tag? |
| 99 | if not idx: |
| 100 | try: |
| 101 | substrate, isConstructed, isOctets = self.encodeValue( |
| 102 | value, asn1Spec, encodeFun, **options |
| 103 | ) |
| 104 | |
| 105 | except error.PyAsn1Error as exc: |
| 106 | raise error.PyAsn1Error( |
| 107 | 'Error encoding %r: %s' % (value, exc)) |
| 108 | |
| 109 | if LOG: |
| 110 | LOG('encoded %svalue %s into %s' % ( |
| 111 | isConstructed and 'constructed ' or '', value, substrate |
| 112 | )) |
| 113 | |
| 114 | if not substrate and isConstructed and options.get('ifNotEmpty', False): |
| 115 | return substrate |
| 116 | |
| 117 | if not isConstructed: |
| 118 | defModeOverride = True |
| 119 | |
| 120 | if LOG: |
| 121 | LOG('overridden encoding mode into definitive for primitive type') |
| 122 | |
| 123 | header = self.encodeTag(singleTag, isConstructed) |
| 124 | |
| 125 | if LOG: |
| 126 | LOG('encoded %stag %s into %s' % ( |
| 127 | isConstructed and 'constructed ' or '', |
| 128 | singleTag, debug.hexdump(bytes(header)))) |
| 129 | |
| 130 | header += self.encodeLength(len(substrate), defModeOverride) |
| 131 | |
| 132 | if LOG: |
| 133 | LOG('encoded %s octets (tag + payload) into %s' % ( |
no test coverage detected