o
    Œõ±jÓ#  ã                   @   sŠ   d Z ddlmZmZmZmZ ddlZddlmZm	Z	m
Z
 ddlmZmZ ejdkZdZd	Zd
d„ Zddd„Zdd„ Zddd„Zdd„ ZdS )zÈ
Functions for parsing and dumping using the ASN.1 DER encoding. Exports the
following items:

 - emit()
 - parse()
 - peek()

Other type classes are defined that help compose the types listed above.
é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )Úbyte_clsÚchr_clsÚ	type_name)Úint_from_bytesÚint_to_bytes)é   z<Insufficient data - %s bytes requested but only %s availableé
   c                 C   sÆ   t | tƒstdt| ƒ ƒ‚| dk s| dkrtd|  ƒ‚t |tƒs(tdt|ƒ ƒ‚|dk s0|dkr6td| ƒ‚t |tƒsCtdt|ƒ ƒ‚|dk rMtd	| ƒ‚t |tƒsZtd
t|ƒ ƒ‚t| |||ƒ| S )a™  
    Constructs a byte string of an ASN.1 DER-encoded value

    This is typically not useful. Instead, use one of the standard classes from
    asn1crypto.core, or construct a new class with specific fields, and call the
    .dump() method.

    :param class_:
        An integer ASN.1 class value: 0 (universal), 1 (application),
        2 (context), 3 (private)

    :param method:
        An integer ASN.1 method value: 0 (primitive), 1 (constructed)

    :param tag:
        An integer ASN.1 tag value

    :param contents:
        A byte string of the encoded byte contents

    :return:
        A byte string of the ASN.1 DER value (header and contents)
    z!class_ must be an integer, not %sr   r   z*class_ must be one of 0, 1, 2 or 3, not %sz!method must be an integer, not %sr   zmethod must be 0 or 1, not %sztag must be an integer, not %sz%tag must be greater than zero, not %sú&contents must be a byte string, not %s)Ú
isinstanceÚintÚ	TypeErrorr	   Ú
ValueErrorr   Ú_dump_header)Úclass_ÚmethodÚtagÚcontents© r   ú„/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asn1crypto/parser.pyÚemit   s   



r   Fc                 C   sP   t | tƒstdt| ƒ ƒ‚t| ƒ}t| |ƒ\}}|r&||kr&td||  ƒ‚|S )al  
    Parses a byte string of ASN.1 BER/DER-encoded data.

    This is typically not useful. Instead, use one of the standard classes from
    asn1crypto.core, or construct a new class with specific fields, and call the
    .load() class method.

    :param contents:
        A byte string of BER/DER-encoded data

    :param strict:
        A boolean indicating if trailing data should be forbidden - if so, a
        ValueError will be raised when trailing data exists

    :raises:
        ValueError - when the contents do not contain an ASN.1 header or are truncated in some way
        TypeError - when contents is not a byte string

    :return:
        A 6-element tuple:
         - 0: integer class (0 to 3)
         - 1: integer method
         - 2: integer tag
         - 3: byte string header
         - 4: byte string content
         - 5: byte string trailer
    r   z4Extra data - %d bytes of trailing data were provided)r   r   r   r	   ÚlenÚ_parser   )r   ÚstrictÚcontents_lenÚinfoÚconsumedr   r   r   ÚparseK   s   
r!   c                 C   s0   t | tƒstdt| ƒ ƒ‚t| t| ƒƒ\}}|S )aW  
    Parses a byte string of ASN.1 BER/DER-encoded data to find the length

    This is typically used to look into an encoded value to see how long the
    next chunk of ASN.1-encoded data is. Primarily it is useful when a
    value is a concatenation of multiple values.

    :param contents:
        A byte string of BER/DER-encoded data

    :raises:
        ValueError - when the contents do not contain an ASN.1 header or are truncated in some way
        TypeError - when contents is not a byte string

    :return:
        An integer with the number of bytes occupied by the ASN.1 value
    r   )r   r   r   r	   r   r   )r   r   r    r   r   r   Úpeekr   s   
r"   c              	   C   sŠ  |t krtdƒ‚|}||d k rttd|| f ƒ‚tr"t| | ƒn| | }|d7 }|d@ }|d? d@ }|dkr�d}	 ||d k rKttd|| f ƒ‚trSt| | ƒn| | }	|d7 }|	dkrg|dkrgtdƒ‚|d9 }||	d	@ 7 }|	d
? dkrxnq;|dk r�tdƒ‚||d k r‘ttd|| f ƒ‚tr™t| | ƒn| | }
|d7 }d}|
d
? dkr°||
d	@  }ne|
d	@ }|rÙ||| k rÆtt||| f ƒ‚||7 }|t| || |… dd� }n<|sßtdƒ‚|}||d k sò| ||d … dk�rt| ||d|d d�\}}||d k sò| ||d … dksò|d7 }d}||k�r&tt|| || f ƒ‚|�r-||fS |d? ||| ||… | ||t|ƒ … |f|fS )aø  
    Parses a byte string into component parts

    :param encoded_data:
        A byte string that contains BER-encoded data

    :param data_len:
        The integer length of the encoded data

    :param pointer:
        The index in the byte string to parse from

    :param lengths_only:
        A boolean to cause the call to return a 2-element tuple of the integer
        number of bytes in the header and the integer number of bytes in the
        contents. Internal use only.

    :param depth:
        The recursion depth when evaluating indefinite-length encoding.

    :return:
        A 2-element tuple:
         - 0: A tuple of (class_, method, tag, header, content, trailer)
         - 1: An integer indicating how many bytes were consumed
    z*Indefinite-length recursion limit exceededr   é   é   r   Té€   zNon-minimal tag encodingé   é   ó    F)Úsignedz-Indefinite-length element must be constructedé   s     )Úlengths_onlyÚdepthé   )Ú
_MAX_DEPTHr   Ú_INSUFFICIENT_DATA_MESSAGEÚ_PY2Úordr
   r   r   )Úencoded_dataÚdata_lenÚpointerr+   r,   ÚstartÚfirst_octetr   ÚconstructedÚnumÚlength_octetÚtrailerÚcontents_endÚlength_octetsÚ_r   r   r   r   Œ   sv   ö" ÿ

ú÷r   c           	      C   sÊ   d}d}|| d> O }||d> O }|dkr9d}|dkr0t ||d@ B ƒ| }|s(d}|d? }|dkst |dB ƒ| }n|t ||B ƒ7 }t|ƒ}|dkrQ|t |ƒ7 }|S t|ƒ}|t dt|ƒB ƒ7 }||7 }|S )	aÇ  
    Constructs the header bytes for an ASN.1 object

    :param class_:
        An integer ASN.1 class value: 0 (universal), 1 (application),
        2 (context), 3 (private)

    :param method:
        An integer ASN.1 method value: 0 (primitive), 1 (constructed)

    :param tag:
        An integer ASN.1 tag value

    :param contents:
        A byte string of the encoded byte contents

    :return:
        A byte string of the ASN.1 DER header
    r(   r   r-   r$   r#   r&   r%   r'   )r   r   r   )	r   r   r   r   ÚheaderÚid_numÚcont_bitÚlengthÚlength_bytesr   r   r   r   ö   s,   üür   )F)r   Fr   )Ú__doc__Ú
__future__r   r   r   r   ÚsysÚ_typesr   r   r	   Úutilr
   r   Úversion_infor0   r/   r.   r   r!   r"   r   r   r   r   r   r   Ú<module>   s   

1'
j