<s> import types <EOL> from pyasn1 . type import tag , univ , char , useful <EOL> from pyasn1 . codec . ber import eoo <EOL> from pyasn1 import error <EOL> class AbstractDecoder : <EOL> protoComponent = None <EOL> def _createComponent ( self , tagSet , asn1Spec ) : <EOL> if asn1Spec is None : <EOL> return self . protoComponent . clone ( tagSet = tagSet ) <EOL> else : <EOL> return asn1Spec . clone ( ) <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> raise error . PyAsn1Error ( '<STR_LIT>' % tagSet ) <EOL> def indefLenValueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> raise error . PyAsn1Error ( '<STR_LIT>' % tagSet ) <EOL> class EndOfOctetsDecoder ( AbstractDecoder ) : <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> return eoo . endOfOctets , substrate <EOL> class IntegerDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . Integer ( <NUM_LIT:0> ) <EOL> def _valueFilter ( self , value ) : <EOL> try : <EOL> return int ( value ) <EOL> except OverflowError : <EOL> return value <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , length , <EOL> state , decodeFun ) : <EOL> if not substrate : <EOL> raise error . PyAsn1Error ( '<STR_LIT>' ) <EOL> octets = map ( ord , substrate ) <EOL> if octets [ <NUM_LIT:0> ] & <NUM_LIT> : <EOL> value = - <NUM_LIT:1> L <EOL> else : <EOL> value = <NUM_LIT:0> L <EOL> for octet in octets : <EOL> value = value << <NUM_LIT:8> | octet <EOL> value = self . _valueFilter ( value ) <EOL> return self . _createComponent ( tagSet , asn1Spec ) . clone ( value ) , substrate <EOL> class BooleanDecoder ( IntegerDecoder ) : <EOL> protoComponent = univ . Boolean ( <NUM_LIT:0> ) <EOL> def _valueFilter ( self , value ) : <EOL> if value : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> class BitStringDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . BitString ( ( ) ) <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , length , <EOL> state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if tagSet [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == tag . tagFormatSimple : <EOL> if not substrate : <EOL> raise error . PyAsn1Error ( '<STR_LIT>' ) <EOL> trailingBits = ord ( substrate [ <NUM_LIT:0> ] ) <EOL> if trailingBits > <NUM_LIT:7> : <EOL> raise error . PyAsn1Error ( <EOL> '<STR_LIT>' % trailingBits <EOL> ) <EOL> substrate = substrate [ <NUM_LIT:1> : ] <EOL> lsb = p = <NUM_LIT:0> ; l = len ( substrate ) - <NUM_LIT:1> ; b = [ ] <EOL> while p <= l : <EOL> if p == l : <EOL> lsb = trailingBits <EOL> j = <NUM_LIT:7> <EOL> o = ord ( substrate [ p ] ) <EOL> while j >= lsb : <EOL> b . append ( ( o >> j ) & <NUM_LIT> ) <EOL> j = j - <NUM_LIT:1> <EOL> p = p + <NUM_LIT:1> <EOL> return r . clone ( tuple ( b ) ) , '<STR_LIT>' <EOL> if r : r = r . clone ( value = ( ) ) <EOL> if not decodeFun : <EOL> return r , substrate <EOL> while substrate : <EOL> component , substrate = decodeFun ( substrate ) <EOL> r = r + component <EOL> return r , substrate <EOL> def indefLenValueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if r : r = r . clone ( value = '<STR_LIT>' ) <EOL> if not decodeFun : <EOL> return r , substrate <EOL> while substrate : <EOL> component , substrate = decodeFun ( substrate ) <EOL> if component == eoo . endOfOctets : <EOL> break <EOL> r = r + component <EOL> else : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return r , substrate <EOL> class OctetStringDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . OctetString ( '<STR_LIT>' ) <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , length , <EOL> state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if tagSet [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == tag . tagFormatSimple : <EOL> return r . clone ( str ( substrate ) ) , '<STR_LIT>' <EOL> if r : r = r . clone ( value = '<STR_LIT>' ) <EOL> if not decodeFun : <EOL> return r , substrate <EOL> while substrate : <EOL> component , substrate = decodeFun ( substrate ) <EOL> r = r + component <EOL> return r , substrate <EOL> def indefLenValueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if r : r = r . clone ( value = '<STR_LIT>' ) <EOL> if not decodeFun : <EOL> return r , substrate <EOL> while substrate : <EOL> component , substrate = decodeFun ( substrate ) <EOL> if component == eoo . endOfOctets : <EOL> break <EOL> r = r + component <EOL> else : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return r , substrate <EOL> class NullDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . Null ( '<STR_LIT>' ) <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if substrate : <EOL> raise error . PyAsn1Error ( '<STR_LIT>' ) <EOL> return r , substrate <EOL> class ObjectIdentifierDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . ObjectIdentifier ( ( ) ) <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , length , <EOL> state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if not substrate : <EOL> raise error . PyAsn1Error ( '<STR_LIT>' ) <EOL> oid = [ ] ; index = <NUM_LIT:0> <EOL> subId = ord ( substrate [ index ] ) <EOL> oid . append ( int ( subId / <NUM_LIT> ) ) <EOL> oid . append ( int ( subId % <NUM_LIT> ) ) <EOL> index = index + <NUM_LIT:1> <EOL> substrateLen = len ( substrate ) <EOL> while index < substrateLen : <EOL> subId = ord ( substrate [ index ] ) <EOL> if subId < <NUM_LIT> : <EOL> oid . append ( subId ) <EOL> index = index + <NUM_LIT:1> <EOL> else : <EOL> nextSubId = subId <EOL> subId = <NUM_LIT:0> <EOL> while nextSubId >= <NUM_LIT> and index < substrateLen : <EOL> subId = ( subId << <NUM_LIT:7> ) + ( nextSubId & <NUM_LIT> ) <EOL> index = index + <NUM_LIT:1> <EOL> nextSubId = ord ( substrate [ index ] ) <EOL> if index == substrateLen : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' % oid <EOL> ) <EOL> subId = ( subId << <NUM_LIT:7> ) + nextSubId <EOL> oid . append ( subId ) <EOL> index = index + <NUM_LIT:1> <EOL> return r . clone ( tuple ( oid ) ) , substrate [ index : ] <EOL> class SequenceDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . Sequence ( ) <EOL> def _getAsn1SpecByPosition ( self , t , idx ) : <EOL> if t . getComponentType ( ) is not None : <EOL> if hasattr ( t , '<STR_LIT>' ) : <EOL> return t . getComponentTypeMapNearPosition ( idx ) <EOL> elif hasattr ( t , '<STR_LIT>' ) : <EOL> return t . getComponentType ( ) <EOL> def _getPositionByType ( self , t , c , idx ) : <EOL> if t . getComponentType ( ) is not None : <EOL> if hasattr ( t , '<STR_LIT>' ) : <EOL> effectiveTagSet = getattr ( <EOL> c , '<STR_LIT>' , c . getTagSet <EOL> ) ( ) <EOL> return t . getComponentPositionNearType ( effectiveTagSet , idx ) <EOL> return idx <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> idx = <NUM_LIT:0> <EOL> if not decodeFun : <EOL> return r , substrate <EOL> while substrate : <EOL> asn1Spec = self . _getAsn1SpecByPosition ( r , idx ) <EOL> component , substrate = decodeFun ( <EOL> substrate , asn1Spec <EOL> ) <EOL> idx = self . _getPositionByType ( r , component , idx ) <EOL> r . setComponentByPosition ( idx , component ) <EOL> idx = idx + <NUM_LIT:1> <EOL> if hasattr ( r , '<STR_LIT>' ) : <EOL> r . setDefaultComponents ( ) <EOL> r . verifySizeSpec ( ) <EOL> return r , substrate <EOL> def indefLenValueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> idx = <NUM_LIT:0> <EOL> while substrate : <EOL> try : <EOL> asn1Spec = self . _getAsn1SpecByPosition ( r , idx ) <EOL> except error . PyAsn1Error : <EOL> asn1Spec = None <EOL> if not decodeFun : <EOL> return r , substrate <EOL> component , substrate = decodeFun ( substrate , asn1Spec ) <EOL> if component == eoo . endOfOctets : <EOL> break <EOL> idx = self . _getPositionByType ( r , component , idx ) <EOL> r . setComponentByPosition ( idx , component ) <EOL> idx = idx + <NUM_LIT:1> <EOL> else : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if hasattr ( r , '<STR_LIT>' ) : <EOL> r . setDefaultComponents ( ) <EOL> r . verifySizeSpec ( ) <EOL> return r , substrate <EOL> class SetDecoder ( SequenceDecoder ) : <EOL> protoComponent = univ . Set ( ) <EOL> def _getAsn1SpecByPosition ( self , t , idx ) : <EOL> if t . getComponentType ( ) is not None : <EOL> if hasattr ( t , '<STR_LIT>' ) : <EOL> return t . getComponentTypeMap ( ) <EOL> def _getPositionByType ( self , t , c , idx ) : <EOL> if t . getComponentType ( ) is not None : <EOL> if hasattr ( t , '<STR_LIT>' ) and t . getComponentType ( ) : <EOL> effectiveTagSet = getattr ( <EOL> c , '<STR_LIT>' , c . getTagSet <EOL> ) ( ) <EOL> return t . getComponentPositionByType ( effectiveTagSet ) <EOL> return idx <EOL> class ChoiceDecoder ( AbstractDecoder ) : <EOL> protoComponent = univ . Choice ( ) <EOL> def valueDecoder ( self , substrate , asn1Spec , tagSet , <EOL> length , state , decodeFun ) : <EOL> r = self . _createComponent ( tagSet , asn1Spec ) <EOL> if not decodeFun : <EOL> return r , substrate <EOL> if r . getTagSet ( ) == tagSet : <EOL> component , substrate = decodeFun ( <EOL> substrate , r . getComponentTypeMap ( ) <EOL> ) <EOL> else : <EOL> component , substrate = decodeFun ( <EOL> substrate , r . getComponentTypeMap ( ) , tagSet , length , state <EOL> ) <EOL> effectiveTagSet = getattr ( <EOL> component , '<STR_LIT>' , component . getTagSet <EOL> ) ( ) <EOL> r . setComponentByType ( effectiveTagSet , component ) <EOL> return r , substrate <EOL> indefLenValueDecoder = valueDecoder <EOL> class UTF8StringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . UTF8String ( ) <EOL> class NumericStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . NumericString ( ) <EOL> class PrintableStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . PrintableString ( ) <EOL> class TeletexStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . TeletexString ( ) <EOL> class VideotexStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . VideotexString ( ) <EOL> class IA5StringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . IA5String ( ) <EOL> class GraphicStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . GraphicString ( ) <EOL> class VisibleStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . VisibleString ( ) <EOL> class GeneralStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . GeneralString ( ) <EOL> class UniversalStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . UniversalString ( ) <EOL> class BMPStringDecoder ( OctetStringDecoder ) : <EOL> protoComponent = char . BMPString ( ) <EOL> class GeneralizedTimeDecoder ( OctetStringDecoder ) : <EOL> protoComponent = useful . GeneralizedTime ( ) <EOL> class UTCTimeDecoder ( OctetStringDecoder ) : <EOL> protoComponent = useful . UTCTime ( ) <EOL> codecMap = { <EOL> eoo . endOfOctets . tagSet : EndOfOctetsDecoder ( ) , <EOL> univ . Integer . tagSet : IntegerDecoder ( ) , <EOL> univ . Boolean . tagSet : BooleanDecoder ( ) , <EOL> univ . BitString . tagSet : BitStringDecoder ( ) , <EOL> univ . OctetString . tagSet : OctetStringDecoder ( ) , <EOL> univ . Null . tagSet : NullDecoder ( ) , <EOL> univ . ObjectIdentifier . tagSet : ObjectIdentifierDecoder ( ) , <EOL> univ . Enumerated . tagSet : IntegerDecoder ( ) , <EOL> univ . Sequence . tagSet : SequenceDecoder ( ) , <EOL> univ . Set . tagSet : SetDecoder ( ) , <EOL> univ . Choice . tagSet : ChoiceDecoder ( ) , <EOL> char . UTF8String . tagSet : UTF8StringDecoder ( ) , <EOL> char . NumericString . tagSet : NumericStringDecoder ( ) , <EOL> char . PrintableString . tagSet : PrintableStringDecoder ( ) , <EOL> char . TeletexString . tagSet : TeletexStringDecoder ( ) , <EOL> char . VideotexString . tagSet : VideotexStringDecoder ( ) , <EOL> char . IA5String . tagSet : IA5StringDecoder ( ) , <EOL> char . GraphicString . tagSet : GraphicStringDecoder ( ) , <EOL> char . VisibleString . tagSet : VisibleStringDecoder ( ) , <EOL> char . GeneralString . tagSet : GeneralStringDecoder ( ) , <EOL> char . UniversalString . tagSet : UniversalStringDecoder ( ) , <EOL> char . BMPString . tagSet : BMPStringDecoder ( ) , <EOL> useful . GeneralizedTime . tagSet : GeneralizedTimeDecoder ( ) , <EOL> useful . UTCTime . tagSet : UTCTimeDecoder ( ) <EOL> } <EOL> ( stDecodeTag , stDecodeLength , stGetValueDecoder , stGetValueDecoderByAsn1Spec , <EOL> stGetValueDecoderByTag , stTryAsExplicitTag , stDecodeValue , stDumpRawValue , <EOL> stErrorCondition , stStop ) = range ( <NUM_LIT:10> ) <EOL> class Decoder : <EOL> defaultErrorState = stErrorCondition <EOL> defaultRawDecoder = OctetStringDecoder ( ) <EOL> def __init__ ( self , codecMap ) : <EOL> self . __codecMap = codecMap <EOL> def __call__ ( self , substrate , asn1Spec = None , tagSet = None , <EOL> length = None , state = stDecodeTag , recursiveFlag = <NUM_LIT:1> ) : <EOL> while state != stStop : <EOL> if state == stDecodeTag : <EOL> if not substrate : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> t = ord ( substrate [ <NUM_LIT:0> ] ) <EOL> tagClass = t & <NUM_LIT> <EOL> tagFormat = t & <NUM_LIT> <EOL> tagId = t & <NUM_LIT> <EOL> substrate = substrate [ <NUM_LIT:1> : ] <EOL> if tagId == <NUM_LIT> : <EOL> tagId = <NUM_LIT:0> L <EOL> while <NUM_LIT:1> : <EOL> if not substrate : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> t = ord ( substrate [ <NUM_LIT:0> ] ) <EOL> tagId = tagId << <NUM_LIT:7> | ( t & <NUM_LIT> ) <EOL> substrate = substrate [ <NUM_LIT:1> : ] <EOL> if not t & <NUM_LIT> : <EOL> break <EOL> lastTag = tag . Tag ( <EOL> tagClass = tagClass , tagFormat = tagFormat , tagId = tagId <EOL> ) <EOL> if tagSet is None : <EOL> tagSet = tag . TagSet ( ( ) , lastTag ) <EOL> else : <EOL> tagSet = lastTag + tagSet <EOL> state = stDecodeLength <EOL> if state == stDecodeLength : <EOL> if not substrate : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> firstOctet = ord ( substrate [ <NUM_LIT:0> ] ) <EOL> if firstOctet == <NUM_LIT> : <EOL> size = <NUM_LIT:1> <EOL> length = - <NUM_LIT:1> <EOL> elif firstOctet < <NUM_LIT> : <EOL> length , size = firstOctet , <NUM_LIT:1> <EOL> else : <EOL> size = firstOctet & <NUM_LIT> <EOL> length = <NUM_LIT:0> <EOL> lengthString = substrate [ <NUM_LIT:1> : size + <NUM_LIT:1> ] <EOL> if len ( lengthString ) != size : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' % <EOL> ( size , len ( lengthString ) , tagSet ) <EOL> ) <EOL> for char in lengthString : <EOL> length = ( length << <NUM_LIT:8> ) | ord ( char ) <EOL> size = size + <NUM_LIT:1> <EOL> state = stGetValueDecoder <EOL> substrate = substrate [ size : ] <EOL> if length != - <NUM_LIT:1> and len ( substrate ) < length : <EOL> raise error . SubstrateUnderrunError ( <EOL> '<STR_LIT>' % ( length - len ( substrate ) ) <EOL> ) <EOL> if state == stGetValueDecoder : <EOL> if asn1Spec is None : <EOL> state = stGetValueDecoderByTag <EOL> else : <EOL> state = stGetValueDecoderByAsn1Spec <EOL> if state == stGetValueDecoderByTag : <EOL> concreteDecoder = self . __codecMap . get ( tagSet ) <EOL> if concreteDecoder : <EOL> state = stDecodeValue <EOL> else : <EOL> concreteDecoder = self . __codecMap . get ( tagSet [ : <NUM_LIT:1> ] ) <EOL> if concreteDecoder : <EOL> state = stDecodeValue <EOL> else : <EOL> state = stTryAsExplicitTag <EOL> if state == stGetValueDecoderByAsn1Spec : <EOL> if tagSet == eoo . endOfOctets . getTagSet ( ) : <EOL> concreteDecoder = self . __codecMap [ tagSet ] <EOL> state = stDecodeValue <EOL> continue <EOL> if type ( asn1Spec ) == types . DictType : <EOL> __chosenSpec = asn1Spec . get ( tagSet ) <EOL> elif asn1Spec is not None : <EOL> __chosenSpec = asn1Spec <EOL> else : <EOL> __chosenSpec = None <EOL> if __chosenSpec is None or not __chosenSpec . getTypeMap ( ) . has_key ( tagSet ) : <EOL> state = stTryAsExplicitTag <EOL> else : <EOL> baseTag = __chosenSpec . getTagSet ( ) . getBaseTag ( ) <EOL> if baseTag : <EOL> baseTagSet = tag . TagSet ( baseTag , baseTag ) <EOL> else : <EOL> baseTagSet = tag . TagSet ( ) <EOL> concreteDecoder = self . __codecMap . get ( <EOL> baseTagSet <EOL> ) <EOL> if concreteDecoder : <EOL> asn1Spec = __chosenSpec <EOL> state = stDecodeValue <EOL> else : <EOL> state = stTryAsExplicitTag <EOL> if state == stTryAsExplicitTag : <EOL> if tagSet and tagSet [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == tag . tagFormatConstructed and tagSet [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] != tag . tagClassUniversal : <EOL> state = stDecodeTag <EOL> else : <EOL> state = self . defaultErrorState <EOL> if state == stDecodeValue : <EOL> if recursiveFlag : <EOL> decodeFun = self <EOL> else : <EOL> decodeFun = None <EOL> if length == - <NUM_LIT:1> : <EOL> value , substrate = concreteDecoder . indefLenValueDecoder ( <EOL> substrate , asn1Spec , tagSet , length , <EOL> stGetValueDecoder , decodeFun <EOL> ) <EOL> else : <EOL> value , _substrate = concreteDecoder . valueDecoder ( <EOL> substrate [ : length ] , asn1Spec , tagSet , <EOL> length , stGetValueDecoder , decodeFun <EOL> ) <EOL> if recursiveFlag : <EOL> substrate = substrate [ length : ] <EOL> else : <EOL> substrate = _substrate <EOL> state = stStop <EOL> if state == stDumpRawValue : <EOL> concreteDecoder = self . defaultRawDecoder <EOL> state = stDecodeValue <EOL> if state == stErrorCondition : <EOL> raise error . PyAsn1Error ( <EOL> '<STR_LIT>' % ( tagSet , asn1Spec ) <EOL> ) <EOL> return value , substrate <EOL> decode = Decoder ( codecMap ) </s>
