OLD | NEW |
(Empty) | |
| 1 # CER decoder |
| 2 from pyasn1.type import univ |
| 3 from pyasn1.codec.ber import decoder |
| 4 from pyasn1.compat.octets import oct2int |
| 5 from pyasn1 import error |
| 6 |
| 7 class BooleanDecoder(decoder.AbstractSimpleDecoder): |
| 8 protoComponent = univ.Boolean(0) |
| 9 def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet, length, |
| 10 state, decodeFun, substrateFun): |
| 11 head, tail = substrate[:length], substrate[length:] |
| 12 if not head or length != 1: |
| 13 raise error.PyAsn1Error('Not single-octet Boolean payload') |
| 14 byte = oct2int(head[0]) |
| 15 # CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while |
| 16 # BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1 |
| 17 # in http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 18 if byte == 0xff: |
| 19 value = 1 |
| 20 elif byte == 0x00: |
| 21 value = 0 |
| 22 else: |
| 23 raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte) |
| 24 return self._createComponent(asn1Spec, tagSet, value), tail |
| 25 |
| 26 tagMap = decoder.tagMap.copy() |
| 27 tagMap.update({ |
| 28 univ.Boolean.tagSet: BooleanDecoder() |
| 29 }) |
| 30 |
| 31 typeMap = decoder.typeMap |
| 32 |
| 33 class Decoder(decoder.Decoder): pass |
| 34 |
| 35 decode = Decoder(tagMap, decoder.typeMap) |
OLD | NEW |