o
    vMf5                    @   sj  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ g dZej e!ej"dZ#ej$Z$ej%Z%G dd de&Z'G dd de'Z(G dd de(Z)e) Z*G dd de(Z+G dd de+Z,G dd  d e(Z-G d!d" d"e(Z.G d#d$ d$e(Z/G d%d& d&e(Z0G d'd( d(e(Z1G d)d* d*e(Z2G d+d, d,e'Z3G d-d. d.e3Z4G d/d0 d0e4Z5G d1d2 d2e5Z6G d3d4 d4e5Z7G d5d6 d6e4Z8G d7d8 d8e8Z9G d9d: d:e8Z:G d;d< d<e4Z;G d=d> d>e(Z<G d?d@ d@e.Z=G dAdB dBe.Z>G dCdD dDe.Z?G dEdF dFe.Z@G dGdH dHe.ZAG dIdJ dJe.ZBG dKdL dLe.ZCG dMdN dNe.ZDG dOdP dPe.ZEG dQdR dRe.ZFG dSdT dTe.ZGG dUdV dVe.ZHG dWdX dXe.ZIG dYdZ dZe.ZJi ejKjLe+ ejMjLe, ejNjLe- ejOjLe. ejPjLe/ ejQjLe0 ejRjLe1 ejSjLe+ ejTjLe2 ejUjLe5 ejVjLe8 ejWjLe; ejXjLe= ejYjLe> ejZjLe? ej[jLe@ ej\jLeA ej]jLeB ej^jLeC ej_jLeD ej`jLeE ejajLeF ejbjLeG ejcjLeH ejdjLeI ejejLeJ i	ZfejVjge9 ejhjge: ejUjge6 ejijge7 ejWjge; ejjjge< iZkefZlekZmefn D ]ZoeojpdureojpjqjgZgegduregekvreoekeg< qdd[d\ erd]D \
ZsZtZuZvZwZxZyZzZ{Z|ed^Z}G d_d` d`e&Z~G dadb dbe&ZG dcdd dde&Ze ZdS )e    N)debug)error)eoo)asSeekableStream)isEndOfStream)peekIntoStreamreadFromStream)_MISSING)
from_bytes)oct2int	octs2ints	ints2octsnull)PyAsn1Error)base)char)tag)tagmap)univ)useful)StreamingDecoderDecoderdecode)flagsc                   @   s8   e Zd ZdZ		dddZ		dddZedd ZdS )	AbstractPayloadDecoderNc           	      K      t d|f )ztDecode value with fixed byte length.

        The decoder is allowed to consume as many bytes as necessary.
        z(SingleItemDecoder not implemented for %sr   r   	self	substrateasn1SpectagSetlengthstate	decodeFunsubstrateFunoptions r(   Z/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/pyasn1/codec/ber/decoder.pyvalueDecoder*      z#AbstractPayloadDecoder.valueDecoderc           	      K   r   )zsDecode value with undefined length.

        The decoder is allowed to consume as many bytes as necessary.
        z5Indefinite length mode decoder not implemented for %sr   r   r(   r(   r)   indefLenValueDecoder4   r+   z+AbstractPayloadDecoder.indefLenValueDecoderc                 C   s   d|vr| |d< |S )N
asn1Objectr(   )r-   r'   r(   r(   r)   _passAsn1Object>   s   z&AbstractPayloadDecoder._passAsn1ObjectNNNNN)__name__
__module____qualname__protoComponentr*   r,   staticmethodr.   r(   r(   r(   r)   r   '   s    


r   c                   @   s    e Zd Zedd Zdd ZdS )AbstractSimplePayloadDecoderc                 c   s    t |||D ]}|V  qd S Nr   )r-   r    r#   r'   chunkr(   r(   r)   substrateCollectorG   s   z/AbstractSimplePayloadDecoder.substrateCollectorc                 K   s<   | dr|S |d u r| jj||dS |tu r|S ||S )Nnativer"   )getr3   clonenoValuer   r!   r"   valuer'   r(   r(   r)   _createComponentL   s   

z-AbstractSimplePayloadDecoder._createComponentN)r0   r1   r2   r4   r8   r@   r(   r(   r(   r)   r5   F   s    
r5   c                   @   s2   e Zd ZedZ		dddZ		dddZdS )RawPayloadDecoder Nc                 k   s`    |r| j ||dfi |}	||	|||D ]}
|
V  qd S |||||fi |D ]}|V  q(d S )NrB   )r@   r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   r?   r(   r(   r)   r*   Z   s   zRawPayloadDecoder.valueDecoderc                 k   st    |r| j ||dfi |}	||	|||D ]}
|
V  qd S 	 |||||fddi|D ]}|tju r5 d S |V  q+q)NrB   TallowEoo)r@   r   endOfOctetsrC   r(   r(   r)   r,   i   s$   

z&RawPayloadDecoder.indefLenValueDecoderr/   r0   r1   r2   r   Anyr3   r*   r,   r(   r(   r(   r)   rA   W   s    

rA   c                   @   $   e Zd ZedZ		dddZdS )IntegerPayloadDecoderr   Nc                 k   sp    |d j tjkrtdt|||D ]
}	t|	tr|	V  q|	r(t|	dd}
nd}
| j	|||
fi |V  d S )Nr   Simple tag format expectedT)signed)
	tagFormatr   tagFormatSimpler   r   r	   
isinstanceSubstrateUnderrunErrorr   r@   )r   r    r!   r"   r#   r$   r%   r&   r'   r7   r?   r(   r(   r)   r*      s   

z"IntegerPayloadDecoder.valueDecoderr/   )r0   r1   r2   r   Integerr3   r*   r(   r(   r(   r)   rI      
    
rI   c                   @   s   e Zd ZedZdd ZdS )BooleanPayloadDecoderr   c                 K   s    t j| |||r	dp
dfi |S )N   r   )rI   r@   r>   r(   r(   r)   r@      s
   z&BooleanPayloadDecoder._createComponentN)r0   r1   r2   r   Booleanr3   r@   r(   r(   r(   r)   rR      s    
rR   c                   @   6   e Zd ZedZdZ		dddZ		dddZdS )	BitStringPayloadDecoderr(   TNc                 k   s   |r| j ||tfi |}	||	|||D ]}
|
V  qd S |s$tdt|D ]
}
t|
tr2|
V  q(|
r:td|d jtj	krt
|d|D ]
}t|trR|V  qHt|}|dkrbtd| t
||d |D ]
}
t|
trt|
V  qj| jj|
d|d}| j |||fi |V  d S | jstd| jj trtd	 | j}| jjtdd
}| }| | |k r||| jfd|i|D ]
}t|tr|V  qt|d }|dkrtd| | jj|dd  d||d}| | |k s| j |||fi |V  d S )NzEmpty BIT STRING substrater   rS      Trailing bits overflow %sT)internalFormatpadding*Constructed encoding form prohibited at %s$assembling constructed serializationrY   r&   rY   prependrZ   )r@   r=   r   r   r   rN   rO   rL   r   rM   r	   ordr3   fromOctetStringsupportConstructedForm	__class__r0   LOGr8   r   tellr   )r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   trailingBitsr?   	bitStringcurrent_position	componentr(   r(   r)   r*      s   






z$BitStringPayloadDecoder.valueDecoderc                 k   s    |r| j ||tfi |}	||	|||D ]}
|
V  qd S | j}| jjtdd}	 ||| jf|dd|D ]}|tju r? n	t|t	rG|V  q6|tju rNn t
|d }|dkr_td| | jj|dd  d||d}q)| j |||fi |V  d S )	NTr]   r&   rD   r   rW   rX   rS   r^   )r@   r=   r8   r3   ra   r   r   rE   rN   rO   r   r   r   )r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   rg   ri   rf   r(   r(   r)   r,      sF   



z,BitStringPayloadDecoder.indefLenValueDecoderr/   )	r0   r1   r2   r   	BitStringr3   rb   r*   r,   r(   r(   r(   r)   rV      s    

QrV   c                   @   rU   )	OctetStringPayloadDecoderrB   TNc                 k   s0   |r| j ||tfi |}	||	|||D ]}
|
V  qd S |d jtjkrDt|||D ]
}
t|
tr5|
V  q+| j |||
fi |V  d S | jsPt	
d| jj trVtd | j}t}| }| | |k r||| jfd|i|D ]
}t|tr}|V  qs||7 }| | |k sg| j |||fi |V  d S )Nr   r[   r\   r&   )r@   r=   rL   r   rM   r	   rN   rO   rb   r   r   rc   r0   rd   r8   r   re   r3   )r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   headeroriginal_positionri   r(   r(   r)   r*   *  sD   


	z&OctetStringPayloadDecoder.valueDecoderc                 k   s    |r"|| j ur"| j||tfi |}	||	|||D ]}
|
V  qd S | j }t}	 ||| jf|dd|D ]}t|tr?|V  |tju rF nq5|tju rMn||7 }q(| j|||fi |V  d S )NTrj   )	r8   r@   r=   r   r3   rN   rO   r   rE   )r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   rm   ri   r(   r(   r)   r,   W  s6   



z.OctetStringPayloadDecoder.indefLenValueDecoderr/   )	r0   r1   r2   r   OctetStringr3   rb   r*   r,   r(   r(   r(   r)   rl   &  s    

.rl   c                   @   rH   )NullPayloadDecoderrB   Nc                 k   sp    |d j tjkrtdt|||D ]
}	t|	tr|	V  q| j||dfi |}
|	r3td| |
V  d S )Nr   rJ   rB   z&Unexpected %d-octet substrate for Null)	rL   r   rM   r   r   r	   rN   rO   r@   )r   r    r!   r"   r#   r$   r%   r&   r'   r7   ri   r(   r(   r)   r*     s   


zNullPayloadDecoder.valueDecoderr/   )r0   r1   r2   r   Nullr3   r*   r(   r(   r(   r)   rp   |  rQ   rp   c                   @   rH   )ObjectIdentifierPayloadDecoderr(   Nc                 k   s   |d j tjkrtdt|||D ]
}	t|	tr|	V  q|	s&tdt|	}	d}
d}t	|	}||k r|	| }|d7 }|dk rH|
|f7 }
n?|dkr~|}d}|dkrt|d> |d@  }||krhtd	|
f |	| }|d7 }|dksT|
|d> | f7 }
n	|dkrtd
||k s6d|
d   krdkrn nd|
 }
n;d|
d   krdkrn nd|
d d f|
dd   }
n|
d dkrd|
d d f|
dd   }
n	td|	d  | j
|||
fi |V  d S )Nr   rJ   Empty substrater(   rS      rW      #Short substrate for sub-OID past %sz"Invalid octet 0x80 in OID encoding'   )r   (   O   P      zMalformed first OID octet: %srL   r   rM   r   r   r	   rN   rO   r   lenr@   )r   r    r!   r"   r#   r$   r%   r&   r'   r7   oidindexsubstrateLensubId	nextSubIdr(   r(   r)   r*     sT   




z+ObjectIdentifierPayloadDecoder.valueDecoderr/   )r0   r1   r2   r   ObjectIdentifierr3   r*   r(   r(   r(   r)   rr     rQ   rr   c                   @   rH   )RelativeOIDPayloadDecoderr(   Nc                 k   s2   |d j tjkrtdt|||D ]
}	t|	tr|	V  q|	s&tdt|	}	d}
d}t	|	}||k r|	| }|d7 }|dk rH|
|f7 }
n?|dkr~|}d}|dkrt|d> |d@  }||krhtd	|
f |	| }|d7 }|dksT|
|d> | f7 }
n	|dkrtd
||k s6| j
|||
fi |V  d S )Nr   rJ   rs   r(   rS   rt   rW   ru   rv   z+Invalid octet 0x80 in RELATIVE-OID encodingr|   )r   r    r!   r"   r#   r$   r%   r&   r'   r7   reloidr   r   r   r   r(   r(   r)   r*     sF   



z&RelativeOIDPayloadDecoder.valueDecoderr/   )r0   r1   r2   r   RelativeOIDr3   r*   r(   r(   r(   r)   r     rQ   r   c                   @   s"   e Zd Ze Z		dddZdS )RealPayloadDecoderNc                 k   s   |d j tjkrtdt|||D ]
}	t|	tr|	V  q|	s/| j||dfi |V  d S t	|	d }
|	dd  }	|
d@ r|	sFtdt
rLt
d |
d@ d }|d	krbt	|	d }|	dd  }	|	d | |	|d  }}	|rs|	sxtd
t	|d d@ rdpd}|r|dK }|t	|d O }|dd  }|s|
d	? d@ }|dkrtd|dkr|d9 }n|dkr|d	9 }d}|	r|dK }|t	|	d O }|	dd  }	|	s|
d@ r| }|
d? d@ }|d| 9 }|d|f}nn|
d@ rt
rt
d |
d@ rdpd}n[|
d@ dkrR|	stdt
rt
d z0|
d@ dkr$t|	ddf}n|
d@ dkr0t|	}n|
d@ dkr<t|	}ntd|
 W n tyQ   tdw td|
 | j|||fi |V  d S )Nr   rJ   g        rS   rt   zIncomplete floating-point valuezdecoding binary encoded REAL      zReal exponent screwed   r{   zIllegal Real base@   zdecoding infinite REALz-infinf   zdecoding character encoded REAL
   zUnknown NR (tag %s)zBad character Real syntaxzUnknown encoding (tag %s))rL   r   rM   r   r   r	   rN   rO   r@   r   rd   intfloat
ValueError)r   r    r!   r"   r#   r$   r%   r&   r'   r7   foneoebpsfr?   r(   r(   r)   r*     s   








zRealPayloadDecoder.valueDecoderr/   )r0   r1   r2   r   Realr3   r*   r(   r(   r(   r)   r     s
    r   c                   @   s   e Zd ZdZdS )!AbstractConstructedPayloadDecoderN)r0   r1   r2   r3   r(   r(   r(   r)   r   o  s    r   c                   @   sN   e Zd ZdZdZdd Zdd Z		dddZ		ddd	Z		dd
dZ	dS )ConstructedPayloadDecoderBaseNc                 C      t  r6   NotImplementedError)r   r-   idxr(   r(   r)   _getComponentTagMapw     z1ConstructedPayloadDecoderBase._getComponentTagMapc                 C   r   r6   r   )r   r-   r"   r   r(   r(   r)   _getComponentPositionByTypez  r   z9ConstructedPayloadDecoderBase._getComponentPositionByTypec                 k   s   d }g }t  }| }	|dks| |	| k ri||fi |D ]
}
t|
tr*|
V  q |dkr5|
tju r5n4||
 ||
j t	|dkrJ| j
}n| j}|jtj|jjg|jR  d}|dks| |	| k strqtd|  t|D ]\}}
|j||
dddd qu|V  d S )Nr   rS   r:   z@guessed %r container type (pass `asn1Spec` to guide the decoder)FverifyConstraints	matchTagsmatchConstraints)setre   rN   rO   r   rE   appendaddr"   r}   protoRecordComponentprotoSequenceComponentr<   r   TagSetbaseTag	superTagsrd   	enumeratesetComponentByPosition)r   r    r"   r%   r#   r'   r-   
componentscomponentTypesrn   ri   r3   r   r(   r(   r)   _decodeComponentsSchemaless}  s@   


z9ConstructedPayloadDecoderBase._decodeComponentsSchemalessc                 k   s   |d j tjkrtd| }	|r?|d ur| }
n| jd ur*| jj|d}
n| j| j	f}
||
|||D ]}|V  q7d S |d u r| j
|f|||d|D ]
}
t|
trZ|
V  qP| |	| k rtrt||dD ]
}t|tru|V  qktdt|t|f  |
V  d S | }
|
  | |
|}|jtjjtjjfv r|j}|jtjjk}| o|j }trtd| rdpd	|rd
pd	|f  t }d}| |	 |k rd|sd }n6|r|j}n0z|r|| j}n|| js|| jr||}n|| j}W n t y   td|f w |||fi |D ]}t|tr$|V  q|sI|rI|r6|!|j"}n|| jsB|| jrI|#|j"|}|
j$||dddd |%| |d7 }| |	 |k strmtd|  |r|j&'|std|
j(j) |j*r|+di }trtd |, D ]\}}td||f  q|s|+ddrt-|j.D ]\}}|j/sq|jr|
0|j1sɐq|
2|j/j3}z|| }W nO t4y%   trtd|
j(j)|j3|
j(j)|j/j3f  |j/, D ]\}}td||f  qz|j/| }W n t4y"   trtd|f  Y Y qw Y nw tr1td||f  |
0|}|jtj5jtj6jfv rqt-|D ](\}}t7|| 8 }||fd|i|D ]}t|trg|V  q\|||< qFqt7|
0|8 }||fd|i|D ]}t|tr|V  q|
$|| qnM|
j9}|r|nD|j}trtd|  d}| |	 |k r|||fi |D ]}t|tr|V  q|
j$||dddd |d7 }| |	 |k s|
V  d S )Nr   Constructed tag format expectedr:   r"   r%   r#   )contextz)Unused trailing %d octets encountered: %s5decoding %sdeterministic %s type %r chosen by type IDnon-rB   SET"Excessive components decoded at %rFr   rS   seen component indices %s,ASN.1 object %s has uninitialized components	openTypesuser-specified open types map:%s -> %rdecodeOpenTypesJdefault open types map of component "%s.%s" governed by component "%s.%s":1failed to resolve open type by governing value %r+resolved open type %r by governing value %rr!   +decoding type %r chosen by given `asn1Spec`):rL   r   tagFormatConstructedr   r   re   r<   r3   r   r   r   rN   rO   rd   r	   r}   r   hexdumpclearr.   typeIdr   SequenceSetcomponentTypehasOptionalOrDefaultr   tagMapUniquer-   
isOptionalisDefaultedgetTagMapNearPosition
IndexErrorgetPositionByTypeeffectiveTagSetgetPositionNearTyper   r   requiredComponentsissubsetrc   r0   hasOpenTypesr;   itemsr   
namedTypesopenTypegetComponentByPositionisValuegetComponentByNamenameKeyErrorSetOf
SequenceOfr   asOctetsisInconsistent)r   r    r!   r"   r#   r$   r%   r&   r'   rn   r-   r7   trailingr   	isSetTypeisDeterministicseenIndicesr   r   ri   r   kv	namedTypegoverningValuer   containerValueposcontainerElementstreaminconsistencyr(   r(   r)   r*     sd  







+

z*ConstructedPayloadDecoderBase.valueDecoderc                 k   s   |d j tjkrtd|d ur=|d ur| }	n| jd ur(| jj|d}	n| j| jf}	||	|||D ]}
|
V  q5d S |d u rb| j	|f|||dt
|ddD ]
}	t|	tr\|	V  qR|	V  d S | }	|	  | |	|}|jtjjtjjfv r|	j}|	jtjjk}| o|j }trtd| rdpd	|rd
pd	|f  t }d}	 t||krd }n5|r|j}n/z|r|| j}n|| js|| jr||}n|| j}W n ty   td|	f w |||fddi|D ]}t|tr|V  |tju r nq|tju r
n7|s-|r-|r| |j!}n|| js&|| jr-|"|j!|}|	j#||dddd |$| |d7 }qtrJtd|  |r|j%&|s]td|	j'j( |j)r|*di }trtd |+ D ]\}}td||f  qr|s|*ddrt,|j-D ]\}}|j.sq|jr|	/|j0sq|	1|j.j2}z|| }W nO t3y   trtd|	j'j(|j2|	j'j(|j.j2f  |j.+ D ]\}}td||f  qz|j.| }W n t3y   trtd|f  Y Y qw Y nw trtd||f  |	/|}|jtj4jtj5jfv rZt,|D ]4\}}t6|| 7 }||fd|it
|ddD ]}t|trH|V  |tju rP nq=|||< q#qt6|	/|7 }||fd|it
|ddD ]}t|tr||V  |tju r n|	#|| qqqnO|	j8}|r|nF|j}trtd|  d}	 |||fddi|D ]}t|tr|V  |tju r nq|tju rn|	j#||dddd |d7 }q|	V  d S )Nr   r   r:   r   TrD   r   r   rB   r   r   rD   Fr   rS   r   r   r   r   r   r   r   r   r   r!   r   )9rL   r   r   r   r   r<   r3   r   r   r   dictrN   rO   r   r.   r   r   r   r   r   r   rd   r   r}   r   r-   r   r   r   r   r   rE   r   r   r   r   r   r   r   rc   r0   r   r;   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   r   r   r   r   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r)   r,     sz  








2






z2ConstructedPayloadDecoderBase.indefLenValueDecoder)NNNr/   )
r0   r1   r2   r   r   r   r   r   r*   r,   r(   r(   r(   r)   r   s  s    
4
 ^r   c                   @      e Zd Ze Ze ZdS )"SequenceOrSequenceOfPayloadDecoderN)r0   r1   r2   r   r   r   r   r   r(   r(   r(   r)   r   w      r   c                   @      e Zd Ze ZdS )SequencePayloadDecoderN)r0   r1   r2   r   r   r3   r(   r(   r(   r)   r   |      r   c                   @   r   )SequenceOfPayloadDecoderN)r0   r1   r2   r   r   r3   r(   r(   r(   r)   r     r   r   c                   @   r   )SetOrSetOfPayloadDecoderN)r0   r1   r2   r   r   r   r   r   r(   r(   r(   r)   r     r   r   c                   @   r   )SetPayloadDecoderN)r0   r1   r2   r   r   r3   r(   r(   r(   r)   r     r   r   c                   @   r   )SetOfPayloadDecoderN)r0   r1   r2   r   r   r3   r(   r(   r(   r)   r     r   r   c                   @   0   e Zd Ze Z		dddZ		dddZdS )ChoicePayloadDecoderNc                 k   s   |d u r| j j|d}	n| }	|r"||	|||D ]}
|
V  qd S | |	|}|	j|krLtr6td|f  |||	jfi |D ]
}t|trJ|V  q@n!trUtd|f  |||	j|||fi |D ]
}t|trl|V  qb|j}trztd||f  |	j	||ddddd |	V  d S )Nr:   z'decoding %s as explicitly tagged CHOICEzdecoding %s as untagged CHOICE*decoded component %s, effective tag set %sFr   r   r   	innerFlag)
r3   r<   r.   r"   rd   componentTagMaprN   rO   r   setComponentByType)r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   ri   r   r(   r(   r)   r*     sT   






z!ChoicePayloadDecoder.valueDecoderc              	   k   s<   |d u r| j j|d}	n| }	|r"||	|||D ]}
|
V  qd S | |	|}|	j|k}tr;td||r6dp7df  	 |rN|||	jjfi t|dd}n|||	jj|||fi t|dd}|D ]-}t|t	rl|V  |t
ju rs n|j}trtd||f  |	j||ddddd	 |s nqb|r|t
ju rnq<|	V  d S )
Nr:   zdecoding %s as %stagged CHOICEzexplicitly unTr   r   Fr  )r3   r<   r.   r"   rd   r   r   r   rN   rO   r   rE   r   r  )r   r    r!   r"   r#   r$   r%   r&   r'   r-   r7   isTaggediteratorri   r   r(   r(   r)   r,     sd   





'z)ChoicePayloadDecoder.indefLenValueDecoderr/   )r0   r1   r2   r   Choicer3   r*   r,   r(   r(   r(   r)   r     s    
4r   c                   @   r   )AnyPayloadDecoderNc                 k   s   |d u rd}	n|j tju r||jv}	n||jk}	|	rJ|j}
| }||
tj	 |||
 7 }t
rJt||D ]
}t|tr@|V  q6t
dt|  |rd|| j||tfi ||||D ]}|V  q\d S t|||D ]
}t|trt|V  qj| j|||fi |V  d S )NTz&decoding as untagged ANY, substrate %s)rc   r   TagMaptagMapr"   markedPositionre   seekosSEEK_SETrd   r   rN   rO   r   r   r@   r=   r	   )r   r    r!   r"   r#   r$   r%   r&   r'   
isUntaggedfullPositioncurrentPositionr7   r(   r(   r)   r*     s>   


zAnyPayloadDecoder.valueDecoderc                 k   s   |d u rd}	n|j tju r||jv }	n||jk}	|	r$t}
tr#td n,|j}| }|	|t
j t||| |D ]
}
t|
trD|
V  q:trPtdt|
  | j}|rz|| jurz| j||tfi |}|||
| |t|
 |D ]}
|
V  qrd S trtd | j}	 |||f|dd|D ]}t|tr|V  |tju r nq|tju rn|
|7 }
q|r|
V  d S | j|||
fi |V  d S )NFzdecoding as tagged ANYz-decoding as untagged ANY, header substrate %sr\   Trj   )rc   r   r
  r  r"   r   rd   r  re   r  r  r  r	   rN   rO   r   r   r3   r8   r@   r=   r}   r   rE   )r   r    r!   r"   r#   r$   r%   r&   r'   r  r7   r  r  r-   ri   r(   r(   r)   r,   5  sp   






z&AnyPayloadDecoder.indefLenValueDecoderr/   rF   r(   r(   r(   r)   r	  	  s    
*r	  c                   @   r   )UTF8StringPayloadDecoderN)r0   r1   r2   r   
UTF8Stringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )NumericStringPayloadDecoderN)r0   r1   r2   r   NumericStringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )PrintableStringPayloadDecoderN)r0   r1   r2   r   PrintableStringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )TeletexStringPayloadDecoderN)r0   r1   r2   r   TeletexStringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )VideotexStringPayloadDecoderN)r0   r1   r2   r   VideotexStringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )IA5StringPayloadDecoderN)r0   r1   r2   r   	IA5Stringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )GraphicStringPayloadDecoderN)r0   r1   r2   r   GraphicStringr3   r(   r(   r(   r)   r    r   r  c                   @   r   )VisibleStringPayloadDecoderN)r0   r1   r2   r   VisibleStringr3   r(   r(   r(   r)   r!    r   r!  c                   @   r   )GeneralStringPayloadDecoderN)r0   r1   r2   r   GeneralStringr3   r(   r(   r(   r)   r#    r   r#  c                   @   r   )UniversalStringPayloadDecoderN)r0   r1   r2   r   UniversalStringr3   r(   r(   r(   r)   r%    r   r%  c                   @   r   )BMPStringPayloadDecoderN)r0   r1   r2   r   	BMPStringr3   r(   r(   r(   r)   r'    r   r'  c                   @   r   )ObjectDescriptorPayloadDecoderN)r0   r1   r2   r   ObjectDescriptorr3   r(   r(   r(   r)   r)    r   r)  c                   @   r   )GeneralizedTimePayloadDecoderN)r0   r1   r2   r   GeneralizedTimer3   r(   r(   r(   r)   r+    r   r+  c                   @   r   )UTCTimePayloadDecoderN)r0   r1   r2   r   UTCTimer3   r(   r(   r(   r)   r-    r   r-  c                 C   s   g | ]}|qS r(   r(   ).0xr(   r(   r)   
<listcomp>  s    r1  r   )r   r   c                   @   sF   e Zd ZeZe ZdZeZe	Z	e
e
fddZdddeddfddZdS )SingleItemDecoderTc                 K   s8   |t ur|n| j| _|t ur|n| j| _i | _i | _d S r6   )r
   TAG_MAP_tagMapTYPE_MAP_typeMap	_tagCache_tagSetCache)r   r  typeMapignoredr(   r(   r)   __init__	  s   
zSingleItemDecoder.__init__Nc           #      k   sh   | dd}	trtdtj|||f  |	rA| jrAt|d|D ]
}
t|
tr)|
V  q|
tkr:tr4td t	j
V  d S |dtj | j}| j}| j}| j}t}| |_|tur |tu rd}t|d|D ]
}t|trp|V  qft|}z|| }W n` ty   |}|d	@ }|d
@ }|d@ }|dkrd}d}d}	 t|d|D ]
}t|tr|V  q|stdt|}|d7 }|dK }||d@ O }|d@ snqtj|||d}|r|||< Y nw |d u r|rz|| }W n ty   td|}|||< Y nw td|}n|| }t}trtd|  |tu rt|d|D ]}t|tr)|V  qt|}|dk r7|}nL|dkr|d@ }t|||D ]}t|trQ|V  qFt|}t||krjtd|t||f d}|D ]}|dK }|t |O }qn|d7 }nd}|dkr| jst!dt"}trtd|  |t"u r|d u rt#}nt$}|t#u rz|| }W n ty   d }Y nw |rt%}nz
||d d  }W n ty   d }Y nw |rt%}nt&}trtd|r|j'j(pd|t%u rdpdf  tj)|d u rdp|j*j'j( |t$u r |j't+j,u rz|| }W n ty2   d }Y nw tr~td |j-. D ]\}}td||j'j(f  q?|j/rltd  |j/. D ]\}}td||j'j(f  q\td!|d u rvdpy|0 |f  n||j1ks||j2v r|}trtd"|j'j(  nd }|d urz||j3 }trtd#|j3f  W n1 ty   t|j1j4|j1j4} z||  }trtd$| f  W n ty   d }Y nw Y nw |r|}t%}nt&}nd }t&}tr td%|t%u r|j'j(pd|t%u rdpdf  tj)|d u rdp|j'j( |t%u r|5d&ds3|s3d'd( }| }!|dkrY|j6||||t"| |fi |D ]}t|trV|V  qKnB|j7||||t"| |fi |D ]}t|trs|V  qh| |! }"|s|"|krt!d)|"|f |r|"|krt!d*|"|f trtd+|j'j(|j'j(t|t8j9r|: p|f  t}nf|t&u r|r|d j;tj<kr|d j=tj>krt?}t%}nd }| j@}trtd,|r|j'j(pd|t%u rdpd-f  |tAu r| jB}trtd.|j'j(  t%}|tCu rt!d/||f |tusYtr/tj   td0tj  |V  d S )1NrD   FzWdecoder called at scope %s with state %d, working with up to %s octets of substrate: %sr{   zend-of-octets sentinel foundTrS   r          r   z'Short octet stream on long tag decodingrW   ru   rt   )tagClassrL   tagIdr(   z$tag decoded into %s, decoding lengthz%s<%s at %sr   r   z6Indefinite length encoding not supported by this codeczvalue length decoded into %dz/codec %s chosen by a built-in type, decoding %sz<none>r?   zas explicit tag?z!candidate ASN.1 spec is a map of:z
  %s -> %szbut neither of: z,new candidate ASN.1 spec is %s, chosen by %szcandidate ASN.1 spec is %sz8value decoder chosen for an ambiguous type by type ID %szvalue decoder chosen by base %sz*codec %s chosen by ASN.1 spec, decoding %srecursiveFlagc                 s   s    | V  dS )ae  Legacy hack to keep the recursiveFlag=False option supported.

                        The decode(..., substrateFun=userCallback) option was introduced in 0.1.4 as a generalization
                        of the old recursiveFlag=False option. Users should pass their callback instead of using
                        recursiveFlag.
                        Nr(   )r-   
_substrate_length_optionsr(   r(   r)   r&     s   
z0SingleItemDecoder.__call__.<locals>.substrateFunz%Read %s bytes instead of expected %s.z(Read %s bytes are more than expected %s.z&codec %s yields type %s, value:
%s
...zcodec %s chosen, decoding %sz
as failurezcodec %s chosen, decoding valuez%s not in asn1Spec: %rz%decoder left scope %s, call completed)Dpoprd   r   scopesupportIndefLengthr	   rN   rO   EOO_SENTINELr   rE   r  r  SEEK_CURr4  r6  r7  r8  r=   re   r  stStopstDecodeTagr`   r   r   r   Tagr   stDecodeLengthlistr}   r   r   stGetValueDecoderstGetValueDecoderByTagstGetValueDecoderByAsn1SpecstDecodeValuestTryAsExplicitTagrc   r0   pushr3   r   r
  presentTypesr   	skipTypesprettyPrintTyper"   r  r   r   r;   r,   r*   r   Asn1ItemprettyPrintrL   r   r?  tagClassUniversalrawPayloadDecoderdefaultErrorStatestDumpRawValuedefaultRawDecoderstErrorCondition)#r   r    r!   r"   r#   r$   r%   r&   r'   rD   eoo_candidater  r9  tagCachetagSetCacher?   
isShortTag	firstByte
firstOctetlastTag
integerTagr?  rL   r@  lengthOctetIdxintegerBytesizeencodedLengthlengthOctetconcreteDecoder
chosenSpecr   
baseTagSetrn   	bytesReadr(   r(   r)   __call__  s  







%







0"
$

4 
	





0


    /

zSingleItemDecoder.__call__)r0   r1   r2   r`  r]  r	  r_  rH  r3  r5  r
   r;  rL  rr  r(   r(   r(   r)   r2    s    r2  c                   @   s&   e Zd ZdZeZdddZdd ZdS )r   a	  Create an iterator that turns BER/CER/DER byte stream into ASN.1 objects.

    On each iteration, consume whatever BER/CER/DER serialization is
    available in the `substrate` stream-like object and turns it into
    one or more, possibly nested, ASN.1 objects.

    Parameters
    ----------
    substrate: :py:class:`file`, :py:class:`io.BytesIO`
        BER/CER/DER serialization in form of a byte stream

    Keyword Args
    ------------
    asn1Spec: :py:class:`~pyasn1.type.base.PyAsn1Item`
        A pyasn1 type object to act as a template guiding the decoder.
        Depending on the ASN.1 structure being decoded, `asn1Spec` may
        or may not be required. One of the reasons why `asn1Spec` may
        me required is that ASN.1 structure is encoded in the *IMPLICIT*
        tagging mode.

    Yields
    ------
    : :py:class:`~pyasn1.type.base.PyAsn1Item`, :py:class:`~pyasn1.error.SubstrateUnderrunError`
        Decoded ASN.1 object (possibly, nested) or
        :py:class:`~pyasn1.error.SubstrateUnderrunError` object indicating
        insufficient BER/CER/DER serialization on input to fully recover ASN.1
        objects from it.
        
        In the latter case the caller is advised to ensure some more data in
        the input stream, then call the iterator again. The decoder will resume
        the decoding process using the newly arrived data.

        The `context` property of :py:class:`~pyasn1.error.SubstrateUnderrunError`
        object might hold a reference to the partially populated ASN.1 object
        being reconstructed.

    Raises
    ------
    ~pyasn1.error.PyAsn1Error, ~pyasn1.error.EndOfStreamError
        `PyAsn1Error` on deserialization error, `EndOfStreamError` on
         premature stream closure.

    Examples
    --------
    Decode BER serialisation without ASN.1 schema

    .. code-block:: pycon

        >>> stream = io.BytesIO(
        ...    b'0	')
        >>>
        >>> for asn1Object in StreamingDecoder(stream):
        ...     print(asn1Object)
        >>>
        SequenceOf:
         1 2 3

    Decode BER serialisation with ASN.1 schema

    .. code-block:: pycon

        >>> stream = io.BytesIO(
        ...    b'0	')
        >>>
        >>> schema = SequenceOf(componentType=Integer())
        >>>
        >>> decoder = StreamingDecoder(stream, asn1Spec=schema)
        >>> for asn1Object in decoder:
        ...     print(asn1Object)
        >>>
        SequenceOf:
         1 2 3
    Nc                 K   s,   | j di || _t|| _|| _|| _d S )Nr(   )SINGLE_ITEM_DECODER_singleItemDecoderr   rC  	_asn1SpecrE  )r   r    r!   r'   r(   r(   r)   r;    s   

zStreamingDecoder.__init__c                 c   sT    	 | j | j| jfi | jD ]}|V  qt| jD ]
}t|tr$d V   |r)d S qr6   )rt  rC  ru  rE  r   rN   rO   )r   r-   r7   r(   r(   r)   __iter__  s   

zStreamingDecoder.__iter__r6   )r0   r1   r2   __doc__r2  rs  r;  rv  r(   r(   r(   r)   r   j  s
    J
r   c                   @   s.   e Zd ZdZeZedddZedd Z	dS )r   znCreate a BER decoder object.

    Parse BER/CER/DER octet-stream into one, possibly nested, ASN.1 object.
    Nc              	      s   t |}d|v r|d  d fdd	}||d< | j||fi |}|D ]&}t|tr/tdztt|}W n tjyC   t}Y nw ||f  S dS )a  Turns BER/CER/DER octet stream into an ASN.1 object.

        Takes BER/CER/DER octet-stream in form of :py:class:`bytes` (Python 3)
        or :py:class:`str` (Python 2) and decode it into an ASN.1 object
        (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
        may be a scalar or an arbitrary nested structure.

        Parameters
        ----------
        substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
            BER/CER/DER octet-stream to parse

        Keyword Args
        ------------
        asn1Spec: :py:class:`~pyasn1.type.base.PyAsn1Item`
            A pyasn1 type object (:py:class:`~pyasn1.type.base.PyAsn1Item`
            derivative) to act as a template guiding the decoder.
            Depending on the ASN.1 structure being decoded, `asn1Spec` may or
            may not be required. Most common reason for it to require is that
            ASN.1 structure is encoded in *IMPLICIT* tagging mode.

        substrateFun: :py:class:`Union[
                Callable[[pyasn1.type.base.PyAsn1Item, bytes, int],
                         Tuple[pyasn1.type.base.PyAsn1Item, bytes]],
                Callable[[pyasn1.type.base.PyAsn1Item, io.BytesIO, int, dict],
                         Generator[Union[pyasn1.type.base.PyAsn1Item,
                                         pyasn1.error.SubstrateUnderrunError],
                                   None, None]]
            ]`
            User callback meant to generalize special use cases like non-recursive or
            partial decoding. A 3-arg non-streaming variant is supported for backwards
            compatiblilty in addition to the newer 4-arg streaming variant.
            The callback will receive the uninitialized object recovered from substrate
            as 1st argument, the uninterpreted payload as 2nd argument, and the length
            of the uninterpreted payload as 3rd argument. The streaming variant will
            additionally receive the decode(..., **options) kwargs as 4th argument.
            The non-streaming variant shall return an object that will be propagated
            as decode() return value as 1st item, and the remainig payload for further
            decode passes as 2nd item.
            The streaming variant shall yield an object that will be propagated as
            decode() return value, and leave the remaining payload in the stream.

        Returns
        -------
        : :py:class:`tuple`
            A tuple of :py:class:`~pyasn1.type.base.PyAsn1Item` object
            recovered from BER/CER/DER substrate and the unprocessed trailing
            portion of the `substrate` (may be empty)

        Raises
        ------
        : :py:class:`~pyasn1.error.PyAsn1Error`
            :py:class:`~pyasn1.error.SubstrateUnderrunError` on insufficient
            input or :py:class:`~pyasn1.error.PyAsn1Error` on decoding error.

        Examples
        --------
        Decode BER/CER/DER serialisation without ASN.1 schema

        .. code-block:: pycon

           >>> s, unprocessed = decode(b'0	')
           >>> str(s)
           SequenceOf:
            1 2 3

        Decode BER/CER/DER serialisation with ASN.1 schema

        .. code-block:: pycon

           >>> seq = SequenceOf(componentType=Integer())
           >>> s, unprocessed = decode(
                b'0	', asn1Spec=seq)
           >>> str(s)
           SequenceOf:
            1 2 3

        r&   Nc           	   	   3   sr    z	 | |||}W n$ t y.   t \}}}|jr t|tjs$J t | ||}Y nw |D ]}|V  q1dS )ar  Support both 0.4 and 0.5 style APIs.

                substrateFun API has changed in 0.5 for use with streaming decoders. To stay backwards compatible,
                we first try if we received a streaming user callback. If that fails,we assume we've received a
                non-streaming v0.4 user callback and convert it for streaming on the fly
                N)		TypeErrorsysexc_infotb_nextrN   ioBytesIOr   _callSubstrateFunV4asV5)	r-   r    r#   r'   substrate_gen_type_value	tracebackr?   origSubstrateFunr(   r)   substrateFunWrapper)  s   z-Decoder.__call__.<locals>.substrateFunWrapperzShort substrate on inputr6   )	r   STREAMING_DECODERrN   rO   r   nextr	   EndOfStreamErrorr   )clsr    r!   r'   r  streamingDecoderr-   tailr(   r  r)   rr    s(   P

zDecoder.__call__c                 c   sV    |  }|dkrt|}| |||\}}||}|  || tj |V  d S )Nr   )readr}   writetruncater  r  rJ  )substrateFunV4r-   r    r#   substrate_bytesr?   nextSubstratenbytesr(   r(   r)   r~  N  s   

zDecoder._callSubstrateFunV4asV5r6   )
r0   r1   r2   rw  r   r  classmethodrr  r4   r~  r(   r(   r(   r)   r     s    yr   )r|  r  ry  pyasn1r   r   pyasn1.codec.berr   pyasn1.codec.streamingr   r   r   r	   pyasn1.compatr
   pyasn1.compat.integerr   pyasn1.compat.octetsr   r   r   r   pyasn1.errorr   pyasn1.typer   r   r   r   r   r   __all__registerLoggeer0   DEBUG_DECODERrd   r=   rO   objectr   r5   rA   r\  rI   rR   rV   rl   rp   rr   r   r   r   r   r   r   r   r   r   r   r   r	  r  r  r  r  r  r  r  r!  r#  r%  r'  r)  r+  r-  rP   r"   rT   rk   ro   rq   r   r   
Enumeratedr   r   r   r  r  r  r  r  r  r  r   r"  r$  r&  r(  r*  r,  r.  r3  r   r   r   rG   r5  r  r9  valuestypeDecoderr3   rc   rangerL  rN  rP  rR  rQ  rT  rS  r^  r`  rK  rI  r2  r   r   r   r(   r(   r(   r)   <module>   s   ) V<2n    xy








	


















!






  mc 
D