| 17 | |
| 18 | |
| 19 | class BooleanPayloadDecoder(decoder.AbstractSimplePayloadDecoder): |
| 20 | protoComponent = univ.Boolean(0) |
| 21 | |
| 22 | def valueDecoder(self, substrate, asn1Spec, |
| 23 | tagSet=None, length=None, state=None, |
| 24 | decodeFun=None, substrateFun=None, |
| 25 | **options): |
| 26 | |
| 27 | if length != 1: |
| 28 | raise error.PyAsn1Error('Not single-octet Boolean payload') |
| 29 | |
| 30 | for chunk in readFromStream(substrate, length, options): |
| 31 | if isinstance(chunk, SubstrateUnderrunError): |
| 32 | yield chunk |
| 33 | |
| 34 | byte = chunk[0] |
| 35 | |
| 36 | # CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while |
| 37 | # BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1 |
| 38 | # in https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 39 | if byte == 0xff: |
| 40 | value = 1 |
| 41 | |
| 42 | elif byte == 0x00: |
| 43 | value = 0 |
| 44 | |
| 45 | else: |
| 46 | raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte) |
| 47 | |
| 48 | yield self._createComponent(asn1Spec, tagSet, value, **options) |
| 49 | |
| 50 | |
| 51 | # TODO: prohibit non-canonical encoding |